xref: /freebsd/sys/dev/re/if_re.c (revision 8281a098c68f30abd7d866957f43d357a9754ecf)
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. */
159c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1605774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1612c21710bSPyun YongHyeon static int prefer_iomap = 0;
1622c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1635774c5ffSPyun YongHyeon 
164a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
165a94100faSBill Paul 
166a94100faSBill Paul /*
167a94100faSBill Paul  * Various supported device vendors/types and their names.
168a94100faSBill Paul  */
169a94100faSBill Paul static struct rl_type re_devs[] = {
1709dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17132aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1729dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
173a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1749dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
1758281a098SPyun YongHyeon 	    "RealTek 8101E/8102E/8102EL/8103E PCIe 10/100baseTX" },
1769dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
1775fa06abeSPyun YongHyeon 	    "RealTek 8168/8168B/8168C/8168CP/8168D/8168DP/"
1785fa06abeSPyun YongHyeon 	    "8111B/8111C/8111CP/8111DP PCIe Gigabit Ethernet" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
180715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1822ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
184ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
18626390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
188dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
189a94100faSBill Paul };
190a94100faSBill Paul 
191a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
192a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
193a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
194a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
195a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
196a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
197a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
198a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
199a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
200498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
201a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
20269a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
20369a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
204566ca8caSJung-uk Kim 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB"},
205566ca8caSJung-uk Kim 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC"},
206566ca8caSJung-uk Kim 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL"},
207566ca8caSJung-uk Kim 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC"},
208a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
209a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
210ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
211ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
212b1d62f0fSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E"},
213b1d62f0fSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL"},
2143d22427cSTai-hwa Liang 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL"},
2158281a098SPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E"},
216498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2171acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
218deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C"},
219deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C"},
220deb5c680SPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP"},
2215fa06abeSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D"},
2225fa06abeSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP"},
223a94100faSBill Paul 	{ 0, 0, NULL }
224a94100faSBill Paul };
225a94100faSBill Paul 
226a94100faSBill Paul static int re_probe		(device_t);
227a94100faSBill Paul static int re_attach		(device_t);
228a94100faSBill Paul static int re_detach		(device_t);
229a94100faSBill Paul 
230d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
231a94100faSBill Paul 
232a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
233a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
234d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
235d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
236d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
237a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
238a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24022a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
24122a11c96SJohn-Mark Gurney 				(struct mbuf *);
24222a11c96SJohn-Mark Gurney #endif
2431abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
244a94100faSBill Paul static void re_txeof		(struct rl_softc *);
24597b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2461abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2471abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
24897b9d4baSJohn-Mark Gurney #endif
249ef544f63SPaolo Pisati static int re_intr		(void *);
250a94100faSBill Paul static void re_tick		(void *);
251ed510fb0SBill Paul static void re_tx_task		(void *, int);
252ed510fb0SBill Paul static void re_int_task		(void *, int);
253a94100faSBill Paul static void re_start		(struct ifnet *);
254a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
255a94100faSBill Paul static void re_init		(void *);
25697b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
257a94100faSBill Paul static void re_stop		(struct rl_softc *);
2581d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
259a94100faSBill Paul static int re_suspend		(device_t);
260a94100faSBill Paul static int re_resume		(device_t);
2616a087a87SPyun YongHyeon static int re_shutdown		(device_t);
262a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
263a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
264a94100faSBill Paul 
265a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
266a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
267ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
268a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
269a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
270a94100faSBill Paul 
271a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
272a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
273a94100faSBill Paul static void re_miibus_statchg	(device_t);
274a94100faSBill Paul 
275ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
276a94100faSBill Paul static void re_reset		(struct rl_softc *);
2777467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2787467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
279a94100faSBill Paul 
280ed510fb0SBill Paul #ifdef RE_DIAG
281a94100faSBill Paul static int re_diag		(struct rl_softc *);
282ed510fb0SBill Paul #endif
283a94100faSBill Paul 
284a94100faSBill Paul static device_method_t re_methods[] = {
285a94100faSBill Paul 	/* Device interface */
286a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
287a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
288a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
289a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
290a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
291a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
292a94100faSBill Paul 
293a94100faSBill Paul 	/* bus interface */
294a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
295a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
296a94100faSBill Paul 
297a94100faSBill Paul 	/* MII interface */
298a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
299a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
300a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
301a94100faSBill Paul 
302a94100faSBill Paul 	{ 0, 0 }
303a94100faSBill Paul };
304a94100faSBill Paul 
305a94100faSBill Paul static driver_t re_driver = {
306a94100faSBill Paul 	"re",
307a94100faSBill Paul 	re_methods,
308a94100faSBill Paul 	sizeof(struct rl_softc)
309a94100faSBill Paul };
310a94100faSBill Paul 
311a94100faSBill Paul static devclass_t re_devclass;
312a94100faSBill Paul 
313a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
314a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
315a94100faSBill Paul 
316a94100faSBill Paul #define EE_SET(x)					\
317a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
318a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
319a94100faSBill Paul 
320a94100faSBill Paul #define EE_CLR(x)					\
321a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
322a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
323a94100faSBill Paul 
324a94100faSBill Paul /*
325a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
326a94100faSBill Paul  */
327a94100faSBill Paul static void
3287b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
329a94100faSBill Paul {
3300ce0868aSPyun YongHyeon 	int			d, i;
331a94100faSBill Paul 
332ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
333a94100faSBill Paul 
334a94100faSBill Paul 	/*
335a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
336a94100faSBill Paul 	 */
337ed510fb0SBill Paul 
338ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
339a94100faSBill Paul 		if (d & i) {
340a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
341a94100faSBill Paul 		} else {
342a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
343a94100faSBill Paul 		}
344a94100faSBill Paul 		DELAY(100);
345a94100faSBill Paul 		EE_SET(RL_EE_CLK);
346a94100faSBill Paul 		DELAY(150);
347a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
348a94100faSBill Paul 		DELAY(100);
349a94100faSBill Paul 	}
350a94100faSBill Paul }
351a94100faSBill Paul 
352a94100faSBill Paul /*
353a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
354a94100faSBill Paul  */
355a94100faSBill Paul static void
3567b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
357a94100faSBill Paul {
3580ce0868aSPyun YongHyeon 	int			i;
359a94100faSBill Paul 	u_int16_t		word = 0;
360a94100faSBill Paul 
361a94100faSBill Paul 	/*
362a94100faSBill Paul 	 * Send address of word we want to read.
363a94100faSBill Paul 	 */
364a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
365a94100faSBill Paul 
366a94100faSBill Paul 	/*
367a94100faSBill Paul 	 * Start reading bits from EEPROM.
368a94100faSBill Paul 	 */
369a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
370a94100faSBill Paul 		EE_SET(RL_EE_CLK);
371a94100faSBill Paul 		DELAY(100);
372a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
373a94100faSBill Paul 			word |= i;
374a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
375a94100faSBill Paul 		DELAY(100);
376a94100faSBill Paul 	}
377a94100faSBill Paul 
378a94100faSBill Paul 	*dest = word;
379a94100faSBill Paul }
380a94100faSBill Paul 
381a94100faSBill Paul /*
382a94100faSBill Paul  * Read a sequence of words from the EEPROM.
383a94100faSBill Paul  */
384a94100faSBill Paul static void
3857b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
386a94100faSBill Paul {
387a94100faSBill Paul 	int			i;
388a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
389a94100faSBill Paul 
390ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
391ed510fb0SBill Paul 
392ed510fb0SBill Paul         DELAY(100);
393ed510fb0SBill Paul 
394a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
395ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
396a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
397ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
398a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
399be099007SPyun YongHyeon                 *ptr = word;
400a94100faSBill Paul 	}
401ed510fb0SBill Paul 
402ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
403a94100faSBill Paul }
404a94100faSBill Paul 
405a94100faSBill Paul static int
4067b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
407a94100faSBill Paul {
408a94100faSBill Paul 	struct rl_softc		*sc;
409a94100faSBill Paul 	u_int32_t		rval;
410a94100faSBill Paul 	int			i;
411a94100faSBill Paul 
412a94100faSBill Paul 	if (phy != 1)
413a94100faSBill Paul 		return (0);
414a94100faSBill Paul 
415a94100faSBill Paul 	sc = device_get_softc(dev);
416a94100faSBill Paul 
4179bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4189bac70b8SBill Paul 
4199bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4209bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4219bac70b8SBill Paul 		return (rval);
4229bac70b8SBill Paul 	}
4239bac70b8SBill Paul 
424a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
425bd9bede5SPyun YongHyeon 	DELAY(1000);
426a94100faSBill Paul 
42796b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
428a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
429a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
430a94100faSBill Paul 			break;
431f68bc089SPyun YongHyeon 		DELAY(100);
432a94100faSBill Paul 	}
433a94100faSBill Paul 
43496b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4356b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
436a94100faSBill Paul 		return (0);
437a94100faSBill Paul 	}
438a94100faSBill Paul 
439a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
440a94100faSBill Paul }
441a94100faSBill Paul 
442a94100faSBill Paul static int
4437b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
444a94100faSBill Paul {
445a94100faSBill Paul 	struct rl_softc		*sc;
446a94100faSBill Paul 	u_int32_t		rval;
447a94100faSBill Paul 	int			i;
448a94100faSBill Paul 
449a94100faSBill Paul 	sc = device_get_softc(dev);
450a94100faSBill Paul 
451a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4529bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
453bd9bede5SPyun YongHyeon 	DELAY(1000);
454a94100faSBill Paul 
45596b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
456a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
457a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
458a94100faSBill Paul 			break;
459f68bc089SPyun YongHyeon 		DELAY(100);
460a94100faSBill Paul 	}
461a94100faSBill Paul 
46296b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4636b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
464a94100faSBill Paul 		return (0);
465a94100faSBill Paul 	}
466a94100faSBill Paul 
467a94100faSBill Paul 	return (0);
468a94100faSBill Paul }
469a94100faSBill Paul 
470a94100faSBill Paul static int
4717b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
472a94100faSBill Paul {
473a94100faSBill Paul 	struct rl_softc		*sc;
474a94100faSBill Paul 	u_int16_t		rval = 0;
475a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
476a94100faSBill Paul 
477a94100faSBill Paul 	sc = device_get_softc(dev);
478a94100faSBill Paul 
479a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
480a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
481a94100faSBill Paul 		return (rval);
482a94100faSBill Paul 	}
483a94100faSBill Paul 
484a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
485a94100faSBill Paul 	if (phy) {
486a94100faSBill Paul 		return (0);
487a94100faSBill Paul 	}
488a94100faSBill Paul 	switch (reg) {
489a94100faSBill Paul 	case MII_BMCR:
490a94100faSBill Paul 		re8139_reg = RL_BMCR;
491a94100faSBill Paul 		break;
492a94100faSBill Paul 	case MII_BMSR:
493a94100faSBill Paul 		re8139_reg = RL_BMSR;
494a94100faSBill Paul 		break;
495a94100faSBill Paul 	case MII_ANAR:
496a94100faSBill Paul 		re8139_reg = RL_ANAR;
497a94100faSBill Paul 		break;
498a94100faSBill Paul 	case MII_ANER:
499a94100faSBill Paul 		re8139_reg = RL_ANER;
500a94100faSBill Paul 		break;
501a94100faSBill Paul 	case MII_ANLPAR:
502a94100faSBill Paul 		re8139_reg = RL_LPAR;
503a94100faSBill Paul 		break;
504a94100faSBill Paul 	case MII_PHYIDR1:
505a94100faSBill Paul 	case MII_PHYIDR2:
506a94100faSBill Paul 		return (0);
507a94100faSBill Paul 	/*
508a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
509a94100faSBill Paul 	 * register. If we have a link partner which does not
510a94100faSBill Paul 	 * support NWAY, this is the register which will tell
511a94100faSBill Paul 	 * us the results of parallel detection.
512a94100faSBill Paul 	 */
513a94100faSBill Paul 	case RL_MEDIASTAT:
514a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
515a94100faSBill Paul 		return (rval);
516a94100faSBill Paul 	default:
5176b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
518a94100faSBill Paul 		return (0);
519a94100faSBill Paul 	}
520a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
521baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
522baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
523baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
524baa12772SPyun YongHyeon 	}
525a94100faSBill Paul 	return (rval);
526a94100faSBill Paul }
527a94100faSBill Paul 
528a94100faSBill Paul static int
5297b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
530a94100faSBill Paul {
531a94100faSBill Paul 	struct rl_softc		*sc;
532a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
533a94100faSBill Paul 	int			rval = 0;
534a94100faSBill Paul 
535a94100faSBill Paul 	sc = device_get_softc(dev);
536a94100faSBill Paul 
537a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
538a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
539a94100faSBill Paul 		return (rval);
540a94100faSBill Paul 	}
541a94100faSBill Paul 
542a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
54397b9d4baSJohn-Mark Gurney 	if (phy)
544a94100faSBill Paul 		return (0);
54597b9d4baSJohn-Mark Gurney 
546a94100faSBill Paul 	switch (reg) {
547a94100faSBill Paul 	case MII_BMCR:
548a94100faSBill Paul 		re8139_reg = RL_BMCR;
549baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
550baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
551baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
552baa12772SPyun YongHyeon 		}
553a94100faSBill Paul 		break;
554a94100faSBill Paul 	case MII_BMSR:
555a94100faSBill Paul 		re8139_reg = RL_BMSR;
556a94100faSBill Paul 		break;
557a94100faSBill Paul 	case MII_ANAR:
558a94100faSBill Paul 		re8139_reg = RL_ANAR;
559a94100faSBill Paul 		break;
560a94100faSBill Paul 	case MII_ANER:
561a94100faSBill Paul 		re8139_reg = RL_ANER;
562a94100faSBill Paul 		break;
563a94100faSBill Paul 	case MII_ANLPAR:
564a94100faSBill Paul 		re8139_reg = RL_LPAR;
565a94100faSBill Paul 		break;
566a94100faSBill Paul 	case MII_PHYIDR1:
567a94100faSBill Paul 	case MII_PHYIDR2:
568a94100faSBill Paul 		return (0);
569a94100faSBill Paul 		break;
570a94100faSBill Paul 	default:
5716b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
572a94100faSBill Paul 		return (0);
573a94100faSBill Paul 	}
574a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
575a94100faSBill Paul 	return (0);
576a94100faSBill Paul }
577a94100faSBill Paul 
578a94100faSBill Paul static void
5797b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
580a94100faSBill Paul {
581130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
582130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
583130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
584a11e2f18SBruce M Simpson 
585130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
586130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
587130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
588130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
589130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
590130b6dfbSPyun YongHyeon 		return;
591130b6dfbSPyun YongHyeon 
592130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
593130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
594130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
595130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
596130b6dfbSPyun YongHyeon 		case IFM_10_T:
597130b6dfbSPyun YongHyeon 		case IFM_100_TX:
598130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
599130b6dfbSPyun YongHyeon 			break;
600130b6dfbSPyun YongHyeon 		case IFM_1000_T:
601130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
602130b6dfbSPyun YongHyeon 				break;
603130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
604130b6dfbSPyun YongHyeon 			break;
605130b6dfbSPyun YongHyeon 		default:
606130b6dfbSPyun YongHyeon 			break;
607130b6dfbSPyun YongHyeon 		}
608130b6dfbSPyun YongHyeon 	}
609130b6dfbSPyun YongHyeon 	/*
610130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
611130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
612130b6dfbSPyun YongHyeon 	 * parameters.
613130b6dfbSPyun YongHyeon 	 */
614a94100faSBill Paul }
615a94100faSBill Paul 
616a94100faSBill Paul /*
617ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
618a94100faSBill Paul  */
619a94100faSBill Paul static void
620ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
621a94100faSBill Paul {
622a94100faSBill Paul 	struct ifnet		*ifp;
623a94100faSBill Paul 	struct ifmultiaddr	*ifma;
624ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
625ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
626a94100faSBill Paul 
62797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
62897b9d4baSJohn-Mark Gurney 
629fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
630a94100faSBill Paul 
631ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
632a94100faSBill Paul 
633ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6347c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6357c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
636a0637caaSPyun YongHyeon 		/*
637a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
638a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
639a0637caaSPyun YongHyeon 		 * promiscuous mode.
640a0637caaSPyun YongHyeon 		 */
641a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
642ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
643ff191365SJung-uk Kim 		goto done;
644a94100faSBill Paul 	}
645a94100faSBill Paul 
646eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
647a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
648a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
649a94100faSBill Paul 			continue;
6500e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6510e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
652a94100faSBill Paul 		if (h < 32)
653a94100faSBill Paul 			hashes[0] |= (1 << h);
654a94100faSBill Paul 		else
655a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
656a94100faSBill Paul 	}
657eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
658a94100faSBill Paul 
659ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
660bb7dfefbSBill Paul 		/*
661ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
662ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
663ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
664ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
665ff191365SJung-uk Kim 		 * devices.
666bb7dfefbSBill Paul 		 */
667aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
668ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
669ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
670ff191365SJung-uk Kim 			hashes[1] = h;
671ff191365SJung-uk Kim 		}
672ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
673ff191365SJung-uk Kim 	}
674ff191365SJung-uk Kim 
675ff191365SJung-uk Kim done:
676a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
677a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
678ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
679bb7dfefbSBill Paul }
680a94100faSBill Paul 
681a94100faSBill Paul static void
6827b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
683a94100faSBill Paul {
6840ce0868aSPyun YongHyeon 	int			i;
685a94100faSBill Paul 
68697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
68797b9d4baSJohn-Mark Gurney 
688a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
689a94100faSBill Paul 
690a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
691a94100faSBill Paul 		DELAY(10);
692a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
693a94100faSBill Paul 			break;
694a94100faSBill Paul 	}
695a94100faSBill Paul 	if (i == RL_TIMEOUT)
6966b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
697a94100faSBill Paul 
698566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
699a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
700566ca8caSJung-uk Kim 	if (sc->rl_hwrev == RL_HWREV_8169S)
701566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
702a94100faSBill Paul }
703a94100faSBill Paul 
704ed510fb0SBill Paul #ifdef RE_DIAG
705ed510fb0SBill Paul 
706a94100faSBill Paul /*
707a94100faSBill Paul  * The following routine is designed to test for a defect on some
708a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
709a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
710a94100faSBill Paul  * should be pulled high. The result of this defect is that the
711a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
712a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
713a94100faSBill Paul  * because the 64-bit data lines aren't connected.
714a94100faSBill Paul  *
715a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
716a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
717a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
718a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
719a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
720a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
721a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
722a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
723a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
724a94100faSBill Paul  */
725a94100faSBill Paul 
726a94100faSBill Paul static int
7277b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
728a94100faSBill Paul {
729fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
730a94100faSBill Paul 	struct mbuf		*m0;
731a94100faSBill Paul 	struct ether_header	*eh;
732a94100faSBill Paul 	struct rl_desc		*cur_rx;
733a94100faSBill Paul 	u_int16_t		status;
734a94100faSBill Paul 	u_int32_t		rxstat;
735ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
736a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
737a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
738a94100faSBill Paul 
739a94100faSBill Paul 	/* Allocate a single mbuf */
740a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
741a94100faSBill Paul 	if (m0 == NULL)
742a94100faSBill Paul 		return (ENOBUFS);
743a94100faSBill Paul 
74497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
74597b9d4baSJohn-Mark Gurney 
746a94100faSBill Paul 	/*
747a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
748a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
749a94100faSBill Paul 	 * following special functions:
750a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
751a94100faSBill Paul 	 * - Enables digital loopback mode
752a94100faSBill Paul 	 * - Leaves interrupts turned off
753a94100faSBill Paul 	 */
754a94100faSBill Paul 
755a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
756a94100faSBill Paul 	sc->rl_testmode = 1;
7578476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
75897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
759351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
760ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
761ed510fb0SBill Paul 		phyaddr = 1;
762ed510fb0SBill Paul 	else
763ed510fb0SBill Paul 		phyaddr = 0;
764ed510fb0SBill Paul 
765ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
766ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
767ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
768ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
769ed510fb0SBill Paul 			break;
770ed510fb0SBill Paul 	}
771ed510fb0SBill Paul 
772ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
773ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
774ed510fb0SBill Paul 
775804af9a1SBill Paul 	DELAY(100000);
776a94100faSBill Paul 
777a94100faSBill Paul 	/* Put some data in the mbuf */
778a94100faSBill Paul 
779a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
780a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
781a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
782a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
783a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
784a94100faSBill Paul 
7857cae6651SBill Paul 	/*
7867cae6651SBill Paul 	 * Queue the packet, start transmission.
7877cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7887cae6651SBill Paul 	 */
789a94100faSBill Paul 
790abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
79197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79252732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7937cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
79497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
795a94100faSBill Paul 	m0 = NULL;
796a94100faSBill Paul 
797a94100faSBill Paul 	/* Wait for it to propagate through the chip */
798a94100faSBill Paul 
799abc8ff44SBill Paul 	DELAY(100000);
800a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
801a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
802ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
803abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
804abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
805a94100faSBill Paul 			break;
806a94100faSBill Paul 		DELAY(10);
807a94100faSBill Paul 	}
808a94100faSBill Paul 
809a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8106b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8116b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8126b9f5c94SGleb Smirnoff 		    " loopback mode\n");
813a94100faSBill Paul 		error = EIO;
814a94100faSBill Paul 		goto done;
815a94100faSBill Paul 	}
816a94100faSBill Paul 
817a94100faSBill Paul 	/*
818a94100faSBill Paul 	 * The packet should have been dumped into the first
819a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
820a94100faSBill Paul 	 */
821a94100faSBill Paul 
822a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
823a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
824a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
825d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
826d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
827d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
828d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
829d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
830a94100faSBill Paul 
831d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
832d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
833a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
834a94100faSBill Paul 
835a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
836a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
837a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
838a94100faSBill Paul 
839a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8406b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8416b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
842a94100faSBill Paul 		error = EIO;
843a94100faSBill Paul 		goto done;
844a94100faSBill Paul 	}
845a94100faSBill Paul 
846a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
847a94100faSBill Paul 
848a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
849a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
850a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8516b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8526b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
853a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8546b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
855a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
856a94100faSBill Paul 		    ntohs(eh->ether_type));
8576b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8586b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8596b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8606b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8616b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8626b9f5c94SGleb Smirnoff 		    "details.\n");
863a94100faSBill Paul 		error = EIO;
864a94100faSBill Paul 	}
865a94100faSBill Paul 
866a94100faSBill Paul done:
867a94100faSBill Paul 	/* Turn interface off, release resources */
868a94100faSBill Paul 
869a94100faSBill Paul 	sc->rl_testmode = 0;
870351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
871a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
872a94100faSBill Paul 	re_stop(sc);
873a94100faSBill Paul 	if (m0 != NULL)
874a94100faSBill Paul 		m_freem(m0);
875a94100faSBill Paul 
87697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
87797b9d4baSJohn-Mark Gurney 
878a94100faSBill Paul 	return (error);
879a94100faSBill Paul }
880a94100faSBill Paul 
881ed510fb0SBill Paul #endif
882ed510fb0SBill Paul 
883a94100faSBill Paul /*
884a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
885a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
886a94100faSBill Paul  */
887a94100faSBill Paul static int
8887b5ffebfSPyun YongHyeon re_probe(device_t dev)
889a94100faSBill Paul {
890a94100faSBill Paul 	struct rl_type		*t;
891dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
892dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
893dfdb409eSPyun YongHyeon 	int			i;
894a94100faSBill Paul 
895dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
896dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
897dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
898dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
899a94100faSBill Paul 
900dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
901dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
90226390635SJohn Baldwin 			/*
90326390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
904dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
90526390635SJohn Baldwin 			 */
906a94100faSBill Paul 			return (ENXIO);
907a94100faSBill Paul 		}
908dfdb409eSPyun YongHyeon 	}
909dfdb409eSPyun YongHyeon 
910dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
911dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
912dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
913dfdb409eSPyun YongHyeon 			return (ENXIO);
914dfdb409eSPyun YongHyeon 		}
915dfdb409eSPyun YongHyeon 	}
916dfdb409eSPyun YongHyeon 
917dfdb409eSPyun YongHyeon 	t = re_devs;
918dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
919dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
920a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
921d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
922a94100faSBill Paul 		}
923a94100faSBill Paul 	}
924a94100faSBill Paul 
925a94100faSBill Paul 	return (ENXIO);
926a94100faSBill Paul }
927a94100faSBill Paul 
928a94100faSBill Paul /*
929a94100faSBill Paul  * Map a single buffer address.
930a94100faSBill Paul  */
931a94100faSBill Paul 
932a94100faSBill Paul static void
9337b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
934a94100faSBill Paul {
9358fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
936a94100faSBill Paul 
937a94100faSBill Paul 	if (error)
938a94100faSBill Paul 		return;
939a94100faSBill Paul 
940a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
941a94100faSBill Paul 	addr = arg;
942a94100faSBill Paul 	*addr = segs->ds_addr;
943a94100faSBill Paul }
944a94100faSBill Paul 
945a94100faSBill Paul static int
9467b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
947a94100faSBill Paul {
948d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
949a94100faSBill Paul 	int			error;
950a94100faSBill Paul 	int			i;
951a94100faSBill Paul 
952d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
953d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
954d65abd66SPyun YongHyeon 
955d65abd66SPyun YongHyeon 	/*
956d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
957ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
958ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
959ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
960ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
961ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
962d65abd66SPyun YongHyeon 	 */
963d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
964ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
965d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
966d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
967d65abd66SPyun YongHyeon 	if (error) {
968d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
969d65abd66SPyun YongHyeon 		return (error);
970d65abd66SPyun YongHyeon 	}
971d65abd66SPyun YongHyeon 
972d65abd66SPyun YongHyeon 	/*
973d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
974d65abd66SPyun YongHyeon 	 */
975d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
976d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
977d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
978d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
979d65abd66SPyun YongHyeon 	if (error) {
980d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
981d65abd66SPyun YongHyeon 		return (error);
982d65abd66SPyun YongHyeon 	}
983d65abd66SPyun YongHyeon 
984a94100faSBill Paul 	/*
985a94100faSBill Paul 	 * Allocate map for RX mbufs.
986a94100faSBill Paul 	 */
987d65abd66SPyun YongHyeon 
988d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
989d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
990d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
991a94100faSBill Paul 	if (error) {
992d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
993d65abd66SPyun YongHyeon 		return (error);
994a94100faSBill Paul 	}
995a94100faSBill Paul 
996a94100faSBill Paul 	/*
997a94100faSBill Paul 	 * Allocate map for TX descriptor list.
998a94100faSBill Paul 	 */
999a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1000a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1001d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1002a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1003a94100faSBill Paul 	if (error) {
1004d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1005d65abd66SPyun YongHyeon 		return (error);
1006a94100faSBill Paul 	}
1007a94100faSBill Paul 
1008a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1009a94100faSBill Paul 
1010a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1011d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1012d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1013a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1014d65abd66SPyun YongHyeon 	if (error) {
1015d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1016d65abd66SPyun YongHyeon 		return (error);
1017d65abd66SPyun YongHyeon 	}
1018a94100faSBill Paul 
1019a94100faSBill Paul 	/* Load the map for the TX ring. */
1020a94100faSBill Paul 
1021d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1022a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1023a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1024d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1025a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1026d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1027d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1028d65abd66SPyun YongHyeon 		return (ENOMEM);
1029d65abd66SPyun YongHyeon 	}
1030a94100faSBill Paul 
1031a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1032a94100faSBill Paul 
1033d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1034d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1035d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1036a94100faSBill Paul 		if (error) {
1037d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1038d65abd66SPyun YongHyeon 			return (error);
1039a94100faSBill Paul 		}
1040a94100faSBill Paul 	}
1041a94100faSBill Paul 
1042a94100faSBill Paul 	/*
1043a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1044a94100faSBill Paul 	 */
1045a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1046a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1047d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1048a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1049a94100faSBill Paul 	if (error) {
1050d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1051d65abd66SPyun YongHyeon 		return (error);
1052a94100faSBill Paul 	}
1053a94100faSBill Paul 
1054a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1055a94100faSBill Paul 
1056a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1057d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1058d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1059a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1060d65abd66SPyun YongHyeon 	if (error) {
1061d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1062d65abd66SPyun YongHyeon 		return (error);
1063d65abd66SPyun YongHyeon 	}
1064a94100faSBill Paul 
1065a94100faSBill Paul 	/* Load the map for the RX ring. */
1066a94100faSBill Paul 
1067d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1068a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1069a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1070d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1071a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1072d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1073d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1074d65abd66SPyun YongHyeon 		return (ENOMEM);
1075d65abd66SPyun YongHyeon 	}
1076a94100faSBill Paul 
1077a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1078a94100faSBill Paul 
1079d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1080d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1081a94100faSBill Paul 	if (error) {
1082d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1083d65abd66SPyun YongHyeon 		return (error);
1084d65abd66SPyun YongHyeon 	}
1085d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1086d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1087d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1088d65abd66SPyun YongHyeon 		if (error) {
1089d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1090d65abd66SPyun YongHyeon 			return (error);
1091a94100faSBill Paul 		}
1092a94100faSBill Paul 	}
1093a94100faSBill Paul 
1094a94100faSBill Paul 	return (0);
1095a94100faSBill Paul }
1096a94100faSBill Paul 
1097a94100faSBill Paul /*
1098a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1099a94100faSBill Paul  * setup and ethernet/BPF attach.
1100a94100faSBill Paul  */
1101a94100faSBill Paul static int
11027b5ffebfSPyun YongHyeon re_attach(device_t dev)
1103a94100faSBill Paul {
1104a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1105be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1106a94100faSBill Paul 	struct rl_softc		*sc;
1107a94100faSBill Paul 	struct ifnet		*ifp;
1108a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1109a94100faSBill Paul 	int			hwrev;
1110ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
1111d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11125774c5ffSPyun YongHyeon 	int			msic, reg;
111303ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1114a94100faSBill Paul 
1115a94100faSBill Paul 	sc = device_get_softc(dev);
1116ed510fb0SBill Paul 	sc->rl_dev = dev;
1117a94100faSBill Paul 
1118a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
111997b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1120d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1121d1754a9bSJohn Baldwin 
1122a94100faSBill Paul 	/*
1123a94100faSBill Paul 	 * Map control/status registers.
1124a94100faSBill Paul 	 */
1125a94100faSBill Paul 	pci_enable_busmaster(dev);
1126a94100faSBill Paul 
1127ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
11282c21710bSPyun YongHyeon 	/*
11292c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
11302c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
11312c21710bSPyun YongHyeon 	 * is used always activate io mapping.
11322c21710bSPyun YongHyeon 	 */
11332c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
11342c21710bSPyun YongHyeon 		prefer_iomap = 1;
11352c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1136ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1137ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1138ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1139ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1140ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
11412c21710bSPyun YongHyeon 	} else {
11422c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
11432c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
11442c21710bSPyun YongHyeon 	}
1145ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1146ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
11472c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1148ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1149ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1150ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1151ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
11522c21710bSPyun YongHyeon 	}
1153ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1154d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1155a94100faSBill Paul 		error = ENXIO;
1156a94100faSBill Paul 		goto fail;
1157a94100faSBill Paul 	}
1158a94100faSBill Paul 
1159a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1160a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1161a94100faSBill Paul 
11625774c5ffSPyun YongHyeon 	msic = 0;
11635774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
1164818951afSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
11655774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11665774c5ffSPyun YongHyeon 		if (bootverbose)
11675774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11685774c5ffSPyun YongHyeon 	}
1169f1bb696aSPyun YongHyeon 	if (msic > 0 && msi_disable == 0) {
1170f1bb696aSPyun YongHyeon 		msic = 1;
11715774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11725774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11735774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11745774c5ffSPyun YongHyeon 				    msic);
1175351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1176339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1177339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1178339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1179339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1180339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1181f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
11825774c5ffSPyun YongHyeon 			} else
11835774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11845774c5ffSPyun YongHyeon 		}
11855774c5ffSPyun YongHyeon 	}
1186a94100faSBill Paul 
11875774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1188351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
11895774c5ffSPyun YongHyeon 		rid = 0;
11905774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
11915774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
11925774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
11935774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1194a94100faSBill Paul 			error = ENXIO;
1195a94100faSBill Paul 			goto fail;
1196a94100faSBill Paul 		}
11975774c5ffSPyun YongHyeon 	} else {
11985774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
11995774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
12005774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
12015774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
12025774c5ffSPyun YongHyeon 				device_printf(dev,
12035774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12045774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12055774c5ffSPyun YongHyeon 				error = ENXIO;
12065774c5ffSPyun YongHyeon 				goto fail;
12075774c5ffSPyun YongHyeon 			}
12085774c5ffSPyun YongHyeon 		}
12095774c5ffSPyun YongHyeon 	}
1210a94100faSBill Paul 
12114d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
12124d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
12134d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
12144d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
12154d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
12164d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
12174d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
12184d2bf239SPyun YongHyeon 		}
12194d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12204d2bf239SPyun YongHyeon 	}
12214d2bf239SPyun YongHyeon 
1222a94100faSBill Paul 	/* Reset the adapter. */
122397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1224a94100faSBill Paul 	re_reset(sc);
122597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1226abc8ff44SBill Paul 
1227abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1228a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1229566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1230566ca8caSJung-uk Kim 	case 0x00000000:
1231566ca8caSJung-uk Kim 	case 0x10000000:
1232566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1233566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1234566ca8caSJung-uk Kim 		break;
1235566ca8caSJung-uk Kim 	default:
1236a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1237a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1238566ca8caSJung-uk Kim 		break;
1239566ca8caSJung-uk Kim 	}
1240566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1241abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1242abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1243abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1244566ca8caSJung-uk Kim 			sc->rl_hwrev = hw_rev->rl_rev;
1245abc8ff44SBill Paul 			break;
1246abc8ff44SBill Paul 		}
1247abc8ff44SBill Paul 		hw_rev++;
1248abc8ff44SBill Paul 	}
1249d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1250a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1251d65abd66SPyun YongHyeon 		error = ENXIO;
1252d65abd66SPyun YongHyeon 		goto fail;
1253d65abd66SPyun YongHyeon 	}
1254abc8ff44SBill Paul 
1255351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1256351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
1257f2e491c9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_FASTETHER |
1258f2e491c9SPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1259351a76f9SPyun YongHyeon 		break;
1260351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1261351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
1262aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE |
1263aaab4fbeSJung-uk Kim 		    RL_FLAG_FASTETHER;
1264351a76f9SPyun YongHyeon 		break;
1265b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1266b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
12673d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
1268aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE |
1269aaab4fbeSJung-uk Kim 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1270f2e491c9SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1271b1d62f0fSPyun YongHyeon 		break;
12728281a098SPyun YongHyeon 	case RL_HWREV_8103E:
12738281a098SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE |
12748281a098SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
12758281a098SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
12768281a098SPyun YongHyeon 		    RL_FLAG_MACSLEEP;
12778281a098SPyun YongHyeon 		break;
1278351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
1279351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
1280886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1281886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1282351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
1283aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1284deb5c680SPyun YongHyeon 		break;
1285deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
128661f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
128761f45a72SPyun YongHyeon 		/* FALLTHROUGH */
128861f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
128961f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
129061f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
129161f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1292deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
129359ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
12945fa06abeSPyun YongHyeon 	case RL_HWREV_8168DP:
1295aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1296f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1297f2e491c9SPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1298deb5c680SPyun YongHyeon 		/*
1299deb5c680SPyun YongHyeon 		 * These controllers support jumbo frame but it seems
1300deb5c680SPyun YongHyeon 		 * that enabling it requires touching additional magic
1301deb5c680SPyun YongHyeon 		 * registers. Depending on MAC revisions some
1302deb5c680SPyun YongHyeon 		 * controllers need to disable checksum offload. So
1303deb5c680SPyun YongHyeon 		 * disable jumbo frame until I have better idea what
1304deb5c680SPyun YongHyeon 		 * it really requires to make it support.
1305deb5c680SPyun YongHyeon 		 * RTL8168C/CP : supports up to 6KB jumbo frame.
1306deb5c680SPyun YongHyeon 		 * RTL8111C/CP : supports up to 9KB jumbo frame.
1307deb5c680SPyun YongHyeon 		 */
1308deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1309351a76f9SPyun YongHyeon 		break;
1310566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1311566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1312566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1313566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1314566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1315566ca8caSJung-uk Kim 		/* FALLTHROUGH */
13160596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
13170596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1318566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1319566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1320351a76f9SPyun YongHyeon 		break;
1321351a76f9SPyun YongHyeon 	default:
1322351a76f9SPyun YongHyeon 		break;
1323351a76f9SPyun YongHyeon 	}
1324351a76f9SPyun YongHyeon 
1325deb5c680SPyun YongHyeon 	/* Enable PME. */
1326deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1327deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1328deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1329deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1330deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1331deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1332deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1333deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1334deb5c680SPyun YongHyeon 
1335deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1336deb5c680SPyun YongHyeon 		/*
1337deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1338deb5c680SPyun YongHyeon 		 * address from EEPROM.
1339deb5c680SPyun YongHyeon 		 */
1340deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1341deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1342deb5c680SPyun YongHyeon 	} else {
1343141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1344ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1345a94100faSBill Paul 		if (re_did != 0x8129)
1346141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1347a94100faSBill Paul 
1348a94100faSBill Paul 		/*
1349a94100faSBill Paul 		 * Get station address from the EEPROM.
1350a94100faSBill Paul 		 */
1351ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1352be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1353be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1354be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1355deb5c680SPyun YongHyeon 	}
1356ed510fb0SBill Paul 
1357ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1358d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1359ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1360ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1361d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1362d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1363ed510fb0SBill Paul 	} else {
1364d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1365ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1366ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1367d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1368d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1369abc8ff44SBill Paul 	}
13709bac70b8SBill Paul 
1371a94100faSBill Paul 	error = re_allocmem(dev, sc);
1372a94100faSBill Paul 	if (error)
1373a94100faSBill Paul 		goto fail;
1374a94100faSBill Paul 
1375cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1376cd036ec1SBrooks Davis 	if (ifp == NULL) {
1377d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1378cd036ec1SBrooks Davis 		error = ENOSPC;
1379cd036ec1SBrooks Davis 		goto fail;
1380cd036ec1SBrooks Davis 	}
1381cd036ec1SBrooks Davis 
138261f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
138361f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
138461f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
138561f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
138661f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
138761f45a72SPyun YongHyeon 		else
138861f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
138961f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
139061f45a72SPyun YongHyeon 	}
139161f45a72SPyun YongHyeon 
1392351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1393351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1394351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1395351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1396351a76f9SPyun YongHyeon 	}
1397351a76f9SPyun YongHyeon 
1398a94100faSBill Paul 	/* Do MII setup */
1399a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1400a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1401d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1402a94100faSBill Paul 		error = ENXIO;
1403a94100faSBill Paul 		goto fail;
1404a94100faSBill Paul 	}
1405a94100faSBill Paul 
1406a94100faSBill Paul 	ifp->if_softc = sc;
14079bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1408a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1409a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1410a94100faSBill Paul 	ifp->if_start = re_start;
1411deb5c680SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1412deb5c680SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1413498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1414a94100faSBill Paul 	ifp->if_init = re_init;
141552732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
141652732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
141752732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1418a94100faSBill Paul 
1419ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1420ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1421ed510fb0SBill Paul 
1422a94100faSBill Paul 	/*
1423deb5c680SPyun YongHyeon 	 * XXX
1424deb5c680SPyun YongHyeon 	 * Still have no idea how to make TSO work on 8168C, 8168CP,
1425deb5c680SPyun YongHyeon 	 * 8111C and 8111CP.
1426deb5c680SPyun YongHyeon 	 */
1427deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1428deb5c680SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
1429deb5c680SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4;
1430deb5c680SPyun YongHyeon 	}
1431deb5c680SPyun YongHyeon 
1432deb5c680SPyun YongHyeon 	/*
1433a94100faSBill Paul 	 * Call MI attach routine.
1434a94100faSBill Paul 	 */
1435a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1436a94100faSBill Paul 
1437960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1438960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1439960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1440960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
14417467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
14427467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
14437467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1444960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1445a2a8420cSPyun YongHyeon 	/*
1446a2a8420cSPyun YongHyeon 	 * Don't enable TSO by default. Under certain
1447a2a8420cSPyun YongHyeon 	 * circumtances the controller generated corrupted
1448a2a8420cSPyun YongHyeon 	 * packets in TSO size.
1449a2a8420cSPyun YongHyeon 	 */
1450a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1451a2a8420cSPyun YongHyeon 	ifp->if_capenable &= ~IFCAP_TSO4;
1452960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1453960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1454960fd5b3SPyun YongHyeon #endif
1455960fd5b3SPyun YongHyeon 	/*
1456960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1457960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1458960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1459960fd5b3SPyun YongHyeon 	 */
1460960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1461960fd5b3SPyun YongHyeon 
1462ed510fb0SBill Paul #ifdef RE_DIAG
1463ed510fb0SBill Paul 	/*
1464ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1465ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1466ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1467ed510fb0SBill Paul 	 */
1468a94100faSBill Paul 
1469ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1470ed510fb0SBill Paul 		error = re_diag(sc);
1471a94100faSBill Paul 		if (error) {
1472ed510fb0SBill Paul 			device_printf(dev,
1473ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1474a94100faSBill Paul 			ether_ifdetach(ifp);
1475a94100faSBill Paul 			goto fail;
1476a94100faSBill Paul 		}
1477ed510fb0SBill Paul 	}
1478ed510fb0SBill Paul #endif
1479a94100faSBill Paul 
1480a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1481351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
14825774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14835774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14845774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14855774c5ffSPyun YongHyeon 	else {
14865774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14875774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14885774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14895774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14905774c5ffSPyun YongHyeon 			if (error != 0)
14915774c5ffSPyun YongHyeon 				break;
14925774c5ffSPyun YongHyeon 		}
14935774c5ffSPyun YongHyeon 	}
1494a94100faSBill Paul 	if (error) {
1495d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1496a94100faSBill Paul 		ether_ifdetach(ifp);
1497a94100faSBill Paul 	}
1498a94100faSBill Paul 
1499a94100faSBill Paul fail:
1500ed510fb0SBill Paul 
1501a94100faSBill Paul 	if (error)
1502a94100faSBill Paul 		re_detach(dev);
1503a94100faSBill Paul 
1504a94100faSBill Paul 	return (error);
1505a94100faSBill Paul }
1506a94100faSBill Paul 
1507a94100faSBill Paul /*
1508a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1509a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1510a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1511a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1512a94100faSBill Paul  * allocated.
1513a94100faSBill Paul  */
1514a94100faSBill Paul static int
15157b5ffebfSPyun YongHyeon re_detach(device_t dev)
1516a94100faSBill Paul {
1517a94100faSBill Paul 	struct rl_softc		*sc;
1518a94100faSBill Paul 	struct ifnet		*ifp;
15195774c5ffSPyun YongHyeon 	int			i, rid;
1520a94100faSBill Paul 
1521a94100faSBill Paul 	sc = device_get_softc(dev);
1522fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1523aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
152497b9d4baSJohn-Mark Gurney 
152581cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
152681cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
152740929967SGleb Smirnoff #ifdef DEVICE_POLLING
152840929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
152940929967SGleb Smirnoff 			ether_poll_deregister(ifp);
153040929967SGleb Smirnoff #endif
153197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
153297b9d4baSJohn-Mark Gurney #if 0
153397b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
153497b9d4baSJohn-Mark Gurney #endif
1535a94100faSBill Paul 		re_stop(sc);
1536525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1537d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
15383d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
15393d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1540a94100faSBill Paul 		/*
1541a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1542a94100faSBill Paul 		 * still had a BPF descriptor attached to this
154397b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1544a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1545a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1546a94100faSBill Paul 		 * which will try to call re_init() again. This will
1547a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1548a94100faSBill Paul 		 * which will panic the system when the kernel tries
1549a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1550a94100faSBill Paul 		 * anymore.
1551a94100faSBill Paul 		 */
1552a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1553525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1554a94100faSBill Paul 	}
1555a94100faSBill Paul 	if (sc->rl_miibus)
1556a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1557a94100faSBill Paul 	bus_generic_detach(dev);
1558a94100faSBill Paul 
155997b9d4baSJohn-Mark Gurney 	/*
156097b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
156197b9d4baSJohn-Mark Gurney 	 * stopped here.
156297b9d4baSJohn-Mark Gurney 	 */
156397b9d4baSJohn-Mark Gurney 
15645774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
15655774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
15665774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
15675774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
15685774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
15695774c5ffSPyun YongHyeon 		}
15705774c5ffSPyun YongHyeon 	}
1571ad4f426eSWarner Losh 	if (ifp != NULL)
1572ad4f426eSWarner Losh 		if_free(ifp);
1573351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
15745774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
15755774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
15765774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
15775774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
15785774c5ffSPyun YongHyeon 		}
15795774c5ffSPyun YongHyeon 	} else {
15805774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15815774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15825774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15835774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15845774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15855774c5ffSPyun YongHyeon 			}
15865774c5ffSPyun YongHyeon 		}
15875774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15885774c5ffSPyun YongHyeon 	}
1589a94100faSBill Paul 	if (sc->rl_res)
1590ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1591ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1592a94100faSBill Paul 
1593a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1594a94100faSBill Paul 
1595a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1596a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1597a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1598a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1599a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1600a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1601a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1602a94100faSBill Paul 	}
1603a94100faSBill Paul 
1604a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1605a94100faSBill Paul 
1606a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1607a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1608a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1609a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1610a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1611a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1612a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1613a94100faSBill Paul 	}
1614a94100faSBill Paul 
1615a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1616a94100faSBill Paul 
1617d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1618d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1619d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1620d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1621d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1622d65abd66SPyun YongHyeon 	}
1623d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1624d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1625d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1626d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1627d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1628d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1629d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1630d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1631a94100faSBill Paul 	}
1632a94100faSBill Paul 
1633a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1634a94100faSBill Paul 
1635a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1636a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1637a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1638a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1639a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1640a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1641a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1642a94100faSBill Paul 	}
1643a94100faSBill Paul 
1644a94100faSBill Paul 	if (sc->rl_parent_tag)
1645a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1646a94100faSBill Paul 
1647a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1648a94100faSBill Paul 
1649a94100faSBill Paul 	return (0);
1650a94100faSBill Paul }
1651a94100faSBill Paul 
1652d65abd66SPyun YongHyeon static __inline void
16537b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1654a94100faSBill Paul {
1655d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1656d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1657d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1658a94100faSBill Paul 
1659d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1660d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1661d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1662d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1663d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1664d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1665d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1666d65abd66SPyun YongHyeon }
1667d65abd66SPyun YongHyeon 
1668d65abd66SPyun YongHyeon static int
16697b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1670d65abd66SPyun YongHyeon {
1671d65abd66SPyun YongHyeon 	struct mbuf		*m;
1672d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1673d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1674d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1675d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1676d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1677d65abd66SPyun YongHyeon 	int			error, nsegs;
1678d65abd66SPyun YongHyeon 
1679d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1680d65abd66SPyun YongHyeon 	if (m == NULL)
1681a94100faSBill Paul 		return (ENOBUFS);
1682a94100faSBill Paul 
1683a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
168422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
168522a11c96SJohn-Mark Gurney 	/*
168622a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
168722a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
168822a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
168922a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
169022a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
169122a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
169222a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
169322a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
169422a11c96SJohn-Mark Gurney 	 */
169522a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
169622a11c96SJohn-Mark Gurney #endif
1697d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1698d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1699d65abd66SPyun YongHyeon 	if (error != 0) {
1700d65abd66SPyun YongHyeon 		m_freem(m);
1701d65abd66SPyun YongHyeon 		return (ENOBUFS);
1702d65abd66SPyun YongHyeon 	}
1703d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1704a94100faSBill Paul 
1705d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1706d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1707d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1708d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1709d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1710a94100faSBill Paul 	}
1711a94100faSBill Paul 
1712d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1713d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1714d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1715d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1716d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1717d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1718a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1719a94100faSBill Paul 
1720d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1721d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1722d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1723d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1724d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1725d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1726d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1727d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1728d65abd66SPyun YongHyeon 
1729a94100faSBill Paul 	return (0);
1730a94100faSBill Paul }
1731a94100faSBill Paul 
173222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
173322a11c96SJohn-Mark Gurney static __inline void
17347b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
173522a11c96SJohn-Mark Gurney {
173622a11c96SJohn-Mark Gurney 	int                     i;
173722a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
173822a11c96SJohn-Mark Gurney 
173922a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
174022a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
174122a11c96SJohn-Mark Gurney 
174222a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
174322a11c96SJohn-Mark Gurney 		*dst++ = *src++;
174422a11c96SJohn-Mark Gurney 
174522a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
174622a11c96SJohn-Mark Gurney }
174722a11c96SJohn-Mark Gurney #endif
174822a11c96SJohn-Mark Gurney 
1749a94100faSBill Paul static int
17507b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1751a94100faSBill Paul {
1752d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1753d65abd66SPyun YongHyeon 	int			i;
175497b9d4baSJohn-Mark Gurney 
175597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
175697b9d4baSJohn-Mark Gurney 
1757d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1758d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1759d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1760d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1761d65abd66SPyun YongHyeon 	/* Set EOR. */
1762d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1763d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1764a94100faSBill Paul 
1765a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1766d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1767d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1768d65abd66SPyun YongHyeon 
1769a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1770a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1771d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1772a94100faSBill Paul 
1773a94100faSBill Paul 	return (0);
1774a94100faSBill Paul }
1775a94100faSBill Paul 
1776a94100faSBill Paul static int
17777b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1778a94100faSBill Paul {
1779d65abd66SPyun YongHyeon 	int			error, i;
1780a94100faSBill Paul 
1781d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1782d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1783d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1784d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1785d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1786d65abd66SPyun YongHyeon 			return (error);
1787a94100faSBill Paul 	}
1788a94100faSBill Paul 
1789a94100faSBill Paul 	/* Flush the RX descriptors */
1790a94100faSBill Paul 
1791a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1792a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1793a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1794a94100faSBill Paul 
1795a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1796a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1797a94100faSBill Paul 
1798a94100faSBill Paul 	return (0);
1799a94100faSBill Paul }
1800a94100faSBill Paul 
1801a94100faSBill Paul /*
1802a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1803a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1804a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1805a94100faSBill Paul  */
1806ed510fb0SBill Paul static int
18071abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
1808a94100faSBill Paul {
1809a94100faSBill Paul 	struct mbuf		*m;
1810a94100faSBill Paul 	struct ifnet		*ifp;
1811a94100faSBill Paul 	int			i, total_len;
1812a94100faSBill Paul 	struct rl_desc		*cur_rx;
1813a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
18141abcdbd1SAttilio Rao 	int			maxpkt = 16, rx_npkts = 0;
1815a94100faSBill Paul 
18165120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
18175120abbfSSam Leffler 
1818fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1819a94100faSBill Paul 
1820a94100faSBill Paul 	/* Invalidate the descriptor memory */
1821a94100faSBill Paul 
1822a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1823a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1824d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1825a94100faSBill Paul 
1826d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1827d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
18285b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
18295b6d1d9dSPyun YongHyeon 			break;
1830a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1831a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1832d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1833d65abd66SPyun YongHyeon 			break;
1834d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1835a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1836d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1837a94100faSBill Paul 
1838a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1839d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1840d65abd66SPyun YongHyeon 				/*
1841d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1842d65abd66SPyun YongHyeon 				 * discard all the pieces.
1843d65abd66SPyun YongHyeon 				 */
1844d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1845d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1846d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1847d65abd66SPyun YongHyeon 				}
1848d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1849d65abd66SPyun YongHyeon 				continue;
1850d65abd66SPyun YongHyeon 			}
185122a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1852a94100faSBill Paul 			if (sc->rl_head == NULL)
1853a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1854a94100faSBill Paul 			else {
1855a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1856a94100faSBill Paul 				sc->rl_tail->m_next = m;
1857a94100faSBill Paul 				sc->rl_tail = m;
1858a94100faSBill Paul 			}
1859a94100faSBill Paul 			continue;
1860a94100faSBill Paul 		}
1861a94100faSBill Paul 
1862a94100faSBill Paul 		/*
1863a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1864a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1865a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1866a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1867a94100faSBill Paul 		 * were already used, so to make room for the extra
1868a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1869a94100faSBill Paul 		 * error' bit and shifted the other status bits
1870a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1871a94100faSBill Paul 		 * still in the same places. We have already extracted
1872a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1873a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1874a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1875a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1876a94100faSBill Paul 		 * same format as that of the 8139C+.
1877a94100faSBill Paul 		 */
1878a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1879a94100faSBill Paul 			rxstat >>= 1;
1880a94100faSBill Paul 
188122a11c96SJohn-Mark Gurney 		/*
188222a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
188322a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
188422a11c96SJohn-Mark Gurney 		 */
188522a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
188622a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1887a94100faSBill Paul 			ifp->if_ierrors++;
1888a94100faSBill Paul 			/*
1889a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1890a94100faSBill Paul 			 * discard all the pieces.
1891a94100faSBill Paul 			 */
1892a94100faSBill Paul 			if (sc->rl_head != NULL) {
1893a94100faSBill Paul 				m_freem(sc->rl_head);
1894a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1895a94100faSBill Paul 			}
1896d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1897a94100faSBill Paul 			continue;
1898a94100faSBill Paul 		}
1899a94100faSBill Paul 
1900a94100faSBill Paul 		/*
1901a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1902a94100faSBill Paul 		 * reload the current one.
1903a94100faSBill Paul 		 */
1904a94100faSBill Paul 
1905d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1906d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1907a94100faSBill Paul 			if (sc->rl_head != NULL) {
1908a94100faSBill Paul 				m_freem(sc->rl_head);
1909a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1910a94100faSBill Paul 			}
1911d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1912a94100faSBill Paul 			continue;
1913a94100faSBill Paul 		}
1914a94100faSBill Paul 
1915a94100faSBill Paul 		if (sc->rl_head != NULL) {
191622a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
191722a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
191822a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1919a94100faSBill Paul 			/*
1920a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1921a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1922a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1923a94100faSBill Paul 			 * care about anyway.
1924a94100faSBill Paul 			 */
1925a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1926a94100faSBill Paul 				sc->rl_tail->m_len -=
1927a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1928a94100faSBill Paul 				m_freem(m);
1929a94100faSBill Paul 			} else {
1930a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1931a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1932a94100faSBill Paul 				sc->rl_tail->m_next = m;
1933a94100faSBill Paul 			}
1934a94100faSBill Paul 			m = sc->rl_head;
1935a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1936a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1937a94100faSBill Paul 		} else
1938a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1939a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1940a94100faSBill Paul 
194122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
194222a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
194322a11c96SJohn-Mark Gurney #endif
1944a94100faSBill Paul 		ifp->if_ipackets++;
1945a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1946a94100faSBill Paul 
1947a94100faSBill Paul 		/* Do RX checksumming if enabled */
1948a94100faSBill Paul 
1949a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1950deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1951a94100faSBill Paul 				/* Check IP header checksum */
1952a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
1953deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1954deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1955a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1956deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1957deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1958a94100faSBill Paul 
1959a94100faSBill Paul 				/* Check TCP/UDP checksum */
1960a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
1961a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1962a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
1963a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1964a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
1965a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1966a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1967a94100faSBill Paul 				}
1968deb5c680SPyun YongHyeon 			} else {
1969deb5c680SPyun YongHyeon 				/*
1970deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
1971deb5c680SPyun YongHyeon 				 */
1972deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
1973deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1974deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1975deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1976deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
1977deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1978deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1979deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1980deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
1981deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1982deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
1983deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1984deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1985deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1986deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
1987deb5c680SPyun YongHyeon 				}
1988deb5c680SPyun YongHyeon 			}
1989a94100faSBill Paul 		}
1990ed510fb0SBill Paul 		maxpkt--;
1991d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
199278ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1993bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
199478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1995d147662cSGleb Smirnoff 		}
19965120abbfSSam Leffler 		RL_UNLOCK(sc);
1997a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
19985120abbfSSam Leffler 		RL_LOCK(sc);
19991abcdbd1SAttilio Rao 		rx_npkts++;
2000a94100faSBill Paul 	}
2001a94100faSBill Paul 
2002a94100faSBill Paul 	/* Flush the RX DMA ring */
2003a94100faSBill Paul 
2004a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2005a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2006a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2007a94100faSBill Paul 
2008a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2009ed510fb0SBill Paul 
20101abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
20111abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2012ed510fb0SBill Paul 	if (maxpkt)
2013ed510fb0SBill Paul 		return(EAGAIN);
2014ed510fb0SBill Paul 
2015ed510fb0SBill Paul 	return(0);
2016a94100faSBill Paul }
2017a94100faSBill Paul 
2018a94100faSBill Paul static void
20197b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2020a94100faSBill Paul {
2021a94100faSBill Paul 	struct ifnet		*ifp;
2022d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2023a94100faSBill Paul 	u_int32_t		txstat;
2024d65abd66SPyun YongHyeon 	int			cons;
2025d65abd66SPyun YongHyeon 
2026d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2027d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2028d65abd66SPyun YongHyeon 		return;
2029a94100faSBill Paul 
2030fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2031a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2032a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2033a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2034d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2035a94100faSBill Paul 
2036d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2037d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2038d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2039d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2040a94100faSBill Paul 			break;
2041a94100faSBill Paul 		/*
2042a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2043a94100faSBill Paul 		 * in a fragment chain, which also happens to
2044a94100faSBill Paul 		 * be the only place where the TX status bits
2045a94100faSBill Paul 		 * are valid.
2046a94100faSBill Paul 		 */
2047a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2048d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2049d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2050d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2051d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2052d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2053d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2054d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2055d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2056d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2057a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2058a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2059a94100faSBill Paul 				ifp->if_collisions++;
2060a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2061a94100faSBill Paul 				ifp->if_oerrors++;
2062a94100faSBill Paul 			else
2063a94100faSBill Paul 				ifp->if_opackets++;
2064a94100faSBill Paul 		}
2065a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2066d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2067a94100faSBill Paul 	}
2068d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2069a94100faSBill Paul 
2070a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2071a94100faSBill Paul 
2072d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2073ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2074a94100faSBill Paul 		/*
2075b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2076b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2077a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2078a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2079a94100faSBill Paul 		 */
2080a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2081ed510fb0SBill Paul #endif
2082b4b95879SMarius Strobl 	} else
2083b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2084a94100faSBill Paul }
2085a94100faSBill Paul 
2086a94100faSBill Paul static void
20877b5ffebfSPyun YongHyeon re_tick(void *xsc)
2088a94100faSBill Paul {
2089a94100faSBill Paul 	struct rl_softc		*sc;
2090d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2091a94100faSBill Paul 
2092a94100faSBill Paul 	sc = xsc;
209397b9d4baSJohn-Mark Gurney 
209497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
209597b9d4baSJohn-Mark Gurney 
20961d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2097a94100faSBill Paul 	mii_tick(mii);
20980fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
20990fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2100c2d2e19cSPyun YongHyeon 	/*
2101c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2102c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2103c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2104c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2105c2d2e19cSPyun YongHyeon 	 */
2106c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2107130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2108d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2109a94100faSBill Paul }
2110a94100faSBill Paul 
2111a94100faSBill Paul #ifdef DEVICE_POLLING
21121abcdbd1SAttilio Rao static int
2113a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2114a94100faSBill Paul {
2115a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
21161abcdbd1SAttilio Rao 	int rx_npkts = 0;
2117a94100faSBill Paul 
2118a94100faSBill Paul 	RL_LOCK(sc);
211940929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
21201abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
212197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
21221abcdbd1SAttilio Rao 	return (rx_npkts);
212397b9d4baSJohn-Mark Gurney }
212497b9d4baSJohn-Mark Gurney 
21251abcdbd1SAttilio Rao static int
212697b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
212797b9d4baSJohn-Mark Gurney {
212897b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
21291abcdbd1SAttilio Rao 	int rx_npkts;
213097b9d4baSJohn-Mark Gurney 
213197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
213297b9d4baSJohn-Mark Gurney 
2133a94100faSBill Paul 	sc->rxcycles = count;
21341abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2135a94100faSBill Paul 	re_txeof(sc);
2136a94100faSBill Paul 
213737652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2138ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2139a94100faSBill Paul 
2140a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2141a94100faSBill Paul 		u_int16_t       status;
2142a94100faSBill Paul 
2143a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2144a94100faSBill Paul 		if (status == 0xffff)
21451abcdbd1SAttilio Rao 			return (rx_npkts);
2146a94100faSBill Paul 		if (status)
2147a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2148818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2149818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2150818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2151a94100faSBill Paul 
2152a94100faSBill Paul 		/*
2153a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2154a94100faSBill Paul 		 */
2155a94100faSBill Paul 
21568476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
21578476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
215897b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2159a94100faSBill Paul 		}
21608476c243SPyun YongHyeon 	}
21611abcdbd1SAttilio Rao 	return (rx_npkts);
2162a94100faSBill Paul }
2163a94100faSBill Paul #endif /* DEVICE_POLLING */
2164a94100faSBill Paul 
2165ef544f63SPaolo Pisati static int
21667b5ffebfSPyun YongHyeon re_intr(void *arg)
2167a94100faSBill Paul {
2168a94100faSBill Paul 	struct rl_softc		*sc;
2169ed510fb0SBill Paul 	uint16_t		status;
2170a94100faSBill Paul 
2171a94100faSBill Paul 	sc = arg;
2172ed510fb0SBill Paul 
2173ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2174498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2175ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2176ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2177ed510fb0SBill Paul 
2178ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2179ed510fb0SBill Paul 
2180ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2181ed510fb0SBill Paul }
2182ed510fb0SBill Paul 
2183ed510fb0SBill Paul static void
21847b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2185ed510fb0SBill Paul {
2186ed510fb0SBill Paul 	struct rl_softc		*sc;
2187ed510fb0SBill Paul 	struct ifnet		*ifp;
2188ed510fb0SBill Paul 	u_int16_t		status;
2189ed510fb0SBill Paul 	int			rval = 0;
2190ed510fb0SBill Paul 
2191ed510fb0SBill Paul 	sc = arg;
2192ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2193a94100faSBill Paul 
2194a94100faSBill Paul 	RL_LOCK(sc);
219597b9d4baSJohn-Mark Gurney 
2196a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2197a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2198a94100faSBill Paul 
2199d65abd66SPyun YongHyeon 	if (sc->suspended ||
2200d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2201ed510fb0SBill Paul 		RL_UNLOCK(sc);
2202ed510fb0SBill Paul 		return;
2203ed510fb0SBill Paul 	}
2204a94100faSBill Paul 
2205ed510fb0SBill Paul #ifdef DEVICE_POLLING
2206ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2207ed510fb0SBill Paul 		RL_UNLOCK(sc);
2208ed510fb0SBill Paul 		return;
2209ed510fb0SBill Paul 	}
2210ed510fb0SBill Paul #endif
2211a94100faSBill Paul 
2212ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
22131abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2214ed510fb0SBill Paul 
2215818951afSPyun YongHyeon 	/*
2216818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2217818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2218818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2219818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2220818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2221818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2222818951afSPyun YongHyeon 	 */
2223818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2224818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2225818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
22263d85c23dSPyun YongHyeon 	if (status & (
2227ed510fb0SBill Paul #ifdef RE_TX_MODERATION
22283d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2229ed510fb0SBill Paul #else
22303d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2231ed510fb0SBill Paul #endif
2232ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2233a94100faSBill Paul 		re_txeof(sc);
2234a94100faSBill Paul 
22358476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
22368476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
223797b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
22388476c243SPyun YongHyeon 	}
2239a94100faSBill Paul 
224052732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2241ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2242a94100faSBill Paul 
2243a94100faSBill Paul 	RL_UNLOCK(sc);
2244ed510fb0SBill Paul 
2245ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2246ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2247ed510fb0SBill Paul 		return;
2248ed510fb0SBill Paul 	}
2249ed510fb0SBill Paul 
2250ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2251a94100faSBill Paul }
2252a94100faSBill Paul 
2253d65abd66SPyun YongHyeon static int
22547b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2255d65abd66SPyun YongHyeon {
2256d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2257d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2258d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2259d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2260d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2261d65abd66SPyun YongHyeon 	int			nsegs, prod;
2262d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2263d65abd66SPyun YongHyeon 	int			padlen;
2264ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2265a94100faSBill Paul 
2266d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2267738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
22680fc4974fSBill Paul 
22690fc4974fSBill Paul 	/*
22700fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
22710fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
22720fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
22730fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
22740fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
22750fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
227699c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
22770fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2278d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
22790fc4974fSBill Paul 	 */
2280f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2281deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
228299c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2283d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2284d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2285d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2286d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2287d65abd66SPyun YongHyeon 			m_freem(*m_head);
2288d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2289d65abd66SPyun YongHyeon 				*m_head = NULL;
2290a94100faSBill Paul 				return (ENOBUFS);
2291a94100faSBill Paul 			}
2292d65abd66SPyun YongHyeon 			*m_head = m_new;
2293d65abd66SPyun YongHyeon 		}
2294d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2295d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
229680a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2297b4b95879SMarius Strobl 			if (m_new == NULL) {
2298b4b95879SMarius Strobl 				m_freem(*m_head);
2299b4b95879SMarius Strobl 				*m_head = NULL;
230080a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2301b4b95879SMarius Strobl 			}
2302d65abd66SPyun YongHyeon 		} else
2303d65abd66SPyun YongHyeon 			m_new = *m_head;
2304a94100faSBill Paul 
23050fc4974fSBill Paul 		/*
23060fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
23070fc4974fSBill Paul 		 * to avoid leaking data.
23080fc4974fSBill Paul 		 */
2309d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2310d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
23110fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2312d65abd66SPyun YongHyeon 		*m_head = m_new;
23130fc4974fSBill Paul 	}
23140fc4974fSBill Paul 
2315d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2316d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2317d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2318d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2319d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2320304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2321d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2322d65abd66SPyun YongHyeon 			m_freem(*m_head);
2323b4b95879SMarius Strobl 			*m_head = NULL;
2324d65abd66SPyun YongHyeon 			return (ENOBUFS);
2325a94100faSBill Paul 		}
2326d65abd66SPyun YongHyeon 		*m_head = m_new;
2327d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2328d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2329d65abd66SPyun YongHyeon 		if (error != 0) {
2330d65abd66SPyun YongHyeon 			m_freem(*m_head);
2331d65abd66SPyun YongHyeon 			*m_head = NULL;
2332d65abd66SPyun YongHyeon 			return (error);
2333a94100faSBill Paul 		}
2334d65abd66SPyun YongHyeon 	} else if (error != 0)
2335d65abd66SPyun YongHyeon 		return (error);
2336d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2337d65abd66SPyun YongHyeon 		m_freem(*m_head);
2338d65abd66SPyun YongHyeon 		*m_head = NULL;
2339d65abd66SPyun YongHyeon 		return (EIO);
2340d65abd66SPyun YongHyeon 	}
2341d65abd66SPyun YongHyeon 
2342d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2343d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2344d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2345d65abd66SPyun YongHyeon 		return (ENOBUFS);
2346d65abd66SPyun YongHyeon 	}
2347d65abd66SPyun YongHyeon 
2348d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2349d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2350a94100faSBill Paul 
2351a94100faSBill Paul 	/*
2352d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2353d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2354d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2355d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2356a94100faSBill Paul 	 */
2357deb5c680SPyun YongHyeon 	vlanctl = 0;
2358d65abd66SPyun YongHyeon 	csum_flags = 0;
2359d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2360d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2361d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2362d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2363d65abd66SPyun YongHyeon 	else {
236499c8ae87SPyun YongHyeon 		/*
236599c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
236699c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
236799c8ae87SPyun YongHyeon 		 * does't make effects.
236899c8ae87SPyun YongHyeon 		 */
236999c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2370deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2371d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2372deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2373deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2374d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2375deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2376deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2377d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2378deb5c680SPyun YongHyeon 			} else {
2379deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2380deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2381deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2382deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2383deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2384deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2385deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2386deb5c680SPyun YongHyeon 			}
2387d65abd66SPyun YongHyeon 		}
238899c8ae87SPyun YongHyeon 	}
2389a94100faSBill Paul 
2390ccf34c81SPyun YongHyeon 	/*
2391ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2392ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2393ccf34c81SPyun YongHyeon 	 * transmission attempt.
2394ccf34c81SPyun YongHyeon 	 */
2395ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2396bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2397deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2398ccf34c81SPyun YongHyeon 
2399d65abd66SPyun YongHyeon 	si = prod;
2400d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2401d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2402deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2403d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2404d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2405d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2406d65abd66SPyun YongHyeon 		if (i != 0)
2407d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2408d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2409d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2410d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2411d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2412d65abd66SPyun YongHyeon 	}
2413d65abd66SPyun YongHyeon 	/* Update producer index. */
2414d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2415a94100faSBill Paul 
2416d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2417d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2418d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2419d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2420d65abd66SPyun YongHyeon 
2421d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2422d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2423d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2424a94100faSBill Paul 
2425d65abd66SPyun YongHyeon 	/*
2426d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2427d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2428d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2429d65abd66SPyun YongHyeon 	 */
2430d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2431d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2432d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2433d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2434d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2435a94100faSBill Paul 
2436a94100faSBill Paul 	return (0);
2437a94100faSBill Paul }
2438a94100faSBill Paul 
243997b9d4baSJohn-Mark Gurney static void
24407b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
244197b9d4baSJohn-Mark Gurney {
2442ed510fb0SBill Paul 	struct ifnet		*ifp;
244397b9d4baSJohn-Mark Gurney 
2444ed510fb0SBill Paul 	ifp = arg;
2445ed510fb0SBill Paul 	re_start(ifp);
244697b9d4baSJohn-Mark Gurney }
244797b9d4baSJohn-Mark Gurney 
2448a94100faSBill Paul /*
2449a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2450a94100faSBill Paul  */
2451a94100faSBill Paul static void
24527b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2453a94100faSBill Paul {
2454a94100faSBill Paul 	struct rl_softc		*sc;
2455d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2456d65abd66SPyun YongHyeon 	int			queued;
2457a94100faSBill Paul 
2458a94100faSBill Paul 	sc = ifp->if_softc;
245997b9d4baSJohn-Mark Gurney 
2460ed510fb0SBill Paul 	RL_LOCK(sc);
2461ed510fb0SBill Paul 
2462d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2463351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2464ed510fb0SBill Paul 		RL_UNLOCK(sc);
2465ed510fb0SBill Paul 		return;
2466ed510fb0SBill Paul 	}
2467a94100faSBill Paul 
2468d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2469d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
247052732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2471a94100faSBill Paul 		if (m_head == NULL)
2472a94100faSBill Paul 			break;
2473a94100faSBill Paul 
2474d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2475b4b95879SMarius Strobl 			if (m_head == NULL)
2476b4b95879SMarius Strobl 				break;
247752732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
247813f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2479a94100faSBill Paul 			break;
2480a94100faSBill Paul 		}
2481a94100faSBill Paul 
2482a94100faSBill Paul 		/*
2483a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2484a94100faSBill Paul 		 * to him.
2485a94100faSBill Paul 		 */
248659a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
248752732175SMax Laier 
248852732175SMax Laier 		queued++;
2489a94100faSBill Paul 	}
2490a94100faSBill Paul 
2491ed510fb0SBill Paul 	if (queued == 0) {
2492ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2493d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2494ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2495ed510fb0SBill Paul #endif
2496ed510fb0SBill Paul 		RL_UNLOCK(sc);
249752732175SMax Laier 		return;
2498ed510fb0SBill Paul 	}
249952732175SMax Laier 
2500a94100faSBill Paul 	/* Flush the TX descriptors */
2501a94100faSBill Paul 
2502a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2503a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2504a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2505a94100faSBill Paul 
25060fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2507a94100faSBill Paul 
2508ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2509a94100faSBill Paul 	/*
2510a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2511a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2512a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2513a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2514a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2515a94100faSBill Paul 	 * the timer count is reset to 0.
2516a94100faSBill Paul 	 */
2517a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2518ed510fb0SBill Paul #endif
2519a94100faSBill Paul 
2520a94100faSBill Paul 	/*
2521a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2522a94100faSBill Paul 	 */
25231d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2524ed510fb0SBill Paul 
2525ed510fb0SBill Paul 	RL_UNLOCK(sc);
2526a94100faSBill Paul }
2527a94100faSBill Paul 
2528a94100faSBill Paul static void
25297b5ffebfSPyun YongHyeon re_init(void *xsc)
2530a94100faSBill Paul {
2531a94100faSBill Paul 	struct rl_softc		*sc = xsc;
253297b9d4baSJohn-Mark Gurney 
253397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
253497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
253597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
253697b9d4baSJohn-Mark Gurney }
253797b9d4baSJohn-Mark Gurney 
253897b9d4baSJohn-Mark Gurney static void
25397b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
254097b9d4baSJohn-Mark Gurney {
2541fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2542a94100faSBill Paul 	struct mii_data		*mii;
2543566ca8caSJung-uk Kim 	uint32_t		reg;
254470acaecfSPyun YongHyeon 	uint16_t		cfg;
25454d3d7085SBernd Walter 	union {
25464d3d7085SBernd Walter 		uint32_t align_dummy;
25474d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
25484d3d7085SBernd Walter         } eaddr;
2549a94100faSBill Paul 
255097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
255197b9d4baSJohn-Mark Gurney 
2552a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2553a94100faSBill Paul 
25548476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
25558476c243SPyun YongHyeon 		return;
25568476c243SPyun YongHyeon 
2557a94100faSBill Paul 	/*
2558a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2559a94100faSBill Paul 	 */
2560a94100faSBill Paul 	re_stop(sc);
2561a94100faSBill Paul 
2562b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2563b659f1f0SPyun YongHyeon 	re_reset(sc);
2564b659f1f0SPyun YongHyeon 
2565a94100faSBill Paul 	/*
2566c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2567edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2568c2c6548bSBill Paul 	 * before all others.
2569c2c6548bSBill Paul 	 */
257070acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
257170acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
257270acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
257370acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
257470acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2575deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2576deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2577deb5c680SPyun YongHyeon 		/* XXX magic. */
2578deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2579deb5c680SPyun YongHyeon 	} else
2580deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2581deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
2582566ca8caSJung-uk Kim 	if (sc->rl_hwrev == RL_HWREV_8169_8110SC ||
2583566ca8caSJung-uk Kim 	    sc->rl_hwrev == RL_HWREV_8169_8110SCE) {
2584566ca8caSJung-uk Kim 		reg = 0x000fff00;
2585566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
2586566ca8caSJung-uk Kim 			reg |= 0x000000ff;
2587566ca8caSJung-uk Kim 		if (sc->rl_hwrev == RL_HWREV_8169_8110SCE)
2588566ca8caSJung-uk Kim 			reg |= 0x00f00000;
2589566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
2590566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
2591566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
2592566ca8caSJung-uk Kim 	}
2593ae644087SPyun YongHyeon 	/*
2594ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2595ae644087SPyun YongHyeon 	 * allowed in controller.
2596ae644087SPyun YongHyeon 	 */
2597ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2598ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2599ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2600ae644087SPyun YongHyeon 	}
2601c2c6548bSBill Paul 
2602c2c6548bSBill Paul 	/*
2603a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2604a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2605a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2606a94100faSBill Paul 	 */
26074d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
26084d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2609a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2610ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2611ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2612ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2613ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2614a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2615a94100faSBill Paul 
2616a94100faSBill Paul 	/*
2617a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2618a94100faSBill Paul 	 */
2619a94100faSBill Paul 	re_rx_list_init(sc);
2620a94100faSBill Paul 	re_tx_list_init(sc);
2621a94100faSBill Paul 
2622a94100faSBill Paul 	/*
2623d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2624d01fac16SPyun YongHyeon 	 */
2625d01fac16SPyun YongHyeon 
2626d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2627d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2628d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2629d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2630d01fac16SPyun YongHyeon 
2631d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2632d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2633d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2634d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2635d01fac16SPyun YongHyeon 
2636d01fac16SPyun YongHyeon 	/*
2637a94100faSBill Paul 	 * Enable transmit and receive.
2638a94100faSBill Paul 	 */
2639a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2640a94100faSBill Paul 
2641a94100faSBill Paul 	/*
2642ff191365SJung-uk Kim 	 * Set the initial TX configuration.
2643a94100faSBill Paul 	 */
2644abc8ff44SBill Paul 	if (sc->rl_testmode) {
2645abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2646abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2647abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2648a94100faSBill Paul 		else
2649abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2650abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2651abc8ff44SBill Paul 	} else
2652a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2653d01fac16SPyun YongHyeon 
2654d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2655d01fac16SPyun YongHyeon 
2656a94100faSBill Paul 	/*
2657ff191365SJung-uk Kim 	 * Set the initial RX configuration.
2658a94100faSBill Paul 	 */
2659ff191365SJung-uk Kim 	re_set_rxmode(sc);
2660a94100faSBill Paul 
2661a94100faSBill Paul #ifdef DEVICE_POLLING
2662a94100faSBill Paul 	/*
2663a94100faSBill Paul 	 * Disable interrupts if we are polling.
2664a94100faSBill Paul 	 */
266540929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2666a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2667a94100faSBill Paul 	else	/* otherwise ... */
266840929967SGleb Smirnoff #endif
2669ed510fb0SBill Paul 
2670a94100faSBill Paul 	/*
2671a94100faSBill Paul 	 * Enable interrupts.
2672a94100faSBill Paul 	 */
2673a94100faSBill Paul 	if (sc->rl_testmode)
2674a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2675a94100faSBill Paul 	else
2676a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2677ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2678a94100faSBill Paul 
2679a94100faSBill Paul 	/* Set initial TX threshold */
2680a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2681a94100faSBill Paul 
2682a94100faSBill Paul 	/* Start RX/TX process. */
2683a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2684a94100faSBill Paul #ifdef notdef
2685a94100faSBill Paul 	/* Enable receiver and transmitter. */
2686a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2687a94100faSBill Paul #endif
2688a94100faSBill Paul 
2689ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2690a94100faSBill Paul 	/*
2691a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2692a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2693a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2694a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2695a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2696a94100faSBill Paul 	 */
2697a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2698a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2699a94100faSBill Paul 	else
2700a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2701ed510fb0SBill Paul #endif
2702a94100faSBill Paul 
2703a94100faSBill Paul 	/*
2704a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2705a94100faSBill Paul 	 * size so we can receive jumbo frames.
2706a94100faSBill Paul 	 */
2707a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2708a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2709a94100faSBill Paul 
271097b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2711a94100faSBill Paul 		return;
2712a94100faSBill Paul 
2713a94100faSBill Paul 	mii_mediachg(mii);
2714a94100faSBill Paul 
271519ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2716a94100faSBill Paul 
271713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
271813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2719a94100faSBill Paul 
2720351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
27211d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2722d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2723a94100faSBill Paul }
2724a94100faSBill Paul 
2725a94100faSBill Paul /*
2726a94100faSBill Paul  * Set media options.
2727a94100faSBill Paul  */
2728a94100faSBill Paul static int
27297b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
2730a94100faSBill Paul {
2731a94100faSBill Paul 	struct rl_softc		*sc;
2732a94100faSBill Paul 	struct mii_data		*mii;
27336f0f9b12SPyun YongHyeon 	int			error;
2734a94100faSBill Paul 
2735a94100faSBill Paul 	sc = ifp->if_softc;
2736a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2737d1754a9bSJohn Baldwin 	RL_LOCK(sc);
27386f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
2739d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2740a94100faSBill Paul 
27416f0f9b12SPyun YongHyeon 	return (error);
2742a94100faSBill Paul }
2743a94100faSBill Paul 
2744a94100faSBill Paul /*
2745a94100faSBill Paul  * Report current media status.
2746a94100faSBill Paul  */
2747a94100faSBill Paul static void
27487b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2749a94100faSBill Paul {
2750a94100faSBill Paul 	struct rl_softc		*sc;
2751a94100faSBill Paul 	struct mii_data		*mii;
2752a94100faSBill Paul 
2753a94100faSBill Paul 	sc = ifp->if_softc;
2754a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2755a94100faSBill Paul 
2756d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2757a94100faSBill Paul 	mii_pollstat(mii);
2758d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2759a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2760a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2761a94100faSBill Paul }
2762a94100faSBill Paul 
2763a94100faSBill Paul static int
27647b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2765a94100faSBill Paul {
2766a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2767a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2768a94100faSBill Paul 	struct mii_data		*mii;
276940929967SGleb Smirnoff 	int			error = 0;
2770a94100faSBill Paul 
2771a94100faSBill Paul 	switch (command) {
2772a94100faSBill Paul 	case SIOCSIFMTU:
2773c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2774a94100faSBill Paul 			error = EINVAL;
2775c1d0b573SPyun YongHyeon 			break;
2776c1d0b573SPyun YongHyeon 		}
2777351a76f9SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 &&
2778c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2779c1d0b573SPyun YongHyeon 			error = EINVAL;
2780c1d0b573SPyun YongHyeon 			break;
2781c1d0b573SPyun YongHyeon 		}
2782c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2783c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2784a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2785ae644087SPyun YongHyeon 		if (ifp->if_mtu > RL_TSO_MTU &&
2786ae644087SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2787ae644087SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_TSO4;
2788ae644087SPyun YongHyeon 			ifp->if_hwassist &= ~CSUM_TSO;
2789ae644087SPyun YongHyeon 		}
2790d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2791a94100faSBill Paul 		break;
2792a94100faSBill Paul 	case SIOCSIFFLAGS:
279397b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2794eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2795eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2796eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
27973021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2798ff191365SJung-uk Kim 					re_set_rxmode(sc);
2799eed497bbSPyun YongHyeon 			} else
280097b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2801eed497bbSPyun YongHyeon 		} else {
2802eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2803a94100faSBill Paul 				re_stop(sc);
2804eed497bbSPyun YongHyeon 		}
2805eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
280697b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2807a94100faSBill Paul 		break;
2808a94100faSBill Paul 	case SIOCADDMULTI:
2809a94100faSBill Paul 	case SIOCDELMULTI:
281097b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
28118476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2812ff191365SJung-uk Kim 			re_set_rxmode(sc);
281397b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2814a94100faSBill Paul 		break;
2815a94100faSBill Paul 	case SIOCGIFMEDIA:
2816a94100faSBill Paul 	case SIOCSIFMEDIA:
2817a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2818a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2819a94100faSBill Paul 		break;
2820a94100faSBill Paul 	case SIOCSIFCAP:
282140929967SGleb Smirnoff 	    {
2822f051cb85SGleb Smirnoff 		int mask, reinit;
2823f051cb85SGleb Smirnoff 
2824f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2825f051cb85SGleb Smirnoff 		reinit = 0;
282640929967SGleb Smirnoff #ifdef DEVICE_POLLING
282740929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
282840929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
282940929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
283040929967SGleb Smirnoff 				if (error)
283140929967SGleb Smirnoff 					return(error);
2832d1754a9bSJohn Baldwin 				RL_LOCK(sc);
283340929967SGleb Smirnoff 				/* Disable interrupts */
283440929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
283540929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
283640929967SGleb Smirnoff 				RL_UNLOCK(sc);
283740929967SGleb Smirnoff 			} else {
283840929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
283940929967SGleb Smirnoff 				/* Enable interrupts. */
284040929967SGleb Smirnoff 				RL_LOCK(sc);
284140929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
284240929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
284340929967SGleb Smirnoff 				RL_UNLOCK(sc);
284440929967SGleb Smirnoff 			}
284540929967SGleb Smirnoff 		}
284640929967SGleb Smirnoff #endif /* DEVICE_POLLING */
284740929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2848f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2849a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2850dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2851a94100faSBill Paul 			else
2852b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2853f051cb85SGleb Smirnoff 			reinit = 1;
285440929967SGleb Smirnoff 		}
2855f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2856f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2857f051cb85SGleb Smirnoff 			reinit = 1;
2858f051cb85SGleb Smirnoff 		}
2859dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2860dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2861dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2862dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2863dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2864dc74159dSPyun YongHyeon 			else
2865dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2866ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
2867ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2868ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
2869ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2870ae644087SPyun YongHyeon 			}
2871dc74159dSPyun YongHyeon 		}
28727467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
28737467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
28747467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
28757467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
28767467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
28777467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
28787467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
28797467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
28807467bd53SPyun YongHyeon 		}
28818476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
28828476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2883f051cb85SGleb Smirnoff 			re_init(sc);
28848476c243SPyun YongHyeon 		}
2885960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
288640929967SGleb Smirnoff 	    }
2887a94100faSBill Paul 		break;
2888a94100faSBill Paul 	default:
2889a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2890a94100faSBill Paul 		break;
2891a94100faSBill Paul 	}
2892a94100faSBill Paul 
2893a94100faSBill Paul 	return (error);
2894a94100faSBill Paul }
2895a94100faSBill Paul 
2896a94100faSBill Paul static void
28977b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
28981d545c7aSMarius Strobl {
2899130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
2900a94100faSBill Paul 
29011d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
29021d545c7aSMarius Strobl 
29031d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
29041d545c7aSMarius Strobl 		return;
29051d545c7aSMarius Strobl 
2906130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
2907a94100faSBill Paul 	re_txeof(sc);
2908130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
2909130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
2910130b6dfbSPyun YongHyeon 		    "-- recovering\n");
2911130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2912130b6dfbSPyun YongHyeon 			taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2913130b6dfbSPyun YongHyeon 		return;
2914130b6dfbSPyun YongHyeon 	}
2915130b6dfbSPyun YongHyeon 
2916130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
2917130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
2918130b6dfbSPyun YongHyeon 
29191abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
29208476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
292197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2922130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2923130b6dfbSPyun YongHyeon 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2924a94100faSBill Paul }
2925a94100faSBill Paul 
2926a94100faSBill Paul /*
2927a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2928a94100faSBill Paul  * RX and TX lists.
2929a94100faSBill Paul  */
2930a94100faSBill Paul static void
29317b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
2932a94100faSBill Paul {
29330ce0868aSPyun YongHyeon 	int			i;
2934a94100faSBill Paul 	struct ifnet		*ifp;
2935d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2936d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2937a94100faSBill Paul 
293897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
293997b9d4baSJohn-Mark Gurney 
2940fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2941a94100faSBill Paul 
29421d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2943d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
294413f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2945a94100faSBill Paul 
2946ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
2947ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
2948ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
2949ead8fc66SPyun YongHyeon 	else
2950a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2951ead8fc66SPyun YongHyeon 	DELAY(1000);
2952a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2953ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2954a94100faSBill Paul 
2955a94100faSBill Paul 	if (sc->rl_head != NULL) {
2956a94100faSBill Paul 		m_freem(sc->rl_head);
2957a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2958a94100faSBill Paul 	}
2959a94100faSBill Paul 
2960a94100faSBill Paul 	/* Free the TX list buffers. */
2961a94100faSBill Paul 
2962d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2963d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2964d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2965d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2966d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2967d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2968d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2969d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2970d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2971a94100faSBill Paul 		}
2972a94100faSBill Paul 	}
2973a94100faSBill Paul 
2974a94100faSBill Paul 	/* Free the RX list buffers. */
2975a94100faSBill Paul 
2976d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2977d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2978d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2979d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2980d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2981d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2982d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2983d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2984d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2985a94100faSBill Paul 		}
2986a94100faSBill Paul 	}
2987a94100faSBill Paul }
2988a94100faSBill Paul 
2989a94100faSBill Paul /*
2990a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2991a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2992a94100faSBill Paul  * resume.
2993a94100faSBill Paul  */
2994a94100faSBill Paul static int
29957b5ffebfSPyun YongHyeon re_suspend(device_t dev)
2996a94100faSBill Paul {
2997a94100faSBill Paul 	struct rl_softc		*sc;
2998a94100faSBill Paul 
2999a94100faSBill Paul 	sc = device_get_softc(dev);
3000a94100faSBill Paul 
300197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3002a94100faSBill Paul 	re_stop(sc);
30037467bd53SPyun YongHyeon 	re_setwol(sc);
3004a94100faSBill Paul 	sc->suspended = 1;
300597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3006a94100faSBill Paul 
3007a94100faSBill Paul 	return (0);
3008a94100faSBill Paul }
3009a94100faSBill Paul 
3010a94100faSBill Paul /*
3011a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3012a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3013a94100faSBill Paul  * appropriate.
3014a94100faSBill Paul  */
3015a94100faSBill Paul static int
30167b5ffebfSPyun YongHyeon re_resume(device_t dev)
3017a94100faSBill Paul {
3018a94100faSBill Paul 	struct rl_softc		*sc;
3019a94100faSBill Paul 	struct ifnet		*ifp;
3020a94100faSBill Paul 
3021a94100faSBill Paul 	sc = device_get_softc(dev);
302297b9d4baSJohn-Mark Gurney 
302397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
302497b9d4baSJohn-Mark Gurney 
3025fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
302661f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
302761f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
302861f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
302961f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
303061f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
303161f45a72SPyun YongHyeon 	}
3032a94100faSBill Paul 
30337467bd53SPyun YongHyeon 	/*
30347467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
30357467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
30367467bd53SPyun YongHyeon 	 */
30377467bd53SPyun YongHyeon 	re_clrwol(sc);
303801d1a6c3SPyun YongHyeon 
303901d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
304001d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
304101d1a6c3SPyun YongHyeon 		re_init_locked(sc);
304201d1a6c3SPyun YongHyeon 
3043a94100faSBill Paul 	sc->suspended = 0;
304497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3045a94100faSBill Paul 
3046a94100faSBill Paul 	return (0);
3047a94100faSBill Paul }
3048a94100faSBill Paul 
3049a94100faSBill Paul /*
3050a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3051a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3052a94100faSBill Paul  */
30536a087a87SPyun YongHyeon static int
30547b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3055a94100faSBill Paul {
3056a94100faSBill Paul 	struct rl_softc		*sc;
3057a94100faSBill Paul 
3058a94100faSBill Paul 	sc = device_get_softc(dev);
3059a94100faSBill Paul 
306097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3061a94100faSBill Paul 	re_stop(sc);
3062536fde34SMaxim Sobolev 	/*
3063536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3064536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
306572293673SRuslan Ermilov 	 * cases.
3066536fde34SMaxim Sobolev 	 */
3067536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
30687467bd53SPyun YongHyeon 	re_setwol(sc);
306997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
30706a087a87SPyun YongHyeon 
30716a087a87SPyun YongHyeon 	return (0);
3072a94100faSBill Paul }
30737467bd53SPyun YongHyeon 
30747467bd53SPyun YongHyeon static void
30757b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
30767467bd53SPyun YongHyeon {
30777467bd53SPyun YongHyeon 	struct ifnet		*ifp;
30787467bd53SPyun YongHyeon 	int			pmc;
30797467bd53SPyun YongHyeon 	uint16_t		pmstat;
30807467bd53SPyun YongHyeon 	uint8_t			v;
30817467bd53SPyun YongHyeon 
30827467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30837467bd53SPyun YongHyeon 
30847467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30857467bd53SPyun YongHyeon 		return;
30867467bd53SPyun YongHyeon 
30877467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
308861f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
308961f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
309061f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
309161f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
309261f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
309361f45a72SPyun YongHyeon 	}
3094886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3095886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3096886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
30977467bd53SPyun YongHyeon 	/* Enable config register write. */
30987467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30997467bd53SPyun YongHyeon 
31007467bd53SPyun YongHyeon 	/* Enable PME. */
31017467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
31027467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
31037467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
31047467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
31057467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
31067467bd53SPyun YongHyeon 
31077467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
31087467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
31097467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
31107467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
31117467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
31127467bd53SPyun YongHyeon 
31137467bd53SPyun YongHyeon 	/* Config register write done. */
3114f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
31157467bd53SPyun YongHyeon 
31167467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
31177467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
31187467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
31197467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
31207467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
31217467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
31227467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
31237467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
31247467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
31257467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
31267467bd53SPyun YongHyeon 
31277467bd53SPyun YongHyeon 	/*
31287467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
31297467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
31307467bd53SPyun YongHyeon 	 * needed.
31317467bd53SPyun YongHyeon 	 */
31327467bd53SPyun YongHyeon 
31337467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
31347467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
31357467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
31367467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
31377467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
31387467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
31397467bd53SPyun YongHyeon }
31407467bd53SPyun YongHyeon 
31417467bd53SPyun YongHyeon static void
31427b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
31437467bd53SPyun YongHyeon {
31447467bd53SPyun YongHyeon 	int			pmc;
31457467bd53SPyun YongHyeon 	uint8_t			v;
31467467bd53SPyun YongHyeon 
31477467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
31487467bd53SPyun YongHyeon 
31497467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
31507467bd53SPyun YongHyeon 		return;
31517467bd53SPyun YongHyeon 
31527467bd53SPyun YongHyeon 	/* Enable config register write. */
31537467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
31547467bd53SPyun YongHyeon 
31557467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
31567467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
31577467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
31587467bd53SPyun YongHyeon 
31597467bd53SPyun YongHyeon 	/* Config register write done. */
3160f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
31617467bd53SPyun YongHyeon 
31627467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
31637467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
31647467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
31657467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
31667467bd53SPyun YongHyeon }
3167