xref: /freebsd/sys/dev/re/if_re.c (revision 1abcdbd127e95ffe08399ec75a7001edd1ca2f5f)
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,
175b1d62f0fSPyun YongHyeon 	    "RealTek 8101E/8102E/8102EL PCIe 10/100baseTX" },
1769dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
17759ef640dSPyun YongHyeon 	    "RealTek 8168/8168B/8168C/8168CP/8168D/8111B/8111C/8111CP PCIe "
178deb5c680SPyun YongHyeon 	    "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"},
214498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2151acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
216deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C"},
217deb5c680SPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C"},
218deb5c680SPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP"},
21959ef640dSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D"},
220a94100faSBill Paul 	{ 0, 0, NULL }
221a94100faSBill Paul };
222a94100faSBill Paul 
223a94100faSBill Paul static int re_probe		(device_t);
224a94100faSBill Paul static int re_attach		(device_t);
225a94100faSBill Paul static int re_detach		(device_t);
226a94100faSBill Paul 
227d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
228a94100faSBill Paul 
229a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
230a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
231d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
232d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
233d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
234a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
235a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23622a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23722a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23822a11c96SJohn-Mark Gurney 				(struct mbuf *);
23922a11c96SJohn-Mark Gurney #endif
2401abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
241a94100faSBill Paul static void re_txeof		(struct rl_softc *);
24297b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2431abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2441abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
24597b9d4baSJohn-Mark Gurney #endif
246ef544f63SPaolo Pisati static int re_intr		(void *);
247a94100faSBill Paul static void re_tick		(void *);
248ed510fb0SBill Paul static void re_tx_task		(void *, int);
249ed510fb0SBill Paul static void re_int_task		(void *, int);
250a94100faSBill Paul static void re_start		(struct ifnet *);
251a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
252a94100faSBill Paul static void re_init		(void *);
25397b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
254a94100faSBill Paul static void re_stop		(struct rl_softc *);
2551d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
256a94100faSBill Paul static int re_suspend		(device_t);
257a94100faSBill Paul static int re_resume		(device_t);
2586a087a87SPyun YongHyeon static int re_shutdown		(device_t);
259a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
260a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
261a94100faSBill Paul 
262a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
263a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
264ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
265a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
266a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
267a94100faSBill Paul 
268a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
269a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
270a94100faSBill Paul static void re_miibus_statchg	(device_t);
271a94100faSBill Paul 
272ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
273a94100faSBill Paul static void re_reset		(struct rl_softc *);
2747467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2757467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
276a94100faSBill Paul 
277ed510fb0SBill Paul #ifdef RE_DIAG
278a94100faSBill Paul static int re_diag		(struct rl_softc *);
279ed510fb0SBill Paul #endif
280a94100faSBill Paul 
281a94100faSBill Paul static device_method_t re_methods[] = {
282a94100faSBill Paul 	/* Device interface */
283a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
284a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
285a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
286a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
287a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
288a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
289a94100faSBill Paul 
290a94100faSBill Paul 	/* bus interface */
291a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
292a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
293a94100faSBill Paul 
294a94100faSBill Paul 	/* MII interface */
295a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
296a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
297a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
298a94100faSBill Paul 
299a94100faSBill Paul 	{ 0, 0 }
300a94100faSBill Paul };
301a94100faSBill Paul 
302a94100faSBill Paul static driver_t re_driver = {
303a94100faSBill Paul 	"re",
304a94100faSBill Paul 	re_methods,
305a94100faSBill Paul 	sizeof(struct rl_softc)
306a94100faSBill Paul };
307a94100faSBill Paul 
308a94100faSBill Paul static devclass_t re_devclass;
309a94100faSBill Paul 
310a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
311a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
312a94100faSBill Paul 
313a94100faSBill Paul #define EE_SET(x)					\
314a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
315a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
316a94100faSBill Paul 
317a94100faSBill Paul #define EE_CLR(x)					\
318a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
319a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
320a94100faSBill Paul 
321a94100faSBill Paul /*
322a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
323a94100faSBill Paul  */
324a94100faSBill Paul static void
3257b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
326a94100faSBill Paul {
3270ce0868aSPyun YongHyeon 	int			d, i;
328a94100faSBill Paul 
329ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
330a94100faSBill Paul 
331a94100faSBill Paul 	/*
332a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
333a94100faSBill Paul 	 */
334ed510fb0SBill Paul 
335ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
336a94100faSBill Paul 		if (d & i) {
337a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
338a94100faSBill Paul 		} else {
339a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
340a94100faSBill Paul 		}
341a94100faSBill Paul 		DELAY(100);
342a94100faSBill Paul 		EE_SET(RL_EE_CLK);
343a94100faSBill Paul 		DELAY(150);
344a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
345a94100faSBill Paul 		DELAY(100);
346a94100faSBill Paul 	}
347a94100faSBill Paul }
348a94100faSBill Paul 
349a94100faSBill Paul /*
350a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
351a94100faSBill Paul  */
352a94100faSBill Paul static void
3537b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
354a94100faSBill Paul {
3550ce0868aSPyun YongHyeon 	int			i;
356a94100faSBill Paul 	u_int16_t		word = 0;
357a94100faSBill Paul 
358a94100faSBill Paul 	/*
359a94100faSBill Paul 	 * Send address of word we want to read.
360a94100faSBill Paul 	 */
361a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
362a94100faSBill Paul 
363a94100faSBill Paul 	/*
364a94100faSBill Paul 	 * Start reading bits from EEPROM.
365a94100faSBill Paul 	 */
366a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
367a94100faSBill Paul 		EE_SET(RL_EE_CLK);
368a94100faSBill Paul 		DELAY(100);
369a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
370a94100faSBill Paul 			word |= i;
371a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
372a94100faSBill Paul 		DELAY(100);
373a94100faSBill Paul 	}
374a94100faSBill Paul 
375a94100faSBill Paul 	*dest = word;
376a94100faSBill Paul }
377a94100faSBill Paul 
378a94100faSBill Paul /*
379a94100faSBill Paul  * Read a sequence of words from the EEPROM.
380a94100faSBill Paul  */
381a94100faSBill Paul static void
3827b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
383a94100faSBill Paul {
384a94100faSBill Paul 	int			i;
385a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
386a94100faSBill Paul 
387ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
388ed510fb0SBill Paul 
389ed510fb0SBill Paul         DELAY(100);
390ed510fb0SBill Paul 
391a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
392ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
393a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
394ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
395a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
396be099007SPyun YongHyeon                 *ptr = word;
397a94100faSBill Paul 	}
398ed510fb0SBill Paul 
399ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
400a94100faSBill Paul }
401a94100faSBill Paul 
402a94100faSBill Paul static int
4037b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
404a94100faSBill Paul {
405a94100faSBill Paul 	struct rl_softc		*sc;
406a94100faSBill Paul 	u_int32_t		rval;
407a94100faSBill Paul 	int			i;
408a94100faSBill Paul 
409a94100faSBill Paul 	if (phy != 1)
410a94100faSBill Paul 		return (0);
411a94100faSBill Paul 
412a94100faSBill Paul 	sc = device_get_softc(dev);
413a94100faSBill Paul 
4149bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4159bac70b8SBill Paul 
4169bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4179bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4189bac70b8SBill Paul 		return (rval);
4199bac70b8SBill Paul 	}
4209bac70b8SBill Paul 
421a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
422bd9bede5SPyun YongHyeon 	DELAY(1000);
423a94100faSBill Paul 
42496b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
425a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
426a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
427a94100faSBill Paul 			break;
428f68bc089SPyun YongHyeon 		DELAY(100);
429a94100faSBill Paul 	}
430a94100faSBill Paul 
43196b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4326b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
433a94100faSBill Paul 		return (0);
434a94100faSBill Paul 	}
435a94100faSBill Paul 
436a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
437a94100faSBill Paul }
438a94100faSBill Paul 
439a94100faSBill Paul static int
4407b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
441a94100faSBill Paul {
442a94100faSBill Paul 	struct rl_softc		*sc;
443a94100faSBill Paul 	u_int32_t		rval;
444a94100faSBill Paul 	int			i;
445a94100faSBill Paul 
446a94100faSBill Paul 	sc = device_get_softc(dev);
447a94100faSBill Paul 
448a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4499bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
450bd9bede5SPyun YongHyeon 	DELAY(1000);
451a94100faSBill Paul 
45296b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
453a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
454a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
455a94100faSBill Paul 			break;
456f68bc089SPyun YongHyeon 		DELAY(100);
457a94100faSBill Paul 	}
458a94100faSBill Paul 
45996b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4606b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
461a94100faSBill Paul 		return (0);
462a94100faSBill Paul 	}
463a94100faSBill Paul 
464a94100faSBill Paul 	return (0);
465a94100faSBill Paul }
466a94100faSBill Paul 
467a94100faSBill Paul static int
4687b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
469a94100faSBill Paul {
470a94100faSBill Paul 	struct rl_softc		*sc;
471a94100faSBill Paul 	u_int16_t		rval = 0;
472a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
473a94100faSBill Paul 
474a94100faSBill Paul 	sc = device_get_softc(dev);
475a94100faSBill Paul 
476a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
477a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
478a94100faSBill Paul 		return (rval);
479a94100faSBill Paul 	}
480a94100faSBill Paul 
481a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
482a94100faSBill Paul 	if (phy) {
483a94100faSBill Paul 		return (0);
484a94100faSBill Paul 	}
485a94100faSBill Paul 	switch (reg) {
486a94100faSBill Paul 	case MII_BMCR:
487a94100faSBill Paul 		re8139_reg = RL_BMCR;
488a94100faSBill Paul 		break;
489a94100faSBill Paul 	case MII_BMSR:
490a94100faSBill Paul 		re8139_reg = RL_BMSR;
491a94100faSBill Paul 		break;
492a94100faSBill Paul 	case MII_ANAR:
493a94100faSBill Paul 		re8139_reg = RL_ANAR;
494a94100faSBill Paul 		break;
495a94100faSBill Paul 	case MII_ANER:
496a94100faSBill Paul 		re8139_reg = RL_ANER;
497a94100faSBill Paul 		break;
498a94100faSBill Paul 	case MII_ANLPAR:
499a94100faSBill Paul 		re8139_reg = RL_LPAR;
500a94100faSBill Paul 		break;
501a94100faSBill Paul 	case MII_PHYIDR1:
502a94100faSBill Paul 	case MII_PHYIDR2:
503a94100faSBill Paul 		return (0);
504a94100faSBill Paul 	/*
505a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
506a94100faSBill Paul 	 * register. If we have a link partner which does not
507a94100faSBill Paul 	 * support NWAY, this is the register which will tell
508a94100faSBill Paul 	 * us the results of parallel detection.
509a94100faSBill Paul 	 */
510a94100faSBill Paul 	case RL_MEDIASTAT:
511a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
512a94100faSBill Paul 		return (rval);
513a94100faSBill Paul 	default:
5146b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
515a94100faSBill Paul 		return (0);
516a94100faSBill Paul 	}
517a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
518baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
519baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
520baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
521baa12772SPyun YongHyeon 	}
522a94100faSBill Paul 	return (rval);
523a94100faSBill Paul }
524a94100faSBill Paul 
525a94100faSBill Paul static int
5267b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
527a94100faSBill Paul {
528a94100faSBill Paul 	struct rl_softc		*sc;
529a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
530a94100faSBill Paul 	int			rval = 0;
531a94100faSBill Paul 
532a94100faSBill Paul 	sc = device_get_softc(dev);
533a94100faSBill Paul 
534a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
535a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
536a94100faSBill Paul 		return (rval);
537a94100faSBill Paul 	}
538a94100faSBill Paul 
539a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
54097b9d4baSJohn-Mark Gurney 	if (phy)
541a94100faSBill Paul 		return (0);
54297b9d4baSJohn-Mark Gurney 
543a94100faSBill Paul 	switch (reg) {
544a94100faSBill Paul 	case MII_BMCR:
545a94100faSBill Paul 		re8139_reg = RL_BMCR;
546baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
547baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
548baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
549baa12772SPyun YongHyeon 		}
550a94100faSBill Paul 		break;
551a94100faSBill Paul 	case MII_BMSR:
552a94100faSBill Paul 		re8139_reg = RL_BMSR;
553a94100faSBill Paul 		break;
554a94100faSBill Paul 	case MII_ANAR:
555a94100faSBill Paul 		re8139_reg = RL_ANAR;
556a94100faSBill Paul 		break;
557a94100faSBill Paul 	case MII_ANER:
558a94100faSBill Paul 		re8139_reg = RL_ANER;
559a94100faSBill Paul 		break;
560a94100faSBill Paul 	case MII_ANLPAR:
561a94100faSBill Paul 		re8139_reg = RL_LPAR;
562a94100faSBill Paul 		break;
563a94100faSBill Paul 	case MII_PHYIDR1:
564a94100faSBill Paul 	case MII_PHYIDR2:
565a94100faSBill Paul 		return (0);
566a94100faSBill Paul 		break;
567a94100faSBill Paul 	default:
5686b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
569a94100faSBill Paul 		return (0);
570a94100faSBill Paul 	}
571a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
572a94100faSBill Paul 	return (0);
573a94100faSBill Paul }
574a94100faSBill Paul 
575a94100faSBill Paul static void
5767b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
577a94100faSBill Paul {
578130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
579130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
580130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
581a11e2f18SBruce M Simpson 
582130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
583130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
584130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
585130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
586130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
587130b6dfbSPyun YongHyeon 		return;
588130b6dfbSPyun YongHyeon 
589130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
590130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
591130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
592130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
593130b6dfbSPyun YongHyeon 		case IFM_10_T:
594130b6dfbSPyun YongHyeon 		case IFM_100_TX:
595130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
596130b6dfbSPyun YongHyeon 			break;
597130b6dfbSPyun YongHyeon 		case IFM_1000_T:
598130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
599130b6dfbSPyun YongHyeon 				break;
600130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
601130b6dfbSPyun YongHyeon 			break;
602130b6dfbSPyun YongHyeon 		default:
603130b6dfbSPyun YongHyeon 			break;
604130b6dfbSPyun YongHyeon 		}
605130b6dfbSPyun YongHyeon 	}
606130b6dfbSPyun YongHyeon 	/*
607130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
608130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
609130b6dfbSPyun YongHyeon 	 * parameters.
610130b6dfbSPyun YongHyeon 	 */
611a94100faSBill Paul }
612a94100faSBill Paul 
613a94100faSBill Paul /*
614ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
615a94100faSBill Paul  */
616a94100faSBill Paul static void
617ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
618a94100faSBill Paul {
619a94100faSBill Paul 	struct ifnet		*ifp;
620a94100faSBill Paul 	struct ifmultiaddr	*ifma;
621ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
622ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
623a94100faSBill Paul 
62497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
62597b9d4baSJohn-Mark Gurney 
626fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
627a94100faSBill Paul 
628ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
629a94100faSBill Paul 
630ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6317c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6327c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
633a0637caaSPyun YongHyeon 		/*
634a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
635a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
636a0637caaSPyun YongHyeon 		 * promiscuous mode.
637a0637caaSPyun YongHyeon 		 */
638a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
639ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
640ff191365SJung-uk Kim 		goto done;
641a94100faSBill Paul 	}
642a94100faSBill Paul 
64313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
644a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
645a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
646a94100faSBill Paul 			continue;
6470e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6480e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
649a94100faSBill Paul 		if (h < 32)
650a94100faSBill Paul 			hashes[0] |= (1 << h);
651a94100faSBill Paul 		else
652a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
653a94100faSBill Paul 	}
65413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
655a94100faSBill Paul 
656ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
657bb7dfefbSBill Paul 		/*
658ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
659ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
660ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
661ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
662ff191365SJung-uk Kim 		 * devices.
663bb7dfefbSBill Paul 		 */
664aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
665ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
666ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
667ff191365SJung-uk Kim 			hashes[1] = h;
668ff191365SJung-uk Kim 		}
669ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
670ff191365SJung-uk Kim 	}
671ff191365SJung-uk Kim 
672ff191365SJung-uk Kim done:
673a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
674a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
675ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
676bb7dfefbSBill Paul }
677a94100faSBill Paul 
678a94100faSBill Paul static void
6797b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
680a94100faSBill Paul {
6810ce0868aSPyun YongHyeon 	int			i;
682a94100faSBill Paul 
68397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
68497b9d4baSJohn-Mark Gurney 
685a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
686a94100faSBill Paul 
687a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
688a94100faSBill Paul 		DELAY(10);
689a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
690a94100faSBill Paul 			break;
691a94100faSBill Paul 	}
692a94100faSBill Paul 	if (i == RL_TIMEOUT)
6936b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
694a94100faSBill Paul 
695566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
696a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
697566ca8caSJung-uk Kim 	if (sc->rl_hwrev == RL_HWREV_8169S)
698566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
699a94100faSBill Paul }
700a94100faSBill Paul 
701ed510fb0SBill Paul #ifdef RE_DIAG
702ed510fb0SBill Paul 
703a94100faSBill Paul /*
704a94100faSBill Paul  * The following routine is designed to test for a defect on some
705a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
706a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
707a94100faSBill Paul  * should be pulled high. The result of this defect is that the
708a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
709a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
710a94100faSBill Paul  * because the 64-bit data lines aren't connected.
711a94100faSBill Paul  *
712a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
713a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
714a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
715a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
716a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
717a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
718a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
719a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
720a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
721a94100faSBill Paul  */
722a94100faSBill Paul 
723a94100faSBill Paul static int
7247b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
725a94100faSBill Paul {
726fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
727a94100faSBill Paul 	struct mbuf		*m0;
728a94100faSBill Paul 	struct ether_header	*eh;
729a94100faSBill Paul 	struct rl_desc		*cur_rx;
730a94100faSBill Paul 	u_int16_t		status;
731a94100faSBill Paul 	u_int32_t		rxstat;
732ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
733a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
734a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
735a94100faSBill Paul 
736a94100faSBill Paul 	/* Allocate a single mbuf */
737a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
738a94100faSBill Paul 	if (m0 == NULL)
739a94100faSBill Paul 		return (ENOBUFS);
740a94100faSBill Paul 
74197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
74297b9d4baSJohn-Mark Gurney 
743a94100faSBill Paul 	/*
744a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
745a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
746a94100faSBill Paul 	 * following special functions:
747a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
748a94100faSBill Paul 	 * - Enables digital loopback mode
749a94100faSBill Paul 	 * - Leaves interrupts turned off
750a94100faSBill Paul 	 */
751a94100faSBill Paul 
752a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
753a94100faSBill Paul 	sc->rl_testmode = 1;
75497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
755351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
756ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
757ed510fb0SBill Paul 		phyaddr = 1;
758ed510fb0SBill Paul 	else
759ed510fb0SBill Paul 		phyaddr = 0;
760ed510fb0SBill Paul 
761ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
762ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
763ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
764ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
765ed510fb0SBill Paul 			break;
766ed510fb0SBill Paul 	}
767ed510fb0SBill Paul 
768ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
769ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
770ed510fb0SBill Paul 
771804af9a1SBill Paul 	DELAY(100000);
772a94100faSBill Paul 
773a94100faSBill Paul 	/* Put some data in the mbuf */
774a94100faSBill Paul 
775a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
776a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
777a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
778a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
779a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
780a94100faSBill Paul 
7817cae6651SBill Paul 	/*
7827cae6651SBill Paul 	 * Queue the packet, start transmission.
7837cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7847cae6651SBill Paul 	 */
785a94100faSBill Paul 
786abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
78797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
78852732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7897cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
79097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
791a94100faSBill Paul 	m0 = NULL;
792a94100faSBill Paul 
793a94100faSBill Paul 	/* Wait for it to propagate through the chip */
794a94100faSBill Paul 
795abc8ff44SBill Paul 	DELAY(100000);
796a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
797a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
798ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
799abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
800abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
801a94100faSBill Paul 			break;
802a94100faSBill Paul 		DELAY(10);
803a94100faSBill Paul 	}
804a94100faSBill Paul 
805a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8066b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8076b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8086b9f5c94SGleb Smirnoff 		    " loopback mode\n");
809a94100faSBill Paul 		error = EIO;
810a94100faSBill Paul 		goto done;
811a94100faSBill Paul 	}
812a94100faSBill Paul 
813a94100faSBill Paul 	/*
814a94100faSBill Paul 	 * The packet should have been dumped into the first
815a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
816a94100faSBill Paul 	 */
817a94100faSBill Paul 
818a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
819a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
820a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
821d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
822d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
823d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
824d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
825d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
826a94100faSBill Paul 
827d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
828d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
829a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
830a94100faSBill Paul 
831a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
832a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
833a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
834a94100faSBill Paul 
835a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8366b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8376b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
838a94100faSBill Paul 		error = EIO;
839a94100faSBill Paul 		goto done;
840a94100faSBill Paul 	}
841a94100faSBill Paul 
842a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
843a94100faSBill Paul 
844a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
845a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
846a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8476b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8486b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
849a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8506b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
851a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
852a94100faSBill Paul 		    ntohs(eh->ether_type));
8536b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8546b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8556b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8566b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8576b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8586b9f5c94SGleb Smirnoff 		    "details.\n");
859a94100faSBill Paul 		error = EIO;
860a94100faSBill Paul 	}
861a94100faSBill Paul 
862a94100faSBill Paul done:
863a94100faSBill Paul 	/* Turn interface off, release resources */
864a94100faSBill Paul 
865a94100faSBill Paul 	sc->rl_testmode = 0;
866351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
867a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
868a94100faSBill Paul 	re_stop(sc);
869a94100faSBill Paul 	if (m0 != NULL)
870a94100faSBill Paul 		m_freem(m0);
871a94100faSBill Paul 
87297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
87397b9d4baSJohn-Mark Gurney 
874a94100faSBill Paul 	return (error);
875a94100faSBill Paul }
876a94100faSBill Paul 
877ed510fb0SBill Paul #endif
878ed510fb0SBill Paul 
879a94100faSBill Paul /*
880a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
881a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
882a94100faSBill Paul  */
883a94100faSBill Paul static int
8847b5ffebfSPyun YongHyeon re_probe(device_t dev)
885a94100faSBill Paul {
886a94100faSBill Paul 	struct rl_type		*t;
887dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
888dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
889dfdb409eSPyun YongHyeon 	int			i;
890a94100faSBill Paul 
891dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
892dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
893dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
894dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
895a94100faSBill Paul 
896dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
897dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
89826390635SJohn Baldwin 			/*
89926390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
900dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
90126390635SJohn Baldwin 			 */
902a94100faSBill Paul 			return (ENXIO);
903a94100faSBill Paul 		}
904dfdb409eSPyun YongHyeon 	}
905dfdb409eSPyun YongHyeon 
906dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
907dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
908dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
909dfdb409eSPyun YongHyeon 			return (ENXIO);
910dfdb409eSPyun YongHyeon 		}
911dfdb409eSPyun YongHyeon 	}
912dfdb409eSPyun YongHyeon 
913dfdb409eSPyun YongHyeon 	t = re_devs;
914dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
915dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
916a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
917d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
918a94100faSBill Paul 		}
919a94100faSBill Paul 	}
920a94100faSBill Paul 
921a94100faSBill Paul 	return (ENXIO);
922a94100faSBill Paul }
923a94100faSBill Paul 
924a94100faSBill Paul /*
925a94100faSBill Paul  * Map a single buffer address.
926a94100faSBill Paul  */
927a94100faSBill Paul 
928a94100faSBill Paul static void
9297b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
930a94100faSBill Paul {
9318fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
932a94100faSBill Paul 
933a94100faSBill Paul 	if (error)
934a94100faSBill Paul 		return;
935a94100faSBill Paul 
936a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
937a94100faSBill Paul 	addr = arg;
938a94100faSBill Paul 	*addr = segs->ds_addr;
939a94100faSBill Paul }
940a94100faSBill Paul 
941a94100faSBill Paul static int
9427b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
943a94100faSBill Paul {
944d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
945a94100faSBill Paul 	int			error;
946a94100faSBill Paul 	int			i;
947a94100faSBill Paul 
948d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
949d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
950d65abd66SPyun YongHyeon 
951d65abd66SPyun YongHyeon 	/*
952d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
953ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
954ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
955ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
956ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
957ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
958d65abd66SPyun YongHyeon 	 */
959d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
960ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
961d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
962d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
963d65abd66SPyun YongHyeon 	if (error) {
964d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
965d65abd66SPyun YongHyeon 		return (error);
966d65abd66SPyun YongHyeon 	}
967d65abd66SPyun YongHyeon 
968d65abd66SPyun YongHyeon 	/*
969d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
970d65abd66SPyun YongHyeon 	 */
971d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
972d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
973d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
974d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
975d65abd66SPyun YongHyeon 	if (error) {
976d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
977d65abd66SPyun YongHyeon 		return (error);
978d65abd66SPyun YongHyeon 	}
979d65abd66SPyun YongHyeon 
980a94100faSBill Paul 	/*
981a94100faSBill Paul 	 * Allocate map for RX mbufs.
982a94100faSBill Paul 	 */
983d65abd66SPyun YongHyeon 
984d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
985d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
986d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
987a94100faSBill Paul 	if (error) {
988d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
989d65abd66SPyun YongHyeon 		return (error);
990a94100faSBill Paul 	}
991a94100faSBill Paul 
992a94100faSBill Paul 	/*
993a94100faSBill Paul 	 * Allocate map for TX descriptor list.
994a94100faSBill Paul 	 */
995a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
996a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
997d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
998a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
999a94100faSBill Paul 	if (error) {
1000d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1001d65abd66SPyun YongHyeon 		return (error);
1002a94100faSBill Paul 	}
1003a94100faSBill Paul 
1004a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1005a94100faSBill Paul 
1006a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1007d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1008d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1009a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1010d65abd66SPyun YongHyeon 	if (error) {
1011d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1012d65abd66SPyun YongHyeon 		return (error);
1013d65abd66SPyun YongHyeon 	}
1014a94100faSBill Paul 
1015a94100faSBill Paul 	/* Load the map for the TX ring. */
1016a94100faSBill Paul 
1017d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1018a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1019a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1020d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1021a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1022d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1023d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1024d65abd66SPyun YongHyeon 		return (ENOMEM);
1025d65abd66SPyun YongHyeon 	}
1026a94100faSBill Paul 
1027a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1028a94100faSBill Paul 
1029d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1030d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1031d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1032a94100faSBill Paul 		if (error) {
1033d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1034d65abd66SPyun YongHyeon 			return (error);
1035a94100faSBill Paul 		}
1036a94100faSBill Paul 	}
1037a94100faSBill Paul 
1038a94100faSBill Paul 	/*
1039a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1040a94100faSBill Paul 	 */
1041a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1042a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1043d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1044a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1045a94100faSBill Paul 	if (error) {
1046d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1047d65abd66SPyun YongHyeon 		return (error);
1048a94100faSBill Paul 	}
1049a94100faSBill Paul 
1050a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1051a94100faSBill Paul 
1052a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1053d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1054d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1055a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1056d65abd66SPyun YongHyeon 	if (error) {
1057d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1058d65abd66SPyun YongHyeon 		return (error);
1059d65abd66SPyun YongHyeon 	}
1060a94100faSBill Paul 
1061a94100faSBill Paul 	/* Load the map for the RX ring. */
1062a94100faSBill Paul 
1063d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1064a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1065a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1066d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1067a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1068d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1069d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1070d65abd66SPyun YongHyeon 		return (ENOMEM);
1071d65abd66SPyun YongHyeon 	}
1072a94100faSBill Paul 
1073a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1074a94100faSBill Paul 
1075d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1076d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1077a94100faSBill Paul 	if (error) {
1078d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1079d65abd66SPyun YongHyeon 		return (error);
1080d65abd66SPyun YongHyeon 	}
1081d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1082d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1083d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1084d65abd66SPyun YongHyeon 		if (error) {
1085d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1086d65abd66SPyun YongHyeon 			return (error);
1087a94100faSBill Paul 		}
1088a94100faSBill Paul 	}
1089a94100faSBill Paul 
1090a94100faSBill Paul 	return (0);
1091a94100faSBill Paul }
1092a94100faSBill Paul 
1093a94100faSBill Paul /*
1094a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1095a94100faSBill Paul  * setup and ethernet/BPF attach.
1096a94100faSBill Paul  */
1097a94100faSBill Paul static int
10987b5ffebfSPyun YongHyeon re_attach(device_t dev)
1099a94100faSBill Paul {
1100a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1101be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1102a94100faSBill Paul 	struct rl_softc		*sc;
1103a94100faSBill Paul 	struct ifnet		*ifp;
1104a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1105a94100faSBill Paul 	int			hwrev;
1106ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
1107d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11085774c5ffSPyun YongHyeon 	int			msic, reg;
110903ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1110a94100faSBill Paul 
1111a94100faSBill Paul 	sc = device_get_softc(dev);
1112ed510fb0SBill Paul 	sc->rl_dev = dev;
1113a94100faSBill Paul 
1114a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
111597b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1116d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1117d1754a9bSJohn Baldwin 
1118a94100faSBill Paul 	/*
1119a94100faSBill Paul 	 * Map control/status registers.
1120a94100faSBill Paul 	 */
1121a94100faSBill Paul 	pci_enable_busmaster(dev);
1122a94100faSBill Paul 
1123ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
11242c21710bSPyun YongHyeon 	/*
11252c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
11262c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
11272c21710bSPyun YongHyeon 	 * is used always activate io mapping.
11282c21710bSPyun YongHyeon 	 */
11292c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
11302c21710bSPyun YongHyeon 		prefer_iomap = 1;
11312c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1132ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1133ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1134ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1135ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1136ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
11372c21710bSPyun YongHyeon 	} else {
11382c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
11392c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
11402c21710bSPyun YongHyeon 	}
1141ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1142ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
11432c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1144ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1145ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1146ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1147ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
11482c21710bSPyun YongHyeon 	}
1149ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1150d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1151a94100faSBill Paul 		error = ENXIO;
1152a94100faSBill Paul 		goto fail;
1153a94100faSBill Paul 	}
1154a94100faSBill Paul 
1155a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1156a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1157a94100faSBill Paul 
11585774c5ffSPyun YongHyeon 	msic = 0;
11595774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
1160818951afSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
11615774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11625774c5ffSPyun YongHyeon 		if (bootverbose)
11635774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11645774c5ffSPyun YongHyeon 	}
1165f1bb696aSPyun YongHyeon 	if (msic > 0 && msi_disable == 0) {
1166f1bb696aSPyun YongHyeon 		msic = 1;
11675774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11685774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11695774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11705774c5ffSPyun YongHyeon 				    msic);
1171351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1172339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1173339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1174339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1175339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1176339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1177f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
11785774c5ffSPyun YongHyeon 			} else
11795774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11805774c5ffSPyun YongHyeon 		}
11815774c5ffSPyun YongHyeon 	}
1182a94100faSBill Paul 
11835774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1184351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
11855774c5ffSPyun YongHyeon 		rid = 0;
11865774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
11875774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
11885774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
11895774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1190a94100faSBill Paul 			error = ENXIO;
1191a94100faSBill Paul 			goto fail;
1192a94100faSBill Paul 		}
11935774c5ffSPyun YongHyeon 	} else {
11945774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
11955774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
11965774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
11975774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
11985774c5ffSPyun YongHyeon 				device_printf(dev,
11995774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12005774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12015774c5ffSPyun YongHyeon 				error = ENXIO;
12025774c5ffSPyun YongHyeon 				goto fail;
12035774c5ffSPyun YongHyeon 			}
12045774c5ffSPyun YongHyeon 		}
12055774c5ffSPyun YongHyeon 	}
1206a94100faSBill Paul 
12074d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
12084d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
12094d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
12104d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
12114d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
12124d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
12134d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
12144d2bf239SPyun YongHyeon 		}
12154d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12164d2bf239SPyun YongHyeon 	}
12174d2bf239SPyun YongHyeon 
1218a94100faSBill Paul 	/* Reset the adapter. */
121997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1220a94100faSBill Paul 	re_reset(sc);
122197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1222abc8ff44SBill Paul 
1223abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1224a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1225566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1226566ca8caSJung-uk Kim 	case 0x00000000:
1227566ca8caSJung-uk Kim 	case 0x10000000:
1228566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1229566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1230566ca8caSJung-uk Kim 		break;
1231566ca8caSJung-uk Kim 	default:
1232a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1233a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1234566ca8caSJung-uk Kim 		break;
1235566ca8caSJung-uk Kim 	}
1236566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1237abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1238abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1239abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1240566ca8caSJung-uk Kim 			sc->rl_hwrev = hw_rev->rl_rev;
1241abc8ff44SBill Paul 			break;
1242abc8ff44SBill Paul 		}
1243abc8ff44SBill Paul 		hw_rev++;
1244abc8ff44SBill Paul 	}
1245d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1246a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1247d65abd66SPyun YongHyeon 		error = ENXIO;
1248d65abd66SPyun YongHyeon 		goto fail;
1249d65abd66SPyun YongHyeon 	}
1250abc8ff44SBill Paul 
1251351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1252351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
1253f2e491c9SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_FASTETHER |
1254f2e491c9SPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1255351a76f9SPyun YongHyeon 		break;
1256351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1257351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
1258aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE |
1259aaab4fbeSJung-uk Kim 		    RL_FLAG_FASTETHER;
1260351a76f9SPyun YongHyeon 		break;
1261b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1262b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
1263aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_NOJUMBO | RL_FLAG_PHYWAKE |
1264aaab4fbeSJung-uk Kim 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1265f2e491c9SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1266b1d62f0fSPyun YongHyeon 		break;
1267351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
1268351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
1269886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1270886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1271351a76f9SPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
1272aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1273deb5c680SPyun YongHyeon 		break;
1274deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
127561f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
127661f45a72SPyun YongHyeon 		/* FALLTHROUGH */
127761f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
127861f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
127961f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
128061f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1281deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
128259ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
1283aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1284f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1285f2e491c9SPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1286deb5c680SPyun YongHyeon 		/*
1287deb5c680SPyun YongHyeon 		 * These controllers support jumbo frame but it seems
1288deb5c680SPyun YongHyeon 		 * that enabling it requires touching additional magic
1289deb5c680SPyun YongHyeon 		 * registers. Depending on MAC revisions some
1290deb5c680SPyun YongHyeon 		 * controllers need to disable checksum offload. So
1291deb5c680SPyun YongHyeon 		 * disable jumbo frame until I have better idea what
1292deb5c680SPyun YongHyeon 		 * it really requires to make it support.
1293deb5c680SPyun YongHyeon 		 * RTL8168C/CP : supports up to 6KB jumbo frame.
1294deb5c680SPyun YongHyeon 		 * RTL8111C/CP : supports up to 9KB jumbo frame.
1295deb5c680SPyun YongHyeon 		 */
1296deb5c680SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_NOJUMBO;
1297351a76f9SPyun YongHyeon 		break;
1298566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1299566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1300566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1301566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1302566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1303566ca8caSJung-uk Kim 		/* FALLTHROUGH */
13040596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
13050596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1306566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1307566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1308351a76f9SPyun YongHyeon 		break;
1309351a76f9SPyun YongHyeon 	default:
1310351a76f9SPyun YongHyeon 		break;
1311351a76f9SPyun YongHyeon 	}
1312351a76f9SPyun YongHyeon 
1313deb5c680SPyun YongHyeon 	/* Enable PME. */
1314deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1315deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1316deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1317deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1318deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1319deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1320deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1321deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1322deb5c680SPyun YongHyeon 
1323deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1324deb5c680SPyun YongHyeon 		/*
1325deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1326deb5c680SPyun YongHyeon 		 * address from EEPROM.
1327deb5c680SPyun YongHyeon 		 */
1328deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1329deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1330deb5c680SPyun YongHyeon 	} else {
1331141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1332ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1333a94100faSBill Paul 		if (re_did != 0x8129)
1334141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1335a94100faSBill Paul 
1336a94100faSBill Paul 		/*
1337a94100faSBill Paul 		 * Get station address from the EEPROM.
1338a94100faSBill Paul 		 */
1339ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1340be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1341be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1342be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1343deb5c680SPyun YongHyeon 	}
1344ed510fb0SBill Paul 
1345ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1346d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1347ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1348ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1349d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1350d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1351ed510fb0SBill Paul 	} else {
1352d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1353ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1354ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1355d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1356d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1357abc8ff44SBill Paul 	}
13589bac70b8SBill Paul 
1359a94100faSBill Paul 	error = re_allocmem(dev, sc);
1360a94100faSBill Paul 	if (error)
1361a94100faSBill Paul 		goto fail;
1362a94100faSBill Paul 
1363cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1364cd036ec1SBrooks Davis 	if (ifp == NULL) {
1365d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1366cd036ec1SBrooks Davis 		error = ENOSPC;
1367cd036ec1SBrooks Davis 		goto fail;
1368cd036ec1SBrooks Davis 	}
1369cd036ec1SBrooks Davis 
137061f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
137161f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
137261f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
137361f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
137461f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
137561f45a72SPyun YongHyeon 		else
137661f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
137761f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
137861f45a72SPyun YongHyeon 	}
137961f45a72SPyun YongHyeon 
1380351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1381351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1382351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1383351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1384351a76f9SPyun YongHyeon 	}
1385351a76f9SPyun YongHyeon 
1386a94100faSBill Paul 	/* Do MII setup */
1387a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1388a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1389d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1390a94100faSBill Paul 		error = ENXIO;
1391a94100faSBill Paul 		goto fail;
1392a94100faSBill Paul 	}
1393a94100faSBill Paul 
1394a94100faSBill Paul 	ifp->if_softc = sc;
13959bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1396a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1397a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1398a94100faSBill Paul 	ifp->if_start = re_start;
1399deb5c680SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1400deb5c680SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1401498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1402a94100faSBill Paul 	ifp->if_init = re_init;
140352732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
140452732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
140552732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1406a94100faSBill Paul 
1407ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1408ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1409ed510fb0SBill Paul 
1410a94100faSBill Paul 	/*
1411deb5c680SPyun YongHyeon 	 * XXX
1412deb5c680SPyun YongHyeon 	 * Still have no idea how to make TSO work on 8168C, 8168CP,
1413deb5c680SPyun YongHyeon 	 * 8111C and 8111CP.
1414deb5c680SPyun YongHyeon 	 */
1415deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1416deb5c680SPyun YongHyeon 		ifp->if_hwassist |= CSUM_TSO;
1417deb5c680SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_TSO4;
1418deb5c680SPyun YongHyeon 	}
1419deb5c680SPyun YongHyeon 
1420deb5c680SPyun YongHyeon 	/*
1421a94100faSBill Paul 	 * Call MI attach routine.
1422a94100faSBill Paul 	 */
1423a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1424a94100faSBill Paul 
1425960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1426960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1427960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1428960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
14297467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
14307467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
14317467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1432960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1433a2a8420cSPyun YongHyeon 	/*
1434a2a8420cSPyun YongHyeon 	 * Don't enable TSO by default. Under certain
1435a2a8420cSPyun YongHyeon 	 * circumtances the controller generated corrupted
1436a2a8420cSPyun YongHyeon 	 * packets in TSO size.
1437a2a8420cSPyun YongHyeon 	 */
1438a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1439a2a8420cSPyun YongHyeon 	ifp->if_capenable &= ~IFCAP_TSO4;
1440960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1441960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1442960fd5b3SPyun YongHyeon #endif
1443960fd5b3SPyun YongHyeon 	/*
1444960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1445960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1446960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1447960fd5b3SPyun YongHyeon 	 */
1448960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1449960fd5b3SPyun YongHyeon 
1450ed510fb0SBill Paul #ifdef RE_DIAG
1451ed510fb0SBill Paul 	/*
1452ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1453ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1454ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1455ed510fb0SBill Paul 	 */
1456a94100faSBill Paul 
1457ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1458ed510fb0SBill Paul 		error = re_diag(sc);
1459a94100faSBill Paul 		if (error) {
1460ed510fb0SBill Paul 			device_printf(dev,
1461ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1462a94100faSBill Paul 			ether_ifdetach(ifp);
1463a94100faSBill Paul 			goto fail;
1464a94100faSBill Paul 		}
1465ed510fb0SBill Paul 	}
1466ed510fb0SBill Paul #endif
1467a94100faSBill Paul 
1468a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1469351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
14705774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
14715774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14725774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
14735774c5ffSPyun YongHyeon 	else {
14745774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
14755774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
14765774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
14775774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
14785774c5ffSPyun YongHyeon 			if (error != 0)
14795774c5ffSPyun YongHyeon 				break;
14805774c5ffSPyun YongHyeon 		}
14815774c5ffSPyun YongHyeon 	}
1482a94100faSBill Paul 	if (error) {
1483d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1484a94100faSBill Paul 		ether_ifdetach(ifp);
1485a94100faSBill Paul 	}
1486a94100faSBill Paul 
1487a94100faSBill Paul fail:
1488ed510fb0SBill Paul 
1489a94100faSBill Paul 	if (error)
1490a94100faSBill Paul 		re_detach(dev);
1491a94100faSBill Paul 
1492a94100faSBill Paul 	return (error);
1493a94100faSBill Paul }
1494a94100faSBill Paul 
1495a94100faSBill Paul /*
1496a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1497a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1498a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1499a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1500a94100faSBill Paul  * allocated.
1501a94100faSBill Paul  */
1502a94100faSBill Paul static int
15037b5ffebfSPyun YongHyeon re_detach(device_t dev)
1504a94100faSBill Paul {
1505a94100faSBill Paul 	struct rl_softc		*sc;
1506a94100faSBill Paul 	struct ifnet		*ifp;
15075774c5ffSPyun YongHyeon 	int			i, rid;
1508a94100faSBill Paul 
1509a94100faSBill Paul 	sc = device_get_softc(dev);
1510fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1511aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
151297b9d4baSJohn-Mark Gurney 
151381cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
151481cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
151540929967SGleb Smirnoff #ifdef DEVICE_POLLING
151640929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
151740929967SGleb Smirnoff 			ether_poll_deregister(ifp);
151840929967SGleb Smirnoff #endif
151997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
152097b9d4baSJohn-Mark Gurney #if 0
152197b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
152297b9d4baSJohn-Mark Gurney #endif
1523a94100faSBill Paul 		re_stop(sc);
1524525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1525d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
15263d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
15273d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1528a94100faSBill Paul 		/*
1529a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1530a94100faSBill Paul 		 * still had a BPF descriptor attached to this
153197b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1532a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1533a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1534a94100faSBill Paul 		 * which will try to call re_init() again. This will
1535a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1536a94100faSBill Paul 		 * which will panic the system when the kernel tries
1537a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1538a94100faSBill Paul 		 * anymore.
1539a94100faSBill Paul 		 */
1540a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1541525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1542a94100faSBill Paul 	}
1543a94100faSBill Paul 	if (sc->rl_miibus)
1544a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1545a94100faSBill Paul 	bus_generic_detach(dev);
1546a94100faSBill Paul 
154797b9d4baSJohn-Mark Gurney 	/*
154897b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
154997b9d4baSJohn-Mark Gurney 	 * stopped here.
155097b9d4baSJohn-Mark Gurney 	 */
155197b9d4baSJohn-Mark Gurney 
15525774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
15535774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
15545774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
15555774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
15565774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
15575774c5ffSPyun YongHyeon 		}
15585774c5ffSPyun YongHyeon 	}
1559ad4f426eSWarner Losh 	if (ifp != NULL)
1560ad4f426eSWarner Losh 		if_free(ifp);
1561351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
15625774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
15635774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
15645774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
15655774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
15665774c5ffSPyun YongHyeon 		}
15675774c5ffSPyun YongHyeon 	} else {
15685774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
15695774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
15705774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
15715774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
15725774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
15735774c5ffSPyun YongHyeon 			}
15745774c5ffSPyun YongHyeon 		}
15755774c5ffSPyun YongHyeon 		pci_release_msi(dev);
15765774c5ffSPyun YongHyeon 	}
1577a94100faSBill Paul 	if (sc->rl_res)
1578ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1579ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1580a94100faSBill Paul 
1581a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1582a94100faSBill Paul 
1583a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1584a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1585a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1586a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1587a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1588a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1589a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1590a94100faSBill Paul 	}
1591a94100faSBill Paul 
1592a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1593a94100faSBill Paul 
1594a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1595a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1596a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1597a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1598a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1599a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1600a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1601a94100faSBill Paul 	}
1602a94100faSBill Paul 
1603a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1604a94100faSBill Paul 
1605d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1606d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1607d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1608d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1609d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1610d65abd66SPyun YongHyeon 	}
1611d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1612d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1613d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1614d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1615d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1616d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1617d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1618d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1619a94100faSBill Paul 	}
1620a94100faSBill Paul 
1621a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1622a94100faSBill Paul 
1623a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1624a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1625a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1626a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1627a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1628a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1629a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1630a94100faSBill Paul 	}
1631a94100faSBill Paul 
1632a94100faSBill Paul 	if (sc->rl_parent_tag)
1633a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1634a94100faSBill Paul 
1635a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1636a94100faSBill Paul 
1637a94100faSBill Paul 	return (0);
1638a94100faSBill Paul }
1639a94100faSBill Paul 
1640d65abd66SPyun YongHyeon static __inline void
16417b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1642a94100faSBill Paul {
1643d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1644d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1645d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1646a94100faSBill Paul 
1647d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1648d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1649d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1650d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1651d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1652d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1653d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1654d65abd66SPyun YongHyeon }
1655d65abd66SPyun YongHyeon 
1656d65abd66SPyun YongHyeon static int
16577b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1658d65abd66SPyun YongHyeon {
1659d65abd66SPyun YongHyeon 	struct mbuf		*m;
1660d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1661d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1662d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1663d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1664d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1665d65abd66SPyun YongHyeon 	int			error, nsegs;
1666d65abd66SPyun YongHyeon 
1667d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1668d65abd66SPyun YongHyeon 	if (m == NULL)
1669a94100faSBill Paul 		return (ENOBUFS);
1670a94100faSBill Paul 
1671a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
167222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
167322a11c96SJohn-Mark Gurney 	/*
167422a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
167522a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
167622a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
167722a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
167822a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
167922a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
168022a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
168122a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
168222a11c96SJohn-Mark Gurney 	 */
168322a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
168422a11c96SJohn-Mark Gurney #endif
1685d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1686d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1687d65abd66SPyun YongHyeon 	if (error != 0) {
1688d65abd66SPyun YongHyeon 		m_freem(m);
1689d65abd66SPyun YongHyeon 		return (ENOBUFS);
1690d65abd66SPyun YongHyeon 	}
1691d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1692a94100faSBill Paul 
1693d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1694d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1695d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1696d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1697d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1698a94100faSBill Paul 	}
1699a94100faSBill Paul 
1700d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1701d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1702d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1703d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1704d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1705d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1706a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1707a94100faSBill Paul 
1708d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1709d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1710d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1711d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1712d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1713d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1714d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1715d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1716d65abd66SPyun YongHyeon 
1717a94100faSBill Paul 	return (0);
1718a94100faSBill Paul }
1719a94100faSBill Paul 
172022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
172122a11c96SJohn-Mark Gurney static __inline void
17227b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
172322a11c96SJohn-Mark Gurney {
172422a11c96SJohn-Mark Gurney 	int                     i;
172522a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
172622a11c96SJohn-Mark Gurney 
172722a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
172822a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
172922a11c96SJohn-Mark Gurney 
173022a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
173122a11c96SJohn-Mark Gurney 		*dst++ = *src++;
173222a11c96SJohn-Mark Gurney 
173322a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
173422a11c96SJohn-Mark Gurney }
173522a11c96SJohn-Mark Gurney #endif
173622a11c96SJohn-Mark Gurney 
1737a94100faSBill Paul static int
17387b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1739a94100faSBill Paul {
1740d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1741d65abd66SPyun YongHyeon 	int			i;
174297b9d4baSJohn-Mark Gurney 
174397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
174497b9d4baSJohn-Mark Gurney 
1745d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1746d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1747d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1748d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1749d65abd66SPyun YongHyeon 	/* Set EOR. */
1750d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1751d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1752a94100faSBill Paul 
1753a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1754d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1755d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1756d65abd66SPyun YongHyeon 
1757a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1758a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1759d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1760a94100faSBill Paul 
1761a94100faSBill Paul 	return (0);
1762a94100faSBill Paul }
1763a94100faSBill Paul 
1764a94100faSBill Paul static int
17657b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1766a94100faSBill Paul {
1767d65abd66SPyun YongHyeon 	int			error, i;
1768a94100faSBill Paul 
1769d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1770d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1771d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1772d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1773d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1774d65abd66SPyun YongHyeon 			return (error);
1775a94100faSBill Paul 	}
1776a94100faSBill Paul 
1777a94100faSBill Paul 	/* Flush the RX descriptors */
1778a94100faSBill Paul 
1779a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1780a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1781a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1782a94100faSBill Paul 
1783a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1784a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1785a94100faSBill Paul 
1786a94100faSBill Paul 	return (0);
1787a94100faSBill Paul }
1788a94100faSBill Paul 
1789a94100faSBill Paul /*
1790a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1791a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1792a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1793a94100faSBill Paul  */
1794ed510fb0SBill Paul static int
17951abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
1796a94100faSBill Paul {
1797a94100faSBill Paul 	struct mbuf		*m;
1798a94100faSBill Paul 	struct ifnet		*ifp;
1799a94100faSBill Paul 	int			i, total_len;
1800a94100faSBill Paul 	struct rl_desc		*cur_rx;
1801a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
18021abcdbd1SAttilio Rao 	int			maxpkt = 16, rx_npkts = 0;
1803a94100faSBill Paul 
18045120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
18055120abbfSSam Leffler 
1806fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1807a94100faSBill Paul 
1808a94100faSBill Paul 	/* Invalidate the descriptor memory */
1809a94100faSBill Paul 
1810a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1811a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1812d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1813a94100faSBill Paul 
1814d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1815d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1816a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1817a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1818d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1819d65abd66SPyun YongHyeon 			break;
1820d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1821a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1822d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1823a94100faSBill Paul 
1824a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1825d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1826d65abd66SPyun YongHyeon 				/*
1827d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1828d65abd66SPyun YongHyeon 				 * discard all the pieces.
1829d65abd66SPyun YongHyeon 				 */
1830d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1831d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1832d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1833d65abd66SPyun YongHyeon 				}
1834d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1835d65abd66SPyun YongHyeon 				continue;
1836d65abd66SPyun YongHyeon 			}
183722a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1838a94100faSBill Paul 			if (sc->rl_head == NULL)
1839a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1840a94100faSBill Paul 			else {
1841a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1842a94100faSBill Paul 				sc->rl_tail->m_next = m;
1843a94100faSBill Paul 				sc->rl_tail = m;
1844a94100faSBill Paul 			}
1845a94100faSBill Paul 			continue;
1846a94100faSBill Paul 		}
1847a94100faSBill Paul 
1848a94100faSBill Paul 		/*
1849a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1850a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1851a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1852a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1853a94100faSBill Paul 		 * were already used, so to make room for the extra
1854a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1855a94100faSBill Paul 		 * error' bit and shifted the other status bits
1856a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1857a94100faSBill Paul 		 * still in the same places. We have already extracted
1858a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1859a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1860a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1861a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1862a94100faSBill Paul 		 * same format as that of the 8139C+.
1863a94100faSBill Paul 		 */
1864a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1865a94100faSBill Paul 			rxstat >>= 1;
1866a94100faSBill Paul 
186722a11c96SJohn-Mark Gurney 		/*
186822a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
186922a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
187022a11c96SJohn-Mark Gurney 		 */
187122a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
187222a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1873a94100faSBill Paul 			ifp->if_ierrors++;
1874a94100faSBill Paul 			/*
1875a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1876a94100faSBill Paul 			 * discard all the pieces.
1877a94100faSBill Paul 			 */
1878a94100faSBill Paul 			if (sc->rl_head != NULL) {
1879a94100faSBill Paul 				m_freem(sc->rl_head);
1880a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1881a94100faSBill Paul 			}
1882d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1883a94100faSBill Paul 			continue;
1884a94100faSBill Paul 		}
1885a94100faSBill Paul 
1886a94100faSBill Paul 		/*
1887a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1888a94100faSBill Paul 		 * reload the current one.
1889a94100faSBill Paul 		 */
1890a94100faSBill Paul 
1891d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1892d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1893a94100faSBill Paul 			if (sc->rl_head != NULL) {
1894a94100faSBill Paul 				m_freem(sc->rl_head);
1895a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1896a94100faSBill Paul 			}
1897d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1898a94100faSBill Paul 			continue;
1899a94100faSBill Paul 		}
1900a94100faSBill Paul 
1901a94100faSBill Paul 		if (sc->rl_head != NULL) {
190222a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
190322a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
190422a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1905a94100faSBill Paul 			/*
1906a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1907a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1908a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1909a94100faSBill Paul 			 * care about anyway.
1910a94100faSBill Paul 			 */
1911a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1912a94100faSBill Paul 				sc->rl_tail->m_len -=
1913a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1914a94100faSBill Paul 				m_freem(m);
1915a94100faSBill Paul 			} else {
1916a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1917a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1918a94100faSBill Paul 				sc->rl_tail->m_next = m;
1919a94100faSBill Paul 			}
1920a94100faSBill Paul 			m = sc->rl_head;
1921a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1922a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1923a94100faSBill Paul 		} else
1924a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1925a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1926a94100faSBill Paul 
192722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
192822a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
192922a11c96SJohn-Mark Gurney #endif
1930a94100faSBill Paul 		ifp->if_ipackets++;
1931a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1932a94100faSBill Paul 
1933a94100faSBill Paul 		/* Do RX checksumming if enabled */
1934a94100faSBill Paul 
1935a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1936deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1937a94100faSBill Paul 				/* Check IP header checksum */
1938a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
1939deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1940deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1941a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1942deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1943deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1944a94100faSBill Paul 
1945a94100faSBill Paul 				/* Check TCP/UDP checksum */
1946a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
1947a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1948a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
1949a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1950a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
1951a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1952a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
1953a94100faSBill Paul 				}
1954deb5c680SPyun YongHyeon 			} else {
1955deb5c680SPyun YongHyeon 				/*
1956deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
1957deb5c680SPyun YongHyeon 				 */
1958deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
1959deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1960deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1961deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
1962deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
1963deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
1964deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1965deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
1966deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
1967deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1968deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
1969deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1970deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
1971deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1972deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
1973deb5c680SPyun YongHyeon 				}
1974deb5c680SPyun YongHyeon 			}
1975a94100faSBill Paul 		}
1976ed510fb0SBill Paul 		maxpkt--;
1977d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
197878ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1979bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
198078ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1981d147662cSGleb Smirnoff 		}
19825120abbfSSam Leffler 		RL_UNLOCK(sc);
1983a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
19845120abbfSSam Leffler 		RL_LOCK(sc);
19851abcdbd1SAttilio Rao 		rx_npkts++;
1986a94100faSBill Paul 	}
1987a94100faSBill Paul 
1988a94100faSBill Paul 	/* Flush the RX DMA ring */
1989a94100faSBill Paul 
1990a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1991a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1992a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1993a94100faSBill Paul 
1994a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1995ed510fb0SBill Paul 
19961abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
19971abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
1998ed510fb0SBill Paul 	if (maxpkt)
1999ed510fb0SBill Paul 		return(EAGAIN);
2000ed510fb0SBill Paul 
2001ed510fb0SBill Paul 	return(0);
2002a94100faSBill Paul }
2003a94100faSBill Paul 
2004a94100faSBill Paul static void
20057b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2006a94100faSBill Paul {
2007a94100faSBill Paul 	struct ifnet		*ifp;
2008d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2009a94100faSBill Paul 	u_int32_t		txstat;
2010d65abd66SPyun YongHyeon 	int			cons;
2011d65abd66SPyun YongHyeon 
2012d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2013d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2014d65abd66SPyun YongHyeon 		return;
2015a94100faSBill Paul 
2016fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2017a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2018a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2019a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2020d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2021a94100faSBill Paul 
2022d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2023d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2024d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2025d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2026a94100faSBill Paul 			break;
2027a94100faSBill Paul 		/*
2028a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2029a94100faSBill Paul 		 * in a fragment chain, which also happens to
2030a94100faSBill Paul 		 * be the only place where the TX status bits
2031a94100faSBill Paul 		 * are valid.
2032a94100faSBill Paul 		 */
2033a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2034d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2035d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2036d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2037d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2038d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2039d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2040d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2041d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2042d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2043a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2044a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2045a94100faSBill Paul 				ifp->if_collisions++;
2046a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2047a94100faSBill Paul 				ifp->if_oerrors++;
2048a94100faSBill Paul 			else
2049a94100faSBill Paul 				ifp->if_opackets++;
2050a94100faSBill Paul 		}
2051a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2052d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2053a94100faSBill Paul 	}
2054d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2055a94100faSBill Paul 
2056a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2057a94100faSBill Paul 
2058d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2059ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2060a94100faSBill Paul 		/*
2061b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2062b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2063a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2064a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2065a94100faSBill Paul 		 */
2066a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2067ed510fb0SBill Paul #endif
2068b4b95879SMarius Strobl 	} else
2069b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2070a94100faSBill Paul }
2071a94100faSBill Paul 
2072a94100faSBill Paul static void
20737b5ffebfSPyun YongHyeon re_tick(void *xsc)
2074a94100faSBill Paul {
2075a94100faSBill Paul 	struct rl_softc		*sc;
2076d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2077a94100faSBill Paul 
2078a94100faSBill Paul 	sc = xsc;
207997b9d4baSJohn-Mark Gurney 
208097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
208197b9d4baSJohn-Mark Gurney 
20821d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2083a94100faSBill Paul 	mii_tick(mii);
20840fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
20850fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2086c2d2e19cSPyun YongHyeon 	/*
2087c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2088c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2089c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2090c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2091c2d2e19cSPyun YongHyeon 	 */
2092c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2093130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2094d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2095a94100faSBill Paul }
2096a94100faSBill Paul 
2097a94100faSBill Paul #ifdef DEVICE_POLLING
20981abcdbd1SAttilio Rao static int
2099a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2100a94100faSBill Paul {
2101a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
21021abcdbd1SAttilio Rao 	int rx_npkts = 0;
2103a94100faSBill Paul 
2104a94100faSBill Paul 	RL_LOCK(sc);
210540929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
21061abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
210797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
21081abcdbd1SAttilio Rao 	return (rx_npkts);
210997b9d4baSJohn-Mark Gurney }
211097b9d4baSJohn-Mark Gurney 
21111abcdbd1SAttilio Rao static int
211297b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
211397b9d4baSJohn-Mark Gurney {
211497b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
21151abcdbd1SAttilio Rao 	int rx_npkts;
211697b9d4baSJohn-Mark Gurney 
211797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
211897b9d4baSJohn-Mark Gurney 
2119a94100faSBill Paul 	sc->rxcycles = count;
21201abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2121a94100faSBill Paul 	re_txeof(sc);
2122a94100faSBill Paul 
212337652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2124ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2125a94100faSBill Paul 
2126a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2127a94100faSBill Paul 		u_int16_t       status;
2128a94100faSBill Paul 
2129a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2130a94100faSBill Paul 		if (status == 0xffff)
21311abcdbd1SAttilio Rao 			return (rx_npkts);
2132a94100faSBill Paul 		if (status)
2133a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2134818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2135818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2136818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2137a94100faSBill Paul 
2138a94100faSBill Paul 		/*
2139a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2140a94100faSBill Paul 		 */
2141a94100faSBill Paul 
2142b659f1f0SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR)
214397b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2144a94100faSBill Paul 	}
21451abcdbd1SAttilio Rao 	return (rx_npkts);
2146a94100faSBill Paul }
2147a94100faSBill Paul #endif /* DEVICE_POLLING */
2148a94100faSBill Paul 
2149ef544f63SPaolo Pisati static int
21507b5ffebfSPyun YongHyeon re_intr(void *arg)
2151a94100faSBill Paul {
2152a94100faSBill Paul 	struct rl_softc		*sc;
2153ed510fb0SBill Paul 	uint16_t		status;
2154a94100faSBill Paul 
2155a94100faSBill Paul 	sc = arg;
2156ed510fb0SBill Paul 
2157ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2158498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2159ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2160ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2161ed510fb0SBill Paul 
2162ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2163ed510fb0SBill Paul 
2164ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2165ed510fb0SBill Paul }
2166ed510fb0SBill Paul 
2167ed510fb0SBill Paul static void
21687b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2169ed510fb0SBill Paul {
2170ed510fb0SBill Paul 	struct rl_softc		*sc;
2171ed510fb0SBill Paul 	struct ifnet		*ifp;
2172ed510fb0SBill Paul 	u_int16_t		status;
2173ed510fb0SBill Paul 	int			rval = 0;
2174ed510fb0SBill Paul 
2175ed510fb0SBill Paul 	sc = arg;
2176ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2177a94100faSBill Paul 
2178a94100faSBill Paul 	RL_LOCK(sc);
217997b9d4baSJohn-Mark Gurney 
2180a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2181a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2182a94100faSBill Paul 
2183d65abd66SPyun YongHyeon 	if (sc->suspended ||
2184d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2185ed510fb0SBill Paul 		RL_UNLOCK(sc);
2186ed510fb0SBill Paul 		return;
2187ed510fb0SBill Paul 	}
2188a94100faSBill Paul 
2189ed510fb0SBill Paul #ifdef DEVICE_POLLING
2190ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2191ed510fb0SBill Paul 		RL_UNLOCK(sc);
2192ed510fb0SBill Paul 		return;
2193ed510fb0SBill Paul 	}
2194ed510fb0SBill Paul #endif
2195a94100faSBill Paul 
2196ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
21971abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2198ed510fb0SBill Paul 
2199818951afSPyun YongHyeon 	/*
2200818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2201818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2202818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2203818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2204818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2205818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2206818951afSPyun YongHyeon 	 */
2207818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2208818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2209818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
22103d85c23dSPyun YongHyeon 	if (status & (
2211ed510fb0SBill Paul #ifdef RE_TX_MODERATION
22123d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2213ed510fb0SBill Paul #else
22143d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2215ed510fb0SBill Paul #endif
2216ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2217a94100faSBill Paul 		re_txeof(sc);
2218a94100faSBill Paul 
2219b659f1f0SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR)
222097b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2221a94100faSBill Paul 
222252732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2223ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2224a94100faSBill Paul 
2225a94100faSBill Paul 	RL_UNLOCK(sc);
2226ed510fb0SBill Paul 
2227ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2228ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2229ed510fb0SBill Paul 		return;
2230ed510fb0SBill Paul 	}
2231ed510fb0SBill Paul 
2232ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2233a94100faSBill Paul }
2234a94100faSBill Paul 
2235d65abd66SPyun YongHyeon static int
22367b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2237d65abd66SPyun YongHyeon {
2238d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2239d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2240d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2241d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2242d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2243d65abd66SPyun YongHyeon 	int			nsegs, prod;
2244d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2245d65abd66SPyun YongHyeon 	int			padlen;
2246ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2247a94100faSBill Paul 
2248d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2249738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
22500fc4974fSBill Paul 
22510fc4974fSBill Paul 	/*
22520fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
22530fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
22540fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
22550fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
22560fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
22570fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
225899c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
22590fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2260d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
22610fc4974fSBill Paul 	 */
2262f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2263deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
226499c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2265d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2266d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2267d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2268d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2269d65abd66SPyun YongHyeon 			m_freem(*m_head);
2270d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2271d65abd66SPyun YongHyeon 				*m_head = NULL;
2272a94100faSBill Paul 				return (ENOBUFS);
2273a94100faSBill Paul 			}
2274d65abd66SPyun YongHyeon 			*m_head = m_new;
2275d65abd66SPyun YongHyeon 		}
2276d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2277d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
227880a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2279b4b95879SMarius Strobl 			if (m_new == NULL) {
2280b4b95879SMarius Strobl 				m_freem(*m_head);
2281b4b95879SMarius Strobl 				*m_head = NULL;
228280a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2283b4b95879SMarius Strobl 			}
2284d65abd66SPyun YongHyeon 		} else
2285d65abd66SPyun YongHyeon 			m_new = *m_head;
2286a94100faSBill Paul 
22870fc4974fSBill Paul 		/*
22880fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
22890fc4974fSBill Paul 		 * to avoid leaking data.
22900fc4974fSBill Paul 		 */
2291d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2292d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
22930fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2294d65abd66SPyun YongHyeon 		*m_head = m_new;
22950fc4974fSBill Paul 	}
22960fc4974fSBill Paul 
2297d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2298d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2299d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2300d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2301d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2302304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2303d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2304d65abd66SPyun YongHyeon 			m_freem(*m_head);
2305b4b95879SMarius Strobl 			*m_head = NULL;
2306d65abd66SPyun YongHyeon 			return (ENOBUFS);
2307a94100faSBill Paul 		}
2308d65abd66SPyun YongHyeon 		*m_head = m_new;
2309d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2310d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2311d65abd66SPyun YongHyeon 		if (error != 0) {
2312d65abd66SPyun YongHyeon 			m_freem(*m_head);
2313d65abd66SPyun YongHyeon 			*m_head = NULL;
2314d65abd66SPyun YongHyeon 			return (error);
2315a94100faSBill Paul 		}
2316d65abd66SPyun YongHyeon 	} else if (error != 0)
2317d65abd66SPyun YongHyeon 		return (error);
2318d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2319d65abd66SPyun YongHyeon 		m_freem(*m_head);
2320d65abd66SPyun YongHyeon 		*m_head = NULL;
2321d65abd66SPyun YongHyeon 		return (EIO);
2322d65abd66SPyun YongHyeon 	}
2323d65abd66SPyun YongHyeon 
2324d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2325d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2326d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2327d65abd66SPyun YongHyeon 		return (ENOBUFS);
2328d65abd66SPyun YongHyeon 	}
2329d65abd66SPyun YongHyeon 
2330d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2331d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2332a94100faSBill Paul 
2333a94100faSBill Paul 	/*
2334d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2335d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2336d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2337d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2338a94100faSBill Paul 	 */
2339deb5c680SPyun YongHyeon 	vlanctl = 0;
2340d65abd66SPyun YongHyeon 	csum_flags = 0;
2341d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2342d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2343d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2344d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2345d65abd66SPyun YongHyeon 	else {
234699c8ae87SPyun YongHyeon 		/*
234799c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
234899c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
234999c8ae87SPyun YongHyeon 		 * does't make effects.
235099c8ae87SPyun YongHyeon 		 */
235199c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2352deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2353d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2354deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2355deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2356d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2357deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2358deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2359d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2360deb5c680SPyun YongHyeon 			} else {
2361deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2362deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2363deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2364deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2365deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2366deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2367deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2368deb5c680SPyun YongHyeon 			}
2369d65abd66SPyun YongHyeon 		}
237099c8ae87SPyun YongHyeon 	}
2371a94100faSBill Paul 
2372ccf34c81SPyun YongHyeon 	/*
2373ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2374ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2375ccf34c81SPyun YongHyeon 	 * transmission attempt.
2376ccf34c81SPyun YongHyeon 	 */
2377ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2378bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2379deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2380ccf34c81SPyun YongHyeon 
2381d65abd66SPyun YongHyeon 	si = prod;
2382d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2383d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2384deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2385d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2386d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2387d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2388d65abd66SPyun YongHyeon 		if (i != 0)
2389d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2390d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2391d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2392d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2393d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2394d65abd66SPyun YongHyeon 	}
2395d65abd66SPyun YongHyeon 	/* Update producer index. */
2396d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2397a94100faSBill Paul 
2398d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2399d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2400d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2401d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2402d65abd66SPyun YongHyeon 
2403d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2404d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2405d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2406a94100faSBill Paul 
2407d65abd66SPyun YongHyeon 	/*
2408d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2409d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2410d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2411d65abd66SPyun YongHyeon 	 */
2412d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2413d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2414d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2415d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2416d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2417a94100faSBill Paul 
2418a94100faSBill Paul 	return (0);
2419a94100faSBill Paul }
2420a94100faSBill Paul 
242197b9d4baSJohn-Mark Gurney static void
24227b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
242397b9d4baSJohn-Mark Gurney {
2424ed510fb0SBill Paul 	struct ifnet		*ifp;
242597b9d4baSJohn-Mark Gurney 
2426ed510fb0SBill Paul 	ifp = arg;
2427ed510fb0SBill Paul 	re_start(ifp);
242897b9d4baSJohn-Mark Gurney }
242997b9d4baSJohn-Mark Gurney 
2430a94100faSBill Paul /*
2431a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2432a94100faSBill Paul  */
2433a94100faSBill Paul static void
24347b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2435a94100faSBill Paul {
2436a94100faSBill Paul 	struct rl_softc		*sc;
2437d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2438d65abd66SPyun YongHyeon 	int			queued;
2439a94100faSBill Paul 
2440a94100faSBill Paul 	sc = ifp->if_softc;
244197b9d4baSJohn-Mark Gurney 
2442ed510fb0SBill Paul 	RL_LOCK(sc);
2443ed510fb0SBill Paul 
2444d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2445351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2446ed510fb0SBill Paul 		RL_UNLOCK(sc);
2447ed510fb0SBill Paul 		return;
2448ed510fb0SBill Paul 	}
2449a94100faSBill Paul 
2450d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2451d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
245252732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2453a94100faSBill Paul 		if (m_head == NULL)
2454a94100faSBill Paul 			break;
2455a94100faSBill Paul 
2456d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2457b4b95879SMarius Strobl 			if (m_head == NULL)
2458b4b95879SMarius Strobl 				break;
245952732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
246013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2461a94100faSBill Paul 			break;
2462a94100faSBill Paul 		}
2463a94100faSBill Paul 
2464a94100faSBill Paul 		/*
2465a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2466a94100faSBill Paul 		 * to him.
2467a94100faSBill Paul 		 */
246859a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
246952732175SMax Laier 
247052732175SMax Laier 		queued++;
2471a94100faSBill Paul 	}
2472a94100faSBill Paul 
2473ed510fb0SBill Paul 	if (queued == 0) {
2474ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2475d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2476ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2477ed510fb0SBill Paul #endif
2478ed510fb0SBill Paul 		RL_UNLOCK(sc);
247952732175SMax Laier 		return;
2480ed510fb0SBill Paul 	}
248152732175SMax Laier 
2482a94100faSBill Paul 	/* Flush the TX descriptors */
2483a94100faSBill Paul 
2484a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2485a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2486a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2487a94100faSBill Paul 
24880fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2489a94100faSBill Paul 
2490ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2491a94100faSBill Paul 	/*
2492a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2493a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2494a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2495a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2496a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2497a94100faSBill Paul 	 * the timer count is reset to 0.
2498a94100faSBill Paul 	 */
2499a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2500ed510fb0SBill Paul #endif
2501a94100faSBill Paul 
2502a94100faSBill Paul 	/*
2503a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2504a94100faSBill Paul 	 */
25051d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2506ed510fb0SBill Paul 
2507ed510fb0SBill Paul 	RL_UNLOCK(sc);
2508a94100faSBill Paul }
2509a94100faSBill Paul 
2510a94100faSBill Paul static void
25117b5ffebfSPyun YongHyeon re_init(void *xsc)
2512a94100faSBill Paul {
2513a94100faSBill Paul 	struct rl_softc		*sc = xsc;
251497b9d4baSJohn-Mark Gurney 
251597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
251697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
251797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
251897b9d4baSJohn-Mark Gurney }
251997b9d4baSJohn-Mark Gurney 
252097b9d4baSJohn-Mark Gurney static void
25217b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
252297b9d4baSJohn-Mark Gurney {
2523fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2524a94100faSBill Paul 	struct mii_data		*mii;
2525566ca8caSJung-uk Kim 	uint32_t		reg;
252670acaecfSPyun YongHyeon 	uint16_t		cfg;
25274d3d7085SBernd Walter 	union {
25284d3d7085SBernd Walter 		uint32_t align_dummy;
25294d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
25304d3d7085SBernd Walter         } eaddr;
2531a94100faSBill Paul 
253297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
253397b9d4baSJohn-Mark Gurney 
2534a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2535a94100faSBill Paul 
2536a94100faSBill Paul 	/*
2537a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2538a94100faSBill Paul 	 */
2539a94100faSBill Paul 	re_stop(sc);
2540a94100faSBill Paul 
2541b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2542b659f1f0SPyun YongHyeon 	re_reset(sc);
2543b659f1f0SPyun YongHyeon 
2544a94100faSBill Paul 	/*
2545c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2546edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2547c2c6548bSBill Paul 	 * before all others.
2548c2c6548bSBill Paul 	 */
254970acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
255070acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
255170acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
255270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
255370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2554deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2555deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2556deb5c680SPyun YongHyeon 		/* XXX magic. */
2557deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2558deb5c680SPyun YongHyeon 	} else
2559deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2560deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
2561566ca8caSJung-uk Kim 	if (sc->rl_hwrev == RL_HWREV_8169_8110SC ||
2562566ca8caSJung-uk Kim 	    sc->rl_hwrev == RL_HWREV_8169_8110SCE) {
2563566ca8caSJung-uk Kim 		reg = 0x000fff00;
2564566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
2565566ca8caSJung-uk Kim 			reg |= 0x000000ff;
2566566ca8caSJung-uk Kim 		if (sc->rl_hwrev == RL_HWREV_8169_8110SCE)
2567566ca8caSJung-uk Kim 			reg |= 0x00f00000;
2568566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
2569566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
2570566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
2571566ca8caSJung-uk Kim 	}
2572ae644087SPyun YongHyeon 	/*
2573ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2574ae644087SPyun YongHyeon 	 * allowed in controller.
2575ae644087SPyun YongHyeon 	 */
2576ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2577ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2578ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2579ae644087SPyun YongHyeon 	}
2580c2c6548bSBill Paul 
2581c2c6548bSBill Paul 	/*
2582a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2583a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2584a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2585a94100faSBill Paul 	 */
25864d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
25874d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2588a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2589ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2590ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2591ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2592ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2593a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2594a94100faSBill Paul 
2595a94100faSBill Paul 	/*
2596a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2597a94100faSBill Paul 	 */
2598a94100faSBill Paul 	re_rx_list_init(sc);
2599a94100faSBill Paul 	re_tx_list_init(sc);
2600a94100faSBill Paul 
2601a94100faSBill Paul 	/*
2602d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2603d01fac16SPyun YongHyeon 	 */
2604d01fac16SPyun YongHyeon 
2605d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2606d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2607d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2608d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2609d01fac16SPyun YongHyeon 
2610d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2611d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2612d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2613d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2614d01fac16SPyun YongHyeon 
2615d01fac16SPyun YongHyeon 	/*
2616a94100faSBill Paul 	 * Enable transmit and receive.
2617a94100faSBill Paul 	 */
2618a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2619a94100faSBill Paul 
2620a94100faSBill Paul 	/*
2621ff191365SJung-uk Kim 	 * Set the initial TX configuration.
2622a94100faSBill Paul 	 */
2623abc8ff44SBill Paul 	if (sc->rl_testmode) {
2624abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2625abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2626abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2627a94100faSBill Paul 		else
2628abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2629abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2630abc8ff44SBill Paul 	} else
2631a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2632d01fac16SPyun YongHyeon 
2633d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2634d01fac16SPyun YongHyeon 
2635a94100faSBill Paul 	/*
2636ff191365SJung-uk Kim 	 * Set the initial RX configuration.
2637a94100faSBill Paul 	 */
2638ff191365SJung-uk Kim 	re_set_rxmode(sc);
2639a94100faSBill Paul 
2640a94100faSBill Paul #ifdef DEVICE_POLLING
2641a94100faSBill Paul 	/*
2642a94100faSBill Paul 	 * Disable interrupts if we are polling.
2643a94100faSBill Paul 	 */
264440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2645a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2646a94100faSBill Paul 	else	/* otherwise ... */
264740929967SGleb Smirnoff #endif
2648ed510fb0SBill Paul 
2649a94100faSBill Paul 	/*
2650a94100faSBill Paul 	 * Enable interrupts.
2651a94100faSBill Paul 	 */
2652a94100faSBill Paul 	if (sc->rl_testmode)
2653a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2654a94100faSBill Paul 	else
2655a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2656ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2657a94100faSBill Paul 
2658a94100faSBill Paul 	/* Set initial TX threshold */
2659a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2660a94100faSBill Paul 
2661a94100faSBill Paul 	/* Start RX/TX process. */
2662a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2663a94100faSBill Paul #ifdef notdef
2664a94100faSBill Paul 	/* Enable receiver and transmitter. */
2665a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2666a94100faSBill Paul #endif
2667a94100faSBill Paul 
2668ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2669a94100faSBill Paul 	/*
2670a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2671a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2672a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2673a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2674a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2675a94100faSBill Paul 	 */
2676a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2677a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2678a94100faSBill Paul 	else
2679a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2680ed510fb0SBill Paul #endif
2681a94100faSBill Paul 
2682a94100faSBill Paul 	/*
2683a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2684a94100faSBill Paul 	 * size so we can receive jumbo frames.
2685a94100faSBill Paul 	 */
2686a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2687a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2688a94100faSBill Paul 
268997b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2690a94100faSBill Paul 		return;
2691a94100faSBill Paul 
2692a94100faSBill Paul 	mii_mediachg(mii);
2693a94100faSBill Paul 
269419ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2695a94100faSBill Paul 
269613f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
269713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2698a94100faSBill Paul 
2699351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
27001d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2701d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2702a94100faSBill Paul }
2703a94100faSBill Paul 
2704a94100faSBill Paul /*
2705a94100faSBill Paul  * Set media options.
2706a94100faSBill Paul  */
2707a94100faSBill Paul static int
27087b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
2709a94100faSBill Paul {
2710a94100faSBill Paul 	struct rl_softc		*sc;
2711a94100faSBill Paul 	struct mii_data		*mii;
27126f0f9b12SPyun YongHyeon 	int			error;
2713a94100faSBill Paul 
2714a94100faSBill Paul 	sc = ifp->if_softc;
2715a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2716d1754a9bSJohn Baldwin 	RL_LOCK(sc);
27176f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
2718d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2719a94100faSBill Paul 
27206f0f9b12SPyun YongHyeon 	return (error);
2721a94100faSBill Paul }
2722a94100faSBill Paul 
2723a94100faSBill Paul /*
2724a94100faSBill Paul  * Report current media status.
2725a94100faSBill Paul  */
2726a94100faSBill Paul static void
27277b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2728a94100faSBill Paul {
2729a94100faSBill Paul 	struct rl_softc		*sc;
2730a94100faSBill Paul 	struct mii_data		*mii;
2731a94100faSBill Paul 
2732a94100faSBill Paul 	sc = ifp->if_softc;
2733a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2734a94100faSBill Paul 
2735d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2736a94100faSBill Paul 	mii_pollstat(mii);
2737d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2738a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2739a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2740a94100faSBill Paul }
2741a94100faSBill Paul 
2742a94100faSBill Paul static int
27437b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2744a94100faSBill Paul {
2745a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2746a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2747a94100faSBill Paul 	struct mii_data		*mii;
274840929967SGleb Smirnoff 	int			error = 0;
2749a94100faSBill Paul 
2750a94100faSBill Paul 	switch (command) {
2751a94100faSBill Paul 	case SIOCSIFMTU:
2752c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2753a94100faSBill Paul 			error = EINVAL;
2754c1d0b573SPyun YongHyeon 			break;
2755c1d0b573SPyun YongHyeon 		}
2756351a76f9SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_NOJUMBO) != 0 &&
2757c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2758c1d0b573SPyun YongHyeon 			error = EINVAL;
2759c1d0b573SPyun YongHyeon 			break;
2760c1d0b573SPyun YongHyeon 		}
2761c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2762c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2763a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2764ae644087SPyun YongHyeon 		if (ifp->if_mtu > RL_TSO_MTU &&
2765ae644087SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2766ae644087SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_TSO4;
2767ae644087SPyun YongHyeon 			ifp->if_hwassist &= ~CSUM_TSO;
2768ae644087SPyun YongHyeon 		}
2769d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2770a94100faSBill Paul 		break;
2771a94100faSBill Paul 	case SIOCSIFFLAGS:
277297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2773eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2774eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2775eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
27763021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
2777ff191365SJung-uk Kim 					re_set_rxmode(sc);
2778eed497bbSPyun YongHyeon 			} else
277997b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2780eed497bbSPyun YongHyeon 		} else {
2781eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2782a94100faSBill Paul 				re_stop(sc);
2783eed497bbSPyun YongHyeon 		}
2784eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
278597b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2786a94100faSBill Paul 		break;
2787a94100faSBill Paul 	case SIOCADDMULTI:
2788a94100faSBill Paul 	case SIOCDELMULTI:
278997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2790ff191365SJung-uk Kim 		re_set_rxmode(sc);
279197b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2792a94100faSBill Paul 		break;
2793a94100faSBill Paul 	case SIOCGIFMEDIA:
2794a94100faSBill Paul 	case SIOCSIFMEDIA:
2795a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2796a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2797a94100faSBill Paul 		break;
2798a94100faSBill Paul 	case SIOCSIFCAP:
279940929967SGleb Smirnoff 	    {
2800f051cb85SGleb Smirnoff 		int mask, reinit;
2801f051cb85SGleb Smirnoff 
2802f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2803f051cb85SGleb Smirnoff 		reinit = 0;
280440929967SGleb Smirnoff #ifdef DEVICE_POLLING
280540929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
280640929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
280740929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
280840929967SGleb Smirnoff 				if (error)
280940929967SGleb Smirnoff 					return(error);
2810d1754a9bSJohn Baldwin 				RL_LOCK(sc);
281140929967SGleb Smirnoff 				/* Disable interrupts */
281240929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
281340929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
281440929967SGleb Smirnoff 				RL_UNLOCK(sc);
281540929967SGleb Smirnoff 			} else {
281640929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
281740929967SGleb Smirnoff 				/* Enable interrupts. */
281840929967SGleb Smirnoff 				RL_LOCK(sc);
281940929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
282040929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
282140929967SGleb Smirnoff 				RL_UNLOCK(sc);
282240929967SGleb Smirnoff 			}
282340929967SGleb Smirnoff 		}
282440929967SGleb Smirnoff #endif /* DEVICE_POLLING */
282540929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2826f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2827a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2828dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2829a94100faSBill Paul 			else
2830b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2831f051cb85SGleb Smirnoff 			reinit = 1;
283240929967SGleb Smirnoff 		}
2833f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2834f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2835f051cb85SGleb Smirnoff 			reinit = 1;
2836f051cb85SGleb Smirnoff 		}
2837dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2838dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2839dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2840dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2841dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2842dc74159dSPyun YongHyeon 			else
2843dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2844ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
2845ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
2846ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
2847ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2848ae644087SPyun YongHyeon 			}
2849dc74159dSPyun YongHyeon 		}
28507467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
28517467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
28527467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
28537467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
28547467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
28557467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
28567467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
28577467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
28587467bd53SPyun YongHyeon 		}
2859f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2860f051cb85SGleb Smirnoff 			re_init(sc);
2861960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
286240929967SGleb Smirnoff 	    }
2863a94100faSBill Paul 		break;
2864a94100faSBill Paul 	default:
2865a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2866a94100faSBill Paul 		break;
2867a94100faSBill Paul 	}
2868a94100faSBill Paul 
2869a94100faSBill Paul 	return (error);
2870a94100faSBill Paul }
2871a94100faSBill Paul 
2872a94100faSBill Paul static void
28737b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
28741d545c7aSMarius Strobl {
2875130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
2876a94100faSBill Paul 
28771d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
28781d545c7aSMarius Strobl 
28791d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
28801d545c7aSMarius Strobl 		return;
28811d545c7aSMarius Strobl 
2882130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
2883a94100faSBill Paul 	re_txeof(sc);
2884130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
2885130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
2886130b6dfbSPyun YongHyeon 		    "-- recovering\n");
2887130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2888130b6dfbSPyun YongHyeon 			taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2889130b6dfbSPyun YongHyeon 		return;
2890130b6dfbSPyun YongHyeon 	}
2891130b6dfbSPyun YongHyeon 
2892130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
2893130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
2894130b6dfbSPyun YongHyeon 
28951abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
289697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2897130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2898130b6dfbSPyun YongHyeon 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2899a94100faSBill Paul }
2900a94100faSBill Paul 
2901a94100faSBill Paul /*
2902a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2903a94100faSBill Paul  * RX and TX lists.
2904a94100faSBill Paul  */
2905a94100faSBill Paul static void
29067b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
2907a94100faSBill Paul {
29080ce0868aSPyun YongHyeon 	int			i;
2909a94100faSBill Paul 	struct ifnet		*ifp;
2910d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2911d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2912a94100faSBill Paul 
291397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
291497b9d4baSJohn-Mark Gurney 
2915fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2916a94100faSBill Paul 
29171d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2918d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
291913f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2920a94100faSBill Paul 
2921ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
2922ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
2923ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
2924ead8fc66SPyun YongHyeon 	else
2925a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2926ead8fc66SPyun YongHyeon 	DELAY(1000);
2927a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2928ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2929a94100faSBill Paul 
2930a94100faSBill Paul 	if (sc->rl_head != NULL) {
2931a94100faSBill Paul 		m_freem(sc->rl_head);
2932a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2933a94100faSBill Paul 	}
2934a94100faSBill Paul 
2935a94100faSBill Paul 	/* Free the TX list buffers. */
2936a94100faSBill Paul 
2937d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2938d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2939d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2940d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2941d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2942d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2943d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2944d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2945d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2946a94100faSBill Paul 		}
2947a94100faSBill Paul 	}
2948a94100faSBill Paul 
2949a94100faSBill Paul 	/* Free the RX list buffers. */
2950a94100faSBill Paul 
2951d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2952d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2953d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2954d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2955d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2956d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2957d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2958d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2959d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2960a94100faSBill Paul 		}
2961a94100faSBill Paul 	}
2962a94100faSBill Paul }
2963a94100faSBill Paul 
2964a94100faSBill Paul /*
2965a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2966a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2967a94100faSBill Paul  * resume.
2968a94100faSBill Paul  */
2969a94100faSBill Paul static int
29707b5ffebfSPyun YongHyeon re_suspend(device_t dev)
2971a94100faSBill Paul {
2972a94100faSBill Paul 	struct rl_softc		*sc;
2973a94100faSBill Paul 
2974a94100faSBill Paul 	sc = device_get_softc(dev);
2975a94100faSBill Paul 
297697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2977a94100faSBill Paul 	re_stop(sc);
29787467bd53SPyun YongHyeon 	re_setwol(sc);
2979a94100faSBill Paul 	sc->suspended = 1;
298097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2981a94100faSBill Paul 
2982a94100faSBill Paul 	return (0);
2983a94100faSBill Paul }
2984a94100faSBill Paul 
2985a94100faSBill Paul /*
2986a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2987a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2988a94100faSBill Paul  * appropriate.
2989a94100faSBill Paul  */
2990a94100faSBill Paul static int
29917b5ffebfSPyun YongHyeon re_resume(device_t dev)
2992a94100faSBill Paul {
2993a94100faSBill Paul 	struct rl_softc		*sc;
2994a94100faSBill Paul 	struct ifnet		*ifp;
2995a94100faSBill Paul 
2996a94100faSBill Paul 	sc = device_get_softc(dev);
299797b9d4baSJohn-Mark Gurney 
299897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
299997b9d4baSJohn-Mark Gurney 
3000fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
300161f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
300261f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
300361f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
300461f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
300561f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
300661f45a72SPyun YongHyeon 	}
3007a94100faSBill Paul 
3008a94100faSBill Paul 	/* reinitialize interface if necessary */
3009a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
301097b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
3011a94100faSBill Paul 
30127467bd53SPyun YongHyeon 	/*
30137467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
30147467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
30157467bd53SPyun YongHyeon 	 */
30167467bd53SPyun YongHyeon 	re_clrwol(sc);
3017a94100faSBill Paul 	sc->suspended = 0;
301897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3019a94100faSBill Paul 
3020a94100faSBill Paul 	return (0);
3021a94100faSBill Paul }
3022a94100faSBill Paul 
3023a94100faSBill Paul /*
3024a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3025a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3026a94100faSBill Paul  */
30276a087a87SPyun YongHyeon static int
30287b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3029a94100faSBill Paul {
3030a94100faSBill Paul 	struct rl_softc		*sc;
3031a94100faSBill Paul 
3032a94100faSBill Paul 	sc = device_get_softc(dev);
3033a94100faSBill Paul 
303497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3035a94100faSBill Paul 	re_stop(sc);
3036536fde34SMaxim Sobolev 	/*
3037536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3038536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
303972293673SRuslan Ermilov 	 * cases.
3040536fde34SMaxim Sobolev 	 */
3041536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
30427467bd53SPyun YongHyeon 	re_setwol(sc);
304397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
30446a087a87SPyun YongHyeon 
30456a087a87SPyun YongHyeon 	return (0);
3046a94100faSBill Paul }
30477467bd53SPyun YongHyeon 
30487467bd53SPyun YongHyeon static void
30497b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
30507467bd53SPyun YongHyeon {
30517467bd53SPyun YongHyeon 	struct ifnet		*ifp;
30527467bd53SPyun YongHyeon 	int			pmc;
30537467bd53SPyun YongHyeon 	uint16_t		pmstat;
30547467bd53SPyun YongHyeon 	uint8_t			v;
30557467bd53SPyun YongHyeon 
30567467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
30577467bd53SPyun YongHyeon 
30587467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
30597467bd53SPyun YongHyeon 		return;
30607467bd53SPyun YongHyeon 
30617467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
306261f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
306361f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
306461f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
306561f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
306661f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
306761f45a72SPyun YongHyeon 	}
3068886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3069886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3070886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
30717467bd53SPyun YongHyeon 	/* Enable config register write. */
30727467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
30737467bd53SPyun YongHyeon 
30747467bd53SPyun YongHyeon 	/* Enable PME. */
30757467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
30767467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
30777467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30787467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
30797467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
30807467bd53SPyun YongHyeon 
30817467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
30827467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
30837467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
30847467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
30857467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30867467bd53SPyun YongHyeon 
30877467bd53SPyun YongHyeon 	/* Config register write done. */
3088f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
30897467bd53SPyun YongHyeon 
30907467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30917467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30927467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30937467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
30947467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
30957467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
30967467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
30977467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
30987467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
30997467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
31007467bd53SPyun YongHyeon 
31017467bd53SPyun YongHyeon 	/*
31027467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
31037467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
31047467bd53SPyun YongHyeon 	 * needed.
31057467bd53SPyun YongHyeon 	 */
31067467bd53SPyun YongHyeon 
31077467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
31087467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
31097467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
31107467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
31117467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
31127467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
31137467bd53SPyun YongHyeon }
31147467bd53SPyun YongHyeon 
31157467bd53SPyun YongHyeon static void
31167b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
31177467bd53SPyun YongHyeon {
31187467bd53SPyun YongHyeon 	int			pmc;
31197467bd53SPyun YongHyeon 	uint8_t			v;
31207467bd53SPyun YongHyeon 
31217467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
31227467bd53SPyun YongHyeon 
31237467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
31247467bd53SPyun YongHyeon 		return;
31257467bd53SPyun YongHyeon 
31267467bd53SPyun YongHyeon 	/* Enable config register write. */
31277467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
31287467bd53SPyun YongHyeon 
31297467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
31307467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
31317467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
31327467bd53SPyun YongHyeon 
31337467bd53SPyun YongHyeon 	/* Config register write done. */
3134f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
31357467bd53SPyun YongHyeon 
31367467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
31377467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
31387467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
31397467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
31407467bd53SPyun YongHyeon }
3141