xref: /freebsd/sys/dev/re/if_re.c (revision 0596d7e65dd3c2289f21192bf02a8aa47f6cef4c)
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,
17559ef640dSPyun YongHyeon 	    "RealTek 8168/8168B/8168C/8168CP/8168D/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"},
21659ef640dSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D"},
217a94100faSBill Paul 	{ 0, 0, NULL }
218a94100faSBill Paul };
219a94100faSBill Paul 
220a94100faSBill Paul static int re_probe		(device_t);
221a94100faSBill Paul static int re_attach		(device_t);
222a94100faSBill Paul static int re_detach		(device_t);
223a94100faSBill Paul 
224d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
225a94100faSBill Paul 
226a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
227a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
228d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
229d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
230d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
231a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
232a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23422a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23522a11c96SJohn-Mark Gurney 				(struct mbuf *);
23622a11c96SJohn-Mark Gurney #endif
237ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
238a94100faSBill Paul static void re_txeof		(struct rl_softc *);
23997b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2400187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2410187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
24297b9d4baSJohn-Mark Gurney #endif
243ef544f63SPaolo Pisati static int re_intr		(void *);
244a94100faSBill Paul static void re_tick		(void *);
245ed510fb0SBill Paul static void re_tx_task		(void *, int);
246ed510fb0SBill Paul static void re_int_task		(void *, int);
247a94100faSBill Paul static void re_start		(struct ifnet *);
248a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
249a94100faSBill Paul static void re_init		(void *);
25097b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
251a94100faSBill Paul static void re_stop		(struct rl_softc *);
2521d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
253a94100faSBill Paul static int re_suspend		(device_t);
254a94100faSBill Paul static int re_resume		(device_t);
2556a087a87SPyun YongHyeon static int re_shutdown		(device_t);
256a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
257a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
258a94100faSBill Paul 
259a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
260a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
261ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
262a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
263a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
264a94100faSBill Paul 
265a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
266a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
267a94100faSBill Paul static void re_miibus_statchg	(device_t);
268a94100faSBill Paul 
269a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
270a94100faSBill Paul static void re_reset		(struct rl_softc *);
2717467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2727467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
273a94100faSBill Paul 
274ed510fb0SBill Paul #ifdef RE_DIAG
275a94100faSBill Paul static int re_diag		(struct rl_softc *);
276ed510fb0SBill Paul #endif
277a94100faSBill Paul 
278a94100faSBill Paul static device_method_t re_methods[] = {
279a94100faSBill Paul 	/* Device interface */
280a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
281a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
282a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
283a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
284a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
285a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
286a94100faSBill Paul 
287a94100faSBill Paul 	/* bus interface */
288a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
289a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
290a94100faSBill Paul 
291a94100faSBill Paul 	/* MII interface */
292a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
293a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
294a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
295a94100faSBill Paul 
296a94100faSBill Paul 	{ 0, 0 }
297a94100faSBill Paul };
298a94100faSBill Paul 
299a94100faSBill Paul static driver_t re_driver = {
300a94100faSBill Paul 	"re",
301a94100faSBill Paul 	re_methods,
302a94100faSBill Paul 	sizeof(struct rl_softc)
303a94100faSBill Paul };
304a94100faSBill Paul 
305a94100faSBill Paul static devclass_t re_devclass;
306a94100faSBill Paul 
307a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
308347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
309a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
310a94100faSBill Paul 
311a94100faSBill Paul #define EE_SET(x)					\
312a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
313a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
314a94100faSBill Paul 
315a94100faSBill Paul #define EE_CLR(x)					\
316a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
317a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
318a94100faSBill Paul 
319a94100faSBill Paul /*
320a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
321a94100faSBill Paul  */
322a94100faSBill Paul static void
3237b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
324a94100faSBill Paul {
3250ce0868aSPyun YongHyeon 	int			d, i;
326a94100faSBill Paul 
327ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
328a94100faSBill Paul 
329a94100faSBill Paul 	/*
330a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
331a94100faSBill Paul 	 */
332ed510fb0SBill Paul 
333ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
334a94100faSBill Paul 		if (d & i) {
335a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
336a94100faSBill Paul 		} else {
337a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
338a94100faSBill Paul 		}
339a94100faSBill Paul 		DELAY(100);
340a94100faSBill Paul 		EE_SET(RL_EE_CLK);
341a94100faSBill Paul 		DELAY(150);
342a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
343a94100faSBill Paul 		DELAY(100);
344a94100faSBill Paul 	}
345a94100faSBill Paul }
346a94100faSBill Paul 
347a94100faSBill Paul /*
348a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
349a94100faSBill Paul  */
350a94100faSBill Paul static void
3517b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
352a94100faSBill Paul {
3530ce0868aSPyun YongHyeon 	int			i;
354a94100faSBill Paul 	u_int16_t		word = 0;
355a94100faSBill Paul 
356a94100faSBill Paul 	/*
357a94100faSBill Paul 	 * Send address of word we want to read.
358a94100faSBill Paul 	 */
359a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
360a94100faSBill Paul 
361a94100faSBill Paul 	/*
362a94100faSBill Paul 	 * Start reading bits from EEPROM.
363a94100faSBill Paul 	 */
364a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
365a94100faSBill Paul 		EE_SET(RL_EE_CLK);
366a94100faSBill Paul 		DELAY(100);
367a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
368a94100faSBill Paul 			word |= i;
369a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
370a94100faSBill Paul 		DELAY(100);
371a94100faSBill Paul 	}
372a94100faSBill Paul 
373a94100faSBill Paul 	*dest = word;
374a94100faSBill Paul }
375a94100faSBill Paul 
376a94100faSBill Paul /*
377a94100faSBill Paul  * Read a sequence of words from the EEPROM.
378a94100faSBill Paul  */
379a94100faSBill Paul static void
3807b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
381a94100faSBill Paul {
382a94100faSBill Paul 	int			i;
383a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
384a94100faSBill Paul 
385ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
386ed510fb0SBill Paul 
387ed510fb0SBill Paul         DELAY(100);
388ed510fb0SBill Paul 
389a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
390ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
391a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
392ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
393a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
394be099007SPyun YongHyeon                 *ptr = word;
395a94100faSBill Paul 	}
396ed510fb0SBill Paul 
397ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
398a94100faSBill Paul }
399a94100faSBill Paul 
400a94100faSBill Paul static int
4017b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
402a94100faSBill Paul {
403a94100faSBill Paul 	struct rl_softc		*sc;
404a94100faSBill Paul 	u_int32_t		rval;
405a94100faSBill Paul 	int			i;
406a94100faSBill Paul 
407a94100faSBill Paul 	if (phy != 1)
408a94100faSBill Paul 		return (0);
409a94100faSBill Paul 
410a94100faSBill Paul 	sc = device_get_softc(dev);
411a94100faSBill Paul 
4129bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4139bac70b8SBill Paul 
4149bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4159bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4169bac70b8SBill Paul 		return (rval);
4179bac70b8SBill Paul 	}
4189bac70b8SBill Paul 
419a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
420a94100faSBill Paul 
421a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
422a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
423a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
424a94100faSBill Paul 			break;
425f68bc089SPyun YongHyeon 		DELAY(100);
426a94100faSBill Paul 	}
427a94100faSBill Paul 
428a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4296b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
430a94100faSBill Paul 		return (0);
431a94100faSBill Paul 	}
432a94100faSBill Paul 
433a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
434a94100faSBill Paul }
435a94100faSBill Paul 
436a94100faSBill Paul static int
4377b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
438a94100faSBill Paul {
439a94100faSBill Paul 	struct rl_softc		*sc;
440a94100faSBill Paul 	u_int32_t		rval;
441a94100faSBill Paul 	int			i;
442a94100faSBill Paul 
443a94100faSBill Paul 	sc = device_get_softc(dev);
444a94100faSBill Paul 
445a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4469bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
447a94100faSBill Paul 
448a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
449a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
450a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
451a94100faSBill Paul 			break;
452f68bc089SPyun YongHyeon 		DELAY(100);
453a94100faSBill Paul 	}
454a94100faSBill Paul 
455a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4566b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
457a94100faSBill Paul 		return (0);
458a94100faSBill Paul 	}
459a94100faSBill Paul 
460a94100faSBill Paul 	return (0);
461a94100faSBill Paul }
462a94100faSBill Paul 
463a94100faSBill Paul static int
4647b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
465a94100faSBill Paul {
466a94100faSBill Paul 	struct rl_softc		*sc;
467a94100faSBill Paul 	u_int16_t		rval = 0;
468a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
469a94100faSBill Paul 
470a94100faSBill Paul 	sc = device_get_softc(dev);
471a94100faSBill Paul 
472a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
473a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
474a94100faSBill Paul 		return (rval);
475a94100faSBill Paul 	}
476a94100faSBill Paul 
477a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
478a94100faSBill Paul 	if (phy) {
479a94100faSBill Paul 		return (0);
480a94100faSBill Paul 	}
481a94100faSBill Paul 	switch (reg) {
482a94100faSBill Paul 	case MII_BMCR:
483a94100faSBill Paul 		re8139_reg = RL_BMCR;
484a94100faSBill Paul 		break;
485a94100faSBill Paul 	case MII_BMSR:
486a94100faSBill Paul 		re8139_reg = RL_BMSR;
487a94100faSBill Paul 		break;
488a94100faSBill Paul 	case MII_ANAR:
489a94100faSBill Paul 		re8139_reg = RL_ANAR;
490a94100faSBill Paul 		break;
491a94100faSBill Paul 	case MII_ANER:
492a94100faSBill Paul 		re8139_reg = RL_ANER;
493a94100faSBill Paul 		break;
494a94100faSBill Paul 	case MII_ANLPAR:
495a94100faSBill Paul 		re8139_reg = RL_LPAR;
496a94100faSBill Paul 		break;
497a94100faSBill Paul 	case MII_PHYIDR1:
498a94100faSBill Paul 	case MII_PHYIDR2:
499a94100faSBill Paul 		return (0);
500a94100faSBill Paul 	/*
501a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
502a94100faSBill Paul 	 * register. If we have a link partner which does not
503a94100faSBill Paul 	 * support NWAY, this is the register which will tell
504a94100faSBill Paul 	 * us the results of parallel detection.
505a94100faSBill Paul 	 */
506a94100faSBill Paul 	case RL_MEDIASTAT:
507a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
508a94100faSBill Paul 		return (rval);
509a94100faSBill Paul 	default:
5106b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
511a94100faSBill Paul 		return (0);
512a94100faSBill Paul 	}
513a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
514baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
515baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
516baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
517baa12772SPyun YongHyeon 	}
518a94100faSBill Paul 	return (rval);
519a94100faSBill Paul }
520a94100faSBill Paul 
521a94100faSBill Paul static int
5227b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
523a94100faSBill Paul {
524a94100faSBill Paul 	struct rl_softc		*sc;
525a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
526a94100faSBill Paul 	int			rval = 0;
527a94100faSBill Paul 
528a94100faSBill Paul 	sc = device_get_softc(dev);
529a94100faSBill Paul 
530a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
531a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
532a94100faSBill Paul 		return (rval);
533a94100faSBill Paul 	}
534a94100faSBill Paul 
535a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
53697b9d4baSJohn-Mark Gurney 	if (phy)
537a94100faSBill Paul 		return (0);
53897b9d4baSJohn-Mark Gurney 
539a94100faSBill Paul 	switch (reg) {
540a94100faSBill Paul 	case MII_BMCR:
541a94100faSBill Paul 		re8139_reg = RL_BMCR;
542baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
543baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
544baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
545baa12772SPyun YongHyeon 		}
546a94100faSBill Paul 		break;
547a94100faSBill Paul 	case MII_BMSR:
548a94100faSBill Paul 		re8139_reg = RL_BMSR;
549a94100faSBill Paul 		break;
550a94100faSBill Paul 	case MII_ANAR:
551a94100faSBill Paul 		re8139_reg = RL_ANAR;
552a94100faSBill Paul 		break;
553a94100faSBill Paul 	case MII_ANER:
554a94100faSBill Paul 		re8139_reg = RL_ANER;
555a94100faSBill Paul 		break;
556a94100faSBill Paul 	case MII_ANLPAR:
557a94100faSBill Paul 		re8139_reg = RL_LPAR;
558a94100faSBill Paul 		break;
559a94100faSBill Paul 	case MII_PHYIDR1:
560a94100faSBill Paul 	case MII_PHYIDR2:
561a94100faSBill Paul 		return (0);
562a94100faSBill Paul 		break;
563a94100faSBill Paul 	default:
5646b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
565a94100faSBill Paul 		return (0);
566a94100faSBill Paul 	}
567a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
568a94100faSBill Paul 	return (0);
569a94100faSBill Paul }
570a94100faSBill Paul 
571a94100faSBill Paul static void
5727b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
573a94100faSBill Paul {
574130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
575130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
576130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
577a11e2f18SBruce M Simpson 
578130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
579130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
580130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
581130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
582130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
583130b6dfbSPyun YongHyeon 		return;
584130b6dfbSPyun YongHyeon 
585130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
586130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
587130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
588130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
589130b6dfbSPyun YongHyeon 		case IFM_10_T:
590130b6dfbSPyun YongHyeon 		case IFM_100_TX:
591130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
592130b6dfbSPyun YongHyeon 			break;
593130b6dfbSPyun YongHyeon 		case IFM_1000_T:
594130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
595130b6dfbSPyun YongHyeon 				break;
596130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
597130b6dfbSPyun YongHyeon 			break;
598130b6dfbSPyun YongHyeon 		default:
599130b6dfbSPyun YongHyeon 			break;
600130b6dfbSPyun YongHyeon 		}
601130b6dfbSPyun YongHyeon 	}
602130b6dfbSPyun YongHyeon 	/*
603130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
604130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
605130b6dfbSPyun YongHyeon 	 * parameters.
606130b6dfbSPyun YongHyeon 	 */
607a94100faSBill Paul }
608a94100faSBill Paul 
609a94100faSBill Paul /*
610a94100faSBill Paul  * Program the 64-bit multicast hash filter.
611a94100faSBill Paul  */
612a94100faSBill Paul static void
6137b5ffebfSPyun YongHyeon re_setmulti(struct rl_softc *sc)
614a94100faSBill Paul {
615a94100faSBill Paul 	struct ifnet		*ifp;
616a94100faSBill Paul 	int			h = 0;
617a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
618a94100faSBill Paul 	struct ifmultiaddr	*ifma;
619a94100faSBill Paul 	u_int32_t		rxfilt;
620a94100faSBill Paul 	int			mcnt = 0;
621a94100faSBill Paul 
62297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
62397b9d4baSJohn-Mark Gurney 
624fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
625a94100faSBill Paul 
626a94100faSBill Paul 
6277c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6287c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
629a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
6307c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6317c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
632a0637caaSPyun YongHyeon 		/*
633a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
634a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
635a0637caaSPyun YongHyeon 		 * promiscuous mode.
636a0637caaSPyun YongHyeon 		 */
637a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
638a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
639a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
640a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
641a94100faSBill Paul 		return;
642a94100faSBill Paul 	}
643a94100faSBill Paul 
644a94100faSBill Paul 	/* first, zot all the existing hash bits */
645a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
646a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
647a94100faSBill Paul 
648a94100faSBill Paul 	/* now program new ones */
64913b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
650a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
651a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
652a94100faSBill Paul 			continue;
6530e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6540e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
655a94100faSBill Paul 		if (h < 32)
656a94100faSBill Paul 			hashes[0] |= (1 << h);
657a94100faSBill Paul 		else
658a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
659a94100faSBill Paul 		mcnt++;
660a94100faSBill Paul 	}
66113b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
662a94100faSBill Paul 
663a94100faSBill Paul 	if (mcnt)
664a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
665a94100faSBill Paul 	else
666a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
667a94100faSBill Paul 
668a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
669bb7dfefbSBill Paul 
670bb7dfefbSBill Paul 	/*
671bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
672bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
673bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
674bb7dfefbSBill Paul 	 * order for those devices.
675bb7dfefbSBill Paul 	 */
676bb7dfefbSBill Paul 
677351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_INVMAR) != 0) {
678bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
679bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
680351a76f9SPyun YongHyeon 	} else {
681a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
682a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
683a94100faSBill Paul 	}
684bb7dfefbSBill Paul }
685a94100faSBill Paul 
686a94100faSBill Paul static void
6877b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
688a94100faSBill Paul {
6890ce0868aSPyun YongHyeon 	int			i;
690a94100faSBill Paul 
69197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
69297b9d4baSJohn-Mark Gurney 
693a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
694a94100faSBill Paul 
695a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
696a94100faSBill Paul 		DELAY(10);
697a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
698a94100faSBill Paul 			break;
699a94100faSBill Paul 	}
700a94100faSBill Paul 	if (i == RL_TIMEOUT)
7016b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
702a94100faSBill Paul 
7030596d7e6SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHY8169) != 0)
704a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
7050596d7e6SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHY8110S) != 0) {
7060596d7e6SPyun YongHyeon 		CSR_WRITE_1(sc, 0x82, 1);
7070596d7e6SPyun YongHyeon 		re_gmii_writereg(sc->rl_dev, 1, 0x0B, 0);
7080596d7e6SPyun YongHyeon 	}
709a94100faSBill Paul }
710a94100faSBill Paul 
711ed510fb0SBill Paul #ifdef RE_DIAG
712ed510fb0SBill Paul 
713a94100faSBill Paul /*
714a94100faSBill Paul  * The following routine is designed to test for a defect on some
715a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
716a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
717a94100faSBill Paul  * should be pulled high. The result of this defect is that the
718a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
719a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
720a94100faSBill Paul  * because the 64-bit data lines aren't connected.
721a94100faSBill Paul  *
722a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
723a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
724a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
725a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
726a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
727a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
728a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
729a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
730a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
731a94100faSBill Paul  */
732a94100faSBill Paul 
733a94100faSBill Paul static int
7347b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
735a94100faSBill Paul {
736fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
737a94100faSBill Paul 	struct mbuf		*m0;
738a94100faSBill Paul 	struct ether_header	*eh;
739a94100faSBill Paul 	struct rl_desc		*cur_rx;
740a94100faSBill Paul 	u_int16_t		status;
741a94100faSBill Paul 	u_int32_t		rxstat;
742ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
743a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
744a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
745a94100faSBill Paul 
746a94100faSBill Paul 	/* Allocate a single mbuf */
747a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
748a94100faSBill Paul 	if (m0 == NULL)
749a94100faSBill Paul 		return (ENOBUFS);
750a94100faSBill Paul 
75197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
75297b9d4baSJohn-Mark Gurney 
753a94100faSBill Paul 	/*
754a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
755a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
756a94100faSBill Paul 	 * following special functions:
757a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
758a94100faSBill Paul 	 * - Enables digital loopback mode
759a94100faSBill Paul 	 * - Leaves interrupts turned off
760a94100faSBill Paul 	 */
761a94100faSBill Paul 
762a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
763a94100faSBill Paul 	sc->rl_testmode = 1;
76497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
765351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
766ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
767ed510fb0SBill Paul 		phyaddr = 1;
768ed510fb0SBill Paul 	else
769ed510fb0SBill Paul 		phyaddr = 0;
770ed510fb0SBill Paul 
771ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
772ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
773ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
774ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
775ed510fb0SBill Paul 			break;
776ed510fb0SBill Paul 	}
777ed510fb0SBill Paul 
778ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
779ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
780ed510fb0SBill Paul 
781804af9a1SBill Paul 	DELAY(100000);
782a94100faSBill Paul 
783a94100faSBill Paul 	/* Put some data in the mbuf */
784a94100faSBill Paul 
785a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
786a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
787a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
788a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
789a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
790a94100faSBill Paul 
7917cae6651SBill Paul 	/*
7927cae6651SBill Paul 	 * Queue the packet, start transmission.
7937cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7947cae6651SBill Paul 	 */
795a94100faSBill Paul 
796abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
79797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79852732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7997cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
80097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
801a94100faSBill Paul 	m0 = NULL;
802a94100faSBill Paul 
803a94100faSBill Paul 	/* Wait for it to propagate through the chip */
804a94100faSBill Paul 
805abc8ff44SBill Paul 	DELAY(100000);
806a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
807a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
808ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
809abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
810abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
811a94100faSBill Paul 			break;
812a94100faSBill Paul 		DELAY(10);
813a94100faSBill Paul 	}
814a94100faSBill Paul 
815a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8166b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8176b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8186b9f5c94SGleb Smirnoff 		    " loopback mode\n");
819a94100faSBill Paul 		error = EIO;
820a94100faSBill Paul 		goto done;
821a94100faSBill Paul 	}
822a94100faSBill Paul 
823a94100faSBill Paul 	/*
824a94100faSBill Paul 	 * The packet should have been dumped into the first
825a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
826a94100faSBill Paul 	 */
827a94100faSBill Paul 
828a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
829a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
830a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
831d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
832d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
833d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
834d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
835d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
836a94100faSBill Paul 
837d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
838d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
839a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
840a94100faSBill Paul 
841a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
842a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
843a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
844a94100faSBill Paul 
845a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8466b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8476b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
848a94100faSBill Paul 		error = EIO;
849a94100faSBill Paul 		goto done;
850a94100faSBill Paul 	}
851a94100faSBill Paul 
852a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
853a94100faSBill Paul 
854a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
855a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
856a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8576b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8586b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
859a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8606b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
861a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
862a94100faSBill Paul 		    ntohs(eh->ether_type));
8636b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8646b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8656b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8666b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8676b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8686b9f5c94SGleb Smirnoff 		    "details.\n");
869a94100faSBill Paul 		error = EIO;
870a94100faSBill Paul 	}
871a94100faSBill Paul 
872a94100faSBill Paul done:
873a94100faSBill Paul 	/* Turn interface off, release resources */
874a94100faSBill Paul 
875a94100faSBill Paul 	sc->rl_testmode = 0;
876351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
877a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
878a94100faSBill Paul 	re_stop(sc);
879a94100faSBill Paul 	if (m0 != NULL)
880a94100faSBill Paul 		m_freem(m0);
881a94100faSBill Paul 
88297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
88397b9d4baSJohn-Mark Gurney 
884a94100faSBill Paul 	return (error);
885a94100faSBill Paul }
886a94100faSBill Paul 
887ed510fb0SBill Paul #endif
888ed510fb0SBill Paul 
889a94100faSBill Paul /*
890a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
891a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
892a94100faSBill Paul  */
893a94100faSBill Paul static int
8947b5ffebfSPyun YongHyeon re_probe(device_t dev)
895a94100faSBill Paul {
896a94100faSBill Paul 	struct rl_type		*t;
897dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
898dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
899dfdb409eSPyun YongHyeon 	int			i;
900a94100faSBill Paul 
901dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
902dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
903dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
904dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
905a94100faSBill Paul 
906dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
907dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
90826390635SJohn Baldwin 			/*
90926390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
910dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
91126390635SJohn Baldwin 			 */
912a94100faSBill Paul 			return (ENXIO);
913a94100faSBill Paul 		}
914dfdb409eSPyun YongHyeon 	}
915dfdb409eSPyun YongHyeon 
916dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
917dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
918dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
919dfdb409eSPyun YongHyeon 			return (ENXIO);
920dfdb409eSPyun YongHyeon 		}
921dfdb409eSPyun YongHyeon 	}
922dfdb409eSPyun YongHyeon 
923dfdb409eSPyun YongHyeon 	t = re_devs;
924dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
925dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
926a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
927d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
928a94100faSBill Paul 		}
929a94100faSBill Paul 	}
930a94100faSBill Paul 
931a94100faSBill Paul 	return (ENXIO);
932a94100faSBill Paul }
933a94100faSBill Paul 
934a94100faSBill Paul /*
935a94100faSBill Paul  * Map a single buffer address.
936a94100faSBill Paul  */
937a94100faSBill Paul 
938a94100faSBill Paul static void
9397b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
940a94100faSBill Paul {
9418fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
942a94100faSBill Paul 
943a94100faSBill Paul 	if (error)
944a94100faSBill Paul 		return;
945a94100faSBill Paul 
946a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
947a94100faSBill Paul 	addr = arg;
948a94100faSBill Paul 	*addr = segs->ds_addr;
949a94100faSBill Paul }
950a94100faSBill Paul 
951a94100faSBill Paul static int
9527b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
953a94100faSBill Paul {
954d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
955a94100faSBill Paul 	int			error;
956a94100faSBill Paul 	int			i;
957a94100faSBill Paul 
958d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
959d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
960d65abd66SPyun YongHyeon 
961d65abd66SPyun YongHyeon 	/*
962d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
963ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
964ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
965ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
966ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
967ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
968d65abd66SPyun YongHyeon 	 */
969d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
970ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
971d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
972d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
973d65abd66SPyun YongHyeon 	if (error) {
974d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
975d65abd66SPyun YongHyeon 		return (error);
976d65abd66SPyun YongHyeon 	}
977d65abd66SPyun YongHyeon 
978d65abd66SPyun YongHyeon 	/*
979d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
980d65abd66SPyun YongHyeon 	 */
981d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
982d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
983d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
984d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
985d65abd66SPyun YongHyeon 	if (error) {
986d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
987d65abd66SPyun YongHyeon 		return (error);
988d65abd66SPyun YongHyeon 	}
989d65abd66SPyun YongHyeon 
990a94100faSBill Paul 	/*
991a94100faSBill Paul 	 * Allocate map for RX mbufs.
992a94100faSBill Paul 	 */
993d65abd66SPyun YongHyeon 
994d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
995d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
996d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
997a94100faSBill Paul 	if (error) {
998d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
999d65abd66SPyun YongHyeon 		return (error);
1000a94100faSBill Paul 	}
1001a94100faSBill Paul 
1002a94100faSBill Paul 	/*
1003a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1004a94100faSBill Paul 	 */
1005a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1006a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1007d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1008a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1009a94100faSBill Paul 	if (error) {
1010d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1011d65abd66SPyun YongHyeon 		return (error);
1012a94100faSBill Paul 	}
1013a94100faSBill Paul 
1014a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1015a94100faSBill Paul 
1016a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1017d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1018d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1019a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1020d65abd66SPyun YongHyeon 	if (error) {
1021d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1022d65abd66SPyun YongHyeon 		return (error);
1023d65abd66SPyun YongHyeon 	}
1024a94100faSBill Paul 
1025a94100faSBill Paul 	/* Load the map for the TX ring. */
1026a94100faSBill Paul 
1027d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1028a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1029a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1030d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1031a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1032d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1033d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1034d65abd66SPyun YongHyeon 		return (ENOMEM);
1035d65abd66SPyun YongHyeon 	}
1036a94100faSBill Paul 
1037a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1038a94100faSBill Paul 
1039d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1040d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1041d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1042a94100faSBill Paul 		if (error) {
1043d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1044d65abd66SPyun YongHyeon 			return (error);
1045a94100faSBill Paul 		}
1046a94100faSBill Paul 	}
1047a94100faSBill Paul 
1048a94100faSBill Paul 	/*
1049a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1050a94100faSBill Paul 	 */
1051a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1052a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1053d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1054a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1055a94100faSBill Paul 	if (error) {
1056d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1057d65abd66SPyun YongHyeon 		return (error);
1058a94100faSBill Paul 	}
1059a94100faSBill Paul 
1060a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1061a94100faSBill Paul 
1062a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1063d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1064d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1065a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1066d65abd66SPyun YongHyeon 	if (error) {
1067d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1068d65abd66SPyun YongHyeon 		return (error);
1069d65abd66SPyun YongHyeon 	}
1070a94100faSBill Paul 
1071a94100faSBill Paul 	/* Load the map for the RX ring. */
1072a94100faSBill Paul 
1073d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1074a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1075a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1076d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1077a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1078d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1079d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1080d65abd66SPyun YongHyeon 		return (ENOMEM);
1081d65abd66SPyun YongHyeon 	}
1082a94100faSBill Paul 
1083a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1084a94100faSBill Paul 
1085d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1086d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1087a94100faSBill Paul 	if (error) {
1088d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1089d65abd66SPyun YongHyeon 		return (error);
1090d65abd66SPyun YongHyeon 	}
1091d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1092d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1093d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1094d65abd66SPyun YongHyeon 		if (error) {
1095d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1096d65abd66SPyun YongHyeon 			return (error);
1097a94100faSBill Paul 		}
1098a94100faSBill Paul 	}
1099a94100faSBill Paul 
1100a94100faSBill Paul 	return (0);
1101a94100faSBill Paul }
1102a94100faSBill Paul 
1103a94100faSBill Paul /*
1104a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1105a94100faSBill Paul  * setup and ethernet/BPF attach.
1106a94100faSBill Paul  */
1107a94100faSBill Paul static int
11087b5ffebfSPyun YongHyeon re_attach(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:
1241130b6dfbSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_FASTETHER;
1242351a76f9SPyun YongHyeon 		break;
12430596d7e6SPyun YongHyeon 	case RL_HWREV_8110S:
12440596d7e6SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHY8110S;
12450596d7e6SPyun YongHyeon 		break;
1246351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1247351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
124847fac8e5SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
1249130b6dfbSPyun YongHyeon 		    RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1250351a76f9SPyun YongHyeon 		break;
1251b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1252b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
1253b1d62f0fSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_INVMAR |
125497cf11ecSPyun YongHyeon 		    RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
1255ead8fc66SPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP;
1256b1d62f0fSPyun YongHyeon 		break;
1257351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
1258351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
1259351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
1260deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1261deb5c680SPyun YongHyeon 		    RL_FLAG_MACSTAT;
1262deb5c680SPyun YongHyeon 		break;
1263deb5c680SPyun YongHyeon 	case RL_HWREV_8168C:
1264deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
1265deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
126659ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
1267deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_INVMAR | RL_FLAG_PHYWAKE |
1268ead8fc66SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1269ead8fc66SPyun YongHyeon 		    RL_FLAG_CMDSTOP;
1270deb5c680SPyun YongHyeon 		/*
1271deb5c680SPyun YongHyeon 		 * These controllers support jumbo frame but it seems
1272deb5c680SPyun YongHyeon 		 * that enabling it requires touching additional magic
1273deb5c680SPyun YongHyeon 		 * registers. Depending on MAC revisions some
1274deb5c680SPyun YongHyeon 		 * controllers need to disable checksum offload. So
1275deb5c680SPyun YongHyeon 		 * disable jumbo frame until I have better idea what
1276deb5c680SPyun YongHyeon 		 * it really requires to make it support.
1277deb5c680SPyun YongHyeon 		 * RTL8168C/CP : supports up to 6KB jumbo frame.
1278deb5c680SPyun YongHyeon 		 * RTL8111C/CP : supports up to 9KB jumbo frame.
1279deb5c680SPyun YongHyeon 		 */
1280deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1281351a76f9SPyun YongHyeon 		break;
12820596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
12830596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
12840596d7e6SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHY8169;
12850596d7e6SPyun YongHyeon 		break;
1286351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SB:
1287351a76f9SPyun YongHyeon 	case RL_HWREV_8169_8110SC:
1288715922d7SPyun YongHyeon 	case RL_HWREV_8169_8110SBL:
12890596d7e6SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHY8169;
1290351a76f9SPyun YongHyeon 		break;
1291351a76f9SPyun YongHyeon 	default:
1292351a76f9SPyun YongHyeon 		break;
1293351a76f9SPyun YongHyeon 	}
1294351a76f9SPyun YongHyeon 
1295deb5c680SPyun YongHyeon 	/* Enable PME. */
1296deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1297deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1298deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1299deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1300deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1301deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1302deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1303deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1304deb5c680SPyun YongHyeon 
1305deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1306deb5c680SPyun YongHyeon 		/*
1307deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1308deb5c680SPyun YongHyeon 		 * address from EEPROM.
1309deb5c680SPyun YongHyeon 		 */
1310deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1311deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1312deb5c680SPyun YongHyeon 	} else {
1313141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1314ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1315a94100faSBill Paul 		if (re_did != 0x8129)
1316141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1317a94100faSBill Paul 
1318a94100faSBill Paul 		/*
1319a94100faSBill Paul 		 * Get station address from the EEPROM.
1320a94100faSBill Paul 		 */
1321ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1322be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1323be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1324be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1325deb5c680SPyun YongHyeon 	}
1326ed510fb0SBill Paul 
1327ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1328d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1329ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1330ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1331d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1332d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1333ed510fb0SBill Paul 	} else {
1334d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1335ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1336ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1337d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1338d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1339abc8ff44SBill Paul 	}
13409bac70b8SBill Paul 
1341a94100faSBill Paul 	error = re_allocmem(dev, sc);
1342a94100faSBill Paul 	if (error)
1343a94100faSBill Paul 		goto fail;
1344a94100faSBill Paul 
1345cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1346cd036ec1SBrooks Davis 	if (ifp == NULL) {
1347d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1348cd036ec1SBrooks Davis 		error = ENOSPC;
1349cd036ec1SBrooks Davis 		goto fail;
1350cd036ec1SBrooks Davis 	}
1351cd036ec1SBrooks Davis 
1352351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1353351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1354351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1355351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1356351a76f9SPyun YongHyeon 	}
1357351a76f9SPyun YongHyeon 
1358a94100faSBill Paul 	/* Do MII setup */
1359a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1360a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1361d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1362a94100faSBill Paul 		error = ENXIO;
1363a94100faSBill Paul 		goto fail;
1364a94100faSBill Paul 	}
1365a94100faSBill Paul 
1366a94100faSBill Paul 	ifp->if_softc = sc;
13679bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1368a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1369a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1370a94100faSBill Paul 	ifp->if_start = re_start;
1371deb5c680SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1372deb5c680SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1373498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1374a94100faSBill Paul 	ifp->if_init = re_init;
137552732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
137652732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
137752732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1378a94100faSBill Paul 
1379ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1380ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1381ed510fb0SBill Paul 
1382a94100faSBill Paul 	/*
1383deb5c680SPyun YongHyeon 	 * XXX
1384deb5c680SPyun YongHyeon 	 * Still have no idea how to make TSO work on 8168C, 8168CP,
1385deb5c680SPyun YongHyeon 	 * 8111C and 8111CP.
1386deb5c680SPyun YongHyeon 	 */
1387deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1388deb5c680SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
1389deb5c680SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4;
1390deb5c680SPyun YongHyeon 	}
1391deb5c680SPyun YongHyeon 
1392deb5c680SPyun YongHyeon 	/*
1393a94100faSBill Paul 	 * Call MI attach routine.
1394a94100faSBill Paul 	 */
1395a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1396a94100faSBill Paul 
1397960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1398960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1399960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1400960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
14017467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
14027467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
14037467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1404960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1405a2a8420cSPyun YongHyeon 	/*
1406a2a8420cSPyun YongHyeon 	 * Don't enable TSO by default. Under certain
1407a2a8420cSPyun YongHyeon 	 * circumtances the controller generated corrupted
1408a2a8420cSPyun YongHyeon 	 * packets in TSO size.
1409a2a8420cSPyun YongHyeon 	 */
1410a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1411a2a8420cSPyun YongHyeon 	ifp->if_capenable &= ~IFCAP_TSO4;
1412960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1413960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1414960fd5b3SPyun YongHyeon #endif
1415960fd5b3SPyun YongHyeon 	/*
1416960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1417960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1418960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1419960fd5b3SPyun YongHyeon 	 */
1420960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1421960fd5b3SPyun YongHyeon 
1422ed510fb0SBill Paul #ifdef RE_DIAG
1423ed510fb0SBill Paul 	/*
1424ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1425ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1426ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1427ed510fb0SBill Paul 	 */
1428a94100faSBill Paul 
1429ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1430ed510fb0SBill Paul 		error = re_diag(sc);
1431a94100faSBill Paul 		if (error) {
1432ed510fb0SBill Paul 			device_printf(dev,
1433ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1434a94100faSBill Paul 			ether_ifdetach(ifp);
1435a94100faSBill Paul 			goto fail;
1436a94100faSBill Paul 		}
1437ed510fb0SBill Paul 	}
1438ed510fb0SBill Paul #endif
1439a94100faSBill Paul 
1440a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1441351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
14425774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14435774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14445774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14455774c5ffSPyun YongHyeon 	else {
14465774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14475774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14485774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14495774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14505774c5ffSPyun YongHyeon 			if (error != 0)
14515774c5ffSPyun YongHyeon 				break;
14525774c5ffSPyun YongHyeon 		}
14535774c5ffSPyun YongHyeon 	}
1454a94100faSBill Paul 	if (error) {
1455d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1456a94100faSBill Paul 		ether_ifdetach(ifp);
1457a94100faSBill Paul 	}
1458a94100faSBill Paul 
1459a94100faSBill Paul fail:
1460ed510fb0SBill Paul 
1461a94100faSBill Paul 	if (error)
1462a94100faSBill Paul 		re_detach(dev);
1463a94100faSBill Paul 
1464a94100faSBill Paul 	return (error);
1465a94100faSBill Paul }
1466a94100faSBill Paul 
1467a94100faSBill Paul /*
1468a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1469a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1470a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1471a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1472a94100faSBill Paul  * allocated.
1473a94100faSBill Paul  */
1474a94100faSBill Paul static int
14757b5ffebfSPyun YongHyeon re_detach(device_t dev)
1476a94100faSBill Paul {
1477a94100faSBill Paul 	struct rl_softc		*sc;
1478a94100faSBill Paul 	struct ifnet		*ifp;
14795774c5ffSPyun YongHyeon 	int			i, rid;
1480a94100faSBill Paul 
1481a94100faSBill Paul 	sc = device_get_softc(dev);
1482fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1483aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
148497b9d4baSJohn-Mark Gurney 
148581cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
148681cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
148740929967SGleb Smirnoff #ifdef DEVICE_POLLING
148840929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
148940929967SGleb Smirnoff 			ether_poll_deregister(ifp);
149040929967SGleb Smirnoff #endif
149197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
149297b9d4baSJohn-Mark Gurney #if 0
149397b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
149497b9d4baSJohn-Mark Gurney #endif
1495a94100faSBill Paul 		re_stop(sc);
1496525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1497d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14983d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14993d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1500a94100faSBill Paul 		/*
1501a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1502a94100faSBill Paul 		 * still had a BPF descriptor attached to this
150397b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1504a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1505a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1506a94100faSBill Paul 		 * which will try to call re_init() again. This will
1507a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1508a94100faSBill Paul 		 * which will panic the system when the kernel tries
1509a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1510a94100faSBill Paul 		 * anymore.
1511a94100faSBill Paul 		 */
1512a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1513525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1514a94100faSBill Paul 	}
1515a94100faSBill Paul 	if (sc->rl_miibus)
1516a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1517a94100faSBill Paul 	bus_generic_detach(dev);
1518a94100faSBill Paul 
151997b9d4baSJohn-Mark Gurney 	/*
152097b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
152197b9d4baSJohn-Mark Gurney 	 * stopped here.
152297b9d4baSJohn-Mark Gurney 	 */
152397b9d4baSJohn-Mark Gurney 
15245774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
15255774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
15265774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
15275774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
15285774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
15295774c5ffSPyun YongHyeon 		}
15305774c5ffSPyun YongHyeon 	}
1531ad4f426eSWarner Losh 	if (ifp != NULL)
1532ad4f426eSWarner Losh 		if_free(ifp);
1533351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
15345774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
15355774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
15365774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
15375774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
15385774c5ffSPyun YongHyeon 		}
15395774c5ffSPyun YongHyeon 	} else {
15405774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15415774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15425774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15435774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15445774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15455774c5ffSPyun YongHyeon 			}
15465774c5ffSPyun YongHyeon 		}
15475774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15485774c5ffSPyun YongHyeon 	}
1549a94100faSBill Paul 	if (sc->rl_res)
1550ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1551ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1552a94100faSBill Paul 
1553a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1554a94100faSBill Paul 
1555a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1556a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1557a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1558a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1559a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1560a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1561a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1562a94100faSBill Paul 	}
1563a94100faSBill Paul 
1564a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1565a94100faSBill Paul 
1566a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1567a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1568a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1569a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1570a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1571a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1572a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1573a94100faSBill Paul 	}
1574a94100faSBill Paul 
1575a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1576a94100faSBill Paul 
1577d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1578d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1579d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1580d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1581d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1582d65abd66SPyun YongHyeon 	}
1583d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1584d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1585d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1586d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1587d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1588d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1589d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1590d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1591a94100faSBill Paul 	}
1592a94100faSBill Paul 
1593a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1594a94100faSBill Paul 
1595a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1596a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1597a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1598a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1599a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1600a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1601a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1602a94100faSBill Paul 	}
1603a94100faSBill Paul 
1604a94100faSBill Paul 	if (sc->rl_parent_tag)
1605a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1606a94100faSBill Paul 
1607a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1608a94100faSBill Paul 
1609a94100faSBill Paul 	return (0);
1610a94100faSBill Paul }
1611a94100faSBill Paul 
1612d65abd66SPyun YongHyeon static __inline void
16137b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1614a94100faSBill Paul {
1615d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1616d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1617d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1618a94100faSBill Paul 
1619d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1620d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1621d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1622d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1623d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1624d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1625d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1626d65abd66SPyun YongHyeon }
1627d65abd66SPyun YongHyeon 
1628d65abd66SPyun YongHyeon static int
16297b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1630d65abd66SPyun YongHyeon {
1631d65abd66SPyun YongHyeon 	struct mbuf		*m;
1632d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1633d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1634d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1635d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1636d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1637d65abd66SPyun YongHyeon 	int			error, nsegs;
1638d65abd66SPyun YongHyeon 
1639d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1640d65abd66SPyun YongHyeon 	if (m == NULL)
1641a94100faSBill Paul 		return (ENOBUFS);
1642a94100faSBill Paul 
1643a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
164422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
164522a11c96SJohn-Mark Gurney 	/*
164622a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
164722a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
164822a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
164922a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
165022a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
165122a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
165222a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
165322a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
165422a11c96SJohn-Mark Gurney 	 */
165522a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
165622a11c96SJohn-Mark Gurney #endif
1657d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1658d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1659d65abd66SPyun YongHyeon 	if (error != 0) {
1660d65abd66SPyun YongHyeon 		m_freem(m);
1661d65abd66SPyun YongHyeon 		return (ENOBUFS);
1662d65abd66SPyun YongHyeon 	}
1663d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1664a94100faSBill Paul 
1665d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1666d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1667d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1668d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1669d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1670a94100faSBill Paul 	}
1671a94100faSBill Paul 
1672d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1673d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1674d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1675d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1676d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1677d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1678a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1679a94100faSBill Paul 
1680d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1681d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1682d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1683d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1684d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1685d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1686d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1687d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1688d65abd66SPyun YongHyeon 
1689a94100faSBill Paul 	return (0);
1690a94100faSBill Paul }
1691a94100faSBill Paul 
169222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
169322a11c96SJohn-Mark Gurney static __inline void
16947b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
169522a11c96SJohn-Mark Gurney {
169622a11c96SJohn-Mark Gurney 	int                     i;
169722a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
169822a11c96SJohn-Mark Gurney 
169922a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
170022a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
170122a11c96SJohn-Mark Gurney 
170222a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
170322a11c96SJohn-Mark Gurney 		*dst++ = *src++;
170422a11c96SJohn-Mark Gurney 
170522a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
170622a11c96SJohn-Mark Gurney }
170722a11c96SJohn-Mark Gurney #endif
170822a11c96SJohn-Mark Gurney 
1709a94100faSBill Paul static int
17107b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1711a94100faSBill Paul {
1712d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1713d65abd66SPyun YongHyeon 	int			i;
171497b9d4baSJohn-Mark Gurney 
171597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
171697b9d4baSJohn-Mark Gurney 
1717d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1718d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1719d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1720d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1721d65abd66SPyun YongHyeon 	/* Set EOR. */
1722d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1723d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1724a94100faSBill Paul 
1725a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1726d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1727d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1728d65abd66SPyun YongHyeon 
1729a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1730a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1731d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1732a94100faSBill Paul 
1733a94100faSBill Paul 	return (0);
1734a94100faSBill Paul }
1735a94100faSBill Paul 
1736a94100faSBill Paul static int
17377b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1738a94100faSBill Paul {
1739d65abd66SPyun YongHyeon 	int			error, i;
1740a94100faSBill Paul 
1741d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1742d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1743d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1744d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1745d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1746d65abd66SPyun YongHyeon 			return (error);
1747a94100faSBill Paul 	}
1748a94100faSBill Paul 
1749a94100faSBill Paul 	/* Flush the RX descriptors */
1750a94100faSBill Paul 
1751a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1752a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1753a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1754a94100faSBill Paul 
1755a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1756a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1757a94100faSBill Paul 
1758a94100faSBill Paul 	return (0);
1759a94100faSBill Paul }
1760a94100faSBill Paul 
1761a94100faSBill Paul /*
1762a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1763a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1764a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1765a94100faSBill Paul  */
1766ed510fb0SBill Paul static int
17677b5ffebfSPyun YongHyeon re_rxeof(struct rl_softc *sc)
1768a94100faSBill Paul {
1769a94100faSBill Paul 	struct mbuf		*m;
1770a94100faSBill Paul 	struct ifnet		*ifp;
1771a94100faSBill Paul 	int			i, total_len;
1772a94100faSBill Paul 	struct rl_desc		*cur_rx;
1773a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1774ed510fb0SBill Paul 	int			maxpkt = 16;
1775a94100faSBill Paul 
17765120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17775120abbfSSam Leffler 
1778fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1779a94100faSBill Paul 
1780a94100faSBill Paul 	/* Invalidate the descriptor memory */
1781a94100faSBill Paul 
1782a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1783a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1784d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1785a94100faSBill Paul 
1786d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1787d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1788a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1789a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1790d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1791d65abd66SPyun YongHyeon 			break;
1792d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1793a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1794d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1795a94100faSBill Paul 
1796a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1797d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1798d65abd66SPyun YongHyeon 				/*
1799d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1800d65abd66SPyun YongHyeon 				 * discard all the pieces.
1801d65abd66SPyun YongHyeon 				 */
1802d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1803d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1804d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1805d65abd66SPyun YongHyeon 				}
1806d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1807d65abd66SPyun YongHyeon 				continue;
1808d65abd66SPyun YongHyeon 			}
180922a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1810a94100faSBill Paul 			if (sc->rl_head == NULL)
1811a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1812a94100faSBill Paul 			else {
1813a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1814a94100faSBill Paul 				sc->rl_tail->m_next = m;
1815a94100faSBill Paul 				sc->rl_tail = m;
1816a94100faSBill Paul 			}
1817a94100faSBill Paul 			continue;
1818a94100faSBill Paul 		}
1819a94100faSBill Paul 
1820a94100faSBill Paul 		/*
1821a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1822a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1823a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1824a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1825a94100faSBill Paul 		 * were already used, so to make room for the extra
1826a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1827a94100faSBill Paul 		 * error' bit and shifted the other status bits
1828a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1829a94100faSBill Paul 		 * still in the same places. We have already extracted
1830a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1831a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1832a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1833a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1834a94100faSBill Paul 		 * same format as that of the 8139C+.
1835a94100faSBill Paul 		 */
1836a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1837a94100faSBill Paul 			rxstat >>= 1;
1838a94100faSBill Paul 
183922a11c96SJohn-Mark Gurney 		/*
184022a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
184122a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
184222a11c96SJohn-Mark Gurney 		 */
184322a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
184422a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1845a94100faSBill Paul 			ifp->if_ierrors++;
1846a94100faSBill Paul 			/*
1847a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1848a94100faSBill Paul 			 * discard all the pieces.
1849a94100faSBill Paul 			 */
1850a94100faSBill Paul 			if (sc->rl_head != NULL) {
1851a94100faSBill Paul 				m_freem(sc->rl_head);
1852a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1853a94100faSBill Paul 			}
1854d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1855a94100faSBill Paul 			continue;
1856a94100faSBill Paul 		}
1857a94100faSBill Paul 
1858a94100faSBill Paul 		/*
1859a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1860a94100faSBill Paul 		 * reload the current one.
1861a94100faSBill Paul 		 */
1862a94100faSBill Paul 
1863d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1864d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1865a94100faSBill Paul 			if (sc->rl_head != NULL) {
1866a94100faSBill Paul 				m_freem(sc->rl_head);
1867a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1868a94100faSBill Paul 			}
1869d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1870a94100faSBill Paul 			continue;
1871a94100faSBill Paul 		}
1872a94100faSBill Paul 
1873a94100faSBill Paul 		if (sc->rl_head != NULL) {
187422a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
187522a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
187622a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1877a94100faSBill Paul 			/*
1878a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1879a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1880a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1881a94100faSBill Paul 			 * care about anyway.
1882a94100faSBill Paul 			 */
1883a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1884a94100faSBill Paul 				sc->rl_tail->m_len -=
1885a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1886a94100faSBill Paul 				m_freem(m);
1887a94100faSBill Paul 			} else {
1888a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1889a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1890a94100faSBill Paul 				sc->rl_tail->m_next = m;
1891a94100faSBill Paul 			}
1892a94100faSBill Paul 			m = sc->rl_head;
1893a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1894a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1895a94100faSBill Paul 		} else
1896a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1897a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1898a94100faSBill Paul 
189922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
190022a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
190122a11c96SJohn-Mark Gurney #endif
1902a94100faSBill Paul 		ifp->if_ipackets++;
1903a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1904a94100faSBill Paul 
1905a94100faSBill Paul 		/* Do RX checksumming if enabled */
1906a94100faSBill Paul 
1907a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1908deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1909a94100faSBill Paul 				/* Check IP header checksum */
1910a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
1911deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1912deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1913a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1914deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1915deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1916a94100faSBill Paul 
1917a94100faSBill Paul 				/* Check TCP/UDP checksum */
1918a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
1919a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1920a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
1921a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1922a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
1923a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1924a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1925a94100faSBill Paul 				}
1926deb5c680SPyun YongHyeon 			} else {
1927deb5c680SPyun YongHyeon 				/*
1928deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
1929deb5c680SPyun YongHyeon 				 */
1930deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
1931deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1932deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1933deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1934deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
1935deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1936deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1937deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1938deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
1939deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1940deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
1941deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1942deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1943deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1944deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
1945deb5c680SPyun YongHyeon 				}
1946deb5c680SPyun YongHyeon 			}
1947a94100faSBill Paul 		}
1948ed510fb0SBill Paul 		maxpkt--;
1949d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
195078ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1951bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
195278ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1953d147662cSGleb Smirnoff 		}
19545120abbfSSam Leffler 		RL_UNLOCK(sc);
1955a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
19565120abbfSSam Leffler 		RL_LOCK(sc);
1957a94100faSBill Paul 	}
1958a94100faSBill Paul 
1959a94100faSBill Paul 	/* Flush the RX DMA ring */
1960a94100faSBill Paul 
1961a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1962a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1963a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1964a94100faSBill Paul 
1965a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1966ed510fb0SBill Paul 
1967ed510fb0SBill Paul 	if (maxpkt)
1968ed510fb0SBill Paul 		return(EAGAIN);
1969ed510fb0SBill Paul 
1970ed510fb0SBill Paul 	return(0);
1971a94100faSBill Paul }
1972a94100faSBill Paul 
1973a94100faSBill Paul static void
19747b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
1975a94100faSBill Paul {
1976a94100faSBill Paul 	struct ifnet		*ifp;
1977d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
1978a94100faSBill Paul 	u_int32_t		txstat;
1979d65abd66SPyun YongHyeon 	int			cons;
1980d65abd66SPyun YongHyeon 
1981d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
1982d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
1983d65abd66SPyun YongHyeon 		return;
1984a94100faSBill Paul 
1985fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1986a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1987a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1988a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1989d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1990a94100faSBill Paul 
1991d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
1992d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
1993d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
1994d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
1995a94100faSBill Paul 			break;
1996a94100faSBill Paul 		/*
1997a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1998a94100faSBill Paul 		 * in a fragment chain, which also happens to
1999a94100faSBill Paul 		 * be the only place where the TX status bits
2000a94100faSBill Paul 		 * are valid.
2001a94100faSBill Paul 		 */
2002a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2003d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2004d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2005d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2006d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2007d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2008d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2009d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2010d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2011d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2012a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2013a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2014a94100faSBill Paul 				ifp->if_collisions++;
2015a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2016a94100faSBill Paul 				ifp->if_oerrors++;
2017a94100faSBill Paul 			else
2018a94100faSBill Paul 				ifp->if_opackets++;
2019a94100faSBill Paul 		}
2020a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2021d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2022a94100faSBill Paul 	}
2023d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2024a94100faSBill Paul 
2025a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2026a94100faSBill Paul 
2027d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
20280fc4974fSBill Paul 		/*
2029b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
2030b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
2031b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
2032b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
2033b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
2034b4b95879SMarius Strobl 		 * be required with the PCIe devices.
20350fc4974fSBill Paul 		 */
20360fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
20370fc4974fSBill Paul 
2038ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2039a94100faSBill Paul 		/*
2040b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2041b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2042a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2043a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2044a94100faSBill Paul 		 */
2045a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2046ed510fb0SBill Paul #endif
2047b4b95879SMarius Strobl 	} else
2048b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2049a94100faSBill Paul }
2050a94100faSBill Paul 
2051a94100faSBill Paul static void
20527b5ffebfSPyun YongHyeon re_tick(void *xsc)
2053a94100faSBill Paul {
2054a94100faSBill Paul 	struct rl_softc		*sc;
2055d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2056a94100faSBill Paul 
2057a94100faSBill Paul 	sc = xsc;
205897b9d4baSJohn-Mark Gurney 
205997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
206097b9d4baSJohn-Mark Gurney 
20611d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2062a94100faSBill Paul 	mii_tick(mii);
2063130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2064d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2065a94100faSBill Paul }
2066a94100faSBill Paul 
2067a94100faSBill Paul #ifdef DEVICE_POLLING
2068a94100faSBill Paul static void
2069a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2070a94100faSBill Paul {
2071a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
2072a94100faSBill Paul 
2073a94100faSBill Paul 	RL_LOCK(sc);
207440929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
207597b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
207697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
207797b9d4baSJohn-Mark Gurney }
207897b9d4baSJohn-Mark Gurney 
207997b9d4baSJohn-Mark Gurney static void
208097b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
208197b9d4baSJohn-Mark Gurney {
208297b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
208397b9d4baSJohn-Mark Gurney 
208497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
208597b9d4baSJohn-Mark Gurney 
2086a94100faSBill Paul 	sc->rxcycles = count;
2087a94100faSBill Paul 	re_rxeof(sc);
2088a94100faSBill Paul 	re_txeof(sc);
2089a94100faSBill Paul 
209037652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2091ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2092a94100faSBill Paul 
2093a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2094a94100faSBill Paul 		u_int16_t       status;
2095a94100faSBill Paul 
2096a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2097a94100faSBill Paul 		if (status == 0xffff)
209897b9d4baSJohn-Mark Gurney 			return;
2099a94100faSBill Paul 		if (status)
2100a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2101a94100faSBill Paul 
2102a94100faSBill Paul 		/*
2103a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2104a94100faSBill Paul 		 */
2105a94100faSBill Paul 
2106b659f1f0SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR)
210797b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2108a94100faSBill Paul 	}
2109a94100faSBill Paul }
2110a94100faSBill Paul #endif /* DEVICE_POLLING */
2111a94100faSBill Paul 
2112ef544f63SPaolo Pisati static int
21137b5ffebfSPyun YongHyeon re_intr(void *arg)
2114a94100faSBill Paul {
2115a94100faSBill Paul 	struct rl_softc		*sc;
2116ed510fb0SBill Paul 	uint16_t		status;
2117a94100faSBill Paul 
2118a94100faSBill Paul 	sc = arg;
2119ed510fb0SBill Paul 
2120ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2121498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2122ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2123ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2124ed510fb0SBill Paul 
2125ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2126ed510fb0SBill Paul 
2127ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2128ed510fb0SBill Paul }
2129ed510fb0SBill Paul 
2130ed510fb0SBill Paul static void
21317b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2132ed510fb0SBill Paul {
2133ed510fb0SBill Paul 	struct rl_softc		*sc;
2134ed510fb0SBill Paul 	struct ifnet		*ifp;
2135ed510fb0SBill Paul 	u_int16_t		status;
2136ed510fb0SBill Paul 	int			rval = 0;
2137ed510fb0SBill Paul 
2138ed510fb0SBill Paul 	sc = arg;
2139ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2140a94100faSBill Paul 
2141a94100faSBill Paul 	RL_LOCK(sc);
214297b9d4baSJohn-Mark Gurney 
2143a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2144a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2145a94100faSBill Paul 
2146d65abd66SPyun YongHyeon 	if (sc->suspended ||
2147d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2148ed510fb0SBill Paul 		RL_UNLOCK(sc);
2149ed510fb0SBill Paul 		return;
2150ed510fb0SBill Paul 	}
2151a94100faSBill Paul 
2152ed510fb0SBill Paul #ifdef DEVICE_POLLING
2153ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2154ed510fb0SBill Paul 		RL_UNLOCK(sc);
2155ed510fb0SBill Paul 		return;
2156ed510fb0SBill Paul 	}
2157ed510fb0SBill Paul #endif
2158a94100faSBill Paul 
2159ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2160ed510fb0SBill Paul 		rval = re_rxeof(sc);
2161ed510fb0SBill Paul 
21623d85c23dSPyun YongHyeon 	if (status & (
2163ed510fb0SBill Paul #ifdef RE_TX_MODERATION
21643d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2165ed510fb0SBill Paul #else
21663d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2167ed510fb0SBill Paul #endif
2168ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2169a94100faSBill Paul 		re_txeof(sc);
2170a94100faSBill Paul 
2171b659f1f0SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR)
217297b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2173a94100faSBill Paul 
217452732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2175ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2176a94100faSBill Paul 
2177a94100faSBill Paul 	RL_UNLOCK(sc);
2178ed510fb0SBill Paul 
2179ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2180ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2181ed510fb0SBill Paul 		return;
2182ed510fb0SBill Paul 	}
2183ed510fb0SBill Paul 
2184ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2185a94100faSBill Paul }
2186a94100faSBill Paul 
2187d65abd66SPyun YongHyeon static int
21887b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2189d65abd66SPyun YongHyeon {
2190d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2191d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2192d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2193d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2194d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2195d65abd66SPyun YongHyeon 	int			nsegs, prod;
2196d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2197d65abd66SPyun YongHyeon 	int			padlen;
2198ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2199a94100faSBill Paul 
2200d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2201738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
22020fc4974fSBill Paul 
22030fc4974fSBill Paul 	/*
22040fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
22050fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
22060fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
22070fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
22080fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
22090fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
221099c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
22110fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2212d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
22130fc4974fSBill Paul 	 */
2214deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0 &&
2215deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
221699c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2217d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2218d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2219d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2220d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2221d65abd66SPyun YongHyeon 			m_freem(*m_head);
2222d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2223d65abd66SPyun YongHyeon 				*m_head = NULL;
2224a94100faSBill Paul 				return (ENOBUFS);
2225a94100faSBill Paul 			}
2226d65abd66SPyun YongHyeon 			*m_head = m_new;
2227d65abd66SPyun YongHyeon 		}
2228d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2229d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
223080a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2231b4b95879SMarius Strobl 			if (m_new == NULL) {
2232b4b95879SMarius Strobl 				m_freem(*m_head);
2233b4b95879SMarius Strobl 				*m_head = NULL;
223480a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2235b4b95879SMarius Strobl 			}
2236d65abd66SPyun YongHyeon 		} else
2237d65abd66SPyun YongHyeon 			m_new = *m_head;
2238a94100faSBill Paul 
22390fc4974fSBill Paul 		/*
22400fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
22410fc4974fSBill Paul 		 * to avoid leaking data.
22420fc4974fSBill Paul 		 */
2243d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2244d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
22450fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2246d65abd66SPyun YongHyeon 		*m_head = m_new;
22470fc4974fSBill Paul 	}
22480fc4974fSBill Paul 
2249d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2250d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2251d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2252d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2253d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2254304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2255d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2256d65abd66SPyun YongHyeon 			m_freem(*m_head);
2257b4b95879SMarius Strobl 			*m_head = NULL;
2258d65abd66SPyun YongHyeon 			return (ENOBUFS);
2259a94100faSBill Paul 		}
2260d65abd66SPyun YongHyeon 		*m_head = m_new;
2261d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2262d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2263d65abd66SPyun YongHyeon 		if (error != 0) {
2264d65abd66SPyun YongHyeon 			m_freem(*m_head);
2265d65abd66SPyun YongHyeon 			*m_head = NULL;
2266d65abd66SPyun YongHyeon 			return (error);
2267a94100faSBill Paul 		}
2268d65abd66SPyun YongHyeon 	} else if (error != 0)
2269d65abd66SPyun YongHyeon 		return (error);
2270d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2271d65abd66SPyun YongHyeon 		m_freem(*m_head);
2272d65abd66SPyun YongHyeon 		*m_head = NULL;
2273d65abd66SPyun YongHyeon 		return (EIO);
2274d65abd66SPyun YongHyeon 	}
2275d65abd66SPyun YongHyeon 
2276d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2277d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2278d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2279d65abd66SPyun YongHyeon 		return (ENOBUFS);
2280d65abd66SPyun YongHyeon 	}
2281d65abd66SPyun YongHyeon 
2282d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2283d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2284a94100faSBill Paul 
2285a94100faSBill Paul 	/*
2286d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2287d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2288d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2289d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2290a94100faSBill Paul 	 */
2291deb5c680SPyun YongHyeon 	vlanctl = 0;
2292d65abd66SPyun YongHyeon 	csum_flags = 0;
2293d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2294d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2295d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2296d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2297d65abd66SPyun YongHyeon 	else {
229899c8ae87SPyun YongHyeon 		/*
229999c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
230099c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
230199c8ae87SPyun YongHyeon 		 * does't make effects.
230299c8ae87SPyun YongHyeon 		 */
230399c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2304deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2305d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2306deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2307deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2308d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2309deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2310deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2311d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2312deb5c680SPyun YongHyeon 			} else {
2313deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2314deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2315deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2316deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2317deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2318deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2319deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2320deb5c680SPyun YongHyeon 			}
2321d65abd66SPyun YongHyeon 		}
232299c8ae87SPyun YongHyeon 	}
2323a94100faSBill Paul 
2324ccf34c81SPyun YongHyeon 	/*
2325ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2326ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2327ccf34c81SPyun YongHyeon 	 * transmission attempt.
2328ccf34c81SPyun YongHyeon 	 */
2329ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2330bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2331deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2332ccf34c81SPyun YongHyeon 
2333d65abd66SPyun YongHyeon 	si = prod;
2334d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2335d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2336deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2337d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2338d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2339d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2340d65abd66SPyun YongHyeon 		if (i != 0)
2341d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2342d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2343d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2344d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2345d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2346d65abd66SPyun YongHyeon 	}
2347d65abd66SPyun YongHyeon 	/* Update producer index. */
2348d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2349a94100faSBill Paul 
2350d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2351d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2352d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2353d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2354d65abd66SPyun YongHyeon 
2355d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2356d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2357d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2358a94100faSBill Paul 
2359d65abd66SPyun YongHyeon 	/*
2360d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2361d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2362d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2363d65abd66SPyun YongHyeon 	 */
2364d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2365d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2366d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2367d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2368d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2369a94100faSBill Paul 
2370a94100faSBill Paul 	return (0);
2371a94100faSBill Paul }
2372a94100faSBill Paul 
237397b9d4baSJohn-Mark Gurney static void
23747b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
237597b9d4baSJohn-Mark Gurney {
2376ed510fb0SBill Paul 	struct ifnet		*ifp;
237797b9d4baSJohn-Mark Gurney 
2378ed510fb0SBill Paul 	ifp = arg;
2379ed510fb0SBill Paul 	re_start(ifp);
238097b9d4baSJohn-Mark Gurney }
238197b9d4baSJohn-Mark Gurney 
2382a94100faSBill Paul /*
2383a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2384a94100faSBill Paul  */
2385a94100faSBill Paul static void
23867b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2387a94100faSBill Paul {
2388a94100faSBill Paul 	struct rl_softc		*sc;
2389d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2390d65abd66SPyun YongHyeon 	int			queued;
2391a94100faSBill Paul 
2392a94100faSBill Paul 	sc = ifp->if_softc;
239397b9d4baSJohn-Mark Gurney 
2394ed510fb0SBill Paul 	RL_LOCK(sc);
2395ed510fb0SBill Paul 
2396d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2397351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2398ed510fb0SBill Paul 		RL_UNLOCK(sc);
2399ed510fb0SBill Paul 		return;
2400ed510fb0SBill Paul 	}
2401a94100faSBill Paul 
2402d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2403d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
240452732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2405a94100faSBill Paul 		if (m_head == NULL)
2406a94100faSBill Paul 			break;
2407a94100faSBill Paul 
2408d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2409b4b95879SMarius Strobl 			if (m_head == NULL)
2410b4b95879SMarius Strobl 				break;
241152732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
241213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2413a94100faSBill Paul 			break;
2414a94100faSBill Paul 		}
2415a94100faSBill Paul 
2416a94100faSBill Paul 		/*
2417a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2418a94100faSBill Paul 		 * to him.
2419a94100faSBill Paul 		 */
242059a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
242152732175SMax Laier 
242252732175SMax Laier 		queued++;
2423a94100faSBill Paul 	}
2424a94100faSBill Paul 
2425ed510fb0SBill Paul 	if (queued == 0) {
2426ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2427d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2428ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2429ed510fb0SBill Paul #endif
2430ed510fb0SBill Paul 		RL_UNLOCK(sc);
243152732175SMax Laier 		return;
2432ed510fb0SBill Paul 	}
243352732175SMax Laier 
2434a94100faSBill Paul 	/* Flush the TX descriptors */
2435a94100faSBill Paul 
2436a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2437a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2438a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2439a94100faSBill Paul 
24400fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2441a94100faSBill Paul 
2442ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2443a94100faSBill Paul 	/*
2444a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2445a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2446a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2447a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2448a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2449a94100faSBill Paul 	 * the timer count is reset to 0.
2450a94100faSBill Paul 	 */
2451a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2452ed510fb0SBill Paul #endif
2453a94100faSBill Paul 
2454a94100faSBill Paul 	/*
2455a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2456a94100faSBill Paul 	 */
24571d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2458ed510fb0SBill Paul 
2459ed510fb0SBill Paul 	RL_UNLOCK(sc);
2460a94100faSBill Paul }
2461a94100faSBill Paul 
2462a94100faSBill Paul static void
24637b5ffebfSPyun YongHyeon re_init(void *xsc)
2464a94100faSBill Paul {
2465a94100faSBill Paul 	struct rl_softc		*sc = xsc;
246697b9d4baSJohn-Mark Gurney 
246797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
246897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
246997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
247097b9d4baSJohn-Mark Gurney }
247197b9d4baSJohn-Mark Gurney 
247297b9d4baSJohn-Mark Gurney static void
24737b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
247497b9d4baSJohn-Mark Gurney {
2475fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2476a94100faSBill Paul 	struct mii_data		*mii;
2477a94100faSBill Paul 	u_int32_t		rxcfg = 0;
247870acaecfSPyun YongHyeon 	uint16_t		cfg;
24794d3d7085SBernd Walter 	union {
24804d3d7085SBernd Walter 		uint32_t align_dummy;
24814d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
24824d3d7085SBernd Walter         } eaddr;
2483a94100faSBill Paul 
248497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
248597b9d4baSJohn-Mark Gurney 
2486a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2487a94100faSBill Paul 
2488a94100faSBill Paul 	/*
2489a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2490a94100faSBill Paul 	 */
2491a94100faSBill Paul 	re_stop(sc);
2492a94100faSBill Paul 
2493b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2494b659f1f0SPyun YongHyeon 	re_reset(sc);
2495b659f1f0SPyun YongHyeon 
2496a94100faSBill Paul 	/*
2497c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2498edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2499c2c6548bSBill Paul 	 * before all others.
2500c2c6548bSBill Paul 	 */
250170acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
250270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
250370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
250470acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
250570acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2506deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2507deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2508deb5c680SPyun YongHyeon 		/* XXX magic. */
2509deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2510deb5c680SPyun YongHyeon 	} else
2511deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2512deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
2513ae644087SPyun YongHyeon 	/*
2514ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2515ae644087SPyun YongHyeon 	 * allowed in controller.
2516ae644087SPyun YongHyeon 	 */
2517ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2518ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2519ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2520ae644087SPyun YongHyeon 	}
2521c2c6548bSBill Paul 
2522c2c6548bSBill Paul 	/*
2523a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2524a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2525a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2526a94100faSBill Paul 	 */
25274d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
25284d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2529a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2530ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2531ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2532ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2533ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2534a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2535a94100faSBill Paul 
2536a94100faSBill Paul 	/*
2537a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2538a94100faSBill Paul 	 */
2539a94100faSBill Paul 	re_rx_list_init(sc);
2540a94100faSBill Paul 	re_tx_list_init(sc);
2541a94100faSBill Paul 
2542a94100faSBill Paul 	/*
2543d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2544d01fac16SPyun YongHyeon 	 */
2545d01fac16SPyun YongHyeon 
2546d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2547d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2548d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2549d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2550d01fac16SPyun YongHyeon 
2551d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2552d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2553d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2554d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2555d01fac16SPyun YongHyeon 
2556d01fac16SPyun YongHyeon 	/*
2557a94100faSBill Paul 	 * Enable transmit and receive.
2558a94100faSBill Paul 	 */
2559a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2560a94100faSBill Paul 
2561a94100faSBill Paul 	/*
2562a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2563a94100faSBill Paul 	 */
2564abc8ff44SBill Paul 	if (sc->rl_testmode) {
2565abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2566abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2567abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2568a94100faSBill Paul 		else
2569abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2570abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2571abc8ff44SBill Paul 	} else
2572a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2573d01fac16SPyun YongHyeon 
2574d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2575d01fac16SPyun YongHyeon 
2576a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2577a94100faSBill Paul 
2578a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2579a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2580a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2581a94100faSBill Paul 
2582a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
258361021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2584a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
258561021536SJohn-Mark Gurney 	else
2586a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2587a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2588a94100faSBill Paul 
2589a94100faSBill Paul 	/*
2590a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2591a94100faSBill Paul 	 */
259261021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2593a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
259461021536SJohn-Mark Gurney 	else
2595a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2596a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2597a94100faSBill Paul 
2598a94100faSBill Paul 	/*
2599a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2600a94100faSBill Paul 	 */
2601a94100faSBill Paul 	re_setmulti(sc);
2602a94100faSBill Paul 
2603a94100faSBill Paul #ifdef DEVICE_POLLING
2604a94100faSBill Paul 	/*
2605a94100faSBill Paul 	 * Disable interrupts if we are polling.
2606a94100faSBill Paul 	 */
260740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2608a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2609a94100faSBill Paul 	else	/* otherwise ... */
261040929967SGleb Smirnoff #endif
2611ed510fb0SBill Paul 
2612a94100faSBill Paul 	/*
2613a94100faSBill Paul 	 * Enable interrupts.
2614a94100faSBill Paul 	 */
2615a94100faSBill Paul 	if (sc->rl_testmode)
2616a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2617a94100faSBill Paul 	else
2618a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2619ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2620a94100faSBill Paul 
2621a94100faSBill Paul 	/* Set initial TX threshold */
2622a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2623a94100faSBill Paul 
2624a94100faSBill Paul 	/* Start RX/TX process. */
2625a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2626a94100faSBill Paul #ifdef notdef
2627a94100faSBill Paul 	/* Enable receiver and transmitter. */
2628a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2629a94100faSBill Paul #endif
2630a94100faSBill Paul 
2631ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2632a94100faSBill Paul 	/*
2633a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2634a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2635a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2636a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2637a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2638a94100faSBill Paul 	 */
2639a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2640a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2641a94100faSBill Paul 	else
2642a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2643ed510fb0SBill Paul #endif
2644a94100faSBill Paul 
2645a94100faSBill Paul 	/*
2646a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2647a94100faSBill Paul 	 * size so we can receive jumbo frames.
2648a94100faSBill Paul 	 */
2649a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2650a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2651a94100faSBill Paul 
265297b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2653a94100faSBill Paul 		return;
2654a94100faSBill Paul 
2655a94100faSBill Paul 	mii_mediachg(mii);
2656a94100faSBill Paul 
265719ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2658a94100faSBill Paul 
265913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
266013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2661a94100faSBill Paul 
2662351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
26631d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2664d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2665a94100faSBill Paul }
2666a94100faSBill Paul 
2667a94100faSBill Paul /*
2668a94100faSBill Paul  * Set media options.
2669a94100faSBill Paul  */
2670a94100faSBill Paul static int
26717b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
2672a94100faSBill Paul {
2673a94100faSBill Paul 	struct rl_softc		*sc;
2674a94100faSBill Paul 	struct mii_data		*mii;
26756f0f9b12SPyun YongHyeon 	int			error;
2676a94100faSBill Paul 
2677a94100faSBill Paul 	sc = ifp->if_softc;
2678a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2679d1754a9bSJohn Baldwin 	RL_LOCK(sc);
26806f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
2681d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2682a94100faSBill Paul 
26836f0f9b12SPyun YongHyeon 	return (error);
2684a94100faSBill Paul }
2685a94100faSBill Paul 
2686a94100faSBill Paul /*
2687a94100faSBill Paul  * Report current media status.
2688a94100faSBill Paul  */
2689a94100faSBill Paul static void
26907b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2691a94100faSBill Paul {
2692a94100faSBill Paul 	struct rl_softc		*sc;
2693a94100faSBill Paul 	struct mii_data		*mii;
2694a94100faSBill Paul 
2695a94100faSBill Paul 	sc = ifp->if_softc;
2696a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2697a94100faSBill Paul 
2698d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2699a94100faSBill Paul 	mii_pollstat(mii);
2700d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2701a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2702a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2703a94100faSBill Paul }
2704a94100faSBill Paul 
2705a94100faSBill Paul static int
27067b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2707a94100faSBill Paul {
2708a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2709a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2710a94100faSBill Paul 	struct mii_data		*mii;
271140929967SGleb Smirnoff 	int			error = 0;
2712a94100faSBill Paul 
2713a94100faSBill Paul 	switch (command) {
2714a94100faSBill Paul 	case SIOCSIFMTU:
2715c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2716a94100faSBill Paul 			error = EINVAL;
2717c1d0b573SPyun YongHyeon 			break;
2718c1d0b573SPyun YongHyeon 		}
2719351a76f9SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 &&
2720c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2721c1d0b573SPyun YongHyeon 			error = EINVAL;
2722c1d0b573SPyun YongHyeon 			break;
2723c1d0b573SPyun YongHyeon 		}
2724c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2725c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2726a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2727ae644087SPyun YongHyeon 		if (ifp->if_mtu > RL_TSO_MTU &&
2728ae644087SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2729ae644087SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_TSO4;
2730ae644087SPyun YongHyeon 			ifp->if_hwassist &= ~CSUM_TSO;
2731ae644087SPyun YongHyeon 		}
2732d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2733a94100faSBill Paul 		break;
2734a94100faSBill Paul 	case SIOCSIFFLAGS:
273597b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2736eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2737eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2738eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
27393021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2740eed497bbSPyun YongHyeon 					re_setmulti(sc);
2741eed497bbSPyun YongHyeon 			} else
274297b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2743eed497bbSPyun YongHyeon 		} else {
2744eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2745a94100faSBill Paul 				re_stop(sc);
2746eed497bbSPyun YongHyeon 		}
2747eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
274897b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2749a94100faSBill Paul 		break;
2750a94100faSBill Paul 	case SIOCADDMULTI:
2751a94100faSBill Paul 	case SIOCDELMULTI:
275297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2753a94100faSBill Paul 		re_setmulti(sc);
275497b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2755a94100faSBill Paul 		break;
2756a94100faSBill Paul 	case SIOCGIFMEDIA:
2757a94100faSBill Paul 	case SIOCSIFMEDIA:
2758a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2759a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2760a94100faSBill Paul 		break;
2761a94100faSBill Paul 	case SIOCSIFCAP:
276240929967SGleb Smirnoff 	    {
2763f051cb85SGleb Smirnoff 		int mask, reinit;
2764f051cb85SGleb Smirnoff 
2765f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2766f051cb85SGleb Smirnoff 		reinit = 0;
276740929967SGleb Smirnoff #ifdef DEVICE_POLLING
276840929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
276940929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
277040929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
277140929967SGleb Smirnoff 				if (error)
277240929967SGleb Smirnoff 					return(error);
2773d1754a9bSJohn Baldwin 				RL_LOCK(sc);
277440929967SGleb Smirnoff 				/* Disable interrupts */
277540929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
277640929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
277740929967SGleb Smirnoff 				RL_UNLOCK(sc);
277840929967SGleb Smirnoff 			} else {
277940929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
278040929967SGleb Smirnoff 				/* Enable interrupts. */
278140929967SGleb Smirnoff 				RL_LOCK(sc);
278240929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
278340929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
278440929967SGleb Smirnoff 				RL_UNLOCK(sc);
278540929967SGleb Smirnoff 			}
278640929967SGleb Smirnoff 		}
278740929967SGleb Smirnoff #endif /* DEVICE_POLLING */
278840929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2789f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2790a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2791dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2792a94100faSBill Paul 			else
2793b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2794f051cb85SGleb Smirnoff 			reinit = 1;
279540929967SGleb Smirnoff 		}
2796f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2797f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2798f051cb85SGleb Smirnoff 			reinit = 1;
2799f051cb85SGleb Smirnoff 		}
2800dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2801dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2802dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2803dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2804dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2805dc74159dSPyun YongHyeon 			else
2806dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2807ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
2808ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2809ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
2810ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2811ae644087SPyun YongHyeon 			}
2812dc74159dSPyun YongHyeon 		}
28137467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
28147467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
28157467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
28167467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
28177467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
28187467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
28197467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
28207467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
28217467bd53SPyun YongHyeon 		}
2822f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2823f051cb85SGleb Smirnoff 			re_init(sc);
2824960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
282540929967SGleb Smirnoff 	    }
2826a94100faSBill Paul 		break;
2827a94100faSBill Paul 	default:
2828a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2829a94100faSBill Paul 		break;
2830a94100faSBill Paul 	}
2831a94100faSBill Paul 
2832a94100faSBill Paul 	return (error);
2833a94100faSBill Paul }
2834a94100faSBill Paul 
2835a94100faSBill Paul static void
28367b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
28371d545c7aSMarius Strobl {
2838130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
2839a94100faSBill Paul 
28401d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
28411d545c7aSMarius Strobl 
28421d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
28431d545c7aSMarius Strobl 		return;
28441d545c7aSMarius Strobl 
2845130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
2846a94100faSBill Paul 	re_txeof(sc);
2847130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
2848130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
2849130b6dfbSPyun YongHyeon 		    "-- recovering\n");
2850130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2851130b6dfbSPyun YongHyeon 			taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2852130b6dfbSPyun YongHyeon 		return;
2853130b6dfbSPyun YongHyeon 	}
2854130b6dfbSPyun YongHyeon 
2855130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
2856130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
2857130b6dfbSPyun YongHyeon 
2858a94100faSBill Paul 	re_rxeof(sc);
285997b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2860130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2861130b6dfbSPyun YongHyeon 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2862a94100faSBill Paul }
2863a94100faSBill Paul 
2864a94100faSBill Paul /*
2865a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2866a94100faSBill Paul  * RX and TX lists.
2867a94100faSBill Paul  */
2868a94100faSBill Paul static void
28697b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
2870a94100faSBill Paul {
28710ce0868aSPyun YongHyeon 	int			i;
2872a94100faSBill Paul 	struct ifnet		*ifp;
2873d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2874d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2875a94100faSBill Paul 
287697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
287797b9d4baSJohn-Mark Gurney 
2878fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2879a94100faSBill Paul 
28801d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2881d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
288213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2883a94100faSBill Paul 
2884ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
2885ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
2886ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
2887ead8fc66SPyun YongHyeon 	else
2888a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2889ead8fc66SPyun YongHyeon 	DELAY(1000);
2890a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2891ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2892a94100faSBill Paul 
2893a94100faSBill Paul 	if (sc->rl_head != NULL) {
2894a94100faSBill Paul 		m_freem(sc->rl_head);
2895a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2896a94100faSBill Paul 	}
2897a94100faSBill Paul 
2898a94100faSBill Paul 	/* Free the TX list buffers. */
2899a94100faSBill Paul 
2900d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2901d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2902d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2903d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2904d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2905d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2906d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2907d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2908d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2909a94100faSBill Paul 		}
2910a94100faSBill Paul 	}
2911a94100faSBill Paul 
2912a94100faSBill Paul 	/* Free the RX list buffers. */
2913a94100faSBill Paul 
2914d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2915d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2916d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2917d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2918d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2919d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2920d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2921d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2922d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2923a94100faSBill Paul 		}
2924a94100faSBill Paul 	}
2925a94100faSBill Paul }
2926a94100faSBill Paul 
2927a94100faSBill Paul /*
2928a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2929a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2930a94100faSBill Paul  * resume.
2931a94100faSBill Paul  */
2932a94100faSBill Paul static int
29337b5ffebfSPyun YongHyeon re_suspend(device_t dev)
2934a94100faSBill Paul {
2935a94100faSBill Paul 	struct rl_softc		*sc;
2936a94100faSBill Paul 
2937a94100faSBill Paul 	sc = device_get_softc(dev);
2938a94100faSBill Paul 
293997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2940a94100faSBill Paul 	re_stop(sc);
29417467bd53SPyun YongHyeon 	re_setwol(sc);
2942a94100faSBill Paul 	sc->suspended = 1;
294397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2944a94100faSBill Paul 
2945a94100faSBill Paul 	return (0);
2946a94100faSBill Paul }
2947a94100faSBill Paul 
2948a94100faSBill Paul /*
2949a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2950a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2951a94100faSBill Paul  * appropriate.
2952a94100faSBill Paul  */
2953a94100faSBill Paul static int
29547b5ffebfSPyun YongHyeon re_resume(device_t dev)
2955a94100faSBill Paul {
2956a94100faSBill Paul 	struct rl_softc		*sc;
2957a94100faSBill Paul 	struct ifnet		*ifp;
2958a94100faSBill Paul 
2959a94100faSBill Paul 	sc = device_get_softc(dev);
296097b9d4baSJohn-Mark Gurney 
296197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
296297b9d4baSJohn-Mark Gurney 
2963fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2964a94100faSBill Paul 
2965a94100faSBill Paul 	/* reinitialize interface if necessary */
2966a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
296797b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2968a94100faSBill Paul 
29697467bd53SPyun YongHyeon 	/*
29707467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
29717467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
29727467bd53SPyun YongHyeon 	 */
29737467bd53SPyun YongHyeon 	re_clrwol(sc);
2974a94100faSBill Paul 	sc->suspended = 0;
297597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2976a94100faSBill Paul 
2977a94100faSBill Paul 	return (0);
2978a94100faSBill Paul }
2979a94100faSBill Paul 
2980a94100faSBill Paul /*
2981a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2982a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2983a94100faSBill Paul  */
29846a087a87SPyun YongHyeon static int
29857b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
2986a94100faSBill Paul {
2987a94100faSBill Paul 	struct rl_softc		*sc;
2988a94100faSBill Paul 
2989a94100faSBill Paul 	sc = device_get_softc(dev);
2990a94100faSBill Paul 
299197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2992a94100faSBill Paul 	re_stop(sc);
2993536fde34SMaxim Sobolev 	/*
2994536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2995536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
299672293673SRuslan Ermilov 	 * cases.
2997536fde34SMaxim Sobolev 	 */
2998536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
29997467bd53SPyun YongHyeon 	re_setwol(sc);
300097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
30016a087a87SPyun YongHyeon 
30026a087a87SPyun YongHyeon 	return (0);
3003a94100faSBill Paul }
30047467bd53SPyun YongHyeon 
30057467bd53SPyun YongHyeon static void
30067b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
30077467bd53SPyun YongHyeon {
30087467bd53SPyun YongHyeon 	struct ifnet		*ifp;
30097467bd53SPyun YongHyeon 	int			pmc;
30107467bd53SPyun YongHyeon 	uint16_t		pmstat;
30117467bd53SPyun YongHyeon 	uint8_t			v;
30127467bd53SPyun YongHyeon 
30137467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30147467bd53SPyun YongHyeon 
30157467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30167467bd53SPyun YongHyeon 		return;
30177467bd53SPyun YongHyeon 
30187467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
30197467bd53SPyun YongHyeon 	/* Enable config register write. */
30207467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30217467bd53SPyun YongHyeon 
30227467bd53SPyun YongHyeon 	/* Enable PME. */
30237467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
30247467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
30257467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30267467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
30277467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
30287467bd53SPyun YongHyeon 
30297467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30307467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30317467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
30327467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
30337467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30347467bd53SPyun YongHyeon 
30357467bd53SPyun YongHyeon 	/* Config register write done. */
3036f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30377467bd53SPyun YongHyeon 
30387467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30397467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30407467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30417467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
30427467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
30437467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
30447467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
30457467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30467467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
30477467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30487467bd53SPyun YongHyeon 
30497467bd53SPyun YongHyeon 	/*
30507467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
30517467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
30527467bd53SPyun YongHyeon 	 * needed.
30537467bd53SPyun YongHyeon 	 */
30547467bd53SPyun YongHyeon 
30557467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
30567467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
30577467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
30587467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30597467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
30607467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
30617467bd53SPyun YongHyeon }
30627467bd53SPyun YongHyeon 
30637467bd53SPyun YongHyeon static void
30647b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
30657467bd53SPyun YongHyeon {
30667467bd53SPyun YongHyeon 	int			pmc;
30677467bd53SPyun YongHyeon 	uint8_t			v;
30687467bd53SPyun YongHyeon 
30697467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30707467bd53SPyun YongHyeon 
30717467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30727467bd53SPyun YongHyeon 		return;
30737467bd53SPyun YongHyeon 
30747467bd53SPyun YongHyeon 	/* Enable config register write. */
30757467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30767467bd53SPyun YongHyeon 
30777467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30787467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30797467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30807467bd53SPyun YongHyeon 
30817467bd53SPyun YongHyeon 	/* Config register write done. */
3082f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30837467bd53SPyun YongHyeon 
30847467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30857467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30867467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30877467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30887467bd53SPyun YongHyeon }
3089