xref: /freebsd/sys/dev/re/if_re.c (revision 339a44fb62595ffeb01a0a2d2f2ca87942038fb4)
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[] = {
17332aa5f0eSAnton Berezin 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169S,
17432aa5f0eSAnton Berezin 		"D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1753b9982e5SRemko Lodder 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169_8110SB,
1763b9982e5SRemko Lodder 		"D-Link DGE-528(T) Rev.B1 Gigabit Ethernet Adapter" },
177a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
178a94100faSBill Paul 		"RealTek 8139C+ 10/100BaseTX" },
179ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8101E, RL_HWREV_8101E,
180ed510fb0SBill Paul 		"RealTek 8101E PCIe 10/100baseTX" },
181498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN1,
182498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
183498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN2,
184498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
1851acbb78aSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN3,
1861acbb78aSPyun YongHyeon 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
187a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
188a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
18969a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
19069a6b7fbSBill Paul 		"RealTek 8169S Single-chip Gigabit Ethernet" },
191ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB,
192ed510fb0SBill Paul 		"RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" },
1932ee2c3b4SRemko Lodder 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC,
1942ee2c3b4SRemko Lodder 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
195498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169SC, RL_HWREV_8169_8110SC,
196ed510fb0SBill Paul 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
19769a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
19869a6b7fbSBill Paul 		"RealTek 8110S Single-chip Gigabit Ethernet" },
199ea263191SMIHIRA Sanpei Yoshiro 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
200ea263191SMIHIRA Sanpei Yoshiro 		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
20126390635SJohn Baldwin 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S,
20226390635SJohn Baldwin 		"Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
2030fc4974fSBill Paul 	{ USR_VENDORID, USR_DEVICEID_997902, RL_HWREV_8169S,
204dfdb409eSPyun YongHyeon 		"US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
205a94100faSBill Paul };
206a94100faSBill Paul 
207a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
208a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
209a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
210a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
211a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
212a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
213a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
214a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
215a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
216498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
217a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
21869a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
21969a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
220ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
221ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
222a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
223a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
224ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
225ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
226498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
2271acbb78aSPyun YongHyeon 	{ RL_HWREV_8168_SPIN3, RL_8169, "8168"},
228a94100faSBill Paul 	{ 0, 0, NULL }
229a94100faSBill Paul };
230a94100faSBill Paul 
231a94100faSBill Paul static int re_probe		(device_t);
232a94100faSBill Paul static int re_attach		(device_t);
233a94100faSBill Paul static int re_detach		(device_t);
234a94100faSBill Paul 
235d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
236a94100faSBill Paul 
237a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
238a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
239d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
240d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
241d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
242a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
243a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
24422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24522a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
24622a11c96SJohn-Mark Gurney 				(struct mbuf *);
24722a11c96SJohn-Mark Gurney #endif
248ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
249a94100faSBill Paul static void re_txeof		(struct rl_softc *);
25097b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2510187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2520187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
25397b9d4baSJohn-Mark Gurney #endif
254ef544f63SPaolo Pisati static int re_intr		(void *);
255a94100faSBill Paul static void re_tick		(void *);
256ed510fb0SBill Paul static void re_tx_task		(void *, int);
257ed510fb0SBill Paul static void re_int_task		(void *, int);
258a94100faSBill Paul static void re_start		(struct ifnet *);
259a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
260a94100faSBill Paul static void re_init		(void *);
26197b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
262a94100faSBill Paul static void re_stop		(struct rl_softc *);
2631d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
264a94100faSBill Paul static int re_suspend		(device_t);
265a94100faSBill Paul static int re_resume		(device_t);
2666a087a87SPyun YongHyeon static int re_shutdown		(device_t);
267a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
268a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
269a94100faSBill Paul 
270a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
271a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
272ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
273a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
274a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
275a94100faSBill Paul 
276a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
277a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
278a94100faSBill Paul static void re_miibus_statchg	(device_t);
279a94100faSBill Paul 
280a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
281a94100faSBill Paul static void re_reset		(struct rl_softc *);
2827467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2837467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
284a94100faSBill Paul 
285ed510fb0SBill Paul #ifdef RE_DIAG
286a94100faSBill Paul static int re_diag		(struct rl_softc *);
287ed510fb0SBill Paul #endif
288a94100faSBill Paul 
289a94100faSBill Paul #ifdef RE_USEIOSPACE
290a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
291a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
292a94100faSBill Paul #else
293a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
294a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
295a94100faSBill Paul #endif
296a94100faSBill Paul 
297a94100faSBill Paul static device_method_t re_methods[] = {
298a94100faSBill Paul 	/* Device interface */
299a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
300a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
301a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
302a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
303a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
304a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
305a94100faSBill Paul 
306a94100faSBill Paul 	/* bus interface */
307a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
308a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
309a94100faSBill Paul 
310a94100faSBill Paul 	/* MII interface */
311a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
312a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
313a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
314a94100faSBill Paul 
315a94100faSBill Paul 	{ 0, 0 }
316a94100faSBill Paul };
317a94100faSBill Paul 
318a94100faSBill Paul static driver_t re_driver = {
319a94100faSBill Paul 	"re",
320a94100faSBill Paul 	re_methods,
321a94100faSBill Paul 	sizeof(struct rl_softc)
322a94100faSBill Paul };
323a94100faSBill Paul 
324a94100faSBill Paul static devclass_t re_devclass;
325a94100faSBill Paul 
326a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
327347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
328a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
329a94100faSBill Paul 
330a94100faSBill Paul #define EE_SET(x)					\
331a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
332a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
333a94100faSBill Paul 
334a94100faSBill Paul #define EE_CLR(x)					\
335a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
336a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
337a94100faSBill Paul 
338a94100faSBill Paul /*
339a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
340a94100faSBill Paul  */
341a94100faSBill Paul static void
342a94100faSBill Paul re_eeprom_putbyte(sc, addr)
343a94100faSBill Paul 	struct rl_softc		*sc;
344a94100faSBill Paul 	int			addr;
345a94100faSBill Paul {
346a94100faSBill Paul 	register int		d, i;
347a94100faSBill Paul 
348ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
349a94100faSBill Paul 
350a94100faSBill Paul 	/*
351a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
352a94100faSBill Paul 	 */
353ed510fb0SBill Paul 
354ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
355a94100faSBill Paul 		if (d & i) {
356a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
357a94100faSBill Paul 		} else {
358a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
359a94100faSBill Paul 		}
360a94100faSBill Paul 		DELAY(100);
361a94100faSBill Paul 		EE_SET(RL_EE_CLK);
362a94100faSBill Paul 		DELAY(150);
363a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
364a94100faSBill Paul 		DELAY(100);
365a94100faSBill Paul 	}
366ed510fb0SBill Paul 
367ed510fb0SBill Paul 	return;
368a94100faSBill Paul }
369a94100faSBill Paul 
370a94100faSBill Paul /*
371a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
372a94100faSBill Paul  */
373a94100faSBill Paul static void
374a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
375a94100faSBill Paul 	struct rl_softc		*sc;
376a94100faSBill Paul 	int			addr;
377a94100faSBill Paul 	u_int16_t		*dest;
378a94100faSBill Paul {
379a94100faSBill Paul 	register int		i;
380a94100faSBill Paul 	u_int16_t		word = 0;
381a94100faSBill Paul 
382a94100faSBill Paul 	/*
383a94100faSBill Paul 	 * Send address of word we want to read.
384a94100faSBill Paul 	 */
385a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
386a94100faSBill Paul 
387a94100faSBill Paul 	/*
388a94100faSBill Paul 	 * Start reading bits from EEPROM.
389a94100faSBill Paul 	 */
390a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
391a94100faSBill Paul 		EE_SET(RL_EE_CLK);
392a94100faSBill Paul 		DELAY(100);
393a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
394a94100faSBill Paul 			word |= i;
395a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
396a94100faSBill Paul 		DELAY(100);
397a94100faSBill Paul 	}
398a94100faSBill Paul 
399a94100faSBill Paul 	*dest = word;
400ed510fb0SBill Paul 
401ed510fb0SBill Paul 	return;
402a94100faSBill Paul }
403a94100faSBill Paul 
404a94100faSBill Paul /*
405a94100faSBill Paul  * Read a sequence of words from the EEPROM.
406a94100faSBill Paul  */
407a94100faSBill Paul static void
408ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
409a94100faSBill Paul 	struct rl_softc		*sc;
410a94100faSBill Paul 	caddr_t			dest;
411a94100faSBill Paul 	int			off;
412a94100faSBill Paul 	int			cnt;
413a94100faSBill Paul {
414a94100faSBill Paul 	int			i;
415a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
416a94100faSBill Paul 
417ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
418ed510fb0SBill Paul 
419ed510fb0SBill Paul         DELAY(100);
420ed510fb0SBill Paul 
421a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
422ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
423a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
424ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
425a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
426be099007SPyun YongHyeon                 *ptr = word;
427a94100faSBill Paul 	}
428ed510fb0SBill Paul 
429ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
430ed510fb0SBill Paul 
431ed510fb0SBill Paul 	return;
432a94100faSBill Paul }
433a94100faSBill Paul 
434a94100faSBill Paul static int
435a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
436a94100faSBill Paul 	device_t		dev;
437a94100faSBill Paul 	int			phy, reg;
438a94100faSBill Paul {
439a94100faSBill Paul 	struct rl_softc		*sc;
440a94100faSBill Paul 	u_int32_t		rval;
441a94100faSBill Paul 	int			i;
442a94100faSBill Paul 
443a94100faSBill Paul 	if (phy != 1)
444a94100faSBill Paul 		return (0);
445a94100faSBill Paul 
446a94100faSBill Paul 	sc = device_get_softc(dev);
447a94100faSBill Paul 
4489bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4499bac70b8SBill Paul 
4509bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4519bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4529bac70b8SBill Paul 		return (rval);
4539bac70b8SBill Paul 	}
4549bac70b8SBill Paul 
455a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
456a94100faSBill Paul 	DELAY(1000);
457a94100faSBill Paul 
458a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
459a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
460a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
461a94100faSBill Paul 			break;
462a94100faSBill Paul 		DELAY(100);
463a94100faSBill Paul 	}
464a94100faSBill Paul 
465a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4666b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
467a94100faSBill Paul 		return (0);
468a94100faSBill Paul 	}
469a94100faSBill Paul 
470a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
471a94100faSBill Paul }
472a94100faSBill Paul 
473a94100faSBill Paul static int
474a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
475a94100faSBill Paul 	device_t		dev;
476a94100faSBill Paul 	int			phy, reg, data;
477a94100faSBill Paul {
478a94100faSBill Paul 	struct rl_softc		*sc;
479a94100faSBill Paul 	u_int32_t		rval;
480a94100faSBill Paul 	int			i;
481a94100faSBill Paul 
482a94100faSBill Paul 	sc = device_get_softc(dev);
483a94100faSBill Paul 
484a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4859bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
486a94100faSBill Paul 	DELAY(1000);
487a94100faSBill Paul 
488a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
489a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
490a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
491a94100faSBill Paul 			break;
492a94100faSBill Paul 		DELAY(100);
493a94100faSBill Paul 	}
494a94100faSBill Paul 
495a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4966b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
497a94100faSBill Paul 		return (0);
498a94100faSBill Paul 	}
499a94100faSBill Paul 
500a94100faSBill Paul 	return (0);
501a94100faSBill Paul }
502a94100faSBill Paul 
503a94100faSBill Paul static int
504a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
505a94100faSBill Paul 	device_t		dev;
506a94100faSBill Paul 	int			phy, reg;
507a94100faSBill Paul {
508a94100faSBill Paul 	struct rl_softc		*sc;
509a94100faSBill Paul 	u_int16_t		rval = 0;
510a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
511a94100faSBill Paul 
512a94100faSBill Paul 	sc = device_get_softc(dev);
513a94100faSBill Paul 
514a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
515a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
516a94100faSBill Paul 		return (rval);
517a94100faSBill Paul 	}
518a94100faSBill Paul 
519a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
520a94100faSBill Paul 	if (phy) {
521a94100faSBill Paul 		return (0);
522a94100faSBill Paul 	}
523a94100faSBill Paul 	switch (reg) {
524a94100faSBill Paul 	case MII_BMCR:
525a94100faSBill Paul 		re8139_reg = RL_BMCR;
526a94100faSBill Paul 		break;
527a94100faSBill Paul 	case MII_BMSR:
528a94100faSBill Paul 		re8139_reg = RL_BMSR;
529a94100faSBill Paul 		break;
530a94100faSBill Paul 	case MII_ANAR:
531a94100faSBill Paul 		re8139_reg = RL_ANAR;
532a94100faSBill Paul 		break;
533a94100faSBill Paul 	case MII_ANER:
534a94100faSBill Paul 		re8139_reg = RL_ANER;
535a94100faSBill Paul 		break;
536a94100faSBill Paul 	case MII_ANLPAR:
537a94100faSBill Paul 		re8139_reg = RL_LPAR;
538a94100faSBill Paul 		break;
539a94100faSBill Paul 	case MII_PHYIDR1:
540a94100faSBill Paul 	case MII_PHYIDR2:
541a94100faSBill Paul 		return (0);
542a94100faSBill Paul 	/*
543a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
544a94100faSBill Paul 	 * register. If we have a link partner which does not
545a94100faSBill Paul 	 * support NWAY, this is the register which will tell
546a94100faSBill Paul 	 * us the results of parallel detection.
547a94100faSBill Paul 	 */
548a94100faSBill Paul 	case RL_MEDIASTAT:
549a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
550a94100faSBill Paul 		return (rval);
551a94100faSBill Paul 	default:
5526b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
553a94100faSBill Paul 		return (0);
554a94100faSBill Paul 	}
555a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
556baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
557baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
558baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
559baa12772SPyun YongHyeon 	}
560a94100faSBill Paul 	return (rval);
561a94100faSBill Paul }
562a94100faSBill Paul 
563a94100faSBill Paul static int
564a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
565a94100faSBill Paul 	device_t		dev;
566a94100faSBill Paul 	int			phy, reg, data;
567a94100faSBill Paul {
568a94100faSBill Paul 	struct rl_softc		*sc;
569a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
570a94100faSBill Paul 	int			rval = 0;
571a94100faSBill Paul 
572a94100faSBill Paul 	sc = device_get_softc(dev);
573a94100faSBill Paul 
574a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
575a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
576a94100faSBill Paul 		return (rval);
577a94100faSBill Paul 	}
578a94100faSBill Paul 
579a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
58097b9d4baSJohn-Mark Gurney 	if (phy)
581a94100faSBill Paul 		return (0);
58297b9d4baSJohn-Mark Gurney 
583a94100faSBill Paul 	switch (reg) {
584a94100faSBill Paul 	case MII_BMCR:
585a94100faSBill Paul 		re8139_reg = RL_BMCR;
586baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
587baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
588baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
589baa12772SPyun YongHyeon 		}
590a94100faSBill Paul 		break;
591a94100faSBill Paul 	case MII_BMSR:
592a94100faSBill Paul 		re8139_reg = RL_BMSR;
593a94100faSBill Paul 		break;
594a94100faSBill Paul 	case MII_ANAR:
595a94100faSBill Paul 		re8139_reg = RL_ANAR;
596a94100faSBill Paul 		break;
597a94100faSBill Paul 	case MII_ANER:
598a94100faSBill Paul 		re8139_reg = RL_ANER;
599a94100faSBill Paul 		break;
600a94100faSBill Paul 	case MII_ANLPAR:
601a94100faSBill Paul 		re8139_reg = RL_LPAR;
602a94100faSBill Paul 		break;
603a94100faSBill Paul 	case MII_PHYIDR1:
604a94100faSBill Paul 	case MII_PHYIDR2:
605a94100faSBill Paul 		return (0);
606a94100faSBill Paul 		break;
607a94100faSBill Paul 	default:
6086b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
609a94100faSBill Paul 		return (0);
610a94100faSBill Paul 	}
611a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
612a94100faSBill Paul 	return (0);
613a94100faSBill Paul }
614a94100faSBill Paul 
615a94100faSBill Paul static void
616a94100faSBill Paul re_miibus_statchg(dev)
617a94100faSBill Paul 	device_t		dev;
618a94100faSBill Paul {
619a11e2f18SBruce M Simpson 
620a94100faSBill Paul }
621a94100faSBill Paul 
622a94100faSBill Paul /*
623a94100faSBill Paul  * Program the 64-bit multicast hash filter.
624a94100faSBill Paul  */
625a94100faSBill Paul static void
626a94100faSBill Paul re_setmulti(sc)
627a94100faSBill Paul 	struct rl_softc		*sc;
628a94100faSBill Paul {
629a94100faSBill Paul 	struct ifnet		*ifp;
630a94100faSBill Paul 	int			h = 0;
631a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
632a94100faSBill Paul 	struct ifmultiaddr	*ifma;
633a94100faSBill Paul 	u_int32_t		rxfilt;
634a94100faSBill Paul 	int			mcnt = 0;
635bb7dfefbSBill Paul 	u_int32_t		hwrev;
636a94100faSBill Paul 
63797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
63897b9d4baSJohn-Mark Gurney 
639fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
640a94100faSBill Paul 
641a94100faSBill Paul 
6427c103000SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
6437c103000SPyun YongHyeon 	rxfilt &= ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_MULTI);
644a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
6457c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6467c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
647a0637caaSPyun YongHyeon 		/*
648a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
649a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
650a0637caaSPyun YongHyeon 		 * promiscuous mode.
651a0637caaSPyun YongHyeon 		 */
652a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
653a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
654a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
655a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
656a94100faSBill Paul 		return;
657a94100faSBill Paul 	}
658a94100faSBill Paul 
659a94100faSBill Paul 	/* first, zot all the existing hash bits */
660a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
661a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
662a94100faSBill Paul 
663a94100faSBill Paul 	/* now program new ones */
66413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
665a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
666a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
667a94100faSBill Paul 			continue;
6680e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6690e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
670a94100faSBill Paul 		if (h < 32)
671a94100faSBill Paul 			hashes[0] |= (1 << h);
672a94100faSBill Paul 		else
673a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
674a94100faSBill Paul 		mcnt++;
675a94100faSBill Paul 	}
67613b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
677a94100faSBill Paul 
678a94100faSBill Paul 	if (mcnt)
679a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
680a94100faSBill Paul 	else
681a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
682a94100faSBill Paul 
683a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
684bb7dfefbSBill Paul 
685bb7dfefbSBill Paul 	/*
686bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
687bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
688bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
689bb7dfefbSBill Paul 	 * order for those devices.
690bb7dfefbSBill Paul 	 */
691bb7dfefbSBill Paul 
692bb7dfefbSBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
693bb7dfefbSBill Paul 
6941acbb78aSPyun YongHyeon 	switch (hwrev) {
6951acbb78aSPyun YongHyeon 	case RL_HWREV_8100E:
6961acbb78aSPyun YongHyeon 	case RL_HWREV_8101E:
6971acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN1:
6981acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN2:
6991acbb78aSPyun YongHyeon 	case RL_HWREV_8168_SPIN3:
700bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
701bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
7021acbb78aSPyun YongHyeon 		break;
7031acbb78aSPyun YongHyeon 	default:
704a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
705a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
7061acbb78aSPyun YongHyeon 		break;
707a94100faSBill Paul 	}
708bb7dfefbSBill Paul }
709a94100faSBill Paul 
710a94100faSBill Paul static void
711a94100faSBill Paul re_reset(sc)
712a94100faSBill Paul 	struct rl_softc		*sc;
713a94100faSBill Paul {
714a94100faSBill Paul 	register int		i;
715a94100faSBill Paul 
71697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
71797b9d4baSJohn-Mark Gurney 
718a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
719a94100faSBill Paul 
720a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
721a94100faSBill Paul 		DELAY(10);
722a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
723a94100faSBill Paul 			break;
724a94100faSBill Paul 	}
725a94100faSBill Paul 	if (i == RL_TIMEOUT)
7266b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
727a94100faSBill Paul 
728a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
729a94100faSBill Paul }
730a94100faSBill Paul 
731ed510fb0SBill Paul #ifdef RE_DIAG
732ed510fb0SBill Paul 
733a94100faSBill Paul /*
734a94100faSBill Paul  * The following routine is designed to test for a defect on some
735a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
736a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
737a94100faSBill Paul  * should be pulled high. The result of this defect is that the
738a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
739a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
740a94100faSBill Paul  * because the 64-bit data lines aren't connected.
741a94100faSBill Paul  *
742a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
743a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
744a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
745a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
746a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
747a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
748a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
749a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
750a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
751a94100faSBill Paul  */
752a94100faSBill Paul 
753a94100faSBill Paul static int
754a94100faSBill Paul re_diag(sc)
755a94100faSBill Paul 	struct rl_softc		*sc;
756a94100faSBill Paul {
757fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
758a94100faSBill Paul 	struct mbuf		*m0;
759a94100faSBill Paul 	struct ether_header	*eh;
760a94100faSBill Paul 	struct rl_desc		*cur_rx;
761a94100faSBill Paul 	u_int16_t		status;
762a94100faSBill Paul 	u_int32_t		rxstat;
763ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
764a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
765a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
766a94100faSBill Paul 
767a94100faSBill Paul 	/* Allocate a single mbuf */
768a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
769a94100faSBill Paul 	if (m0 == NULL)
770a94100faSBill Paul 		return (ENOBUFS);
771a94100faSBill Paul 
77297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
77397b9d4baSJohn-Mark Gurney 
774a94100faSBill Paul 	/*
775a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
776a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
777a94100faSBill Paul 	 * following special functions:
778a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
779a94100faSBill Paul 	 * - Enables digital loopback mode
780a94100faSBill Paul 	 * - Leaves interrupts turned off
781a94100faSBill Paul 	 */
782a94100faSBill Paul 
783a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
784a94100faSBill Paul 	sc->rl_testmode = 1;
785ed510fb0SBill Paul 	re_reset(sc);
78697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
787ed510fb0SBill Paul 	sc->rl_link = 1;
788ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
789ed510fb0SBill Paul 		phyaddr = 1;
790ed510fb0SBill Paul 	else
791ed510fb0SBill Paul 		phyaddr = 0;
792ed510fb0SBill Paul 
793ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
794ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
795ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
796ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
797ed510fb0SBill Paul 			break;
798ed510fb0SBill Paul 	}
799ed510fb0SBill Paul 
800ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
801ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
802ed510fb0SBill Paul 
803804af9a1SBill Paul 	DELAY(100000);
804a94100faSBill Paul 
805a94100faSBill Paul 	/* Put some data in the mbuf */
806a94100faSBill Paul 
807a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
808a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
809a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
810a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
811a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
812a94100faSBill Paul 
8137cae6651SBill Paul 	/*
8147cae6651SBill Paul 	 * Queue the packet, start transmission.
8157cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8167cae6651SBill Paul 	 */
817a94100faSBill Paul 
818abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
81997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
82052732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8217cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
82297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
823a94100faSBill Paul 	m0 = NULL;
824a94100faSBill Paul 
825a94100faSBill Paul 	/* Wait for it to propagate through the chip */
826a94100faSBill Paul 
827abc8ff44SBill Paul 	DELAY(100000);
828a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
829a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
830ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
831abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
832abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
833a94100faSBill Paul 			break;
834a94100faSBill Paul 		DELAY(10);
835a94100faSBill Paul 	}
836a94100faSBill Paul 
837a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8386b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8396b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8406b9f5c94SGleb Smirnoff 		    " loopback mode\n");
841a94100faSBill Paul 		error = EIO;
842a94100faSBill Paul 		goto done;
843a94100faSBill Paul 	}
844a94100faSBill Paul 
845a94100faSBill Paul 	/*
846a94100faSBill Paul 	 * The packet should have been dumped into the first
847a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
848a94100faSBill Paul 	 */
849a94100faSBill Paul 
850a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
851a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
852a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
853d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
854d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
855d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
856d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
857d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
858a94100faSBill Paul 
859d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
860d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
861a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
862a94100faSBill Paul 
863a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
864a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
865a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
866a94100faSBill Paul 
867a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8686b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8696b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
870a94100faSBill Paul 		error = EIO;
871a94100faSBill Paul 		goto done;
872a94100faSBill Paul 	}
873a94100faSBill Paul 
874a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
875a94100faSBill Paul 
876a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
877a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
878a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8796b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8806b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
881a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8826b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
883a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
884a94100faSBill Paul 		    ntohs(eh->ether_type));
8856b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8866b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8876b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8886b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8896b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8906b9f5c94SGleb Smirnoff 		    "details.\n");
891a94100faSBill Paul 		error = EIO;
892a94100faSBill Paul 	}
893a94100faSBill Paul 
894a94100faSBill Paul done:
895a94100faSBill Paul 	/* Turn interface off, release resources */
896a94100faSBill Paul 
897a94100faSBill Paul 	sc->rl_testmode = 0;
898ed510fb0SBill Paul 	sc->rl_link = 0;
899a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
900a94100faSBill Paul 	re_stop(sc);
901a94100faSBill Paul 	if (m0 != NULL)
902a94100faSBill Paul 		m_freem(m0);
903a94100faSBill Paul 
90497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
90597b9d4baSJohn-Mark Gurney 
906a94100faSBill Paul 	return (error);
907a94100faSBill Paul }
908a94100faSBill Paul 
909ed510fb0SBill Paul #endif
910ed510fb0SBill Paul 
911a94100faSBill Paul /*
912a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
913a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
914a94100faSBill Paul  */
915a94100faSBill Paul static int
916a94100faSBill Paul re_probe(dev)
917a94100faSBill Paul 	device_t		dev;
918a94100faSBill Paul {
919a94100faSBill Paul 	struct rl_type		*t;
920dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
921dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
922dfdb409eSPyun YongHyeon 	int			i;
923a94100faSBill Paul 
924dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
925dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
926dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
927dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
928a94100faSBill Paul 
929dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
930dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
93126390635SJohn Baldwin 			/*
93226390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
933dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
93426390635SJohn Baldwin 			 */
935a94100faSBill Paul 			return (ENXIO);
936a94100faSBill Paul 		}
937dfdb409eSPyun YongHyeon 	}
938dfdb409eSPyun YongHyeon 
939dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
940dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
941dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
942dfdb409eSPyun YongHyeon 			return (ENXIO);
943dfdb409eSPyun YongHyeon 		}
944dfdb409eSPyun YongHyeon 	}
945dfdb409eSPyun YongHyeon 
946dfdb409eSPyun YongHyeon 	t = re_devs;
947dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
948dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
949a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
950d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
951a94100faSBill Paul 		}
952a94100faSBill Paul 	}
953a94100faSBill Paul 
954a94100faSBill Paul 	return (ENXIO);
955a94100faSBill Paul }
956a94100faSBill Paul 
957a94100faSBill Paul /*
958a94100faSBill Paul  * Map a single buffer address.
959a94100faSBill Paul  */
960a94100faSBill Paul 
961a94100faSBill Paul static void
962a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
963a94100faSBill Paul 	void			*arg;
964a94100faSBill Paul 	bus_dma_segment_t	*segs;
965a94100faSBill Paul 	int			nseg;
966a94100faSBill Paul 	int			error;
967a94100faSBill Paul {
9688fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
969a94100faSBill Paul 
970a94100faSBill Paul 	if (error)
971a94100faSBill Paul 		return;
972a94100faSBill Paul 
973a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
974a94100faSBill Paul 	addr = arg;
975a94100faSBill Paul 	*addr = segs->ds_addr;
976a94100faSBill Paul }
977a94100faSBill Paul 
978a94100faSBill Paul static int
979a94100faSBill Paul re_allocmem(dev, sc)
980a94100faSBill Paul 	device_t		dev;
981a94100faSBill Paul 	struct rl_softc		*sc;
982a94100faSBill Paul {
983d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
984a94100faSBill Paul 	int			error;
985a94100faSBill Paul 	int			i;
986a94100faSBill Paul 
987d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
988d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
989d65abd66SPyun YongHyeon 
990d65abd66SPyun YongHyeon 	/*
991d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
992ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
993ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
994ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
995ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
996ce628393SPyun YongHyeon 	 * may not have the limitation but I took safer path.
997d65abd66SPyun YongHyeon 	 */
998d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
999ce628393SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
1000d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1001d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
1002d65abd66SPyun YongHyeon 	if (error) {
1003d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
1004d65abd66SPyun YongHyeon 		return (error);
1005d65abd66SPyun YongHyeon 	}
1006d65abd66SPyun YongHyeon 
1007d65abd66SPyun YongHyeon 	/*
1008d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
1009d65abd66SPyun YongHyeon 	 */
1010d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1011d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1012d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1013d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1014d65abd66SPyun YongHyeon 	if (error) {
1015d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1016d65abd66SPyun YongHyeon 		return (error);
1017d65abd66SPyun YongHyeon 	}
1018d65abd66SPyun YongHyeon 
1019a94100faSBill Paul 	/*
1020a94100faSBill Paul 	 * Allocate map for RX mbufs.
1021a94100faSBill Paul 	 */
1022d65abd66SPyun YongHyeon 
1023d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1024d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1025d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1026a94100faSBill Paul 	if (error) {
1027d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1028d65abd66SPyun YongHyeon 		return (error);
1029a94100faSBill Paul 	}
1030a94100faSBill Paul 
1031a94100faSBill Paul 	/*
1032a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1033a94100faSBill Paul 	 */
1034a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1035a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1036d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1037a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1038a94100faSBill Paul 	if (error) {
1039d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1040d65abd66SPyun YongHyeon 		return (error);
1041a94100faSBill Paul 	}
1042a94100faSBill Paul 
1043a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1044a94100faSBill Paul 
1045a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1046d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1047d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1048a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1049d65abd66SPyun YongHyeon 	if (error) {
1050d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1051d65abd66SPyun YongHyeon 		return (error);
1052d65abd66SPyun YongHyeon 	}
1053a94100faSBill Paul 
1054a94100faSBill Paul 	/* Load the map for the TX ring. */
1055a94100faSBill Paul 
1056d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1057a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1058a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1059d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1060a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1061d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1062d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1063d65abd66SPyun YongHyeon 		return (ENOMEM);
1064d65abd66SPyun YongHyeon 	}
1065a94100faSBill Paul 
1066a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1067a94100faSBill Paul 
1068d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1069d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1070d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1071a94100faSBill Paul 		if (error) {
1072d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1073d65abd66SPyun YongHyeon 			return (error);
1074a94100faSBill Paul 		}
1075a94100faSBill Paul 	}
1076a94100faSBill Paul 
1077a94100faSBill Paul 	/*
1078a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1079a94100faSBill Paul 	 */
1080a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1081a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1082d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1083a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1084a94100faSBill Paul 	if (error) {
1085d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1086d65abd66SPyun YongHyeon 		return (error);
1087a94100faSBill Paul 	}
1088a94100faSBill Paul 
1089a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1090a94100faSBill Paul 
1091a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1092d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1093d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1094a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1095d65abd66SPyun YongHyeon 	if (error) {
1096d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1097d65abd66SPyun YongHyeon 		return (error);
1098d65abd66SPyun YongHyeon 	}
1099a94100faSBill Paul 
1100a94100faSBill Paul 	/* Load the map for the RX ring. */
1101a94100faSBill Paul 
1102d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1103a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1104a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1105d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1106a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1107d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1108d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1109d65abd66SPyun YongHyeon 		return (ENOMEM);
1110d65abd66SPyun YongHyeon 	}
1111a94100faSBill Paul 
1112a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1113a94100faSBill Paul 
1114d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1115d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1116a94100faSBill Paul 	if (error) {
1117d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1118d65abd66SPyun YongHyeon 		return (error);
1119d65abd66SPyun YongHyeon 	}
1120d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1121d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1122d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1123d65abd66SPyun YongHyeon 		if (error) {
1124d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1125d65abd66SPyun YongHyeon 			return (error);
1126a94100faSBill Paul 		}
1127a94100faSBill Paul 	}
1128a94100faSBill Paul 
1129a94100faSBill Paul 	return (0);
1130a94100faSBill Paul }
1131a94100faSBill Paul 
1132a94100faSBill Paul /*
1133a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1134a94100faSBill Paul  * setup and ethernet/BPF attach.
1135a94100faSBill Paul  */
1136a94100faSBill Paul static int
1137a94100faSBill Paul re_attach(dev)
1138a94100faSBill Paul 	device_t		dev;
1139a94100faSBill Paul {
1140a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1141be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1142a94100faSBill Paul 	struct rl_softc		*sc;
1143a94100faSBill Paul 	struct ifnet		*ifp;
1144a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1145a94100faSBill Paul 	int			hwrev;
1146a94100faSBill Paul 	u_int16_t		re_did = 0;
1147d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
11485774c5ffSPyun YongHyeon 	int			msic, reg;
114903ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1150a94100faSBill Paul 
1151a94100faSBill Paul 	sc = device_get_softc(dev);
1152ed510fb0SBill Paul 	sc->rl_dev = dev;
1153a94100faSBill Paul 
1154a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
115597b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1156d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1157d1754a9bSJohn Baldwin 
1158a94100faSBill Paul 	/*
1159a94100faSBill Paul 	 * Map control/status registers.
1160a94100faSBill Paul 	 */
1161a94100faSBill Paul 	pci_enable_busmaster(dev);
1162a94100faSBill Paul 
1163a94100faSBill Paul 	rid = RL_RID;
11645f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
11655f96beb9SNate Lawson 	    RF_ACTIVE);
1166a94100faSBill Paul 
1167a94100faSBill Paul 	if (sc->rl_res == NULL) {
1168d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1169a94100faSBill Paul 		error = ENXIO;
1170a94100faSBill Paul 		goto fail;
1171a94100faSBill Paul 	}
1172a94100faSBill Paul 
1173a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1174a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1175a94100faSBill Paul 
11765774c5ffSPyun YongHyeon 	msic = 0;
11775774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
11785774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
11795774c5ffSPyun YongHyeon 		if (bootverbose)
11805774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
11815774c5ffSPyun YongHyeon 	}
11825774c5ffSPyun YongHyeon 	if (msic == RL_MSI_MESSAGES  && msi_disable == 0) {
11835774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
11845774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
11855774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
11865774c5ffSPyun YongHyeon 				    msic);
11875774c5ffSPyun YongHyeon 				sc->rl_msi = 1;
1188339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1189339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1190339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1191339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1192339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1193339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, 0);
11945774c5ffSPyun YongHyeon 			} else
11955774c5ffSPyun YongHyeon 				pci_release_msi(dev);
11965774c5ffSPyun YongHyeon 		}
11975774c5ffSPyun YongHyeon 	}
1198a94100faSBill Paul 
11995774c5ffSPyun YongHyeon 	/* Allocate interrupt */
12005774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
12015774c5ffSPyun YongHyeon 		rid = 0;
12025774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
12035774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
12045774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
12055774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1206a94100faSBill Paul 			error = ENXIO;
1207a94100faSBill Paul 			goto fail;
1208a94100faSBill Paul 		}
12095774c5ffSPyun YongHyeon 	} else {
12105774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
12115774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
12125774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
12135774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
12145774c5ffSPyun YongHyeon 				device_printf(dev,
12155774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12165774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12175774c5ffSPyun YongHyeon 				error = ENXIO;
12185774c5ffSPyun YongHyeon 				goto fail;
12195774c5ffSPyun YongHyeon 			}
12205774c5ffSPyun YongHyeon 		}
12215774c5ffSPyun YongHyeon 	}
1222a94100faSBill Paul 
1223a94100faSBill Paul 	/* Reset the adapter. */
122497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1225a94100faSBill Paul 	re_reset(sc);
122697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1227abc8ff44SBill Paul 
1228abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1229abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1230abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1231abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1232abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1233abc8ff44SBill Paul 			break;
1234abc8ff44SBill Paul 		}
1235abc8ff44SBill Paul 		hw_rev++;
1236abc8ff44SBill Paul 	}
1237d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1238d65abd66SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: %08x\n", hwrev);
1239d65abd66SPyun YongHyeon 		error = ENXIO;
1240d65abd66SPyun YongHyeon 		goto fail;
1241d65abd66SPyun YongHyeon 	}
1242abc8ff44SBill Paul 
1243141f92e7SPyun YongHyeon 	sc->rl_eewidth = RL_9356_ADDR_LEN;
1244ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1245a94100faSBill Paul 	if (re_did != 0x8129)
1246141f92e7SPyun YongHyeon 	        sc->rl_eewidth = RL_9346_ADDR_LEN;
1247a94100faSBill Paul 
1248a94100faSBill Paul 	/*
1249a94100faSBill Paul 	 * Get station address from the EEPROM.
1250a94100faSBill Paul 	 */
1251ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1252be099007SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1253be099007SPyun YongHyeon 		as[i] = le16toh(as[i]);
1254be099007SPyun YongHyeon 	bcopy(as, eaddr, sizeof(eaddr));
1255ed510fb0SBill Paul 
1256ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1257d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1258ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1259ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1260d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1261d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1262ed510fb0SBill Paul 	} else {
1263d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1264ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1265ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1266d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1267d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1268abc8ff44SBill Paul 	}
1269dfdb409eSPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1270dfdb409eSPyun YongHyeon 		device_printf(dev, "Unsupported revision : 0x%08x\n", hwrev);
1271dfdb409eSPyun YongHyeon 		error = ENXIO;
1272dfdb409eSPyun YongHyeon 		goto fail;
1273dfdb409eSPyun YongHyeon 	}
12749bac70b8SBill Paul 
1275a94100faSBill Paul 	error = re_allocmem(dev, sc);
1276a94100faSBill Paul 	if (error)
1277a94100faSBill Paul 		goto fail;
1278a94100faSBill Paul 
1279cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1280cd036ec1SBrooks Davis 	if (ifp == NULL) {
1281d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1282cd036ec1SBrooks Davis 		error = ENOSPC;
1283cd036ec1SBrooks Davis 		goto fail;
1284cd036ec1SBrooks Davis 	}
1285cd036ec1SBrooks Davis 
1286a94100faSBill Paul 	/* Do MII setup */
1287a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1288a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1289d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1290a94100faSBill Paul 		error = ENXIO;
1291a94100faSBill Paul 		goto fail;
1292a94100faSBill Paul 	}
1293a94100faSBill Paul 
1294c4aca09aSPyun YongHyeon 	/* Take PHY out of power down mode. */
1295c4aca09aSPyun YongHyeon 	if (sc->rl_type == RL_8169) {
1296c4aca09aSPyun YongHyeon 		uint32_t rev;
1297c4aca09aSPyun YongHyeon 
1298c4aca09aSPyun YongHyeon 		rev = CSR_READ_4(sc, RL_TXCFG);
1299c4aca09aSPyun YongHyeon 		/* HWVERID 0, 1 and 2 :  bit26-30, bit23 */
1300c4aca09aSPyun YongHyeon 		rev &= 0x7c800000;
1301c4aca09aSPyun YongHyeon 		if (rev != 0) {
1302c4aca09aSPyun YongHyeon 			/* RTL8169S single chip */
1303c4aca09aSPyun YongHyeon 			switch (rev) {
1304c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SB:
1305c4aca09aSPyun YongHyeon 			case RL_HWREV_8169_8110SC:
1306c4aca09aSPyun YongHyeon 			case RL_HWREV_8168_SPIN2:
13071acbb78aSPyun YongHyeon 			case RL_HWREV_8168_SPIN3:
1308c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x1f, 0);
1309c4aca09aSPyun YongHyeon 				re_gmii_writereg(dev, 1, 0x0e, 0);
1310c4aca09aSPyun YongHyeon 				break;
1311c4aca09aSPyun YongHyeon 			default:
1312c4aca09aSPyun YongHyeon 				break;
1313c4aca09aSPyun YongHyeon 			}
1314c4aca09aSPyun YongHyeon 		}
1315c4aca09aSPyun YongHyeon 	}
1316c4aca09aSPyun YongHyeon 
1317a94100faSBill Paul 	ifp->if_softc = sc;
13189bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1319a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1320a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1321a94100faSBill Paul 	ifp->if_start = re_start;
1322d65abd66SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO;
1323d65abd66SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1324498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1325a94100faSBill Paul 	ifp->if_init = re_init;
132652732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
132752732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
132852732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1329a94100faSBill Paul 
1330ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1331ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1332ed510fb0SBill Paul 
1333a94100faSBill Paul 	/*
1334a94100faSBill Paul 	 * Call MI attach routine.
1335a94100faSBill Paul 	 */
1336a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1337a94100faSBill Paul 
1338960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1339960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1340960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1341960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
13427467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
13437467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
13447467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1345960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1346960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1347960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1348960fd5b3SPyun YongHyeon #endif
1349960fd5b3SPyun YongHyeon 	/*
1350960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1351960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1352960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1353960fd5b3SPyun YongHyeon 	 */
1354960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1355960fd5b3SPyun YongHyeon 
1356ed510fb0SBill Paul #ifdef RE_DIAG
1357ed510fb0SBill Paul 	/*
1358ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1359ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1360ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1361ed510fb0SBill Paul 	 */
1362a94100faSBill Paul 
1363ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1364ed510fb0SBill Paul 		error = re_diag(sc);
1365a94100faSBill Paul 		if (error) {
1366ed510fb0SBill Paul 			device_printf(dev,
1367ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1368a94100faSBill Paul 			ether_ifdetach(ifp);
1369a94100faSBill Paul 			goto fail;
1370a94100faSBill Paul 		}
1371ed510fb0SBill Paul 	}
1372ed510fb0SBill Paul #endif
1373a94100faSBill Paul 
1374a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
13755774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0)
13765774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
13775774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
13785774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
13795774c5ffSPyun YongHyeon 	else {
13805774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
13815774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
13825774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
13835774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
13845774c5ffSPyun YongHyeon 			if (error != 0)
13855774c5ffSPyun YongHyeon 				break;
13865774c5ffSPyun YongHyeon 		}
13875774c5ffSPyun YongHyeon 	}
1388a94100faSBill Paul 	if (error) {
1389d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1390a94100faSBill Paul 		ether_ifdetach(ifp);
1391a94100faSBill Paul 	}
1392a94100faSBill Paul 
1393a94100faSBill Paul fail:
1394ed510fb0SBill Paul 
1395a94100faSBill Paul 	if (error)
1396a94100faSBill Paul 		re_detach(dev);
1397a94100faSBill Paul 
1398a94100faSBill Paul 	return (error);
1399a94100faSBill Paul }
1400a94100faSBill Paul 
1401a94100faSBill Paul /*
1402a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1403a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1404a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1405a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1406a94100faSBill Paul  * allocated.
1407a94100faSBill Paul  */
1408a94100faSBill Paul static int
1409a94100faSBill Paul re_detach(dev)
1410a94100faSBill Paul 	device_t		dev;
1411a94100faSBill Paul {
1412a94100faSBill Paul 	struct rl_softc		*sc;
1413a94100faSBill Paul 	struct ifnet		*ifp;
14145774c5ffSPyun YongHyeon 	int			i, rid;
1415a94100faSBill Paul 
1416a94100faSBill Paul 	sc = device_get_softc(dev);
1417fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1418aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
141997b9d4baSJohn-Mark Gurney 
142040929967SGleb Smirnoff #ifdef DEVICE_POLLING
142140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
142240929967SGleb Smirnoff 		ether_poll_deregister(ifp);
142340929967SGleb Smirnoff #endif
142497b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
1425525e6a87SRuslan Ermilov 	if (device_is_attached(dev)) {
142697b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
142797b9d4baSJohn-Mark Gurney #if 0
142897b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
142997b9d4baSJohn-Mark Gurney #endif
1430a94100faSBill Paul 		re_stop(sc);
1431525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1432d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
14333d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
14343d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1435a94100faSBill Paul 		/*
1436a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1437a94100faSBill Paul 		 * still had a BPF descriptor attached to this
143897b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1439a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1440a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1441a94100faSBill Paul 		 * which will try to call re_init() again. This will
1442a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1443a94100faSBill Paul 		 * which will panic the system when the kernel tries
1444a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1445a94100faSBill Paul 		 * anymore.
1446a94100faSBill Paul 		 */
1447a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1448525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1449a94100faSBill Paul 	}
1450a94100faSBill Paul 	if (sc->rl_miibus)
1451a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1452a94100faSBill Paul 	bus_generic_detach(dev);
1453a94100faSBill Paul 
145497b9d4baSJohn-Mark Gurney 	/*
145597b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
145697b9d4baSJohn-Mark Gurney 	 * stopped here.
145797b9d4baSJohn-Mark Gurney 	 */
145897b9d4baSJohn-Mark Gurney 
14595774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
14605774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
14615774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
14625774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
14635774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
14645774c5ffSPyun YongHyeon 		}
14655774c5ffSPyun YongHyeon 	}
1466ad4f426eSWarner Losh 	if (ifp != NULL)
1467ad4f426eSWarner Losh 		if_free(ifp);
14685774c5ffSPyun YongHyeon 	if (sc->rl_msi == 0) {
14695774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
14705774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
14715774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
14725774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
14735774c5ffSPyun YongHyeon 		}
14745774c5ffSPyun YongHyeon 	} else {
14755774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
14765774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
14775774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
14785774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
14795774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
14805774c5ffSPyun YongHyeon 			}
14815774c5ffSPyun YongHyeon 		}
14825774c5ffSPyun YongHyeon 		pci_release_msi(dev);
14835774c5ffSPyun YongHyeon 	}
1484a94100faSBill Paul 	if (sc->rl_res)
1485a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1486a94100faSBill Paul 
1487a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1488a94100faSBill Paul 
1489a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1490a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1491a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1492a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1493a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1494a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1495a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1496a94100faSBill Paul 	}
1497a94100faSBill Paul 
1498a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1499a94100faSBill Paul 
1500a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1501a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1502a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1503a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1504a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1505a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1506a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1507a94100faSBill Paul 	}
1508a94100faSBill Paul 
1509a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1510a94100faSBill Paul 
1511d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
1512d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1513d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1514d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1515d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1516d65abd66SPyun YongHyeon 	}
1517d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
1518d65abd66SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++)
1519d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1520d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1521d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1522d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1523d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1524d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1525a94100faSBill Paul 	}
1526a94100faSBill Paul 
1527a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1528a94100faSBill Paul 
1529a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1530a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1531a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1532a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1533a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1534a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1535a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1536a94100faSBill Paul 	}
1537a94100faSBill Paul 
1538a94100faSBill Paul 	if (sc->rl_parent_tag)
1539a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1540a94100faSBill Paul 
1541a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1542a94100faSBill Paul 
1543a94100faSBill Paul 	return (0);
1544a94100faSBill Paul }
1545a94100faSBill Paul 
1546d65abd66SPyun YongHyeon static __inline void
1547d65abd66SPyun YongHyeon re_discard_rxbuf(sc, idx)
1548a94100faSBill Paul 	struct rl_softc		*sc;
1549a94100faSBill Paul 	int			idx;
1550a94100faSBill Paul {
1551d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1552d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1553d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1554a94100faSBill Paul 
1555d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1556d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1557d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1558d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1559d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1560d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1561d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1562d65abd66SPyun YongHyeon }
1563d65abd66SPyun YongHyeon 
1564d65abd66SPyun YongHyeon static int
1565d65abd66SPyun YongHyeon re_newbuf(sc, idx)
1566d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
1567d65abd66SPyun YongHyeon 	int			idx;
1568d65abd66SPyun YongHyeon {
1569d65abd66SPyun YongHyeon 	struct mbuf		*m;
1570d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1571d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1572d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1573d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1574d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1575d65abd66SPyun YongHyeon 	int			error, nsegs;
1576d65abd66SPyun YongHyeon 
1577d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1578d65abd66SPyun YongHyeon 	if (m == NULL)
1579a94100faSBill Paul 		return (ENOBUFS);
1580a94100faSBill Paul 
1581a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
158222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
158322a11c96SJohn-Mark Gurney 	/*
158422a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
158522a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
158622a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
158722a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
158822a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
158922a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
159022a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
159122a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
159222a11c96SJohn-Mark Gurney 	 */
159322a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
159422a11c96SJohn-Mark Gurney #endif
1595d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1596d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1597d65abd66SPyun YongHyeon 	if (error != 0) {
1598d65abd66SPyun YongHyeon 		m_freem(m);
1599d65abd66SPyun YongHyeon 		return (ENOBUFS);
1600d65abd66SPyun YongHyeon 	}
1601d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1602a94100faSBill Paul 
1603d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1604d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1605d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1606d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1607d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1608a94100faSBill Paul 	}
1609a94100faSBill Paul 
1610d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1611d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1612d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1613d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1614d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1615d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1616a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1617a94100faSBill Paul 
1618d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1619d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1620d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1621d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1622d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1623d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1624d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1625d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1626d65abd66SPyun YongHyeon 
1627a94100faSBill Paul 	return (0);
1628a94100faSBill Paul }
1629a94100faSBill Paul 
163022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
163122a11c96SJohn-Mark Gurney static __inline void
163222a11c96SJohn-Mark Gurney re_fixup_rx(m)
163322a11c96SJohn-Mark Gurney 	struct mbuf		*m;
163422a11c96SJohn-Mark Gurney {
163522a11c96SJohn-Mark Gurney 	int                     i;
163622a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
163722a11c96SJohn-Mark Gurney 
163822a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
163922a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
164022a11c96SJohn-Mark Gurney 
164122a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
164222a11c96SJohn-Mark Gurney 		*dst++ = *src++;
164322a11c96SJohn-Mark Gurney 
164422a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
164522a11c96SJohn-Mark Gurney 
164622a11c96SJohn-Mark Gurney 	return;
164722a11c96SJohn-Mark Gurney }
164822a11c96SJohn-Mark Gurney #endif
164922a11c96SJohn-Mark Gurney 
1650a94100faSBill Paul static int
1651a94100faSBill Paul re_tx_list_init(sc)
1652a94100faSBill Paul 	struct rl_softc		*sc;
1653a94100faSBill Paul {
1654d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1655d65abd66SPyun YongHyeon 	int			i;
165697b9d4baSJohn-Mark Gurney 
165797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
165897b9d4baSJohn-Mark Gurney 
1659d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1660d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1661d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1662d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1663d65abd66SPyun YongHyeon 	/* Set EOR. */
1664d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1665d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1666a94100faSBill Paul 
1667a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1668d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1669d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1670d65abd66SPyun YongHyeon 
1671a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1672a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1673d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1674a94100faSBill Paul 
1675a94100faSBill Paul 	return (0);
1676a94100faSBill Paul }
1677a94100faSBill Paul 
1678a94100faSBill Paul static int
1679a94100faSBill Paul re_rx_list_init(sc)
1680a94100faSBill Paul 	struct rl_softc		*sc;
1681a94100faSBill Paul {
1682d65abd66SPyun YongHyeon 	int			error, i;
1683a94100faSBill Paul 
1684d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1685d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1686d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1687d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1688d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1689d65abd66SPyun YongHyeon 			return (error);
1690a94100faSBill Paul 	}
1691a94100faSBill Paul 
1692a94100faSBill Paul 	/* Flush the RX descriptors */
1693a94100faSBill Paul 
1694a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1695a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1696a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1697a94100faSBill Paul 
1698a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1699a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1700a94100faSBill Paul 
1701a94100faSBill Paul 	return (0);
1702a94100faSBill Paul }
1703a94100faSBill Paul 
1704a94100faSBill Paul /*
1705a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1706a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1707a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1708a94100faSBill Paul  */
1709ed510fb0SBill Paul static int
1710a94100faSBill Paul re_rxeof(sc)
1711a94100faSBill Paul 	struct rl_softc		*sc;
1712a94100faSBill Paul {
1713a94100faSBill Paul 	struct mbuf		*m;
1714a94100faSBill Paul 	struct ifnet		*ifp;
1715a94100faSBill Paul 	int			i, total_len;
1716a94100faSBill Paul 	struct rl_desc		*cur_rx;
1717a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1718ed510fb0SBill Paul 	int			maxpkt = 16;
1719a94100faSBill Paul 
17205120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
17215120abbfSSam Leffler 
1722fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1723a94100faSBill Paul 
1724a94100faSBill Paul 	/* Invalidate the descriptor memory */
1725a94100faSBill Paul 
1726a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1727a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1728d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1729a94100faSBill Paul 
1730d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1731d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
1732a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1733a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1734d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1735d65abd66SPyun YongHyeon 			break;
1736d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
1737a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1738d65abd66SPyun YongHyeon 		m = sc->rl_ldata.rl_rx_desc[i].rx_m;
1739a94100faSBill Paul 
1740a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1741d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
1742d65abd66SPyun YongHyeon 				/*
1743d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
1744d65abd66SPyun YongHyeon 				 * discard all the pieces.
1745d65abd66SPyun YongHyeon 				 */
1746d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
1747d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
1748d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
1749d65abd66SPyun YongHyeon 				}
1750d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
1751d65abd66SPyun YongHyeon 				continue;
1752d65abd66SPyun YongHyeon 			}
175322a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1754a94100faSBill Paul 			if (sc->rl_head == NULL)
1755a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1756a94100faSBill Paul 			else {
1757a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1758a94100faSBill Paul 				sc->rl_tail->m_next = m;
1759a94100faSBill Paul 				sc->rl_tail = m;
1760a94100faSBill Paul 			}
1761a94100faSBill Paul 			continue;
1762a94100faSBill Paul 		}
1763a94100faSBill Paul 
1764a94100faSBill Paul 		/*
1765a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1766a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1767a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1768a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1769a94100faSBill Paul 		 * were already used, so to make room for the extra
1770a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1771a94100faSBill Paul 		 * error' bit and shifted the other status bits
1772a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1773a94100faSBill Paul 		 * still in the same places. We have already extracted
1774a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1775a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1776a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1777a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1778a94100faSBill Paul 		 * same format as that of the 8139C+.
1779a94100faSBill Paul 		 */
1780a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1781a94100faSBill Paul 			rxstat >>= 1;
1782a94100faSBill Paul 
178322a11c96SJohn-Mark Gurney 		/*
178422a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
178522a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
178622a11c96SJohn-Mark Gurney 		 */
178722a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
178822a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1789a94100faSBill Paul 			ifp->if_ierrors++;
1790a94100faSBill Paul 			/*
1791a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1792a94100faSBill Paul 			 * discard all the pieces.
1793a94100faSBill Paul 			 */
1794a94100faSBill Paul 			if (sc->rl_head != NULL) {
1795a94100faSBill Paul 				m_freem(sc->rl_head);
1796a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1797a94100faSBill Paul 			}
1798d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1799a94100faSBill Paul 			continue;
1800a94100faSBill Paul 		}
1801a94100faSBill Paul 
1802a94100faSBill Paul 		/*
1803a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1804a94100faSBill Paul 		 * reload the current one.
1805a94100faSBill Paul 		 */
1806a94100faSBill Paul 
1807d65abd66SPyun YongHyeon 		if (re_newbuf(sc, i) != 0) {
1808d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
1809a94100faSBill Paul 			if (sc->rl_head != NULL) {
1810a94100faSBill Paul 				m_freem(sc->rl_head);
1811a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1812a94100faSBill Paul 			}
1813d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
1814a94100faSBill Paul 			continue;
1815a94100faSBill Paul 		}
1816a94100faSBill Paul 
1817a94100faSBill Paul 		if (sc->rl_head != NULL) {
181822a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
181922a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
182022a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1821a94100faSBill Paul 			/*
1822a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1823a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1824a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1825a94100faSBill Paul 			 * care about anyway.
1826a94100faSBill Paul 			 */
1827a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1828a94100faSBill Paul 				sc->rl_tail->m_len -=
1829a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1830a94100faSBill Paul 				m_freem(m);
1831a94100faSBill Paul 			} else {
1832a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1833a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1834a94100faSBill Paul 				sc->rl_tail->m_next = m;
1835a94100faSBill Paul 			}
1836a94100faSBill Paul 			m = sc->rl_head;
1837a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1838a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1839a94100faSBill Paul 		} else
1840a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1841a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1842a94100faSBill Paul 
184322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
184422a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
184522a11c96SJohn-Mark Gurney #endif
1846a94100faSBill Paul 		ifp->if_ipackets++;
1847a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1848a94100faSBill Paul 
1849a94100faSBill Paul 		/* Do RX checksumming if enabled */
1850a94100faSBill Paul 
1851a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1852a94100faSBill Paul 
1853a94100faSBill Paul 			/* Check IP header checksum */
1854a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1855a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1856a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1857a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1858a94100faSBill Paul 
1859a94100faSBill Paul 			/* Check TCP/UDP checksum */
1860a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1861a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1862a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1863a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1864a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1865a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1866a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1867a94100faSBill Paul 			}
1868a94100faSBill Paul 		}
1869ed510fb0SBill Paul 		maxpkt--;
1870d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
187178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
187278ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
187378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1874d147662cSGleb Smirnoff 		}
18755120abbfSSam Leffler 		RL_UNLOCK(sc);
1876a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
18775120abbfSSam Leffler 		RL_LOCK(sc);
1878a94100faSBill Paul 	}
1879a94100faSBill Paul 
1880a94100faSBill Paul 	/* Flush the RX DMA ring */
1881a94100faSBill Paul 
1882a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1883a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1884a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1885a94100faSBill Paul 
1886a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1887ed510fb0SBill Paul 
1888ed510fb0SBill Paul 	if (maxpkt)
1889ed510fb0SBill Paul 		return(EAGAIN);
1890ed510fb0SBill Paul 
1891ed510fb0SBill Paul 	return(0);
1892a94100faSBill Paul }
1893a94100faSBill Paul 
1894a94100faSBill Paul static void
1895a94100faSBill Paul re_txeof(sc)
1896a94100faSBill Paul 	struct rl_softc		*sc;
1897a94100faSBill Paul {
1898a94100faSBill Paul 	struct ifnet		*ifp;
1899d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
1900a94100faSBill Paul 	u_int32_t		txstat;
1901d65abd66SPyun YongHyeon 	int			cons;
1902d65abd66SPyun YongHyeon 
1903d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
1904d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
1905d65abd66SPyun YongHyeon 		return;
1906a94100faSBill Paul 
1907fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1908a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1909a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1910a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1911d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1912a94100faSBill Paul 
1913d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
1914d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
1915d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
1916d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
1917a94100faSBill Paul 			break;
1918a94100faSBill Paul 		/*
1919a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1920a94100faSBill Paul 		 * in a fragment chain, which also happens to
1921a94100faSBill Paul 		 * be the only place where the TX status bits
1922a94100faSBill Paul 		 * are valid.
1923a94100faSBill Paul 		 */
1924a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1925d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
1926d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
1927d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
1928d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
1929d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
1930d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
1931d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
1932d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
1933d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
1934a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1935a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1936a94100faSBill Paul 				ifp->if_collisions++;
1937a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1938a94100faSBill Paul 				ifp->if_oerrors++;
1939a94100faSBill Paul 			else
1940a94100faSBill Paul 				ifp->if_opackets++;
1941a94100faSBill Paul 		}
1942a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1943d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1944a94100faSBill Paul 	}
1945d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
1946a94100faSBill Paul 
1947a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1948a94100faSBill Paul 
1949d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
19500fc4974fSBill Paul 		/*
1951b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
1952b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
1953b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
1954b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
1955b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
1956b4b95879SMarius Strobl 		 * be required with the PCIe devices.
19570fc4974fSBill Paul 		 */
19580fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
19590fc4974fSBill Paul 
1960ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1961a94100faSBill Paul 		/*
1962b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
1963b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
1964a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
1965a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
1966a94100faSBill Paul 		 */
1967a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1968ed510fb0SBill Paul #endif
1969b4b95879SMarius Strobl 	} else
1970b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
1971a94100faSBill Paul }
1972a94100faSBill Paul 
1973a94100faSBill Paul static void
1974a94100faSBill Paul re_tick(xsc)
1975a94100faSBill Paul 	void			*xsc;
1976a94100faSBill Paul {
1977a94100faSBill Paul 	struct rl_softc		*sc;
1978d1754a9bSJohn Baldwin 	struct mii_data		*mii;
1979ed510fb0SBill Paul 	struct ifnet		*ifp;
1980a94100faSBill Paul 
1981a94100faSBill Paul 	sc = xsc;
1982ed510fb0SBill Paul 	ifp = sc->rl_ifp;
198397b9d4baSJohn-Mark Gurney 
198497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
198597b9d4baSJohn-Mark Gurney 
19861d545c7aSMarius Strobl 	re_watchdog(sc);
1987a94100faSBill Paul 
19881d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
1989a94100faSBill Paul 	mii_tick(mii);
1990ed510fb0SBill Paul 	if (sc->rl_link) {
1991ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
1992ed510fb0SBill Paul 			sc->rl_link = 0;
1993ed510fb0SBill Paul 	} else {
1994ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1995ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1996ed510fb0SBill Paul 			sc->rl_link = 1;
1997ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1998ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
1999ed510fb0SBill Paul 				    &sc->rl_txtask);
2000ed510fb0SBill Paul 		}
2001ed510fb0SBill Paul 	}
2002a94100faSBill Paul 
2003d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2004a94100faSBill Paul }
2005a94100faSBill Paul 
2006a94100faSBill Paul #ifdef DEVICE_POLLING
2007a94100faSBill Paul static void
2008a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2009a94100faSBill Paul {
2010a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
2011a94100faSBill Paul 
2012a94100faSBill Paul 	RL_LOCK(sc);
201340929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
201497b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
201597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
201697b9d4baSJohn-Mark Gurney }
201797b9d4baSJohn-Mark Gurney 
201897b9d4baSJohn-Mark Gurney static void
201997b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
202097b9d4baSJohn-Mark Gurney {
202197b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
202297b9d4baSJohn-Mark Gurney 
202397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
202497b9d4baSJohn-Mark Gurney 
2025a94100faSBill Paul 	sc->rxcycles = count;
2026a94100faSBill Paul 	re_rxeof(sc);
2027a94100faSBill Paul 	re_txeof(sc);
2028a94100faSBill Paul 
202937652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2030ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2031a94100faSBill Paul 
2032a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2033a94100faSBill Paul 		u_int16_t       status;
2034a94100faSBill Paul 
2035a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2036a94100faSBill Paul 		if (status == 0xffff)
203797b9d4baSJohn-Mark Gurney 			return;
2038a94100faSBill Paul 		if (status)
2039a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2040a94100faSBill Paul 
2041a94100faSBill Paul 		/*
2042a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2043a94100faSBill Paul 		 */
2044a94100faSBill Paul 
2045a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
2046a94100faSBill Paul 			re_reset(sc);
204797b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2048a94100faSBill Paul 		}
2049a94100faSBill Paul 	}
2050a94100faSBill Paul }
2051a94100faSBill Paul #endif /* DEVICE_POLLING */
2052a94100faSBill Paul 
2053ef544f63SPaolo Pisati static int
2054a94100faSBill Paul re_intr(arg)
2055a94100faSBill Paul 	void			*arg;
2056a94100faSBill Paul {
2057a94100faSBill Paul 	struct rl_softc		*sc;
2058ed510fb0SBill Paul 	uint16_t		status;
2059a94100faSBill Paul 
2060a94100faSBill Paul 	sc = arg;
2061ed510fb0SBill Paul 
2062ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2063498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2064ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2065ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2066ed510fb0SBill Paul 
2067ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2068ed510fb0SBill Paul 
2069ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2070ed510fb0SBill Paul }
2071ed510fb0SBill Paul 
2072ed510fb0SBill Paul static void
2073ed510fb0SBill Paul re_int_task(arg, npending)
2074ed510fb0SBill Paul 	void			*arg;
2075ed510fb0SBill Paul 	int			npending;
2076ed510fb0SBill Paul {
2077ed510fb0SBill Paul 	struct rl_softc		*sc;
2078ed510fb0SBill Paul 	struct ifnet		*ifp;
2079ed510fb0SBill Paul 	u_int16_t		status;
2080ed510fb0SBill Paul 	int			rval = 0;
2081ed510fb0SBill Paul 
2082ed510fb0SBill Paul 	sc = arg;
2083ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2084a94100faSBill Paul 
2085a94100faSBill Paul 	RL_LOCK(sc);
208697b9d4baSJohn-Mark Gurney 
2087a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2088a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2089a94100faSBill Paul 
2090d65abd66SPyun YongHyeon 	if (sc->suspended ||
2091d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2092ed510fb0SBill Paul 		RL_UNLOCK(sc);
2093ed510fb0SBill Paul 		return;
2094ed510fb0SBill Paul 	}
2095a94100faSBill Paul 
2096ed510fb0SBill Paul #ifdef DEVICE_POLLING
2097ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2098ed510fb0SBill Paul 		RL_UNLOCK(sc);
2099ed510fb0SBill Paul 		return;
2100ed510fb0SBill Paul 	}
2101ed510fb0SBill Paul #endif
2102a94100faSBill Paul 
2103ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
2104ed510fb0SBill Paul 		rval = re_rxeof(sc);
2105ed510fb0SBill Paul 
2106ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2107ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
2108ed510fb0SBill Paul #else
2109ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
2110ed510fb0SBill Paul #endif
2111ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2112a94100faSBill Paul 		re_txeof(sc);
2113a94100faSBill Paul 
2114a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
2115a94100faSBill Paul 		re_reset(sc);
211697b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2117a94100faSBill Paul 	}
2118a94100faSBill Paul 
2119a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
2120d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
2121d1754a9bSJohn Baldwin 		re_tick(sc);
2122a94100faSBill Paul 	}
2123a94100faSBill Paul 
212452732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2125ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2126a94100faSBill Paul 
2127a94100faSBill Paul 	RL_UNLOCK(sc);
2128ed510fb0SBill Paul 
2129ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2130ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2131ed510fb0SBill Paul 		return;
2132ed510fb0SBill Paul 	}
2133ed510fb0SBill Paul 
2134ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2135ed510fb0SBill Paul 
2136ed510fb0SBill Paul 	return;
2137a94100faSBill Paul }
2138a94100faSBill Paul 
2139d65abd66SPyun YongHyeon static int
2140d65abd66SPyun YongHyeon re_encap(sc, m_head)
2141d65abd66SPyun YongHyeon 	struct rl_softc		*sc;
2142d65abd66SPyun YongHyeon 	struct mbuf		**m_head;
2143d65abd66SPyun YongHyeon {
2144d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2145d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2146d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2147d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2148d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2149d65abd66SPyun YongHyeon 	int			nsegs, prod;
2150d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2151d65abd66SPyun YongHyeon 	int			padlen;
2152ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2153a94100faSBill Paul 
2154d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2155738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
21560fc4974fSBill Paul 
21570fc4974fSBill Paul 	/*
21580fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
21590fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
21600fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
21610fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
21620fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
21630fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
216499c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
21650fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2166d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
21670fc4974fSBill Paul 	 */
2168a4148af5SPyun YongHyeon 	if ((*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
216999c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2170d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2171d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2172d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2173d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2174d65abd66SPyun YongHyeon 			m_freem(*m_head);
2175d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2176d65abd66SPyun YongHyeon 				*m_head = NULL;
2177a94100faSBill Paul 				return (ENOBUFS);
2178a94100faSBill Paul 			}
2179d65abd66SPyun YongHyeon 			*m_head = m_new;
2180d65abd66SPyun YongHyeon 		}
2181d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2182d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
218380a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2184b4b95879SMarius Strobl 			if (m_new == NULL) {
2185b4b95879SMarius Strobl 				m_freem(*m_head);
2186b4b95879SMarius Strobl 				*m_head = NULL;
218780a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2188b4b95879SMarius Strobl 			}
2189d65abd66SPyun YongHyeon 		} else
2190d65abd66SPyun YongHyeon 			m_new = *m_head;
2191a94100faSBill Paul 
21920fc4974fSBill Paul 		/*
21930fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
21940fc4974fSBill Paul 		 * to avoid leaking data.
21950fc4974fSBill Paul 		 */
2196d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2197d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
21980fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2199d65abd66SPyun YongHyeon 		*m_head = m_new;
22000fc4974fSBill Paul 	}
22010fc4974fSBill Paul 
2202d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2203d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2204d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2205d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2206d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2207304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2208d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2209d65abd66SPyun YongHyeon 			m_freem(*m_head);
2210b4b95879SMarius Strobl 			*m_head = NULL;
2211d65abd66SPyun YongHyeon 			return (ENOBUFS);
2212a94100faSBill Paul 		}
2213d65abd66SPyun YongHyeon 		*m_head = m_new;
2214d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2215d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2216d65abd66SPyun YongHyeon 		if (error != 0) {
2217d65abd66SPyun YongHyeon 			m_freem(*m_head);
2218d65abd66SPyun YongHyeon 			*m_head = NULL;
2219d65abd66SPyun YongHyeon 			return (error);
2220a94100faSBill Paul 		}
2221d65abd66SPyun YongHyeon 	} else if (error != 0)
2222d65abd66SPyun YongHyeon 		return (error);
2223d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2224d65abd66SPyun YongHyeon 		m_freem(*m_head);
2225d65abd66SPyun YongHyeon 		*m_head = NULL;
2226d65abd66SPyun YongHyeon 		return (EIO);
2227d65abd66SPyun YongHyeon 	}
2228d65abd66SPyun YongHyeon 
2229d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2230d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2231d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2232d65abd66SPyun YongHyeon 		return (ENOBUFS);
2233d65abd66SPyun YongHyeon 	}
2234d65abd66SPyun YongHyeon 
2235d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2236d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2237a94100faSBill Paul 
2238a94100faSBill Paul 	/*
2239d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2240d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2241d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2242d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2243a94100faSBill Paul 	 */
2244d65abd66SPyun YongHyeon 	csum_flags = 0;
2245d65abd66SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2246d65abd66SPyun YongHyeon 		csum_flags = RL_TDESC_CMD_LGSEND |
2247d65abd66SPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2248d65abd66SPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2249d65abd66SPyun YongHyeon 	else {
225099c8ae87SPyun YongHyeon 		/*
225199c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
225299c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
225399c8ae87SPyun YongHyeon 		 * does't make effects.
225499c8ae87SPyun YongHyeon 		 */
225599c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2256d65abd66SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_IPCSUM;
225799c8ae87SPyun YongHyeon 			if (((*m_head)->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2258d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_TCPCSUM;
225999c8ae87SPyun YongHyeon 			if (((*m_head)->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2260d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_UDPCSUM;
2261d65abd66SPyun YongHyeon 		}
226299c8ae87SPyun YongHyeon 	}
2263a94100faSBill Paul 
2264ccf34c81SPyun YongHyeon 	/*
2265ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2266ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2267ccf34c81SPyun YongHyeon 	 * transmission attempt.
2268ccf34c81SPyun YongHyeon 	 */
2269ccf34c81SPyun YongHyeon 	vlanctl = 0;
2270ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2271ccf34c81SPyun YongHyeon 		vlanctl =
2272ccf34c81SPyun YongHyeon 		    htole32(htons((*m_head)->m_pkthdr.ether_vtag) |
2273ccf34c81SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG);
2274ccf34c81SPyun YongHyeon 
2275d65abd66SPyun YongHyeon 	si = prod;
2276d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2277d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2278ccf34c81SPyun YongHyeon 		desc->rl_vlanctl = vlanctl;
2279d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2280d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2281d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2282d65abd66SPyun YongHyeon 		if (i != 0)
2283d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2284d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2285d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2286d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2287d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2288d65abd66SPyun YongHyeon 	}
2289d65abd66SPyun YongHyeon 	/* Update producer index. */
2290d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2291a94100faSBill Paul 
2292d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2293d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2294d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2295d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2296d65abd66SPyun YongHyeon 
2297d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2298d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2299d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2300a94100faSBill Paul 
2301d65abd66SPyun YongHyeon 	/*
2302d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2303d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2304d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2305d65abd66SPyun YongHyeon 	 */
2306d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2307d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2308d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2309d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2310d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2311a94100faSBill Paul 
2312a94100faSBill Paul 	return (0);
2313a94100faSBill Paul }
2314a94100faSBill Paul 
231597b9d4baSJohn-Mark Gurney static void
2316ed510fb0SBill Paul re_tx_task(arg, npending)
2317ed510fb0SBill Paul 	void			*arg;
2318ed510fb0SBill Paul 	int			npending;
231997b9d4baSJohn-Mark Gurney {
2320ed510fb0SBill Paul 	struct ifnet		*ifp;
232197b9d4baSJohn-Mark Gurney 
2322ed510fb0SBill Paul 	ifp = arg;
2323ed510fb0SBill Paul 	re_start(ifp);
2324ed510fb0SBill Paul 
2325ed510fb0SBill Paul 	return;
232697b9d4baSJohn-Mark Gurney }
232797b9d4baSJohn-Mark Gurney 
2328a94100faSBill Paul /*
2329a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2330a94100faSBill Paul  */
2331a94100faSBill Paul static void
2332ed510fb0SBill Paul re_start(ifp)
2333a94100faSBill Paul 	struct ifnet		*ifp;
2334a94100faSBill Paul {
2335a94100faSBill Paul 	struct rl_softc		*sc;
2336d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2337d65abd66SPyun YongHyeon 	int			queued;
2338a94100faSBill Paul 
2339a94100faSBill Paul 	sc = ifp->if_softc;
234097b9d4baSJohn-Mark Gurney 
2341ed510fb0SBill Paul 	RL_LOCK(sc);
2342ed510fb0SBill Paul 
2343d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2344d65abd66SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->rl_link == 0) {
2345ed510fb0SBill Paul 		RL_UNLOCK(sc);
2346ed510fb0SBill Paul 		return;
2347ed510fb0SBill Paul 	}
2348a94100faSBill Paul 
2349d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2350d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
235152732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2352a94100faSBill Paul 		if (m_head == NULL)
2353a94100faSBill Paul 			break;
2354a94100faSBill Paul 
2355d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2356b4b95879SMarius Strobl 			if (m_head == NULL)
2357b4b95879SMarius Strobl 				break;
235852732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
235913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2360a94100faSBill Paul 			break;
2361a94100faSBill Paul 		}
2362a94100faSBill Paul 
2363a94100faSBill Paul 		/*
2364a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2365a94100faSBill Paul 		 * to him.
2366a94100faSBill Paul 		 */
236759a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
236852732175SMax Laier 
236952732175SMax Laier 		queued++;
2370a94100faSBill Paul 	}
2371a94100faSBill Paul 
2372ed510fb0SBill Paul 	if (queued == 0) {
2373ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2374d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2375ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2376ed510fb0SBill Paul #endif
2377ed510fb0SBill Paul 		RL_UNLOCK(sc);
237852732175SMax Laier 		return;
2379ed510fb0SBill Paul 	}
238052732175SMax Laier 
2381a94100faSBill Paul 	/* Flush the TX descriptors */
2382a94100faSBill Paul 
2383a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2384a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2385a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2386a94100faSBill Paul 
23870fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2388a94100faSBill Paul 
2389ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2390a94100faSBill Paul 	/*
2391a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2392a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2393a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2394a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2395a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2396a94100faSBill Paul 	 * the timer count is reset to 0.
2397a94100faSBill Paul 	 */
2398a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2399ed510fb0SBill Paul #endif
2400a94100faSBill Paul 
2401a94100faSBill Paul 	/*
2402a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2403a94100faSBill Paul 	 */
24041d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2405ed510fb0SBill Paul 
2406ed510fb0SBill Paul 	RL_UNLOCK(sc);
2407ed510fb0SBill Paul 
2408ed510fb0SBill Paul 	return;
2409a94100faSBill Paul }
2410a94100faSBill Paul 
2411a94100faSBill Paul static void
2412a94100faSBill Paul re_init(xsc)
2413a94100faSBill Paul 	void			*xsc;
2414a94100faSBill Paul {
2415a94100faSBill Paul 	struct rl_softc		*sc = xsc;
241697b9d4baSJohn-Mark Gurney 
241797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
241897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
241997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
242097b9d4baSJohn-Mark Gurney }
242197b9d4baSJohn-Mark Gurney 
242297b9d4baSJohn-Mark Gurney static void
242397b9d4baSJohn-Mark Gurney re_init_locked(sc)
242497b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
242597b9d4baSJohn-Mark Gurney {
2426fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2427a94100faSBill Paul 	struct mii_data		*mii;
2428a94100faSBill Paul 	u_int32_t		rxcfg = 0;
242970acaecfSPyun YongHyeon 	uint16_t		cfg;
24304d3d7085SBernd Walter 	union {
24314d3d7085SBernd Walter 		uint32_t align_dummy;
24324d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
24334d3d7085SBernd Walter         } eaddr;
2434a94100faSBill Paul 
243597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
243697b9d4baSJohn-Mark Gurney 
2437a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2438a94100faSBill Paul 
2439a94100faSBill Paul 	/*
2440a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2441a94100faSBill Paul 	 */
2442a94100faSBill Paul 	re_stop(sc);
2443a94100faSBill Paul 
2444a94100faSBill Paul 	/*
2445c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2446edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2447c2c6548bSBill Paul 	 * before all others.
2448c2c6548bSBill Paul 	 */
244970acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
245070acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
245170acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
245270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
245370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
245470acaecfSPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD,
245570acaecfSPyun YongHyeon 	    cfg | RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB);
2456c2c6548bSBill Paul 
2457c2c6548bSBill Paul 	/*
2458a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2459a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2460a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2461a94100faSBill Paul 	 */
24624d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
24634d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2464a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2465ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2466ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2467ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2468ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2469a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2470a94100faSBill Paul 
2471a94100faSBill Paul 	/*
2472a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2473a94100faSBill Paul 	 */
2474a94100faSBill Paul 	re_rx_list_init(sc);
2475a94100faSBill Paul 	re_tx_list_init(sc);
2476a94100faSBill Paul 
2477a94100faSBill Paul 	/*
2478d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2479d01fac16SPyun YongHyeon 	 */
2480d01fac16SPyun YongHyeon 
2481d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2482d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2483d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2484d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2485d01fac16SPyun YongHyeon 
2486d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2487d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2488d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2489d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2490d01fac16SPyun YongHyeon 
2491d01fac16SPyun YongHyeon 	/*
2492a94100faSBill Paul 	 * Enable transmit and receive.
2493a94100faSBill Paul 	 */
2494a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2495a94100faSBill Paul 
2496a94100faSBill Paul 	/*
2497a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2498a94100faSBill Paul 	 */
2499abc8ff44SBill Paul 	if (sc->rl_testmode) {
2500abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2501abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2502abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2503a94100faSBill Paul 		else
2504abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2505abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2506abc8ff44SBill Paul 	} else
2507a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2508d01fac16SPyun YongHyeon 
2509d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2510d01fac16SPyun YongHyeon 
2511a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2512a94100faSBill Paul 
2513a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2514a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2515a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2516a94100faSBill Paul 
2517a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
251861021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2519a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
252061021536SJohn-Mark Gurney 	else
2521a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2522a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2523a94100faSBill Paul 
2524a94100faSBill Paul 	/*
2525a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2526a94100faSBill Paul 	 */
252761021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2528a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
252961021536SJohn-Mark Gurney 	else
2530a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2531a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2532a94100faSBill Paul 
2533a94100faSBill Paul 	/*
2534a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2535a94100faSBill Paul 	 */
2536a94100faSBill Paul 	re_setmulti(sc);
2537a94100faSBill Paul 
2538a94100faSBill Paul #ifdef DEVICE_POLLING
2539a94100faSBill Paul 	/*
2540a94100faSBill Paul 	 * Disable interrupts if we are polling.
2541a94100faSBill Paul 	 */
254240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2543a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2544a94100faSBill Paul 	else	/* otherwise ... */
254540929967SGleb Smirnoff #endif
2546ed510fb0SBill Paul 
2547a94100faSBill Paul 	/*
2548a94100faSBill Paul 	 * Enable interrupts.
2549a94100faSBill Paul 	 */
2550a94100faSBill Paul 	if (sc->rl_testmode)
2551a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2552a94100faSBill Paul 	else
2553a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2554ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2555a94100faSBill Paul 
2556a94100faSBill Paul 	/* Set initial TX threshold */
2557a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2558a94100faSBill Paul 
2559a94100faSBill Paul 	/* Start RX/TX process. */
2560a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2561a94100faSBill Paul #ifdef notdef
2562a94100faSBill Paul 	/* Enable receiver and transmitter. */
2563a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2564a94100faSBill Paul #endif
2565a94100faSBill Paul 
2566ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2567a94100faSBill Paul 	/*
2568a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2569a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2570a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2571a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2572a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2573a94100faSBill Paul 	 */
2574a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2575a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2576a94100faSBill Paul 	else
2577a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2578ed510fb0SBill Paul #endif
2579a94100faSBill Paul 
2580a94100faSBill Paul 	/*
2581a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2582a94100faSBill Paul 	 * size so we can receive jumbo frames.
2583a94100faSBill Paul 	 */
2584a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2585a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2586a94100faSBill Paul 
258797b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2588a94100faSBill Paul 		return;
2589a94100faSBill Paul 
2590a94100faSBill Paul 	mii_mediachg(mii);
2591a94100faSBill Paul 
259219ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2593a94100faSBill Paul 
259413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
259513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2596a94100faSBill Paul 
2597ed510fb0SBill Paul 	sc->rl_link = 0;
25981d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2599d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2600a94100faSBill Paul }
2601a94100faSBill Paul 
2602a94100faSBill Paul /*
2603a94100faSBill Paul  * Set media options.
2604a94100faSBill Paul  */
2605a94100faSBill Paul static int
2606a94100faSBill Paul re_ifmedia_upd(ifp)
2607a94100faSBill Paul 	struct ifnet		*ifp;
2608a94100faSBill Paul {
2609a94100faSBill Paul 	struct rl_softc		*sc;
2610a94100faSBill Paul 	struct mii_data		*mii;
2611a94100faSBill Paul 
2612a94100faSBill Paul 	sc = ifp->if_softc;
2613a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2614d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2615a94100faSBill Paul 	mii_mediachg(mii);
2616d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2617a94100faSBill Paul 
2618a94100faSBill Paul 	return (0);
2619a94100faSBill Paul }
2620a94100faSBill Paul 
2621a94100faSBill Paul /*
2622a94100faSBill Paul  * Report current media status.
2623a94100faSBill Paul  */
2624a94100faSBill Paul static void
2625a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2626a94100faSBill Paul 	struct ifnet		*ifp;
2627a94100faSBill Paul 	struct ifmediareq	*ifmr;
2628a94100faSBill Paul {
2629a94100faSBill Paul 	struct rl_softc		*sc;
2630a94100faSBill Paul 	struct mii_data		*mii;
2631a94100faSBill Paul 
2632a94100faSBill Paul 	sc = ifp->if_softc;
2633a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2634a94100faSBill Paul 
2635d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2636a94100faSBill Paul 	mii_pollstat(mii);
2637d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2638a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2639a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2640a94100faSBill Paul }
2641a94100faSBill Paul 
2642a94100faSBill Paul static int
2643a94100faSBill Paul re_ioctl(ifp, command, data)
2644a94100faSBill Paul 	struct ifnet		*ifp;
2645a94100faSBill Paul 	u_long			command;
2646a94100faSBill Paul 	caddr_t			data;
2647a94100faSBill Paul {
2648a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2649a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2650a94100faSBill Paul 	struct mii_data		*mii;
265140929967SGleb Smirnoff 	int			error = 0;
2652a94100faSBill Paul 
2653a94100faSBill Paul 	switch (command) {
2654a94100faSBill Paul 	case SIOCSIFMTU:
2655c1d0b573SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > RL_JUMBO_MTU) {
2656a94100faSBill Paul 			error = EINVAL;
2657c1d0b573SPyun YongHyeon 			break;
2658c1d0b573SPyun YongHyeon 		}
2659c1d0b573SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS &&
2660c1d0b573SPyun YongHyeon 		    ifr->ifr_mtu > RL_MAX_FRAMELEN) {
2661c1d0b573SPyun YongHyeon 			error = EINVAL;
2662c1d0b573SPyun YongHyeon 			break;
2663c1d0b573SPyun YongHyeon 		}
2664c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
2665c1d0b573SPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu)
2666a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2667d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2668a94100faSBill Paul 		break;
2669a94100faSBill Paul 	case SIOCSIFFLAGS:
267097b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2671eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2672eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2673eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
2674eed497bbSPyun YongHyeon 				    & IFF_PROMISC) != 0)
2675eed497bbSPyun YongHyeon 					re_setmulti(sc);
2676eed497bbSPyun YongHyeon 			} else
267797b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2678eed497bbSPyun YongHyeon 		} else {
2679eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2680a94100faSBill Paul 				re_stop(sc);
2681eed497bbSPyun YongHyeon 		}
2682eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
268397b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2684a94100faSBill Paul 		break;
2685a94100faSBill Paul 	case SIOCADDMULTI:
2686a94100faSBill Paul 	case SIOCDELMULTI:
268797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2688a94100faSBill Paul 		re_setmulti(sc);
268997b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2690a94100faSBill Paul 		break;
2691a94100faSBill Paul 	case SIOCGIFMEDIA:
2692a94100faSBill Paul 	case SIOCSIFMEDIA:
2693a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2694a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2695a94100faSBill Paul 		break;
2696a94100faSBill Paul 	case SIOCSIFCAP:
269740929967SGleb Smirnoff 	    {
2698f051cb85SGleb Smirnoff 		int mask, reinit;
2699f051cb85SGleb Smirnoff 
2700f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2701f051cb85SGleb Smirnoff 		reinit = 0;
270240929967SGleb Smirnoff #ifdef DEVICE_POLLING
270340929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
270440929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
270540929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
270640929967SGleb Smirnoff 				if (error)
270740929967SGleb Smirnoff 					return(error);
2708d1754a9bSJohn Baldwin 				RL_LOCK(sc);
270940929967SGleb Smirnoff 				/* Disable interrupts */
271040929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
271140929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
271240929967SGleb Smirnoff 				RL_UNLOCK(sc);
271340929967SGleb Smirnoff 			} else {
271440929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
271540929967SGleb Smirnoff 				/* Enable interrupts. */
271640929967SGleb Smirnoff 				RL_LOCK(sc);
271740929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
271840929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
271940929967SGleb Smirnoff 				RL_UNLOCK(sc);
272040929967SGleb Smirnoff 			}
272140929967SGleb Smirnoff 		}
272240929967SGleb Smirnoff #endif /* DEVICE_POLLING */
272340929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2724f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2725a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2726dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2727a94100faSBill Paul 			else
2728b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2729f051cb85SGleb Smirnoff 			reinit = 1;
273040929967SGleb Smirnoff 		}
2731f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2732f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2733f051cb85SGleb Smirnoff 			reinit = 1;
2734f051cb85SGleb Smirnoff 		}
2735dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2736dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2737dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2738dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2739dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2740dc74159dSPyun YongHyeon 			else
2741dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2742dc74159dSPyun YongHyeon 		}
27437467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
27447467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
27457467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
27467467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
27477467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
27487467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
27497467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
27507467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
27517467bd53SPyun YongHyeon 		}
2752f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2753f051cb85SGleb Smirnoff 			re_init(sc);
2754960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
275540929967SGleb Smirnoff 	    }
2756a94100faSBill Paul 		break;
2757a94100faSBill Paul 	default:
2758a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2759a94100faSBill Paul 		break;
2760a94100faSBill Paul 	}
2761a94100faSBill Paul 
2762a94100faSBill Paul 	return (error);
2763a94100faSBill Paul }
2764a94100faSBill Paul 
2765a94100faSBill Paul static void
27661d545c7aSMarius Strobl re_watchdog(sc)
2767a94100faSBill Paul 	struct rl_softc		*sc;
27681d545c7aSMarius Strobl {
2769a94100faSBill Paul 
27701d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
27711d545c7aSMarius Strobl 
27721d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
27731d545c7aSMarius Strobl 		return;
27741d545c7aSMarius Strobl 
27751d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
27761d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2777a94100faSBill Paul 
2778a94100faSBill Paul 	re_txeof(sc);
2779a94100faSBill Paul 	re_rxeof(sc);
278097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2781a94100faSBill Paul }
2782a94100faSBill Paul 
2783a94100faSBill Paul /*
2784a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2785a94100faSBill Paul  * RX and TX lists.
2786a94100faSBill Paul  */
2787a94100faSBill Paul static void
2788a94100faSBill Paul re_stop(sc)
2789a94100faSBill Paul 	struct rl_softc		*sc;
2790a94100faSBill Paul {
2791a94100faSBill Paul 	register int		i;
2792a94100faSBill Paul 	struct ifnet		*ifp;
2793d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2794d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
2795a94100faSBill Paul 
279697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
279797b9d4baSJohn-Mark Gurney 
2798fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2799a94100faSBill Paul 
28001d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2801d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
280213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2803a94100faSBill Paul 
2804a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2805a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2806ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2807a94100faSBill Paul 
2808a94100faSBill Paul 	if (sc->rl_head != NULL) {
2809a94100faSBill Paul 		m_freem(sc->rl_head);
2810a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2811a94100faSBill Paul 	}
2812a94100faSBill Paul 
2813a94100faSBill Paul 	/* Free the TX list buffers. */
2814a94100faSBill Paul 
2815d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
2816d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
2817d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
2818d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2819d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2820d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2821d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2822d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2823d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2824a94100faSBill Paul 		}
2825a94100faSBill Paul 	}
2826a94100faSBill Paul 
2827a94100faSBill Paul 	/* Free the RX list buffers. */
2828a94100faSBill Paul 
2829d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2830d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
2831d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2832d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2833d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2834d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
2835d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
2836d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
2837d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
2838a94100faSBill Paul 		}
2839a94100faSBill Paul 	}
2840a94100faSBill Paul }
2841a94100faSBill Paul 
2842a94100faSBill Paul /*
2843a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2844a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2845a94100faSBill Paul  * resume.
2846a94100faSBill Paul  */
2847a94100faSBill Paul static int
2848a94100faSBill Paul re_suspend(dev)
2849a94100faSBill Paul 	device_t		dev;
2850a94100faSBill Paul {
2851a94100faSBill Paul 	struct rl_softc		*sc;
2852a94100faSBill Paul 
2853a94100faSBill Paul 	sc = device_get_softc(dev);
2854a94100faSBill Paul 
285597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2856a94100faSBill Paul 	re_stop(sc);
28577467bd53SPyun YongHyeon 	re_setwol(sc);
2858a94100faSBill Paul 	sc->suspended = 1;
285997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2860a94100faSBill Paul 
2861a94100faSBill Paul 	return (0);
2862a94100faSBill Paul }
2863a94100faSBill Paul 
2864a94100faSBill Paul /*
2865a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2866a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2867a94100faSBill Paul  * appropriate.
2868a94100faSBill Paul  */
2869a94100faSBill Paul static int
2870a94100faSBill Paul re_resume(dev)
2871a94100faSBill Paul 	device_t		dev;
2872a94100faSBill Paul {
2873a94100faSBill Paul 	struct rl_softc		*sc;
2874a94100faSBill Paul 	struct ifnet		*ifp;
2875a94100faSBill Paul 
2876a94100faSBill Paul 	sc = device_get_softc(dev);
287797b9d4baSJohn-Mark Gurney 
287897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
287997b9d4baSJohn-Mark Gurney 
2880fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2881a94100faSBill Paul 
2882a94100faSBill Paul 	/* reinitialize interface if necessary */
2883a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
288497b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2885a94100faSBill Paul 
28867467bd53SPyun YongHyeon 	/*
28877467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
28887467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
28897467bd53SPyun YongHyeon 	 */
28907467bd53SPyun YongHyeon 	re_clrwol(sc);
2891a94100faSBill Paul 	sc->suspended = 0;
289297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2893a94100faSBill Paul 
2894a94100faSBill Paul 	return (0);
2895a94100faSBill Paul }
2896a94100faSBill Paul 
2897a94100faSBill Paul /*
2898a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2899a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2900a94100faSBill Paul  */
29016a087a87SPyun YongHyeon static int
2902a94100faSBill Paul re_shutdown(dev)
2903a94100faSBill Paul 	device_t		dev;
2904a94100faSBill Paul {
2905a94100faSBill Paul 	struct rl_softc		*sc;
2906a94100faSBill Paul 
2907a94100faSBill Paul 	sc = device_get_softc(dev);
2908a94100faSBill Paul 
290997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2910a94100faSBill Paul 	re_stop(sc);
2911536fde34SMaxim Sobolev 	/*
2912536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2913536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
291472293673SRuslan Ermilov 	 * cases.
2915536fde34SMaxim Sobolev 	 */
2916536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
29177467bd53SPyun YongHyeon 	re_setwol(sc);
291897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
29196a087a87SPyun YongHyeon 
29206a087a87SPyun YongHyeon 	return (0);
2921a94100faSBill Paul }
29227467bd53SPyun YongHyeon 
29237467bd53SPyun YongHyeon static void
29247467bd53SPyun YongHyeon re_setwol(sc)
29257467bd53SPyun YongHyeon 	struct rl_softc		*sc;
29267467bd53SPyun YongHyeon {
29277467bd53SPyun YongHyeon 	struct ifnet		*ifp;
29287467bd53SPyun YongHyeon 	int			pmc;
29297467bd53SPyun YongHyeon 	uint16_t		pmstat;
29307467bd53SPyun YongHyeon 	uint8_t			v;
29317467bd53SPyun YongHyeon 
29327467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
29337467bd53SPyun YongHyeon 
29347467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
29357467bd53SPyun YongHyeon 		return;
29367467bd53SPyun YongHyeon 
29377467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
29387467bd53SPyun YongHyeon 	/* Enable config register write. */
29397467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
29407467bd53SPyun YongHyeon 
29417467bd53SPyun YongHyeon 	/* Enable PME. */
29427467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
29437467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
29447467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29457467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
29467467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
29477467bd53SPyun YongHyeon 
29487467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
29497467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
29507467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
29517467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
29527467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
29537467bd53SPyun YongHyeon 
29547467bd53SPyun YongHyeon 	/* Config register write done. */
29557467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, 0);
29567467bd53SPyun YongHyeon 
29577467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
29587467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
29597467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
29607467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
29617467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
29627467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
29637467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
29647467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29657467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
29667467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
29677467bd53SPyun YongHyeon 
29687467bd53SPyun YongHyeon 	/*
29697467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
29707467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
29717467bd53SPyun YongHyeon 	 * needed.
29727467bd53SPyun YongHyeon 	 */
29737467bd53SPyun YongHyeon 
29747467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
29757467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
29767467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
29777467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
29787467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
29797467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
29807467bd53SPyun YongHyeon }
29817467bd53SPyun YongHyeon 
29827467bd53SPyun YongHyeon static void
29837467bd53SPyun YongHyeon re_clrwol(sc)
29847467bd53SPyun YongHyeon 	struct rl_softc		*sc;
29857467bd53SPyun YongHyeon {
29867467bd53SPyun YongHyeon 	int			pmc;
29877467bd53SPyun YongHyeon 	uint8_t			v;
29887467bd53SPyun YongHyeon 
29897467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
29907467bd53SPyun YongHyeon 
29917467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
29927467bd53SPyun YongHyeon 		return;
29937467bd53SPyun YongHyeon 
29947467bd53SPyun YongHyeon 	/* Enable config register write. */
29957467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
29967467bd53SPyun YongHyeon 
29977467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
29987467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
29997467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
30007467bd53SPyun YongHyeon 
30017467bd53SPyun YongHyeon 	/* Config register write done. */
30027467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, 0);
30037467bd53SPyun YongHyeon 
30047467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
30057467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
30067467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
30077467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
30087467bd53SPyun YongHyeon }
3009