xref: /freebsd/sys/dev/re/if_re.c (revision 0ce0868acd6d23ed9fb41b58d6f9508b6271935f)
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>
126ed510fb0SBill Paul #include <sys/taskqueue.h>
127a94100faSBill Paul 
128a94100faSBill Paul #include <net/if.h>
129a94100faSBill Paul #include <net/if_arp.h>
130a94100faSBill Paul #include <net/ethernet.h>
131a94100faSBill Paul #include <net/if_dl.h>
132a94100faSBill Paul #include <net/if_media.h>
133fc74a9f9SBrooks Davis #include <net/if_types.h>
134a94100faSBill Paul #include <net/if_vlan_var.h>
135a94100faSBill Paul 
136a94100faSBill Paul #include <net/bpf.h>
137a94100faSBill Paul 
138a94100faSBill Paul #include <machine/bus.h>
139a94100faSBill Paul #include <machine/resource.h>
140a94100faSBill Paul #include <sys/bus.h>
141a94100faSBill Paul #include <sys/rman.h>
142a94100faSBill Paul 
143a94100faSBill Paul #include <dev/mii/mii.h>
144a94100faSBill Paul #include <dev/mii/miivar.h>
145a94100faSBill Paul 
146a94100faSBill Paul #include <dev/pci/pcireg.h>
147a94100faSBill Paul #include <dev/pci/pcivar.h>
148a94100faSBill Paul 
149d65abd66SPyun YongHyeon #include <pci/if_rlreg.h>
150d65abd66SPyun YongHyeon 
151a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
152a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
153a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
154a94100faSBill Paul 
155298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
156a94100faSBill Paul #include "miibus_if.h"
157a94100faSBill Paul 
1585774c5ffSPyun YongHyeon /* Tunables. */
1592000cf6cSPyun YongHyeon static int msi_disable = 1;
1605774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1615774c5ffSPyun YongHyeon 
162a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
163a94100faSBill Paul 
164a94100faSBill Paul /*
165a94100faSBill Paul  * Various supported device vendors/types and their names.
166a94100faSBill Paul  */
167a94100faSBill Paul static struct rl_type re_devs[] = {
1689dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
16932aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1709dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
171a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1729dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
173b1d62f0fSPyun YongHyeon 	    "RealTek 8101E/8102E/8102EL PCIe 10/100baseTX" },
1749dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
175deb5c680SPyun YongHyeon 	    "RealTek 8168/8168B/8168C/8168CP/8111B/8111C/8111CP PCIe "
176deb5c680SPyun YongHyeon 	    "Gigabit Ethernet" },
1779dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
178715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1802ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1819dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
182ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
18426390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
186dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
187a94100faSBill Paul };
188a94100faSBill Paul 
189a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
190a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
191a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
192a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
193a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
194a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
195a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
196a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
197a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
198498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
199a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
20069a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
20169a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
202ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
203ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
204715922d7SPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL"},
205a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
206a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
207ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
208ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
209b1d62f0fSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E"},
210b1d62f0fSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL"},
211498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2121acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
213deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C"},
214deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C"},
215deb5c680SPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP"},
216a94100faSBill Paul 	{ 0, 0, NULL }
217a94100faSBill Paul };
218a94100faSBill Paul 
219a94100faSBill Paul static int re_probe		(device_t);
220a94100faSBill Paul static int re_attach		(device_t);
221a94100faSBill Paul static int re_detach		(device_t);
222a94100faSBill Paul 
223d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
224a94100faSBill Paul 
225a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
226a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
227d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
228d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
229d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
230a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
231a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23322a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23422a11c96SJohn-Mark Gurney 				(struct mbuf *);
23522a11c96SJohn-Mark Gurney #endif
236ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
237a94100faSBill Paul static void re_txeof		(struct rl_softc *);
23897b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2390187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2400187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
24197b9d4baSJohn-Mark Gurney #endif
242ef544f63SPaolo Pisati static int re_intr		(void *);
243a94100faSBill Paul static void re_tick		(void *);
244ed510fb0SBill Paul static void re_tx_task		(void *, int);
245ed510fb0SBill Paul static void re_int_task		(void *, int);
246a94100faSBill Paul static void re_start		(struct ifnet *);
247a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
248a94100faSBill Paul static void re_init		(void *);
24997b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
250a94100faSBill Paul static void re_stop		(struct rl_softc *);
2511d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
252a94100faSBill Paul static int re_suspend		(device_t);
253a94100faSBill Paul static int re_resume		(device_t);
2546a087a87SPyun YongHyeon static int re_shutdown		(device_t);
255a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
256a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
257a94100faSBill Paul 
258a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
259a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
260ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
261a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
262a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
263a94100faSBill Paul 
264a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
265a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
266a94100faSBill Paul static void re_miibus_statchg	(device_t);
267a94100faSBill Paul 
268a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
269a94100faSBill Paul static void re_reset		(struct rl_softc *);
2707467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2717467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
272a94100faSBill Paul 
273ed510fb0SBill Paul #ifdef RE_DIAG
274a94100faSBill Paul static int re_diag		(struct rl_softc *);
275ed510fb0SBill Paul #endif
276a94100faSBill Paul 
277a94100faSBill Paul static device_method_t re_methods[] = {
278a94100faSBill Paul 	/* Device interface */
279a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
280a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
281a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
282a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
283a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
284a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
285a94100faSBill Paul 
286a94100faSBill Paul 	/* bus interface */
287a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
288a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
289a94100faSBill Paul 
290a94100faSBill Paul 	/* MII interface */
291a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
292a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
293a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
294a94100faSBill Paul 
295a94100faSBill Paul 	{ 0, 0 }
296a94100faSBill Paul };
297a94100faSBill Paul 
298a94100faSBill Paul static driver_t re_driver = {
299a94100faSBill Paul 	"re",
300a94100faSBill Paul 	re_methods,
301a94100faSBill Paul 	sizeof(struct rl_softc)
302a94100faSBill Paul };
303a94100faSBill Paul 
304a94100faSBill Paul static devclass_t re_devclass;
305a94100faSBill Paul 
306a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
307347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
308a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
309a94100faSBill Paul 
310a94100faSBill Paul #define EE_SET(x)					\
311a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
312a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
313a94100faSBill Paul 
314a94100faSBill Paul #define EE_CLR(x)					\
315a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
316a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
317a94100faSBill Paul 
318a94100faSBill Paul /*
319a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
320a94100faSBill Paul  */
321a94100faSBill Paul static void
3227b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
323a94100faSBill Paul {
3240ce0868aSPyun YongHyeon 	int			d, i;
325a94100faSBill Paul 
326ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
327a94100faSBill Paul 
328a94100faSBill Paul 	/*
329a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
330a94100faSBill Paul 	 */
331ed510fb0SBill Paul 
332ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
333a94100faSBill Paul 		if (d & i) {
334a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
335a94100faSBill Paul 		} else {
336a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
337a94100faSBill Paul 		}
338a94100faSBill Paul 		DELAY(100);
339a94100faSBill Paul 		EE_SET(RL_EE_CLK);
340a94100faSBill Paul 		DELAY(150);
341a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
342a94100faSBill Paul 		DELAY(100);
343a94100faSBill Paul 	}
344ed510fb0SBill Paul 
345ed510fb0SBill Paul 	return;
346a94100faSBill Paul }
347a94100faSBill Paul 
348a94100faSBill Paul /*
349a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
350a94100faSBill Paul  */
351a94100faSBill Paul static void
3527b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
353a94100faSBill Paul {
3540ce0868aSPyun YongHyeon 	int			i;
355a94100faSBill Paul 	u_int16_t		word = 0;
356a94100faSBill Paul 
357a94100faSBill Paul 	/*
358a94100faSBill Paul 	 * Send address of word we want to read.
359a94100faSBill Paul 	 */
360a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
361a94100faSBill Paul 
362a94100faSBill Paul 	/*
363a94100faSBill Paul 	 * Start reading bits from EEPROM.
364a94100faSBill Paul 	 */
365a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
366a94100faSBill Paul 		EE_SET(RL_EE_CLK);
367a94100faSBill Paul 		DELAY(100);
368a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
369a94100faSBill Paul 			word |= i;
370a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
371a94100faSBill Paul 		DELAY(100);
372a94100faSBill Paul 	}
373a94100faSBill Paul 
374a94100faSBill Paul 	*dest = word;
375ed510fb0SBill Paul 
376ed510fb0SBill Paul 	return;
377a94100faSBill Paul }
378a94100faSBill Paul 
379a94100faSBill Paul /*
380a94100faSBill Paul  * Read a sequence of words from the EEPROM.
381a94100faSBill Paul  */
382a94100faSBill Paul static void
3837b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
384a94100faSBill Paul {
385a94100faSBill Paul 	int			i;
386a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
387a94100faSBill Paul 
388ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
389ed510fb0SBill Paul 
390ed510fb0SBill Paul         DELAY(100);
391ed510fb0SBill Paul 
392a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
393ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
394a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
395ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
396a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
397be099007SPyun YongHyeon                 *ptr = word;
398a94100faSBill Paul 	}
399ed510fb0SBill Paul 
400ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
401ed510fb0SBill Paul 
402ed510fb0SBill Paul 	return;
403a94100faSBill Paul }
404a94100faSBill Paul 
405a94100faSBill Paul static int
4067b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
407a94100faSBill Paul {
408a94100faSBill Paul 	struct rl_softc		*sc;
409a94100faSBill Paul 	u_int32_t		rval;
410a94100faSBill Paul 	int			i;
411a94100faSBill Paul 
412a94100faSBill Paul 	if (phy != 1)
413a94100faSBill Paul 		return (0);
414a94100faSBill Paul 
415a94100faSBill Paul 	sc = device_get_softc(dev);
416a94100faSBill Paul 
4179bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4189bac70b8SBill Paul 
4199bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4209bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4219bac70b8SBill Paul 		return (rval);
4229bac70b8SBill Paul 	}
4239bac70b8SBill Paul 
424a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
425a94100faSBill Paul 	DELAY(1000);
426a94100faSBill Paul 
427a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
428a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
429a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
430a94100faSBill Paul 			break;
431a94100faSBill Paul 		DELAY(100);
432a94100faSBill Paul 	}
433a94100faSBill Paul 
434a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4356b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
436a94100faSBill Paul 		return (0);
437a94100faSBill Paul 	}
438a94100faSBill Paul 
439a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
440a94100faSBill Paul }
441a94100faSBill Paul 
442a94100faSBill Paul static int
4437b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
444a94100faSBill Paul {
445a94100faSBill Paul 	struct rl_softc		*sc;
446a94100faSBill Paul 	u_int32_t		rval;
447a94100faSBill Paul 	int			i;
448a94100faSBill Paul 
449a94100faSBill Paul 	sc = device_get_softc(dev);
450a94100faSBill Paul 
451a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4529bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
453a94100faSBill Paul 	DELAY(1000);
454a94100faSBill Paul 
455a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
456a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
457a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
458a94100faSBill Paul 			break;
459a94100faSBill Paul 		DELAY(100);
460a94100faSBill Paul 	}
461a94100faSBill Paul 
462a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4636b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
464a94100faSBill Paul 		return (0);
465a94100faSBill Paul 	}
466a94100faSBill Paul 
467a94100faSBill Paul 	return (0);
468a94100faSBill Paul }
469a94100faSBill Paul 
470a94100faSBill Paul static int
4717b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
472a94100faSBill Paul {
473a94100faSBill Paul 	struct rl_softc		*sc;
474a94100faSBill Paul 	u_int16_t		rval = 0;
475a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
476a94100faSBill Paul 
477a94100faSBill Paul 	sc = device_get_softc(dev);
478a94100faSBill Paul 
479a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
480a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
481a94100faSBill Paul 		return (rval);
482a94100faSBill Paul 	}
483a94100faSBill Paul 
484a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
485a94100faSBill Paul 	if (phy) {
486a94100faSBill Paul 		return (0);
487a94100faSBill Paul 	}
488a94100faSBill Paul 	switch (reg) {
489a94100faSBill Paul 	case MII_BMCR:
490a94100faSBill Paul 		re8139_reg = RL_BMCR;
491a94100faSBill Paul 		break;
492a94100faSBill Paul 	case MII_BMSR:
493a94100faSBill Paul 		re8139_reg = RL_BMSR;
494a94100faSBill Paul 		break;
495a94100faSBill Paul 	case MII_ANAR:
496a94100faSBill Paul 		re8139_reg = RL_ANAR;
497a94100faSBill Paul 		break;
498a94100faSBill Paul 	case MII_ANER:
499a94100faSBill Paul 		re8139_reg = RL_ANER;
500a94100faSBill Paul 		break;
501a94100faSBill Paul 	case MII_ANLPAR:
502a94100faSBill Paul 		re8139_reg = RL_LPAR;
503a94100faSBill Paul 		break;
504a94100faSBill Paul 	case MII_PHYIDR1:
505a94100faSBill Paul 	case MII_PHYIDR2:
506a94100faSBill Paul 		return (0);
507a94100faSBill Paul 	/*
508a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
509a94100faSBill Paul 	 * register. If we have a link partner which does not
510a94100faSBill Paul 	 * support NWAY, this is the register which will tell
511a94100faSBill Paul 	 * us the results of parallel detection.
512a94100faSBill Paul 	 */
513a94100faSBill Paul 	case RL_MEDIASTAT:
514a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
515a94100faSBill Paul 		return (rval);
516a94100faSBill Paul 	default:
5176b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
518a94100faSBill Paul 		return (0);
519a94100faSBill Paul 	}
520a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
521baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
522baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
523baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
524baa12772SPyun YongHyeon 	}
525a94100faSBill Paul 	return (rval);
526a94100faSBill Paul }
527a94100faSBill Paul 
528a94100faSBill Paul static int
5297b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
530a94100faSBill Paul {
531a94100faSBill Paul 	struct rl_softc		*sc;
532a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
533a94100faSBill Paul 	int			rval = 0;
534a94100faSBill Paul 
535a94100faSBill Paul 	sc = device_get_softc(dev);
536a94100faSBill Paul 
537a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
538a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
539a94100faSBill Paul 		return (rval);
540a94100faSBill Paul 	}
541a94100faSBill Paul 
542a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
54397b9d4baSJohn-Mark Gurney 	if (phy)
544a94100faSBill Paul 		return (0);
54597b9d4baSJohn-Mark Gurney 
546a94100faSBill Paul 	switch (reg) {
547a94100faSBill Paul 	case MII_BMCR:
548a94100faSBill Paul 		re8139_reg = RL_BMCR;
549baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
550baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
551baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
552baa12772SPyun YongHyeon 		}
553a94100faSBill Paul 		break;
554a94100faSBill Paul 	case MII_BMSR:
555a94100faSBill Paul 		re8139_reg = RL_BMSR;
556a94100faSBill Paul 		break;
557a94100faSBill Paul 	case MII_ANAR:
558a94100faSBill Paul 		re8139_reg = RL_ANAR;
559a94100faSBill Paul 		break;
560a94100faSBill Paul 	case MII_ANER:
561a94100faSBill Paul 		re8139_reg = RL_ANER;
562a94100faSBill Paul 		break;
563a94100faSBill Paul 	case MII_ANLPAR:
564a94100faSBill Paul 		re8139_reg = RL_LPAR;
565a94100faSBill Paul 		break;
566a94100faSBill Paul 	case MII_PHYIDR1:
567a94100faSBill Paul 	case MII_PHYIDR2:
568a94100faSBill Paul 		return (0);
569a94100faSBill Paul 		break;
570a94100faSBill Paul 	default:
5716b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
572a94100faSBill Paul 		return (0);
573a94100faSBill Paul 	}
574a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
575a94100faSBill Paul 	return (0);
576a94100faSBill Paul }
577a94100faSBill Paul 
578a94100faSBill Paul static void
5797b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
580a94100faSBill Paul {
581a11e2f18SBruce M Simpson 
582a94100faSBill Paul }
583a94100faSBill Paul 
584a94100faSBill Paul /*
585a94100faSBill Paul  * Program the 64-bit multicast hash filter.
586a94100faSBill Paul  */
587a94100faSBill Paul static void
5887b5ffebfSPyun YongHyeon re_setmulti(struct rl_softc *sc)
589a94100faSBill Paul {
590a94100faSBill Paul 	struct ifnet		*ifp;
591a94100faSBill Paul 	int			h = 0;
592a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
593a94100faSBill Paul 	struct ifmultiaddr	*ifma;
594a94100faSBill Paul 	u_int32_t		rxfilt;
595a94100faSBill Paul 	int			mcnt = 0;
596a94100faSBill Paul 
59797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
59897b9d4baSJohn-Mark Gurney 
599fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
600a94100faSBill Paul 
601a94100faSBill Paul 
6027c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6037c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
604a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
6057c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6067c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
607a0637caaSPyun YongHyeon 		/*
608a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
609a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
610a0637caaSPyun YongHyeon 		 * promiscuous mode.
611a0637caaSPyun YongHyeon 		 */
612a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
613a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
614a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
615a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
616a94100faSBill Paul 		return;
617a94100faSBill Paul 	}
618a94100faSBill Paul 
619a94100faSBill Paul 	/* first, zot all the existing hash bits */
620a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
621a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
622a94100faSBill Paul 
623a94100faSBill Paul 	/* now program new ones */
62413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
625a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
626a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
627a94100faSBill Paul 			continue;
6280e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6290e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
630a94100faSBill Paul 		if (h < 32)
631a94100faSBill Paul 			hashes[0] |= (1 << h);
632a94100faSBill Paul 		else
633a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
634a94100faSBill Paul 		mcnt++;
635a94100faSBill Paul 	}
63613b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
637a94100faSBill Paul 
638a94100faSBill Paul 	if (mcnt)
639a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
640a94100faSBill Paul 	else
641a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
642a94100faSBill Paul 
643a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
644bb7dfefbSBill Paul 
645bb7dfefbSBill Paul 	/*
646bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
647bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
648bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
649bb7dfefbSBill Paul 	 * order for those devices.
650bb7dfefbSBill Paul 	 */
651bb7dfefbSBill Paul 
652351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_INVMAR) != 0) {
653bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
654bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
655351a76f9SPyun YongHyeon 	} else {
656a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
657a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
658a94100faSBill Paul 	}
659bb7dfefbSBill Paul }
660a94100faSBill Paul 
661a94100faSBill Paul static void
6627b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
663a94100faSBill Paul {
6640ce0868aSPyun YongHyeon 	int			i;
665a94100faSBill Paul 
66697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
66797b9d4baSJohn-Mark Gurney 
668a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
669a94100faSBill Paul 
670a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
671a94100faSBill Paul 		DELAY(10);
672a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
673a94100faSBill Paul 			break;
674a94100faSBill Paul 	}
675a94100faSBill Paul 	if (i == RL_TIMEOUT)
6766b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
677a94100faSBill Paul 
678a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
679a94100faSBill Paul }
680a94100faSBill Paul 
681ed510fb0SBill Paul #ifdef RE_DIAG
682ed510fb0SBill Paul 
683a94100faSBill Paul /*
684a94100faSBill Paul  * The following routine is designed to test for a defect on some
685a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
686a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
687a94100faSBill Paul  * should be pulled high. The result of this defect is that the
688a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
689a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
690a94100faSBill Paul  * because the 64-bit data lines aren't connected.
691a94100faSBill Paul  *
692a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
693a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
694a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
695a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
696a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
697a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
698a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
699a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
700a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
701a94100faSBill Paul  */
702a94100faSBill Paul 
703a94100faSBill Paul static int
7047b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
705a94100faSBill Paul {
706fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
707a94100faSBill Paul 	struct mbuf		*m0;
708a94100faSBill Paul 	struct ether_header	*eh;
709a94100faSBill Paul 	struct rl_desc		*cur_rx;
710a94100faSBill Paul 	u_int16_t		status;
711a94100faSBill Paul 	u_int32_t		rxstat;
712ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
713a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
714a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
715a94100faSBill Paul 
716a94100faSBill Paul 	/* Allocate a single mbuf */
717a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
718a94100faSBill Paul 	if (m0 == NULL)
719a94100faSBill Paul 		return (ENOBUFS);
720a94100faSBill Paul 
72197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
72297b9d4baSJohn-Mark Gurney 
723a94100faSBill Paul 	/*
724a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
725a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
726a94100faSBill Paul 	 * following special functions:
727a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
728a94100faSBill Paul 	 * - Enables digital loopback mode
729a94100faSBill Paul 	 * - Leaves interrupts turned off
730a94100faSBill Paul 	 */
731a94100faSBill Paul 
732a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
733a94100faSBill Paul 	sc->rl_testmode = 1;
734ed510fb0SBill Paul 	re_reset(sc);
73597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
736351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
737ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
738ed510fb0SBill Paul 		phyaddr = 1;
739ed510fb0SBill Paul 	else
740ed510fb0SBill Paul 		phyaddr = 0;
741ed510fb0SBill Paul 
742ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
743ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
744ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
745ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
746ed510fb0SBill Paul 			break;
747ed510fb0SBill Paul 	}
748ed510fb0SBill Paul 
749ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
750ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
751ed510fb0SBill Paul 
752804af9a1SBill Paul 	DELAY(100000);
753a94100faSBill Paul 
754a94100faSBill Paul 	/* Put some data in the mbuf */
755a94100faSBill Paul 
756a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
757a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
758a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
759a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
760a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
761a94100faSBill Paul 
7627cae6651SBill Paul 	/*
7637cae6651SBill Paul 	 * Queue the packet, start transmission.
7647cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7657cae6651SBill Paul 	 */
766a94100faSBill Paul 
767abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
76897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
76952732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7707cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
77197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
772a94100faSBill Paul 	m0 = NULL;
773a94100faSBill Paul 
774a94100faSBill Paul 	/* Wait for it to propagate through the chip */
775a94100faSBill Paul 
776abc8ff44SBill Paul 	DELAY(100000);
777a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
778a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
779ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
780abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
781abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
782a94100faSBill Paul 			break;
783a94100faSBill Paul 		DELAY(10);
784a94100faSBill Paul 	}
785a94100faSBill Paul 
786a94100faSBill Paul 	if (i == RL_TIMEOUT) {
7876b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
7886b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
7896b9f5c94SGleb Smirnoff 		    " loopback mode\n");
790a94100faSBill Paul 		error = EIO;
791a94100faSBill Paul 		goto done;
792a94100faSBill Paul 	}
793a94100faSBill Paul 
794a94100faSBill Paul 	/*
795a94100faSBill Paul 	 * The packet should have been dumped into the first
796a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
797a94100faSBill Paul 	 */
798a94100faSBill Paul 
799a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
800a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
801a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
802d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
803d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
804d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
805d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
806d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
807a94100faSBill Paul 
808d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
809d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
810a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
811a94100faSBill Paul 
812a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
813a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
814a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
815a94100faSBill Paul 
816a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8176b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8186b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
819a94100faSBill Paul 		error = EIO;
820a94100faSBill Paul 		goto done;
821a94100faSBill Paul 	}
822a94100faSBill Paul 
823a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
824a94100faSBill Paul 
825a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
826a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
827a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8286b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8296b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
830a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8316b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
832a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
833a94100faSBill Paul 		    ntohs(eh->ether_type));
8346b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8356b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8366b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8376b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8386b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8396b9f5c94SGleb Smirnoff 		    "details.\n");
840a94100faSBill Paul 		error = EIO;
841a94100faSBill Paul 	}
842a94100faSBill Paul 
843a94100faSBill Paul done:
844a94100faSBill Paul 	/* Turn interface off, release resources */
845a94100faSBill Paul 
846a94100faSBill Paul 	sc->rl_testmode = 0;
847351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
848a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
849a94100faSBill Paul 	re_stop(sc);
850a94100faSBill Paul 	if (m0 != NULL)
851a94100faSBill Paul 		m_freem(m0);
852a94100faSBill Paul 
85397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
85497b9d4baSJohn-Mark Gurney 
855a94100faSBill Paul 	return (error);
856a94100faSBill Paul }
857a94100faSBill Paul 
858ed510fb0SBill Paul #endif
859ed510fb0SBill Paul 
860a94100faSBill Paul /*
861a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
862a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
863a94100faSBill Paul  */
864a94100faSBill Paul static int
8657b5ffebfSPyun YongHyeon re_probe(device_t dev)
866a94100faSBill Paul {
867a94100faSBill Paul 	struct rl_type		*t;
868dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
869dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
870dfdb409eSPyun YongHyeon 	int			i;
871a94100faSBill Paul 
872dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
873dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
874dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
875dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
876a94100faSBill Paul 
877dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
878dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
87926390635SJohn Baldwin 			/*
88026390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
881dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
88226390635SJohn Baldwin 			 */
883a94100faSBill Paul 			return (ENXIO);
884a94100faSBill Paul 		}
885dfdb409eSPyun YongHyeon 	}
886dfdb409eSPyun YongHyeon 
887dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
888dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
889dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
890dfdb409eSPyun YongHyeon 			return (ENXIO);
891dfdb409eSPyun YongHyeon 		}
892dfdb409eSPyun YongHyeon 	}
893dfdb409eSPyun YongHyeon 
894dfdb409eSPyun YongHyeon 	t = re_devs;
895dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
896dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
897a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
898d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
899a94100faSBill Paul 		}
900a94100faSBill Paul 	}
901a94100faSBill Paul 
902a94100faSBill Paul 	return (ENXIO);
903a94100faSBill Paul }
904a94100faSBill Paul 
905a94100faSBill Paul /*
906a94100faSBill Paul  * Map a single buffer address.
907a94100faSBill Paul  */
908a94100faSBill Paul 
909a94100faSBill Paul static void
9107b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
911a94100faSBill Paul {
9128fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
913a94100faSBill Paul 
914a94100faSBill Paul 	if (error)
915a94100faSBill Paul 		return;
916a94100faSBill Paul 
917a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
918a94100faSBill Paul 	addr = arg;
919a94100faSBill Paul 	*addr = segs->ds_addr;
920a94100faSBill Paul }
921a94100faSBill Paul 
922a94100faSBill Paul static int
9237b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
924a94100faSBill Paul {
925d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
926a94100faSBill Paul 	int			error;
927a94100faSBill Paul 	int			i;
928a94100faSBill Paul 
929d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
930d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
931d65abd66SPyun YongHyeon 
932d65abd66SPyun YongHyeon 	/*
933d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
934ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
935ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
936ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
937ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
938ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
939d65abd66SPyun YongHyeon 	 */
940d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
941ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
942d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
943d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
944d65abd66SPyun YongHyeon 	if (error) {
945d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
946d65abd66SPyun YongHyeon 		return (error);
947d65abd66SPyun YongHyeon 	}
948d65abd66SPyun YongHyeon 
949d65abd66SPyun YongHyeon 	/*
950d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
951d65abd66SPyun YongHyeon 	 */
952d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
953d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
954d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
955d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
956d65abd66SPyun YongHyeon 	if (error) {
957d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
958d65abd66SPyun YongHyeon 		return (error);
959d65abd66SPyun YongHyeon 	}
960d65abd66SPyun YongHyeon 
961a94100faSBill Paul 	/*
962a94100faSBill Paul 	 * Allocate map for RX mbufs.
963a94100faSBill Paul 	 */
964d65abd66SPyun YongHyeon 
965d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
966d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
967d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
968a94100faSBill Paul 	if (error) {
969d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
970d65abd66SPyun YongHyeon 		return (error);
971a94100faSBill Paul 	}
972a94100faSBill Paul 
973a94100faSBill Paul 	/*
974a94100faSBill Paul 	 * Allocate map for TX descriptor list.
975a94100faSBill Paul 	 */
976a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
977a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
978d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
979a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
980a94100faSBill Paul 	if (error) {
981d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
982d65abd66SPyun YongHyeon 		return (error);
983a94100faSBill Paul 	}
984a94100faSBill Paul 
985a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
986a94100faSBill Paul 
987a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
988d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
989d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
990a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
991d65abd66SPyun YongHyeon 	if (error) {
992d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
993d65abd66SPyun YongHyeon 		return (error);
994d65abd66SPyun YongHyeon 	}
995a94100faSBill Paul 
996a94100faSBill Paul 	/* Load the map for the TX ring. */
997a94100faSBill Paul 
998d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
999a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1000a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1001d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1002a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1003d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1004d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1005d65abd66SPyun YongHyeon 		return (ENOMEM);
1006d65abd66SPyun YongHyeon 	}
1007a94100faSBill Paul 
1008a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1009a94100faSBill Paul 
1010d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1011d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1012d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1013a94100faSBill Paul 		if (error) {
1014d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1015d65abd66SPyun YongHyeon 			return (error);
1016a94100faSBill Paul 		}
1017a94100faSBill Paul 	}
1018a94100faSBill Paul 
1019a94100faSBill Paul 	/*
1020a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1021a94100faSBill Paul 	 */
1022a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1023a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1024d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1025a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1026a94100faSBill Paul 	if (error) {
1027d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1028d65abd66SPyun YongHyeon 		return (error);
1029a94100faSBill Paul 	}
1030a94100faSBill Paul 
1031a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1032a94100faSBill Paul 
1033a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1034d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1035d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1036a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1037d65abd66SPyun YongHyeon 	if (error) {
1038d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1039d65abd66SPyun YongHyeon 		return (error);
1040d65abd66SPyun YongHyeon 	}
1041a94100faSBill Paul 
1042a94100faSBill Paul 	/* Load the map for the RX ring. */
1043a94100faSBill Paul 
1044d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1045a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1046a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1047d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1048a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1049d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1050d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1051d65abd66SPyun YongHyeon 		return (ENOMEM);
1052d65abd66SPyun YongHyeon 	}
1053a94100faSBill Paul 
1054a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1055a94100faSBill Paul 
1056d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1057d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1058a94100faSBill Paul 	if (error) {
1059d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1060d65abd66SPyun YongHyeon 		return (error);
1061d65abd66SPyun YongHyeon 	}
1062d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1063d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1064d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1065d65abd66SPyun YongHyeon 		if (error) {
1066d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1067d65abd66SPyun YongHyeon 			return (error);
1068a94100faSBill Paul 		}
1069a94100faSBill Paul 	}
1070a94100faSBill Paul 
1071a94100faSBill Paul 	return (0);
1072a94100faSBill Paul }
1073a94100faSBill Paul 
1074a94100faSBill Paul /*
1075a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1076a94100faSBill Paul  * setup and ethernet/BPF attach.
1077a94100faSBill Paul  */
1078a94100faSBill Paul static int
10797b5ffebfSPyun YongHyeon re_attach(device_t dev)
1080a94100faSBill Paul {
1081a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1082be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1083a94100faSBill Paul 	struct rl_softc		*sc;
1084a94100faSBill Paul 	struct ifnet		*ifp;
1085a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1086a94100faSBill Paul 	int			hwrev;
1087ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
1088d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
10895774c5ffSPyun YongHyeon 	int			msic, reg;
109003ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1091a94100faSBill Paul 
1092a94100faSBill Paul 	sc = device_get_softc(dev);
1093ed510fb0SBill Paul 	sc->rl_dev = dev;
1094a94100faSBill Paul 
1095a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
109697b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1097d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1098d1754a9bSJohn Baldwin 
1099a94100faSBill Paul 	/*
1100a94100faSBill Paul 	 * Map control/status registers.
1101a94100faSBill Paul 	 */
1102a94100faSBill Paul 	pci_enable_busmaster(dev);
1103a94100faSBill Paul 
1104ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
1105ace7ed5dSPyun YongHyeon 	/* Prefer memory space register mapping over IO space. */
1106ace7ed5dSPyun YongHyeon 	sc->rl_res_id = PCIR_BAR(1);
1107ace7ed5dSPyun YongHyeon 	sc->rl_res_type = SYS_RES_MEMORY;
1108ace7ed5dSPyun YongHyeon 	/* RTL8168/8101E seems to use different BARs. */
1109ace7ed5dSPyun YongHyeon 	if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1110ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(2);
1111ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1112ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
1113a94100faSBill Paul 
1114a94100faSBill Paul 	if (sc->rl_res == NULL) {
1115ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1116ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1117ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1118ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
1119ace7ed5dSPyun YongHyeon 		if (sc->rl_res == NULL) {
1120d1754a9bSJohn Baldwin 			device_printf(dev, "couldn't map ports/memory\n");
1121a94100faSBill Paul 			error = ENXIO;
1122a94100faSBill Paul 			goto fail;
1123a94100faSBill Paul 		}
1124ace7ed5dSPyun YongHyeon 	}
1125a94100faSBill Paul 
1126a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1127a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1128a94100faSBill Paul 
11295774c5ffSPyun YongHyeon 	msic = 0;
11305774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
11315774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11325774c5ffSPyun YongHyeon 		if (bootverbose)
11335774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11345774c5ffSPyun YongHyeon 	}
11355774c5ffSPyun YongHyeon 	if (msic == RL_MSI_MESSAGES  && msi_disable == 0) {
11365774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11375774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11385774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11395774c5ffSPyun YongHyeon 				    msic);
1140351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1141339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1142339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1143339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1144339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1145339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1146f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
11475774c5ffSPyun YongHyeon 			} else
11485774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11495774c5ffSPyun YongHyeon 		}
11505774c5ffSPyun YongHyeon 	}
1151a94100faSBill Paul 
11525774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1153351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
11545774c5ffSPyun YongHyeon 		rid = 0;
11555774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
11565774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
11575774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
11585774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1159a94100faSBill Paul 			error = ENXIO;
1160a94100faSBill Paul 			goto fail;
1161a94100faSBill Paul 		}
11625774c5ffSPyun YongHyeon 	} else {
11635774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
11645774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
11655774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
11665774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
11675774c5ffSPyun YongHyeon 				device_printf(dev,
11685774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
11695774c5ffSPyun YongHyeon 				    "message %d\n", rid);
11705774c5ffSPyun YongHyeon 				error = ENXIO;
11715774c5ffSPyun YongHyeon 				goto fail;
11725774c5ffSPyun YongHyeon 			}
11735774c5ffSPyun YongHyeon 		}
11745774c5ffSPyun YongHyeon 	}
1175a94100faSBill Paul 
11764d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
11774d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
11784d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
11794d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
11804d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
11814d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
11824d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
11834d2bf239SPyun YongHyeon 		}
11844d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
11854d2bf239SPyun YongHyeon 	}
11864d2bf239SPyun YongHyeon 
1187a94100faSBill Paul 	/* Reset the adapter. */
118897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1189a94100faSBill Paul 	re_reset(sc);
119097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1191abc8ff44SBill Paul 
1192abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1193a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1194a810fc83SPyun YongHyeon 	device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1195a810fc83SPyun YongHyeon 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1196a810fc83SPyun YongHyeon 	hwrev &= RL_TXCFG_HWREV;
1197abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1198abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1199abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1200abc8ff44SBill Paul 			break;
1201abc8ff44SBill Paul 		}
1202abc8ff44SBill Paul 		hw_rev++;
1203abc8ff44SBill Paul 	}
1204d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1205a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1206d65abd66SPyun YongHyeon 		error = ENXIO;
1207d65abd66SPyun YongHyeon 		goto fail;
1208d65abd66SPyun YongHyeon 	}
1209abc8ff44SBill Paul 
1210351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1211351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
1212351a76f9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1213351a76f9SPyun YongHyeon 		break;
1214351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1215351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
121647fac8e5SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
121747fac8e5SPyun YongHyeon 		    RL_FLAG_PHYWAKE;
1218351a76f9SPyun YongHyeon 		break;
1219b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1220b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
1221b1d62f0fSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
1222b1d62f0fSPyun YongHyeon 		    RL_FLAG_PHYWAKE | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT;
1223b1d62f0fSPyun YongHyeon 		break;
1224351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
1225351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
1226351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
1227deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1228deb5c680SPyun YongHyeon 		    RL_FLAG_MACSTAT;
1229deb5c680SPyun YongHyeon 		break;
1230deb5c680SPyun YongHyeon 	case RL_HWREV_8168C:
1231deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
1232deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
1233deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1234deb5c680SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT;
1235deb5c680SPyun YongHyeon 		/*
1236deb5c680SPyun YongHyeon 		 * These controllers support jumbo frame but it seems
1237deb5c680SPyun YongHyeon 		 * that enabling it requires touching additional magic
1238deb5c680SPyun YongHyeon 		 * registers. Depending on MAC revisions some
1239deb5c680SPyun YongHyeon 		 * controllers need to disable checksum offload. So
1240deb5c680SPyun YongHyeon 		 * disable jumbo frame until I have better idea what
1241deb5c680SPyun YongHyeon 		 * it really requires to make it support.
1242deb5c680SPyun YongHyeon 		 * RTL8168C/CP : supports up to 6KB jumbo frame.
1243deb5c680SPyun YongHyeon 		 * RTL8111C/CP : supports up to 9KB jumbo frame.
1244deb5c680SPyun YongHyeon 		 */
1245deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1246351a76f9SPyun YongHyeon 		break;
1247351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SB:
1248351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SC:
1249715922d7SPyun YongHyeon 	case RL_HWREV_8169_8110SBL:
1250351a76f9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1251351a76f9SPyun YongHyeon 		break;
1252351a76f9SPyun YongHyeon 	default:
1253351a76f9SPyun YongHyeon 		break;
1254351a76f9SPyun YongHyeon 	}
1255351a76f9SPyun YongHyeon 
1256deb5c680SPyun YongHyeon 	/* Enable PME. */
1257deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1258deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1259deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1260deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1261deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1262deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1263deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1264deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1265deb5c680SPyun YongHyeon 
1266deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1267deb5c680SPyun YongHyeon 		/*
1268deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1269deb5c680SPyun YongHyeon 		 * address from EEPROM.
1270deb5c680SPyun YongHyeon 		 */
1271deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1272deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1273deb5c680SPyun YongHyeon 	} else {
1274141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1275ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1276a94100faSBill Paul 		if (re_did != 0x8129)
1277141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1278a94100faSBill Paul 
1279a94100faSBill Paul 		/*
1280a94100faSBill Paul 		 * Get station address from the EEPROM.
1281a94100faSBill Paul 		 */
1282ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1283be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1284be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1285be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1286deb5c680SPyun YongHyeon 	}
1287ed510fb0SBill Paul 
1288ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1289d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1290ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1291ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1292d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1293d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1294ed510fb0SBill Paul 	} else {
1295d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1296ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1297ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1298d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1299d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1300abc8ff44SBill Paul 	}
13019bac70b8SBill Paul 
1302a94100faSBill Paul 	error = re_allocmem(dev, sc);
1303a94100faSBill Paul 	if (error)
1304a94100faSBill Paul 		goto fail;
1305a94100faSBill Paul 
1306cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1307cd036ec1SBrooks Davis 	if (ifp == NULL) {
1308d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1309cd036ec1SBrooks Davis 		error = ENOSPC;
1310cd036ec1SBrooks Davis 		goto fail;
1311cd036ec1SBrooks Davis 	}
1312cd036ec1SBrooks Davis 
1313351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1314351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1315351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1316351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1317351a76f9SPyun YongHyeon 	}
1318351a76f9SPyun YongHyeon 
1319a94100faSBill Paul 	/* Do MII setup */
1320a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1321a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1322d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1323a94100faSBill Paul 		error = ENXIO;
1324a94100faSBill Paul 		goto fail;
1325a94100faSBill Paul 	}
1326a94100faSBill Paul 
1327a94100faSBill Paul 	ifp->if_softc = sc;
13289bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1329a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1330a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1331a94100faSBill Paul 	ifp->if_start = re_start;
1332deb5c680SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1333deb5c680SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1334498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1335a94100faSBill Paul 	ifp->if_init = re_init;
133652732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
133752732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
133852732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1339a94100faSBill Paul 
1340ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1341ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1342ed510fb0SBill Paul 
1343a94100faSBill Paul 	/*
1344deb5c680SPyun YongHyeon 	 * XXX
1345deb5c680SPyun YongHyeon 	 * Still have no idea how to make TSO work on 8168C, 8168CP,
1346deb5c680SPyun YongHyeon 	 * 8111C and 8111CP.
1347deb5c680SPyun YongHyeon 	 */
1348deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1349deb5c680SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
1350deb5c680SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4;
1351deb5c680SPyun YongHyeon 	}
1352deb5c680SPyun YongHyeon 
1353deb5c680SPyun YongHyeon 	/*
1354a94100faSBill Paul 	 * Call MI attach routine.
1355a94100faSBill Paul 	 */
1356a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1357a94100faSBill Paul 
1358960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1359960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1360960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1361960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
13627467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
13637467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
13647467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1365960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1366a2a8420cSPyun YongHyeon 	/*
1367a2a8420cSPyun YongHyeon 	 * Don't enable TSO by default. Under certain
1368a2a8420cSPyun YongHyeon 	 * circumtances the controller generated corrupted
1369a2a8420cSPyun YongHyeon 	 * packets in TSO size.
1370a2a8420cSPyun YongHyeon 	 */
1371a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1372a2a8420cSPyun YongHyeon 	ifp->if_capenable &= ~IFCAP_TSO4;
1373960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1374960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1375960fd5b3SPyun YongHyeon #endif
1376960fd5b3SPyun YongHyeon 	/*
1377960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1378960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1379960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1380960fd5b3SPyun YongHyeon 	 */
1381960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1382960fd5b3SPyun YongHyeon 
1383ed510fb0SBill Paul #ifdef RE_DIAG
1384ed510fb0SBill Paul 	/*
1385ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1386ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1387ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1388ed510fb0SBill Paul 	 */
1389a94100faSBill Paul 
1390ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1391ed510fb0SBill Paul 		error = re_diag(sc);
1392a94100faSBill Paul 		if (error) {
1393ed510fb0SBill Paul 			device_printf(dev,
1394ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1395a94100faSBill Paul 			ether_ifdetach(ifp);
1396a94100faSBill Paul 			goto fail;
1397a94100faSBill Paul 		}
1398ed510fb0SBill Paul 	}
1399ed510fb0SBill Paul #endif
1400a94100faSBill Paul 
1401a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1402351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
14035774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14045774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14055774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14065774c5ffSPyun YongHyeon 	else {
14075774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14085774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14095774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14105774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14115774c5ffSPyun YongHyeon 			if (error != 0)
14125774c5ffSPyun YongHyeon 				break;
14135774c5ffSPyun YongHyeon 		}
14145774c5ffSPyun YongHyeon 	}
1415a94100faSBill Paul 	if (error) {
1416d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1417a94100faSBill Paul 		ether_ifdetach(ifp);
1418a94100faSBill Paul 	}
1419a94100faSBill Paul 
1420a94100faSBill Paul fail:
1421ed510fb0SBill Paul 
1422a94100faSBill Paul 	if (error)
1423a94100faSBill Paul 		re_detach(dev);
1424a94100faSBill Paul 
1425a94100faSBill Paul 	return (error);
1426a94100faSBill Paul }
1427a94100faSBill Paul 
1428a94100faSBill Paul /*
1429a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1430a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1431a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1432a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1433a94100faSBill Paul  * allocated.
1434a94100faSBill Paul  */
1435a94100faSBill Paul static int
14367b5ffebfSPyun YongHyeon re_detach(device_t dev)
1437a94100faSBill Paul {
1438a94100faSBill Paul 	struct rl_softc		*sc;
1439a94100faSBill Paul 	struct ifnet		*ifp;
14405774c5ffSPyun YongHyeon 	int			i, rid;
1441a94100faSBill Paul 
1442a94100faSBill Paul 	sc = device_get_softc(dev);
1443fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1444aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
144597b9d4baSJohn-Mark Gurney 
144681cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
144781cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
144840929967SGleb Smirnoff #ifdef DEVICE_POLLING
144940929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
145040929967SGleb Smirnoff 			ether_poll_deregister(ifp);
145140929967SGleb Smirnoff #endif
145297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
145397b9d4baSJohn-Mark Gurney #if 0
145497b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
145597b9d4baSJohn-Mark Gurney #endif
1456a94100faSBill Paul 		re_stop(sc);
1457525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1458d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14593d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14603d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1461a94100faSBill Paul 		/*
1462a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1463a94100faSBill Paul 		 * still had a BPF descriptor attached to this
146497b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1465a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1466a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1467a94100faSBill Paul 		 * which will try to call re_init() again. This will
1468a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1469a94100faSBill Paul 		 * which will panic the system when the kernel tries
1470a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1471a94100faSBill Paul 		 * anymore.
1472a94100faSBill Paul 		 */
1473a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1474525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1475a94100faSBill Paul 	}
1476a94100faSBill Paul 	if (sc->rl_miibus)
1477a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1478a94100faSBill Paul 	bus_generic_detach(dev);
1479a94100faSBill Paul 
148097b9d4baSJohn-Mark Gurney 	/*
148197b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
148297b9d4baSJohn-Mark Gurney 	 * stopped here.
148397b9d4baSJohn-Mark Gurney 	 */
148497b9d4baSJohn-Mark Gurney 
14855774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
14865774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
14875774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
14885774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
14895774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
14905774c5ffSPyun YongHyeon 		}
14915774c5ffSPyun YongHyeon 	}
1492ad4f426eSWarner Losh 	if (ifp != NULL)
1493ad4f426eSWarner Losh 		if_free(ifp);
1494351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
14955774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
14965774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
14975774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
14985774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
14995774c5ffSPyun YongHyeon 		}
15005774c5ffSPyun YongHyeon 	} else {
15015774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15025774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15035774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15045774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15055774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15065774c5ffSPyun YongHyeon 			}
15075774c5ffSPyun YongHyeon 		}
15085774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15095774c5ffSPyun YongHyeon 	}
1510a94100faSBill Paul 	if (sc->rl_res)
1511ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1512ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1513a94100faSBill Paul 
1514a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1515a94100faSBill Paul 
1516a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1517a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1518a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1519a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1520a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1521a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1522a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1523a94100faSBill Paul 	}
1524a94100faSBill Paul 
1525a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1526a94100faSBill Paul 
1527a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1528a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1529a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1530a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1531a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1532a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1533a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1534a94100faSBill Paul 	}
1535a94100faSBill Paul 
1536a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1537a94100faSBill Paul 
1538d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1539d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1540d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1541d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1542d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1543d65abd66SPyun YongHyeon 	}
1544d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1545d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1546d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1547d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1548d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1549d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1550d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1551d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1552a94100faSBill Paul 	}
1553a94100faSBill Paul 
1554a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1555a94100faSBill Paul 
1556a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1557a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1558a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1559a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1560a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1561a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1562a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1563a94100faSBill Paul 	}
1564a94100faSBill Paul 
1565a94100faSBill Paul 	if (sc->rl_parent_tag)
1566a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1567a94100faSBill Paul 
1568a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1569a94100faSBill Paul 
1570a94100faSBill Paul 	return (0);
1571a94100faSBill Paul }
1572a94100faSBill Paul 
1573d65abd66SPyun YongHyeon static __inline void
15747b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1575a94100faSBill Paul {
1576d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1577d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1578d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1579a94100faSBill Paul 
1580d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1581d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1582d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1583d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1584d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1585d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1586d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1587d65abd66SPyun YongHyeon }
1588d65abd66SPyun YongHyeon 
1589d65abd66SPyun YongHyeon static int
15907b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1591d65abd66SPyun YongHyeon {
1592d65abd66SPyun YongHyeon 	struct mbuf		*m;
1593d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1594d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1595d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1596d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1597d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1598d65abd66SPyun YongHyeon 	int			error, nsegs;
1599d65abd66SPyun YongHyeon 
1600d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1601d65abd66SPyun YongHyeon 	if (m == NULL)
1602a94100faSBill Paul 		return (ENOBUFS);
1603a94100faSBill Paul 
1604a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
160522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
160622a11c96SJohn-Mark Gurney 	/*
160722a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
160822a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
160922a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
161022a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
161122a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
161222a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
161322a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
161422a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
161522a11c96SJohn-Mark Gurney 	 */
161622a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
161722a11c96SJohn-Mark Gurney #endif
1618d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1619d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1620d65abd66SPyun YongHyeon 	if (error != 0) {
1621d65abd66SPyun YongHyeon 		m_freem(m);
1622d65abd66SPyun YongHyeon 		return (ENOBUFS);
1623d65abd66SPyun YongHyeon 	}
1624d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1625a94100faSBill Paul 
1626d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1627d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1628d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1629d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1630d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1631a94100faSBill Paul 	}
1632a94100faSBill Paul 
1633d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1634d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1635d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1636d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1637d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1638d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1639a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1640a94100faSBill Paul 
1641d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1642d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1643d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1644d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1645d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1646d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1647d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1648d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1649d65abd66SPyun YongHyeon 
1650a94100faSBill Paul 	return (0);
1651a94100faSBill Paul }
1652a94100faSBill Paul 
165322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
165422a11c96SJohn-Mark Gurney static __inline void
16557b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
165622a11c96SJohn-Mark Gurney {
165722a11c96SJohn-Mark Gurney 	int                     i;
165822a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
165922a11c96SJohn-Mark Gurney 
166022a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
166122a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
166222a11c96SJohn-Mark Gurney 
166322a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
166422a11c96SJohn-Mark Gurney 		*dst++ = *src++;
166522a11c96SJohn-Mark Gurney 
166622a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
166722a11c96SJohn-Mark Gurney 
166822a11c96SJohn-Mark Gurney 	return;
166922a11c96SJohn-Mark Gurney }
167022a11c96SJohn-Mark Gurney #endif
167122a11c96SJohn-Mark Gurney 
1672a94100faSBill Paul static int
16737b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1674a94100faSBill Paul {
1675d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1676d65abd66SPyun YongHyeon 	int			i;
167797b9d4baSJohn-Mark Gurney 
167897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
167997b9d4baSJohn-Mark Gurney 
1680d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1681d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1682d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1683d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1684d65abd66SPyun YongHyeon 	/* Set EOR. */
1685d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1686d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1687a94100faSBill Paul 
1688a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1689d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1690d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1691d65abd66SPyun YongHyeon 
1692a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1693a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1694d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1695a94100faSBill Paul 
1696a94100faSBill Paul 	return (0);
1697a94100faSBill Paul }
1698a94100faSBill Paul 
1699a94100faSBill Paul static int
17007b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1701a94100faSBill Paul {
1702d65abd66SPyun YongHyeon 	int			error, i;
1703a94100faSBill Paul 
1704d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1705d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1706d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1707d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1708d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1709d65abd66SPyun YongHyeon 			return (error);
1710a94100faSBill Paul 	}
1711a94100faSBill Paul 
1712a94100faSBill Paul 	/* Flush the RX descriptors */
1713a94100faSBill Paul 
1714a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1715a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1716a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1717a94100faSBill Paul 
1718a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1719a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1720a94100faSBill Paul 
1721a94100faSBill Paul 	return (0);
1722a94100faSBill Paul }
1723a94100faSBill Paul 
1724a94100faSBill Paul /*
1725a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1726a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1727a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1728a94100faSBill Paul  */
1729ed510fb0SBill Paul static int
17307b5ffebfSPyun YongHyeon re_rxeof(struct rl_softc *sc)
1731a94100faSBill Paul {
1732a94100faSBill Paul 	struct mbuf		*m;
1733a94100faSBill Paul 	struct ifnet		*ifp;
1734a94100faSBill Paul 	int			i, total_len;
1735a94100faSBill Paul 	struct rl_desc		*cur_rx;
1736a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1737ed510fb0SBill Paul 	int			maxpkt = 16;
1738a94100faSBill Paul 
17395120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17405120abbfSSam Leffler 
1741fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1742a94100faSBill Paul 
1743a94100faSBill Paul 	/* Invalidate the descriptor memory */
1744a94100faSBill Paul 
1745a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1746a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1747d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1748a94100faSBill Paul 
1749d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1750d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1751a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1752a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1753d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1754d65abd66SPyun YongHyeon 			break;
1755d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1756a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1757d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1758a94100faSBill Paul 
1759a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1760d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1761d65abd66SPyun YongHyeon 				/*
1762d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1763d65abd66SPyun YongHyeon 				 * discard all the pieces.
1764d65abd66SPyun YongHyeon 				 */
1765d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1766d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1767d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1768d65abd66SPyun YongHyeon 				}
1769d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1770d65abd66SPyun YongHyeon 				continue;
1771d65abd66SPyun YongHyeon 			}
177222a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1773a94100faSBill Paul 			if (sc->rl_head == NULL)
1774a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1775a94100faSBill Paul 			else {
1776a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1777a94100faSBill Paul 				sc->rl_tail->m_next = m;
1778a94100faSBill Paul 				sc->rl_tail = m;
1779a94100faSBill Paul 			}
1780a94100faSBill Paul 			continue;
1781a94100faSBill Paul 		}
1782a94100faSBill Paul 
1783a94100faSBill Paul 		/*
1784a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1785a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1786a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1787a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1788a94100faSBill Paul 		 * were already used, so to make room for the extra
1789a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1790a94100faSBill Paul 		 * error' bit and shifted the other status bits
1791a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1792a94100faSBill Paul 		 * still in the same places. We have already extracted
1793a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1794a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1795a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1796a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1797a94100faSBill Paul 		 * same format as that of the 8139C+.
1798a94100faSBill Paul 		 */
1799a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1800a94100faSBill Paul 			rxstat >>= 1;
1801a94100faSBill Paul 
180222a11c96SJohn-Mark Gurney 		/*
180322a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
180422a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
180522a11c96SJohn-Mark Gurney 		 */
180622a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
180722a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1808a94100faSBill Paul 			ifp->if_ierrors++;
1809a94100faSBill Paul 			/*
1810a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1811a94100faSBill Paul 			 * discard all the pieces.
1812a94100faSBill Paul 			 */
1813a94100faSBill Paul 			if (sc->rl_head != NULL) {
1814a94100faSBill Paul 				m_freem(sc->rl_head);
1815a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1816a94100faSBill Paul 			}
1817d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1818a94100faSBill Paul 			continue;
1819a94100faSBill Paul 		}
1820a94100faSBill Paul 
1821a94100faSBill Paul 		/*
1822a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1823a94100faSBill Paul 		 * reload the current one.
1824a94100faSBill Paul 		 */
1825a94100faSBill Paul 
1826d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1827d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1828a94100faSBill Paul 			if (sc->rl_head != NULL) {
1829a94100faSBill Paul 				m_freem(sc->rl_head);
1830a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1831a94100faSBill Paul 			}
1832d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1833a94100faSBill Paul 			continue;
1834a94100faSBill Paul 		}
1835a94100faSBill Paul 
1836a94100faSBill Paul 		if (sc->rl_head != NULL) {
183722a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
183822a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
183922a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1840a94100faSBill Paul 			/*
1841a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1842a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1843a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1844a94100faSBill Paul 			 * care about anyway.
1845a94100faSBill Paul 			 */
1846a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1847a94100faSBill Paul 				sc->rl_tail->m_len -=
1848a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1849a94100faSBill Paul 				m_freem(m);
1850a94100faSBill Paul 			} else {
1851a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1852a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1853a94100faSBill Paul 				sc->rl_tail->m_next = m;
1854a94100faSBill Paul 			}
1855a94100faSBill Paul 			m = sc->rl_head;
1856a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1857a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1858a94100faSBill Paul 		} else
1859a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1860a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1861a94100faSBill Paul 
186222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
186322a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
186422a11c96SJohn-Mark Gurney #endif
1865a94100faSBill Paul 		ifp->if_ipackets++;
1866a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1867a94100faSBill Paul 
1868a94100faSBill Paul 		/* Do RX checksumming if enabled */
1869a94100faSBill Paul 
1870a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1871deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1872a94100faSBill Paul 				/* Check IP header checksum */
1873a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
1874deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1875deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1876a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1877deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1878deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1879a94100faSBill Paul 
1880a94100faSBill Paul 				/* Check TCP/UDP checksum */
1881a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
1882a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1883a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
1884a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1885a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
1886a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1887a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1888a94100faSBill Paul 				}
1889deb5c680SPyun YongHyeon 			} else {
1890deb5c680SPyun YongHyeon 				/*
1891deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
1892deb5c680SPyun YongHyeon 				 */
1893deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
1894deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1895deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1896deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1897deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
1898deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1899deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1900deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1901deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
1902deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1903deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
1904deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1905deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1906deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1907deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
1908deb5c680SPyun YongHyeon 				}
1909deb5c680SPyun YongHyeon 			}
1910a94100faSBill Paul 		}
1911ed510fb0SBill Paul 		maxpkt--;
1912d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
191378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
191478ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
191578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1916d147662cSGleb Smirnoff 		}
19175120abbfSSam Leffler 		RL_UNLOCK(sc);
1918a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
19195120abbfSSam Leffler 		RL_LOCK(sc);
1920a94100faSBill Paul 	}
1921a94100faSBill Paul 
1922a94100faSBill Paul 	/* Flush the RX DMA ring */
1923a94100faSBill Paul 
1924a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1925a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1926a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1927a94100faSBill Paul 
1928a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1929ed510fb0SBill Paul 
1930ed510fb0SBill Paul 	if (maxpkt)
1931ed510fb0SBill Paul 		return(EAGAIN);
1932ed510fb0SBill Paul 
1933ed510fb0SBill Paul 	return(0);
1934a94100faSBill Paul }
1935a94100faSBill Paul 
1936a94100faSBill Paul static void
19377b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
1938a94100faSBill Paul {
1939a94100faSBill Paul 	struct ifnet		*ifp;
1940d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
1941a94100faSBill Paul 	u_int32_t		txstat;
1942d65abd66SPyun YongHyeon 	int			cons;
1943d65abd66SPyun YongHyeon 
1944d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
1945d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
1946d65abd66SPyun YongHyeon 		return;
1947a94100faSBill Paul 
1948fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1949a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1950a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1951a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1952d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1953a94100faSBill Paul 
1954d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
1955d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
1956d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
1957d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
1958a94100faSBill Paul 			break;
1959a94100faSBill Paul 		/*
1960a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1961a94100faSBill Paul 		 * in a fragment chain, which also happens to
1962a94100faSBill Paul 		 * be the only place where the TX status bits
1963a94100faSBill Paul 		 * are valid.
1964a94100faSBill Paul 		 */
1965a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1966d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
1967d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
1968d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1969d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
1970d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
1971d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
1972d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
1973d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
1974d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
1975a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1976a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1977a94100faSBill Paul 				ifp->if_collisions++;
1978a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1979a94100faSBill Paul 				ifp->if_oerrors++;
1980a94100faSBill Paul 			else
1981a94100faSBill Paul 				ifp->if_opackets++;
1982a94100faSBill Paul 		}
1983a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1984d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1985a94100faSBill Paul 	}
1986d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
1987a94100faSBill Paul 
1988a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1989a94100faSBill Paul 
1990d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
19910fc4974fSBill Paul 		/*
1992b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
1993b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
1994b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
1995b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
1996b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
1997b4b95879SMarius Strobl 		 * be required with the PCIe devices.
19980fc4974fSBill Paul 		 */
19990fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
20000fc4974fSBill Paul 
2001ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2002a94100faSBill Paul 		/*
2003b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2004b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2005a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2006a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2007a94100faSBill Paul 		 */
2008a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2009ed510fb0SBill Paul #endif
2010b4b95879SMarius Strobl 	} else
2011b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2012a94100faSBill Paul }
2013a94100faSBill Paul 
2014a94100faSBill Paul static void
20157b5ffebfSPyun YongHyeon re_tick(void *xsc)
2016a94100faSBill Paul {
2017a94100faSBill Paul 	struct rl_softc		*sc;
2018d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2019ed510fb0SBill Paul 	struct ifnet		*ifp;
2020a94100faSBill Paul 
2021a94100faSBill Paul 	sc = xsc;
2022ed510fb0SBill Paul 	ifp = sc->rl_ifp;
202397b9d4baSJohn-Mark Gurney 
202497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
202597b9d4baSJohn-Mark Gurney 
20261d545c7aSMarius Strobl 	re_watchdog(sc);
2027a94100faSBill Paul 
20281d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2029a94100faSBill Paul 	mii_tick(mii);
2030351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) != 0) {
2031ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
2032351a76f9SPyun YongHyeon 			sc->rl_flags &= ~RL_FLAG_LINK;
2033ed510fb0SBill Paul 	} else {
2034ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
2035ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2036351a76f9SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
2037ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2038ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
2039ed510fb0SBill Paul 				    &sc->rl_txtask);
2040ed510fb0SBill Paul 		}
2041ed510fb0SBill Paul 	}
2042a94100faSBill Paul 
2043d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2044a94100faSBill Paul }
2045a94100faSBill Paul 
2046a94100faSBill Paul #ifdef DEVICE_POLLING
2047a94100faSBill Paul static void
2048a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2049a94100faSBill Paul {
2050a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
2051a94100faSBill Paul 
2052a94100faSBill Paul 	RL_LOCK(sc);
205340929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
205497b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
205597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
205697b9d4baSJohn-Mark Gurney }
205797b9d4baSJohn-Mark Gurney 
205897b9d4baSJohn-Mark Gurney static void
205997b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
206097b9d4baSJohn-Mark Gurney {
206197b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
206297b9d4baSJohn-Mark Gurney 
206397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
206497b9d4baSJohn-Mark Gurney 
2065a94100faSBill Paul 	sc->rxcycles = count;
2066a94100faSBill Paul 	re_rxeof(sc);
2067a94100faSBill Paul 	re_txeof(sc);
2068a94100faSBill Paul 
206937652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2070ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2071a94100faSBill Paul 
2072a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2073a94100faSBill Paul 		u_int16_t       status;
2074a94100faSBill Paul 
2075a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2076a94100faSBill Paul 		if (status == 0xffff)
207797b9d4baSJohn-Mark Gurney 			return;
2078a94100faSBill Paul 		if (status)
2079a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2080a94100faSBill Paul 
2081a94100faSBill Paul 		/*
2082a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2083a94100faSBill Paul 		 */
2084a94100faSBill Paul 
2085a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
2086a94100faSBill Paul 			re_reset(sc);
208797b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2088a94100faSBill Paul 		}
2089a94100faSBill Paul 	}
2090a94100faSBill Paul }
2091a94100faSBill Paul #endif /* DEVICE_POLLING */
2092a94100faSBill Paul 
2093ef544f63SPaolo Pisati static int
20947b5ffebfSPyun YongHyeon re_intr(void *arg)
2095a94100faSBill Paul {
2096a94100faSBill Paul 	struct rl_softc		*sc;
2097ed510fb0SBill Paul 	uint16_t		status;
2098a94100faSBill Paul 
2099a94100faSBill Paul 	sc = arg;
2100ed510fb0SBill Paul 
2101ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2102498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2103ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2104ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2105ed510fb0SBill Paul 
2106ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2107ed510fb0SBill Paul 
2108ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2109ed510fb0SBill Paul }
2110ed510fb0SBill Paul 
2111ed510fb0SBill Paul static void
21127b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2113ed510fb0SBill Paul {
2114ed510fb0SBill Paul 	struct rl_softc		*sc;
2115ed510fb0SBill Paul 	struct ifnet		*ifp;
2116ed510fb0SBill Paul 	u_int16_t		status;
2117ed510fb0SBill Paul 	int			rval = 0;
2118ed510fb0SBill Paul 
2119ed510fb0SBill Paul 	sc = arg;
2120ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2121a94100faSBill Paul 
2122a94100faSBill Paul 	RL_LOCK(sc);
212397b9d4baSJohn-Mark Gurney 
2124a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2125a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2126a94100faSBill Paul 
2127d65abd66SPyun YongHyeon 	if (sc->suspended ||
2128d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2129ed510fb0SBill Paul 		RL_UNLOCK(sc);
2130ed510fb0SBill Paul 		return;
2131ed510fb0SBill Paul 	}
2132a94100faSBill Paul 
2133ed510fb0SBill Paul #ifdef DEVICE_POLLING
2134ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2135ed510fb0SBill Paul 		RL_UNLOCK(sc);
2136ed510fb0SBill Paul 		return;
2137ed510fb0SBill Paul 	}
2138ed510fb0SBill Paul #endif
2139a94100faSBill Paul 
2140ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2141ed510fb0SBill Paul 		rval = re_rxeof(sc);
2142ed510fb0SBill Paul 
2143ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2144ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
2145ed510fb0SBill Paul #else
2146ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
2147ed510fb0SBill Paul #endif
2148ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2149a94100faSBill Paul 		re_txeof(sc);
2150a94100faSBill Paul 
2151a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
2152a94100faSBill Paul 		re_reset(sc);
215397b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2154a94100faSBill Paul 	}
2155a94100faSBill Paul 
2156a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
2157d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
2158d1754a9bSJohn Baldwin 		re_tick(sc);
2159a94100faSBill Paul 	}
2160a94100faSBill Paul 
216152732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2162ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2163a94100faSBill Paul 
2164a94100faSBill Paul 	RL_UNLOCK(sc);
2165ed510fb0SBill Paul 
2166ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2167ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2168ed510fb0SBill Paul 		return;
2169ed510fb0SBill Paul 	}
2170ed510fb0SBill Paul 
2171ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2172ed510fb0SBill Paul 
2173ed510fb0SBill Paul 	return;
2174a94100faSBill Paul }
2175a94100faSBill Paul 
2176d65abd66SPyun YongHyeon static int
21777b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2178d65abd66SPyun YongHyeon {
2179d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2180d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2181d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2182d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2183d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2184d65abd66SPyun YongHyeon 	int			nsegs, prod;
2185d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2186d65abd66SPyun YongHyeon 	int			padlen;
2187ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2188a94100faSBill Paul 
2189d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2190738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
21910fc4974fSBill Paul 
21920fc4974fSBill Paul 	/*
21930fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
21940fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
21950fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
21960fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
21970fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
21980fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
219999c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
22000fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2201d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
22020fc4974fSBill Paul 	 */
2203deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0 &&
2204deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
220599c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2206d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2207d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2208d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2209d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2210d65abd66SPyun YongHyeon 			m_freem(*m_head);
2211d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2212d65abd66SPyun YongHyeon 				*m_head = NULL;
2213a94100faSBill Paul 				return (ENOBUFS);
2214a94100faSBill Paul 			}
2215d65abd66SPyun YongHyeon 			*m_head = m_new;
2216d65abd66SPyun YongHyeon 		}
2217d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2218d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
221980a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2220b4b95879SMarius Strobl 			if (m_new == NULL) {
2221b4b95879SMarius Strobl 				m_freem(*m_head);
2222b4b95879SMarius Strobl 				*m_head = NULL;
222380a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2224b4b95879SMarius Strobl 			}
2225d65abd66SPyun YongHyeon 		} else
2226d65abd66SPyun YongHyeon 			m_new = *m_head;
2227a94100faSBill Paul 
22280fc4974fSBill Paul 		/*
22290fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
22300fc4974fSBill Paul 		 * to avoid leaking data.
22310fc4974fSBill Paul 		 */
2232d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2233d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
22340fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2235d65abd66SPyun YongHyeon 		*m_head = m_new;
22360fc4974fSBill Paul 	}
22370fc4974fSBill Paul 
2238d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2239d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2240d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2241d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2242d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2243304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2244d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2245d65abd66SPyun YongHyeon 			m_freem(*m_head);
2246b4b95879SMarius Strobl 			*m_head = NULL;
2247d65abd66SPyun YongHyeon 			return (ENOBUFS);
2248a94100faSBill Paul 		}
2249d65abd66SPyun YongHyeon 		*m_head = m_new;
2250d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2251d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2252d65abd66SPyun YongHyeon 		if (error != 0) {
2253d65abd66SPyun YongHyeon 			m_freem(*m_head);
2254d65abd66SPyun YongHyeon 			*m_head = NULL;
2255d65abd66SPyun YongHyeon 			return (error);
2256a94100faSBill Paul 		}
2257d65abd66SPyun YongHyeon 	} else if (error != 0)
2258d65abd66SPyun YongHyeon 		return (error);
2259d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2260d65abd66SPyun YongHyeon 		m_freem(*m_head);
2261d65abd66SPyun YongHyeon 		*m_head = NULL;
2262d65abd66SPyun YongHyeon 		return (EIO);
2263d65abd66SPyun YongHyeon 	}
2264d65abd66SPyun YongHyeon 
2265d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2266d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2267d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2268d65abd66SPyun YongHyeon 		return (ENOBUFS);
2269d65abd66SPyun YongHyeon 	}
2270d65abd66SPyun YongHyeon 
2271d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2272d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2273a94100faSBill Paul 
2274a94100faSBill Paul 	/*
2275d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2276d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2277d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2278d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2279a94100faSBill Paul 	 */
2280deb5c680SPyun YongHyeon 	vlanctl = 0;
2281d65abd66SPyun YongHyeon 	csum_flags = 0;
2282d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2283d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2284d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2285d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2286d65abd66SPyun YongHyeon 	else {
228799c8ae87SPyun YongHyeon 		/*
228899c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
228999c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
229099c8ae87SPyun YongHyeon 		 * does't make effects.
229199c8ae87SPyun YongHyeon 		 */
229299c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2293deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2294d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2295deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2296deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2297d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2298deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2299deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2300d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2301deb5c680SPyun YongHyeon 			} else {
2302deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2303deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2304deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2305deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2306deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2307deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2308deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2309deb5c680SPyun YongHyeon 			}
2310d65abd66SPyun YongHyeon 		}
231199c8ae87SPyun YongHyeon 	}
2312a94100faSBill Paul 
2313ccf34c81SPyun YongHyeon 	/*
2314ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2315ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2316ccf34c81SPyun YongHyeon 	 * transmission attempt.
2317ccf34c81SPyun YongHyeon 	 */
2318ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2319deb5c680SPyun YongHyeon 		vlanctl |= htons((*m_head)->m_pkthdr.ether_vtag) |
2320deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2321ccf34c81SPyun YongHyeon 
2322d65abd66SPyun YongHyeon 	si = prod;
2323d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2324d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2325deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2326d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2327d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2328d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2329d65abd66SPyun YongHyeon 		if (i != 0)
2330d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2331d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2332d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2333d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2334d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2335d65abd66SPyun YongHyeon 	}
2336d65abd66SPyun YongHyeon 	/* Update producer index. */
2337d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2338a94100faSBill Paul 
2339d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2340d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2341d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2342d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2343d65abd66SPyun YongHyeon 
2344d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2345d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2346d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2347a94100faSBill Paul 
2348d65abd66SPyun YongHyeon 	/*
2349d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2350d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2351d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2352d65abd66SPyun YongHyeon 	 */
2353d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2354d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2355d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2356d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2357d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2358a94100faSBill Paul 
2359a94100faSBill Paul 	return (0);
2360a94100faSBill Paul }
2361a94100faSBill Paul 
236297b9d4baSJohn-Mark Gurney static void
23637b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
236497b9d4baSJohn-Mark Gurney {
2365ed510fb0SBill Paul 	struct ifnet		*ifp;
236697b9d4baSJohn-Mark Gurney 
2367ed510fb0SBill Paul 	ifp = arg;
2368ed510fb0SBill Paul 	re_start(ifp);
2369ed510fb0SBill Paul 
2370ed510fb0SBill Paul 	return;
237197b9d4baSJohn-Mark Gurney }
237297b9d4baSJohn-Mark Gurney 
2373a94100faSBill Paul /*
2374a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2375a94100faSBill Paul  */
2376a94100faSBill Paul static void
23777b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2378a94100faSBill Paul {
2379a94100faSBill Paul 	struct rl_softc		*sc;
2380d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2381d65abd66SPyun YongHyeon 	int			queued;
2382a94100faSBill Paul 
2383a94100faSBill Paul 	sc = ifp->if_softc;
238497b9d4baSJohn-Mark Gurney 
2385ed510fb0SBill Paul 	RL_LOCK(sc);
2386ed510fb0SBill Paul 
2387d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2388351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2389ed510fb0SBill Paul 		RL_UNLOCK(sc);
2390ed510fb0SBill Paul 		return;
2391ed510fb0SBill Paul 	}
2392a94100faSBill Paul 
2393d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2394d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
239552732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2396a94100faSBill Paul 		if (m_head == NULL)
2397a94100faSBill Paul 			break;
2398a94100faSBill Paul 
2399d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2400b4b95879SMarius Strobl 			if (m_head == NULL)
2401b4b95879SMarius Strobl 				break;
240252732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
240313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2404a94100faSBill Paul 			break;
2405a94100faSBill Paul 		}
2406a94100faSBill Paul 
2407a94100faSBill Paul 		/*
2408a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2409a94100faSBill Paul 		 * to him.
2410a94100faSBill Paul 		 */
241159a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
241252732175SMax Laier 
241352732175SMax Laier 		queued++;
2414a94100faSBill Paul 	}
2415a94100faSBill Paul 
2416ed510fb0SBill Paul 	if (queued == 0) {
2417ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2418d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2419ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2420ed510fb0SBill Paul #endif
2421ed510fb0SBill Paul 		RL_UNLOCK(sc);
242252732175SMax Laier 		return;
2423ed510fb0SBill Paul 	}
242452732175SMax Laier 
2425a94100faSBill Paul 	/* Flush the TX descriptors */
2426a94100faSBill Paul 
2427a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2428a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2429a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2430a94100faSBill Paul 
24310fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2432a94100faSBill Paul 
2433ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2434a94100faSBill Paul 	/*
2435a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2436a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2437a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2438a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2439a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2440a94100faSBill Paul 	 * the timer count is reset to 0.
2441a94100faSBill Paul 	 */
2442a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2443ed510fb0SBill Paul #endif
2444a94100faSBill Paul 
2445a94100faSBill Paul 	/*
2446a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2447a94100faSBill Paul 	 */
24481d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2449ed510fb0SBill Paul 
2450ed510fb0SBill Paul 	RL_UNLOCK(sc);
2451ed510fb0SBill Paul 
2452ed510fb0SBill Paul 	return;
2453a94100faSBill Paul }
2454a94100faSBill Paul 
2455a94100faSBill Paul static void
24567b5ffebfSPyun YongHyeon re_init(void *xsc)
2457a94100faSBill Paul {
2458a94100faSBill Paul 	struct rl_softc		*sc = xsc;
245997b9d4baSJohn-Mark Gurney 
246097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
246197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
246297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
246397b9d4baSJohn-Mark Gurney }
246497b9d4baSJohn-Mark Gurney 
246597b9d4baSJohn-Mark Gurney static void
24667b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
246797b9d4baSJohn-Mark Gurney {
2468fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2469a94100faSBill Paul 	struct mii_data		*mii;
2470a94100faSBill Paul 	u_int32_t		rxcfg = 0;
247170acaecfSPyun YongHyeon 	uint16_t		cfg;
24724d3d7085SBernd Walter 	union {
24734d3d7085SBernd Walter 		uint32_t align_dummy;
24744d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
24754d3d7085SBernd Walter         } eaddr;
2476a94100faSBill Paul 
247797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
247897b9d4baSJohn-Mark Gurney 
2479a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2480a94100faSBill Paul 
2481a94100faSBill Paul 	/*
2482a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2483a94100faSBill Paul 	 */
2484a94100faSBill Paul 	re_stop(sc);
2485a94100faSBill Paul 
2486a94100faSBill Paul 	/*
2487c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2488edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2489c2c6548bSBill Paul 	 * before all others.
2490c2c6548bSBill Paul 	 */
249170acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
249270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
249370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
249470acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
249570acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2496deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2497deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2498deb5c680SPyun YongHyeon 		/* XXX magic. */
2499deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2500deb5c680SPyun YongHyeon 	} else
2501deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2502deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
2503ae644087SPyun YongHyeon 	/*
2504ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2505ae644087SPyun YongHyeon 	 * allowed in controller.
2506ae644087SPyun YongHyeon 	 */
2507ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2508ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2509ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2510ae644087SPyun YongHyeon 	}
2511c2c6548bSBill Paul 
2512c2c6548bSBill Paul 	/*
2513a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2514a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2515a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2516a94100faSBill Paul 	 */
25174d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
25184d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2519a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2520ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2521ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2522ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2523ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2524a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2525a94100faSBill Paul 
2526a94100faSBill Paul 	/*
2527a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2528a94100faSBill Paul 	 */
2529a94100faSBill Paul 	re_rx_list_init(sc);
2530a94100faSBill Paul 	re_tx_list_init(sc);
2531a94100faSBill Paul 
2532a94100faSBill Paul 	/*
2533d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2534d01fac16SPyun YongHyeon 	 */
2535d01fac16SPyun YongHyeon 
2536d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2537d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2538d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2539d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2540d01fac16SPyun YongHyeon 
2541d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2542d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2543d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2544d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2545d01fac16SPyun YongHyeon 
2546d01fac16SPyun YongHyeon 	/*
2547a94100faSBill Paul 	 * Enable transmit and receive.
2548a94100faSBill Paul 	 */
2549a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2550a94100faSBill Paul 
2551a94100faSBill Paul 	/*
2552a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2553a94100faSBill Paul 	 */
2554abc8ff44SBill Paul 	if (sc->rl_testmode) {
2555abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2556abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2557abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2558a94100faSBill Paul 		else
2559abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2560abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2561abc8ff44SBill Paul 	} else
2562a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2563d01fac16SPyun YongHyeon 
2564d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2565d01fac16SPyun YongHyeon 
2566a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2567a94100faSBill Paul 
2568a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2569a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2570a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2571a94100faSBill Paul 
2572a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
257361021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2574a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
257561021536SJohn-Mark Gurney 	else
2576a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2577a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2578a94100faSBill Paul 
2579a94100faSBill Paul 	/*
2580a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2581a94100faSBill Paul 	 */
258261021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2583a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
258461021536SJohn-Mark Gurney 	else
2585a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2586a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2587a94100faSBill Paul 
2588a94100faSBill Paul 	/*
2589a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2590a94100faSBill Paul 	 */
2591a94100faSBill Paul 	re_setmulti(sc);
2592a94100faSBill Paul 
2593a94100faSBill Paul #ifdef DEVICE_POLLING
2594a94100faSBill Paul 	/*
2595a94100faSBill Paul 	 * Disable interrupts if we are polling.
2596a94100faSBill Paul 	 */
259740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2598a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2599a94100faSBill Paul 	else	/* otherwise ... */
260040929967SGleb Smirnoff #endif
2601ed510fb0SBill Paul 
2602a94100faSBill Paul 	/*
2603a94100faSBill Paul 	 * Enable interrupts.
2604a94100faSBill Paul 	 */
2605a94100faSBill Paul 	if (sc->rl_testmode)
2606a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2607a94100faSBill Paul 	else
2608a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2609ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2610a94100faSBill Paul 
2611a94100faSBill Paul 	/* Set initial TX threshold */
2612a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2613a94100faSBill Paul 
2614a94100faSBill Paul 	/* Start RX/TX process. */
2615a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2616a94100faSBill Paul #ifdef notdef
2617a94100faSBill Paul 	/* Enable receiver and transmitter. */
2618a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2619a94100faSBill Paul #endif
2620a94100faSBill Paul 
2621ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2622a94100faSBill Paul 	/*
2623a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2624a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2625a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2626a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2627a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2628a94100faSBill Paul 	 */
2629a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2630a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2631a94100faSBill Paul 	else
2632a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2633ed510fb0SBill Paul #endif
2634a94100faSBill Paul 
2635a94100faSBill Paul 	/*
2636a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2637a94100faSBill Paul 	 * size so we can receive jumbo frames.
2638a94100faSBill Paul 	 */
2639a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2640a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2641a94100faSBill Paul 
264297b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2643a94100faSBill Paul 		return;
2644a94100faSBill Paul 
2645a94100faSBill Paul 	mii_mediachg(mii);
2646a94100faSBill Paul 
264719ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2648a94100faSBill Paul 
264913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
265013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2651a94100faSBill Paul 
2652351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
26531d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2654d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2655a94100faSBill Paul }
2656a94100faSBill Paul 
2657a94100faSBill Paul /*
2658a94100faSBill Paul  * Set media options.
2659a94100faSBill Paul  */
2660a94100faSBill Paul static int
26617b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
2662a94100faSBill Paul {
2663a94100faSBill Paul 	struct rl_softc		*sc;
2664a94100faSBill Paul 	struct mii_data		*mii;
2665a94100faSBill Paul 
2666a94100faSBill Paul 	sc = ifp->if_softc;
2667a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2668d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2669a94100faSBill Paul 	mii_mediachg(mii);
2670d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2671a94100faSBill Paul 
2672a94100faSBill Paul 	return (0);
2673a94100faSBill Paul }
2674a94100faSBill Paul 
2675a94100faSBill Paul /*
2676a94100faSBill Paul  * Report current media status.
2677a94100faSBill Paul  */
2678a94100faSBill Paul static void
26797b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2680a94100faSBill Paul {
2681a94100faSBill Paul 	struct rl_softc		*sc;
2682a94100faSBill Paul 	struct mii_data		*mii;
2683a94100faSBill Paul 
2684a94100faSBill Paul 	sc = ifp->if_softc;
2685a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2686a94100faSBill Paul 
2687d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2688a94100faSBill Paul 	mii_pollstat(mii);
2689d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2690a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2691a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2692a94100faSBill Paul }
2693a94100faSBill Paul 
2694a94100faSBill Paul static int
26957b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2696a94100faSBill Paul {
2697a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2698a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2699a94100faSBill Paul 	struct mii_data		*mii;
270040929967SGleb Smirnoff 	int			error = 0;
2701a94100faSBill Paul 
2702a94100faSBill Paul 	switch (command) {
2703a94100faSBill Paul 	case SIOCSIFMTU:
2704c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2705a94100faSBill Paul 			error = EINVAL;
2706c1d0b573SPyun YongHyeon 			break;
2707c1d0b573SPyun YongHyeon 		}
2708351a76f9SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 &&
2709c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2710c1d0b573SPyun YongHyeon 			error = EINVAL;
2711c1d0b573SPyun YongHyeon 			break;
2712c1d0b573SPyun YongHyeon 		}
2713c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2714c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2715a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2716ae644087SPyun YongHyeon 		if (ifp->if_mtu > RL_TSO_MTU &&
2717ae644087SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2718ae644087SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_TSO4;
2719ae644087SPyun YongHyeon 			ifp->if_hwassist &= ~CSUM_TSO;
2720ae644087SPyun YongHyeon 		}
2721d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2722a94100faSBill Paul 		break;
2723a94100faSBill Paul 	case SIOCSIFFLAGS:
272497b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2725eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2726eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2727eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
27283021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2729eed497bbSPyun YongHyeon 					re_setmulti(sc);
2730eed497bbSPyun YongHyeon 			} else
273197b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2732eed497bbSPyun YongHyeon 		} else {
2733eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2734a94100faSBill Paul 				re_stop(sc);
2735eed497bbSPyun YongHyeon 		}
2736eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
273797b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2738a94100faSBill Paul 		break;
2739a94100faSBill Paul 	case SIOCADDMULTI:
2740a94100faSBill Paul 	case SIOCDELMULTI:
274197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2742a94100faSBill Paul 		re_setmulti(sc);
274397b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2744a94100faSBill Paul 		break;
2745a94100faSBill Paul 	case SIOCGIFMEDIA:
2746a94100faSBill Paul 	case SIOCSIFMEDIA:
2747a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2748a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2749a94100faSBill Paul 		break;
2750a94100faSBill Paul 	case SIOCSIFCAP:
275140929967SGleb Smirnoff 	    {
2752f051cb85SGleb Smirnoff 		int mask, reinit;
2753f051cb85SGleb Smirnoff 
2754f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2755f051cb85SGleb Smirnoff 		reinit = 0;
275640929967SGleb Smirnoff #ifdef DEVICE_POLLING
275740929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
275840929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
275940929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
276040929967SGleb Smirnoff 				if (error)
276140929967SGleb Smirnoff 					return(error);
2762d1754a9bSJohn Baldwin 				RL_LOCK(sc);
276340929967SGleb Smirnoff 				/* Disable interrupts */
276440929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
276540929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
276640929967SGleb Smirnoff 				RL_UNLOCK(sc);
276740929967SGleb Smirnoff 			} else {
276840929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
276940929967SGleb Smirnoff 				/* Enable interrupts. */
277040929967SGleb Smirnoff 				RL_LOCK(sc);
277140929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
277240929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
277340929967SGleb Smirnoff 				RL_UNLOCK(sc);
277440929967SGleb Smirnoff 			}
277540929967SGleb Smirnoff 		}
277640929967SGleb Smirnoff #endif /* DEVICE_POLLING */
277740929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2778f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2779a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2780dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2781a94100faSBill Paul 			else
2782b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2783f051cb85SGleb Smirnoff 			reinit = 1;
278440929967SGleb Smirnoff 		}
2785f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2786f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2787f051cb85SGleb Smirnoff 			reinit = 1;
2788f051cb85SGleb Smirnoff 		}
2789dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2790dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2791dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2792dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2793dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2794dc74159dSPyun YongHyeon 			else
2795dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2796ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
2797ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2798ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
2799ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2800ae644087SPyun YongHyeon 			}
2801dc74159dSPyun YongHyeon 		}
28027467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
28037467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
28047467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
28057467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
28067467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
28077467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
28087467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
28097467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
28107467bd53SPyun YongHyeon 		}
2811f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2812f051cb85SGleb Smirnoff 			re_init(sc);
2813960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
281440929967SGleb Smirnoff 	    }
2815a94100faSBill Paul 		break;
2816a94100faSBill Paul 	default:
2817a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2818a94100faSBill Paul 		break;
2819a94100faSBill Paul 	}
2820a94100faSBill Paul 
2821a94100faSBill Paul 	return (error);
2822a94100faSBill Paul }
2823a94100faSBill Paul 
2824a94100faSBill Paul static void
28257b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
28261d545c7aSMarius Strobl {
2827a94100faSBill Paul 
28281d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
28291d545c7aSMarius Strobl 
28301d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
28311d545c7aSMarius Strobl 		return;
28321d545c7aSMarius Strobl 
28331d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
28341d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2835a94100faSBill Paul 
2836a94100faSBill Paul 	re_txeof(sc);
2837a94100faSBill Paul 	re_rxeof(sc);
283897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2839a94100faSBill Paul }
2840a94100faSBill Paul 
2841a94100faSBill Paul /*
2842a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2843a94100faSBill Paul  * RX and TX lists.
2844a94100faSBill Paul  */
2845a94100faSBill Paul static void
28467b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
2847a94100faSBill Paul {
28480ce0868aSPyun YongHyeon 	int			i;
2849a94100faSBill Paul 	struct ifnet		*ifp;
2850d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2851d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2852a94100faSBill Paul 
285397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
285497b9d4baSJohn-Mark Gurney 
2855fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2856a94100faSBill Paul 
28571d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2858d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
285913f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2860a94100faSBill Paul 
2861a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2862a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2863ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2864a94100faSBill Paul 
2865a94100faSBill Paul 	if (sc->rl_head != NULL) {
2866a94100faSBill Paul 		m_freem(sc->rl_head);
2867a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2868a94100faSBill Paul 	}
2869a94100faSBill Paul 
2870a94100faSBill Paul 	/* Free the TX list buffers. */
2871a94100faSBill Paul 
2872d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2873d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2874d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2875d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2876d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2877d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2878d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2879d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2880d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2881a94100faSBill Paul 		}
2882a94100faSBill Paul 	}
2883a94100faSBill Paul 
2884a94100faSBill Paul 	/* Free the RX list buffers. */
2885a94100faSBill Paul 
2886d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2887d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2888d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2889d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2890d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2891d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2892d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2893d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2894d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2895a94100faSBill Paul 		}
2896a94100faSBill Paul 	}
2897a94100faSBill Paul }
2898a94100faSBill Paul 
2899a94100faSBill Paul /*
2900a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2901a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2902a94100faSBill Paul  * resume.
2903a94100faSBill Paul  */
2904a94100faSBill Paul static int
29057b5ffebfSPyun YongHyeon re_suspend(device_t dev)
2906a94100faSBill Paul {
2907a94100faSBill Paul 	struct rl_softc		*sc;
2908a94100faSBill Paul 
2909a94100faSBill Paul 	sc = device_get_softc(dev);
2910a94100faSBill Paul 
291197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2912a94100faSBill Paul 	re_stop(sc);
29137467bd53SPyun YongHyeon 	re_setwol(sc);
2914a94100faSBill Paul 	sc->suspended = 1;
291597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2916a94100faSBill Paul 
2917a94100faSBill Paul 	return (0);
2918a94100faSBill Paul }
2919a94100faSBill Paul 
2920a94100faSBill Paul /*
2921a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2922a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2923a94100faSBill Paul  * appropriate.
2924a94100faSBill Paul  */
2925a94100faSBill Paul static int
29267b5ffebfSPyun YongHyeon re_resume(device_t dev)
2927a94100faSBill Paul {
2928a94100faSBill Paul 	struct rl_softc		*sc;
2929a94100faSBill Paul 	struct ifnet		*ifp;
2930a94100faSBill Paul 
2931a94100faSBill Paul 	sc = device_get_softc(dev);
293297b9d4baSJohn-Mark Gurney 
293397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
293497b9d4baSJohn-Mark Gurney 
2935fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2936a94100faSBill Paul 
2937a94100faSBill Paul 	/* reinitialize interface if necessary */
2938a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
293997b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2940a94100faSBill Paul 
29417467bd53SPyun YongHyeon 	/*
29427467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
29437467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
29447467bd53SPyun YongHyeon 	 */
29457467bd53SPyun YongHyeon 	re_clrwol(sc);
2946a94100faSBill Paul 	sc->suspended = 0;
294797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2948a94100faSBill Paul 
2949a94100faSBill Paul 	return (0);
2950a94100faSBill Paul }
2951a94100faSBill Paul 
2952a94100faSBill Paul /*
2953a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2954a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2955a94100faSBill Paul  */
29566a087a87SPyun YongHyeon static int
29577b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
2958a94100faSBill Paul {
2959a94100faSBill Paul 	struct rl_softc		*sc;
2960a94100faSBill Paul 
2961a94100faSBill Paul 	sc = device_get_softc(dev);
2962a94100faSBill Paul 
296397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2964a94100faSBill Paul 	re_stop(sc);
2965536fde34SMaxim Sobolev 	/*
2966536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2967536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
296872293673SRuslan Ermilov 	 * cases.
2969536fde34SMaxim Sobolev 	 */
2970536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
29717467bd53SPyun YongHyeon 	re_setwol(sc);
297297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
29736a087a87SPyun YongHyeon 
29746a087a87SPyun YongHyeon 	return (0);
2975a94100faSBill Paul }
29767467bd53SPyun YongHyeon 
29777467bd53SPyun YongHyeon static void
29787b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
29797467bd53SPyun YongHyeon {
29807467bd53SPyun YongHyeon 	struct ifnet		*ifp;
29817467bd53SPyun YongHyeon 	int			pmc;
29827467bd53SPyun YongHyeon 	uint16_t		pmstat;
29837467bd53SPyun YongHyeon 	uint8_t			v;
29847467bd53SPyun YongHyeon 
29857467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
29867467bd53SPyun YongHyeon 
29877467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
29887467bd53SPyun YongHyeon 		return;
29897467bd53SPyun YongHyeon 
29907467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
29917467bd53SPyun YongHyeon 	/* Enable config register write. */
29927467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
29937467bd53SPyun YongHyeon 
29947467bd53SPyun YongHyeon 	/* Enable PME. */
29957467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
29967467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
29977467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29987467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
29997467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
30007467bd53SPyun YongHyeon 
30017467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30027467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30037467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
30047467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
30057467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30067467bd53SPyun YongHyeon 
30077467bd53SPyun YongHyeon 	/* Config register write done. */
3008f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30097467bd53SPyun YongHyeon 
30107467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30117467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30127467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30137467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
30147467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
30157467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
30167467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
30177467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30187467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
30197467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30207467bd53SPyun YongHyeon 
30217467bd53SPyun YongHyeon 	/*
30227467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
30237467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
30247467bd53SPyun YongHyeon 	 * needed.
30257467bd53SPyun YongHyeon 	 */
30267467bd53SPyun YongHyeon 
30277467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
30287467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
30297467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
30307467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30317467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
30327467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
30337467bd53SPyun YongHyeon }
30347467bd53SPyun YongHyeon 
30357467bd53SPyun YongHyeon static void
30367b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
30377467bd53SPyun YongHyeon {
30387467bd53SPyun YongHyeon 	int			pmc;
30397467bd53SPyun YongHyeon 	uint8_t			v;
30407467bd53SPyun YongHyeon 
30417467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30427467bd53SPyun YongHyeon 
30437467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30447467bd53SPyun YongHyeon 		return;
30457467bd53SPyun YongHyeon 
30467467bd53SPyun YongHyeon 	/* Enable config register write. */
30477467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30487467bd53SPyun YongHyeon 
30497467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30507467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30517467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30527467bd53SPyun YongHyeon 
30537467bd53SPyun YongHyeon 	/* Config register write done. */
3054f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30557467bd53SPyun YongHyeon 
30567467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30577467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30587467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30597467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30607467bd53SPyun YongHyeon }
3061