xref: /freebsd/sys/dev/re/if_re.c (revision 9dfcacbe2970666865e0bcb76eddb8139c926eaa)
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 
158a94100faSBill Paul /*
159a94100faSBill Paul  * Default to using PIO access for this driver.
160a94100faSBill Paul  */
161a94100faSBill Paul #define RE_USEIOSPACE
162a94100faSBill Paul 
1635774c5ffSPyun YongHyeon /* Tunables. */
1642000cf6cSPyun YongHyeon static int msi_disable = 1;
1655774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1665774c5ffSPyun YongHyeon 
167a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
168a94100faSBill Paul 
169a94100faSBill Paul /*
170a94100faSBill Paul  * Various supported device vendors/types and their names.
171a94100faSBill Paul  */
172a94100faSBill Paul static struct rl_type re_devs[] = {
1739dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17432aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1759dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
176a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1779dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
178ed510fb0SBill Paul 	    "RealTek 8101E PCIe 10/100baseTX" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
1809dfcacbeSPyun YongHyeon 	    "RealTek 8168/8168B/8111B PCIe Gigabit Ethernet" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
1829dfcacbeSPyun YongHyeon 	    "RealTek 8169/8169S/8169SB/8110S/8110SB Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1842ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
186ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
18826390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1899dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
190dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
191a94100faSBill Paul };
192a94100faSBill Paul 
193a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
194a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
195a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
196a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
197a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
198a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
199a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
200a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
201a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
202498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
203a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
20469a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
20569a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
206ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
207ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
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"},
212498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2131acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
214a94100faSBill Paul 	{ 0, 0, NULL }
215a94100faSBill Paul };
216a94100faSBill Paul 
217a94100faSBill Paul static int re_probe		(device_t);
218a94100faSBill Paul static int re_attach		(device_t);
219a94100faSBill Paul static int re_detach		(device_t);
220a94100faSBill Paul 
221d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
222a94100faSBill Paul 
223a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
224a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
225d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
226d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
227d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
228a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
229a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23122a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23222a11c96SJohn-Mark Gurney 				(struct mbuf *);
23322a11c96SJohn-Mark Gurney #endif
234ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
235a94100faSBill Paul static void re_txeof		(struct rl_softc *);
23697b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2370187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2380187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
23997b9d4baSJohn-Mark Gurney #endif
240ef544f63SPaolo Pisati static int re_intr		(void *);
241a94100faSBill Paul static void re_tick		(void *);
242ed510fb0SBill Paul static void re_tx_task		(void *, int);
243ed510fb0SBill Paul static void re_int_task		(void *, int);
244a94100faSBill Paul static void re_start		(struct ifnet *);
245a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
246a94100faSBill Paul static void re_init		(void *);
24797b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
248a94100faSBill Paul static void re_stop		(struct rl_softc *);
2491d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
250a94100faSBill Paul static int re_suspend		(device_t);
251a94100faSBill Paul static int re_resume		(device_t);
2526a087a87SPyun YongHyeon static int re_shutdown		(device_t);
253a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
254a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
255a94100faSBill Paul 
256a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
257a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
258ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
259a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
260a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
261a94100faSBill Paul 
262a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
263a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
264a94100faSBill Paul static void re_miibus_statchg	(device_t);
265a94100faSBill Paul 
266a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
267a94100faSBill Paul static void re_reset		(struct rl_softc *);
2687467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2697467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
270a94100faSBill Paul 
271ed510fb0SBill Paul #ifdef RE_DIAG
272a94100faSBill Paul static int re_diag		(struct rl_softc *);
273ed510fb0SBill Paul #endif
274a94100faSBill Paul 
275a94100faSBill Paul #ifdef RE_USEIOSPACE
276a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
277a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
278a94100faSBill Paul #else
279a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
280a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
281a94100faSBill Paul #endif
282a94100faSBill Paul 
283a94100faSBill Paul static device_method_t re_methods[] = {
284a94100faSBill Paul 	/* Device interface */
285a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
286a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
287a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
288a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
289a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
290a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
291a94100faSBill Paul 
292a94100faSBill Paul 	/* bus interface */
293a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
294a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
295a94100faSBill Paul 
296a94100faSBill Paul 	/* MII interface */
297a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
298a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
299a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
300a94100faSBill Paul 
301a94100faSBill Paul 	{ 0, 0 }
302a94100faSBill Paul };
303a94100faSBill Paul 
304a94100faSBill Paul static driver_t re_driver = {
305a94100faSBill Paul 	"re",
306a94100faSBill Paul 	re_methods,
307a94100faSBill Paul 	sizeof(struct rl_softc)
308a94100faSBill Paul };
309a94100faSBill Paul 
310a94100faSBill Paul static devclass_t re_devclass;
311a94100faSBill Paul 
312a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
313347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
314a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
315a94100faSBill Paul 
316a94100faSBill Paul #define EE_SET(x)					\
317a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
318a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
319a94100faSBill Paul 
320a94100faSBill Paul #define EE_CLR(x)					\
321a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
322a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
323a94100faSBill Paul 
324a94100faSBill Paul /*
325a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
326a94100faSBill Paul  */
327a94100faSBill Paul static void
328a94100faSBill Paul re_eeprom_putbyte(sc, addr)
329a94100faSBill Paul 	struct rl_softc		*sc;
330a94100faSBill Paul 	int			addr;
331a94100faSBill Paul {
332a94100faSBill Paul 	register int		d, i;
333a94100faSBill Paul 
334ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
335a94100faSBill Paul 
336a94100faSBill Paul 	/*
337a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
338a94100faSBill Paul 	 */
339ed510fb0SBill Paul 
340ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
341a94100faSBill Paul 		if (d & i) {
342a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
343a94100faSBill Paul 		} else {
344a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
345a94100faSBill Paul 		}
346a94100faSBill Paul 		DELAY(100);
347a94100faSBill Paul 		EE_SET(RL_EE_CLK);
348a94100faSBill Paul 		DELAY(150);
349a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
350a94100faSBill Paul 		DELAY(100);
351a94100faSBill Paul 	}
352ed510fb0SBill Paul 
353ed510fb0SBill Paul 	return;
354a94100faSBill Paul }
355a94100faSBill Paul 
356a94100faSBill Paul /*
357a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
358a94100faSBill Paul  */
359a94100faSBill Paul static void
360a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
361a94100faSBill Paul 	struct rl_softc		*sc;
362a94100faSBill Paul 	int			addr;
363a94100faSBill Paul 	u_int16_t		*dest;
364a94100faSBill Paul {
365a94100faSBill Paul 	register int		i;
366a94100faSBill Paul 	u_int16_t		word = 0;
367a94100faSBill Paul 
368a94100faSBill Paul 	/*
369a94100faSBill Paul 	 * Send address of word we want to read.
370a94100faSBill Paul 	 */
371a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
372a94100faSBill Paul 
373a94100faSBill Paul 	/*
374a94100faSBill Paul 	 * Start reading bits from EEPROM.
375a94100faSBill Paul 	 */
376a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
377a94100faSBill Paul 		EE_SET(RL_EE_CLK);
378a94100faSBill Paul 		DELAY(100);
379a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
380a94100faSBill Paul 			word |= i;
381a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
382a94100faSBill Paul 		DELAY(100);
383a94100faSBill Paul 	}
384a94100faSBill Paul 
385a94100faSBill Paul 	*dest = word;
386ed510fb0SBill Paul 
387ed510fb0SBill Paul 	return;
388a94100faSBill Paul }
389a94100faSBill Paul 
390a94100faSBill Paul /*
391a94100faSBill Paul  * Read a sequence of words from the EEPROM.
392a94100faSBill Paul  */
393a94100faSBill Paul static void
394ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
395a94100faSBill Paul 	struct rl_softc		*sc;
396a94100faSBill Paul 	caddr_t			dest;
397a94100faSBill Paul 	int			off;
398a94100faSBill Paul 	int			cnt;
399a94100faSBill Paul {
400a94100faSBill Paul 	int			i;
401a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
402a94100faSBill Paul 
403ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
404ed510fb0SBill Paul 
405ed510fb0SBill Paul         DELAY(100);
406ed510fb0SBill Paul 
407a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
408ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
409a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
410ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
411a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
412be099007SPyun YongHyeon                 *ptr = word;
413a94100faSBill Paul 	}
414ed510fb0SBill Paul 
415ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
416ed510fb0SBill Paul 
417ed510fb0SBill Paul 	return;
418a94100faSBill Paul }
419a94100faSBill Paul 
420a94100faSBill Paul static int
421a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
422a94100faSBill Paul 	device_t		dev;
423a94100faSBill Paul 	int			phy, reg;
424a94100faSBill Paul {
425a94100faSBill Paul 	struct rl_softc		*sc;
426a94100faSBill Paul 	u_int32_t		rval;
427a94100faSBill Paul 	int			i;
428a94100faSBill Paul 
429a94100faSBill Paul 	if (phy != 1)
430a94100faSBill Paul 		return (0);
431a94100faSBill Paul 
432a94100faSBill Paul 	sc = device_get_softc(dev);
433a94100faSBill Paul 
4349bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4359bac70b8SBill Paul 
4369bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4379bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4389bac70b8SBill Paul 		return (rval);
4399bac70b8SBill Paul 	}
4409bac70b8SBill Paul 
441a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
442a94100faSBill Paul 	DELAY(1000);
443a94100faSBill Paul 
444a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
445a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
446a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
447a94100faSBill Paul 			break;
448a94100faSBill Paul 		DELAY(100);
449a94100faSBill Paul 	}
450a94100faSBill Paul 
451a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4526b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
453a94100faSBill Paul 		return (0);
454a94100faSBill Paul 	}
455a94100faSBill Paul 
456a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
457a94100faSBill Paul }
458a94100faSBill Paul 
459a94100faSBill Paul static int
460a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
461a94100faSBill Paul 	device_t		dev;
462a94100faSBill Paul 	int			phy, reg, data;
463a94100faSBill Paul {
464a94100faSBill Paul 	struct rl_softc		*sc;
465a94100faSBill Paul 	u_int32_t		rval;
466a94100faSBill Paul 	int			i;
467a94100faSBill Paul 
468a94100faSBill Paul 	sc = device_get_softc(dev);
469a94100faSBill Paul 
470a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4719bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
472a94100faSBill Paul 	DELAY(1000);
473a94100faSBill Paul 
474a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
475a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
476a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
477a94100faSBill Paul 			break;
478a94100faSBill Paul 		DELAY(100);
479a94100faSBill Paul 	}
480a94100faSBill Paul 
481a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4826b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
483a94100faSBill Paul 		return (0);
484a94100faSBill Paul 	}
485a94100faSBill Paul 
486a94100faSBill Paul 	return (0);
487a94100faSBill Paul }
488a94100faSBill Paul 
489a94100faSBill Paul static int
490a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
491a94100faSBill Paul 	device_t		dev;
492a94100faSBill Paul 	int			phy, reg;
493a94100faSBill Paul {
494a94100faSBill Paul 	struct rl_softc		*sc;
495a94100faSBill Paul 	u_int16_t		rval = 0;
496a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
497a94100faSBill Paul 
498a94100faSBill Paul 	sc = device_get_softc(dev);
499a94100faSBill Paul 
500a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
501a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
502a94100faSBill Paul 		return (rval);
503a94100faSBill Paul 	}
504a94100faSBill Paul 
505a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
506a94100faSBill Paul 	if (phy) {
507a94100faSBill Paul 		return (0);
508a94100faSBill Paul 	}
509a94100faSBill Paul 	switch (reg) {
510a94100faSBill Paul 	case MII_BMCR:
511a94100faSBill Paul 		re8139_reg = RL_BMCR;
512a94100faSBill Paul 		break;
513a94100faSBill Paul 	case MII_BMSR:
514a94100faSBill Paul 		re8139_reg = RL_BMSR;
515a94100faSBill Paul 		break;
516a94100faSBill Paul 	case MII_ANAR:
517a94100faSBill Paul 		re8139_reg = RL_ANAR;
518a94100faSBill Paul 		break;
519a94100faSBill Paul 	case MII_ANER:
520a94100faSBill Paul 		re8139_reg = RL_ANER;
521a94100faSBill Paul 		break;
522a94100faSBill Paul 	case MII_ANLPAR:
523a94100faSBill Paul 		re8139_reg = RL_LPAR;
524a94100faSBill Paul 		break;
525a94100faSBill Paul 	case MII_PHYIDR1:
526a94100faSBill Paul 	case MII_PHYIDR2:
527a94100faSBill Paul 		return (0);
528a94100faSBill Paul 	/*
529a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
530a94100faSBill Paul 	 * register. If we have a link partner which does not
531a94100faSBill Paul 	 * support NWAY, this is the register which will tell
532a94100faSBill Paul 	 * us the results of parallel detection.
533a94100faSBill Paul 	 */
534a94100faSBill Paul 	case RL_MEDIASTAT:
535a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
536a94100faSBill Paul 		return (rval);
537a94100faSBill Paul 	default:
5386b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
539a94100faSBill Paul 		return (0);
540a94100faSBill Paul 	}
541a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
542baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
543baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
544baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
545baa12772SPyun YongHyeon 	}
546a94100faSBill Paul 	return (rval);
547a94100faSBill Paul }
548a94100faSBill Paul 
549a94100faSBill Paul static int
550a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
551a94100faSBill Paul 	device_t		dev;
552a94100faSBill Paul 	int			phy, reg, data;
553a94100faSBill Paul {
554a94100faSBill Paul 	struct rl_softc		*sc;
555a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
556a94100faSBill Paul 	int			rval = 0;
557a94100faSBill Paul 
558a94100faSBill Paul 	sc = device_get_softc(dev);
559a94100faSBill Paul 
560a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
561a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
562a94100faSBill Paul 		return (rval);
563a94100faSBill Paul 	}
564a94100faSBill Paul 
565a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
56697b9d4baSJohn-Mark Gurney 	if (phy)
567a94100faSBill Paul 		return (0);
56897b9d4baSJohn-Mark Gurney 
569a94100faSBill Paul 	switch (reg) {
570a94100faSBill Paul 	case MII_BMCR:
571a94100faSBill Paul 		re8139_reg = RL_BMCR;
572baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
573baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
574baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
575baa12772SPyun YongHyeon 		}
576a94100faSBill Paul 		break;
577a94100faSBill Paul 	case MII_BMSR:
578a94100faSBill Paul 		re8139_reg = RL_BMSR;
579a94100faSBill Paul 		break;
580a94100faSBill Paul 	case MII_ANAR:
581a94100faSBill Paul 		re8139_reg = RL_ANAR;
582a94100faSBill Paul 		break;
583a94100faSBill Paul 	case MII_ANER:
584a94100faSBill Paul 		re8139_reg = RL_ANER;
585a94100faSBill Paul 		break;
586a94100faSBill Paul 	case MII_ANLPAR:
587a94100faSBill Paul 		re8139_reg = RL_LPAR;
588a94100faSBill Paul 		break;
589a94100faSBill Paul 	case MII_PHYIDR1:
590a94100faSBill Paul 	case MII_PHYIDR2:
591a94100faSBill Paul 		return (0);
592a94100faSBill Paul 		break;
593a94100faSBill Paul 	default:
5946b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
595a94100faSBill Paul 		return (0);
596a94100faSBill Paul 	}
597a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
598a94100faSBill Paul 	return (0);
599a94100faSBill Paul }
600a94100faSBill Paul 
601a94100faSBill Paul static void
602a94100faSBill Paul re_miibus_statchg(dev)
603a94100faSBill Paul 	device_t		dev;
604a94100faSBill Paul {
605a11e2f18SBruce M Simpson 
606a94100faSBill Paul }
607a94100faSBill Paul 
608a94100faSBill Paul /*
609a94100faSBill Paul  * Program the 64-bit multicast hash filter.
610a94100faSBill Paul  */
611a94100faSBill Paul static void
612a94100faSBill Paul re_setmulti(sc)
613a94100faSBill Paul 	struct rl_softc		*sc;
614a94100faSBill Paul {
615a94100faSBill Paul 	struct ifnet		*ifp;
616a94100faSBill Paul 	int			h = 0;
617a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
618a94100faSBill Paul 	struct ifmultiaddr	*ifma;
619a94100faSBill Paul 	u_int32_t		rxfilt;
620a94100faSBill Paul 	int			mcnt = 0;
621bb7dfefbSBill Paul 	u_int32_t		hwrev;
622a94100faSBill Paul 
62397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
62497b9d4baSJohn-Mark Gurney 
625fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
626a94100faSBill Paul 
627a94100faSBill Paul 
6287c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6297c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
630a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & 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;
639a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
640a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
641a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
642a94100faSBill Paul 		return;
643a94100faSBill Paul 	}
644a94100faSBill Paul 
645a94100faSBill Paul 	/* first, zot all the existing hash bits */
646a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
647a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
648a94100faSBill Paul 
649a94100faSBill Paul 	/* now program new ones */
65013b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
651a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
652a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
653a94100faSBill Paul 			continue;
6540e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6550e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
656a94100faSBill Paul 		if (h < 32)
657a94100faSBill Paul 			hashes[0] |= (1 << h);
658a94100faSBill Paul 		else
659a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
660a94100faSBill Paul 		mcnt++;
661a94100faSBill Paul 	}
66213b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
663a94100faSBill Paul 
664a94100faSBill Paul 	if (mcnt)
665a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
666a94100faSBill Paul 	else
667a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
668a94100faSBill Paul 
669a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
670bb7dfefbSBill Paul 
671bb7dfefbSBill Paul 	/*
672bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
673bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
674bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
675bb7dfefbSBill Paul 	 * order for those devices.
676bb7dfefbSBill Paul 	 */
677bb7dfefbSBill Paul 
678bb7dfefbSBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
679bb7dfefbSBill Paul 
6801acbb78aSPyun YongHyeon 	switch (hwrev) {
6811acbb78aSPyun YongHyeon 	case RL_HWREV_8100E:
6821acbb78aSPyun YongHyeon 	case RL_HWREV_8101E:
6831acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
6841acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
6851acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
686bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
687bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
6881acbb78aSPyun YongHyeon 		break;
6891acbb78aSPyun YongHyeon 	default:
690a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
691a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
6921acbb78aSPyun YongHyeon 		break;
693a94100faSBill Paul 	}
694bb7dfefbSBill Paul }
695a94100faSBill Paul 
696a94100faSBill Paul static void
697a94100faSBill Paul re_reset(sc)
698a94100faSBill Paul 	struct rl_softc		*sc;
699a94100faSBill Paul {
700a94100faSBill Paul 	register int		i;
701a94100faSBill Paul 
70297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
70397b9d4baSJohn-Mark Gurney 
704a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
705a94100faSBill Paul 
706a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
707a94100faSBill Paul 		DELAY(10);
708a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
709a94100faSBill Paul 			break;
710a94100faSBill Paul 	}
711a94100faSBill Paul 	if (i == RL_TIMEOUT)
7126b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
713a94100faSBill Paul 
714a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
715a94100faSBill Paul }
716a94100faSBill Paul 
717ed510fb0SBill Paul #ifdef RE_DIAG
718ed510fb0SBill Paul 
719a94100faSBill Paul /*
720a94100faSBill Paul  * The following routine is designed to test for a defect on some
721a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
722a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
723a94100faSBill Paul  * should be pulled high. The result of this defect is that the
724a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
725a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
726a94100faSBill Paul  * because the 64-bit data lines aren't connected.
727a94100faSBill Paul  *
728a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
729a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
730a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
731a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
732a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
733a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
734a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
735a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
736a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
737a94100faSBill Paul  */
738a94100faSBill Paul 
739a94100faSBill Paul static int
740a94100faSBill Paul re_diag(sc)
741a94100faSBill Paul 	struct rl_softc		*sc;
742a94100faSBill Paul {
743fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
744a94100faSBill Paul 	struct mbuf		*m0;
745a94100faSBill Paul 	struct ether_header	*eh;
746a94100faSBill Paul 	struct rl_desc		*cur_rx;
747a94100faSBill Paul 	u_int16_t		status;
748a94100faSBill Paul 	u_int32_t		rxstat;
749ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
750a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
751a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
752a94100faSBill Paul 
753a94100faSBill Paul 	/* Allocate a single mbuf */
754a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
755a94100faSBill Paul 	if (m0 == NULL)
756a94100faSBill Paul 		return (ENOBUFS);
757a94100faSBill Paul 
75897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
75997b9d4baSJohn-Mark Gurney 
760a94100faSBill Paul 	/*
761a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
762a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
763a94100faSBill Paul 	 * following special functions:
764a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
765a94100faSBill Paul 	 * - Enables digital loopback mode
766a94100faSBill Paul 	 * - Leaves interrupts turned off
767a94100faSBill Paul 	 */
768a94100faSBill Paul 
769a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
770a94100faSBill Paul 	sc->rl_testmode = 1;
771ed510fb0SBill Paul 	re_reset(sc);
77297b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
773ed510fb0SBill Paul 	sc->rl_link = 1;
774ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
775ed510fb0SBill Paul 		phyaddr = 1;
776ed510fb0SBill Paul 	else
777ed510fb0SBill Paul 		phyaddr = 0;
778ed510fb0SBill Paul 
779ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
780ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
781ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
782ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
783ed510fb0SBill Paul 			break;
784ed510fb0SBill Paul 	}
785ed510fb0SBill Paul 
786ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
787ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
788ed510fb0SBill Paul 
789804af9a1SBill Paul 	DELAY(100000);
790a94100faSBill Paul 
791a94100faSBill Paul 	/* Put some data in the mbuf */
792a94100faSBill Paul 
793a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
794a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
795a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
796a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
797a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
798a94100faSBill Paul 
7997cae6651SBill Paul 	/*
8007cae6651SBill Paul 	 * Queue the packet, start transmission.
8017cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8027cae6651SBill Paul 	 */
803a94100faSBill Paul 
804abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
80597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
80652732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8077cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
80897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
809a94100faSBill Paul 	m0 = NULL;
810a94100faSBill Paul 
811a94100faSBill Paul 	/* Wait for it to propagate through the chip */
812a94100faSBill Paul 
813abc8ff44SBill Paul 	DELAY(100000);
814a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
815a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
816ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
817abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
818abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
819a94100faSBill Paul 			break;
820a94100faSBill Paul 		DELAY(10);
821a94100faSBill Paul 	}
822a94100faSBill Paul 
823a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8246b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8256b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8266b9f5c94SGleb Smirnoff 		    " loopback mode\n");
827a94100faSBill Paul 		error = EIO;
828a94100faSBill Paul 		goto done;
829a94100faSBill Paul 	}
830a94100faSBill Paul 
831a94100faSBill Paul 	/*
832a94100faSBill Paul 	 * The packet should have been dumped into the first
833a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
834a94100faSBill Paul 	 */
835a94100faSBill Paul 
836a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
837a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
838a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
839d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
840d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
841d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
842d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
843d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
844a94100faSBill Paul 
845d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
846d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
847a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
848a94100faSBill Paul 
849a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
850a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
851a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
852a94100faSBill Paul 
853a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8546b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8556b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
856a94100faSBill Paul 		error = EIO;
857a94100faSBill Paul 		goto done;
858a94100faSBill Paul 	}
859a94100faSBill Paul 
860a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
861a94100faSBill Paul 
862a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
863a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
864a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8656b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8666b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
867a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8686b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
869a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
870a94100faSBill Paul 		    ntohs(eh->ether_type));
8716b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8726b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8736b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8746b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8756b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8766b9f5c94SGleb Smirnoff 		    "details.\n");
877a94100faSBill Paul 		error = EIO;
878a94100faSBill Paul 	}
879a94100faSBill Paul 
880a94100faSBill Paul done:
881a94100faSBill Paul 	/* Turn interface off, release resources */
882a94100faSBill Paul 
883a94100faSBill Paul 	sc->rl_testmode = 0;
884ed510fb0SBill Paul 	sc->rl_link = 0;
885a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
886a94100faSBill Paul 	re_stop(sc);
887a94100faSBill Paul 	if (m0 != NULL)
888a94100faSBill Paul 		m_freem(m0);
889a94100faSBill Paul 
89097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
89197b9d4baSJohn-Mark Gurney 
892a94100faSBill Paul 	return (error);
893a94100faSBill Paul }
894a94100faSBill Paul 
895ed510fb0SBill Paul #endif
896ed510fb0SBill Paul 
897a94100faSBill Paul /*
898a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
899a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
900a94100faSBill Paul  */
901a94100faSBill Paul static int
902a94100faSBill Paul re_probe(dev)
903a94100faSBill Paul 	device_t		dev;
904a94100faSBill Paul {
905a94100faSBill Paul 	struct rl_type		*t;
906dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
907dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
908dfdb409eSPyun YongHyeon 	int			i;
909a94100faSBill Paul 
910dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
911dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
912dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
913dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
914a94100faSBill Paul 
915dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
916dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
91726390635SJohn Baldwin 			/*
91826390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
919dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
92026390635SJohn Baldwin 			 */
921a94100faSBill Paul 			return (ENXIO);
922a94100faSBill Paul 		}
923dfdb409eSPyun YongHyeon 	}
924dfdb409eSPyun YongHyeon 
925dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
926dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
927dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
928dfdb409eSPyun YongHyeon 			return (ENXIO);
929dfdb409eSPyun YongHyeon 		}
930dfdb409eSPyun YongHyeon 	}
931dfdb409eSPyun YongHyeon 
932dfdb409eSPyun YongHyeon 	t = re_devs;
933dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
934dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
935a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
936d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
937a94100faSBill Paul 		}
938a94100faSBill Paul 	}
939a94100faSBill Paul 
940a94100faSBill Paul 	return (ENXIO);
941a94100faSBill Paul }
942a94100faSBill Paul 
943a94100faSBill Paul /*
944a94100faSBill Paul  * Map a single buffer address.
945a94100faSBill Paul  */
946a94100faSBill Paul 
947a94100faSBill Paul static void
948a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
949a94100faSBill Paul 	void			*arg;
950a94100faSBill Paul 	bus_dma_segment_t	*segs;
951a94100faSBill Paul 	int			nseg;
952a94100faSBill Paul 	int			error;
953a94100faSBill Paul {
9548fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
955a94100faSBill Paul 
956a94100faSBill Paul 	if (error)
957a94100faSBill Paul 		return;
958a94100faSBill Paul 
959a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
960a94100faSBill Paul 	addr = arg;
961a94100faSBill Paul 	*addr = segs->ds_addr;
962a94100faSBill Paul }
963a94100faSBill Paul 
964a94100faSBill Paul static int
965a94100faSBill Paul re_allocmem(dev, sc)
966a94100faSBill Paul 	device_t		dev;
967a94100faSBill Paul 	struct rl_softc		*sc;
968a94100faSBill Paul {
969d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
970a94100faSBill Paul 	int			error;
971a94100faSBill Paul 	int			i;
972a94100faSBill Paul 
973d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
974d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
975d65abd66SPyun YongHyeon 
976d65abd66SPyun YongHyeon 	/*
977d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
978ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
979ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
980ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
981ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
982ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
983d65abd66SPyun YongHyeon 	 */
984d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
985ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
986d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
987d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
988d65abd66SPyun YongHyeon 	if (error) {
989d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
990d65abd66SPyun YongHyeon 		return (error);
991d65abd66SPyun YongHyeon 	}
992d65abd66SPyun YongHyeon 
993d65abd66SPyun YongHyeon 	/*
994d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
995d65abd66SPyun YongHyeon 	 */
996d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
997d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
998d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
999d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1000d65abd66SPyun YongHyeon 	if (error) {
1001d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1002d65abd66SPyun YongHyeon 		return (error);
1003d65abd66SPyun YongHyeon 	}
1004d65abd66SPyun YongHyeon 
1005a94100faSBill Paul 	/*
1006a94100faSBill Paul 	 * Allocate map for RX mbufs.
1007a94100faSBill Paul 	 */
1008d65abd66SPyun YongHyeon 
1009d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1010d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1011d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1012a94100faSBill Paul 	if (error) {
1013d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1014d65abd66SPyun YongHyeon 		return (error);
1015a94100faSBill Paul 	}
1016a94100faSBill Paul 
1017a94100faSBill Paul 	/*
1018a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1019a94100faSBill Paul 	 */
1020a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1021a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1022d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1023a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1024a94100faSBill Paul 	if (error) {
1025d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1026d65abd66SPyun YongHyeon 		return (error);
1027a94100faSBill Paul 	}
1028a94100faSBill Paul 
1029a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1030a94100faSBill Paul 
1031a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1032d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1033d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1034a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1035d65abd66SPyun YongHyeon 	if (error) {
1036d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1037d65abd66SPyun YongHyeon 		return (error);
1038d65abd66SPyun YongHyeon 	}
1039a94100faSBill Paul 
1040a94100faSBill Paul 	/* Load the map for the TX ring. */
1041a94100faSBill Paul 
1042d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1043a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1044a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1045d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1046a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1047d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1048d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1049d65abd66SPyun YongHyeon 		return (ENOMEM);
1050d65abd66SPyun YongHyeon 	}
1051a94100faSBill Paul 
1052a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1053a94100faSBill Paul 
1054d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1055d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1056d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1057a94100faSBill Paul 		if (error) {
1058d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1059d65abd66SPyun YongHyeon 			return (error);
1060a94100faSBill Paul 		}
1061a94100faSBill Paul 	}
1062a94100faSBill Paul 
1063a94100faSBill Paul 	/*
1064a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1065a94100faSBill Paul 	 */
1066a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1067a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1068d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1069a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1070a94100faSBill Paul 	if (error) {
1071d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1072d65abd66SPyun YongHyeon 		return (error);
1073a94100faSBill Paul 	}
1074a94100faSBill Paul 
1075a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1076a94100faSBill Paul 
1077a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1078d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1079d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1080a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1081d65abd66SPyun YongHyeon 	if (error) {
1082d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1083d65abd66SPyun YongHyeon 		return (error);
1084d65abd66SPyun YongHyeon 	}
1085a94100faSBill Paul 
1086a94100faSBill Paul 	/* Load the map for the RX ring. */
1087a94100faSBill Paul 
1088d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1089a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1090a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1091d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1092a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1093d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1094d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1095d65abd66SPyun YongHyeon 		return (ENOMEM);
1096d65abd66SPyun YongHyeon 	}
1097a94100faSBill Paul 
1098a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1099a94100faSBill Paul 
1100d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1101d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1102a94100faSBill Paul 	if (error) {
1103d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1104d65abd66SPyun YongHyeon 		return (error);
1105d65abd66SPyun YongHyeon 	}
1106d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1107d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1108d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1109d65abd66SPyun YongHyeon 		if (error) {
1110d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1111d65abd66SPyun YongHyeon 			return (error);
1112a94100faSBill Paul 		}
1113a94100faSBill Paul 	}
1114a94100faSBill Paul 
1115a94100faSBill Paul 	return (0);
1116a94100faSBill Paul }
1117a94100faSBill Paul 
1118a94100faSBill Paul /*
1119a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1120a94100faSBill Paul  * setup and ethernet/BPF attach.
1121a94100faSBill Paul  */
1122a94100faSBill Paul static int
1123a94100faSBill Paul re_attach(dev)
1124a94100faSBill Paul 	device_t		dev;
1125a94100faSBill Paul {
1126a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1127be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1128a94100faSBill Paul 	struct rl_softc		*sc;
1129a94100faSBill Paul 	struct ifnet		*ifp;
1130a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1131a94100faSBill Paul 	int			hwrev;
1132a94100faSBill Paul 	u_int16_t		re_did = 0;
1133d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11345774c5ffSPyun YongHyeon 	int			msic, reg;
113503ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1136a94100faSBill Paul 
1137a94100faSBill Paul 	sc = device_get_softc(dev);
1138ed510fb0SBill Paul 	sc->rl_dev = dev;
1139a94100faSBill Paul 
1140a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
114197b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1142d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1143d1754a9bSJohn Baldwin 
1144a94100faSBill Paul 	/*
1145a94100faSBill Paul 	 * Map control/status registers.
1146a94100faSBill Paul 	 */
1147a94100faSBill Paul 	pci_enable_busmaster(dev);
1148a94100faSBill Paul 
1149a94100faSBill Paul 	rid = RL_RID;
11505f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
11515f96beb9SNate Lawson 	    RF_ACTIVE);
1152a94100faSBill Paul 
1153a94100faSBill Paul 	if (sc->rl_res == NULL) {
1154d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1155a94100faSBill Paul 		error = ENXIO;
1156a94100faSBill Paul 		goto fail;
1157a94100faSBill Paul 	}
1158a94100faSBill Paul 
1159a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1160a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1161a94100faSBill Paul 
11625774c5ffSPyun YongHyeon 	msic = 0;
11635774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
11645774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11655774c5ffSPyun YongHyeon 		if (bootverbose)
11665774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11675774c5ffSPyun YongHyeon 	}
11685774c5ffSPyun YongHyeon 	if (msic == RL_MSI_MESSAGES  && msi_disable == 0) {
11695774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11705774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11715774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11725774c5ffSPyun YongHyeon 				    msic);
11735774c5ffSPyun YongHyeon 				sc->rl_msi = 1;
1174339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1175339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1176339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1177339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1178339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1179339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, 0);
11805774c5ffSPyun YongHyeon 			} else
11815774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11825774c5ffSPyun YongHyeon 		}
11835774c5ffSPyun YongHyeon 	}
1184a94100faSBill Paul 
11855774c5ffSPyun YongHyeon 	/* Allocate interrupt */
11865774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
11875774c5ffSPyun YongHyeon 		rid = 0;
11885774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
11895774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
11905774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
11915774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1192a94100faSBill Paul 			error = ENXIO;
1193a94100faSBill Paul 			goto fail;
1194a94100faSBill Paul 		}
11955774c5ffSPyun YongHyeon 	} else {
11965774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
11975774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
11985774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
11995774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
12005774c5ffSPyun YongHyeon 				device_printf(dev,
12015774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12025774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12035774c5ffSPyun YongHyeon 				error = ENXIO;
12045774c5ffSPyun YongHyeon 				goto fail;
12055774c5ffSPyun YongHyeon 			}
12065774c5ffSPyun YongHyeon 		}
12075774c5ffSPyun YongHyeon 	}
1208a94100faSBill Paul 
1209a94100faSBill Paul 	/* Reset the adapter. */
121097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1211a94100faSBill Paul 	re_reset(sc);
121297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1213abc8ff44SBill Paul 
1214abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1215abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1216abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1217abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1218abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1219abc8ff44SBill Paul 			break;
1220abc8ff44SBill Paul 		}
1221abc8ff44SBill Paul 		hw_rev++;
1222abc8ff44SBill Paul 	}
1223d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1224d65abd66SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: %08x\n", hwrev);
1225d65abd66SPyun YongHyeon 		error = ENXIO;
1226d65abd66SPyun YongHyeon 		goto fail;
1227d65abd66SPyun YongHyeon 	}
1228abc8ff44SBill Paul 
1229141f92e7SPyun YongHyeon 	sc->rl_eewidth = RL_9356_ADDR_LEN;
1230ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1231a94100faSBill Paul 	if (re_did != 0x8129)
1232141f92e7SPyun YongHyeon 	        sc->rl_eewidth = RL_9346_ADDR_LEN;
1233a94100faSBill Paul 
1234a94100faSBill Paul 	/*
1235a94100faSBill Paul 	 * Get station address from the EEPROM.
1236a94100faSBill Paul 	 */
1237ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1238be099007SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1239be099007SPyun YongHyeon 		as[i] = le16toh(as[i]);
1240be099007SPyun YongHyeon 	bcopy(as, eaddr, sizeof(eaddr));
1241ed510fb0SBill Paul 
1242ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1243d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1244ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1245ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1246d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1247d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1248ed510fb0SBill Paul 	} else {
1249d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1250ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1251ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1252d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1253d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1254abc8ff44SBill Paul 	}
12559bac70b8SBill Paul 
1256a94100faSBill Paul 	error = re_allocmem(dev, sc);
1257a94100faSBill Paul 	if (error)
1258a94100faSBill Paul 		goto fail;
1259a94100faSBill Paul 
1260cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1261cd036ec1SBrooks Davis 	if (ifp == NULL) {
1262d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1263cd036ec1SBrooks Davis 		error = ENOSPC;
1264cd036ec1SBrooks Davis 		goto fail;
1265cd036ec1SBrooks Davis 	}
1266cd036ec1SBrooks Davis 
1267a94100faSBill Paul 	/* Do MII setup */
1268a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1269a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1270d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1271a94100faSBill Paul 		error = ENXIO;
1272a94100faSBill Paul 		goto fail;
1273a94100faSBill Paul 	}
1274a94100faSBill Paul 
1275c4aca09aSPyun YongHyeon 	/* Take PHY out of power down mode. */
1276c4aca09aSPyun YongHyeon 	if (sc->rl_type == RL_8169) {
1277c4aca09aSPyun YongHyeon 		uint32_t rev;
1278c4aca09aSPyun YongHyeon 
1279c4aca09aSPyun YongHyeon 		rev = CSR_READ_4(sc, RL_TXCFG);
1280c4aca09aSPyun YongHyeon 		/* HWVERID 0, 1 and 2 :  bit26-30, bit23 */
1281c4aca09aSPyun YongHyeon 		rev &= 0x7c800000;
1282c4aca09aSPyun YongHyeon 		if (rev != 0) {
1283c4aca09aSPyun YongHyeon 			/* RTL8169S single chip */
1284c4aca09aSPyun YongHyeon 			switch (rev) {
1285c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SB:
1286c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SC:
1287c4aca09aSPyun YongHyeon 			case RL_HWREV_8168_SPIN2:
12881acbb78aSPyun YongHyeon 			case RL_HWREV_8168_SPIN3:
1289c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x1f, 0);
1290c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x0e, 0);
1291c4aca09aSPyun YongHyeon 				break;
1292c4aca09aSPyun YongHyeon 			default:
1293c4aca09aSPyun YongHyeon 				break;
1294c4aca09aSPyun YongHyeon 			}
1295c4aca09aSPyun YongHyeon 		}
1296c4aca09aSPyun YongHyeon 	}
1297c4aca09aSPyun YongHyeon 
1298a94100faSBill Paul 	ifp->if_softc = sc;
12999bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1300a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1301a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1302a94100faSBill Paul 	ifp->if_start = re_start;
1303d65abd66SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO;
1304d65abd66SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1305498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1306a94100faSBill Paul 	ifp->if_init = re_init;
130752732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
130852732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
130952732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1310a94100faSBill Paul 
1311ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1312ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1313ed510fb0SBill Paul 
1314a94100faSBill Paul 	/*
1315a94100faSBill Paul 	 * Call MI attach routine.
1316a94100faSBill Paul 	 */
1317a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1318a94100faSBill Paul 
1319960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1320960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1321960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1322960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
13237467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
13247467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
13257467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1326960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1327960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1328960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1329960fd5b3SPyun YongHyeon #endif
1330960fd5b3SPyun YongHyeon 	/*
1331960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1332960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1333960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1334960fd5b3SPyun YongHyeon 	 */
1335960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1336960fd5b3SPyun YongHyeon 
1337ed510fb0SBill Paul #ifdef RE_DIAG
1338ed510fb0SBill Paul 	/*
1339ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1340ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1341ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1342ed510fb0SBill Paul 	 */
1343a94100faSBill Paul 
1344ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1345ed510fb0SBill Paul 		error = re_diag(sc);
1346a94100faSBill Paul 		if (error) {
1347ed510fb0SBill Paul 			device_printf(dev,
1348ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1349a94100faSBill Paul 			ether_ifdetach(ifp);
1350a94100faSBill Paul 			goto fail;
1351a94100faSBill Paul 		}
1352ed510fb0SBill Paul 	}
1353ed510fb0SBill Paul #endif
1354a94100faSBill Paul 
1355a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
13565774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0)
13575774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
13585774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
13595774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
13605774c5ffSPyun YongHyeon 	else {
13615774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
13625774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
13635774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
13645774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
13655774c5ffSPyun YongHyeon 			if (error != 0)
13665774c5ffSPyun YongHyeon 				break;
13675774c5ffSPyun YongHyeon 		}
13685774c5ffSPyun YongHyeon 	}
1369a94100faSBill Paul 	if (error) {
1370d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1371a94100faSBill Paul 		ether_ifdetach(ifp);
1372a94100faSBill Paul 	}
1373a94100faSBill Paul 
1374a94100faSBill Paul fail:
1375ed510fb0SBill Paul 
1376a94100faSBill Paul 	if (error)
1377a94100faSBill Paul 		re_detach(dev);
1378a94100faSBill Paul 
1379a94100faSBill Paul 	return (error);
1380a94100faSBill Paul }
1381a94100faSBill Paul 
1382a94100faSBill Paul /*
1383a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1384a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1385a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1386a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1387a94100faSBill Paul  * allocated.
1388a94100faSBill Paul  */
1389a94100faSBill Paul static int
1390a94100faSBill Paul re_detach(dev)
1391a94100faSBill Paul 	device_t		dev;
1392a94100faSBill Paul {
1393a94100faSBill Paul 	struct rl_softc		*sc;
1394a94100faSBill Paul 	struct ifnet		*ifp;
13955774c5ffSPyun YongHyeon 	int			i, rid;
1396a94100faSBill Paul 
1397a94100faSBill Paul 	sc = device_get_softc(dev);
1398fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1399aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
140097b9d4baSJohn-Mark Gurney 
140140929967SGleb Smirnoff #ifdef DEVICE_POLLING
140240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
140340929967SGleb Smirnoff 		ether_poll_deregister(ifp);
140440929967SGleb Smirnoff #endif
140597b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
1406525e6a87SRuslan Ermilov 	if (device_is_attached(dev)) {
140797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
140897b9d4baSJohn-Mark Gurney #if 0
140997b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
141097b9d4baSJohn-Mark Gurney #endif
1411a94100faSBill Paul 		re_stop(sc);
1412525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1413d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14143d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14153d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1416a94100faSBill Paul 		/*
1417a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1418a94100faSBill Paul 		 * still had a BPF descriptor attached to this
141997b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1420a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1421a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1422a94100faSBill Paul 		 * which will try to call re_init() again. This will
1423a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1424a94100faSBill Paul 		 * which will panic the system when the kernel tries
1425a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1426a94100faSBill Paul 		 * anymore.
1427a94100faSBill Paul 		 */
1428a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1429525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1430a94100faSBill Paul 	}
1431a94100faSBill Paul 	if (sc->rl_miibus)
1432a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1433a94100faSBill Paul 	bus_generic_detach(dev);
1434a94100faSBill Paul 
143597b9d4baSJohn-Mark Gurney 	/*
143697b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
143797b9d4baSJohn-Mark Gurney 	 * stopped here.
143897b9d4baSJohn-Mark Gurney 	 */
143997b9d4baSJohn-Mark Gurney 
14405774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
14415774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
14425774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
14435774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
14445774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
14455774c5ffSPyun YongHyeon 		}
14465774c5ffSPyun YongHyeon 	}
1447ad4f426eSWarner Losh 	if (ifp != NULL)
1448ad4f426eSWarner Losh 		if_free(ifp);
14495774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
14505774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
14515774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
14525774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
14535774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
14545774c5ffSPyun YongHyeon 		}
14555774c5ffSPyun YongHyeon 	} else {
14565774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
14575774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
14585774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
14595774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
14605774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
14615774c5ffSPyun YongHyeon 			}
14625774c5ffSPyun YongHyeon 		}
14635774c5ffSPyun YongHyeon 		pci_release_msi(dev);
14645774c5ffSPyun YongHyeon 	}
1465a94100faSBill Paul 	if (sc->rl_res)
1466a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1467a94100faSBill Paul 
1468a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1469a94100faSBill Paul 
1470a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1471a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1472a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1473a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1474a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1475a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1476a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1477a94100faSBill Paul 	}
1478a94100faSBill Paul 
1479a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1480a94100faSBill Paul 
1481a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1482a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1483a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1484a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1485a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1486a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1487a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1488a94100faSBill Paul 	}
1489a94100faSBill Paul 
1490a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1491a94100faSBill Paul 
1492d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1493d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1494d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1495d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1496d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1497d65abd66SPyun YongHyeon 	}
1498d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1499d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1500d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1501d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1502d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1503d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1504d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1505d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1506a94100faSBill Paul 	}
1507a94100faSBill Paul 
1508a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1509a94100faSBill Paul 
1510a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1511a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1512a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1513a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1514a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1515a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1516a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1517a94100faSBill Paul 	}
1518a94100faSBill Paul 
1519a94100faSBill Paul 	if (sc->rl_parent_tag)
1520a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1521a94100faSBill Paul 
1522a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1523a94100faSBill Paul 
1524a94100faSBill Paul 	return (0);
1525a94100faSBill Paul }
1526a94100faSBill Paul 
1527d65abd66SPyun YongHyeon static __inline void
1528d65abd66SPyun YongHyeon re_discard_rxbuf(sc, idx)
1529a94100faSBill Paul 	struct rl_softc		*sc;
1530a94100faSBill Paul 	int			idx;
1531a94100faSBill Paul {
1532d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1533d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1534d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1535a94100faSBill Paul 
1536d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1537d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1538d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1539d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1540d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1541d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1542d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1543d65abd66SPyun YongHyeon }
1544d65abd66SPyun YongHyeon 
1545d65abd66SPyun YongHyeon static int
1546d65abd66SPyun YongHyeon re_newbuf(sc, idx)
1547d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
1548d65abd66SPyun YongHyeon 	int			idx;
1549d65abd66SPyun YongHyeon {
1550d65abd66SPyun YongHyeon 	struct mbuf		*m;
1551d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1552d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1553d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1554d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1555d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1556d65abd66SPyun YongHyeon 	int			error, nsegs;
1557d65abd66SPyun YongHyeon 
1558d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1559d65abd66SPyun YongHyeon 	if (m == NULL)
1560a94100faSBill Paul 		return (ENOBUFS);
1561a94100faSBill Paul 
1562a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
156322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
156422a11c96SJohn-Mark Gurney 	/*
156522a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
156622a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
156722a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
156822a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
156922a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
157022a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
157122a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
157222a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
157322a11c96SJohn-Mark Gurney 	 */
157422a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
157522a11c96SJohn-Mark Gurney #endif
1576d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1577d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1578d65abd66SPyun YongHyeon 	if (error != 0) {
1579d65abd66SPyun YongHyeon 		m_freem(m);
1580d65abd66SPyun YongHyeon 		return (ENOBUFS);
1581d65abd66SPyun YongHyeon 	}
1582d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1583a94100faSBill Paul 
1584d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1585d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1586d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1587d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1588d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1589a94100faSBill Paul 	}
1590a94100faSBill Paul 
1591d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1592d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1593d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1594d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1595d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1596d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1597a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1598a94100faSBill Paul 
1599d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1600d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1601d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1602d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1603d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1604d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1605d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1606d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1607d65abd66SPyun YongHyeon 
1608a94100faSBill Paul 	return (0);
1609a94100faSBill Paul }
1610a94100faSBill Paul 
161122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
161222a11c96SJohn-Mark Gurney static __inline void
161322a11c96SJohn-Mark Gurney re_fixup_rx(m)
161422a11c96SJohn-Mark Gurney 	struct mbuf		*m;
161522a11c96SJohn-Mark Gurney {
161622a11c96SJohn-Mark Gurney 	int                     i;
161722a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
161822a11c96SJohn-Mark Gurney 
161922a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
162022a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
162122a11c96SJohn-Mark Gurney 
162222a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
162322a11c96SJohn-Mark Gurney 		*dst++ = *src++;
162422a11c96SJohn-Mark Gurney 
162522a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
162622a11c96SJohn-Mark Gurney 
162722a11c96SJohn-Mark Gurney 	return;
162822a11c96SJohn-Mark Gurney }
162922a11c96SJohn-Mark Gurney #endif
163022a11c96SJohn-Mark Gurney 
1631a94100faSBill Paul static int
1632a94100faSBill Paul re_tx_list_init(sc)
1633a94100faSBill Paul 	struct rl_softc		*sc;
1634a94100faSBill Paul {
1635d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1636d65abd66SPyun YongHyeon 	int			i;
163797b9d4baSJohn-Mark Gurney 
163897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
163997b9d4baSJohn-Mark Gurney 
1640d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1641d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1642d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1643d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1644d65abd66SPyun YongHyeon 	/* Set EOR. */
1645d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1646d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1647a94100faSBill Paul 
1648a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1649d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1650d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1651d65abd66SPyun YongHyeon 
1652a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1653a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1654d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1655a94100faSBill Paul 
1656a94100faSBill Paul 	return (0);
1657a94100faSBill Paul }
1658a94100faSBill Paul 
1659a94100faSBill Paul static int
1660a94100faSBill Paul re_rx_list_init(sc)
1661a94100faSBill Paul 	struct rl_softc		*sc;
1662a94100faSBill Paul {
1663d65abd66SPyun YongHyeon 	int			error, i;
1664a94100faSBill Paul 
1665d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1666d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1667d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1668d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1669d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1670d65abd66SPyun YongHyeon 			return (error);
1671a94100faSBill Paul 	}
1672a94100faSBill Paul 
1673a94100faSBill Paul 	/* Flush the RX descriptors */
1674a94100faSBill Paul 
1675a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1676a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1677a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1678a94100faSBill Paul 
1679a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1680a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1681a94100faSBill Paul 
1682a94100faSBill Paul 	return (0);
1683a94100faSBill Paul }
1684a94100faSBill Paul 
1685a94100faSBill Paul /*
1686a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1687a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1688a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1689a94100faSBill Paul  */
1690ed510fb0SBill Paul static int
1691a94100faSBill Paul re_rxeof(sc)
1692a94100faSBill Paul 	struct rl_softc		*sc;
1693a94100faSBill Paul {
1694a94100faSBill Paul 	struct mbuf		*m;
1695a94100faSBill Paul 	struct ifnet		*ifp;
1696a94100faSBill Paul 	int			i, total_len;
1697a94100faSBill Paul 	struct rl_desc		*cur_rx;
1698a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1699ed510fb0SBill Paul 	int			maxpkt = 16;
1700a94100faSBill Paul 
17015120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17025120abbfSSam Leffler 
1703fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1704a94100faSBill Paul 
1705a94100faSBill Paul 	/* Invalidate the descriptor memory */
1706a94100faSBill Paul 
1707a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1708a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1709d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1710a94100faSBill Paul 
1711d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1712d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1713a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1714a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1715d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1716d65abd66SPyun YongHyeon 			break;
1717d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1718a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1719d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1720a94100faSBill Paul 
1721a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1722d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1723d65abd66SPyun YongHyeon 				/*
1724d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1725d65abd66SPyun YongHyeon 				 * discard all the pieces.
1726d65abd66SPyun YongHyeon 				 */
1727d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1728d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1729d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1730d65abd66SPyun YongHyeon 				}
1731d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1732d65abd66SPyun YongHyeon 				continue;
1733d65abd66SPyun YongHyeon 			}
173422a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1735a94100faSBill Paul 			if (sc->rl_head == NULL)
1736a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1737a94100faSBill Paul 			else {
1738a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1739a94100faSBill Paul 				sc->rl_tail->m_next = m;
1740a94100faSBill Paul 				sc->rl_tail = m;
1741a94100faSBill Paul 			}
1742a94100faSBill Paul 			continue;
1743a94100faSBill Paul 		}
1744a94100faSBill Paul 
1745a94100faSBill Paul 		/*
1746a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1747a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1748a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1749a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1750a94100faSBill Paul 		 * were already used, so to make room for the extra
1751a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1752a94100faSBill Paul 		 * error' bit and shifted the other status bits
1753a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1754a94100faSBill Paul 		 * still in the same places. We have already extracted
1755a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1756a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1757a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1758a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1759a94100faSBill Paul 		 * same format as that of the 8139C+.
1760a94100faSBill Paul 		 */
1761a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1762a94100faSBill Paul 			rxstat >>= 1;
1763a94100faSBill Paul 
176422a11c96SJohn-Mark Gurney 		/*
176522a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
176622a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
176722a11c96SJohn-Mark Gurney 		 */
176822a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
176922a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1770a94100faSBill Paul 			ifp->if_ierrors++;
1771a94100faSBill Paul 			/*
1772a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1773a94100faSBill Paul 			 * discard all the pieces.
1774a94100faSBill Paul 			 */
1775a94100faSBill Paul 			if (sc->rl_head != NULL) {
1776a94100faSBill Paul 				m_freem(sc->rl_head);
1777a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1778a94100faSBill Paul 			}
1779d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1780a94100faSBill Paul 			continue;
1781a94100faSBill Paul 		}
1782a94100faSBill Paul 
1783a94100faSBill Paul 		/*
1784a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1785a94100faSBill Paul 		 * reload the current one.
1786a94100faSBill Paul 		 */
1787a94100faSBill Paul 
1788d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1789d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1790a94100faSBill Paul 			if (sc->rl_head != NULL) {
1791a94100faSBill Paul 				m_freem(sc->rl_head);
1792a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1793a94100faSBill Paul 			}
1794d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1795a94100faSBill Paul 			continue;
1796a94100faSBill Paul 		}
1797a94100faSBill Paul 
1798a94100faSBill Paul 		if (sc->rl_head != NULL) {
179922a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
180022a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
180122a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1802a94100faSBill Paul 			/*
1803a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1804a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1805a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1806a94100faSBill Paul 			 * care about anyway.
1807a94100faSBill Paul 			 */
1808a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1809a94100faSBill Paul 				sc->rl_tail->m_len -=
1810a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1811a94100faSBill Paul 				m_freem(m);
1812a94100faSBill Paul 			} else {
1813a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1814a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1815a94100faSBill Paul 				sc->rl_tail->m_next = m;
1816a94100faSBill Paul 			}
1817a94100faSBill Paul 			m = sc->rl_head;
1818a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1819a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1820a94100faSBill Paul 		} else
1821a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1822a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1823a94100faSBill Paul 
182422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
182522a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
182622a11c96SJohn-Mark Gurney #endif
1827a94100faSBill Paul 		ifp->if_ipackets++;
1828a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1829a94100faSBill Paul 
1830a94100faSBill Paul 		/* Do RX checksumming if enabled */
1831a94100faSBill Paul 
1832a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1833a94100faSBill Paul 
1834a94100faSBill Paul 			/* Check IP header checksum */
1835a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1836a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1837a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1838a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1839a94100faSBill Paul 
1840a94100faSBill Paul 			/* Check TCP/UDP checksum */
1841a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1842a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1843a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1844a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1845a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1846a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1847a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1848a94100faSBill Paul 			}
1849a94100faSBill Paul 		}
1850ed510fb0SBill Paul 		maxpkt--;
1851d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
185278ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
185378ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
185478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1855d147662cSGleb Smirnoff 		}
18565120abbfSSam Leffler 		RL_UNLOCK(sc);
1857a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
18585120abbfSSam Leffler 		RL_LOCK(sc);
1859a94100faSBill Paul 	}
1860a94100faSBill Paul 
1861a94100faSBill Paul 	/* Flush the RX DMA ring */
1862a94100faSBill Paul 
1863a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1864a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1865a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1866a94100faSBill Paul 
1867a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1868ed510fb0SBill Paul 
1869ed510fb0SBill Paul 	if (maxpkt)
1870ed510fb0SBill Paul 		return(EAGAIN);
1871ed510fb0SBill Paul 
1872ed510fb0SBill Paul 	return(0);
1873a94100faSBill Paul }
1874a94100faSBill Paul 
1875a94100faSBill Paul static void
1876a94100faSBill Paul re_txeof(sc)
1877a94100faSBill Paul 	struct rl_softc		*sc;
1878a94100faSBill Paul {
1879a94100faSBill Paul 	struct ifnet		*ifp;
1880d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
1881a94100faSBill Paul 	u_int32_t		txstat;
1882d65abd66SPyun YongHyeon 	int			cons;
1883d65abd66SPyun YongHyeon 
1884d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
1885d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
1886d65abd66SPyun YongHyeon 		return;
1887a94100faSBill Paul 
1888fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1889a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1890a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1891a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1892d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1893a94100faSBill Paul 
1894d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
1895d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
1896d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
1897d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
1898a94100faSBill Paul 			break;
1899a94100faSBill Paul 		/*
1900a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1901a94100faSBill Paul 		 * in a fragment chain, which also happens to
1902a94100faSBill Paul 		 * be the only place where the TX status bits
1903a94100faSBill Paul 		 * are valid.
1904a94100faSBill Paul 		 */
1905a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1906d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
1907d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
1908d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1909d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
1910d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
1911d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
1912d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
1913d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
1914d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
1915a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1916a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1917a94100faSBill Paul 				ifp->if_collisions++;
1918a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1919a94100faSBill Paul 				ifp->if_oerrors++;
1920a94100faSBill Paul 			else
1921a94100faSBill Paul 				ifp->if_opackets++;
1922a94100faSBill Paul 		}
1923a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1924d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1925a94100faSBill Paul 	}
1926d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
1927a94100faSBill Paul 
1928a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1929a94100faSBill Paul 
1930d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
19310fc4974fSBill Paul 		/*
1932b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
1933b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
1934b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
1935b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
1936b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
1937b4b95879SMarius Strobl 		 * be required with the PCIe devices.
19380fc4974fSBill Paul 		 */
19390fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
19400fc4974fSBill Paul 
1941ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1942a94100faSBill Paul 		/*
1943b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
1944b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
1945a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
1946a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
1947a94100faSBill Paul 		 */
1948a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1949ed510fb0SBill Paul #endif
1950b4b95879SMarius Strobl 	} else
1951b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
1952a94100faSBill Paul }
1953a94100faSBill Paul 
1954a94100faSBill Paul static void
1955a94100faSBill Paul re_tick(xsc)
1956a94100faSBill Paul 	void			*xsc;
1957a94100faSBill Paul {
1958a94100faSBill Paul 	struct rl_softc		*sc;
1959d1754a9bSJohn Baldwin 	struct mii_data		*mii;
1960ed510fb0SBill Paul 	struct ifnet		*ifp;
1961a94100faSBill Paul 
1962a94100faSBill Paul 	sc = xsc;
1963ed510fb0SBill Paul 	ifp = sc->rl_ifp;
196497b9d4baSJohn-Mark Gurney 
196597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
196697b9d4baSJohn-Mark Gurney 
19671d545c7aSMarius Strobl 	re_watchdog(sc);
1968a94100faSBill Paul 
19691d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
1970a94100faSBill Paul 	mii_tick(mii);
1971ed510fb0SBill Paul 	if (sc->rl_link) {
1972ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
1973ed510fb0SBill Paul 			sc->rl_link = 0;
1974ed510fb0SBill Paul 	} else {
1975ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1976ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1977ed510fb0SBill Paul 			sc->rl_link = 1;
1978ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1979ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
1980ed510fb0SBill Paul 				    &sc->rl_txtask);
1981ed510fb0SBill Paul 		}
1982ed510fb0SBill Paul 	}
1983a94100faSBill Paul 
1984d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
1985a94100faSBill Paul }
1986a94100faSBill Paul 
1987a94100faSBill Paul #ifdef DEVICE_POLLING
1988a94100faSBill Paul static void
1989a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1990a94100faSBill Paul {
1991a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1992a94100faSBill Paul 
1993a94100faSBill Paul 	RL_LOCK(sc);
199440929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
199597b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
199697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
199797b9d4baSJohn-Mark Gurney }
199897b9d4baSJohn-Mark Gurney 
199997b9d4baSJohn-Mark Gurney static void
200097b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
200197b9d4baSJohn-Mark Gurney {
200297b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
200397b9d4baSJohn-Mark Gurney 
200497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
200597b9d4baSJohn-Mark Gurney 
2006a94100faSBill Paul 	sc->rxcycles = count;
2007a94100faSBill Paul 	re_rxeof(sc);
2008a94100faSBill Paul 	re_txeof(sc);
2009a94100faSBill Paul 
201037652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2011ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2012a94100faSBill Paul 
2013a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2014a94100faSBill Paul 		u_int16_t       status;
2015a94100faSBill Paul 
2016a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2017a94100faSBill Paul 		if (status == 0xffff)
201897b9d4baSJohn-Mark Gurney 			return;
2019a94100faSBill Paul 		if (status)
2020a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2021a94100faSBill Paul 
2022a94100faSBill Paul 		/*
2023a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2024a94100faSBill Paul 		 */
2025a94100faSBill Paul 
2026a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
2027a94100faSBill Paul 			re_reset(sc);
202897b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2029a94100faSBill Paul 		}
2030a94100faSBill Paul 	}
2031a94100faSBill Paul }
2032a94100faSBill Paul #endif /* DEVICE_POLLING */
2033a94100faSBill Paul 
2034ef544f63SPaolo Pisati static int
2035a94100faSBill Paul re_intr(arg)
2036a94100faSBill Paul 	void			*arg;
2037a94100faSBill Paul {
2038a94100faSBill Paul 	struct rl_softc		*sc;
2039ed510fb0SBill Paul 	uint16_t		status;
2040a94100faSBill Paul 
2041a94100faSBill Paul 	sc = arg;
2042ed510fb0SBill Paul 
2043ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2044498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2045ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2046ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2047ed510fb0SBill Paul 
2048ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2049ed510fb0SBill Paul 
2050ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2051ed510fb0SBill Paul }
2052ed510fb0SBill Paul 
2053ed510fb0SBill Paul static void
2054ed510fb0SBill Paul re_int_task(arg, npending)
2055ed510fb0SBill Paul 	void			*arg;
2056ed510fb0SBill Paul 	int			npending;
2057ed510fb0SBill Paul {
2058ed510fb0SBill Paul 	struct rl_softc		*sc;
2059ed510fb0SBill Paul 	struct ifnet		*ifp;
2060ed510fb0SBill Paul 	u_int16_t		status;
2061ed510fb0SBill Paul 	int			rval = 0;
2062ed510fb0SBill Paul 
2063ed510fb0SBill Paul 	sc = arg;
2064ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2065a94100faSBill Paul 
2066a94100faSBill Paul 	RL_LOCK(sc);
206797b9d4baSJohn-Mark Gurney 
2068a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2069a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2070a94100faSBill Paul 
2071d65abd66SPyun YongHyeon 	if (sc->suspended ||
2072d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2073ed510fb0SBill Paul 		RL_UNLOCK(sc);
2074ed510fb0SBill Paul 		return;
2075ed510fb0SBill Paul 	}
2076a94100faSBill Paul 
2077ed510fb0SBill Paul #ifdef DEVICE_POLLING
2078ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2079ed510fb0SBill Paul 		RL_UNLOCK(sc);
2080ed510fb0SBill Paul 		return;
2081ed510fb0SBill Paul 	}
2082ed510fb0SBill Paul #endif
2083a94100faSBill Paul 
2084ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2085ed510fb0SBill Paul 		rval = re_rxeof(sc);
2086ed510fb0SBill Paul 
2087ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2088ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
2089ed510fb0SBill Paul #else
2090ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
2091ed510fb0SBill Paul #endif
2092ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2093a94100faSBill Paul 		re_txeof(sc);
2094a94100faSBill Paul 
2095a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
2096a94100faSBill Paul 		re_reset(sc);
209797b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2098a94100faSBill Paul 	}
2099a94100faSBill Paul 
2100a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
2101d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
2102d1754a9bSJohn Baldwin 		re_tick(sc);
2103a94100faSBill Paul 	}
2104a94100faSBill Paul 
210552732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2106ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2107a94100faSBill Paul 
2108a94100faSBill Paul 	RL_UNLOCK(sc);
2109ed510fb0SBill Paul 
2110ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2111ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2112ed510fb0SBill Paul 		return;
2113ed510fb0SBill Paul 	}
2114ed510fb0SBill Paul 
2115ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2116ed510fb0SBill Paul 
2117ed510fb0SBill Paul 	return;
2118a94100faSBill Paul }
2119a94100faSBill Paul 
2120d65abd66SPyun YongHyeon static int
2121d65abd66SPyun YongHyeon re_encap(sc, m_head)
2122d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
2123d65abd66SPyun YongHyeon 	struct mbuf		**m_head;
2124d65abd66SPyun YongHyeon {
2125d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2126d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2127d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2128d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2129d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2130d65abd66SPyun YongHyeon 	int			nsegs, prod;
2131d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2132d65abd66SPyun YongHyeon 	int			padlen;
2133ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2134a94100faSBill Paul 
2135d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2136738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
21370fc4974fSBill Paul 
21380fc4974fSBill Paul 	/*
21390fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
21400fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
21410fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
21420fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
21430fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
21440fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
214599c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
21460fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2147d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
21480fc4974fSBill Paul 	 */
2149a4148af5SPyun YongHyeon 	if ((*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
215099c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2151d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2152d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2153d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2154d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2155d65abd66SPyun YongHyeon 			m_freem(*m_head);
2156d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2157d65abd66SPyun YongHyeon 				*m_head = NULL;
2158a94100faSBill Paul 				return (ENOBUFS);
2159a94100faSBill Paul 			}
2160d65abd66SPyun YongHyeon 			*m_head = m_new;
2161d65abd66SPyun YongHyeon 		}
2162d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2163d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
216480a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2165b4b95879SMarius Strobl 			if (m_new == NULL) {
2166b4b95879SMarius Strobl 				m_freem(*m_head);
2167b4b95879SMarius Strobl 				*m_head = NULL;
216880a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2169b4b95879SMarius Strobl 			}
2170d65abd66SPyun YongHyeon 		} else
2171d65abd66SPyun YongHyeon 			m_new = *m_head;
2172a94100faSBill Paul 
21730fc4974fSBill Paul 		/*
21740fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
21750fc4974fSBill Paul 		 * to avoid leaking data.
21760fc4974fSBill Paul 		 */
2177d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2178d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
21790fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2180d65abd66SPyun YongHyeon 		*m_head = m_new;
21810fc4974fSBill Paul 	}
21820fc4974fSBill Paul 
2183d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2184d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2185d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2186d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2187d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2188304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2189d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2190d65abd66SPyun YongHyeon 			m_freem(*m_head);
2191b4b95879SMarius Strobl 			*m_head = NULL;
2192d65abd66SPyun YongHyeon 			return (ENOBUFS);
2193a94100faSBill Paul 		}
2194d65abd66SPyun YongHyeon 		*m_head = m_new;
2195d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2196d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2197d65abd66SPyun YongHyeon 		if (error != 0) {
2198d65abd66SPyun YongHyeon 			m_freem(*m_head);
2199d65abd66SPyun YongHyeon 			*m_head = NULL;
2200d65abd66SPyun YongHyeon 			return (error);
2201a94100faSBill Paul 		}
2202d65abd66SPyun YongHyeon 	} else if (error != 0)
2203d65abd66SPyun YongHyeon 		return (error);
2204d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2205d65abd66SPyun YongHyeon 		m_freem(*m_head);
2206d65abd66SPyun YongHyeon 		*m_head = NULL;
2207d65abd66SPyun YongHyeon 		return (EIO);
2208d65abd66SPyun YongHyeon 	}
2209d65abd66SPyun YongHyeon 
2210d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2211d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2212d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2213d65abd66SPyun YongHyeon 		return (ENOBUFS);
2214d65abd66SPyun YongHyeon 	}
2215d65abd66SPyun YongHyeon 
2216d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2217d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2218a94100faSBill Paul 
2219a94100faSBill Paul 	/*
2220d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2221d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2222d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2223d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2224a94100faSBill Paul 	 */
2225d65abd66SPyun YongHyeon 	csum_flags = 0;
2226d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2227d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2228d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2229d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2230d65abd66SPyun YongHyeon 	else {
223199c8ae87SPyun YongHyeon 		/*
223299c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
223399c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
223499c8ae87SPyun YongHyeon 		 * does't make effects.
223599c8ae87SPyun YongHyeon 		 */
223699c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2237d65abd66SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_IPCSUM;
223899c8ae87SPyun YongHyeon 			if (((*m_head)->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2239d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_TCPCSUM;
224099c8ae87SPyun YongHyeon 			if (((*m_head)->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2241d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_UDPCSUM;
2242d65abd66SPyun YongHyeon 		}
224399c8ae87SPyun YongHyeon 	}
2244a94100faSBill Paul 
2245ccf34c81SPyun YongHyeon 	/*
2246ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2247ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2248ccf34c81SPyun YongHyeon 	 * transmission attempt.
2249ccf34c81SPyun YongHyeon 	 */
2250ccf34c81SPyun YongHyeon 	vlanctl = 0;
2251ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2252ccf34c81SPyun YongHyeon 		vlanctl =
2253ccf34c81SPyun YongHyeon 		    htole32(htons((*m_head)->m_pkthdr.ether_vtag) |
2254ccf34c81SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG);
2255ccf34c81SPyun YongHyeon 
2256d65abd66SPyun YongHyeon 	si = prod;
2257d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2258d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2259ccf34c81SPyun YongHyeon 		desc->rl_vlanctl = vlanctl;
2260d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2261d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2262d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2263d65abd66SPyun YongHyeon 		if (i != 0)
2264d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2265d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2266d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2267d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2268d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2269d65abd66SPyun YongHyeon 	}
2270d65abd66SPyun YongHyeon 	/* Update producer index. */
2271d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2272a94100faSBill Paul 
2273d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2274d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2275d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2276d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2277d65abd66SPyun YongHyeon 
2278d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2279d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2280d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2281a94100faSBill Paul 
2282d65abd66SPyun YongHyeon 	/*
2283d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2284d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2285d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2286d65abd66SPyun YongHyeon 	 */
2287d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2288d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2289d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2290d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2291d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2292a94100faSBill Paul 
2293a94100faSBill Paul 	return (0);
2294a94100faSBill Paul }
2295a94100faSBill Paul 
229697b9d4baSJohn-Mark Gurney static void
2297ed510fb0SBill Paul re_tx_task(arg, npending)
2298ed510fb0SBill Paul 	void			*arg;
2299ed510fb0SBill Paul 	int			npending;
230097b9d4baSJohn-Mark Gurney {
2301ed510fb0SBill Paul 	struct ifnet		*ifp;
230297b9d4baSJohn-Mark Gurney 
2303ed510fb0SBill Paul 	ifp = arg;
2304ed510fb0SBill Paul 	re_start(ifp);
2305ed510fb0SBill Paul 
2306ed510fb0SBill Paul 	return;
230797b9d4baSJohn-Mark Gurney }
230897b9d4baSJohn-Mark Gurney 
2309a94100faSBill Paul /*
2310a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2311a94100faSBill Paul  */
2312a94100faSBill Paul static void
2313ed510fb0SBill Paul re_start(ifp)
2314a94100faSBill Paul 	struct ifnet		*ifp;
2315a94100faSBill Paul {
2316a94100faSBill Paul 	struct rl_softc		*sc;
2317d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2318d65abd66SPyun YongHyeon 	int			queued;
2319a94100faSBill Paul 
2320a94100faSBill Paul 	sc = ifp->if_softc;
232197b9d4baSJohn-Mark Gurney 
2322ed510fb0SBill Paul 	RL_LOCK(sc);
2323ed510fb0SBill Paul 
2324d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2325d65abd66SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->rl_link == 0) {
2326ed510fb0SBill Paul 		RL_UNLOCK(sc);
2327ed510fb0SBill Paul 		return;
2328ed510fb0SBill Paul 	}
2329a94100faSBill Paul 
2330d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2331d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
233252732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2333a94100faSBill Paul 		if (m_head == NULL)
2334a94100faSBill Paul 			break;
2335a94100faSBill Paul 
2336d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2337b4b95879SMarius Strobl 			if (m_head == NULL)
2338b4b95879SMarius Strobl 				break;
233952732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
234013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2341a94100faSBill Paul 			break;
2342a94100faSBill Paul 		}
2343a94100faSBill Paul 
2344a94100faSBill Paul 		/*
2345a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2346a94100faSBill Paul 		 * to him.
2347a94100faSBill Paul 		 */
234859a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
234952732175SMax Laier 
235052732175SMax Laier 		queued++;
2351a94100faSBill Paul 	}
2352a94100faSBill Paul 
2353ed510fb0SBill Paul 	if (queued == 0) {
2354ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2355d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2356ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2357ed510fb0SBill Paul #endif
2358ed510fb0SBill Paul 		RL_UNLOCK(sc);
235952732175SMax Laier 		return;
2360ed510fb0SBill Paul 	}
236152732175SMax Laier 
2362a94100faSBill Paul 	/* Flush the TX descriptors */
2363a94100faSBill Paul 
2364a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2365a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2366a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2367a94100faSBill Paul 
23680fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2369a94100faSBill Paul 
2370ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2371a94100faSBill Paul 	/*
2372a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2373a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2374a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2375a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2376a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2377a94100faSBill Paul 	 * the timer count is reset to 0.
2378a94100faSBill Paul 	 */
2379a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2380ed510fb0SBill Paul #endif
2381a94100faSBill Paul 
2382a94100faSBill Paul 	/*
2383a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2384a94100faSBill Paul 	 */
23851d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2386ed510fb0SBill Paul 
2387ed510fb0SBill Paul 	RL_UNLOCK(sc);
2388ed510fb0SBill Paul 
2389ed510fb0SBill Paul 	return;
2390a94100faSBill Paul }
2391a94100faSBill Paul 
2392a94100faSBill Paul static void
2393a94100faSBill Paul re_init(xsc)
2394a94100faSBill Paul 	void			*xsc;
2395a94100faSBill Paul {
2396a94100faSBill Paul 	struct rl_softc		*sc = xsc;
239797b9d4baSJohn-Mark Gurney 
239897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
239997b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
240097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
240197b9d4baSJohn-Mark Gurney }
240297b9d4baSJohn-Mark Gurney 
240397b9d4baSJohn-Mark Gurney static void
240497b9d4baSJohn-Mark Gurney re_init_locked(sc)
240597b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
240697b9d4baSJohn-Mark Gurney {
2407fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2408a94100faSBill Paul 	struct mii_data		*mii;
2409a94100faSBill Paul 	u_int32_t		rxcfg = 0;
241070acaecfSPyun YongHyeon 	uint16_t		cfg;
24114d3d7085SBernd Walter 	union {
24124d3d7085SBernd Walter 		uint32_t align_dummy;
24134d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
24144d3d7085SBernd Walter         } eaddr;
2415a94100faSBill Paul 
241697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
241797b9d4baSJohn-Mark Gurney 
2418a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2419a94100faSBill Paul 
2420a94100faSBill Paul 	/*
2421a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2422a94100faSBill Paul 	 */
2423a94100faSBill Paul 	re_stop(sc);
2424a94100faSBill Paul 
2425a94100faSBill Paul 	/*
2426c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2427edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2428c2c6548bSBill Paul 	 * before all others.
2429c2c6548bSBill Paul 	 */
243070acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
243170acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
243270acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
243370acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
243470acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
243570acaecfSPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD,
243670acaecfSPyun YongHyeon 	    cfg | RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB);
2437c2c6548bSBill Paul 
2438c2c6548bSBill Paul 	/*
2439a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2440a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2441a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2442a94100faSBill Paul 	 */
24434d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
24444d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2445a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2446ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2447ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2448ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2449ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2450a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2451a94100faSBill Paul 
2452a94100faSBill Paul 	/*
2453a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2454a94100faSBill Paul 	 */
2455a94100faSBill Paul 	re_rx_list_init(sc);
2456a94100faSBill Paul 	re_tx_list_init(sc);
2457a94100faSBill Paul 
2458a94100faSBill Paul 	/*
2459d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2460d01fac16SPyun YongHyeon 	 */
2461d01fac16SPyun YongHyeon 
2462d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2463d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2464d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2465d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2466d01fac16SPyun YongHyeon 
2467d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2468d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2469d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2470d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2471d01fac16SPyun YongHyeon 
2472d01fac16SPyun YongHyeon 	/*
2473a94100faSBill Paul 	 * Enable transmit and receive.
2474a94100faSBill Paul 	 */
2475a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2476a94100faSBill Paul 
2477a94100faSBill Paul 	/*
2478a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2479a94100faSBill Paul 	 */
2480abc8ff44SBill Paul 	if (sc->rl_testmode) {
2481abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2482abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2483abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2484a94100faSBill Paul 		else
2485abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2486abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2487abc8ff44SBill Paul 	} else
2488a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2489d01fac16SPyun YongHyeon 
2490d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2491d01fac16SPyun YongHyeon 
2492a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2493a94100faSBill Paul 
2494a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2495a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2496a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2497a94100faSBill Paul 
2498a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
249961021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2500a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
250161021536SJohn-Mark Gurney 	else
2502a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2503a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2504a94100faSBill Paul 
2505a94100faSBill Paul 	/*
2506a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2507a94100faSBill Paul 	 */
250861021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2509a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
251061021536SJohn-Mark Gurney 	else
2511a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2512a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2513a94100faSBill Paul 
2514a94100faSBill Paul 	/*
2515a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2516a94100faSBill Paul 	 */
2517a94100faSBill Paul 	re_setmulti(sc);
2518a94100faSBill Paul 
2519a94100faSBill Paul #ifdef DEVICE_POLLING
2520a94100faSBill Paul 	/*
2521a94100faSBill Paul 	 * Disable interrupts if we are polling.
2522a94100faSBill Paul 	 */
252340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2524a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2525a94100faSBill Paul 	else	/* otherwise ... */
252640929967SGleb Smirnoff #endif
2527ed510fb0SBill Paul 
2528a94100faSBill Paul 	/*
2529a94100faSBill Paul 	 * Enable interrupts.
2530a94100faSBill Paul 	 */
2531a94100faSBill Paul 	if (sc->rl_testmode)
2532a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2533a94100faSBill Paul 	else
2534a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2535ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2536a94100faSBill Paul 
2537a94100faSBill Paul 	/* Set initial TX threshold */
2538a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2539a94100faSBill Paul 
2540a94100faSBill Paul 	/* Start RX/TX process. */
2541a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2542a94100faSBill Paul #ifdef notdef
2543a94100faSBill Paul 	/* Enable receiver and transmitter. */
2544a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2545a94100faSBill Paul #endif
2546a94100faSBill Paul 
2547ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2548a94100faSBill Paul 	/*
2549a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2550a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2551a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2552a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2553a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2554a94100faSBill Paul 	 */
2555a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2556a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2557a94100faSBill Paul 	else
2558a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2559ed510fb0SBill Paul #endif
2560a94100faSBill Paul 
2561a94100faSBill Paul 	/*
2562a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2563a94100faSBill Paul 	 * size so we can receive jumbo frames.
2564a94100faSBill Paul 	 */
2565a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2566a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2567a94100faSBill Paul 
256897b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2569a94100faSBill Paul 		return;
2570a94100faSBill Paul 
2571a94100faSBill Paul 	mii_mediachg(mii);
2572a94100faSBill Paul 
257319ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2574a94100faSBill Paul 
257513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
257613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2577a94100faSBill Paul 
2578ed510fb0SBill Paul 	sc->rl_link = 0;
25791d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2580d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2581a94100faSBill Paul }
2582a94100faSBill Paul 
2583a94100faSBill Paul /*
2584a94100faSBill Paul  * Set media options.
2585a94100faSBill Paul  */
2586a94100faSBill Paul static int
2587a94100faSBill Paul re_ifmedia_upd(ifp)
2588a94100faSBill Paul 	struct ifnet		*ifp;
2589a94100faSBill Paul {
2590a94100faSBill Paul 	struct rl_softc		*sc;
2591a94100faSBill Paul 	struct mii_data		*mii;
2592a94100faSBill Paul 
2593a94100faSBill Paul 	sc = ifp->if_softc;
2594a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2595d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2596a94100faSBill Paul 	mii_mediachg(mii);
2597d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2598a94100faSBill Paul 
2599a94100faSBill Paul 	return (0);
2600a94100faSBill Paul }
2601a94100faSBill Paul 
2602a94100faSBill Paul /*
2603a94100faSBill Paul  * Report current media status.
2604a94100faSBill Paul  */
2605a94100faSBill Paul static void
2606a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2607a94100faSBill Paul 	struct ifnet		*ifp;
2608a94100faSBill Paul 	struct ifmediareq	*ifmr;
2609a94100faSBill Paul {
2610a94100faSBill Paul 	struct rl_softc		*sc;
2611a94100faSBill Paul 	struct mii_data		*mii;
2612a94100faSBill Paul 
2613a94100faSBill Paul 	sc = ifp->if_softc;
2614a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2615a94100faSBill Paul 
2616d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2617a94100faSBill Paul 	mii_pollstat(mii);
2618d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2619a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2620a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2621a94100faSBill Paul }
2622a94100faSBill Paul 
2623a94100faSBill Paul static int
2624a94100faSBill Paul re_ioctl(ifp, command, data)
2625a94100faSBill Paul 	struct ifnet		*ifp;
2626a94100faSBill Paul 	u_long			command;
2627a94100faSBill Paul 	caddr_t			data;
2628a94100faSBill Paul {
2629a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2630a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2631a94100faSBill Paul 	struct mii_data		*mii;
263240929967SGleb Smirnoff 	int			error = 0;
2633a94100faSBill Paul 
2634a94100faSBill Paul 	switch (command) {
2635a94100faSBill Paul 	case SIOCSIFMTU:
2636c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2637a94100faSBill Paul 			error = EINVAL;
2638c1d0b573SPyun YongHyeon 			break;
2639c1d0b573SPyun YongHyeon 		}
2640c1d0b573SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS &&
2641c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2642c1d0b573SPyun YongHyeon 			error = EINVAL;
2643c1d0b573SPyun YongHyeon 			break;
2644c1d0b573SPyun YongHyeon 		}
2645c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2646c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2647a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2648d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2649a94100faSBill Paul 		break;
2650a94100faSBill Paul 	case SIOCSIFFLAGS:
265197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2652eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2653eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2654eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
2655eed497bbSPyun YongHyeon 				    & IFF_PROMISC) != 0)
2656eed497bbSPyun YongHyeon 					re_setmulti(sc);
2657eed497bbSPyun YongHyeon 			} else
265897b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2659eed497bbSPyun YongHyeon 		} else {
2660eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2661a94100faSBill Paul 				re_stop(sc);
2662eed497bbSPyun YongHyeon 		}
2663eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
266497b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2665a94100faSBill Paul 		break;
2666a94100faSBill Paul 	case SIOCADDMULTI:
2667a94100faSBill Paul 	case SIOCDELMULTI:
266897b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2669a94100faSBill Paul 		re_setmulti(sc);
267097b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2671a94100faSBill Paul 		break;
2672a94100faSBill Paul 	case SIOCGIFMEDIA:
2673a94100faSBill Paul 	case SIOCSIFMEDIA:
2674a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2675a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2676a94100faSBill Paul 		break;
2677a94100faSBill Paul 	case SIOCSIFCAP:
267840929967SGleb Smirnoff 	    {
2679f051cb85SGleb Smirnoff 		int mask, reinit;
2680f051cb85SGleb Smirnoff 
2681f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2682f051cb85SGleb Smirnoff 		reinit = 0;
268340929967SGleb Smirnoff #ifdef DEVICE_POLLING
268440929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
268540929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
268640929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
268740929967SGleb Smirnoff 				if (error)
268840929967SGleb Smirnoff 					return(error);
2689d1754a9bSJohn Baldwin 				RL_LOCK(sc);
269040929967SGleb Smirnoff 				/* Disable interrupts */
269140929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
269240929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
269340929967SGleb Smirnoff 				RL_UNLOCK(sc);
269440929967SGleb Smirnoff 			} else {
269540929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
269640929967SGleb Smirnoff 				/* Enable interrupts. */
269740929967SGleb Smirnoff 				RL_LOCK(sc);
269840929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
269940929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
270040929967SGleb Smirnoff 				RL_UNLOCK(sc);
270140929967SGleb Smirnoff 			}
270240929967SGleb Smirnoff 		}
270340929967SGleb Smirnoff #endif /* DEVICE_POLLING */
270440929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2705f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2706a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2707dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2708a94100faSBill Paul 			else
2709b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2710f051cb85SGleb Smirnoff 			reinit = 1;
271140929967SGleb Smirnoff 		}
2712f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2713f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2714f051cb85SGleb Smirnoff 			reinit = 1;
2715f051cb85SGleb Smirnoff 		}
2716dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2717dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2718dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2719dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2720dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2721dc74159dSPyun YongHyeon 			else
2722dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2723dc74159dSPyun YongHyeon 		}
27247467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
27257467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
27267467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
27277467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
27287467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
27297467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
27307467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
27317467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
27327467bd53SPyun YongHyeon 		}
2733f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2734f051cb85SGleb Smirnoff 			re_init(sc);
2735960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
273640929967SGleb Smirnoff 	    }
2737a94100faSBill Paul 		break;
2738a94100faSBill Paul 	default:
2739a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2740a94100faSBill Paul 		break;
2741a94100faSBill Paul 	}
2742a94100faSBill Paul 
2743a94100faSBill Paul 	return (error);
2744a94100faSBill Paul }
2745a94100faSBill Paul 
2746a94100faSBill Paul static void
27471d545c7aSMarius Strobl re_watchdog(sc)
2748a94100faSBill Paul 	struct rl_softc		*sc;
27491d545c7aSMarius Strobl {
2750a94100faSBill Paul 
27511d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
27521d545c7aSMarius Strobl 
27531d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
27541d545c7aSMarius Strobl 		return;
27551d545c7aSMarius Strobl 
27561d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
27571d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2758a94100faSBill Paul 
2759a94100faSBill Paul 	re_txeof(sc);
2760a94100faSBill Paul 	re_rxeof(sc);
276197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2762a94100faSBill Paul }
2763a94100faSBill Paul 
2764a94100faSBill Paul /*
2765a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2766a94100faSBill Paul  * RX and TX lists.
2767a94100faSBill Paul  */
2768a94100faSBill Paul static void
2769a94100faSBill Paul re_stop(sc)
2770a94100faSBill Paul 	struct rl_softc		*sc;
2771a94100faSBill Paul {
2772a94100faSBill Paul 	register int		i;
2773a94100faSBill Paul 	struct ifnet		*ifp;
2774d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2775d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2776a94100faSBill Paul 
277797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
277897b9d4baSJohn-Mark Gurney 
2779fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2780a94100faSBill Paul 
27811d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2782d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
278313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2784a94100faSBill Paul 
2785a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2786a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2787ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2788a94100faSBill Paul 
2789a94100faSBill Paul 	if (sc->rl_head != NULL) {
2790a94100faSBill Paul 		m_freem(sc->rl_head);
2791a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2792a94100faSBill Paul 	}
2793a94100faSBill Paul 
2794a94100faSBill Paul 	/* Free the TX list buffers. */
2795a94100faSBill Paul 
2796d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2797d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2798d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2799d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2800d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2801d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2802d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2803d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2804d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2805a94100faSBill Paul 		}
2806a94100faSBill Paul 	}
2807a94100faSBill Paul 
2808a94100faSBill Paul 	/* Free the RX list buffers. */
2809a94100faSBill Paul 
2810d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2811d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2812d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2813d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2814d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2815d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2816d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2817d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2818d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2819a94100faSBill Paul 		}
2820a94100faSBill Paul 	}
2821a94100faSBill Paul }
2822a94100faSBill Paul 
2823a94100faSBill Paul /*
2824a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2825a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2826a94100faSBill Paul  * resume.
2827a94100faSBill Paul  */
2828a94100faSBill Paul static int
2829a94100faSBill Paul re_suspend(dev)
2830a94100faSBill Paul 	device_t		dev;
2831a94100faSBill Paul {
2832a94100faSBill Paul 	struct rl_softc		*sc;
2833a94100faSBill Paul 
2834a94100faSBill Paul 	sc = device_get_softc(dev);
2835a94100faSBill Paul 
283697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2837a94100faSBill Paul 	re_stop(sc);
28387467bd53SPyun YongHyeon 	re_setwol(sc);
2839a94100faSBill Paul 	sc->suspended = 1;
284097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2841a94100faSBill Paul 
2842a94100faSBill Paul 	return (0);
2843a94100faSBill Paul }
2844a94100faSBill Paul 
2845a94100faSBill Paul /*
2846a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2847a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2848a94100faSBill Paul  * appropriate.
2849a94100faSBill Paul  */
2850a94100faSBill Paul static int
2851a94100faSBill Paul re_resume(dev)
2852a94100faSBill Paul 	device_t		dev;
2853a94100faSBill Paul {
2854a94100faSBill Paul 	struct rl_softc		*sc;
2855a94100faSBill Paul 	struct ifnet		*ifp;
2856a94100faSBill Paul 
2857a94100faSBill Paul 	sc = device_get_softc(dev);
285897b9d4baSJohn-Mark Gurney 
285997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
286097b9d4baSJohn-Mark Gurney 
2861fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2862a94100faSBill Paul 
2863a94100faSBill Paul 	/* reinitialize interface if necessary */
2864a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
286597b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2866a94100faSBill Paul 
28677467bd53SPyun YongHyeon 	/*
28687467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
28697467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
28707467bd53SPyun YongHyeon 	 */
28717467bd53SPyun YongHyeon 	re_clrwol(sc);
2872a94100faSBill Paul 	sc->suspended = 0;
287397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2874a94100faSBill Paul 
2875a94100faSBill Paul 	return (0);
2876a94100faSBill Paul }
2877a94100faSBill Paul 
2878a94100faSBill Paul /*
2879a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2880a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2881a94100faSBill Paul  */
28826a087a87SPyun YongHyeon static int
2883a94100faSBill Paul re_shutdown(dev)
2884a94100faSBill Paul 	device_t		dev;
2885a94100faSBill Paul {
2886a94100faSBill Paul 	struct rl_softc		*sc;
2887a94100faSBill Paul 
2888a94100faSBill Paul 	sc = device_get_softc(dev);
2889a94100faSBill Paul 
289097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2891a94100faSBill Paul 	re_stop(sc);
2892536fde34SMaxim Sobolev 	/*
2893536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2894536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
289572293673SRuslan Ermilov 	 * cases.
2896536fde34SMaxim Sobolev 	 */
2897536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
28987467bd53SPyun YongHyeon 	re_setwol(sc);
289997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
29006a087a87SPyun YongHyeon 
29016a087a87SPyun YongHyeon 	return (0);
2902a94100faSBill Paul }
29037467bd53SPyun YongHyeon 
29047467bd53SPyun YongHyeon static void
29057467bd53SPyun YongHyeon re_setwol(sc)
29067467bd53SPyun YongHyeon 	struct rl_softc		*sc;
29077467bd53SPyun YongHyeon {
29087467bd53SPyun YongHyeon 	struct ifnet		*ifp;
29097467bd53SPyun YongHyeon 	int			pmc;
29107467bd53SPyun YongHyeon 	uint16_t		pmstat;
29117467bd53SPyun YongHyeon 	uint8_t			v;
29127467bd53SPyun YongHyeon 
29137467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
29147467bd53SPyun YongHyeon 
29157467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
29167467bd53SPyun YongHyeon 		return;
29177467bd53SPyun YongHyeon 
29187467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
29197467bd53SPyun YongHyeon 	/* Enable config register write. */
29207467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
29217467bd53SPyun YongHyeon 
29227467bd53SPyun YongHyeon 	/* Enable PME. */
29237467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
29247467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
29257467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29267467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
29277467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
29287467bd53SPyun YongHyeon 
29297467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
29307467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
29317467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
29327467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
29337467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
29347467bd53SPyun YongHyeon 
29357467bd53SPyun YongHyeon 	/* Config register write done. */
29367467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, 0);
29377467bd53SPyun YongHyeon 
29387467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
29397467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
29407467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
29417467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
29427467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
29437467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
29447467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
29457467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29467467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
29477467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
29487467bd53SPyun YongHyeon 
29497467bd53SPyun YongHyeon 	/*
29507467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
29517467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
29527467bd53SPyun YongHyeon 	 * needed.
29537467bd53SPyun YongHyeon 	 */
29547467bd53SPyun YongHyeon 
29557467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
29567467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
29577467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
29587467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29597467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
29607467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
29617467bd53SPyun YongHyeon }
29627467bd53SPyun YongHyeon 
29637467bd53SPyun YongHyeon static void
29647467bd53SPyun YongHyeon re_clrwol(sc)
29657467bd53SPyun YongHyeon 	struct rl_softc		*sc;
29667467bd53SPyun YongHyeon {
29677467bd53SPyun YongHyeon 	int			pmc;
29687467bd53SPyun YongHyeon 	uint8_t			v;
29697467bd53SPyun YongHyeon 
29707467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
29717467bd53SPyun YongHyeon 
29727467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
29737467bd53SPyun YongHyeon 		return;
29747467bd53SPyun YongHyeon 
29757467bd53SPyun YongHyeon 	/* Enable config register write. */
29767467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
29777467bd53SPyun YongHyeon 
29787467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
29797467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
29807467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
29817467bd53SPyun YongHyeon 
29827467bd53SPyun YongHyeon 	/* Config register write done. */
29837467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, 0);
29847467bd53SPyun YongHyeon 
29857467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
29867467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
29877467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
29887467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
29897467bd53SPyun YongHyeon }
2990