xref: /freebsd/sys/dev/re/if_re.c (revision 141f92e7b56cad24f92c33cfc318e551609a4a0b)
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 
149a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
150a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
151a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
152a94100faSBill Paul 
153298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
154a94100faSBill Paul #include "miibus_if.h"
155a94100faSBill Paul 
156a94100faSBill Paul /*
157a94100faSBill Paul  * Default to using PIO access for this driver.
158a94100faSBill Paul  */
159a94100faSBill Paul #define RE_USEIOSPACE
160a94100faSBill Paul 
161a94100faSBill Paul #include <pci/if_rlreg.h>
162a94100faSBill Paul 
163a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
164a94100faSBill Paul 
165a94100faSBill Paul /*
166a94100faSBill Paul  * Various supported device vendors/types and their names.
167a94100faSBill Paul  */
168a94100faSBill Paul static struct rl_type re_devs[] = {
16932aa5f0eSAnton Berezin 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, RL_HWREV_8169S,
17032aa5f0eSAnton Berezin 		"D-Link DGE-528(T) Gigabit Ethernet Adapter" },
171a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
172a94100faSBill Paul 		"RealTek 8139C+ 10/100BaseTX" },
173ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8101E, RL_HWREV_8101E,
174ed510fb0SBill Paul 		"RealTek 8101E PCIe 10/100baseTX" },
175498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN1,
176498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
177498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168_SPIN2,
178498bd0d3SBill Paul 		"RealTek 8168/8111B PCIe Gigabit Ethernet" },
179a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
180a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
18169a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
18269a6b7fbSBill Paul 		"RealTek 8169S Single-chip Gigabit Ethernet" },
183ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB,
184ed510fb0SBill Paul 		"RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" },
1852ee2c3b4SRemko Lodder 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC,
1862ee2c3b4SRemko Lodder 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
187498bd0d3SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169SC, RL_HWREV_8169_8110SC,
188ed510fb0SBill Paul 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
18969a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
19069a6b7fbSBill Paul 		"RealTek 8110S Single-chip Gigabit Ethernet" },
191ea263191SMIHIRA Sanpei Yoshiro 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
192ea263191SMIHIRA Sanpei Yoshiro 		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
19326390635SJohn Baldwin 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S,
19426390635SJohn Baldwin 		"Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1950fc4974fSBill Paul 	{ USR_VENDORID, USR_DEVICEID_997902, RL_HWREV_8169S,
1960fc4974fSBill Paul 		"US Robotics 997902 (RTL8169S) Gigabit Ethernet" },
197a94100faSBill Paul 	{ 0, 0, 0, NULL }
198a94100faSBill Paul };
199a94100faSBill Paul 
200a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
201a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
202a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
203a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
204a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
205a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
206a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
207a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
208a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
209498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN1, RL_8169, "8168"},
210a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
21169a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
21269a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
213ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
214ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
215a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
216a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
217ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
218ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
219498bd0d3SBill Paul 	{ RL_HWREV_8168_SPIN2, RL_8169, "8168"},
220a94100faSBill Paul 	{ 0, 0, NULL }
221a94100faSBill Paul };
222a94100faSBill Paul 
223a94100faSBill Paul static int re_probe		(device_t);
224a94100faSBill Paul static int re_attach		(device_t);
225a94100faSBill Paul static int re_detach		(device_t);
226a94100faSBill Paul 
22780a2a305SJohn-Mark Gurney static int re_encap		(struct rl_softc *, struct mbuf **, int *);
228a94100faSBill Paul 
229a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
230a94100faSBill Paul static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
231a94100faSBill Paul 				    bus_size_t, int);
232a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
233a94100faSBill Paul static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
234a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
235a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
23622a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23722a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23822a11c96SJohn-Mark Gurney 				(struct mbuf *);
23922a11c96SJohn-Mark Gurney #endif
240ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
241a94100faSBill Paul static void re_txeof		(struct rl_softc *);
24297b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2430187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2440187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
24597b9d4baSJohn-Mark Gurney #endif
246ef544f63SPaolo Pisati static int re_intr		(void *);
247a94100faSBill Paul static void re_tick		(void *);
248ed510fb0SBill Paul static void re_tx_task		(void *, int);
249ed510fb0SBill Paul static void re_int_task		(void *, int);
250a94100faSBill Paul static void re_start		(struct ifnet *);
251a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
252a94100faSBill Paul static void re_init		(void *);
25397b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
254a94100faSBill Paul static void re_stop		(struct rl_softc *);
2551d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
256a94100faSBill Paul static int re_suspend		(device_t);
257a94100faSBill Paul static int re_resume		(device_t);
258a94100faSBill Paul static void re_shutdown		(device_t);
259a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
260a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
261a94100faSBill Paul 
262a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
263a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
264ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
265a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
266a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
267a94100faSBill Paul 
268a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
269a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
270a94100faSBill Paul static void re_miibus_statchg	(device_t);
271a94100faSBill Paul 
272a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
273a94100faSBill Paul static void re_reset		(struct rl_softc *);
274a94100faSBill Paul 
275ed510fb0SBill Paul #ifdef RE_DIAG
276a94100faSBill Paul static int re_diag		(struct rl_softc *);
277ed510fb0SBill Paul #endif
278a94100faSBill Paul 
279a94100faSBill Paul #ifdef RE_USEIOSPACE
280a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
281a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
282a94100faSBill Paul #else
283a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
284a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
285a94100faSBill Paul #endif
286a94100faSBill Paul 
287a94100faSBill Paul static device_method_t re_methods[] = {
288a94100faSBill Paul 	/* Device interface */
289a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
290a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
291a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
292a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
293a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
294a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
295a94100faSBill Paul 
296a94100faSBill Paul 	/* bus interface */
297a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
298a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
299a94100faSBill Paul 
300a94100faSBill Paul 	/* MII interface */
301a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
302a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
303a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
304a94100faSBill Paul 
305a94100faSBill Paul 	{ 0, 0 }
306a94100faSBill Paul };
307a94100faSBill Paul 
308a94100faSBill Paul static driver_t re_driver = {
309a94100faSBill Paul 	"re",
310a94100faSBill Paul 	re_methods,
311a94100faSBill Paul 	sizeof(struct rl_softc)
312a94100faSBill Paul };
313a94100faSBill Paul 
314a94100faSBill Paul static devclass_t re_devclass;
315a94100faSBill Paul 
316a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
317347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
318a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
319a94100faSBill Paul 
320a94100faSBill Paul #define EE_SET(x)					\
321a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
322a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
323a94100faSBill Paul 
324a94100faSBill Paul #define EE_CLR(x)					\
325a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
326a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
327a94100faSBill Paul 
328a94100faSBill Paul /*
329a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
330a94100faSBill Paul  */
331a94100faSBill Paul static void
332a94100faSBill Paul re_eeprom_putbyte(sc, addr)
333a94100faSBill Paul 	struct rl_softc		*sc;
334a94100faSBill Paul 	int			addr;
335a94100faSBill Paul {
336a94100faSBill Paul 	register int		d, i;
337a94100faSBill Paul 
338ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
339a94100faSBill Paul 
340a94100faSBill Paul 	/*
341a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
342a94100faSBill Paul 	 */
343ed510fb0SBill Paul 
344ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
345a94100faSBill Paul 		if (d & i) {
346a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
347a94100faSBill Paul 		} else {
348a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
349a94100faSBill Paul 		}
350a94100faSBill Paul 		DELAY(100);
351a94100faSBill Paul 		EE_SET(RL_EE_CLK);
352a94100faSBill Paul 		DELAY(150);
353a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
354a94100faSBill Paul 		DELAY(100);
355a94100faSBill Paul 	}
356ed510fb0SBill Paul 
357ed510fb0SBill Paul 	return;
358a94100faSBill Paul }
359a94100faSBill Paul 
360a94100faSBill Paul /*
361a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
362a94100faSBill Paul  */
363a94100faSBill Paul static void
364a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
365a94100faSBill Paul 	struct rl_softc		*sc;
366a94100faSBill Paul 	int			addr;
367a94100faSBill Paul 	u_int16_t		*dest;
368a94100faSBill Paul {
369a94100faSBill Paul 	register int		i;
370a94100faSBill Paul 	u_int16_t		word = 0;
371a94100faSBill Paul 
372a94100faSBill Paul 	/*
373a94100faSBill Paul 	 * Send address of word we want to read.
374a94100faSBill Paul 	 */
375a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
376a94100faSBill Paul 
377a94100faSBill Paul 	/*
378a94100faSBill Paul 	 * Start reading bits from EEPROM.
379a94100faSBill Paul 	 */
380a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
381a94100faSBill Paul 		EE_SET(RL_EE_CLK);
382a94100faSBill Paul 		DELAY(100);
383a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
384a94100faSBill Paul 			word |= i;
385a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
386a94100faSBill Paul 		DELAY(100);
387a94100faSBill Paul 	}
388a94100faSBill Paul 
389a94100faSBill Paul 	*dest = word;
390ed510fb0SBill Paul 
391ed510fb0SBill Paul 	return;
392a94100faSBill Paul }
393a94100faSBill Paul 
394a94100faSBill Paul /*
395a94100faSBill Paul  * Read a sequence of words from the EEPROM.
396a94100faSBill Paul  */
397a94100faSBill Paul static void
398ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
399a94100faSBill Paul 	struct rl_softc		*sc;
400a94100faSBill Paul 	caddr_t			dest;
401a94100faSBill Paul 	int			off;
402a94100faSBill Paul 	int			cnt;
403a94100faSBill Paul {
404a94100faSBill Paul 	int			i;
405a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
406a94100faSBill Paul 
407ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
408ed510fb0SBill Paul 
409ed510fb0SBill Paul         DELAY(100);
410ed510fb0SBill Paul 
411a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
412ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
413a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
414ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
415a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
416be099007SPyun YongHyeon                 *ptr = word;
417a94100faSBill Paul 	}
418ed510fb0SBill Paul 
419ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
420ed510fb0SBill Paul 
421ed510fb0SBill Paul 	return;
422a94100faSBill Paul }
423a94100faSBill Paul 
424a94100faSBill Paul static int
425a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
426a94100faSBill Paul 	device_t		dev;
427a94100faSBill Paul 	int			phy, reg;
428a94100faSBill Paul {
429a94100faSBill Paul 	struct rl_softc		*sc;
430a94100faSBill Paul 	u_int32_t		rval;
431a94100faSBill Paul 	int			i;
432a94100faSBill Paul 
433a94100faSBill Paul 	if (phy != 1)
434a94100faSBill Paul 		return (0);
435a94100faSBill Paul 
436a94100faSBill Paul 	sc = device_get_softc(dev);
437a94100faSBill Paul 
4389bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4399bac70b8SBill Paul 
4409bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4419bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4429bac70b8SBill Paul 		return (rval);
4439bac70b8SBill Paul 	}
4449bac70b8SBill Paul 
445a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
446a94100faSBill Paul 	DELAY(1000);
447a94100faSBill Paul 
448a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
449a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
450a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
451a94100faSBill Paul 			break;
452a94100faSBill Paul 		DELAY(100);
453a94100faSBill Paul 	}
454a94100faSBill Paul 
455a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4566b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
457a94100faSBill Paul 		return (0);
458a94100faSBill Paul 	}
459a94100faSBill Paul 
460a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
461a94100faSBill Paul }
462a94100faSBill Paul 
463a94100faSBill Paul static int
464a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
465a94100faSBill Paul 	device_t		dev;
466a94100faSBill Paul 	int			phy, reg, data;
467a94100faSBill Paul {
468a94100faSBill Paul 	struct rl_softc		*sc;
469a94100faSBill Paul 	u_int32_t		rval;
470a94100faSBill Paul 	int			i;
471a94100faSBill Paul 
472a94100faSBill Paul 	sc = device_get_softc(dev);
473a94100faSBill Paul 
474a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4759bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
476a94100faSBill Paul 	DELAY(1000);
477a94100faSBill Paul 
478a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
479a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
480a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
481a94100faSBill Paul 			break;
482a94100faSBill Paul 		DELAY(100);
483a94100faSBill Paul 	}
484a94100faSBill Paul 
485a94100faSBill Paul 	if (i == RL_TIMEOUT) {
4866b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
487a94100faSBill Paul 		return (0);
488a94100faSBill Paul 	}
489a94100faSBill Paul 
490a94100faSBill Paul 	return (0);
491a94100faSBill Paul }
492a94100faSBill Paul 
493a94100faSBill Paul static int
494a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
495a94100faSBill Paul 	device_t		dev;
496a94100faSBill Paul 	int			phy, reg;
497a94100faSBill Paul {
498a94100faSBill Paul 	struct rl_softc		*sc;
499a94100faSBill Paul 	u_int16_t		rval = 0;
500a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
501a94100faSBill Paul 
502a94100faSBill Paul 	sc = device_get_softc(dev);
503a94100faSBill Paul 
504a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
505a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
506a94100faSBill Paul 		return (rval);
507a94100faSBill Paul 	}
508a94100faSBill Paul 
509a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
510a94100faSBill Paul 	if (phy) {
511a94100faSBill Paul 		return (0);
512a94100faSBill Paul 	}
513a94100faSBill Paul 	switch (reg) {
514a94100faSBill Paul 	case MII_BMCR:
515a94100faSBill Paul 		re8139_reg = RL_BMCR;
516a94100faSBill Paul 		break;
517a94100faSBill Paul 	case MII_BMSR:
518a94100faSBill Paul 		re8139_reg = RL_BMSR;
519a94100faSBill Paul 		break;
520a94100faSBill Paul 	case MII_ANAR:
521a94100faSBill Paul 		re8139_reg = RL_ANAR;
522a94100faSBill Paul 		break;
523a94100faSBill Paul 	case MII_ANER:
524a94100faSBill Paul 		re8139_reg = RL_ANER;
525a94100faSBill Paul 		break;
526a94100faSBill Paul 	case MII_ANLPAR:
527a94100faSBill Paul 		re8139_reg = RL_LPAR;
528a94100faSBill Paul 		break;
529a94100faSBill Paul 	case MII_PHYIDR1:
530a94100faSBill Paul 	case MII_PHYIDR2:
531a94100faSBill Paul 		return (0);
532a94100faSBill Paul 	/*
533a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
534a94100faSBill Paul 	 * register. If we have a link partner which does not
535a94100faSBill Paul 	 * support NWAY, this is the register which will tell
536a94100faSBill Paul 	 * us the results of parallel detection.
537a94100faSBill Paul 	 */
538a94100faSBill Paul 	case RL_MEDIASTAT:
539a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
540a94100faSBill Paul 		return (rval);
541a94100faSBill Paul 	default:
5426b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
543a94100faSBill Paul 		return (0);
544a94100faSBill Paul 	}
545a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
546baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
547baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
548baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
549baa12772SPyun YongHyeon 	}
550a94100faSBill Paul 	return (rval);
551a94100faSBill Paul }
552a94100faSBill Paul 
553a94100faSBill Paul static int
554a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
555a94100faSBill Paul 	device_t		dev;
556a94100faSBill Paul 	int			phy, reg, data;
557a94100faSBill Paul {
558a94100faSBill Paul 	struct rl_softc		*sc;
559a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
560a94100faSBill Paul 	int			rval = 0;
561a94100faSBill Paul 
562a94100faSBill Paul 	sc = device_get_softc(dev);
563a94100faSBill Paul 
564a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
565a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
566a94100faSBill Paul 		return (rval);
567a94100faSBill Paul 	}
568a94100faSBill Paul 
569a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
57097b9d4baSJohn-Mark Gurney 	if (phy)
571a94100faSBill Paul 		return (0);
57297b9d4baSJohn-Mark Gurney 
573a94100faSBill Paul 	switch (reg) {
574a94100faSBill Paul 	case MII_BMCR:
575a94100faSBill Paul 		re8139_reg = RL_BMCR;
576baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
577baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
578baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
579baa12772SPyun YongHyeon 		}
580a94100faSBill Paul 		break;
581a94100faSBill Paul 	case MII_BMSR:
582a94100faSBill Paul 		re8139_reg = RL_BMSR;
583a94100faSBill Paul 		break;
584a94100faSBill Paul 	case MII_ANAR:
585a94100faSBill Paul 		re8139_reg = RL_ANAR;
586a94100faSBill Paul 		break;
587a94100faSBill Paul 	case MII_ANER:
588a94100faSBill Paul 		re8139_reg = RL_ANER;
589a94100faSBill Paul 		break;
590a94100faSBill Paul 	case MII_ANLPAR:
591a94100faSBill Paul 		re8139_reg = RL_LPAR;
592a94100faSBill Paul 		break;
593a94100faSBill Paul 	case MII_PHYIDR1:
594a94100faSBill Paul 	case MII_PHYIDR2:
595a94100faSBill Paul 		return (0);
596a94100faSBill Paul 		break;
597a94100faSBill Paul 	default:
5986b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
599a94100faSBill Paul 		return (0);
600a94100faSBill Paul 	}
601a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
602a94100faSBill Paul 	return (0);
603a94100faSBill Paul }
604a94100faSBill Paul 
605a94100faSBill Paul static void
606a94100faSBill Paul re_miibus_statchg(dev)
607a94100faSBill Paul 	device_t		dev;
608a94100faSBill Paul {
609a11e2f18SBruce M Simpson 
610a94100faSBill Paul }
611a94100faSBill Paul 
612a94100faSBill Paul /*
613a94100faSBill Paul  * Program the 64-bit multicast hash filter.
614a94100faSBill Paul  */
615a94100faSBill Paul static void
616a94100faSBill Paul re_setmulti(sc)
617a94100faSBill Paul 	struct rl_softc		*sc;
618a94100faSBill Paul {
619a94100faSBill Paul 	struct ifnet		*ifp;
620a94100faSBill Paul 	int			h = 0;
621a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
622a94100faSBill Paul 	struct ifmultiaddr	*ifma;
623a94100faSBill Paul 	u_int32_t		rxfilt;
624a94100faSBill Paul 	int			mcnt = 0;
625bb7dfefbSBill Paul 	u_int32_t		hwrev;
626a94100faSBill Paul 
62797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
62897b9d4baSJohn-Mark Gurney 
629fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
630a94100faSBill Paul 
631a94100faSBill Paul 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
632a94100faSBill Paul 
633a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
634a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
635a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
636a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
637a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
638a94100faSBill Paul 		return;
639a94100faSBill Paul 	}
640a94100faSBill Paul 
641a94100faSBill Paul 	/* first, zot all the existing hash bits */
642a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
643a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
644a94100faSBill Paul 
645a94100faSBill Paul 	/* now program new ones */
64613b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
647a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
648a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
649a94100faSBill Paul 			continue;
6500e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6510e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
652a94100faSBill Paul 		if (h < 32)
653a94100faSBill Paul 			hashes[0] |= (1 << h);
654a94100faSBill Paul 		else
655a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
656a94100faSBill Paul 		mcnt++;
657a94100faSBill Paul 	}
65813b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
659a94100faSBill Paul 
660a94100faSBill Paul 	if (mcnt)
661a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
662a94100faSBill Paul 	else
663a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
664a94100faSBill Paul 
665a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
666bb7dfefbSBill Paul 
667bb7dfefbSBill Paul 	/*
668bb7dfefbSBill Paul 	 * For some unfathomable reason, RealTek decided to reverse
669bb7dfefbSBill Paul 	 * the order of the multicast hash registers in the PCI Express
670bb7dfefbSBill Paul 	 * parts. This means we have to write the hash pattern in reverse
671bb7dfefbSBill Paul 	 * order for those devices.
672bb7dfefbSBill Paul 	 */
673bb7dfefbSBill Paul 
674bb7dfefbSBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
675bb7dfefbSBill Paul 
676bb7dfefbSBill Paul 	if (hwrev == RL_HWREV_8100E || hwrev == RL_HWREV_8101E ||
677bb7dfefbSBill Paul 	    hwrev == RL_HWREV_8168_SPIN1 || hwrev == RL_HWREV_8168_SPIN2) {
678bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, bswap32(hashes[1]));
679bb7dfefbSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, bswap32(hashes[0]));
680bb7dfefbSBill Paul 	} else {
681a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
682a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
683a94100faSBill Paul 	}
684bb7dfefbSBill Paul }
685a94100faSBill Paul 
686a94100faSBill Paul static void
687a94100faSBill Paul re_reset(sc)
688a94100faSBill Paul 	struct rl_softc		*sc;
689a94100faSBill Paul {
690a94100faSBill Paul 	register int		i;
691a94100faSBill Paul 
69297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
69397b9d4baSJohn-Mark Gurney 
694a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
695a94100faSBill Paul 
696a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
697a94100faSBill Paul 		DELAY(10);
698a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
699a94100faSBill Paul 			break;
700a94100faSBill Paul 	}
701a94100faSBill Paul 	if (i == RL_TIMEOUT)
7026b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
703a94100faSBill Paul 
704a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
705a94100faSBill Paul }
706a94100faSBill Paul 
707ed510fb0SBill Paul #ifdef RE_DIAG
708ed510fb0SBill Paul 
709a94100faSBill Paul /*
710a94100faSBill Paul  * The following routine is designed to test for a defect on some
711a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
712a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
713a94100faSBill Paul  * should be pulled high. The result of this defect is that the
714a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
715a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
716a94100faSBill Paul  * because the 64-bit data lines aren't connected.
717a94100faSBill Paul  *
718a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
719a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
720a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
721a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
722a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
723a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
724a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
725a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
726a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
727a94100faSBill Paul  */
728a94100faSBill Paul 
729a94100faSBill Paul static int
730a94100faSBill Paul re_diag(sc)
731a94100faSBill Paul 	struct rl_softc		*sc;
732a94100faSBill Paul {
733fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
734a94100faSBill Paul 	struct mbuf		*m0;
735a94100faSBill Paul 	struct ether_header	*eh;
736a94100faSBill Paul 	struct rl_desc		*cur_rx;
737a94100faSBill Paul 	u_int16_t		status;
738a94100faSBill Paul 	u_int32_t		rxstat;
739ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
740a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
741a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
742a94100faSBill Paul 
743a94100faSBill Paul 	/* Allocate a single mbuf */
744a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
745a94100faSBill Paul 	if (m0 == NULL)
746a94100faSBill Paul 		return (ENOBUFS);
747a94100faSBill Paul 
74897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
74997b9d4baSJohn-Mark Gurney 
750a94100faSBill Paul 	/*
751a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
752a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
753a94100faSBill Paul 	 * following special functions:
754a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
755a94100faSBill Paul 	 * - Enables digital loopback mode
756a94100faSBill Paul 	 * - Leaves interrupts turned off
757a94100faSBill Paul 	 */
758a94100faSBill Paul 
759a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
760a94100faSBill Paul 	sc->rl_testmode = 1;
761ed510fb0SBill Paul 	re_reset(sc);
76297b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
763ed510fb0SBill Paul 	sc->rl_link = 1;
764ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
765ed510fb0SBill Paul 		phyaddr = 1;
766ed510fb0SBill Paul 	else
767ed510fb0SBill Paul 		phyaddr = 0;
768ed510fb0SBill Paul 
769ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
770ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
771ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
772ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
773ed510fb0SBill Paul 			break;
774ed510fb0SBill Paul 	}
775ed510fb0SBill Paul 
776ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
777ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
778ed510fb0SBill Paul 
779804af9a1SBill Paul 	DELAY(100000);
780a94100faSBill Paul 
781a94100faSBill Paul 	/* Put some data in the mbuf */
782a94100faSBill Paul 
783a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
784a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
785a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
786a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
787a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
788a94100faSBill Paul 
7897cae6651SBill Paul 	/*
7907cae6651SBill Paul 	 * Queue the packet, start transmission.
7917cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7927cae6651SBill Paul 	 */
793a94100faSBill Paul 
794abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
79597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79652732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7977cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
79897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
799a94100faSBill Paul 	m0 = NULL;
800a94100faSBill Paul 
801a94100faSBill Paul 	/* Wait for it to propagate through the chip */
802a94100faSBill Paul 
803abc8ff44SBill Paul 	DELAY(100000);
804a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
805a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
806ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
807abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
808abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
809a94100faSBill Paul 			break;
810a94100faSBill Paul 		DELAY(10);
811a94100faSBill Paul 	}
812a94100faSBill Paul 
813a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8146b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8156b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8166b9f5c94SGleb Smirnoff 		    " loopback mode\n");
817a94100faSBill Paul 		error = EIO;
818a94100faSBill Paul 		goto done;
819a94100faSBill Paul 	}
820a94100faSBill Paul 
821a94100faSBill Paul 	/*
822a94100faSBill Paul 	 * The packet should have been dumped into the first
823a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
824a94100faSBill Paul 	 */
825a94100faSBill Paul 
826a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
827a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
828a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
829a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
830a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0],
831a94100faSBill Paul 	    BUS_DMASYNC_POSTWRITE);
832a94100faSBill Paul 	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
833a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0]);
834a94100faSBill Paul 
835a94100faSBill Paul 	m0 = sc->rl_ldata.rl_rx_mbuf[0];
836a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
837a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
838a94100faSBill Paul 
839a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
840a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
841a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
842a94100faSBill Paul 
843a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8446b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8456b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
846a94100faSBill Paul 		error = EIO;
847a94100faSBill Paul 		goto done;
848a94100faSBill Paul 	}
849a94100faSBill Paul 
850a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
851a94100faSBill Paul 
852a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
853a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
854a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8556b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8566b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
857a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8586b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
859a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
860a94100faSBill Paul 		    ntohs(eh->ether_type));
8616b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8626b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8636b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8646b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8656b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8666b9f5c94SGleb Smirnoff 		    "details.\n");
867a94100faSBill Paul 		error = EIO;
868a94100faSBill Paul 	}
869a94100faSBill Paul 
870a94100faSBill Paul done:
871a94100faSBill Paul 	/* Turn interface off, release resources */
872a94100faSBill Paul 
873a94100faSBill Paul 	sc->rl_testmode = 0;
874ed510fb0SBill Paul 	sc->rl_link = 0;
875a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
876a94100faSBill Paul 	re_stop(sc);
877a94100faSBill Paul 	if (m0 != NULL)
878a94100faSBill Paul 		m_freem(m0);
879a94100faSBill Paul 
88097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
88197b9d4baSJohn-Mark Gurney 
882a94100faSBill Paul 	return (error);
883a94100faSBill Paul }
884a94100faSBill Paul 
885ed510fb0SBill Paul #endif
886ed510fb0SBill Paul 
887a94100faSBill Paul /*
888a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
889a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
890a94100faSBill Paul  */
891a94100faSBill Paul static int
892a94100faSBill Paul re_probe(dev)
893a94100faSBill Paul 	device_t		dev;
894a94100faSBill Paul {
895a94100faSBill Paul 	struct rl_type		*t;
896a94100faSBill Paul 	struct rl_softc		*sc;
897a94100faSBill Paul 	int			rid;
898a94100faSBill Paul 	u_int32_t		hwrev;
899a94100faSBill Paul 
900a94100faSBill Paul 	t = re_devs;
901a94100faSBill Paul 	sc = device_get_softc(dev);
902a94100faSBill Paul 
903a94100faSBill Paul 	while (t->rl_name != NULL) {
904a94100faSBill Paul 		if ((pci_get_vendor(dev) == t->rl_vid) &&
905a94100faSBill Paul 		    (pci_get_device(dev) == t->rl_did)) {
90626390635SJohn Baldwin 			/*
90726390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
90826390635SJohn Baldwin 			 * Rev. 2 i supported by sk(4).
90926390635SJohn Baldwin 			 */
91026390635SJohn Baldwin 			if ((t->rl_vid == LINKSYS_VENDORID) &&
91126390635SJohn Baldwin 				(t->rl_did == LINKSYS_DEVICEID_EG1032) &&
91226390635SJohn Baldwin 				(pci_get_subdevice(dev) !=
91326390635SJohn Baldwin 				LINKSYS_SUBDEVICE_EG1032_REV3)) {
91426390635SJohn Baldwin 				t++;
91526390635SJohn Baldwin 				continue;
91626390635SJohn Baldwin 			}
917a94100faSBill Paul 
918a94100faSBill Paul 			/*
919a94100faSBill Paul 			 * Temporarily map the I/O space
920a94100faSBill Paul 			 * so we can read the chip ID register.
921a94100faSBill Paul 			 */
922a94100faSBill Paul 			rid = RL_RID;
9235f96beb9SNate Lawson 			sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
9245f96beb9SNate Lawson 			    RF_ACTIVE);
925a94100faSBill Paul 			if (sc->rl_res == NULL) {
926a94100faSBill Paul 				device_printf(dev,
927a94100faSBill Paul 				    "couldn't map ports/memory\n");
928a94100faSBill Paul 				return (ENXIO);
929a94100faSBill Paul 			}
930a94100faSBill Paul 			sc->rl_btag = rman_get_bustag(sc->rl_res);
931a94100faSBill Paul 			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
932a94100faSBill Paul 			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
933a94100faSBill Paul 			bus_release_resource(dev, RL_RES,
934a94100faSBill Paul 			    RL_RID, sc->rl_res);
935a94100faSBill Paul 			if (t->rl_basetype == hwrev) {
936a94100faSBill Paul 				device_set_desc(dev, t->rl_name);
937d2b677bbSWarner Losh 				return (BUS_PROBE_DEFAULT);
938a94100faSBill Paul 			}
939a94100faSBill Paul 		}
940a94100faSBill Paul 		t++;
941a94100faSBill Paul 	}
942a94100faSBill Paul 
943a94100faSBill Paul 	return (ENXIO);
944a94100faSBill Paul }
945a94100faSBill Paul 
946a94100faSBill Paul /*
947a94100faSBill Paul  * This routine takes the segment list provided as the result of
948a94100faSBill Paul  * a bus_dma_map_load() operation and assigns the addresses/lengths
949a94100faSBill Paul  * to RealTek DMA descriptors. This can be called either by the RX
950a94100faSBill Paul  * code or the TX code. In the RX case, we'll probably wind up mapping
951a94100faSBill Paul  * at most one segment. For the TX case, there could be any number of
952a94100faSBill Paul  * segments since TX packets may span multiple mbufs. In either case,
953a94100faSBill Paul  * if the number of segments is larger than the rl_maxsegs limit
954a94100faSBill Paul  * specified by the caller, we abort the mapping operation. Sadly,
955a94100faSBill Paul  * whoever designed the buffer mapping API did not provide a way to
956a94100faSBill Paul  * return an error from here, so we have to fake it a bit.
957a94100faSBill Paul  */
958a94100faSBill Paul 
959a94100faSBill Paul static void
960a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error)
961a94100faSBill Paul 	void			*arg;
962a94100faSBill Paul 	bus_dma_segment_t	*segs;
963a94100faSBill Paul 	int			nseg;
964a94100faSBill Paul 	bus_size_t		mapsize;
965a94100faSBill Paul 	int			error;
966a94100faSBill Paul {
967a94100faSBill Paul 	struct rl_dmaload_arg	*ctx;
968a94100faSBill Paul 	struct rl_desc		*d = NULL;
969a94100faSBill Paul 	int			i = 0, idx;
970498bd0d3SBill Paul 	u_int32_t		cmdstat;
971498bd0d3SBill Paul 	int			totlen = 0;
972a94100faSBill Paul 
973a94100faSBill Paul 	if (error)
974a94100faSBill Paul 		return;
975a94100faSBill Paul 
976a94100faSBill Paul 	ctx = arg;
977a94100faSBill Paul 
978a94100faSBill Paul 	/* Signal error to caller if there's too many segments */
979a94100faSBill Paul 	if (nseg > ctx->rl_maxsegs) {
980a94100faSBill Paul 		ctx->rl_maxsegs = 0;
981a94100faSBill Paul 		return;
982a94100faSBill Paul 	}
983a94100faSBill Paul 
984a94100faSBill Paul 	/*
985a94100faSBill Paul 	 * Map the segment array into descriptors. Note that we set the
986a94100faSBill Paul 	 * start-of-frame and end-of-frame markers for either TX or RX, but
987a94100faSBill Paul 	 * they really only have meaning in the TX case. (In the RX case,
988a94100faSBill Paul 	 * it's the chip that tells us where packets begin and end.)
989a94100faSBill Paul 	 * We also keep track of the end of the ring and set the
990a94100faSBill Paul 	 * end-of-ring bits as needed, and we set the ownership bits
991a94100faSBill Paul 	 * in all except the very first descriptor. (The caller will
992a94100faSBill Paul 	 * set this descriptor later when it start transmission or
993a94100faSBill Paul 	 * reception.)
994a94100faSBill Paul 	 */
995a94100faSBill Paul 	idx = ctx->rl_idx;
99659b5d934SBruce M Simpson 	for (;;) {
997a94100faSBill Paul 		d = &ctx->rl_ring[idx];
998a94100faSBill Paul 		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
999a94100faSBill Paul 			ctx->rl_maxsegs = 0;
1000a94100faSBill Paul 			return;
1001a94100faSBill Paul 		}
1002a94100faSBill Paul 		cmdstat = segs[i].ds_len;
1003498bd0d3SBill Paul 		totlen += segs[i].ds_len;
1004a94100faSBill Paul 		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
1005a94100faSBill Paul 		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
1006a94100faSBill Paul 		if (i == 0)
1007a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_SOF;
1008a94100faSBill Paul 		else
1009a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_OWN;
1010a94100faSBill Paul 		if (idx == (RL_RX_DESC_CNT - 1))
1011a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_EOR;
1012a94100faSBill Paul 		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
1013a94100faSBill Paul 		i++;
1014a94100faSBill Paul 		if (i == nseg)
1015a94100faSBill Paul 			break;
1016a94100faSBill Paul 		RL_DESC_INC(idx);
1017a94100faSBill Paul 	}
1018a94100faSBill Paul 
1019a94100faSBill Paul 	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
1020a94100faSBill Paul 	ctx->rl_maxsegs = nseg;
1021a94100faSBill Paul 	ctx->rl_idx = idx;
1022a94100faSBill Paul }
1023a94100faSBill Paul 
1024a94100faSBill Paul /*
1025a94100faSBill Paul  * Map a single buffer address.
1026a94100faSBill Paul  */
1027a94100faSBill Paul 
1028a94100faSBill Paul static void
1029a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
1030a94100faSBill Paul 	void			*arg;
1031a94100faSBill Paul 	bus_dma_segment_t	*segs;
1032a94100faSBill Paul 	int			nseg;
1033a94100faSBill Paul 	int			error;
1034a94100faSBill Paul {
10358fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
1036a94100faSBill Paul 
1037a94100faSBill Paul 	if (error)
1038a94100faSBill Paul 		return;
1039a94100faSBill Paul 
1040a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1041a94100faSBill Paul 	addr = arg;
1042a94100faSBill Paul 	*addr = segs->ds_addr;
1043a94100faSBill Paul }
1044a94100faSBill Paul 
1045a94100faSBill Paul static int
1046a94100faSBill Paul re_allocmem(dev, sc)
1047a94100faSBill Paul 	device_t		dev;
1048a94100faSBill Paul 	struct rl_softc		*sc;
1049a94100faSBill Paul {
1050a94100faSBill Paul 	int			error;
1051a94100faSBill Paul 	int			nseg;
1052a94100faSBill Paul 	int			i;
1053a94100faSBill Paul 
1054a94100faSBill Paul 	/*
1055a94100faSBill Paul 	 * Allocate map for RX mbufs.
1056a94100faSBill Paul 	 */
1057a94100faSBill Paul 	nseg = 32;
1058a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
1059a94100faSBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
10606110675fSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
1061a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_mtag);
1062a94100faSBill Paul 	if (error) {
1063a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1064a94100faSBill Paul 		return (ENOMEM);
1065a94100faSBill Paul 	}
1066a94100faSBill Paul 
1067a94100faSBill Paul 	/*
1068a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1069a94100faSBill Paul 	 */
1070a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1071a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
10721d545c7aSMarius Strobl 	    NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, 0,
1073a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1074a94100faSBill Paul 	if (error) {
1075a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1076a94100faSBill Paul 		return (ENOMEM);
1077a94100faSBill Paul 	}
1078a94100faSBill Paul 
1079a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1080a94100faSBill Paul 
1081a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1082a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1083a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1084a94100faSBill Paul 	if (error)
1085a94100faSBill Paul 		return (ENOMEM);
1086a94100faSBill Paul 
1087a94100faSBill Paul 	/* Load the map for the TX ring. */
1088a94100faSBill Paul 
1089a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1090a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1091a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
1092a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1093a94100faSBill Paul 
1094a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1095a94100faSBill Paul 
1096a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
1097a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1098a94100faSBill Paul 			    &sc->rl_ldata.rl_tx_dmamap[i]);
1099a94100faSBill Paul 		if (error) {
1100a94100faSBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
1101a94100faSBill Paul 			return (ENOMEM);
1102a94100faSBill Paul 		}
1103a94100faSBill Paul 	}
1104a94100faSBill Paul 
1105a94100faSBill Paul 	/*
1106a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1107a94100faSBill Paul 	 */
1108a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1109a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
11101d545c7aSMarius Strobl 	    NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, 0,
1111a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1112a94100faSBill Paul 	if (error) {
1113a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1114a94100faSBill Paul 		return (ENOMEM);
1115a94100faSBill Paul 	}
1116a94100faSBill Paul 
1117a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1118a94100faSBill Paul 
1119a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1120a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1121a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1122a94100faSBill Paul 	if (error)
1123a94100faSBill Paul 		return (ENOMEM);
1124a94100faSBill Paul 
1125a94100faSBill Paul 	/* Load the map for the RX ring. */
1126a94100faSBill Paul 
1127a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1128a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
112961021536SJohn-Mark Gurney 	     RL_RX_LIST_SZ, re_dma_map_addr,
1130a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1131a94100faSBill Paul 
1132a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1133a94100faSBill Paul 
1134a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1135a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1136a94100faSBill Paul 			    &sc->rl_ldata.rl_rx_dmamap[i]);
1137a94100faSBill Paul 		if (error) {
1138a94100faSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1139a94100faSBill Paul 			return (ENOMEM);
1140a94100faSBill Paul 		}
1141a94100faSBill Paul 	}
1142a94100faSBill Paul 
1143a94100faSBill Paul 	return (0);
1144a94100faSBill Paul }
1145a94100faSBill Paul 
1146a94100faSBill Paul /*
1147a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1148a94100faSBill Paul  * setup and ethernet/BPF attach.
1149a94100faSBill Paul  */
1150a94100faSBill Paul static int
1151a94100faSBill Paul re_attach(dev)
1152a94100faSBill Paul 	device_t		dev;
1153a94100faSBill Paul {
1154a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1155be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1156a94100faSBill Paul 	struct rl_softc		*sc;
1157a94100faSBill Paul 	struct ifnet		*ifp;
1158a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1159a94100faSBill Paul 	int			hwrev;
1160a94100faSBill Paul 	u_int16_t		re_did = 0;
1161d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
1162a94100faSBill Paul 
1163a94100faSBill Paul 	sc = device_get_softc(dev);
1164ed510fb0SBill Paul 	sc->rl_dev = dev;
1165a94100faSBill Paul 
1166a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
116797b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1168d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1169d1754a9bSJohn Baldwin 
1170a94100faSBill Paul 	/*
1171a94100faSBill Paul 	 * Map control/status registers.
1172a94100faSBill Paul 	 */
1173a94100faSBill Paul 	pci_enable_busmaster(dev);
1174a94100faSBill Paul 
1175a94100faSBill Paul 	rid = RL_RID;
11765f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
11775f96beb9SNate Lawson 	    RF_ACTIVE);
1178a94100faSBill Paul 
1179a94100faSBill Paul 	if (sc->rl_res == NULL) {
1180d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1181a94100faSBill Paul 		error = ENXIO;
1182a94100faSBill Paul 		goto fail;
1183a94100faSBill Paul 	}
1184a94100faSBill Paul 
1185a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1186a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1187a94100faSBill Paul 
1188a94100faSBill Paul 	/* Allocate interrupt */
1189a94100faSBill Paul 	rid = 0;
11905f96beb9SNate Lawson 	sc->rl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1191a94100faSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
1192a94100faSBill Paul 
1193a94100faSBill Paul 	if (sc->rl_irq == NULL) {
1194d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
1195a94100faSBill Paul 		error = ENXIO;
1196a94100faSBill Paul 		goto fail;
1197a94100faSBill Paul 	}
1198a94100faSBill Paul 
1199a94100faSBill Paul 	/* Reset the adapter. */
120097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1201a94100faSBill Paul 	re_reset(sc);
120297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1203abc8ff44SBill Paul 
1204abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1205abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1206abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1207abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1208abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1209abc8ff44SBill Paul 			break;
1210abc8ff44SBill Paul 		}
1211abc8ff44SBill Paul 		hw_rev++;
1212abc8ff44SBill Paul 	}
1213abc8ff44SBill Paul 
1214141f92e7SPyun YongHyeon 	sc->rl_eewidth = RL_9356_ADDR_LEN;
1215ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1216a94100faSBill Paul 	if (re_did != 0x8129)
1217141f92e7SPyun YongHyeon 	        sc->rl_eewidth = RL_9346_ADDR_LEN;
1218a94100faSBill Paul 
1219a94100faSBill Paul 	/*
1220a94100faSBill Paul 	 * Get station address from the EEPROM.
1221a94100faSBill Paul 	 */
1222ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1223be099007SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1224be099007SPyun YongHyeon 		as[i] = le16toh(as[i]);
1225be099007SPyun YongHyeon 	bcopy(as, eaddr, sizeof(eaddr));
1226ed510fb0SBill Paul 
1227ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1228ed510fb0SBill Paul 		/* Set RX length mask */
1229ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1230ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1231ed510fb0SBill Paul 	} else {
1232ed510fb0SBill Paul 		/* Set RX length mask */
1233ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1234ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1235abc8ff44SBill Paul 	}
12369bac70b8SBill Paul 
1237a94100faSBill Paul 	/*
1238a94100faSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1239a94100faSBill Paul 	 */
1240a94100faSBill Paul #define RL_NSEG_NEW 32
12411d545c7aSMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
12421d545c7aSMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
12431d545c7aSMarius Strobl 	    MAXBSIZE, RL_NSEG_NEW, BUS_SPACE_MAXSIZE_32BIT, 0,
12441d545c7aSMarius Strobl 	    NULL, NULL, &sc->rl_parent_tag);
1245a94100faSBill Paul 	if (error)
1246a94100faSBill Paul 		goto fail;
1247a94100faSBill Paul 
1248a94100faSBill Paul 	error = re_allocmem(dev, sc);
1249a94100faSBill Paul 
1250a94100faSBill Paul 	if (error)
1251a94100faSBill Paul 		goto fail;
1252a94100faSBill Paul 
1253cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1254cd036ec1SBrooks Davis 	if (ifp == NULL) {
1255d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1256cd036ec1SBrooks Davis 		error = ENOSPC;
1257cd036ec1SBrooks Davis 		goto fail;
1258cd036ec1SBrooks Davis 	}
1259cd036ec1SBrooks Davis 
1260a94100faSBill Paul 	/* Do MII setup */
1261a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1262a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1263d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1264a94100faSBill Paul 		error = ENXIO;
1265a94100faSBill Paul 		goto fail;
1266a94100faSBill Paul 	}
1267a94100faSBill Paul 
1268a94100faSBill Paul 	ifp->if_softc = sc;
12699bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1270a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1271a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1272a94100faSBill Paul 	ifp->if_start = re_start;
1273f28a171cSPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES;
1274f28a171cSPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
1275498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1276a94100faSBill Paul 	ifp->if_init = re_init;
127752732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
127852732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
127952732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1280a94100faSBill Paul 
1281ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1282ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1283ed510fb0SBill Paul 
1284a94100faSBill Paul 	/*
1285a94100faSBill Paul 	 * Call MI attach routine.
1286a94100faSBill Paul 	 */
1287a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1288a94100faSBill Paul 
1289960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1290960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1291960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1292960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
1293960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1294960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1295960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1296960fd5b3SPyun YongHyeon #endif
1297960fd5b3SPyun YongHyeon 	/*
1298960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1299960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1300960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1301960fd5b3SPyun YongHyeon 	 */
1302960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1303960fd5b3SPyun YongHyeon 
1304ed510fb0SBill Paul #ifdef RE_DIAG
1305ed510fb0SBill Paul 	/*
1306ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1307ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1308ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1309ed510fb0SBill Paul 	 */
1310a94100faSBill Paul 
1311ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1312ed510fb0SBill Paul 		error = re_diag(sc);
1313a94100faSBill Paul 		if (error) {
1314ed510fb0SBill Paul 			device_printf(dev,
1315ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1316a94100faSBill Paul 			ether_ifdetach(ifp);
1317a94100faSBill Paul 			goto fail;
1318a94100faSBill Paul 		}
1319ed510fb0SBill Paul 	}
1320ed510fb0SBill Paul #endif
1321a94100faSBill Paul 
1322a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1323ef544f63SPaolo Pisati 	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET | INTR_MPSAFE,
1324ef544f63SPaolo Pisati 	    re_intr, NULL, sc, &sc->rl_intrhand);
1325a94100faSBill Paul 	if (error) {
1326d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1327a94100faSBill Paul 		ether_ifdetach(ifp);
1328a94100faSBill Paul 	}
1329a94100faSBill Paul 
1330a94100faSBill Paul fail:
1331ed510fb0SBill Paul 
1332a94100faSBill Paul 	if (error)
1333a94100faSBill Paul 		re_detach(dev);
1334a94100faSBill Paul 
1335a94100faSBill Paul 	return (error);
1336a94100faSBill Paul }
1337a94100faSBill Paul 
1338a94100faSBill Paul /*
1339a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1340a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1341a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1342a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1343a94100faSBill Paul  * allocated.
1344a94100faSBill Paul  */
1345a94100faSBill Paul static int
1346a94100faSBill Paul re_detach(dev)
1347a94100faSBill Paul 	device_t		dev;
1348a94100faSBill Paul {
1349a94100faSBill Paul 	struct rl_softc		*sc;
1350a94100faSBill Paul 	struct ifnet		*ifp;
1351a94100faSBill Paul 	int			i;
1352a94100faSBill Paul 
1353a94100faSBill Paul 	sc = device_get_softc(dev);
1354fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1355aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
135697b9d4baSJohn-Mark Gurney 
135740929967SGleb Smirnoff #ifdef DEVICE_POLLING
135840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
135940929967SGleb Smirnoff 		ether_poll_deregister(ifp);
136040929967SGleb Smirnoff #endif
136197b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
1362525e6a87SRuslan Ermilov 	if (device_is_attached(dev)) {
136397b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
136497b9d4baSJohn-Mark Gurney #if 0
136597b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
136697b9d4baSJohn-Mark Gurney #endif
1367a94100faSBill Paul 		re_stop(sc);
1368525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1369d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
13703d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
13713d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1372a94100faSBill Paul 		/*
1373a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1374a94100faSBill Paul 		 * still had a BPF descriptor attached to this
137597b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1376a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1377a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1378a94100faSBill Paul 		 * which will try to call re_init() again. This will
1379a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1380a94100faSBill Paul 		 * which will panic the system when the kernel tries
1381a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1382a94100faSBill Paul 		 * anymore.
1383a94100faSBill Paul 		 */
1384a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1385525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1386a94100faSBill Paul 	}
1387a94100faSBill Paul 	if (sc->rl_miibus)
1388a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1389a94100faSBill Paul 	bus_generic_detach(dev);
1390a94100faSBill Paul 
139197b9d4baSJohn-Mark Gurney 	/*
139297b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
139397b9d4baSJohn-Mark Gurney 	 * stopped here.
139497b9d4baSJohn-Mark Gurney 	 */
139597b9d4baSJohn-Mark Gurney 
1396a94100faSBill Paul 	if (sc->rl_intrhand)
1397a94100faSBill Paul 		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1398ad4f426eSWarner Losh 	if (ifp != NULL)
1399ad4f426eSWarner Losh 		if_free(ifp);
1400a94100faSBill Paul 	if (sc->rl_irq)
1401a94100faSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1402a94100faSBill Paul 	if (sc->rl_res)
1403a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1404a94100faSBill Paul 
1405a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1406a94100faSBill Paul 
1407a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1408a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1409a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1410a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1411a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1412a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1413a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1414a94100faSBill Paul 	}
1415a94100faSBill Paul 
1416a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1417a94100faSBill Paul 
1418a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1419a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1420a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1421a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1422a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1423a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1424a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1425a94100faSBill Paul 	}
1426a94100faSBill Paul 
1427a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1428a94100faSBill Paul 
1429a94100faSBill Paul 	if (sc->rl_ldata.rl_mtag) {
1430a94100faSBill Paul 		for (i = 0; i < RL_TX_DESC_CNT; i++)
1431a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1432a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
1433a94100faSBill Paul 		for (i = 0; i < RL_RX_DESC_CNT; i++)
1434a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1435a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
1436a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1437a94100faSBill Paul 	}
1438a94100faSBill Paul 
1439a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1440a94100faSBill Paul 
1441a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1442a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1443a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1444a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1445a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1446a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1447a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1448a94100faSBill Paul 	}
1449a94100faSBill Paul 
1450a94100faSBill Paul 	if (sc->rl_parent_tag)
1451a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1452a94100faSBill Paul 
1453a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1454a94100faSBill Paul 
1455a94100faSBill Paul 	return (0);
1456a94100faSBill Paul }
1457a94100faSBill Paul 
1458a94100faSBill Paul static int
1459a94100faSBill Paul re_newbuf(sc, idx, m)
1460a94100faSBill Paul 	struct rl_softc		*sc;
1461a94100faSBill Paul 	int			idx;
1462a94100faSBill Paul 	struct mbuf		*m;
1463a94100faSBill Paul {
1464a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1465a94100faSBill Paul 	struct mbuf		*n = NULL;
1466a94100faSBill Paul 	int			error;
1467a94100faSBill Paul 
1468a94100faSBill Paul 	if (m == NULL) {
1469a94100faSBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1470a94100faSBill Paul 		if (n == NULL)
1471a94100faSBill Paul 			return (ENOBUFS);
1472a94100faSBill Paul 		m = n;
1473a94100faSBill Paul 	} else
1474a94100faSBill Paul 		m->m_data = m->m_ext.ext_buf;
1475a94100faSBill Paul 
1476a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
147722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
147822a11c96SJohn-Mark Gurney 	/*
147922a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
148022a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
148122a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
148222a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
148322a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
148422a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
148522a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
148622a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
148722a11c96SJohn-Mark Gurney 	 */
148822a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
148922a11c96SJohn-Mark Gurney #endif
1490a94100faSBill Paul 	arg.rl_idx = idx;
1491a94100faSBill Paul 	arg.rl_maxsegs = 1;
1492a94100faSBill Paul 	arg.rl_flags = 0;
1493a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1494a94100faSBill Paul 
1495a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1496a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1497a94100faSBill Paul 	    &arg, BUS_DMA_NOWAIT);
1498a94100faSBill Paul 	if (error || arg.rl_maxsegs != 1) {
1499a94100faSBill Paul 		if (n != NULL)
1500a94100faSBill Paul 			m_freem(n);
1501b4b95879SMarius Strobl 		if (arg.rl_maxsegs == 0)
1502b4b95879SMarius Strobl 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1503b4b95879SMarius Strobl 			    sc->rl_ldata.rl_rx_dmamap[idx]);
1504a94100faSBill Paul 		return (ENOMEM);
1505a94100faSBill Paul 	}
1506a94100faSBill Paul 
1507a94100faSBill Paul 	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1508a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1509a94100faSBill Paul 
1510a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1511a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx],
1512a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1513a94100faSBill Paul 
1514a94100faSBill Paul 	return (0);
1515a94100faSBill Paul }
1516a94100faSBill Paul 
151722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
151822a11c96SJohn-Mark Gurney static __inline void
151922a11c96SJohn-Mark Gurney re_fixup_rx(m)
152022a11c96SJohn-Mark Gurney 	struct mbuf		*m;
152122a11c96SJohn-Mark Gurney {
152222a11c96SJohn-Mark Gurney 	int                     i;
152322a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
152422a11c96SJohn-Mark Gurney 
152522a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
152622a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
152722a11c96SJohn-Mark Gurney 
152822a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
152922a11c96SJohn-Mark Gurney 		*dst++ = *src++;
153022a11c96SJohn-Mark Gurney 
153122a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
153222a11c96SJohn-Mark Gurney 
153322a11c96SJohn-Mark Gurney 	return;
153422a11c96SJohn-Mark Gurney }
153522a11c96SJohn-Mark Gurney #endif
153622a11c96SJohn-Mark Gurney 
1537a94100faSBill Paul static int
1538a94100faSBill Paul re_tx_list_init(sc)
1539a94100faSBill Paul 	struct rl_softc		*sc;
1540a94100faSBill Paul {
154197b9d4baSJohn-Mark Gurney 
154297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
154397b9d4baSJohn-Mark Gurney 
1544a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1545a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1546a94100faSBill Paul 	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1547a94100faSBill Paul 
1548a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1549a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1550a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1551a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1552a94100faSBill Paul 	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1553a94100faSBill Paul 
1554a94100faSBill Paul 	return (0);
1555a94100faSBill Paul }
1556a94100faSBill Paul 
1557a94100faSBill Paul static int
1558a94100faSBill Paul re_rx_list_init(sc)
1559a94100faSBill Paul 	struct rl_softc		*sc;
1560a94100faSBill Paul {
1561a94100faSBill Paul 	int			i;
1562a94100faSBill Paul 
1563a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1564a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1565a94100faSBill Paul 	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1566a94100faSBill Paul 
1567a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1568a94100faSBill Paul 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1569a94100faSBill Paul 			return (ENOBUFS);
1570a94100faSBill Paul 	}
1571a94100faSBill Paul 
1572a94100faSBill Paul 	/* Flush the RX descriptors */
1573a94100faSBill Paul 
1574a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1575a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1576a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1577a94100faSBill Paul 
1578a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1579a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1580a94100faSBill Paul 
1581a94100faSBill Paul 	return (0);
1582a94100faSBill Paul }
1583a94100faSBill Paul 
1584a94100faSBill Paul /*
1585a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1586a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1587a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1588a94100faSBill Paul  */
1589ed510fb0SBill Paul static int
1590a94100faSBill Paul re_rxeof(sc)
1591a94100faSBill Paul 	struct rl_softc		*sc;
1592a94100faSBill Paul {
1593a94100faSBill Paul 	struct mbuf		*m;
1594a94100faSBill Paul 	struct ifnet		*ifp;
1595a94100faSBill Paul 	int			i, total_len;
1596a94100faSBill Paul 	struct rl_desc		*cur_rx;
1597a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1598ed510fb0SBill Paul 	int			maxpkt = 16;
1599a94100faSBill Paul 
16005120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
16015120abbfSSam Leffler 
1602fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1603a94100faSBill Paul 	i = sc->rl_ldata.rl_rx_prodidx;
1604a94100faSBill Paul 
1605a94100faSBill Paul 	/* Invalidate the descriptor memory */
1606a94100faSBill Paul 
1607a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1608a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1609a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1610a94100faSBill Paul 
1611ed510fb0SBill Paul 	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i]) && maxpkt) {
1612a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1613a94100faSBill Paul 		m = sc->rl_ldata.rl_rx_mbuf[i];
1614a94100faSBill Paul 		total_len = RL_RXBYTES(cur_rx);
1615a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1616a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1617a94100faSBill Paul 
1618a94100faSBill Paul 		/* Invalidate the RX mbuf and unload its map */
1619a94100faSBill Paul 
1620a94100faSBill Paul 		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1621a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i],
1622a94100faSBill Paul 		    BUS_DMASYNC_POSTWRITE);
1623a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1624a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i]);
1625a94100faSBill Paul 
1626a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
162722a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1628a94100faSBill Paul 			if (sc->rl_head == NULL)
1629a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1630a94100faSBill Paul 			else {
1631a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1632a94100faSBill Paul 				sc->rl_tail->m_next = m;
1633a94100faSBill Paul 				sc->rl_tail = m;
1634a94100faSBill Paul 			}
1635a94100faSBill Paul 			re_newbuf(sc, i, NULL);
1636a94100faSBill Paul 			RL_DESC_INC(i);
1637a94100faSBill Paul 			continue;
1638a94100faSBill Paul 		}
1639a94100faSBill Paul 
1640a94100faSBill Paul 		/*
1641a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1642a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1643a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1644a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1645a94100faSBill Paul 		 * were already used, so to make room for the extra
1646a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1647a94100faSBill Paul 		 * error' bit and shifted the other status bits
1648a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1649a94100faSBill Paul 		 * still in the same places. We have already extracted
1650a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1651a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1652a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1653a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1654a94100faSBill Paul 		 * same format as that of the 8139C+.
1655a94100faSBill Paul 		 */
1656a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1657a94100faSBill Paul 			rxstat >>= 1;
1658a94100faSBill Paul 
165922a11c96SJohn-Mark Gurney 		/*
166022a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
166122a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
166222a11c96SJohn-Mark Gurney 		 */
166322a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
166422a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1665a94100faSBill Paul 			ifp->if_ierrors++;
1666a94100faSBill Paul 			/*
1667a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1668a94100faSBill Paul 			 * discard all the pieces.
1669a94100faSBill Paul 			 */
1670a94100faSBill Paul 			if (sc->rl_head != NULL) {
1671a94100faSBill Paul 				m_freem(sc->rl_head);
1672a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1673a94100faSBill Paul 			}
1674a94100faSBill Paul 			re_newbuf(sc, i, m);
1675a94100faSBill Paul 			RL_DESC_INC(i);
1676a94100faSBill Paul 			continue;
1677a94100faSBill Paul 		}
1678a94100faSBill Paul 
1679a94100faSBill Paul 		/*
1680a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1681a94100faSBill Paul 		 * reload the current one.
1682a94100faSBill Paul 		 */
1683a94100faSBill Paul 
1684a94100faSBill Paul 		if (re_newbuf(sc, i, NULL)) {
1685a94100faSBill Paul 			ifp->if_ierrors++;
1686a94100faSBill Paul 			if (sc->rl_head != NULL) {
1687a94100faSBill Paul 				m_freem(sc->rl_head);
1688a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1689a94100faSBill Paul 			}
1690a94100faSBill Paul 			re_newbuf(sc, i, m);
1691a94100faSBill Paul 			RL_DESC_INC(i);
1692a94100faSBill Paul 			continue;
1693a94100faSBill Paul 		}
1694a94100faSBill Paul 
1695a94100faSBill Paul 		RL_DESC_INC(i);
1696a94100faSBill Paul 
1697a94100faSBill Paul 		if (sc->rl_head != NULL) {
169822a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
169922a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
170022a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1701a94100faSBill Paul 			/*
1702a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1703a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1704a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1705a94100faSBill Paul 			 * care about anyway.
1706a94100faSBill Paul 			 */
1707a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1708a94100faSBill Paul 				sc->rl_tail->m_len -=
1709a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1710a94100faSBill Paul 				m_freem(m);
1711a94100faSBill Paul 			} else {
1712a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1713a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1714a94100faSBill Paul 				sc->rl_tail->m_next = m;
1715a94100faSBill Paul 			}
1716a94100faSBill Paul 			m = sc->rl_head;
1717a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1718a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1719a94100faSBill Paul 		} else
1720a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1721a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1722a94100faSBill Paul 
172322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
172422a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
172522a11c96SJohn-Mark Gurney #endif
1726a94100faSBill Paul 		ifp->if_ipackets++;
1727a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1728a94100faSBill Paul 
1729a94100faSBill Paul 		/* Do RX checksumming if enabled */
1730a94100faSBill Paul 
1731a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1732a94100faSBill Paul 
1733a94100faSBill Paul 			/* Check IP header checksum */
1734a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1735a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1736a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1737a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1738a94100faSBill Paul 
1739a94100faSBill Paul 			/* Check TCP/UDP checksum */
1740a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1741a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1742a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1743a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1744a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1745a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1746a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1747a94100faSBill Paul 			}
1748a94100faSBill Paul 		}
1749ed510fb0SBill Paul 		maxpkt--;
1750d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
175178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
175278ba57b9SAndre Oppermann 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA));
175378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1754d147662cSGleb Smirnoff 		}
17555120abbfSSam Leffler 		RL_UNLOCK(sc);
1756a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
17575120abbfSSam Leffler 		RL_LOCK(sc);
1758a94100faSBill Paul 	}
1759a94100faSBill Paul 
1760a94100faSBill Paul 	/* Flush the RX DMA ring */
1761a94100faSBill Paul 
1762a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1763a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1764a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1765a94100faSBill Paul 
1766a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1767ed510fb0SBill Paul 
1768ed510fb0SBill Paul 	if (maxpkt)
1769ed510fb0SBill Paul 		return(EAGAIN);
1770ed510fb0SBill Paul 
1771ed510fb0SBill Paul 	return(0);
1772a94100faSBill Paul }
1773a94100faSBill Paul 
1774a94100faSBill Paul static void
1775a94100faSBill Paul re_txeof(sc)
1776a94100faSBill Paul 	struct rl_softc		*sc;
1777a94100faSBill Paul {
1778a94100faSBill Paul 	struct ifnet		*ifp;
1779a94100faSBill Paul 	u_int32_t		txstat;
1780a94100faSBill Paul 	int			idx;
1781a94100faSBill Paul 
1782fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1783a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_considx;
1784a94100faSBill Paul 
1785a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1786a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1787a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1788a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1789a94100faSBill Paul 
1790ed510fb0SBill Paul 	while (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) {
1791a94100faSBill Paul 		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1792a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_OWN)
1793a94100faSBill Paul 			break;
1794a94100faSBill Paul 
1795ed510fb0SBill Paul 		sc->rl_ldata.rl_tx_list[idx].rl_bufaddr_lo = 0;
1796ed510fb0SBill Paul 
1797a94100faSBill Paul 		/*
1798a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1799a94100faSBill Paul 		 * in a fragment chain, which also happens to
1800a94100faSBill Paul 		 * be the only place where the TX status bits
1801a94100faSBill Paul 		 * are valid.
1802a94100faSBill Paul 		 */
1803a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1804a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1805a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1806a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1807a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[idx]);
1808a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1809a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1810a94100faSBill Paul 				ifp->if_collisions++;
1811a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1812a94100faSBill Paul 				ifp->if_oerrors++;
1813a94100faSBill Paul 			else
1814a94100faSBill Paul 				ifp->if_opackets++;
1815a94100faSBill Paul 		}
1816a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1817a94100faSBill Paul 		RL_DESC_INC(idx);
1818a94100faSBill Paul 	}
1819b4b95879SMarius Strobl 	sc->rl_ldata.rl_tx_considx = idx;
1820a94100faSBill Paul 
1821a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1822a94100faSBill Paul 
1823b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free > RL_TX_DESC_THLD)
182413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1825a94100faSBill Paul 
1826b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) {
18270fc4974fSBill Paul 		/*
1828b4b95879SMarius Strobl 		 * Some chips will ignore a second TX request issued
1829b4b95879SMarius Strobl 		 * while an existing transmission is in progress. If
1830b4b95879SMarius Strobl 		 * the transmitter goes idle but there are still
1831b4b95879SMarius Strobl 		 * packets waiting to be sent, we need to restart the
1832b4b95879SMarius Strobl 		 * channel here to flush them out. This only seems to
1833b4b95879SMarius Strobl 		 * be required with the PCIe devices.
18340fc4974fSBill Paul 		 */
18350fc4974fSBill Paul 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
18360fc4974fSBill Paul 
1837ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1838a94100faSBill Paul 		/*
1839b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
1840b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
1841a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
1842a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
1843a94100faSBill Paul 		 */
1844a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1845ed510fb0SBill Paul #endif
1846b4b95879SMarius Strobl 	} else
1847b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
1848a94100faSBill Paul }
1849a94100faSBill Paul 
1850a94100faSBill Paul static void
1851a94100faSBill Paul re_tick(xsc)
1852a94100faSBill Paul 	void			*xsc;
1853a94100faSBill Paul {
1854a94100faSBill Paul 	struct rl_softc		*sc;
1855d1754a9bSJohn Baldwin 	struct mii_data		*mii;
1856ed510fb0SBill Paul 	struct ifnet		*ifp;
1857a94100faSBill Paul 
1858a94100faSBill Paul 	sc = xsc;
1859ed510fb0SBill Paul 	ifp = sc->rl_ifp;
186097b9d4baSJohn-Mark Gurney 
186197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
186297b9d4baSJohn-Mark Gurney 
18631d545c7aSMarius Strobl 	re_watchdog(sc);
1864a94100faSBill Paul 
18651d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
1866a94100faSBill Paul 	mii_tick(mii);
1867ed510fb0SBill Paul 	if (sc->rl_link) {
1868ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
1869ed510fb0SBill Paul 			sc->rl_link = 0;
1870ed510fb0SBill Paul 	} else {
1871ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1872ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1873ed510fb0SBill Paul 			sc->rl_link = 1;
1874ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1875ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
1876ed510fb0SBill Paul 				    &sc->rl_txtask);
1877ed510fb0SBill Paul 		}
1878ed510fb0SBill Paul 	}
1879a94100faSBill Paul 
1880d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
1881a94100faSBill Paul }
1882a94100faSBill Paul 
1883a94100faSBill Paul #ifdef DEVICE_POLLING
1884a94100faSBill Paul static void
1885a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1886a94100faSBill Paul {
1887a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1888a94100faSBill Paul 
1889a94100faSBill Paul 	RL_LOCK(sc);
189040929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
189197b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
189297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
189397b9d4baSJohn-Mark Gurney }
189497b9d4baSJohn-Mark Gurney 
189597b9d4baSJohn-Mark Gurney static void
189697b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
189797b9d4baSJohn-Mark Gurney {
189897b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
189997b9d4baSJohn-Mark Gurney 
190097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
190197b9d4baSJohn-Mark Gurney 
1902a94100faSBill Paul 	sc->rxcycles = count;
1903a94100faSBill Paul 	re_rxeof(sc);
1904a94100faSBill Paul 	re_txeof(sc);
1905a94100faSBill Paul 
190637652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1907ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
1908a94100faSBill Paul 
1909a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1910a94100faSBill Paul 		u_int16_t       status;
1911a94100faSBill Paul 
1912a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1913a94100faSBill Paul 		if (status == 0xffff)
191497b9d4baSJohn-Mark Gurney 			return;
1915a94100faSBill Paul 		if (status)
1916a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1917a94100faSBill Paul 
1918a94100faSBill Paul 		/*
1919a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
1920a94100faSBill Paul 		 */
1921a94100faSBill Paul 
1922a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1923a94100faSBill Paul 			re_reset(sc);
192497b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
1925a94100faSBill Paul 		}
1926a94100faSBill Paul 	}
1927a94100faSBill Paul }
1928a94100faSBill Paul #endif /* DEVICE_POLLING */
1929a94100faSBill Paul 
1930ef544f63SPaolo Pisati static int
1931a94100faSBill Paul re_intr(arg)
1932a94100faSBill Paul 	void			*arg;
1933a94100faSBill Paul {
1934a94100faSBill Paul 	struct rl_softc		*sc;
1935ed510fb0SBill Paul 	uint16_t		status;
1936a94100faSBill Paul 
1937a94100faSBill Paul 	sc = arg;
1938ed510fb0SBill Paul 
1939ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
1940498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
1941ef544f63SPaolo Pisati                 return (FILTER_STRAY);
1942ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
1943ed510fb0SBill Paul 
1944ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
1945ed510fb0SBill Paul 
1946ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
1947ed510fb0SBill Paul }
1948ed510fb0SBill Paul 
1949ed510fb0SBill Paul static void
1950ed510fb0SBill Paul re_int_task(arg, npending)
1951ed510fb0SBill Paul 	void			*arg;
1952ed510fb0SBill Paul 	int			npending;
1953ed510fb0SBill Paul {
1954ed510fb0SBill Paul 	struct rl_softc		*sc;
1955ed510fb0SBill Paul 	struct ifnet		*ifp;
1956ed510fb0SBill Paul 	u_int16_t		status;
1957ed510fb0SBill Paul 	int			rval = 0;
1958ed510fb0SBill Paul 
1959ed510fb0SBill Paul 	sc = arg;
1960ed510fb0SBill Paul 	ifp = sc->rl_ifp;
1961a94100faSBill Paul 
1962a94100faSBill Paul 	RL_LOCK(sc);
196397b9d4baSJohn-Mark Gurney 
1964a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
1965a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
1966a94100faSBill Paul 
1967ed510fb0SBill Paul 	if (sc->suspended || !(ifp->if_flags & IFF_UP)) {
1968ed510fb0SBill Paul 		RL_UNLOCK(sc);
1969ed510fb0SBill Paul 		return;
1970ed510fb0SBill Paul 	}
1971a94100faSBill Paul 
1972ed510fb0SBill Paul #ifdef DEVICE_POLLING
1973ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
1974ed510fb0SBill Paul 		RL_UNLOCK(sc);
1975ed510fb0SBill Paul 		return;
1976ed510fb0SBill Paul 	}
1977ed510fb0SBill Paul #endif
1978a94100faSBill Paul 
1979ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
1980ed510fb0SBill Paul 		rval = re_rxeof(sc);
1981ed510fb0SBill Paul 
1982ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1983ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
1984ed510fb0SBill Paul #else
1985ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
1986ed510fb0SBill Paul #endif
1987ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
1988a94100faSBill Paul 		re_txeof(sc);
1989a94100faSBill Paul 
1990a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
1991a94100faSBill Paul 		re_reset(sc);
199297b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
1993a94100faSBill Paul 	}
1994a94100faSBill Paul 
1995a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
1996d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
1997d1754a9bSJohn Baldwin 		re_tick(sc);
1998a94100faSBill Paul 	}
1999a94100faSBill Paul 
200052732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2001ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2002a94100faSBill Paul 
2003a94100faSBill Paul 	RL_UNLOCK(sc);
2004ed510fb0SBill Paul 
2005ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2006ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2007ed510fb0SBill Paul 		return;
2008ed510fb0SBill Paul 	}
2009ed510fb0SBill Paul 
2010ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2011ed510fb0SBill Paul 
2012ed510fb0SBill Paul 	return;
2013a94100faSBill Paul }
2014a94100faSBill Paul 
2015a94100faSBill Paul static int
2016a94100faSBill Paul re_encap(sc, m_head, idx)
2017a94100faSBill Paul 	struct rl_softc		*sc;
201880a2a305SJohn-Mark Gurney 	struct mbuf		**m_head;
2019a94100faSBill Paul 	int			*idx;
2020a94100faSBill Paul {
2021a94100faSBill Paul 	struct mbuf		*m_new = NULL;
2022a94100faSBill Paul 	struct rl_dmaload_arg	arg;
2023a94100faSBill Paul 	bus_dmamap_t		map;
2024a94100faSBill Paul 	int			error;
2025a94100faSBill Paul 
202697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
202797b9d4baSJohn-Mark Gurney 
2028b4b95879SMarius Strobl 	if (sc->rl_ldata.rl_tx_free <= RL_TX_DESC_THLD)
2029a94100faSBill Paul 		return (EFBIG);
2030a94100faSBill Paul 
2031a94100faSBill Paul 	/*
2032a94100faSBill Paul 	 * Set up checksum offload. Note: checksum offload bits must
2033a94100faSBill Paul 	 * appear in all descriptors of a multi-descriptor transmit
203422a11c96SJohn-Mark Gurney 	 * attempt. This is according to testing done with an 8169
203522a11c96SJohn-Mark Gurney 	 * chip. This is a requirement.
2036a94100faSBill Paul 	 */
2037a94100faSBill Paul 
2038a94100faSBill Paul 	arg.rl_flags = 0;
2039a94100faSBill Paul 
2040dc74159dSPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0)
2041dc74159dSPyun YongHyeon 		arg.rl_flags = RL_TDESC_CMD_LGSEND |
2042dc74159dSPyun YongHyeon 		    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2043dc74159dSPyun YongHyeon 		    RL_TDESC_CMD_MSSVAL_SHIFT);
2044dc74159dSPyun YongHyeon 	else {
204580a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
2046a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
204780a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
2048a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
204980a2a305SJohn-Mark Gurney 		if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
2050a94100faSBill Paul 			arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
2051dc74159dSPyun YongHyeon 	}
2052a94100faSBill Paul 
2053a94100faSBill Paul 	arg.rl_idx = *idx;
2054a94100faSBill Paul 	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
2055b4b95879SMarius Strobl 	if (arg.rl_maxsegs > RL_TX_DESC_THLD)
2056b4b95879SMarius Strobl 		arg.rl_maxsegs -= RL_TX_DESC_THLD;
2057a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_tx_list;
2058a94100faSBill Paul 
2059a94100faSBill Paul 	map = sc->rl_ldata.rl_tx_dmamap[*idx];
20600fc4974fSBill Paul 
20610fc4974fSBill Paul 	/*
20620fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
20630fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
20640fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
20650fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
20660fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
20670fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
20680fc4974fSBill Paul 	 * have garbled payload. To work around this, if TX checksum
20690fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
20700fc4974fSBill Paul 	 * to the minimum ethernet frame size. We do this by pretending
20710fc4974fSBill Paul 	 * the mbuf chain has too many fragments so the coalescing code
20720fc4974fSBill Paul 	 * below can assemble the packet into a single buffer that's
20730fc4974fSBill Paul 	 * padded out to the mininum frame size.
2074e2bcb489SBill Paul 	 *
2075e2bcb489SBill Paul 	 * Note: this appears unnecessary for TCP, and doing it for TCP
2076e2bcb489SBill Paul 	 * with PCIe adapters seems to result in bad checksums.
20770fc4974fSBill Paul 	 */
2078e2bcb489SBill Paul 
2079e2bcb489SBill Paul 	if (arg.rl_flags && !(arg.rl_flags & RL_TDESC_CMD_TCPCSUM) &&
2080e2bcb489SBill Paul             (*m_head)->m_pkthdr.len < RL_MIN_FRAMELEN)
20810fc4974fSBill Paul 		error = EFBIG;
20820fc4974fSBill Paul 	else
2083a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
208480a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2085a94100faSBill Paul 
2086a94100faSBill Paul 	if (error && error != EFBIG) {
20876b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "can't map mbuf (error %d)\n", error);
2088a94100faSBill Paul 		return (ENOBUFS);
2089a94100faSBill Paul 	}
2090a94100faSBill Paul 
2091a94100faSBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
2092a94100faSBill Paul 
2093a94100faSBill Paul 	if (error || arg.rl_maxsegs == 0) {
2094b4b95879SMarius Strobl 		if (arg.rl_maxsegs == 0)
2095b4b95879SMarius Strobl 			bus_dmamap_unload(sc->rl_ldata.rl_mtag, map);
209680a2a305SJohn-Mark Gurney 		m_new = m_defrag(*m_head, M_DONTWAIT);
2097b4b95879SMarius Strobl 		if (m_new == NULL) {
2098b4b95879SMarius Strobl 			m_freem(*m_head);
2099b4b95879SMarius Strobl 			*m_head = NULL;
210080a2a305SJohn-Mark Gurney 			return (ENOBUFS);
2101b4b95879SMarius Strobl 		}
210280a2a305SJohn-Mark Gurney 		*m_head = m_new;
2103a94100faSBill Paul 
21040fc4974fSBill Paul 		/*
21050fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
21060fc4974fSBill Paul 		 * to avoid leaking data.
21070fc4974fSBill Paul 		 */
21080fc4974fSBill Paul 		if (m_new->m_pkthdr.len < RL_MIN_FRAMELEN) {
21090fc4974fSBill Paul 			bzero(mtod(m_new, char *) + m_new->m_pkthdr.len,
21100fc4974fSBill Paul 			    RL_MIN_FRAMELEN - m_new->m_pkthdr.len);
21110fc4974fSBill Paul 			m_new->m_pkthdr.len += RL_MIN_FRAMELEN -
21120fc4974fSBill Paul 			    m_new->m_pkthdr.len;
21130fc4974fSBill Paul 			m_new->m_len = m_new->m_pkthdr.len;
21140fc4974fSBill Paul 		}
21150fc4974fSBill Paul 
2116b4b95879SMarius Strobl 		/* Note that we'll run over RL_TX_DESC_THLD here. */
2117a94100faSBill Paul 		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
2118a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
211980a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2120b4b95879SMarius Strobl 		if (error || arg.rl_maxsegs == 0) {
2121b4b95879SMarius Strobl 			device_printf(sc->rl_dev,
2122b4b95879SMarius Strobl 			    "can't map defragmented mbuf (error %d)\n", error);
2123b4b95879SMarius Strobl 			m_freem(m_new);
2124b4b95879SMarius Strobl 			*m_head = NULL;
2125b4b95879SMarius Strobl 			if (arg.rl_maxsegs == 0)
2126b4b95879SMarius Strobl 				bus_dmamap_unload(sc->rl_ldata.rl_mtag, map);
2127a94100faSBill Paul 			return (EFBIG);
2128a94100faSBill Paul 		}
2129a94100faSBill Paul 	}
2130a94100faSBill Paul 
2131a94100faSBill Paul 	/*
2132a94100faSBill Paul 	 * Insure that the map for this transmission
2133a94100faSBill Paul 	 * is placed at the array index of the last descriptor
213422a11c96SJohn-Mark Gurney 	 * in this chain.  (Swap last and first dmamaps.)
2135a94100faSBill Paul 	 */
2136a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[*idx] =
2137a94100faSBill Paul 	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
2138a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
2139a94100faSBill Paul 
214080a2a305SJohn-Mark Gurney 	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head;
2141a94100faSBill Paul 	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
2142a94100faSBill Paul 
2143a94100faSBill Paul 	/*
2144a94100faSBill Paul 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2145a94100faSBill Paul 	 * appear in the first descriptor of a multi-descriptor
2146a94100faSBill Paul 	 * transmission attempt.
2147a94100faSBill Paul 	 */
214878ba57b9SAndre Oppermann 	if ((*m_head)->m_flags & M_VLANTAG)
2149a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
215078ba57b9SAndre Oppermann 		    htole32(htons((*m_head)->m_pkthdr.ether_vtag) |
215178ba57b9SAndre Oppermann 		    RL_TDESC_VLANCTL_TAG);
2152a94100faSBill Paul 
2153a94100faSBill Paul 	/* Transfer ownership of packet to the chip. */
2154a94100faSBill Paul 
2155a94100faSBill Paul 	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
2156a94100faSBill Paul 	    htole32(RL_TDESC_CMD_OWN);
2157a94100faSBill Paul 	if (*idx != arg.rl_idx)
2158a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
2159a94100faSBill Paul 		    htole32(RL_TDESC_CMD_OWN);
2160a94100faSBill Paul 
2161a94100faSBill Paul         RL_DESC_INC(arg.rl_idx);
2162a94100faSBill Paul 	*idx = arg.rl_idx;
2163a94100faSBill Paul 
2164a94100faSBill Paul 	return (0);
2165a94100faSBill Paul }
2166a94100faSBill Paul 
216797b9d4baSJohn-Mark Gurney static void
2168ed510fb0SBill Paul re_tx_task(arg, npending)
2169ed510fb0SBill Paul 	void			*arg;
2170ed510fb0SBill Paul 	int			npending;
217197b9d4baSJohn-Mark Gurney {
2172ed510fb0SBill Paul 	struct ifnet		*ifp;
217397b9d4baSJohn-Mark Gurney 
2174ed510fb0SBill Paul 	ifp = arg;
2175ed510fb0SBill Paul 	re_start(ifp);
2176ed510fb0SBill Paul 
2177ed510fb0SBill Paul 	return;
217897b9d4baSJohn-Mark Gurney }
217997b9d4baSJohn-Mark Gurney 
2180a94100faSBill Paul /*
2181a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2182a94100faSBill Paul  */
2183a94100faSBill Paul static void
2184ed510fb0SBill Paul re_start(ifp)
2185a94100faSBill Paul 	struct ifnet		*ifp;
2186a94100faSBill Paul {
2187a94100faSBill Paul 	struct rl_softc		*sc;
2188a94100faSBill Paul 	struct mbuf		*m_head = NULL;
218952732175SMax Laier 	int			idx, queued = 0;
2190a94100faSBill Paul 
2191a94100faSBill Paul 	sc = ifp->if_softc;
219297b9d4baSJohn-Mark Gurney 
2193ed510fb0SBill Paul 	RL_LOCK(sc);
2194ed510fb0SBill Paul 
2195ed510fb0SBill Paul 	if (!sc->rl_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) {
2196ed510fb0SBill Paul 		RL_UNLOCK(sc);
2197ed510fb0SBill Paul 		return;
2198ed510fb0SBill Paul 	}
2199a94100faSBill Paul 
2200a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_prodidx;
2201a94100faSBill Paul 
2202a94100faSBill Paul 	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
220352732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2204a94100faSBill Paul 		if (m_head == NULL)
2205a94100faSBill Paul 			break;
2206a94100faSBill Paul 
220780a2a305SJohn-Mark Gurney 		if (re_encap(sc, &m_head, &idx)) {
2208b4b95879SMarius Strobl 			if (m_head == NULL)
2209b4b95879SMarius Strobl 				break;
221052732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
221113f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2212a94100faSBill Paul 			break;
2213a94100faSBill Paul 		}
2214a94100faSBill Paul 
2215a94100faSBill Paul 		/*
2216a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2217a94100faSBill Paul 		 * to him.
2218a94100faSBill Paul 		 */
221959a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
222052732175SMax Laier 
222152732175SMax Laier 		queued++;
2222a94100faSBill Paul 	}
2223a94100faSBill Paul 
2224ed510fb0SBill Paul 	if (queued == 0) {
2225ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2226ed510fb0SBill Paul 		if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
2227ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2228ed510fb0SBill Paul #endif
2229ed510fb0SBill Paul 		RL_UNLOCK(sc);
223052732175SMax Laier 		return;
2231ed510fb0SBill Paul 	}
223252732175SMax Laier 
2233a94100faSBill Paul 	/* Flush the TX descriptors */
2234a94100faSBill Paul 
2235a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2236a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2237a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2238a94100faSBill Paul 
2239a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = idx;
2240a94100faSBill Paul 
22410fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2242a94100faSBill Paul 
2243ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2244a94100faSBill Paul 	/*
2245a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2246a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2247a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2248a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2249a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2250a94100faSBill Paul 	 * the timer count is reset to 0.
2251a94100faSBill Paul 	 */
2252a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2253ed510fb0SBill Paul #endif
2254a94100faSBill Paul 
2255a94100faSBill Paul 	/*
2256a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2257a94100faSBill Paul 	 */
22581d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2259ed510fb0SBill Paul 
2260ed510fb0SBill Paul 	RL_UNLOCK(sc);
2261ed510fb0SBill Paul 
2262ed510fb0SBill Paul 	return;
2263a94100faSBill Paul }
2264a94100faSBill Paul 
2265a94100faSBill Paul static void
2266a94100faSBill Paul re_init(xsc)
2267a94100faSBill Paul 	void			*xsc;
2268a94100faSBill Paul {
2269a94100faSBill Paul 	struct rl_softc		*sc = xsc;
227097b9d4baSJohn-Mark Gurney 
227197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
227297b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
227397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
227497b9d4baSJohn-Mark Gurney }
227597b9d4baSJohn-Mark Gurney 
227697b9d4baSJohn-Mark Gurney static void
227797b9d4baSJohn-Mark Gurney re_init_locked(sc)
227897b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
227997b9d4baSJohn-Mark Gurney {
2280fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2281a94100faSBill Paul 	struct mii_data		*mii;
2282a94100faSBill Paul 	u_int32_t		rxcfg = 0;
22834d3d7085SBernd Walter 	union {
22844d3d7085SBernd Walter 		uint32_t align_dummy;
22854d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
22864d3d7085SBernd Walter         } eaddr;
2287a94100faSBill Paul 
228897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
228997b9d4baSJohn-Mark Gurney 
2290a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2291a94100faSBill Paul 
2292a94100faSBill Paul 	/*
2293a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2294a94100faSBill Paul 	 */
2295a94100faSBill Paul 	re_stop(sc);
2296a94100faSBill Paul 
2297a94100faSBill Paul 	/*
2298c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2299edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2300c2c6548bSBill Paul 	 * before all others.
2301c2c6548bSBill Paul 	 */
2302c2c6548bSBill Paul 	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2303c2c6548bSBill Paul 	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2304ed510fb0SBill Paul 	    RL_CPLUSCMD_VLANSTRIP|RL_CPLUSCMD_RXCSUM_ENB);
2305c2c6548bSBill Paul 
2306c2c6548bSBill Paul 	/*
2307a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2308a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2309a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2310a94100faSBill Paul 	 */
23114d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
23124d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2313a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2314ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2315ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2316ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2317ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2318a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2319a94100faSBill Paul 
2320a94100faSBill Paul 	/*
2321a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2322a94100faSBill Paul 	 */
2323a94100faSBill Paul 	re_rx_list_init(sc);
2324a94100faSBill Paul 	re_tx_list_init(sc);
2325a94100faSBill Paul 
2326a94100faSBill Paul 	/*
2327d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2328d01fac16SPyun YongHyeon 	 */
2329d01fac16SPyun YongHyeon 
2330d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2331d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2332d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2333d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2334d01fac16SPyun YongHyeon 
2335d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2336d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2337d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2338d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2339d01fac16SPyun YongHyeon 
2340d01fac16SPyun YongHyeon 	/*
2341a94100faSBill Paul 	 * Enable transmit and receive.
2342a94100faSBill Paul 	 */
2343a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2344a94100faSBill Paul 
2345a94100faSBill Paul 	/*
2346a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2347a94100faSBill Paul 	 */
2348abc8ff44SBill Paul 	if (sc->rl_testmode) {
2349abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2350abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2351abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2352a94100faSBill Paul 		else
2353abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2354abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2355abc8ff44SBill Paul 	} else
2356a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2357d01fac16SPyun YongHyeon 
2358d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2359d01fac16SPyun YongHyeon 
2360a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2361a94100faSBill Paul 
2362a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2363a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2364a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2365a94100faSBill Paul 
2366a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
236761021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2368a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
236961021536SJohn-Mark Gurney 	else
2370a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2371a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2372a94100faSBill Paul 
2373a94100faSBill Paul 	/*
2374a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2375a94100faSBill Paul 	 */
237661021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2377a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
237861021536SJohn-Mark Gurney 	else
2379a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2380a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2381a94100faSBill Paul 
2382a94100faSBill Paul 	/*
2383a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2384a94100faSBill Paul 	 */
2385a94100faSBill Paul 	re_setmulti(sc);
2386a94100faSBill Paul 
2387a94100faSBill Paul #ifdef DEVICE_POLLING
2388a94100faSBill Paul 	/*
2389a94100faSBill Paul 	 * Disable interrupts if we are polling.
2390a94100faSBill Paul 	 */
239140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2392a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2393a94100faSBill Paul 	else	/* otherwise ... */
239440929967SGleb Smirnoff #endif
2395ed510fb0SBill Paul 
2396a94100faSBill Paul 	/*
2397a94100faSBill Paul 	 * Enable interrupts.
2398a94100faSBill Paul 	 */
2399a94100faSBill Paul 	if (sc->rl_testmode)
2400a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2401a94100faSBill Paul 	else
2402a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2403ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2404a94100faSBill Paul 
2405a94100faSBill Paul 	/* Set initial TX threshold */
2406a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2407a94100faSBill Paul 
2408a94100faSBill Paul 	/* Start RX/TX process. */
2409a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2410a94100faSBill Paul #ifdef notdef
2411a94100faSBill Paul 	/* Enable receiver and transmitter. */
2412a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2413a94100faSBill Paul #endif
2414a94100faSBill Paul 
2415ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2416a94100faSBill Paul 	/*
2417a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2418a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2419a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2420a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2421a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2422a94100faSBill Paul 	 */
2423a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2424a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2425a94100faSBill Paul 	else
2426a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2427ed510fb0SBill Paul #endif
2428a94100faSBill Paul 
2429a94100faSBill Paul 	/*
2430a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2431a94100faSBill Paul 	 * size so we can receive jumbo frames.
2432a94100faSBill Paul 	 */
2433a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2434a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2435a94100faSBill Paul 
243697b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2437a94100faSBill Paul 		return;
2438a94100faSBill Paul 
2439a94100faSBill Paul 	mii_mediachg(mii);
2440a94100faSBill Paul 
244119ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
2442a94100faSBill Paul 
244313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
244413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2445a94100faSBill Paul 
2446ed510fb0SBill Paul 	sc->rl_link = 0;
24471d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2448d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2449a94100faSBill Paul }
2450a94100faSBill Paul 
2451a94100faSBill Paul /*
2452a94100faSBill Paul  * Set media options.
2453a94100faSBill Paul  */
2454a94100faSBill Paul static int
2455a94100faSBill Paul re_ifmedia_upd(ifp)
2456a94100faSBill Paul 	struct ifnet		*ifp;
2457a94100faSBill Paul {
2458a94100faSBill Paul 	struct rl_softc		*sc;
2459a94100faSBill Paul 	struct mii_data		*mii;
2460a94100faSBill Paul 
2461a94100faSBill Paul 	sc = ifp->if_softc;
2462a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2463d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2464a94100faSBill Paul 	mii_mediachg(mii);
2465d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2466a94100faSBill Paul 
2467a94100faSBill Paul 	return (0);
2468a94100faSBill Paul }
2469a94100faSBill Paul 
2470a94100faSBill Paul /*
2471a94100faSBill Paul  * Report current media status.
2472a94100faSBill Paul  */
2473a94100faSBill Paul static void
2474a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2475a94100faSBill Paul 	struct ifnet		*ifp;
2476a94100faSBill Paul 	struct ifmediareq	*ifmr;
2477a94100faSBill Paul {
2478a94100faSBill Paul 	struct rl_softc		*sc;
2479a94100faSBill Paul 	struct mii_data		*mii;
2480a94100faSBill Paul 
2481a94100faSBill Paul 	sc = ifp->if_softc;
2482a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2483a94100faSBill Paul 
2484d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2485a94100faSBill Paul 	mii_pollstat(mii);
2486d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2487a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2488a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2489a94100faSBill Paul }
2490a94100faSBill Paul 
2491a94100faSBill Paul static int
2492a94100faSBill Paul re_ioctl(ifp, command, data)
2493a94100faSBill Paul 	struct ifnet		*ifp;
2494a94100faSBill Paul 	u_long			command;
2495a94100faSBill Paul 	caddr_t			data;
2496a94100faSBill Paul {
2497a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2498a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2499a94100faSBill Paul 	struct mii_data		*mii;
250040929967SGleb Smirnoff 	int			error = 0;
2501a94100faSBill Paul 
2502a94100faSBill Paul 	switch (command) {
2503a94100faSBill Paul 	case SIOCSIFMTU:
2504d1754a9bSJohn Baldwin 		RL_LOCK(sc);
2505a94100faSBill Paul 		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2506a94100faSBill Paul 			error = EINVAL;
2507a94100faSBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2508d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2509a94100faSBill Paul 		break;
2510a94100faSBill Paul 	case SIOCSIFFLAGS:
251197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2512eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2513eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2514eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
2515eed497bbSPyun YongHyeon 				    & IFF_PROMISC) != 0)
2516eed497bbSPyun YongHyeon 					re_setmulti(sc);
2517eed497bbSPyun YongHyeon 			} else
251897b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
2519eed497bbSPyun YongHyeon 		} else {
2520eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2521a94100faSBill Paul 				re_stop(sc);
2522eed497bbSPyun YongHyeon 		}
2523eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
252497b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2525a94100faSBill Paul 		break;
2526a94100faSBill Paul 	case SIOCADDMULTI:
2527a94100faSBill Paul 	case SIOCDELMULTI:
252897b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2529a94100faSBill Paul 		re_setmulti(sc);
253097b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2531a94100faSBill Paul 		break;
2532a94100faSBill Paul 	case SIOCGIFMEDIA:
2533a94100faSBill Paul 	case SIOCSIFMEDIA:
2534a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2535a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2536a94100faSBill Paul 		break;
2537a94100faSBill Paul 	case SIOCSIFCAP:
253840929967SGleb Smirnoff 	    {
2539f051cb85SGleb Smirnoff 		int mask, reinit;
2540f051cb85SGleb Smirnoff 
2541f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2542f051cb85SGleb Smirnoff 		reinit = 0;
254340929967SGleb Smirnoff #ifdef DEVICE_POLLING
254440929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
254540929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
254640929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
254740929967SGleb Smirnoff 				if (error)
254840929967SGleb Smirnoff 					return(error);
2549d1754a9bSJohn Baldwin 				RL_LOCK(sc);
255040929967SGleb Smirnoff 				/* Disable interrupts */
255140929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
255240929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
255340929967SGleb Smirnoff 				RL_UNLOCK(sc);
255440929967SGleb Smirnoff 			} else {
255540929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
255640929967SGleb Smirnoff 				/* Enable interrupts. */
255740929967SGleb Smirnoff 				RL_LOCK(sc);
255840929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
255940929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
256040929967SGleb Smirnoff 				RL_UNLOCK(sc);
256140929967SGleb Smirnoff 			}
256240929967SGleb Smirnoff 		}
256340929967SGleb Smirnoff #endif /* DEVICE_POLLING */
256440929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2565f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2566a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2567dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
2568a94100faSBill Paul 			else
2569b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
2570f051cb85SGleb Smirnoff 			reinit = 1;
257140929967SGleb Smirnoff 		}
2572f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2573f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2574f051cb85SGleb Smirnoff 			reinit = 1;
2575f051cb85SGleb Smirnoff 		}
2576dc74159dSPyun YongHyeon 		if (mask & IFCAP_TSO4) {
2577dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
2578dc74159dSPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) &&
2579dc74159dSPyun YongHyeon 			    (IFCAP_TSO4 & ifp->if_capabilities))
2580dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
2581dc74159dSPyun YongHyeon 			else
2582dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
2583dc74159dSPyun YongHyeon 		}
2584f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2585f051cb85SGleb Smirnoff 			re_init(sc);
2586960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
258740929967SGleb Smirnoff 	    }
2588a94100faSBill Paul 		break;
2589a94100faSBill Paul 	default:
2590a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2591a94100faSBill Paul 		break;
2592a94100faSBill Paul 	}
2593a94100faSBill Paul 
2594a94100faSBill Paul 	return (error);
2595a94100faSBill Paul }
2596a94100faSBill Paul 
2597a94100faSBill Paul static void
25981d545c7aSMarius Strobl re_watchdog(sc)
2599a94100faSBill Paul 	struct rl_softc		*sc;
26001d545c7aSMarius Strobl {
2601a94100faSBill Paul 
26021d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
26031d545c7aSMarius Strobl 
26041d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
26051d545c7aSMarius Strobl 		return;
26061d545c7aSMarius Strobl 
26071d545c7aSMarius Strobl 	device_printf(sc->rl_dev, "watchdog timeout\n");
26081d545c7aSMarius Strobl 	sc->rl_ifp->if_oerrors++;
2609a94100faSBill Paul 
2610a94100faSBill Paul 	re_txeof(sc);
2611a94100faSBill Paul 	re_rxeof(sc);
261297b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2613a94100faSBill Paul }
2614a94100faSBill Paul 
2615a94100faSBill Paul /*
2616a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2617a94100faSBill Paul  * RX and TX lists.
2618a94100faSBill Paul  */
2619a94100faSBill Paul static void
2620a94100faSBill Paul re_stop(sc)
2621a94100faSBill Paul 	struct rl_softc		*sc;
2622a94100faSBill Paul {
2623a94100faSBill Paul 	register int		i;
2624a94100faSBill Paul 	struct ifnet		*ifp;
2625a94100faSBill Paul 
262697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
262797b9d4baSJohn-Mark Gurney 
2628fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2629a94100faSBill Paul 
26301d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
2631d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
263213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2633a94100faSBill Paul 
2634a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2635a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2636ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2637a94100faSBill Paul 
2638a94100faSBill Paul 	if (sc->rl_head != NULL) {
2639a94100faSBill Paul 		m_freem(sc->rl_head);
2640a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2641a94100faSBill Paul 	}
2642a94100faSBill Paul 
2643a94100faSBill Paul 	/* Free the TX list buffers. */
2644a94100faSBill Paul 
2645a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2646a94100faSBill Paul 		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2647a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2648a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
2649a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2650a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2651a94100faSBill Paul 		}
2652a94100faSBill Paul 	}
2653a94100faSBill Paul 
2654a94100faSBill Paul 	/* Free the RX list buffers. */
2655a94100faSBill Paul 
2656a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2657a94100faSBill Paul 		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2658a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2659a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
2660a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2661a94100faSBill Paul 			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2662a94100faSBill Paul 		}
2663a94100faSBill Paul 	}
2664a94100faSBill Paul }
2665a94100faSBill Paul 
2666a94100faSBill Paul /*
2667a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2668a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2669a94100faSBill Paul  * resume.
2670a94100faSBill Paul  */
2671a94100faSBill Paul static int
2672a94100faSBill Paul re_suspend(dev)
2673a94100faSBill Paul 	device_t		dev;
2674a94100faSBill Paul {
2675a94100faSBill Paul 	struct rl_softc		*sc;
2676a94100faSBill Paul 
2677a94100faSBill Paul 	sc = device_get_softc(dev);
2678a94100faSBill Paul 
267997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2680a94100faSBill Paul 	re_stop(sc);
2681a94100faSBill Paul 	sc->suspended = 1;
268297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2683a94100faSBill Paul 
2684a94100faSBill Paul 	return (0);
2685a94100faSBill Paul }
2686a94100faSBill Paul 
2687a94100faSBill Paul /*
2688a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2689a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2690a94100faSBill Paul  * appropriate.
2691a94100faSBill Paul  */
2692a94100faSBill Paul static int
2693a94100faSBill Paul re_resume(dev)
2694a94100faSBill Paul 	device_t		dev;
2695a94100faSBill Paul {
2696a94100faSBill Paul 	struct rl_softc		*sc;
2697a94100faSBill Paul 	struct ifnet		*ifp;
2698a94100faSBill Paul 
2699a94100faSBill Paul 	sc = device_get_softc(dev);
270097b9d4baSJohn-Mark Gurney 
270197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
270297b9d4baSJohn-Mark Gurney 
2703fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2704a94100faSBill Paul 
2705a94100faSBill Paul 	/* reinitialize interface if necessary */
2706a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
270797b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2708a94100faSBill Paul 
2709a94100faSBill Paul 	sc->suspended = 0;
271097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2711a94100faSBill Paul 
2712a94100faSBill Paul 	return (0);
2713a94100faSBill Paul }
2714a94100faSBill Paul 
2715a94100faSBill Paul /*
2716a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2717a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2718a94100faSBill Paul  */
2719a94100faSBill Paul static void
2720a94100faSBill Paul re_shutdown(dev)
2721a94100faSBill Paul 	device_t		dev;
2722a94100faSBill Paul {
2723a94100faSBill Paul 	struct rl_softc		*sc;
2724a94100faSBill Paul 
2725a94100faSBill Paul 	sc = device_get_softc(dev);
2726a94100faSBill Paul 
272797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2728a94100faSBill Paul 	re_stop(sc);
2729536fde34SMaxim Sobolev 	/*
2730536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2731536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
273272293673SRuslan Ermilov 	 * cases.
2733536fde34SMaxim Sobolev 	 */
2734536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
273597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2736a94100faSBill Paul }
2737