xref: /freebsd/sys/dev/re/if_re.c (revision b1d62f0fd731b3118e9cd288ef2152fe5d1e24d2)
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
322a94100faSBill Paul re_eeprom_putbyte(sc, addr)
323a94100faSBill Paul 	struct rl_softc		*sc;
324a94100faSBill Paul 	int			addr;
325a94100faSBill Paul {
326a94100faSBill Paul 	register int		d, i;
327a94100faSBill Paul 
328ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
329a94100faSBill Paul 
330a94100faSBill Paul 	/*
331a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
332a94100faSBill Paul 	 */
333ed510fb0SBill Paul 
334ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
335a94100faSBill Paul 		if (d & i) {
336a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
337a94100faSBill Paul 		} else {
338a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
339a94100faSBill Paul 		}
340a94100faSBill Paul 		DELAY(100);
341a94100faSBill Paul 		EE_SET(RL_EE_CLK);
342a94100faSBill Paul 		DELAY(150);
343a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
344a94100faSBill Paul 		DELAY(100);
345a94100faSBill Paul 	}
346ed510fb0SBill Paul 
347ed510fb0SBill Paul 	return;
348a94100faSBill Paul }
349a94100faSBill Paul 
350a94100faSBill Paul /*
351a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
352a94100faSBill Paul  */
353a94100faSBill Paul static void
354a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
355a94100faSBill Paul 	struct rl_softc		*sc;
356a94100faSBill Paul 	int			addr;
357a94100faSBill Paul 	u_int16_t		*dest;
358a94100faSBill Paul {
359a94100faSBill Paul 	register int		i;
360a94100faSBill Paul 	u_int16_t		word = 0;
361a94100faSBill Paul 
362a94100faSBill Paul 	/*
363a94100faSBill Paul 	 * Send address of word we want to read.
364a94100faSBill Paul 	 */
365a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
366a94100faSBill Paul 
367a94100faSBill Paul 	/*
368a94100faSBill Paul 	 * Start reading bits from EEPROM.
369a94100faSBill Paul 	 */
370a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
371a94100faSBill Paul 		EE_SET(RL_EE_CLK);
372a94100faSBill Paul 		DELAY(100);
373a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
374a94100faSBill Paul 			word |= i;
375a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
376a94100faSBill Paul 		DELAY(100);
377a94100faSBill Paul 	}
378a94100faSBill Paul 
379a94100faSBill Paul 	*dest = word;
380ed510fb0SBill Paul 
381ed510fb0SBill Paul 	return;
382a94100faSBill Paul }
383a94100faSBill Paul 
384a94100faSBill Paul /*
385a94100faSBill Paul  * Read a sequence of words from the EEPROM.
386a94100faSBill Paul  */
387a94100faSBill Paul static void
388ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
389a94100faSBill Paul 	struct rl_softc		*sc;
390a94100faSBill Paul 	caddr_t			dest;
391a94100faSBill Paul 	int			off;
392a94100faSBill Paul 	int			cnt;
393a94100faSBill Paul {
394a94100faSBill Paul 	int			i;
395a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
396a94100faSBill Paul 
397ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
398ed510fb0SBill Paul 
399ed510fb0SBill Paul         DELAY(100);
400ed510fb0SBill Paul 
401a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
402ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
403a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
404ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
405a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
406be099007SPyun YongHyeon                 *ptr = word;
407a94100faSBill Paul 	}
408ed510fb0SBill Paul 
409ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
410ed510fb0SBill Paul 
411ed510fb0SBill Paul 	return;
412a94100faSBill Paul }
413a94100faSBill Paul 
414a94100faSBill Paul static int
415a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
416a94100faSBill Paul 	device_t		dev;
417a94100faSBill Paul 	int			phy, reg;
418a94100faSBill Paul {
419a94100faSBill Paul 	struct rl_softc		*sc;
420a94100faSBill Paul 	u_int32_t		rval;
421a94100faSBill Paul 	int			i;
422a94100faSBill Paul 
423a94100faSBill Paul 	if (phy != 1)
424a94100faSBill Paul 		return (0);
425a94100faSBill Paul 
426a94100faSBill Paul 	sc = device_get_softc(dev);
427a94100faSBill Paul 
4289bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4299bac70b8SBill Paul 
4309bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4319bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4329bac70b8SBill Paul 		return (rval);
4339bac70b8SBill Paul 	}
4349bac70b8SBill Paul 
435a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
436a94100faSBill Paul 	DELAY(1000);
437a94100faSBill Paul 
438a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
439a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
440a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
441a94100faSBill Paul 			break;
442a94100faSBill Paul 		DELAY(100);
443a94100faSBill Paul 	}
444a94100faSBill Paul 
445a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4466b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
447a94100faSBill Paul 		return (0);
448a94100faSBill Paul 	}
449a94100faSBill Paul 
450a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
451a94100faSBill Paul }
452a94100faSBill Paul 
453a94100faSBill Paul static int
454a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
455a94100faSBill Paul 	device_t		dev;
456a94100faSBill Paul 	int			phy, reg, data;
457a94100faSBill Paul {
458a94100faSBill Paul 	struct rl_softc		*sc;
459a94100faSBill Paul 	u_int32_t		rval;
460a94100faSBill Paul 	int			i;
461a94100faSBill Paul 
462a94100faSBill Paul 	sc = device_get_softc(dev);
463a94100faSBill Paul 
464a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4659bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
466a94100faSBill Paul 	DELAY(1000);
467a94100faSBill Paul 
468a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
469a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
470a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
471a94100faSBill Paul 			break;
472a94100faSBill Paul 		DELAY(100);
473a94100faSBill Paul 	}
474a94100faSBill Paul 
475a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4766b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
477a94100faSBill Paul 		return (0);
478a94100faSBill Paul 	}
479a94100faSBill Paul 
480a94100faSBill Paul 	return (0);
481a94100faSBill Paul }
482a94100faSBill Paul 
483a94100faSBill Paul static int
484a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
485a94100faSBill Paul 	device_t		dev;
486a94100faSBill Paul 	int			phy, reg;
487a94100faSBill Paul {
488a94100faSBill Paul 	struct rl_softc		*sc;
489a94100faSBill Paul 	u_int16_t		rval = 0;
490a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
491a94100faSBill Paul 
492a94100faSBill Paul 	sc = device_get_softc(dev);
493a94100faSBill Paul 
494a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
495a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
496a94100faSBill Paul 		return (rval);
497a94100faSBill Paul 	}
498a94100faSBill Paul 
499a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
500a94100faSBill Paul 	if (phy) {
501a94100faSBill Paul 		return (0);
502a94100faSBill Paul 	}
503a94100faSBill Paul 	switch (reg) {
504a94100faSBill Paul 	case MII_BMCR:
505a94100faSBill Paul 		re8139_reg = RL_BMCR;
506a94100faSBill Paul 		break;
507a94100faSBill Paul 	case MII_BMSR:
508a94100faSBill Paul 		re8139_reg = RL_BMSR;
509a94100faSBill Paul 		break;
510a94100faSBill Paul 	case MII_ANAR:
511a94100faSBill Paul 		re8139_reg = RL_ANAR;
512a94100faSBill Paul 		break;
513a94100faSBill Paul 	case MII_ANER:
514a94100faSBill Paul 		re8139_reg = RL_ANER;
515a94100faSBill Paul 		break;
516a94100faSBill Paul 	case MII_ANLPAR:
517a94100faSBill Paul 		re8139_reg = RL_LPAR;
518a94100faSBill Paul 		break;
519a94100faSBill Paul 	case MII_PHYIDR1:
520a94100faSBill Paul 	case MII_PHYIDR2:
521a94100faSBill Paul 		return (0);
522a94100faSBill Paul 	/*
523a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
524a94100faSBill Paul 	 * register. If we have a link partner which does not
525a94100faSBill Paul 	 * support NWAY, this is the register which will tell
526a94100faSBill Paul 	 * us the results of parallel detection.
527a94100faSBill Paul 	 */
528a94100faSBill Paul 	case RL_MEDIASTAT:
529a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
530a94100faSBill Paul 		return (rval);
531a94100faSBill Paul 	default:
5326b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
533a94100faSBill Paul 		return (0);
534a94100faSBill Paul 	}
535a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
536baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
537baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
538baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
539baa12772SPyun YongHyeon 	}
540a94100faSBill Paul 	return (rval);
541a94100faSBill Paul }
542a94100faSBill Paul 
543a94100faSBill Paul static int
544a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
545a94100faSBill Paul 	device_t		dev;
546a94100faSBill Paul 	int			phy, reg, data;
547a94100faSBill Paul {
548a94100faSBill Paul 	struct rl_softc		*sc;
549a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
550a94100faSBill Paul 	int			rval = 0;
551a94100faSBill Paul 
552a94100faSBill Paul 	sc = device_get_softc(dev);
553a94100faSBill Paul 
554a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
555a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
556a94100faSBill Paul 		return (rval);
557a94100faSBill Paul 	}
558a94100faSBill Paul 
559a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
56097b9d4baSJohn-Mark Gurney 	if (phy)
561a94100faSBill Paul 		return (0);
56297b9d4baSJohn-Mark Gurney 
563a94100faSBill Paul 	switch (reg) {
564a94100faSBill Paul 	case MII_BMCR:
565a94100faSBill Paul 		re8139_reg = RL_BMCR;
566baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
567baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
568baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
569baa12772SPyun YongHyeon 		}
570a94100faSBill Paul 		break;
571a94100faSBill Paul 	case MII_BMSR:
572a94100faSBill Paul 		re8139_reg = RL_BMSR;
573a94100faSBill Paul 		break;
574a94100faSBill Paul 	case MII_ANAR:
575a94100faSBill Paul 		re8139_reg = RL_ANAR;
576a94100faSBill Paul 		break;
577a94100faSBill Paul 	case MII_ANER:
578a94100faSBill Paul 		re8139_reg = RL_ANER;
579a94100faSBill Paul 		break;
580a94100faSBill Paul 	case MII_ANLPAR:
581a94100faSBill Paul 		re8139_reg = RL_LPAR;
582a94100faSBill Paul 		break;
583a94100faSBill Paul 	case MII_PHYIDR1:
584a94100faSBill Paul 	case MII_PHYIDR2:
585a94100faSBill Paul 		return (0);
586a94100faSBill Paul 		break;
587a94100faSBill Paul 	default:
5886b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
589a94100faSBill Paul 		return (0);
590a94100faSBill Paul 	}
591a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
592a94100faSBill Paul 	return (0);
593a94100faSBill Paul }
594a94100faSBill Paul 
595a94100faSBill Paul static void
596a94100faSBill Paul re_miibus_statchg(dev)
597a94100faSBill Paul 	device_t		dev;
598a94100faSBill Paul {
599a11e2f18SBruce M Simpson 
600a94100faSBill Paul }
601a94100faSBill Paul 
602a94100faSBill Paul /*
603a94100faSBill Paul  * Program the 64-bit multicast hash filter.
604a94100faSBill Paul  */
605a94100faSBill Paul static void
606a94100faSBill Paul re_setmulti(sc)
607a94100faSBill Paul 	struct rl_softc		*sc;
608a94100faSBill Paul {
609a94100faSBill Paul 	struct ifnet		*ifp;
610a94100faSBill Paul 	int			h = 0;
611a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
612a94100faSBill Paul 	struct ifmultiaddr	*ifma;
613a94100faSBill Paul 	u_int32_t		rxfilt;
614a94100faSBill Paul 	int			mcnt = 0;
615a94100faSBill Paul 
61697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
61797b9d4baSJohn-Mark Gurney 
618fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
619a94100faSBill Paul 
620a94100faSBill Paul 
6217c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6227c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
623a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
6247c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6257c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
626a0637caaSPyun YongHyeon 		/*
627a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
628a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
629a0637caaSPyun YongHyeon 		 * promiscuous mode.
630a0637caaSPyun YongHyeon 		 */
631a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
632a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
633a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
634a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
635a94100faSBill Paul 		return;
636a94100faSBill Paul 	}
637a94100faSBill Paul 
638a94100faSBill Paul 	/* first, zot all the existing hash bits */
639a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
640a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
641a94100faSBill Paul 
642a94100faSBill Paul 	/* now program new ones */
64313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
644a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
645a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
646a94100faSBill Paul 			continue;
6470e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6480e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
649a94100faSBill Paul 		if (h < 32)
650a94100faSBill Paul 			hashes[0] |= (1 << h);
651a94100faSBill Paul 		else
652a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
653a94100faSBill Paul 		mcnt++;
654a94100faSBill Paul 	}
65513b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
656a94100faSBill Paul 
657a94100faSBill Paul 	if (mcnt)
658a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
659a94100faSBill Paul 	else
660a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
661a94100faSBill Paul 
662a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
663bb7dfefbSBill Paul 
664bb7dfefbSBill Paul 	/*
665bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
666bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
667bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
668bb7dfefbSBill Paul 	 * order for those devices.
669bb7dfefbSBill Paul 	 */
670bb7dfefbSBill Paul 
671351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_INVMAR) != 0) {
672bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
673bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
674351a76f9SPyun YongHyeon 	} else {
675a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
676a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
677a94100faSBill Paul 	}
678bb7dfefbSBill Paul }
679a94100faSBill Paul 
680a94100faSBill Paul static void
681a94100faSBill Paul re_reset(sc)
682a94100faSBill Paul 	struct rl_softc		*sc;
683a94100faSBill Paul {
684a94100faSBill Paul 	register int		i;
685a94100faSBill Paul 
68697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
68797b9d4baSJohn-Mark Gurney 
688a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
689a94100faSBill Paul 
690a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
691a94100faSBill Paul 		DELAY(10);
692a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
693a94100faSBill Paul 			break;
694a94100faSBill Paul 	}
695a94100faSBill Paul 	if (i == RL_TIMEOUT)
6966b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
697a94100faSBill Paul 
698a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
699a94100faSBill Paul }
700a94100faSBill Paul 
701ed510fb0SBill Paul #ifdef RE_DIAG
702ed510fb0SBill Paul 
703a94100faSBill Paul /*
704a94100faSBill Paul  * The following routine is designed to test for a defect on some
705a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
706a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
707a94100faSBill Paul  * should be pulled high. The result of this defect is that the
708a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
709a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
710a94100faSBill Paul  * because the 64-bit data lines aren't connected.
711a94100faSBill Paul  *
712a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
713a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
714a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
715a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
716a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
717a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
718a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
719a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
720a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
721a94100faSBill Paul  */
722a94100faSBill Paul 
723a94100faSBill Paul static int
724a94100faSBill Paul re_diag(sc)
725a94100faSBill Paul 	struct rl_softc		*sc;
726a94100faSBill Paul {
727fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
728a94100faSBill Paul 	struct mbuf		*m0;
729a94100faSBill Paul 	struct ether_header	*eh;
730a94100faSBill Paul 	struct rl_desc		*cur_rx;
731a94100faSBill Paul 	u_int16_t		status;
732a94100faSBill Paul 	u_int32_t		rxstat;
733ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
734a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
735a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
736a94100faSBill Paul 
737a94100faSBill Paul 	/* Allocate a single mbuf */
738a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
739a94100faSBill Paul 	if (m0 == NULL)
740a94100faSBill Paul 		return (ENOBUFS);
741a94100faSBill Paul 
74297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
74397b9d4baSJohn-Mark Gurney 
744a94100faSBill Paul 	/*
745a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
746a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
747a94100faSBill Paul 	 * following special functions:
748a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
749a94100faSBill Paul 	 * - Enables digital loopback mode
750a94100faSBill Paul 	 * - Leaves interrupts turned off
751a94100faSBill Paul 	 */
752a94100faSBill Paul 
753a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
754a94100faSBill Paul 	sc->rl_testmode = 1;
755ed510fb0SBill Paul 	re_reset(sc);
75697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
757351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
758ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
759ed510fb0SBill Paul 		phyaddr = 1;
760ed510fb0SBill Paul 	else
761ed510fb0SBill Paul 		phyaddr = 0;
762ed510fb0SBill Paul 
763ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
764ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
765ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
766ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
767ed510fb0SBill Paul 			break;
768ed510fb0SBill Paul 	}
769ed510fb0SBill Paul 
770ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
771ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
772ed510fb0SBill Paul 
773804af9a1SBill Paul 	DELAY(100000);
774a94100faSBill Paul 
775a94100faSBill Paul 	/* Put some data in the mbuf */
776a94100faSBill Paul 
777a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
778a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
779a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
780a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
781a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
782a94100faSBill Paul 
7837cae6651SBill Paul 	/*
7847cae6651SBill Paul 	 * Queue the packet, start transmission.
7857cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7867cae6651SBill Paul 	 */
787a94100faSBill Paul 
788abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
78997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79052732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7917cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
79297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
793a94100faSBill Paul 	m0 = NULL;
794a94100faSBill Paul 
795a94100faSBill Paul 	/* Wait for it to propagate through the chip */
796a94100faSBill Paul 
797abc8ff44SBill Paul 	DELAY(100000);
798a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
799a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
800ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
801abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
802abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
803a94100faSBill Paul 			break;
804a94100faSBill Paul 		DELAY(10);
805a94100faSBill Paul 	}
806a94100faSBill Paul 
807a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8086b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8096b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8106b9f5c94SGleb Smirnoff 		    " loopback mode\n");
811a94100faSBill Paul 		error = EIO;
812a94100faSBill Paul 		goto done;
813a94100faSBill Paul 	}
814a94100faSBill Paul 
815a94100faSBill Paul 	/*
816a94100faSBill Paul 	 * The packet should have been dumped into the first
817a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
818a94100faSBill Paul 	 */
819a94100faSBill Paul 
820a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
821a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
822a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
823d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
824d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
825d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
826d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
827d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
828a94100faSBill Paul 
829d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
830d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
831a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
832a94100faSBill Paul 
833a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
834a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
835a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
836a94100faSBill Paul 
837a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8386b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8396b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
840a94100faSBill Paul 		error = EIO;
841a94100faSBill Paul 		goto done;
842a94100faSBill Paul 	}
843a94100faSBill Paul 
844a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
845a94100faSBill Paul 
846a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
847a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
848a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8496b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8506b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
851a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8526b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
853a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
854a94100faSBill Paul 		    ntohs(eh->ether_type));
8556b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8566b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8576b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8586b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8596b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8606b9f5c94SGleb Smirnoff 		    "details.\n");
861a94100faSBill Paul 		error = EIO;
862a94100faSBill Paul 	}
863a94100faSBill Paul 
864a94100faSBill Paul done:
865a94100faSBill Paul 	/* Turn interface off, release resources */
866a94100faSBill Paul 
867a94100faSBill Paul 	sc->rl_testmode = 0;
868351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
869a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
870a94100faSBill Paul 	re_stop(sc);
871a94100faSBill Paul 	if (m0 != NULL)
872a94100faSBill Paul 		m_freem(m0);
873a94100faSBill Paul 
87497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
87597b9d4baSJohn-Mark Gurney 
876a94100faSBill Paul 	return (error);
877a94100faSBill Paul }
878a94100faSBill Paul 
879ed510fb0SBill Paul #endif
880ed510fb0SBill Paul 
881a94100faSBill Paul /*
882a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
883a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
884a94100faSBill Paul  */
885a94100faSBill Paul static int
886a94100faSBill Paul re_probe(dev)
887a94100faSBill Paul 	device_t		dev;
888a94100faSBill Paul {
889a94100faSBill Paul 	struct rl_type		*t;
890dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
891dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
892dfdb409eSPyun YongHyeon 	int			i;
893a94100faSBill Paul 
894dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
895dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
896dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
897dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
898a94100faSBill Paul 
899dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
900dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
90126390635SJohn Baldwin 			/*
90226390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
903dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
90426390635SJohn Baldwin 			 */
905a94100faSBill Paul 			return (ENXIO);
906a94100faSBill Paul 		}
907dfdb409eSPyun YongHyeon 	}
908dfdb409eSPyun YongHyeon 
909dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
910dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
911dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
912dfdb409eSPyun YongHyeon 			return (ENXIO);
913dfdb409eSPyun YongHyeon 		}
914dfdb409eSPyun YongHyeon 	}
915dfdb409eSPyun YongHyeon 
916dfdb409eSPyun YongHyeon 	t = re_devs;
917dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
918dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
919a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
920d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
921a94100faSBill Paul 		}
922a94100faSBill Paul 	}
923a94100faSBill Paul 
924a94100faSBill Paul 	return (ENXIO);
925a94100faSBill Paul }
926a94100faSBill Paul 
927a94100faSBill Paul /*
928a94100faSBill Paul  * Map a single buffer address.
929a94100faSBill Paul  */
930a94100faSBill Paul 
931a94100faSBill Paul static void
932a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
933a94100faSBill Paul 	void			*arg;
934a94100faSBill Paul 	bus_dma_segment_t	*segs;
935a94100faSBill Paul 	int			nseg;
936a94100faSBill Paul 	int			error;
937a94100faSBill Paul {
9388fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
939a94100faSBill Paul 
940a94100faSBill Paul 	if (error)
941a94100faSBill Paul 		return;
942a94100faSBill Paul 
943a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
944a94100faSBill Paul 	addr = arg;
945a94100faSBill Paul 	*addr = segs->ds_addr;
946a94100faSBill Paul }
947a94100faSBill Paul 
948a94100faSBill Paul static int
949a94100faSBill Paul re_allocmem(dev, sc)
950a94100faSBill Paul 	device_t		dev;
951a94100faSBill Paul 	struct rl_softc		*sc;
952a94100faSBill Paul {
953d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
954a94100faSBill Paul 	int			error;
955a94100faSBill Paul 	int			i;
956a94100faSBill Paul 
957d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
958d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
959d65abd66SPyun YongHyeon 
960d65abd66SPyun YongHyeon 	/*
961d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
962ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
963ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
964ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
965ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
966ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
967d65abd66SPyun YongHyeon 	 */
968d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
969ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
970d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
971d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
972d65abd66SPyun YongHyeon 	if (error) {
973d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
974d65abd66SPyun YongHyeon 		return (error);
975d65abd66SPyun YongHyeon 	}
976d65abd66SPyun YongHyeon 
977d65abd66SPyun YongHyeon 	/*
978d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
979d65abd66SPyun YongHyeon 	 */
980d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
981d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
982d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
983d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
984d65abd66SPyun YongHyeon 	if (error) {
985d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
986d65abd66SPyun YongHyeon 		return (error);
987d65abd66SPyun YongHyeon 	}
988d65abd66SPyun YongHyeon 
989a94100faSBill Paul 	/*
990a94100faSBill Paul 	 * Allocate map for RX mbufs.
991a94100faSBill Paul 	 */
992d65abd66SPyun YongHyeon 
993d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
994d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
995d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
996a94100faSBill Paul 	if (error) {
997d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
998d65abd66SPyun YongHyeon 		return (error);
999a94100faSBill Paul 	}
1000a94100faSBill Paul 
1001a94100faSBill Paul 	/*
1002a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1003a94100faSBill Paul 	 */
1004a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1005a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1006d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1007a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1008a94100faSBill Paul 	if (error) {
1009d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1010d65abd66SPyun YongHyeon 		return (error);
1011a94100faSBill Paul 	}
1012a94100faSBill Paul 
1013a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1014a94100faSBill Paul 
1015a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1016d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1017d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1018a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1019d65abd66SPyun YongHyeon 	if (error) {
1020d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1021d65abd66SPyun YongHyeon 		return (error);
1022d65abd66SPyun YongHyeon 	}
1023a94100faSBill Paul 
1024a94100faSBill Paul 	/* Load the map for the TX ring. */
1025a94100faSBill Paul 
1026d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1027a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1028a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1029d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1030a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1031d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1032d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1033d65abd66SPyun YongHyeon 		return (ENOMEM);
1034d65abd66SPyun YongHyeon 	}
1035a94100faSBill Paul 
1036a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1037a94100faSBill Paul 
1038d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1039d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1040d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1041a94100faSBill Paul 		if (error) {
1042d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1043d65abd66SPyun YongHyeon 			return (error);
1044a94100faSBill Paul 		}
1045a94100faSBill Paul 	}
1046a94100faSBill Paul 
1047a94100faSBill Paul 	/*
1048a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1049a94100faSBill Paul 	 */
1050a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1051a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1052d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1053a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1054a94100faSBill Paul 	if (error) {
1055d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1056d65abd66SPyun YongHyeon 		return (error);
1057a94100faSBill Paul 	}
1058a94100faSBill Paul 
1059a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1060a94100faSBill Paul 
1061a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1062d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1063d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1064a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1065d65abd66SPyun YongHyeon 	if (error) {
1066d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1067d65abd66SPyun YongHyeon 		return (error);
1068d65abd66SPyun YongHyeon 	}
1069a94100faSBill Paul 
1070a94100faSBill Paul 	/* Load the map for the RX ring. */
1071a94100faSBill Paul 
1072d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1073a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1074a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1075d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1076a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1077d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1078d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1079d65abd66SPyun YongHyeon 		return (ENOMEM);
1080d65abd66SPyun YongHyeon 	}
1081a94100faSBill Paul 
1082a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1083a94100faSBill Paul 
1084d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1085d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1086a94100faSBill Paul 	if (error) {
1087d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1088d65abd66SPyun YongHyeon 		return (error);
1089d65abd66SPyun YongHyeon 	}
1090d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1091d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1092d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1093d65abd66SPyun YongHyeon 		if (error) {
1094d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1095d65abd66SPyun YongHyeon 			return (error);
1096a94100faSBill Paul 		}
1097a94100faSBill Paul 	}
1098a94100faSBill Paul 
1099a94100faSBill Paul 	return (0);
1100a94100faSBill Paul }
1101a94100faSBill Paul 
1102a94100faSBill Paul /*
1103a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1104a94100faSBill Paul  * setup and ethernet/BPF attach.
1105a94100faSBill Paul  */
1106a94100faSBill Paul static int
1107a94100faSBill Paul re_attach(dev)
1108a94100faSBill Paul 	device_t		dev;
1109a94100faSBill Paul {
1110a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1111be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1112a94100faSBill Paul 	struct rl_softc		*sc;
1113a94100faSBill Paul 	struct ifnet		*ifp;
1114a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1115a94100faSBill Paul 	int			hwrev;
1116ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
1117d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11185774c5ffSPyun YongHyeon 	int			msic, reg;
111903ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1120a94100faSBill Paul 
1121a94100faSBill Paul 	sc = device_get_softc(dev);
1122ed510fb0SBill Paul 	sc->rl_dev = dev;
1123a94100faSBill Paul 
1124a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
112597b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1126d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1127d1754a9bSJohn Baldwin 
1128a94100faSBill Paul 	/*
1129a94100faSBill Paul 	 * Map control/status registers.
1130a94100faSBill Paul 	 */
1131a94100faSBill Paul 	pci_enable_busmaster(dev);
1132a94100faSBill Paul 
1133ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
1134ace7ed5dSPyun YongHyeon 	/* Prefer memory space register mapping over IO space. */
1135ace7ed5dSPyun YongHyeon 	sc->rl_res_id = PCIR_BAR(1);
1136ace7ed5dSPyun YongHyeon 	sc->rl_res_type = SYS_RES_MEMORY;
1137ace7ed5dSPyun YongHyeon 	/* RTL8168/8101E seems to use different BARs. */
1138ace7ed5dSPyun YongHyeon 	if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1139ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(2);
1140ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1141ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
1142a94100faSBill Paul 
1143a94100faSBill Paul 	if (sc->rl_res == NULL) {
1144ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1145ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1146ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1147ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
1148ace7ed5dSPyun YongHyeon 		if (sc->rl_res == NULL) {
1149d1754a9bSJohn Baldwin 			device_printf(dev, "couldn't map ports/memory\n");
1150a94100faSBill Paul 			error = ENXIO;
1151a94100faSBill Paul 			goto fail;
1152a94100faSBill Paul 		}
1153ace7ed5dSPyun YongHyeon 	}
1154a94100faSBill Paul 
1155a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1156a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1157a94100faSBill Paul 
11585774c5ffSPyun YongHyeon 	msic = 0;
11595774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
11605774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11615774c5ffSPyun YongHyeon 		if (bootverbose)
11625774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11635774c5ffSPyun YongHyeon 	}
11645774c5ffSPyun YongHyeon 	if (msic == RL_MSI_MESSAGES  && msi_disable == 0) {
11655774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11665774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11675774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11685774c5ffSPyun YongHyeon 				    msic);
1169351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1170339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1171339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1172339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1173339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1174339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1175f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
11765774c5ffSPyun YongHyeon 			} else
11775774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11785774c5ffSPyun YongHyeon 		}
11795774c5ffSPyun YongHyeon 	}
1180a94100faSBill Paul 
11815774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1182351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
11835774c5ffSPyun YongHyeon 		rid = 0;
11845774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
11855774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
11865774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
11875774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1188a94100faSBill Paul 			error = ENXIO;
1189a94100faSBill Paul 			goto fail;
1190a94100faSBill Paul 		}
11915774c5ffSPyun YongHyeon 	} else {
11925774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
11935774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
11945774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
11955774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
11965774c5ffSPyun YongHyeon 				device_printf(dev,
11975774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
11985774c5ffSPyun YongHyeon 				    "message %d\n", rid);
11995774c5ffSPyun YongHyeon 				error = ENXIO;
12005774c5ffSPyun YongHyeon 				goto fail;
12015774c5ffSPyun YongHyeon 			}
12025774c5ffSPyun YongHyeon 		}
12035774c5ffSPyun YongHyeon 	}
1204a94100faSBill Paul 
12054d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
12064d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
12074d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
12084d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
12094d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
12104d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
12114d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
12124d2bf239SPyun YongHyeon 		}
12134d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12144d2bf239SPyun YongHyeon 	}
12154d2bf239SPyun YongHyeon 
1216a94100faSBill Paul 	/* Reset the adapter. */
121797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1218a94100faSBill Paul 	re_reset(sc);
121997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1220abc8ff44SBill Paul 
1221abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1222a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1223a810fc83SPyun YongHyeon 	device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1224a810fc83SPyun YongHyeon 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1225a810fc83SPyun YongHyeon 	hwrev &= RL_TXCFG_HWREV;
1226abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1227abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1228abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1229abc8ff44SBill Paul 			break;
1230abc8ff44SBill Paul 		}
1231abc8ff44SBill Paul 		hw_rev++;
1232abc8ff44SBill Paul 	}
1233d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1234a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1235d65abd66SPyun YongHyeon 		error = ENXIO;
1236d65abd66SPyun YongHyeon 		goto fail;
1237d65abd66SPyun YongHyeon 	}
1238abc8ff44SBill Paul 
1239351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1240351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
1241351a76f9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1242351a76f9SPyun YongHyeon 		break;
1243351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1244351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
124547fac8e5SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
124647fac8e5SPyun YongHyeon 		    RL_FLAG_PHYWAKE;
1247351a76f9SPyun YongHyeon 		break;
1248b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1249b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
1250b1d62f0fSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
1251b1d62f0fSPyun YongHyeon 		    RL_FLAG_PHYWAKE | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT;
1252b1d62f0fSPyun YongHyeon 		break;
1253351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
1254351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
1255351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
1256deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1257deb5c680SPyun YongHyeon 		    RL_FLAG_MACSTAT;
1258deb5c680SPyun YongHyeon 		break;
1259deb5c680SPyun YongHyeon 	case RL_HWREV_8168C:
1260deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
1261deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
1262deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1263deb5c680SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT;
1264deb5c680SPyun YongHyeon 		/*
1265deb5c680SPyun YongHyeon 		 * These controllers support jumbo frame but it seems
1266deb5c680SPyun YongHyeon 		 * that enabling it requires touching additional magic
1267deb5c680SPyun YongHyeon 		 * registers. Depending on MAC revisions some
1268deb5c680SPyun YongHyeon 		 * controllers need to disable checksum offload. So
1269deb5c680SPyun YongHyeon 		 * disable jumbo frame until I have better idea what
1270deb5c680SPyun YongHyeon 		 * it really requires to make it support.
1271deb5c680SPyun YongHyeon 		 * RTL8168C/CP : supports up to 6KB jumbo frame.
1272deb5c680SPyun YongHyeon 		 * RTL8111C/CP : supports up to 9KB jumbo frame.
1273deb5c680SPyun YongHyeon 		 */
1274deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1275351a76f9SPyun YongHyeon 		break;
1276351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SB:
1277351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SC:
1278715922d7SPyun YongHyeon 	case RL_HWREV_8169_8110SBL:
1279351a76f9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1280351a76f9SPyun YongHyeon 		break;
1281351a76f9SPyun YongHyeon 	default:
1282351a76f9SPyun YongHyeon 		break;
1283351a76f9SPyun YongHyeon 	}
1284351a76f9SPyun YongHyeon 
1285deb5c680SPyun YongHyeon 	/* Enable PME. */
1286deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1287deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1288deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1289deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1290deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1291deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1292deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1293deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1294deb5c680SPyun YongHyeon 
1295deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1296deb5c680SPyun YongHyeon 		/*
1297deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1298deb5c680SPyun YongHyeon 		 * address from EEPROM.
1299deb5c680SPyun YongHyeon 		 */
1300deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1301deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1302deb5c680SPyun YongHyeon 	} else {
1303141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1304ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1305a94100faSBill Paul 		if (re_did != 0x8129)
1306141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1307a94100faSBill Paul 
1308a94100faSBill Paul 		/*
1309a94100faSBill Paul 		 * Get station address from the EEPROM.
1310a94100faSBill Paul 		 */
1311ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1312be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1313be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1314be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1315deb5c680SPyun YongHyeon 	}
1316ed510fb0SBill Paul 
1317ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1318d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1319ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1320ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1321d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1322d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1323ed510fb0SBill Paul 	} else {
1324d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1325ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1326ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1327d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1328d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1329abc8ff44SBill Paul 	}
13309bac70b8SBill Paul 
1331a94100faSBill Paul 	error = re_allocmem(dev, sc);
1332a94100faSBill Paul 	if (error)
1333a94100faSBill Paul 		goto fail;
1334a94100faSBill Paul 
1335cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1336cd036ec1SBrooks Davis 	if (ifp == NULL) {
1337d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1338cd036ec1SBrooks Davis 		error = ENOSPC;
1339cd036ec1SBrooks Davis 		goto fail;
1340cd036ec1SBrooks Davis 	}
1341cd036ec1SBrooks Davis 
1342351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1343351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1344351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1345351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1346351a76f9SPyun YongHyeon 	}
1347351a76f9SPyun YongHyeon 
1348a94100faSBill Paul 	/* Do MII setup */
1349a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1350a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1351d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1352a94100faSBill Paul 		error = ENXIO;
1353a94100faSBill Paul 		goto fail;
1354a94100faSBill Paul 	}
1355a94100faSBill Paul 
1356a94100faSBill Paul 	ifp->if_softc = sc;
13579bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1358a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1359a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1360a94100faSBill Paul 	ifp->if_start = re_start;
1361deb5c680SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1362deb5c680SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1363498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1364a94100faSBill Paul 	ifp->if_init = re_init;
136552732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
136652732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
136752732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1368a94100faSBill Paul 
1369ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1370ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1371ed510fb0SBill Paul 
1372a94100faSBill Paul 	/*
1373deb5c680SPyun YongHyeon 	 * XXX
1374deb5c680SPyun YongHyeon 	 * Still have no idea how to make TSO work on 8168C, 8168CP,
1375deb5c680SPyun YongHyeon 	 * 8111C and 8111CP.
1376deb5c680SPyun YongHyeon 	 */
1377deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1378deb5c680SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
1379deb5c680SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4;
1380deb5c680SPyun YongHyeon 	}
1381deb5c680SPyun YongHyeon 
1382deb5c680SPyun YongHyeon 	/*
1383a94100faSBill Paul 	 * Call MI attach routine.
1384a94100faSBill Paul 	 */
1385a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1386a94100faSBill Paul 
1387960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1388960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1389960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1390960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
13917467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
13927467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
13937467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1394960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1395960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1396960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1397960fd5b3SPyun YongHyeon #endif
1398960fd5b3SPyun YongHyeon 	/*
1399960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1400960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1401960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1402960fd5b3SPyun YongHyeon 	 */
1403960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1404960fd5b3SPyun YongHyeon 
1405ed510fb0SBill Paul #ifdef RE_DIAG
1406ed510fb0SBill Paul 	/*
1407ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1408ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1409ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1410ed510fb0SBill Paul 	 */
1411a94100faSBill Paul 
1412ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1413ed510fb0SBill Paul 		error = re_diag(sc);
1414a94100faSBill Paul 		if (error) {
1415ed510fb0SBill Paul 			device_printf(dev,
1416ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1417a94100faSBill Paul 			ether_ifdetach(ifp);
1418a94100faSBill Paul 			goto fail;
1419a94100faSBill Paul 		}
1420ed510fb0SBill Paul 	}
1421ed510fb0SBill Paul #endif
1422a94100faSBill Paul 
1423a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1424351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
14255774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14265774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14275774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14285774c5ffSPyun YongHyeon 	else {
14295774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14305774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14315774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14325774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14335774c5ffSPyun YongHyeon 			if (error != 0)
14345774c5ffSPyun YongHyeon 				break;
14355774c5ffSPyun YongHyeon 		}
14365774c5ffSPyun YongHyeon 	}
1437a94100faSBill Paul 	if (error) {
1438d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1439a94100faSBill Paul 		ether_ifdetach(ifp);
1440a94100faSBill Paul 	}
1441a94100faSBill Paul 
1442a94100faSBill Paul fail:
1443ed510fb0SBill Paul 
1444a94100faSBill Paul 	if (error)
1445a94100faSBill Paul 		re_detach(dev);
1446a94100faSBill Paul 
1447a94100faSBill Paul 	return (error);
1448a94100faSBill Paul }
1449a94100faSBill Paul 
1450a94100faSBill Paul /*
1451a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1452a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1453a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1454a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1455a94100faSBill Paul  * allocated.
1456a94100faSBill Paul  */
1457a94100faSBill Paul static int
1458a94100faSBill Paul re_detach(dev)
1459a94100faSBill Paul 	device_t		dev;
1460a94100faSBill Paul {
1461a94100faSBill Paul 	struct rl_softc		*sc;
1462a94100faSBill Paul 	struct ifnet		*ifp;
14635774c5ffSPyun YongHyeon 	int			i, rid;
1464a94100faSBill Paul 
1465a94100faSBill Paul 	sc = device_get_softc(dev);
1466fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1467aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
146897b9d4baSJohn-Mark Gurney 
146981cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
147081cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
147140929967SGleb Smirnoff #ifdef DEVICE_POLLING
147240929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
147340929967SGleb Smirnoff 			ether_poll_deregister(ifp);
147440929967SGleb Smirnoff #endif
147597b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
147697b9d4baSJohn-Mark Gurney #if 0
147797b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
147897b9d4baSJohn-Mark Gurney #endif
1479a94100faSBill Paul 		re_stop(sc);
1480525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1481d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14823d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14833d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1484a94100faSBill Paul 		/*
1485a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1486a94100faSBill Paul 		 * still had a BPF descriptor attached to this
148797b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1488a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1489a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1490a94100faSBill Paul 		 * which will try to call re_init() again. This will
1491a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1492a94100faSBill Paul 		 * which will panic the system when the kernel tries
1493a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1494a94100faSBill Paul 		 * anymore.
1495a94100faSBill Paul 		 */
1496a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1497525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1498a94100faSBill Paul 	}
1499a94100faSBill Paul 	if (sc->rl_miibus)
1500a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1501a94100faSBill Paul 	bus_generic_detach(dev);
1502a94100faSBill Paul 
150397b9d4baSJohn-Mark Gurney 	/*
150497b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
150597b9d4baSJohn-Mark Gurney 	 * stopped here.
150697b9d4baSJohn-Mark Gurney 	 */
150797b9d4baSJohn-Mark Gurney 
15085774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
15095774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
15105774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
15115774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
15125774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
15135774c5ffSPyun YongHyeon 		}
15145774c5ffSPyun YongHyeon 	}
1515ad4f426eSWarner Losh 	if (ifp != NULL)
1516ad4f426eSWarner Losh 		if_free(ifp);
1517351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
15185774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
15195774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
15205774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
15215774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
15225774c5ffSPyun YongHyeon 		}
15235774c5ffSPyun YongHyeon 	} else {
15245774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15255774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15265774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15275774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15285774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15295774c5ffSPyun YongHyeon 			}
15305774c5ffSPyun YongHyeon 		}
15315774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15325774c5ffSPyun YongHyeon 	}
1533a94100faSBill Paul 	if (sc->rl_res)
1534ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1535ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1536a94100faSBill Paul 
1537a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1538a94100faSBill Paul 
1539a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1540a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1541a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1542a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1543a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1544a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1545a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1546a94100faSBill Paul 	}
1547a94100faSBill Paul 
1548a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1549a94100faSBill Paul 
1550a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1551a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1552a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1553a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1554a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1555a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1556a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1557a94100faSBill Paul 	}
1558a94100faSBill Paul 
1559a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1560a94100faSBill Paul 
1561d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1562d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1563d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1564d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1565d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1566d65abd66SPyun YongHyeon 	}
1567d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1568d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1569d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1570d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1571d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1572d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1573d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1574d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1575a94100faSBill Paul 	}
1576a94100faSBill Paul 
1577a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1578a94100faSBill Paul 
1579a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1580a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1581a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1582a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1583a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1584a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1585a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1586a94100faSBill Paul 	}
1587a94100faSBill Paul 
1588a94100faSBill Paul 	if (sc->rl_parent_tag)
1589a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1590a94100faSBill Paul 
1591a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1592a94100faSBill Paul 
1593a94100faSBill Paul 	return (0);
1594a94100faSBill Paul }
1595a94100faSBill Paul 
1596d65abd66SPyun YongHyeon static __inline void
1597d65abd66SPyun YongHyeon re_discard_rxbuf(sc, idx)
1598a94100faSBill Paul 	struct rl_softc		*sc;
1599a94100faSBill Paul 	int			idx;
1600a94100faSBill Paul {
1601d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1602d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1603d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1604a94100faSBill Paul 
1605d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1606d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1607d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1608d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1609d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1610d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1611d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1612d65abd66SPyun YongHyeon }
1613d65abd66SPyun YongHyeon 
1614d65abd66SPyun YongHyeon static int
1615d65abd66SPyun YongHyeon re_newbuf(sc, idx)
1616d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
1617d65abd66SPyun YongHyeon 	int			idx;
1618d65abd66SPyun YongHyeon {
1619d65abd66SPyun YongHyeon 	struct mbuf		*m;
1620d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1621d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1622d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1623d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1624d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1625d65abd66SPyun YongHyeon 	int			error, nsegs;
1626d65abd66SPyun YongHyeon 
1627d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1628d65abd66SPyun YongHyeon 	if (m == NULL)
1629a94100faSBill Paul 		return (ENOBUFS);
1630a94100faSBill Paul 
1631a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
163222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
163322a11c96SJohn-Mark Gurney 	/*
163422a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
163522a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
163622a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
163722a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
163822a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
163922a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
164022a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
164122a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
164222a11c96SJohn-Mark Gurney 	 */
164322a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
164422a11c96SJohn-Mark Gurney #endif
1645d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1646d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1647d65abd66SPyun YongHyeon 	if (error != 0) {
1648d65abd66SPyun YongHyeon 		m_freem(m);
1649d65abd66SPyun YongHyeon 		return (ENOBUFS);
1650d65abd66SPyun YongHyeon 	}
1651d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1652a94100faSBill Paul 
1653d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1654d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1655d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1656d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1657d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1658a94100faSBill Paul 	}
1659a94100faSBill Paul 
1660d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1661d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1662d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1663d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1664d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1665d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1666a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1667a94100faSBill Paul 
1668d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1669d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1670d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1671d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1672d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1673d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1674d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1675d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1676d65abd66SPyun YongHyeon 
1677a94100faSBill Paul 	return (0);
1678a94100faSBill Paul }
1679a94100faSBill Paul 
168022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
168122a11c96SJohn-Mark Gurney static __inline void
168222a11c96SJohn-Mark Gurney re_fixup_rx(m)
168322a11c96SJohn-Mark Gurney 	struct mbuf		*m;
168422a11c96SJohn-Mark Gurney {
168522a11c96SJohn-Mark Gurney 	int                     i;
168622a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
168722a11c96SJohn-Mark Gurney 
168822a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
168922a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
169022a11c96SJohn-Mark Gurney 
169122a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
169222a11c96SJohn-Mark Gurney 		*dst++ = *src++;
169322a11c96SJohn-Mark Gurney 
169422a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
169522a11c96SJohn-Mark Gurney 
169622a11c96SJohn-Mark Gurney 	return;
169722a11c96SJohn-Mark Gurney }
169822a11c96SJohn-Mark Gurney #endif
169922a11c96SJohn-Mark Gurney 
1700a94100faSBill Paul static int
1701a94100faSBill Paul re_tx_list_init(sc)
1702a94100faSBill Paul 	struct rl_softc		*sc;
1703a94100faSBill Paul {
1704d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1705d65abd66SPyun YongHyeon 	int			i;
170697b9d4baSJohn-Mark Gurney 
170797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
170897b9d4baSJohn-Mark Gurney 
1709d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1710d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1711d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1712d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1713d65abd66SPyun YongHyeon 	/* Set EOR. */
1714d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1715d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1716a94100faSBill Paul 
1717a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1718d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1719d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1720d65abd66SPyun YongHyeon 
1721a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1722a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1723d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1724a94100faSBill Paul 
1725a94100faSBill Paul 	return (0);
1726a94100faSBill Paul }
1727a94100faSBill Paul 
1728a94100faSBill Paul static int
1729a94100faSBill Paul re_rx_list_init(sc)
1730a94100faSBill Paul 	struct rl_softc		*sc;
1731a94100faSBill Paul {
1732d65abd66SPyun YongHyeon 	int			error, i;
1733a94100faSBill Paul 
1734d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1735d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1736d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1737d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1738d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1739d65abd66SPyun YongHyeon 			return (error);
1740a94100faSBill Paul 	}
1741a94100faSBill Paul 
1742a94100faSBill Paul 	/* Flush the RX descriptors */
1743a94100faSBill Paul 
1744a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1745a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1746a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1747a94100faSBill Paul 
1748a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1749a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1750a94100faSBill Paul 
1751a94100faSBill Paul 	return (0);
1752a94100faSBill Paul }
1753a94100faSBill Paul 
1754a94100faSBill Paul /*
1755a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1756a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1757a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1758a94100faSBill Paul  */
1759ed510fb0SBill Paul static int
1760a94100faSBill Paul re_rxeof(sc)
1761a94100faSBill Paul 	struct rl_softc		*sc;
1762a94100faSBill Paul {
1763a94100faSBill Paul 	struct mbuf		*m;
1764a94100faSBill Paul 	struct ifnet		*ifp;
1765a94100faSBill Paul 	int			i, total_len;
1766a94100faSBill Paul 	struct rl_desc		*cur_rx;
1767a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1768ed510fb0SBill Paul 	int			maxpkt = 16;
1769a94100faSBill Paul 
17705120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17715120abbfSSam Leffler 
1772fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1773a94100faSBill Paul 
1774a94100faSBill Paul 	/* Invalidate the descriptor memory */
1775a94100faSBill Paul 
1776a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1777a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1778d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1779a94100faSBill Paul 
1780d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1781d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1782a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1783a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1784d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1785d65abd66SPyun YongHyeon 			break;
1786d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1787a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1788d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1789a94100faSBill Paul 
1790a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1791d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1792d65abd66SPyun YongHyeon 				/*
1793d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1794d65abd66SPyun YongHyeon 				 * discard all the pieces.
1795d65abd66SPyun YongHyeon 				 */
1796d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1797d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1798d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1799d65abd66SPyun YongHyeon 				}
1800d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1801d65abd66SPyun YongHyeon 				continue;
1802d65abd66SPyun YongHyeon 			}
180322a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1804a94100faSBill Paul 			if (sc->rl_head == NULL)
1805a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1806a94100faSBill Paul 			else {
1807a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1808a94100faSBill Paul 				sc->rl_tail->m_next = m;
1809a94100faSBill Paul 				sc->rl_tail = m;
1810a94100faSBill Paul 			}
1811a94100faSBill Paul 			continue;
1812a94100faSBill Paul 		}
1813a94100faSBill Paul 
1814a94100faSBill Paul 		/*
1815a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1816a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1817a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1818a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1819a94100faSBill Paul 		 * were already used, so to make room for the extra
1820a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1821a94100faSBill Paul 		 * error' bit and shifted the other status bits
1822a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1823a94100faSBill Paul 		 * still in the same places. We have already extracted
1824a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1825a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1826a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1827a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1828a94100faSBill Paul 		 * same format as that of the 8139C+.
1829a94100faSBill Paul 		 */
1830a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1831a94100faSBill Paul 			rxstat >>= 1;
1832a94100faSBill Paul 
183322a11c96SJohn-Mark Gurney 		/*
183422a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
183522a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
183622a11c96SJohn-Mark Gurney 		 */
183722a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
183822a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1839a94100faSBill Paul 			ifp->if_ierrors++;
1840a94100faSBill Paul 			/*
1841a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1842a94100faSBill Paul 			 * discard all the pieces.
1843a94100faSBill Paul 			 */
1844a94100faSBill Paul 			if (sc->rl_head != NULL) {
1845a94100faSBill Paul 				m_freem(sc->rl_head);
1846a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1847a94100faSBill Paul 			}
1848d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1849a94100faSBill Paul 			continue;
1850a94100faSBill Paul 		}
1851a94100faSBill Paul 
1852a94100faSBill Paul 		/*
1853a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1854a94100faSBill Paul 		 * reload the current one.
1855a94100faSBill Paul 		 */
1856a94100faSBill Paul 
1857d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1858d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1859a94100faSBill Paul 			if (sc->rl_head != NULL) {
1860a94100faSBill Paul 				m_freem(sc->rl_head);
1861a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1862a94100faSBill Paul 			}
1863d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1864a94100faSBill Paul 			continue;
1865a94100faSBill Paul 		}
1866a94100faSBill Paul 
1867a94100faSBill Paul 		if (sc->rl_head != NULL) {
186822a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
186922a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
187022a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1871a94100faSBill Paul 			/*
1872a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1873a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1874a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1875a94100faSBill Paul 			 * care about anyway.
1876a94100faSBill Paul 			 */
1877a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1878a94100faSBill Paul 				sc->rl_tail->m_len -=
1879a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1880a94100faSBill Paul 				m_freem(m);
1881a94100faSBill Paul 			} else {
1882a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1883a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1884a94100faSBill Paul 				sc->rl_tail->m_next = m;
1885a94100faSBill Paul 			}
1886a94100faSBill Paul 			m = sc->rl_head;
1887a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1888a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1889a94100faSBill Paul 		} else
1890a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1891a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1892a94100faSBill Paul 
189322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
189422a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
189522a11c96SJohn-Mark Gurney #endif
1896a94100faSBill Paul 		ifp->if_ipackets++;
1897a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1898a94100faSBill Paul 
1899a94100faSBill Paul 		/* Do RX checksumming if enabled */
1900a94100faSBill Paul 
1901a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1902deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1903a94100faSBill Paul 				/* Check IP header checksum */
1904a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
1905deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1906deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1907a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1908deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1909deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1910a94100faSBill Paul 
1911a94100faSBill Paul 				/* Check TCP/UDP checksum */
1912a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
1913a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1914a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
1915a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1916a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
1917a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1918a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1919a94100faSBill Paul 				}
1920deb5c680SPyun YongHyeon 			} else {
1921deb5c680SPyun YongHyeon 				/*
1922deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
1923deb5c680SPyun YongHyeon 				 */
1924deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
1925deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1926deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1927deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1928deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
1929deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1930deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1931deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1932deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
1933deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1934deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
1935deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1936deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1937deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1938deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
1939deb5c680SPyun YongHyeon 				}
1940deb5c680SPyun YongHyeon 			}
1941a94100faSBill Paul 		}
1942ed510fb0SBill Paul 		maxpkt--;
1943d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
194478ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
194578ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
194678ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1947d147662cSGleb Smirnoff 		}
19485120abbfSSam Leffler 		RL_UNLOCK(sc);
1949a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
19505120abbfSSam Leffler 		RL_LOCK(sc);
1951a94100faSBill Paul 	}
1952a94100faSBill Paul 
1953a94100faSBill Paul 	/* Flush the RX DMA ring */
1954a94100faSBill Paul 
1955a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1956a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1957a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1958a94100faSBill Paul 
1959a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1960ed510fb0SBill Paul 
1961ed510fb0SBill Paul 	if (maxpkt)
1962ed510fb0SBill Paul 		return(EAGAIN);
1963ed510fb0SBill Paul 
1964ed510fb0SBill Paul 	return(0);
1965a94100faSBill Paul }
1966a94100faSBill Paul 
1967a94100faSBill Paul static void
1968a94100faSBill Paul re_txeof(sc)
1969a94100faSBill Paul 	struct rl_softc		*sc;
1970a94100faSBill Paul {
1971a94100faSBill Paul 	struct ifnet		*ifp;
1972d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
1973a94100faSBill Paul 	u_int32_t		txstat;
1974d65abd66SPyun YongHyeon 	int			cons;
1975d65abd66SPyun YongHyeon 
1976d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
1977d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
1978d65abd66SPyun YongHyeon 		return;
1979a94100faSBill Paul 
1980fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1981a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1982a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1983a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1984d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1985a94100faSBill Paul 
1986d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
1987d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
1988d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
1989d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
1990a94100faSBill Paul 			break;
1991a94100faSBill Paul 		/*
1992a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1993a94100faSBill Paul 		 * in a fragment chain, which also happens to
1994a94100faSBill Paul 		 * be the only place where the TX status bits
1995a94100faSBill Paul 		 * are valid.
1996a94100faSBill Paul 		 */
1997a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1998d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
1999d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2000d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2001d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2002d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2003d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2004d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2005d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2006d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2007a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2008a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2009a94100faSBill Paul 				ifp->if_collisions++;
2010a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2011a94100faSBill Paul 				ifp->if_oerrors++;
2012a94100faSBill Paul 			else
2013a94100faSBill Paul 				ifp->if_opackets++;
2014a94100faSBill Paul 		}
2015a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2016d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2017a94100faSBill Paul 	}
2018d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2019a94100faSBill Paul 
2020a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2021a94100faSBill Paul 
2022d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
20230fc4974fSBill Paul 		/*
2024b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
2025b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
2026b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
2027b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
2028b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
2029b4b95879SMarius Strobl 		 * be required with the PCIe devices.
20300fc4974fSBill Paul 		 */
20310fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
20320fc4974fSBill Paul 
2033ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2034a94100faSBill Paul 		/*
2035b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2036b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2037a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2038a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2039a94100faSBill Paul 		 */
2040a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2041ed510fb0SBill Paul #endif
2042b4b95879SMarius Strobl 	} else
2043b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2044a94100faSBill Paul }
2045a94100faSBill Paul 
2046a94100faSBill Paul static void
2047a94100faSBill Paul re_tick(xsc)
2048a94100faSBill Paul 	void			*xsc;
2049a94100faSBill Paul {
2050a94100faSBill Paul 	struct rl_softc		*sc;
2051d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2052ed510fb0SBill Paul 	struct ifnet		*ifp;
2053a94100faSBill Paul 
2054a94100faSBill Paul 	sc = xsc;
2055ed510fb0SBill Paul 	ifp = sc->rl_ifp;
205697b9d4baSJohn-Mark Gurney 
205797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
205897b9d4baSJohn-Mark Gurney 
20591d545c7aSMarius Strobl 	re_watchdog(sc);
2060a94100faSBill Paul 
20611d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2062a94100faSBill Paul 	mii_tick(mii);
2063351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) != 0) {
2064ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
2065351a76f9SPyun YongHyeon 			sc->rl_flags &= ~RL_FLAG_LINK;
2066ed510fb0SBill Paul 	} else {
2067ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
2068ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2069351a76f9SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
2070ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2071ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
2072ed510fb0SBill Paul 				    &sc->rl_txtask);
2073ed510fb0SBill Paul 		}
2074ed510fb0SBill Paul 	}
2075a94100faSBill Paul 
2076d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2077a94100faSBill Paul }
2078a94100faSBill Paul 
2079a94100faSBill Paul #ifdef DEVICE_POLLING
2080a94100faSBill Paul static void
2081a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2082a94100faSBill Paul {
2083a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
2084a94100faSBill Paul 
2085a94100faSBill Paul 	RL_LOCK(sc);
208640929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
208797b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
208897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
208997b9d4baSJohn-Mark Gurney }
209097b9d4baSJohn-Mark Gurney 
209197b9d4baSJohn-Mark Gurney static void
209297b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
209397b9d4baSJohn-Mark Gurney {
209497b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
209597b9d4baSJohn-Mark Gurney 
209697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
209797b9d4baSJohn-Mark Gurney 
2098a94100faSBill Paul 	sc->rxcycles = count;
2099a94100faSBill Paul 	re_rxeof(sc);
2100a94100faSBill Paul 	re_txeof(sc);
2101a94100faSBill Paul 
210237652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2103ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2104a94100faSBill Paul 
2105a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2106a94100faSBill Paul 		u_int16_t       status;
2107a94100faSBill Paul 
2108a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2109a94100faSBill Paul 		if (status == 0xffff)
211097b9d4baSJohn-Mark Gurney 			return;
2111a94100faSBill Paul 		if (status)
2112a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2113a94100faSBill Paul 
2114a94100faSBill Paul 		/*
2115a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2116a94100faSBill Paul 		 */
2117a94100faSBill Paul 
2118a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
2119a94100faSBill Paul 			re_reset(sc);
212097b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2121a94100faSBill Paul 		}
2122a94100faSBill Paul 	}
2123a94100faSBill Paul }
2124a94100faSBill Paul #endif /* DEVICE_POLLING */
2125a94100faSBill Paul 
2126ef544f63SPaolo Pisati static int
2127a94100faSBill Paul re_intr(arg)
2128a94100faSBill Paul 	void			*arg;
2129a94100faSBill Paul {
2130a94100faSBill Paul 	struct rl_softc		*sc;
2131ed510fb0SBill Paul 	uint16_t		status;
2132a94100faSBill Paul 
2133a94100faSBill Paul 	sc = arg;
2134ed510fb0SBill Paul 
2135ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2136498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2137ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2138ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2139ed510fb0SBill Paul 
2140ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2141ed510fb0SBill Paul 
2142ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2143ed510fb0SBill Paul }
2144ed510fb0SBill Paul 
2145ed510fb0SBill Paul static void
2146ed510fb0SBill Paul re_int_task(arg, npending)
2147ed510fb0SBill Paul 	void			*arg;
2148ed510fb0SBill Paul 	int			npending;
2149ed510fb0SBill Paul {
2150ed510fb0SBill Paul 	struct rl_softc		*sc;
2151ed510fb0SBill Paul 	struct ifnet		*ifp;
2152ed510fb0SBill Paul 	u_int16_t		status;
2153ed510fb0SBill Paul 	int			rval = 0;
2154ed510fb0SBill Paul 
2155ed510fb0SBill Paul 	sc = arg;
2156ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2157a94100faSBill Paul 
2158a94100faSBill Paul 	RL_LOCK(sc);
215997b9d4baSJohn-Mark Gurney 
2160a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2161a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2162a94100faSBill Paul 
2163d65abd66SPyun YongHyeon 	if (sc->suspended ||
2164d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2165ed510fb0SBill Paul 		RL_UNLOCK(sc);
2166ed510fb0SBill Paul 		return;
2167ed510fb0SBill Paul 	}
2168a94100faSBill Paul 
2169ed510fb0SBill Paul #ifdef DEVICE_POLLING
2170ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2171ed510fb0SBill Paul 		RL_UNLOCK(sc);
2172ed510fb0SBill Paul 		return;
2173ed510fb0SBill Paul 	}
2174ed510fb0SBill Paul #endif
2175a94100faSBill Paul 
2176ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2177ed510fb0SBill Paul 		rval = re_rxeof(sc);
2178ed510fb0SBill Paul 
2179ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2180ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
2181ed510fb0SBill Paul #else
2182ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
2183ed510fb0SBill Paul #endif
2184ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2185a94100faSBill Paul 		re_txeof(sc);
2186a94100faSBill Paul 
2187a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
2188a94100faSBill Paul 		re_reset(sc);
218997b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2190a94100faSBill Paul 	}
2191a94100faSBill Paul 
2192a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
2193d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
2194d1754a9bSJohn Baldwin 		re_tick(sc);
2195a94100faSBill Paul 	}
2196a94100faSBill Paul 
219752732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2198ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2199a94100faSBill Paul 
2200a94100faSBill Paul 	RL_UNLOCK(sc);
2201ed510fb0SBill Paul 
2202ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2203ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2204ed510fb0SBill Paul 		return;
2205ed510fb0SBill Paul 	}
2206ed510fb0SBill Paul 
2207ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2208ed510fb0SBill Paul 
2209ed510fb0SBill Paul 	return;
2210a94100faSBill Paul }
2211a94100faSBill Paul 
2212d65abd66SPyun YongHyeon static int
2213d65abd66SPyun YongHyeon re_encap(sc, m_head)
2214d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
2215d65abd66SPyun YongHyeon 	struct mbuf		**m_head;
2216d65abd66SPyun YongHyeon {
2217d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2218d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2219d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2220d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2221d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2222d65abd66SPyun YongHyeon 	int			nsegs, prod;
2223d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2224d65abd66SPyun YongHyeon 	int			padlen;
2225ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2226a94100faSBill Paul 
2227d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2228738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
22290fc4974fSBill Paul 
22300fc4974fSBill Paul 	/*
22310fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
22320fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
22330fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
22340fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
22350fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
22360fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
223799c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
22380fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2239d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
22400fc4974fSBill Paul 	 */
2241deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0 &&
2242deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
224399c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2244d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2245d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2246d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2247d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2248d65abd66SPyun YongHyeon 			m_freem(*m_head);
2249d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2250d65abd66SPyun YongHyeon 				*m_head = NULL;
2251a94100faSBill Paul 				return (ENOBUFS);
2252a94100faSBill Paul 			}
2253d65abd66SPyun YongHyeon 			*m_head = m_new;
2254d65abd66SPyun YongHyeon 		}
2255d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2256d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
225780a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2258b4b95879SMarius Strobl 			if (m_new == NULL) {
2259b4b95879SMarius Strobl 				m_freem(*m_head);
2260b4b95879SMarius Strobl 				*m_head = NULL;
226180a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2262b4b95879SMarius Strobl 			}
2263d65abd66SPyun YongHyeon 		} else
2264d65abd66SPyun YongHyeon 			m_new = *m_head;
2265a94100faSBill Paul 
22660fc4974fSBill Paul 		/*
22670fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
22680fc4974fSBill Paul 		 * to avoid leaking data.
22690fc4974fSBill Paul 		 */
2270d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2271d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
22720fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2273d65abd66SPyun YongHyeon 		*m_head = m_new;
22740fc4974fSBill Paul 	}
22750fc4974fSBill Paul 
2276d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2277d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2278d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2279d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2280d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2281304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2282d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2283d65abd66SPyun YongHyeon 			m_freem(*m_head);
2284b4b95879SMarius Strobl 			*m_head = NULL;
2285d65abd66SPyun YongHyeon 			return (ENOBUFS);
2286a94100faSBill Paul 		}
2287d65abd66SPyun YongHyeon 		*m_head = m_new;
2288d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2289d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2290d65abd66SPyun YongHyeon 		if (error != 0) {
2291d65abd66SPyun YongHyeon 			m_freem(*m_head);
2292d65abd66SPyun YongHyeon 			*m_head = NULL;
2293d65abd66SPyun YongHyeon 			return (error);
2294a94100faSBill Paul 		}
2295d65abd66SPyun YongHyeon 	} else if (error != 0)
2296d65abd66SPyun YongHyeon 		return (error);
2297d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2298d65abd66SPyun YongHyeon 		m_freem(*m_head);
2299d65abd66SPyun YongHyeon 		*m_head = NULL;
2300d65abd66SPyun YongHyeon 		return (EIO);
2301d65abd66SPyun YongHyeon 	}
2302d65abd66SPyun YongHyeon 
2303d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2304d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2305d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2306d65abd66SPyun YongHyeon 		return (ENOBUFS);
2307d65abd66SPyun YongHyeon 	}
2308d65abd66SPyun YongHyeon 
2309d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2310d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2311a94100faSBill Paul 
2312a94100faSBill Paul 	/*
2313d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2314d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2315d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2316d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2317a94100faSBill Paul 	 */
2318deb5c680SPyun YongHyeon 	vlanctl = 0;
2319d65abd66SPyun YongHyeon 	csum_flags = 0;
2320d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2321d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2322d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2323d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2324d65abd66SPyun YongHyeon 	else {
232599c8ae87SPyun YongHyeon 		/*
232699c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
232799c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
232899c8ae87SPyun YongHyeon 		 * does't make effects.
232999c8ae87SPyun YongHyeon 		 */
233099c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2331deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2332d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2333deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2334deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2335d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2336deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2337deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2338d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2339deb5c680SPyun YongHyeon 			} else {
2340deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2341deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2342deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2343deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2344deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2345deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2346deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2347deb5c680SPyun YongHyeon 			}
2348d65abd66SPyun YongHyeon 		}
234999c8ae87SPyun YongHyeon 	}
2350a94100faSBill Paul 
2351ccf34c81SPyun YongHyeon 	/*
2352ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2353ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2354ccf34c81SPyun YongHyeon 	 * transmission attempt.
2355ccf34c81SPyun YongHyeon 	 */
2356ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2357deb5c680SPyun YongHyeon 		vlanctl |= htons((*m_head)->m_pkthdr.ether_vtag) |
2358deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2359ccf34c81SPyun YongHyeon 
2360d65abd66SPyun YongHyeon 	si = prod;
2361d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2362d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2363deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2364d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2365d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2366d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2367d65abd66SPyun YongHyeon 		if (i != 0)
2368d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2369d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2370d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2371d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2372d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2373d65abd66SPyun YongHyeon 	}
2374d65abd66SPyun YongHyeon 	/* Update producer index. */
2375d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2376a94100faSBill Paul 
2377d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2378d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2379d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2380d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2381d65abd66SPyun YongHyeon 
2382d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2383d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2384d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2385a94100faSBill Paul 
2386d65abd66SPyun YongHyeon 	/*
2387d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2388d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2389d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2390d65abd66SPyun YongHyeon 	 */
2391d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2392d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2393d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2394d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2395d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2396a94100faSBill Paul 
2397a94100faSBill Paul 	return (0);
2398a94100faSBill Paul }
2399a94100faSBill Paul 
240097b9d4baSJohn-Mark Gurney static void
2401ed510fb0SBill Paul re_tx_task(arg, npending)
2402ed510fb0SBill Paul 	void			*arg;
2403ed510fb0SBill Paul 	int			npending;
240497b9d4baSJohn-Mark Gurney {
2405ed510fb0SBill Paul 	struct ifnet		*ifp;
240697b9d4baSJohn-Mark Gurney 
2407ed510fb0SBill Paul 	ifp = arg;
2408ed510fb0SBill Paul 	re_start(ifp);
2409ed510fb0SBill Paul 
2410ed510fb0SBill Paul 	return;
241197b9d4baSJohn-Mark Gurney }
241297b9d4baSJohn-Mark Gurney 
2413a94100faSBill Paul /*
2414a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2415a94100faSBill Paul  */
2416a94100faSBill Paul static void
2417ed510fb0SBill Paul re_start(ifp)
2418a94100faSBill Paul 	struct ifnet		*ifp;
2419a94100faSBill Paul {
2420a94100faSBill Paul 	struct rl_softc		*sc;
2421d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2422d65abd66SPyun YongHyeon 	int			queued;
2423a94100faSBill Paul 
2424a94100faSBill Paul 	sc = ifp->if_softc;
242597b9d4baSJohn-Mark Gurney 
2426ed510fb0SBill Paul 	RL_LOCK(sc);
2427ed510fb0SBill Paul 
2428d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2429351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2430ed510fb0SBill Paul 		RL_UNLOCK(sc);
2431ed510fb0SBill Paul 		return;
2432ed510fb0SBill Paul 	}
2433a94100faSBill Paul 
2434d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2435d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
243652732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2437a94100faSBill Paul 		if (m_head == NULL)
2438a94100faSBill Paul 			break;
2439a94100faSBill Paul 
2440d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2441b4b95879SMarius Strobl 			if (m_head == NULL)
2442b4b95879SMarius Strobl 				break;
244352732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
244413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2445a94100faSBill Paul 			break;
2446a94100faSBill Paul 		}
2447a94100faSBill Paul 
2448a94100faSBill Paul 		/*
2449a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2450a94100faSBill Paul 		 * to him.
2451a94100faSBill Paul 		 */
245259a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
245352732175SMax Laier 
245452732175SMax Laier 		queued++;
2455a94100faSBill Paul 	}
2456a94100faSBill Paul 
2457ed510fb0SBill Paul 	if (queued == 0) {
2458ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2459d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2460ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2461ed510fb0SBill Paul #endif
2462ed510fb0SBill Paul 		RL_UNLOCK(sc);
246352732175SMax Laier 		return;
2464ed510fb0SBill Paul 	}
246552732175SMax Laier 
2466a94100faSBill Paul 	/* Flush the TX descriptors */
2467a94100faSBill Paul 
2468a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2469a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2470a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2471a94100faSBill Paul 
24720fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2473a94100faSBill Paul 
2474ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2475a94100faSBill Paul 	/*
2476a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2477a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2478a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2479a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2480a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2481a94100faSBill Paul 	 * the timer count is reset to 0.
2482a94100faSBill Paul 	 */
2483a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2484ed510fb0SBill Paul #endif
2485a94100faSBill Paul 
2486a94100faSBill Paul 	/*
2487a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2488a94100faSBill Paul 	 */
24891d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2490ed510fb0SBill Paul 
2491ed510fb0SBill Paul 	RL_UNLOCK(sc);
2492ed510fb0SBill Paul 
2493ed510fb0SBill Paul 	return;
2494a94100faSBill Paul }
2495a94100faSBill Paul 
2496a94100faSBill Paul static void
2497a94100faSBill Paul re_init(xsc)
2498a94100faSBill Paul 	void			*xsc;
2499a94100faSBill Paul {
2500a94100faSBill Paul 	struct rl_softc		*sc = xsc;
250197b9d4baSJohn-Mark Gurney 
250297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
250397b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
250497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
250597b9d4baSJohn-Mark Gurney }
250697b9d4baSJohn-Mark Gurney 
250797b9d4baSJohn-Mark Gurney static void
250897b9d4baSJohn-Mark Gurney re_init_locked(sc)
250997b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
251097b9d4baSJohn-Mark Gurney {
2511fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2512a94100faSBill Paul 	struct mii_data		*mii;
2513a94100faSBill Paul 	u_int32_t		rxcfg = 0;
251470acaecfSPyun YongHyeon 	uint16_t		cfg;
25154d3d7085SBernd Walter 	union {
25164d3d7085SBernd Walter 		uint32_t align_dummy;
25174d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
25184d3d7085SBernd Walter         } eaddr;
2519a94100faSBill Paul 
252097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
252197b9d4baSJohn-Mark Gurney 
2522a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2523a94100faSBill Paul 
2524a94100faSBill Paul 	/*
2525a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2526a94100faSBill Paul 	 */
2527a94100faSBill Paul 	re_stop(sc);
2528a94100faSBill Paul 
2529a94100faSBill Paul 	/*
2530c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2531edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2532c2c6548bSBill Paul 	 * before all others.
2533c2c6548bSBill Paul 	 */
253470acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
253570acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
253670acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
253770acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
253870acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2539deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2540deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2541deb5c680SPyun YongHyeon 		/* XXX magic. */
2542deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2543deb5c680SPyun YongHyeon 	} else
2544deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2545deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
2546c2c6548bSBill Paul 
2547c2c6548bSBill Paul 	/*
2548a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2549a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2550a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2551a94100faSBill Paul 	 */
25524d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
25534d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2554a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2555ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2556ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2557ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2558ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2559a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2560a94100faSBill Paul 
2561a94100faSBill Paul 	/*
2562a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2563a94100faSBill Paul 	 */
2564a94100faSBill Paul 	re_rx_list_init(sc);
2565a94100faSBill Paul 	re_tx_list_init(sc);
2566a94100faSBill Paul 
2567a94100faSBill Paul 	/*
2568d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2569d01fac16SPyun YongHyeon 	 */
2570d01fac16SPyun YongHyeon 
2571d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2572d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2573d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2574d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2575d01fac16SPyun YongHyeon 
2576d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2577d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2578d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2579d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2580d01fac16SPyun YongHyeon 
2581d01fac16SPyun YongHyeon 	/*
2582a94100faSBill Paul 	 * Enable transmit and receive.
2583a94100faSBill Paul 	 */
2584a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2585a94100faSBill Paul 
2586a94100faSBill Paul 	/*
2587a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2588a94100faSBill Paul 	 */
2589abc8ff44SBill Paul 	if (sc->rl_testmode) {
2590abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2591abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2592abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2593a94100faSBill Paul 		else
2594abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2595abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2596abc8ff44SBill Paul 	} else
2597a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2598d01fac16SPyun YongHyeon 
2599d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2600d01fac16SPyun YongHyeon 
2601a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2602a94100faSBill Paul 
2603a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2604a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2605a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2606a94100faSBill Paul 
2607a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
260861021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2609a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
261061021536SJohn-Mark Gurney 	else
2611a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2612a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2613a94100faSBill Paul 
2614a94100faSBill Paul 	/*
2615a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2616a94100faSBill Paul 	 */
261761021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2618a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
261961021536SJohn-Mark Gurney 	else
2620a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2621a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2622a94100faSBill Paul 
2623a94100faSBill Paul 	/*
2624a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2625a94100faSBill Paul 	 */
2626a94100faSBill Paul 	re_setmulti(sc);
2627a94100faSBill Paul 
2628a94100faSBill Paul #ifdef DEVICE_POLLING
2629a94100faSBill Paul 	/*
2630a94100faSBill Paul 	 * Disable interrupts if we are polling.
2631a94100faSBill Paul 	 */
263240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2633a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2634a94100faSBill Paul 	else	/* otherwise ... */
263540929967SGleb Smirnoff #endif
2636ed510fb0SBill Paul 
2637a94100faSBill Paul 	/*
2638a94100faSBill Paul 	 * Enable interrupts.
2639a94100faSBill Paul 	 */
2640a94100faSBill Paul 	if (sc->rl_testmode)
2641a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2642a94100faSBill Paul 	else
2643a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2644ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2645a94100faSBill Paul 
2646a94100faSBill Paul 	/* Set initial TX threshold */
2647a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2648a94100faSBill Paul 
2649a94100faSBill Paul 	/* Start RX/TX process. */
2650a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2651a94100faSBill Paul #ifdef notdef
2652a94100faSBill Paul 	/* Enable receiver and transmitter. */
2653a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2654a94100faSBill Paul #endif
2655a94100faSBill Paul 
2656ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2657a94100faSBill Paul 	/*
2658a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2659a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2660a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2661a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2662a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2663a94100faSBill Paul 	 */
2664a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2665a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2666a94100faSBill Paul 	else
2667a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2668ed510fb0SBill Paul #endif
2669a94100faSBill Paul 
2670a94100faSBill Paul 	/*
2671a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2672a94100faSBill Paul 	 * size so we can receive jumbo frames.
2673a94100faSBill Paul 	 */
2674a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2675a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2676a94100faSBill Paul 
267797b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2678a94100faSBill Paul 		return;
2679a94100faSBill Paul 
2680a94100faSBill Paul 	mii_mediachg(mii);
2681a94100faSBill Paul 
268219ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2683a94100faSBill Paul 
268413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
268513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2686a94100faSBill Paul 
2687351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
26881d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2689d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2690a94100faSBill Paul }
2691a94100faSBill Paul 
2692a94100faSBill Paul /*
2693a94100faSBill Paul  * Set media options.
2694a94100faSBill Paul  */
2695a94100faSBill Paul static int
2696a94100faSBill Paul re_ifmedia_upd(ifp)
2697a94100faSBill Paul 	struct ifnet		*ifp;
2698a94100faSBill Paul {
2699a94100faSBill Paul 	struct rl_softc		*sc;
2700a94100faSBill Paul 	struct mii_data		*mii;
2701a94100faSBill Paul 
2702a94100faSBill Paul 	sc = ifp->if_softc;
2703a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2704d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2705a94100faSBill Paul 	mii_mediachg(mii);
2706d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2707a94100faSBill Paul 
2708a94100faSBill Paul 	return (0);
2709a94100faSBill Paul }
2710a94100faSBill Paul 
2711a94100faSBill Paul /*
2712a94100faSBill Paul  * Report current media status.
2713a94100faSBill Paul  */
2714a94100faSBill Paul static void
2715a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2716a94100faSBill Paul 	struct ifnet		*ifp;
2717a94100faSBill Paul 	struct ifmediareq	*ifmr;
2718a94100faSBill Paul {
2719a94100faSBill Paul 	struct rl_softc		*sc;
2720a94100faSBill Paul 	struct mii_data		*mii;
2721a94100faSBill Paul 
2722a94100faSBill Paul 	sc = ifp->if_softc;
2723a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2724a94100faSBill Paul 
2725d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2726a94100faSBill Paul 	mii_pollstat(mii);
2727d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2728a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2729a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2730a94100faSBill Paul }
2731a94100faSBill Paul 
2732a94100faSBill Paul static int
2733a94100faSBill Paul re_ioctl(ifp, command, data)
2734a94100faSBill Paul 	struct ifnet		*ifp;
2735a94100faSBill Paul 	u_long			command;
2736a94100faSBill Paul 	caddr_t			data;
2737a94100faSBill Paul {
2738a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2739a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2740a94100faSBill Paul 	struct mii_data		*mii;
274140929967SGleb Smirnoff 	int			error = 0;
2742a94100faSBill Paul 
2743a94100faSBill Paul 	switch (command) {
2744a94100faSBill Paul 	case SIOCSIFMTU:
2745c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2746a94100faSBill Paul 			error = EINVAL;
2747c1d0b573SPyun YongHyeon 			break;
2748c1d0b573SPyun YongHyeon 		}
2749351a76f9SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 &&
2750c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2751c1d0b573SPyun YongHyeon 			error = EINVAL;
2752c1d0b573SPyun YongHyeon 			break;
2753c1d0b573SPyun YongHyeon 		}
2754c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2755c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2756a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2757d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2758a94100faSBill Paul 		break;
2759a94100faSBill Paul 	case SIOCSIFFLAGS:
276097b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2761eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2762eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2763eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
27643021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2765eed497bbSPyun YongHyeon 					re_setmulti(sc);
2766eed497bbSPyun YongHyeon 			} else
276797b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2768eed497bbSPyun YongHyeon 		} else {
2769eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2770a94100faSBill Paul 				re_stop(sc);
2771eed497bbSPyun YongHyeon 		}
2772eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
277397b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2774a94100faSBill Paul 		break;
2775a94100faSBill Paul 	case SIOCADDMULTI:
2776a94100faSBill Paul 	case SIOCDELMULTI:
277797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2778a94100faSBill Paul 		re_setmulti(sc);
277997b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2780a94100faSBill Paul 		break;
2781a94100faSBill Paul 	case SIOCGIFMEDIA:
2782a94100faSBill Paul 	case SIOCSIFMEDIA:
2783a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2784a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2785a94100faSBill Paul 		break;
2786a94100faSBill Paul 	case SIOCSIFCAP:
278740929967SGleb Smirnoff 	    {
2788f051cb85SGleb Smirnoff 		int mask, reinit;
2789f051cb85SGleb Smirnoff 
2790f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2791f051cb85SGleb Smirnoff 		reinit = 0;
279240929967SGleb Smirnoff #ifdef DEVICE_POLLING
279340929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
279440929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
279540929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
279640929967SGleb Smirnoff 				if (error)
279740929967SGleb Smirnoff 					return(error);
2798d1754a9bSJohn Baldwin 				RL_LOCK(sc);
279940929967SGleb Smirnoff 				/* Disable interrupts */
280040929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
280140929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
280240929967SGleb Smirnoff 				RL_UNLOCK(sc);
280340929967SGleb Smirnoff 			} else {
280440929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
280540929967SGleb Smirnoff 				/* Enable interrupts. */
280640929967SGleb Smirnoff 				RL_LOCK(sc);
280740929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
280840929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
280940929967SGleb Smirnoff 				RL_UNLOCK(sc);
281040929967SGleb Smirnoff 			}
281140929967SGleb Smirnoff 		}
281240929967SGleb Smirnoff #endif /* DEVICE_POLLING */
281340929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2814f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2815a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2816dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2817a94100faSBill Paul 			else
2818b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2819f051cb85SGleb Smirnoff 			reinit = 1;
282040929967SGleb Smirnoff 		}
2821f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2822f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2823f051cb85SGleb Smirnoff 			reinit = 1;
2824f051cb85SGleb Smirnoff 		}
2825dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2826dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2827dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2828dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2829dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2830dc74159dSPyun YongHyeon 			else
2831dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2832dc74159dSPyun YongHyeon 		}
28337467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
28347467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
28357467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
28367467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
28377467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
28387467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
28397467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
28407467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
28417467bd53SPyun YongHyeon 		}
2842f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2843f051cb85SGleb Smirnoff 			re_init(sc);
2844960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
284540929967SGleb Smirnoff 	    }
2846a94100faSBill Paul 		break;
2847a94100faSBill Paul 	default:
2848a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2849a94100faSBill Paul 		break;
2850a94100faSBill Paul 	}
2851a94100faSBill Paul 
2852a94100faSBill Paul 	return (error);
2853a94100faSBill Paul }
2854a94100faSBill Paul 
2855a94100faSBill Paul static void
28561d545c7aSMarius Strobl re_watchdog(sc)
2857a94100faSBill Paul 	struct rl_softc		*sc;
28581d545c7aSMarius Strobl {
2859a94100faSBill Paul 
28601d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
28611d545c7aSMarius Strobl 
28621d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
28631d545c7aSMarius Strobl 		return;
28641d545c7aSMarius Strobl 
28651d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
28661d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2867a94100faSBill Paul 
2868a94100faSBill Paul 	re_txeof(sc);
2869a94100faSBill Paul 	re_rxeof(sc);
287097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2871a94100faSBill Paul }
2872a94100faSBill Paul 
2873a94100faSBill Paul /*
2874a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2875a94100faSBill Paul  * RX and TX lists.
2876a94100faSBill Paul  */
2877a94100faSBill Paul static void
2878a94100faSBill Paul re_stop(sc)
2879a94100faSBill Paul 	struct rl_softc		*sc;
2880a94100faSBill Paul {
2881a94100faSBill Paul 	register int		i;
2882a94100faSBill Paul 	struct ifnet		*ifp;
2883d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2884d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2885a94100faSBill Paul 
288697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
288797b9d4baSJohn-Mark Gurney 
2888fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2889a94100faSBill Paul 
28901d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2891d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
289213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2893a94100faSBill Paul 
2894a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2895a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2896ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2897a94100faSBill Paul 
2898a94100faSBill Paul 	if (sc->rl_head != NULL) {
2899a94100faSBill Paul 		m_freem(sc->rl_head);
2900a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2901a94100faSBill Paul 	}
2902a94100faSBill Paul 
2903a94100faSBill Paul 	/* Free the TX list buffers. */
2904a94100faSBill Paul 
2905d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2906d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2907d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2908d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2909d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2910d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2911d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2912d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2913d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2914a94100faSBill Paul 		}
2915a94100faSBill Paul 	}
2916a94100faSBill Paul 
2917a94100faSBill Paul 	/* Free the RX list buffers. */
2918a94100faSBill Paul 
2919d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2920d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2921d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2922d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2923d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2924d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2925d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2926d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2927d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2928a94100faSBill Paul 		}
2929a94100faSBill Paul 	}
2930a94100faSBill Paul }
2931a94100faSBill Paul 
2932a94100faSBill Paul /*
2933a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2934a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2935a94100faSBill Paul  * resume.
2936a94100faSBill Paul  */
2937a94100faSBill Paul static int
2938a94100faSBill Paul re_suspend(dev)
2939a94100faSBill Paul 	device_t		dev;
2940a94100faSBill Paul {
2941a94100faSBill Paul 	struct rl_softc		*sc;
2942a94100faSBill Paul 
2943a94100faSBill Paul 	sc = device_get_softc(dev);
2944a94100faSBill Paul 
294597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2946a94100faSBill Paul 	re_stop(sc);
29477467bd53SPyun YongHyeon 	re_setwol(sc);
2948a94100faSBill Paul 	sc->suspended = 1;
294997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2950a94100faSBill Paul 
2951a94100faSBill Paul 	return (0);
2952a94100faSBill Paul }
2953a94100faSBill Paul 
2954a94100faSBill Paul /*
2955a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2956a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2957a94100faSBill Paul  * appropriate.
2958a94100faSBill Paul  */
2959a94100faSBill Paul static int
2960a94100faSBill Paul re_resume(dev)
2961a94100faSBill Paul 	device_t		dev;
2962a94100faSBill Paul {
2963a94100faSBill Paul 	struct rl_softc		*sc;
2964a94100faSBill Paul 	struct ifnet		*ifp;
2965a94100faSBill Paul 
2966a94100faSBill Paul 	sc = device_get_softc(dev);
296797b9d4baSJohn-Mark Gurney 
296897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
296997b9d4baSJohn-Mark Gurney 
2970fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2971a94100faSBill Paul 
2972a94100faSBill Paul 	/* reinitialize interface if necessary */
2973a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
297497b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2975a94100faSBill Paul 
29767467bd53SPyun YongHyeon 	/*
29777467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
29787467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
29797467bd53SPyun YongHyeon 	 */
29807467bd53SPyun YongHyeon 	re_clrwol(sc);
2981a94100faSBill Paul 	sc->suspended = 0;
298297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2983a94100faSBill Paul 
2984a94100faSBill Paul 	return (0);
2985a94100faSBill Paul }
2986a94100faSBill Paul 
2987a94100faSBill Paul /*
2988a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2989a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2990a94100faSBill Paul  */
29916a087a87SPyun YongHyeon static int
2992a94100faSBill Paul re_shutdown(dev)
2993a94100faSBill Paul 	device_t		dev;
2994a94100faSBill Paul {
2995a94100faSBill Paul 	struct rl_softc		*sc;
2996a94100faSBill Paul 
2997a94100faSBill Paul 	sc = device_get_softc(dev);
2998a94100faSBill Paul 
299997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3000a94100faSBill Paul 	re_stop(sc);
3001536fde34SMaxim Sobolev 	/*
3002536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3003536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
300472293673SRuslan Ermilov 	 * cases.
3005536fde34SMaxim Sobolev 	 */
3006536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
30077467bd53SPyun YongHyeon 	re_setwol(sc);
300897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
30096a087a87SPyun YongHyeon 
30106a087a87SPyun YongHyeon 	return (0);
3011a94100faSBill Paul }
30127467bd53SPyun YongHyeon 
30137467bd53SPyun YongHyeon static void
30147467bd53SPyun YongHyeon re_setwol(sc)
30157467bd53SPyun YongHyeon 	struct rl_softc		*sc;
30167467bd53SPyun YongHyeon {
30177467bd53SPyun YongHyeon 	struct ifnet		*ifp;
30187467bd53SPyun YongHyeon 	int			pmc;
30197467bd53SPyun YongHyeon 	uint16_t		pmstat;
30207467bd53SPyun YongHyeon 	uint8_t			v;
30217467bd53SPyun YongHyeon 
30227467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30237467bd53SPyun YongHyeon 
30247467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30257467bd53SPyun YongHyeon 		return;
30267467bd53SPyun YongHyeon 
30277467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
30287467bd53SPyun YongHyeon 	/* Enable config register write. */
30297467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30307467bd53SPyun YongHyeon 
30317467bd53SPyun YongHyeon 	/* Enable PME. */
30327467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
30337467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
30347467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30357467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
30367467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
30377467bd53SPyun YongHyeon 
30387467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30397467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30407467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
30417467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
30427467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30437467bd53SPyun YongHyeon 
30447467bd53SPyun YongHyeon 	/* Config register write done. */
3045f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30467467bd53SPyun YongHyeon 
30477467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30487467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30497467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30507467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
30517467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
30527467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
30537467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
30547467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30557467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
30567467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30577467bd53SPyun YongHyeon 
30587467bd53SPyun YongHyeon 	/*
30597467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
30607467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
30617467bd53SPyun YongHyeon 	 * needed.
30627467bd53SPyun YongHyeon 	 */
30637467bd53SPyun YongHyeon 
30647467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
30657467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
30667467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
30677467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30687467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
30697467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
30707467bd53SPyun YongHyeon }
30717467bd53SPyun YongHyeon 
30727467bd53SPyun YongHyeon static void
30737467bd53SPyun YongHyeon re_clrwol(sc)
30747467bd53SPyun YongHyeon 	struct rl_softc		*sc;
30757467bd53SPyun YongHyeon {
30767467bd53SPyun YongHyeon 	int			pmc;
30777467bd53SPyun YongHyeon 	uint8_t			v;
30787467bd53SPyun YongHyeon 
30797467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30807467bd53SPyun YongHyeon 
30817467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30827467bd53SPyun YongHyeon 		return;
30837467bd53SPyun YongHyeon 
30847467bd53SPyun YongHyeon 	/* Enable config register write. */
30857467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30867467bd53SPyun YongHyeon 
30877467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30887467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30897467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30907467bd53SPyun YongHyeon 
30917467bd53SPyun YongHyeon 	/* Config register write done. */
3092f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30937467bd53SPyun YongHyeon 
30947467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30957467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30967467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30977467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30987467bd53SPyun YongHyeon }
3099