xref: /freebsd/sys/dev/re/if_re.c (revision ed510fb04f29a372219ef9c587ff9259eec87e11)
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" },
17516a67f53SGleb Smirnoff 	{ RT_VENDORID, RT_DEVICEID_8168, RL_HWREV_8168,
176ed510fb0SBill Paul 		"RealTek 8168B/8111B PCIe Gigabit Ethernet" },
177a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
178a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
17969a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
18069a6b7fbSBill Paul 		"RealTek 8169S Single-chip Gigabit Ethernet" },
181ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SB,
182ed510fb0SBill Paul 		"RealTek 8169SB/8110SB Single-chip Gigabit Ethernet" },
183ed510fb0SBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169_8110SC,
184ed510fb0SBill Paul 		"RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
18569a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
18669a6b7fbSBill Paul 		"RealTek 8110S Single-chip Gigabit Ethernet" },
187ea263191SMIHIRA Sanpei Yoshiro 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
188ea263191SMIHIRA Sanpei Yoshiro 		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
18926390635SJohn Baldwin 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, RL_HWREV_8169S,
19026390635SJohn Baldwin 		"Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
191a94100faSBill Paul 	{ 0, 0, 0, NULL }
192a94100faSBill Paul };
193a94100faSBill Paul 
194a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
195a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
196a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
197a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
198a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
199a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
200a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
201a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
202a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
203ed510fb0SBill Paul 	{ RL_HWREV_8168, RL_8169, "8168"},
204a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
20569a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
20669a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
207ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB"},
208ed510fb0SBill Paul 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC"},
209a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
210a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
211ed510fb0SBill Paul 	{ RL_HWREV_8100E, RL_8169, "8100E"},
212ed510fb0SBill Paul 	{ RL_HWREV_8101E, RL_8169, "8101E"},
213a94100faSBill Paul 	{ 0, 0, NULL }
214a94100faSBill Paul };
215a94100faSBill Paul 
216a94100faSBill Paul static int re_probe		(device_t);
217a94100faSBill Paul static int re_attach		(device_t);
218a94100faSBill Paul static int re_detach		(device_t);
219a94100faSBill Paul 
22080a2a305SJohn-Mark Gurney static int re_encap		(struct rl_softc *, struct mbuf **, int *);
221a94100faSBill Paul 
222a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
223a94100faSBill Paul static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
224a94100faSBill Paul 				    bus_size_t, int);
225a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
226a94100faSBill Paul static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
227a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
228a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
22922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
23022a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
23122a11c96SJohn-Mark Gurney 				(struct mbuf *);
23222a11c96SJohn-Mark Gurney #endif
233ed510fb0SBill Paul static int re_rxeof		(struct rl_softc *);
234a94100faSBill Paul static void re_txeof		(struct rl_softc *);
23597b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2360187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2370187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
23897b9d4baSJohn-Mark Gurney #endif
239a94100faSBill Paul static void re_intr		(void *);
240a94100faSBill Paul static void re_tick		(void *);
241ed510fb0SBill Paul static void re_tx_task		(void *, int);
242ed510fb0SBill Paul static void re_int_task		(void *, int);
243a94100faSBill Paul static void re_start		(struct ifnet *);
244a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
245a94100faSBill Paul static void re_init		(void *);
24697b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
247a94100faSBill Paul static void re_stop		(struct rl_softc *);
248a94100faSBill Paul static void re_watchdog		(struct ifnet *);
249a94100faSBill Paul static int re_suspend		(device_t);
250a94100faSBill Paul static int re_resume		(device_t);
251a94100faSBill Paul static void re_shutdown		(device_t);
252a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
253a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
254a94100faSBill Paul 
255a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
256a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
257ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
258a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
259a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
260a94100faSBill Paul 
261a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
262a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
263a94100faSBill Paul static void re_miibus_statchg	(device_t);
264a94100faSBill Paul 
265a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
266a94100faSBill Paul static void re_reset		(struct rl_softc *);
267a94100faSBill Paul 
268ed510fb0SBill Paul #ifdef RE_DIAG
269a94100faSBill Paul static int re_diag		(struct rl_softc *);
270ed510fb0SBill Paul #endif
271a94100faSBill Paul 
272a94100faSBill Paul #ifdef RE_USEIOSPACE
273a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
274a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
275a94100faSBill Paul #else
276a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
277a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
278a94100faSBill Paul #endif
279a94100faSBill Paul 
280a94100faSBill Paul static device_method_t re_methods[] = {
281a94100faSBill Paul 	/* Device interface */
282a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
283a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
284a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
285a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
286a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
287a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
288a94100faSBill Paul 
289a94100faSBill Paul 	/* bus interface */
290a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
291a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
292a94100faSBill Paul 
293a94100faSBill Paul 	/* MII interface */
294a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
295a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
296a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
297a94100faSBill Paul 
298a94100faSBill Paul 	{ 0, 0 }
299a94100faSBill Paul };
300a94100faSBill Paul 
301a94100faSBill Paul static driver_t re_driver = {
302a94100faSBill Paul 	"re",
303a94100faSBill Paul 	re_methods,
304a94100faSBill Paul 	sizeof(struct rl_softc)
305a94100faSBill Paul };
306a94100faSBill Paul 
307a94100faSBill Paul static devclass_t re_devclass;
308a94100faSBill Paul 
309a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
310347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
311a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
312a94100faSBill Paul 
313a94100faSBill Paul #define EE_SET(x)					\
314a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
315a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
316a94100faSBill Paul 
317a94100faSBill Paul #define EE_CLR(x)					\
318a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
319a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
320a94100faSBill Paul 
321a94100faSBill Paul /*
322a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
323a94100faSBill Paul  */
324a94100faSBill Paul static void
325a94100faSBill Paul re_eeprom_putbyte(sc, addr)
326a94100faSBill Paul 	struct rl_softc		*sc;
327a94100faSBill Paul 	int			addr;
328a94100faSBill Paul {
329a94100faSBill Paul 	register int		d, i;
330a94100faSBill Paul 
331ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
332a94100faSBill Paul 
333a94100faSBill Paul 	/*
334a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
335a94100faSBill Paul 	 */
336ed510fb0SBill Paul 
337ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
338a94100faSBill Paul 		if (d & i) {
339a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
340a94100faSBill Paul 		} else {
341a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
342a94100faSBill Paul 		}
343a94100faSBill Paul 		DELAY(100);
344a94100faSBill Paul 		EE_SET(RL_EE_CLK);
345a94100faSBill Paul 		DELAY(150);
346a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
347a94100faSBill Paul 		DELAY(100);
348a94100faSBill Paul 	}
349ed510fb0SBill Paul 
350ed510fb0SBill Paul 	return;
351a94100faSBill Paul }
352a94100faSBill Paul 
353a94100faSBill Paul /*
354a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
355a94100faSBill Paul  */
356a94100faSBill Paul static void
357a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
358a94100faSBill Paul 	struct rl_softc		*sc;
359a94100faSBill Paul 	int			addr;
360a94100faSBill Paul 	u_int16_t		*dest;
361a94100faSBill Paul {
362a94100faSBill Paul 	register int		i;
363a94100faSBill Paul 	u_int16_t		word = 0;
364a94100faSBill Paul 
365a94100faSBill Paul 	/*
366a94100faSBill Paul 	 * Send address of word we want to read.
367a94100faSBill Paul 	 */
368a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
369a94100faSBill Paul 
370a94100faSBill Paul 	/*
371a94100faSBill Paul 	 * Start reading bits from EEPROM.
372a94100faSBill Paul 	 */
373a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
374a94100faSBill Paul 		EE_SET(RL_EE_CLK);
375a94100faSBill Paul 		DELAY(100);
376a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
377a94100faSBill Paul 			word |= i;
378a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
379a94100faSBill Paul 		DELAY(100);
380a94100faSBill Paul 	}
381a94100faSBill Paul 
382a94100faSBill Paul 	*dest = word;
383ed510fb0SBill Paul 
384ed510fb0SBill Paul 	return;
385a94100faSBill Paul }
386a94100faSBill Paul 
387a94100faSBill Paul /*
388a94100faSBill Paul  * Read a sequence of words from the EEPROM.
389a94100faSBill Paul  */
390a94100faSBill Paul static void
391ed510fb0SBill Paul re_read_eeprom(sc, dest, off, cnt)
392a94100faSBill Paul 	struct rl_softc		*sc;
393a94100faSBill Paul 	caddr_t			dest;
394a94100faSBill Paul 	int			off;
395a94100faSBill Paul 	int			cnt;
396a94100faSBill Paul {
397a94100faSBill Paul 	int			i;
398a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
399a94100faSBill Paul 
400ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
401ed510fb0SBill Paul 
402ed510fb0SBill Paul         DELAY(100);
403ed510fb0SBill Paul 
404a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
405ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
406a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
407ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
408a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
409ed510fb0SBill Paul                 *ptr = le16toh(word);
410a94100faSBill Paul 	}
411ed510fb0SBill Paul 
412ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
413ed510fb0SBill Paul 
414ed510fb0SBill Paul 	return;
415a94100faSBill Paul }
416a94100faSBill Paul 
417a94100faSBill Paul static int
418a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
419a94100faSBill Paul 	device_t		dev;
420a94100faSBill Paul 	int			phy, reg;
421a94100faSBill Paul {
422a94100faSBill Paul 	struct rl_softc		*sc;
423a94100faSBill Paul 	u_int32_t		rval;
424a94100faSBill Paul 	int			i;
425a94100faSBill Paul 
426a94100faSBill Paul 	if (phy != 1)
427a94100faSBill Paul 		return (0);
428a94100faSBill Paul 
429a94100faSBill Paul 	sc = device_get_softc(dev);
430a94100faSBill Paul 
4319bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4329bac70b8SBill Paul 
4339bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4349bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4359bac70b8SBill Paul 		return (rval);
4369bac70b8SBill Paul 	}
4379bac70b8SBill Paul 
438a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
439a94100faSBill Paul 	DELAY(1000);
440a94100faSBill Paul 
441a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
442a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
443a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
444a94100faSBill Paul 			break;
445a94100faSBill Paul 		DELAY(100);
446a94100faSBill Paul 	}
447a94100faSBill Paul 
448a94100faSBill Paul 	if (i == RL_TIMEOUT) {
449d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "PHY read failed\n");
450a94100faSBill Paul 		return (0);
451a94100faSBill Paul 	}
452a94100faSBill Paul 
453a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
454a94100faSBill Paul }
455a94100faSBill Paul 
456a94100faSBill Paul static int
457a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
458a94100faSBill Paul 	device_t		dev;
459a94100faSBill Paul 	int			phy, reg, data;
460a94100faSBill Paul {
461a94100faSBill Paul 	struct rl_softc		*sc;
462a94100faSBill Paul 	u_int32_t		rval;
463a94100faSBill Paul 	int			i;
464a94100faSBill Paul 
465a94100faSBill Paul 	sc = device_get_softc(dev);
466a94100faSBill Paul 
467a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4689bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
469a94100faSBill Paul 	DELAY(1000);
470a94100faSBill Paul 
471a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
472a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
473a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
474a94100faSBill Paul 			break;
475a94100faSBill Paul 		DELAY(100);
476a94100faSBill Paul 	}
477a94100faSBill Paul 
478a94100faSBill Paul 	if (i == RL_TIMEOUT) {
479d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "PHY write failed\n");
480a94100faSBill Paul 		return (0);
481a94100faSBill Paul 	}
482a94100faSBill Paul 
483a94100faSBill Paul 	return (0);
484a94100faSBill Paul }
485a94100faSBill Paul 
486a94100faSBill Paul static int
487a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
488a94100faSBill Paul 	device_t		dev;
489a94100faSBill Paul 	int			phy, reg;
490a94100faSBill Paul {
491a94100faSBill Paul 	struct rl_softc		*sc;
492a94100faSBill Paul 	u_int16_t		rval = 0;
493a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
494a94100faSBill Paul 
495a94100faSBill Paul 	sc = device_get_softc(dev);
496a94100faSBill Paul 
497a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
498a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
499a94100faSBill Paul 		return (rval);
500a94100faSBill Paul 	}
501a94100faSBill Paul 
502a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
503a94100faSBill Paul 	if (phy) {
504a94100faSBill Paul 		return (0);
505a94100faSBill Paul 	}
506a94100faSBill Paul 	switch (reg) {
507a94100faSBill Paul 	case MII_BMCR:
508a94100faSBill Paul 		re8139_reg = RL_BMCR;
509a94100faSBill Paul 		break;
510a94100faSBill Paul 	case MII_BMSR:
511a94100faSBill Paul 		re8139_reg = RL_BMSR;
512a94100faSBill Paul 		break;
513a94100faSBill Paul 	case MII_ANAR:
514a94100faSBill Paul 		re8139_reg = RL_ANAR;
515a94100faSBill Paul 		break;
516a94100faSBill Paul 	case MII_ANER:
517a94100faSBill Paul 		re8139_reg = RL_ANER;
518a94100faSBill Paul 		break;
519a94100faSBill Paul 	case MII_ANLPAR:
520a94100faSBill Paul 		re8139_reg = RL_LPAR;
521a94100faSBill Paul 		break;
522a94100faSBill Paul 	case MII_PHYIDR1:
523a94100faSBill Paul 	case MII_PHYIDR2:
524a94100faSBill Paul 		return (0);
525a94100faSBill Paul 	/*
526a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
527a94100faSBill Paul 	 * register. If we have a link partner which does not
528a94100faSBill Paul 	 * support NWAY, this is the register which will tell
529a94100faSBill Paul 	 * us the results of parallel detection.
530a94100faSBill Paul 	 */
531a94100faSBill Paul 	case RL_MEDIASTAT:
532a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
533a94100faSBill Paul 		return (rval);
534a94100faSBill Paul 	default:
535d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "bad phy register\n");
536a94100faSBill Paul 		return (0);
537a94100faSBill Paul 	}
538a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
539a94100faSBill Paul 	return (rval);
540a94100faSBill Paul }
541a94100faSBill Paul 
542a94100faSBill Paul static int
543a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
544a94100faSBill Paul 	device_t		dev;
545a94100faSBill Paul 	int			phy, reg, data;
546a94100faSBill Paul {
547a94100faSBill Paul 	struct rl_softc		*sc;
548a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
549a94100faSBill Paul 	int			rval = 0;
550a94100faSBill Paul 
551a94100faSBill Paul 	sc = device_get_softc(dev);
552a94100faSBill Paul 
553a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
554a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
555a94100faSBill Paul 		return (rval);
556a94100faSBill Paul 	}
557a94100faSBill Paul 
558a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
55997b9d4baSJohn-Mark Gurney 	if (phy)
560a94100faSBill Paul 		return (0);
56197b9d4baSJohn-Mark Gurney 
562a94100faSBill Paul 	switch (reg) {
563a94100faSBill Paul 	case MII_BMCR:
564a94100faSBill Paul 		re8139_reg = RL_BMCR;
565a94100faSBill Paul 		break;
566a94100faSBill Paul 	case MII_BMSR:
567a94100faSBill Paul 		re8139_reg = RL_BMSR;
568a94100faSBill Paul 		break;
569a94100faSBill Paul 	case MII_ANAR:
570a94100faSBill Paul 		re8139_reg = RL_ANAR;
571a94100faSBill Paul 		break;
572a94100faSBill Paul 	case MII_ANER:
573a94100faSBill Paul 		re8139_reg = RL_ANER;
574a94100faSBill Paul 		break;
575a94100faSBill Paul 	case MII_ANLPAR:
576a94100faSBill Paul 		re8139_reg = RL_LPAR;
577a94100faSBill Paul 		break;
578a94100faSBill Paul 	case MII_PHYIDR1:
579a94100faSBill Paul 	case MII_PHYIDR2:
580a94100faSBill Paul 		return (0);
581a94100faSBill Paul 		break;
582a94100faSBill Paul 	default:
583d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "bad phy register\n");
584a94100faSBill Paul 		return (0);
585a94100faSBill Paul 	}
586a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
587a94100faSBill Paul 	return (0);
588a94100faSBill Paul }
589a94100faSBill Paul 
590a94100faSBill Paul static void
591a94100faSBill Paul re_miibus_statchg(dev)
592a94100faSBill Paul 	device_t		dev;
593a94100faSBill Paul {
594a11e2f18SBruce M Simpson 
595a94100faSBill Paul }
596a94100faSBill Paul 
597a94100faSBill Paul /*
598a94100faSBill Paul  * Program the 64-bit multicast hash filter.
599a94100faSBill Paul  */
600a94100faSBill Paul static void
601a94100faSBill Paul re_setmulti(sc)
602a94100faSBill Paul 	struct rl_softc		*sc;
603a94100faSBill Paul {
604a94100faSBill Paul 	struct ifnet		*ifp;
605a94100faSBill Paul 	int			h = 0;
606a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
607a94100faSBill Paul 	struct ifmultiaddr	*ifma;
608a94100faSBill Paul 	u_int32_t		rxfilt;
609a94100faSBill Paul 	int			mcnt = 0;
610a94100faSBill Paul 
61197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
61297b9d4baSJohn-Mark Gurney 
613fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
614a94100faSBill Paul 
615a94100faSBill Paul 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
616a94100faSBill Paul 
617a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
618a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
619a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
620a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
621a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
622a94100faSBill Paul 		return;
623a94100faSBill Paul 	}
624a94100faSBill Paul 
625a94100faSBill Paul 	/* first, zot all the existing hash bits */
626a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
627a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
628a94100faSBill Paul 
629a94100faSBill Paul 	/* now program new ones */
63013b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
631a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
632a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
633a94100faSBill Paul 			continue;
6340e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6350e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
636a94100faSBill Paul 		if (h < 32)
637a94100faSBill Paul 			hashes[0] |= (1 << h);
638a94100faSBill Paul 		else
639a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
640a94100faSBill Paul 		mcnt++;
641a94100faSBill Paul 	}
64213b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
643a94100faSBill Paul 
644a94100faSBill Paul 	if (mcnt)
645a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
646a94100faSBill Paul 	else
647a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
648a94100faSBill Paul 
649a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
650a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
651a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
652a94100faSBill Paul }
653a94100faSBill Paul 
654a94100faSBill Paul static void
655a94100faSBill Paul re_reset(sc)
656a94100faSBill Paul 	struct rl_softc		*sc;
657a94100faSBill Paul {
658a94100faSBill Paul 	register int		i;
659a94100faSBill Paul 
66097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
66197b9d4baSJohn-Mark Gurney 
662a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
663a94100faSBill Paul 
664a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
665a94100faSBill Paul 		DELAY(10);
666a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
667a94100faSBill Paul 			break;
668a94100faSBill Paul 	}
669a94100faSBill Paul 	if (i == RL_TIMEOUT)
670d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "reset never completed!\n");
671a94100faSBill Paul 
672a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
673a94100faSBill Paul }
674a94100faSBill Paul 
675ed510fb0SBill Paul #ifdef RE_DIAG
676ed510fb0SBill Paul 
677a94100faSBill Paul /*
678a94100faSBill Paul  * The following routine is designed to test for a defect on some
679a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
680a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
681a94100faSBill Paul  * should be pulled high. The result of this defect is that the
682a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
683a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
684a94100faSBill Paul  * because the 64-bit data lines aren't connected.
685a94100faSBill Paul  *
686a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
687a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
688a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
689a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
690a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
691a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
692a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
693a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
694a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
695a94100faSBill Paul  */
696a94100faSBill Paul 
697a94100faSBill Paul static int
698a94100faSBill Paul re_diag(sc)
699a94100faSBill Paul 	struct rl_softc		*sc;
700a94100faSBill Paul {
701fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
702a94100faSBill Paul 	struct mbuf		*m0;
703a94100faSBill Paul 	struct ether_header	*eh;
704a94100faSBill Paul 	struct rl_desc		*cur_rx;
705a94100faSBill Paul 	u_int16_t		status;
706a94100faSBill Paul 	u_int32_t		rxstat;
707ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
708a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
709a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
710a94100faSBill Paul 
711a94100faSBill Paul 	/* Allocate a single mbuf */
712a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
713a94100faSBill Paul 	if (m0 == NULL)
714a94100faSBill Paul 		return (ENOBUFS);
715a94100faSBill Paul 
71697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
71797b9d4baSJohn-Mark Gurney 
718a94100faSBill Paul 	/*
719a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
720a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
721a94100faSBill Paul 	 * following special functions:
722a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
723a94100faSBill Paul 	 * - Enables digital loopback mode
724a94100faSBill Paul 	 * - Leaves interrupts turned off
725a94100faSBill Paul 	 */
726a94100faSBill Paul 
727a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
728a94100faSBill Paul 	sc->rl_testmode = 1;
729ed510fb0SBill Paul 	re_reset(sc);
73097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
731ed510fb0SBill Paul 	sc->rl_link = 1;
732ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
733ed510fb0SBill Paul 		phyaddr = 1;
734ed510fb0SBill Paul 	else
735ed510fb0SBill Paul 		phyaddr = 0;
736ed510fb0SBill Paul 
737ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
738ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
739ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
740ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
741ed510fb0SBill Paul 			break;
742ed510fb0SBill Paul 	}
743ed510fb0SBill Paul 
744ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
745ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
746ed510fb0SBill Paul 
747804af9a1SBill Paul 	DELAY(100000);
748a94100faSBill Paul 
749a94100faSBill Paul 	/* Put some data in the mbuf */
750a94100faSBill Paul 
751a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
752a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
753a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
754a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
755a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
756a94100faSBill Paul 
7577cae6651SBill Paul 	/*
7587cae6651SBill Paul 	 * Queue the packet, start transmission.
7597cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7607cae6651SBill Paul 	 */
761a94100faSBill Paul 
762abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
76397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
76452732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7657cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
76697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
767a94100faSBill Paul 	m0 = NULL;
768a94100faSBill Paul 
769a94100faSBill Paul 	/* Wait for it to propagate through the chip */
770a94100faSBill Paul 
771abc8ff44SBill Paul 	DELAY(100000);
772a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
773a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
774ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
775abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
776abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
777a94100faSBill Paul 			break;
778a94100faSBill Paul 		DELAY(10);
779a94100faSBill Paul 	}
780a94100faSBill Paul 
781a94100faSBill Paul 	if (i == RL_TIMEOUT) {
782d1754a9bSJohn Baldwin 		if_printf(ifp, "diagnostic failed, failed to receive packet "
783d1754a9bSJohn Baldwin 		    "in loopback mode\n");
784a94100faSBill Paul 		error = EIO;
785a94100faSBill Paul 		goto done;
786a94100faSBill Paul 	}
787a94100faSBill Paul 
788a94100faSBill Paul 	/*
789a94100faSBill Paul 	 * The packet should have been dumped into the first
790a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
791a94100faSBill Paul 	 */
792a94100faSBill Paul 
793a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
794a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
795a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
796a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
797a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0],
798a94100faSBill Paul 	    BUS_DMASYNC_POSTWRITE);
799a94100faSBill Paul 	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
800a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0]);
801a94100faSBill Paul 
802a94100faSBill Paul 	m0 = sc->rl_ldata.rl_rx_mbuf[0];
803a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
804a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
805a94100faSBill Paul 
806a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
807a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
808a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
809a94100faSBill Paul 
810a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
811d1754a9bSJohn Baldwin 		if_printf(ifp, "diagnostic failed, received short packet\n");
812a94100faSBill Paul 		error = EIO;
813a94100faSBill Paul 		goto done;
814a94100faSBill Paul 	}
815a94100faSBill Paul 
816a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
817a94100faSBill Paul 
818a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
819a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
820a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
821d1754a9bSJohn Baldwin 		if_printf(ifp, "WARNING, DMA FAILURE!\n");
822d1754a9bSJohn Baldwin 		if_printf(ifp, "expected TX data: %6D/%6D/0x%x\n",
823a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
824d1754a9bSJohn Baldwin 		if_printf(ifp, "received RX data: %6D/%6D/0x%x\n",
825a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
826a94100faSBill Paul 		    ntohs(eh->ether_type));
827d1754a9bSJohn Baldwin 		if_printf(ifp, "You may have a defective 32-bit NIC plugged "
828d1754a9bSJohn Baldwin 		    "into a 64-bit PCI slot.\n");
829d1754a9bSJohn Baldwin 		if_printf(ifp, "Please re-install the NIC in a 32-bit slot "
830d1754a9bSJohn Baldwin 		    "for proper operation.\n");
831d1754a9bSJohn Baldwin 		if_printf(ifp, "Read the re(4) man page for more details.\n");
832a94100faSBill Paul 		error = EIO;
833a94100faSBill Paul 	}
834a94100faSBill Paul 
835a94100faSBill Paul done:
836a94100faSBill Paul 	/* Turn interface off, release resources */
837a94100faSBill Paul 
838a94100faSBill Paul 	sc->rl_testmode = 0;
839ed510fb0SBill Paul 	sc->rl_link = 0;
840a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
841a94100faSBill Paul 	re_stop(sc);
842a94100faSBill Paul 	if (m0 != NULL)
843a94100faSBill Paul 		m_freem(m0);
844a94100faSBill Paul 
84597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
84697b9d4baSJohn-Mark Gurney 
847a94100faSBill Paul 	return (error);
848a94100faSBill Paul }
849a94100faSBill Paul 
850ed510fb0SBill Paul #endif
851ed510fb0SBill Paul 
852a94100faSBill Paul /*
853a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
854a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
855a94100faSBill Paul  */
856a94100faSBill Paul static int
857a94100faSBill Paul re_probe(dev)
858a94100faSBill Paul 	device_t		dev;
859a94100faSBill Paul {
860a94100faSBill Paul 	struct rl_type		*t;
861a94100faSBill Paul 	struct rl_softc		*sc;
862a94100faSBill Paul 	int			rid;
863a94100faSBill Paul 	u_int32_t		hwrev;
864a94100faSBill Paul 
865a94100faSBill Paul 	t = re_devs;
866a94100faSBill Paul 	sc = device_get_softc(dev);
867a94100faSBill Paul 
868a94100faSBill Paul 	while (t->rl_name != NULL) {
869a94100faSBill Paul 		if ((pci_get_vendor(dev) == t->rl_vid) &&
870a94100faSBill Paul 		    (pci_get_device(dev) == t->rl_did)) {
87126390635SJohn Baldwin 			/*
87226390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
87326390635SJohn Baldwin 			 * Rev. 2 i supported by sk(4).
87426390635SJohn Baldwin 			 */
87526390635SJohn Baldwin 			if ((t->rl_vid == LINKSYS_VENDORID) &&
87626390635SJohn Baldwin 				(t->rl_did == LINKSYS_DEVICEID_EG1032) &&
87726390635SJohn Baldwin 				(pci_get_subdevice(dev) !=
87826390635SJohn Baldwin 				LINKSYS_SUBDEVICE_EG1032_REV3)) {
87926390635SJohn Baldwin 				t++;
88026390635SJohn Baldwin 				continue;
88126390635SJohn Baldwin 			}
882a94100faSBill Paul 
883a94100faSBill Paul 			/*
884a94100faSBill Paul 			 * Temporarily map the I/O space
885a94100faSBill Paul 			 * so we can read the chip ID register.
886a94100faSBill Paul 			 */
887a94100faSBill Paul 			rid = RL_RID;
8885f96beb9SNate Lawson 			sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
8895f96beb9SNate Lawson 			    RF_ACTIVE);
890a94100faSBill Paul 			if (sc->rl_res == NULL) {
891a94100faSBill Paul 				device_printf(dev,
892a94100faSBill Paul 				    "couldn't map ports/memory\n");
893a94100faSBill Paul 				return (ENXIO);
894a94100faSBill Paul 			}
895a94100faSBill Paul 			sc->rl_btag = rman_get_bustag(sc->rl_res);
896a94100faSBill Paul 			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
897a94100faSBill Paul 			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
898a94100faSBill Paul 			bus_release_resource(dev, RL_RES,
899a94100faSBill Paul 			    RL_RID, sc->rl_res);
900a94100faSBill Paul 			if (t->rl_basetype == hwrev) {
901a94100faSBill Paul 				device_set_desc(dev, t->rl_name);
902d2b677bbSWarner Losh 				return (BUS_PROBE_DEFAULT);
903a94100faSBill Paul 			}
904a94100faSBill Paul 		}
905a94100faSBill Paul 		t++;
906a94100faSBill Paul 	}
907a94100faSBill Paul 
908a94100faSBill Paul 	return (ENXIO);
909a94100faSBill Paul }
910a94100faSBill Paul 
911a94100faSBill Paul /*
912a94100faSBill Paul  * This routine takes the segment list provided as the result of
913a94100faSBill Paul  * a bus_dma_map_load() operation and assigns the addresses/lengths
914a94100faSBill Paul  * to RealTek DMA descriptors. This can be called either by the RX
915a94100faSBill Paul  * code or the TX code. In the RX case, we'll probably wind up mapping
916a94100faSBill Paul  * at most one segment. For the TX case, there could be any number of
917a94100faSBill Paul  * segments since TX packets may span multiple mbufs. In either case,
918a94100faSBill Paul  * if the number of segments is larger than the rl_maxsegs limit
919a94100faSBill Paul  * specified by the caller, we abort the mapping operation. Sadly,
920a94100faSBill Paul  * whoever designed the buffer mapping API did not provide a way to
921a94100faSBill Paul  * return an error from here, so we have to fake it a bit.
922a94100faSBill Paul  */
923a94100faSBill Paul 
924a94100faSBill Paul static void
925a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error)
926a94100faSBill Paul 	void			*arg;
927a94100faSBill Paul 	bus_dma_segment_t	*segs;
928a94100faSBill Paul 	int			nseg;
929a94100faSBill Paul 	bus_size_t		mapsize;
930a94100faSBill Paul 	int			error;
931a94100faSBill Paul {
932a94100faSBill Paul 	struct rl_dmaload_arg	*ctx;
933a94100faSBill Paul 	struct rl_desc		*d = NULL;
934a94100faSBill Paul 	int			i = 0, idx;
935a94100faSBill Paul 
936a94100faSBill Paul 	if (error)
937a94100faSBill Paul 		return;
938a94100faSBill Paul 
939a94100faSBill Paul 	ctx = arg;
940a94100faSBill Paul 
941a94100faSBill Paul 	/* Signal error to caller if there's too many segments */
942a94100faSBill Paul 	if (nseg > ctx->rl_maxsegs) {
943a94100faSBill Paul 		ctx->rl_maxsegs = 0;
944a94100faSBill Paul 		return;
945a94100faSBill Paul 	}
946a94100faSBill Paul 
947a94100faSBill Paul 	/*
948a94100faSBill Paul 	 * Map the segment array into descriptors. Note that we set the
949a94100faSBill Paul 	 * start-of-frame and end-of-frame markers for either TX or RX, but
950a94100faSBill Paul 	 * they really only have meaning in the TX case. (In the RX case,
951a94100faSBill Paul 	 * it's the chip that tells us where packets begin and end.)
952a94100faSBill Paul 	 * We also keep track of the end of the ring and set the
953a94100faSBill Paul 	 * end-of-ring bits as needed, and we set the ownership bits
954a94100faSBill Paul 	 * in all except the very first descriptor. (The caller will
955a94100faSBill Paul 	 * set this descriptor later when it start transmission or
956a94100faSBill Paul 	 * reception.)
957a94100faSBill Paul 	 */
958a94100faSBill Paul 	idx = ctx->rl_idx;
95959b5d934SBruce M Simpson 	for (;;) {
960a94100faSBill Paul 		u_int32_t		cmdstat;
961a94100faSBill Paul 		d = &ctx->rl_ring[idx];
962a94100faSBill Paul 		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
963a94100faSBill Paul 			ctx->rl_maxsegs = 0;
964a94100faSBill Paul 			return;
965a94100faSBill Paul 		}
966a94100faSBill Paul 		cmdstat = segs[i].ds_len;
967a94100faSBill Paul 		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
968a94100faSBill Paul 		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
969a94100faSBill Paul 		if (i == 0)
970a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_SOF;
971a94100faSBill Paul 		else
972a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_OWN;
973a94100faSBill Paul 		if (idx == (RL_RX_DESC_CNT - 1))
974a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_EOR;
975a94100faSBill Paul 		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
976a94100faSBill Paul 		i++;
977a94100faSBill Paul 		if (i == nseg)
978a94100faSBill Paul 			break;
979a94100faSBill Paul 		RL_DESC_INC(idx);
980a94100faSBill Paul 	}
981a94100faSBill Paul 
982a94100faSBill Paul 	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
983a94100faSBill Paul 	ctx->rl_maxsegs = nseg;
984a94100faSBill Paul 	ctx->rl_idx = idx;
985a94100faSBill Paul }
986a94100faSBill Paul 
987a94100faSBill Paul /*
988a94100faSBill Paul  * Map a single buffer address.
989a94100faSBill Paul  */
990a94100faSBill Paul 
991a94100faSBill Paul static void
992a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
993a94100faSBill Paul 	void			*arg;
994a94100faSBill Paul 	bus_dma_segment_t	*segs;
995a94100faSBill Paul 	int			nseg;
996a94100faSBill Paul 	int			error;
997a94100faSBill Paul {
9988fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
999a94100faSBill Paul 
1000a94100faSBill Paul 	if (error)
1001a94100faSBill Paul 		return;
1002a94100faSBill Paul 
1003a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
1004a94100faSBill Paul 	addr = arg;
1005a94100faSBill Paul 	*addr = segs->ds_addr;
1006a94100faSBill Paul }
1007a94100faSBill Paul 
1008a94100faSBill Paul static int
1009a94100faSBill Paul re_allocmem(dev, sc)
1010a94100faSBill Paul 	device_t		dev;
1011a94100faSBill Paul 	struct rl_softc		*sc;
1012a94100faSBill Paul {
1013a94100faSBill Paul 	int			error;
1014a94100faSBill Paul 	int			nseg;
1015a94100faSBill Paul 	int			i;
1016a94100faSBill Paul 
1017a94100faSBill Paul 	/*
1018a94100faSBill Paul 	 * Allocate map for RX mbufs.
1019a94100faSBill Paul 	 */
1020a94100faSBill Paul 	nseg = 32;
1021a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
1022a94100faSBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
10236110675fSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
1024a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_mtag);
1025a94100faSBill Paul 	if (error) {
1026a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1027a94100faSBill Paul 		return (ENOMEM);
1028a94100faSBill Paul 	}
1029a94100faSBill Paul 
1030a94100faSBill Paul 	/*
1031a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1032a94100faSBill Paul 	 */
1033a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1034a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1035a94100faSBill Paul 	    NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
1036a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1037a94100faSBill Paul 	if (error) {
1038a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1039a94100faSBill Paul 		return (ENOMEM);
1040a94100faSBill Paul 	}
1041a94100faSBill Paul 
1042a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1043a94100faSBill Paul 
1044a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1045a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1046a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1047a94100faSBill Paul 	if (error)
1048a94100faSBill Paul 		return (ENOMEM);
1049a94100faSBill Paul 
1050a94100faSBill Paul 	/* Load the map for the TX ring. */
1051a94100faSBill Paul 
1052a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1053a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1054a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
1055a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1056a94100faSBill Paul 
1057a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1058a94100faSBill Paul 
1059a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
1060a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1061a94100faSBill Paul 			    &sc->rl_ldata.rl_tx_dmamap[i]);
1062a94100faSBill Paul 		if (error) {
1063a94100faSBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
1064a94100faSBill Paul 			return (ENOMEM);
1065a94100faSBill Paul 		}
1066a94100faSBill Paul 	}
1067a94100faSBill Paul 
1068a94100faSBill Paul 	/*
1069a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1070a94100faSBill Paul 	 */
1071a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1072a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
107361021536SJohn-Mark Gurney 	    NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, BUS_DMA_ALLOCNOW,
1074a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1075a94100faSBill Paul 	if (error) {
1076a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1077a94100faSBill Paul 		return (ENOMEM);
1078a94100faSBill Paul 	}
1079a94100faSBill Paul 
1080a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1081a94100faSBill Paul 
1082a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1083a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1084a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1085a94100faSBill Paul 	if (error)
1086a94100faSBill Paul 		return (ENOMEM);
1087a94100faSBill Paul 
1088a94100faSBill Paul 	/* Load the map for the RX ring. */
1089a94100faSBill Paul 
1090a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1091a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
109261021536SJohn-Mark Gurney 	     RL_RX_LIST_SZ, re_dma_map_addr,
1093a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1094a94100faSBill Paul 
1095a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1096a94100faSBill Paul 
1097a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1098a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1099a94100faSBill Paul 			    &sc->rl_ldata.rl_rx_dmamap[i]);
1100a94100faSBill Paul 		if (error) {
1101a94100faSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1102a94100faSBill Paul 			return (ENOMEM);
1103a94100faSBill Paul 		}
1104a94100faSBill Paul 	}
1105a94100faSBill Paul 
1106a94100faSBill Paul 	return (0);
1107a94100faSBill Paul }
1108a94100faSBill Paul 
1109a94100faSBill Paul /*
1110a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1111a94100faSBill Paul  * setup and ethernet/BPF attach.
1112a94100faSBill Paul  */
1113a94100faSBill Paul static int
1114a94100faSBill Paul re_attach(dev)
1115a94100faSBill Paul 	device_t		dev;
1116a94100faSBill Paul {
1117a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1118a94100faSBill Paul 	u_int16_t		as[3];
1119a94100faSBill Paul 	struct rl_softc		*sc;
1120a94100faSBill Paul 	struct ifnet		*ifp;
1121a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1122a94100faSBill Paul 	int			hwrev;
1123a94100faSBill Paul 	u_int16_t		re_did = 0;
1124d1754a9bSJohn Baldwin 	int			error = 0, rid, i;
1125a94100faSBill Paul 
1126a94100faSBill Paul 	sc = device_get_softc(dev);
1127ed510fb0SBill Paul 	sc->rl_dev = dev;
1128a94100faSBill Paul 
1129a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
113097b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1131ed510fb0SBill Paul 	mtx_init(&sc->rl_intlock, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1132ed510fb0SBill Paul 	    MTX_SPIN);
1133d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1134d1754a9bSJohn Baldwin 
1135a94100faSBill Paul 	/*
1136a94100faSBill Paul 	 * Map control/status registers.
1137a94100faSBill Paul 	 */
1138a94100faSBill Paul 	pci_enable_busmaster(dev);
1139a94100faSBill Paul 
1140a94100faSBill Paul 	rid = RL_RID;
11415f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
11425f96beb9SNate Lawson 	    RF_ACTIVE);
1143a94100faSBill Paul 
1144a94100faSBill Paul 	if (sc->rl_res == NULL) {
1145d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1146a94100faSBill Paul 		error = ENXIO;
1147a94100faSBill Paul 		goto fail;
1148a94100faSBill Paul 	}
1149a94100faSBill Paul 
1150a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1151a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1152a94100faSBill Paul 
1153a94100faSBill Paul 	/* Allocate interrupt */
1154a94100faSBill Paul 	rid = 0;
11555f96beb9SNate Lawson 	sc->rl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1156a94100faSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
1157a94100faSBill Paul 
1158a94100faSBill Paul 	if (sc->rl_irq == NULL) {
1159d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
1160a94100faSBill Paul 		error = ENXIO;
1161a94100faSBill Paul 		goto fail;
1162a94100faSBill Paul 	}
1163a94100faSBill Paul 
1164a94100faSBill Paul 	/* Reset the adapter. */
116597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1166a94100faSBill Paul 	re_reset(sc);
116797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1168abc8ff44SBill Paul 
1169abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1170abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1171abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1172abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1173abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1174abc8ff44SBill Paul 			break;
1175abc8ff44SBill Paul 		}
1176abc8ff44SBill Paul 		hw_rev++;
1177abc8ff44SBill Paul 	}
1178abc8ff44SBill Paul 
1179ed510fb0SBill Paul 	sc->rl_eewidth = 6;
1180ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1181a94100faSBill Paul 	if (re_did != 0x8129)
1182ed510fb0SBill Paul 	        sc->rl_eewidth = 8;
1183a94100faSBill Paul 
1184a94100faSBill Paul 	/*
1185a94100faSBill Paul 	 * Get station address from the EEPROM.
1186a94100faSBill Paul 	 */
1187ed510fb0SBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1188a94100faSBill Paul 	for (i = 0; i < 3; i++) {
1189a94100faSBill Paul 		eaddr[(i * 2) + 0] = as[i] & 0xff;
1190a94100faSBill Paul 		eaddr[(i * 2) + 1] = as[i] >> 8;
1191a94100faSBill Paul 	}
1192ed510fb0SBill Paul 
1193ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1194ed510fb0SBill Paul 		/* Set RX length mask */
1195ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1196ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1197ed510fb0SBill Paul 	} else {
1198ed510fb0SBill Paul 		/* Set RX length mask */
1199ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1200ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1201abc8ff44SBill Paul 	}
12029bac70b8SBill Paul 
1203a94100faSBill Paul 	/*
1204a94100faSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1205a94100faSBill Paul 	 */
1206a94100faSBill Paul #define RL_NSEG_NEW 32
1207a94100faSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1208a94100faSBill Paul 			1, 0,			/* alignment, boundary */
1209a94100faSBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1210a94100faSBill Paul 			BUS_SPACE_MAXADDR,	/* highaddr */
1211a94100faSBill Paul 			NULL, NULL,		/* filter, filterarg */
1212a94100faSBill Paul 			MAXBSIZE, RL_NSEG_NEW,	/* maxsize, nsegments */
1213a94100faSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1214a94100faSBill Paul 			BUS_DMA_ALLOCNOW,	/* flags */
1215a94100faSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1216a94100faSBill Paul 			&sc->rl_parent_tag);
1217a94100faSBill Paul 	if (error)
1218a94100faSBill Paul 		goto fail;
1219a94100faSBill Paul 
1220a94100faSBill Paul 	error = re_allocmem(dev, sc);
1221a94100faSBill Paul 
1222a94100faSBill Paul 	if (error)
1223a94100faSBill Paul 		goto fail;
1224a94100faSBill Paul 
1225cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1226cd036ec1SBrooks Davis 	if (ifp == NULL) {
1227d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1228cd036ec1SBrooks Davis 		error = ENOSPC;
1229cd036ec1SBrooks Davis 		goto fail;
1230cd036ec1SBrooks Davis 	}
1231cd036ec1SBrooks Davis 
1232a94100faSBill Paul 	/* Do MII setup */
1233a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1234a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1235d1754a9bSJohn Baldwin 		device_printf(dev, "MII without any phy!\n");
1236a94100faSBill Paul 		error = ENXIO;
1237a94100faSBill Paul 		goto fail;
1238a94100faSBill Paul 	}
1239a94100faSBill Paul 
1240a94100faSBill Paul 	ifp->if_softc = sc;
12419bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1242a94100faSBill Paul 	ifp->if_mtu = ETHERMTU;
1243a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1244a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1245a94100faSBill Paul 	ifp->if_capabilities = IFCAP_VLAN_MTU;
1246a94100faSBill Paul 	ifp->if_start = re_start;
1247ed510fb0SBill Paul 	ifp->if_hwassist = RE_CSUM_FEATURES;
1248a94100faSBill Paul 	ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING;
124940929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities & ~IFCAP_HWCSUM;
1250f4ab22c9SRuslan Ermilov #ifdef DEVICE_POLLING
1251f4ab22c9SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
1252f4ab22c9SRuslan Ermilov #endif
1253a94100faSBill Paul 	ifp->if_watchdog = re_watchdog;
1254a94100faSBill Paul 	ifp->if_init = re_init;
125552732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
125652732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
125752732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1258a94100faSBill Paul 
1259ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1260ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1261ed510fb0SBill Paul 
1262a94100faSBill Paul 	/*
1263a94100faSBill Paul 	 * Call MI attach routine.
1264a94100faSBill Paul 	 */
1265a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1266a94100faSBill Paul 
1267ed510fb0SBill Paul #ifdef RE_DIAG
1268ed510fb0SBill Paul 	/*
1269ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1270ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1271ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1272ed510fb0SBill Paul 	 */
1273a94100faSBill Paul 
1274ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1275ed510fb0SBill Paul 		error = re_diag(sc);
1276a94100faSBill Paul 		if (error) {
1277ed510fb0SBill Paul 			device_printf(dev,
1278ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1279a94100faSBill Paul 			ether_ifdetach(ifp);
1280a94100faSBill Paul 			goto fail;
1281a94100faSBill Paul 		}
1282ed510fb0SBill Paul 	}
1283ed510fb0SBill Paul #endif
1284a94100faSBill Paul 
1285a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1286ed510fb0SBill Paul 	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET | INTR_MPSAFE |
1287ed510fb0SBill Paul 	    INTR_FAST, re_intr, sc, &sc->rl_intrhand);
1288a94100faSBill Paul 	if (error) {
1289d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1290a94100faSBill Paul 		ether_ifdetach(ifp);
1291a94100faSBill Paul 	}
1292a94100faSBill Paul 
1293a94100faSBill Paul fail:
1294ed510fb0SBill Paul 
1295a94100faSBill Paul 	if (error)
1296a94100faSBill Paul 		re_detach(dev);
1297a94100faSBill Paul 
1298a94100faSBill Paul 	return (error);
1299a94100faSBill Paul }
1300a94100faSBill Paul 
1301a94100faSBill Paul /*
1302a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1303a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1304a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1305a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1306a94100faSBill Paul  * allocated.
1307a94100faSBill Paul  */
1308a94100faSBill Paul static int
1309a94100faSBill Paul re_detach(dev)
1310a94100faSBill Paul 	device_t		dev;
1311a94100faSBill Paul {
1312a94100faSBill Paul 	struct rl_softc		*sc;
1313a94100faSBill Paul 	struct ifnet		*ifp;
1314a94100faSBill Paul 	int			i;
1315a94100faSBill Paul 
1316a94100faSBill Paul 	sc = device_get_softc(dev);
1317fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1318aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
131997b9d4baSJohn-Mark Gurney 
132040929967SGleb Smirnoff #ifdef DEVICE_POLLING
132140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
132240929967SGleb Smirnoff 		ether_poll_deregister(ifp);
132340929967SGleb Smirnoff #endif
132497b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
1325525e6a87SRuslan Ermilov 	if (device_is_attached(dev)) {
132697b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
132797b9d4baSJohn-Mark Gurney #if 0
132897b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
132997b9d4baSJohn-Mark Gurney #endif
1330a94100faSBill Paul 		re_stop(sc);
1331525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1332d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
1333a94100faSBill Paul 		/*
1334a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1335a94100faSBill Paul 		 * still had a BPF descriptor attached to this
133697b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1337a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1338a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1339a94100faSBill Paul 		 * which will try to call re_init() again. This will
1340a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1341a94100faSBill Paul 		 * which will panic the system when the kernel tries
1342a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1343a94100faSBill Paul 		 * anymore.
1344a94100faSBill Paul 		 */
1345a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1346525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1347a94100faSBill Paul 	}
1348a94100faSBill Paul 	if (sc->rl_miibus)
1349a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1350a94100faSBill Paul 	bus_generic_detach(dev);
1351a94100faSBill Paul 
135297b9d4baSJohn-Mark Gurney 	/*
135397b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
135497b9d4baSJohn-Mark Gurney 	 * stopped here.
135597b9d4baSJohn-Mark Gurney 	 */
135697b9d4baSJohn-Mark Gurney 
1357a94100faSBill Paul 	if (sc->rl_intrhand)
1358a94100faSBill Paul 		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1359ad4f426eSWarner Losh 	if (ifp != NULL)
1360ad4f426eSWarner Losh 		if_free(ifp);
1361a94100faSBill Paul 	if (sc->rl_irq)
1362a94100faSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1363a94100faSBill Paul 	if (sc->rl_res)
1364a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1365a94100faSBill Paul 
1366a94100faSBill Paul 
1367a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1368a94100faSBill Paul 
1369a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1370a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1371a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1372a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1373a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1374a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1375a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1376a94100faSBill Paul 	}
1377a94100faSBill Paul 
1378a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1379a94100faSBill Paul 
1380a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1381a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1382a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1383a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1384a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1385a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1386a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1387a94100faSBill Paul 	}
1388a94100faSBill Paul 
1389a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1390a94100faSBill Paul 
1391a94100faSBill Paul 	if (sc->rl_ldata.rl_mtag) {
1392a94100faSBill Paul 		for (i = 0; i < RL_TX_DESC_CNT; i++)
1393a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1394a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
1395a94100faSBill Paul 		for (i = 0; i < RL_RX_DESC_CNT; i++)
1396a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1397a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
1398a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1399a94100faSBill Paul 	}
1400a94100faSBill Paul 
1401a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1402a94100faSBill Paul 
1403a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1404a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1405a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1406a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1407a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1408a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1409a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1410a94100faSBill Paul 	}
1411a94100faSBill Paul 
1412a94100faSBill Paul 	if (sc->rl_parent_tag)
1413a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1414a94100faSBill Paul 
1415a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1416ed510fb0SBill Paul 	mtx_destroy(&sc->rl_intlock);
1417a94100faSBill Paul 
1418a94100faSBill Paul 	return (0);
1419a94100faSBill Paul }
1420a94100faSBill Paul 
1421a94100faSBill Paul static int
1422a94100faSBill Paul re_newbuf(sc, idx, m)
1423a94100faSBill Paul 	struct rl_softc		*sc;
1424a94100faSBill Paul 	int			idx;
1425a94100faSBill Paul 	struct mbuf		*m;
1426a94100faSBill Paul {
1427a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1428a94100faSBill Paul 	struct mbuf		*n = NULL;
1429a94100faSBill Paul 	int			error;
1430a94100faSBill Paul 
1431a94100faSBill Paul 	if (m == NULL) {
1432a94100faSBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1433a94100faSBill Paul 		if (n == NULL)
1434a94100faSBill Paul 			return (ENOBUFS);
1435a94100faSBill Paul 		m = n;
1436a94100faSBill Paul 	} else
1437a94100faSBill Paul 		m->m_data = m->m_ext.ext_buf;
1438a94100faSBill Paul 
1439a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
144022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
144122a11c96SJohn-Mark Gurney 	/*
144222a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
144322a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
144422a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
144522a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
144622a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
144722a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
144822a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
144922a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
145022a11c96SJohn-Mark Gurney 	 */
145122a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
145222a11c96SJohn-Mark Gurney #endif
1453a94100faSBill Paul 	arg.sc = sc;
1454a94100faSBill Paul 	arg.rl_idx = idx;
1455a94100faSBill Paul 	arg.rl_maxsegs = 1;
1456a94100faSBill Paul 	arg.rl_flags = 0;
1457a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1458a94100faSBill Paul 
1459a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1460a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1461a94100faSBill Paul 	    &arg, BUS_DMA_NOWAIT);
1462a94100faSBill Paul 	if (error || arg.rl_maxsegs != 1) {
1463a94100faSBill Paul 		if (n != NULL)
1464a94100faSBill Paul 			m_freem(n);
1465a94100faSBill Paul 		return (ENOMEM);
1466a94100faSBill Paul 	}
1467a94100faSBill Paul 
1468a94100faSBill Paul 	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1469a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1470a94100faSBill Paul 
1471a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1472a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx],
1473a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1474a94100faSBill Paul 
1475a94100faSBill Paul 	return (0);
1476a94100faSBill Paul }
1477a94100faSBill Paul 
147822a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
147922a11c96SJohn-Mark Gurney static __inline void
148022a11c96SJohn-Mark Gurney re_fixup_rx(m)
148122a11c96SJohn-Mark Gurney 	struct mbuf		*m;
148222a11c96SJohn-Mark Gurney {
148322a11c96SJohn-Mark Gurney 	int                     i;
148422a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
148522a11c96SJohn-Mark Gurney 
148622a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
148722a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
148822a11c96SJohn-Mark Gurney 
148922a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
149022a11c96SJohn-Mark Gurney 		*dst++ = *src++;
149122a11c96SJohn-Mark Gurney 
149222a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
149322a11c96SJohn-Mark Gurney 
149422a11c96SJohn-Mark Gurney 	return;
149522a11c96SJohn-Mark Gurney }
149622a11c96SJohn-Mark Gurney #endif
149722a11c96SJohn-Mark Gurney 
1498a94100faSBill Paul static int
1499a94100faSBill Paul re_tx_list_init(sc)
1500a94100faSBill Paul 	struct rl_softc		*sc;
1501a94100faSBill Paul {
150297b9d4baSJohn-Mark Gurney 
150397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
150497b9d4baSJohn-Mark Gurney 
1505a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1506a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1507a94100faSBill Paul 	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1508a94100faSBill Paul 
1509a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1510a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1511a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1512a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1513a94100faSBill Paul 	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1514a94100faSBill Paul 
1515a94100faSBill Paul 	return (0);
1516a94100faSBill Paul }
1517a94100faSBill Paul 
1518a94100faSBill Paul static int
1519a94100faSBill Paul re_rx_list_init(sc)
1520a94100faSBill Paul 	struct rl_softc		*sc;
1521a94100faSBill Paul {
1522a94100faSBill Paul 	int			i;
1523a94100faSBill Paul 
1524a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1525a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1526a94100faSBill Paul 	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1527a94100faSBill Paul 
1528a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1529a94100faSBill Paul 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1530a94100faSBill Paul 			return (ENOBUFS);
1531a94100faSBill Paul 	}
1532a94100faSBill Paul 
1533a94100faSBill Paul 	/* Flush the RX descriptors */
1534a94100faSBill Paul 
1535a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1536a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1537a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1538a94100faSBill Paul 
1539a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1540a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1541a94100faSBill Paul 
1542a94100faSBill Paul 	return (0);
1543a94100faSBill Paul }
1544a94100faSBill Paul 
1545a94100faSBill Paul /*
1546a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1547a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1548a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1549a94100faSBill Paul  */
1550ed510fb0SBill Paul static int
1551a94100faSBill Paul re_rxeof(sc)
1552a94100faSBill Paul 	struct rl_softc		*sc;
1553a94100faSBill Paul {
1554a94100faSBill Paul 	struct mbuf		*m;
1555a94100faSBill Paul 	struct ifnet		*ifp;
1556a94100faSBill Paul 	int			i, total_len;
1557a94100faSBill Paul 	struct rl_desc		*cur_rx;
1558a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1559ed510fb0SBill Paul 	int			maxpkt = 16;
1560a94100faSBill Paul 
15615120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
15625120abbfSSam Leffler 
1563fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1564a94100faSBill Paul 	i = sc->rl_ldata.rl_rx_prodidx;
1565a94100faSBill Paul 
1566a94100faSBill Paul 	/* Invalidate the descriptor memory */
1567a94100faSBill Paul 
1568a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1569a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1570a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1571a94100faSBill Paul 
1572ed510fb0SBill Paul 	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i]) && maxpkt) {
1573a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1574a94100faSBill Paul 		m = sc->rl_ldata.rl_rx_mbuf[i];
1575a94100faSBill Paul 		total_len = RL_RXBYTES(cur_rx);
1576a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1577a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1578a94100faSBill Paul 
1579a94100faSBill Paul 		/* Invalidate the RX mbuf and unload its map */
1580a94100faSBill Paul 
1581a94100faSBill Paul 		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1582a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i],
1583a94100faSBill Paul 		    BUS_DMASYNC_POSTWRITE);
1584a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1585a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i]);
1586a94100faSBill Paul 
1587a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
158822a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1589a94100faSBill Paul 			if (sc->rl_head == NULL)
1590a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1591a94100faSBill Paul 			else {
1592a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1593a94100faSBill Paul 				sc->rl_tail->m_next = m;
1594a94100faSBill Paul 				sc->rl_tail = m;
1595a94100faSBill Paul 			}
1596a94100faSBill Paul 			re_newbuf(sc, i, NULL);
1597a94100faSBill Paul 			RL_DESC_INC(i);
1598a94100faSBill Paul 			continue;
1599a94100faSBill Paul 		}
1600a94100faSBill Paul 
1601a94100faSBill Paul 		/*
1602a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1603a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1604a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1605a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1606a94100faSBill Paul 		 * were already used, so to make room for the extra
1607a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1608a94100faSBill Paul 		 * error' bit and shifted the other status bits
1609a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1610a94100faSBill Paul 		 * still in the same places. We have already extracted
1611a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1612a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1613a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1614a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1615a94100faSBill Paul 		 * same format as that of the 8139C+.
1616a94100faSBill Paul 		 */
1617a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1618a94100faSBill Paul 			rxstat >>= 1;
1619a94100faSBill Paul 
162022a11c96SJohn-Mark Gurney 		/*
162122a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
162222a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
162322a11c96SJohn-Mark Gurney 		 */
162422a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
162522a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1626a94100faSBill Paul 			ifp->if_ierrors++;
1627a94100faSBill Paul 			/*
1628a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1629a94100faSBill Paul 			 * discard all the pieces.
1630a94100faSBill Paul 			 */
1631a94100faSBill Paul 			if (sc->rl_head != NULL) {
1632a94100faSBill Paul 				m_freem(sc->rl_head);
1633a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1634a94100faSBill Paul 			}
1635a94100faSBill Paul 			re_newbuf(sc, i, m);
1636a94100faSBill Paul 			RL_DESC_INC(i);
1637a94100faSBill Paul 			continue;
1638a94100faSBill Paul 		}
1639a94100faSBill Paul 
1640a94100faSBill Paul 		/*
1641a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1642a94100faSBill Paul 		 * reload the current one.
1643a94100faSBill Paul 		 */
1644a94100faSBill Paul 
1645a94100faSBill Paul 		if (re_newbuf(sc, i, NULL)) {
1646a94100faSBill Paul 			ifp->if_ierrors++;
1647a94100faSBill Paul 			if (sc->rl_head != NULL) {
1648a94100faSBill Paul 				m_freem(sc->rl_head);
1649a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1650a94100faSBill Paul 			}
1651a94100faSBill Paul 			re_newbuf(sc, i, m);
1652a94100faSBill Paul 			RL_DESC_INC(i);
1653a94100faSBill Paul 			continue;
1654a94100faSBill Paul 		}
1655a94100faSBill Paul 
1656a94100faSBill Paul 		RL_DESC_INC(i);
1657a94100faSBill Paul 
1658a94100faSBill Paul 		if (sc->rl_head != NULL) {
165922a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
166022a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
166122a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1662a94100faSBill Paul 			/*
1663a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1664a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1665a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1666a94100faSBill Paul 			 * care about anyway.
1667a94100faSBill Paul 			 */
1668a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1669a94100faSBill Paul 				sc->rl_tail->m_len -=
1670a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1671a94100faSBill Paul 				m_freem(m);
1672a94100faSBill Paul 			} else {
1673a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1674a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1675a94100faSBill Paul 				sc->rl_tail->m_next = m;
1676a94100faSBill Paul 			}
1677a94100faSBill Paul 			m = sc->rl_head;
1678a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1679a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1680a94100faSBill Paul 		} else
1681a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1682a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1683a94100faSBill Paul 
168422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
168522a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
168622a11c96SJohn-Mark Gurney #endif
1687a94100faSBill Paul 		ifp->if_ipackets++;
1688a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1689a94100faSBill Paul 
1690a94100faSBill Paul 		/* Do RX checksumming if enabled */
1691a94100faSBill Paul 
1692a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1693a94100faSBill Paul 
1694a94100faSBill Paul 			/* Check IP header checksum */
1695a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1696a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1697a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1698a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1699a94100faSBill Paul 
1700a94100faSBill Paul 			/* Check TCP/UDP checksum */
1701a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1702a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1703a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1704a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1705a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1706a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1707a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1708a94100faSBill Paul 			}
1709a94100faSBill Paul 		}
1710ed510fb0SBill Paul 		maxpkt--;
1711d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
1712a94100faSBill Paul 			VLAN_INPUT_TAG(ifp, m,
1713d147662cSGleb Smirnoff 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)));
1714d147662cSGleb Smirnoff 			if (m == NULL)
1715d147662cSGleb Smirnoff 				continue;
1716d147662cSGleb Smirnoff 		}
17175120abbfSSam Leffler 		RL_UNLOCK(sc);
1718a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
17195120abbfSSam Leffler 		RL_LOCK(sc);
1720a94100faSBill Paul 	}
1721a94100faSBill Paul 
1722a94100faSBill Paul 	/* Flush the RX DMA ring */
1723a94100faSBill Paul 
1724a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1725a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1726a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1727a94100faSBill Paul 
1728a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1729ed510fb0SBill Paul 
1730ed510fb0SBill Paul 	if (maxpkt)
1731ed510fb0SBill Paul 		return(EAGAIN);
1732ed510fb0SBill Paul 
1733ed510fb0SBill Paul 	return(0);
1734a94100faSBill Paul }
1735a94100faSBill Paul 
1736a94100faSBill Paul static void
1737a94100faSBill Paul re_txeof(sc)
1738a94100faSBill Paul 	struct rl_softc		*sc;
1739a94100faSBill Paul {
1740a94100faSBill Paul 	struct ifnet		*ifp;
1741a94100faSBill Paul 	u_int32_t		txstat;
1742a94100faSBill Paul 	int			idx;
1743a94100faSBill Paul 
1744fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1745a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_considx;
1746a94100faSBill Paul 
1747a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1748a94100faSBill Paul 
1749a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1750a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1751a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1752a94100faSBill Paul 
1753ed510fb0SBill Paul 	while (sc->rl_ldata.rl_tx_free < RL_TX_DESC_CNT) {
1754a94100faSBill Paul 
1755a94100faSBill Paul 		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1756a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_OWN)
1757a94100faSBill Paul 			break;
1758a94100faSBill Paul 
1759ed510fb0SBill Paul 		sc->rl_ldata.rl_tx_list[idx].rl_bufaddr_lo = 0;
1760ed510fb0SBill Paul 
1761a94100faSBill Paul 		/*
1762a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1763a94100faSBill Paul 		 * in a fragment chain, which also happens to
1764a94100faSBill Paul 		 * be the only place where the TX status bits
1765a94100faSBill Paul 		 * are valid.
1766a94100faSBill Paul 		 */
1767a94100faSBill Paul 
1768a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1769a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1770a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1771a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1772a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[idx]);
1773a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1774a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1775a94100faSBill Paul 				ifp->if_collisions++;
1776a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1777a94100faSBill Paul 				ifp->if_oerrors++;
1778a94100faSBill Paul 			else
1779a94100faSBill Paul 				ifp->if_opackets++;
1780a94100faSBill Paul 		}
1781a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1782a94100faSBill Paul 		RL_DESC_INC(idx);
1783a94100faSBill Paul 	}
1784a94100faSBill Paul 
1785a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1786a94100faSBill Paul 
1787ed510fb0SBill Paul 	if (sc->rl_ldata.rl_tx_free) {
1788a94100faSBill Paul 		sc->rl_ldata.rl_tx_considx = idx;
178913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1790a94100faSBill Paul 		ifp->if_timer = 0;
1791a94100faSBill Paul 	}
1792a94100faSBill Paul 
1793ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1794a94100faSBill Paul 	/*
1795a94100faSBill Paul 	 * If not all descriptors have been released reaped yet,
1796a94100faSBill Paul 	 * reload the timer so that we will eventually get another
1797a94100faSBill Paul 	 * interrupt that will cause us to re-enter this routine.
1798a94100faSBill Paul 	 * This is done in case the transmitter has gone idle.
1799a94100faSBill Paul 	 */
1800a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
1801a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1802ed510fb0SBill Paul #endif
1803a94100faSBill Paul }
1804a94100faSBill Paul 
1805a94100faSBill Paul static void
1806a94100faSBill Paul re_tick(xsc)
1807a94100faSBill Paul 	void			*xsc;
1808a94100faSBill Paul {
1809a94100faSBill Paul 	struct rl_softc		*sc;
1810d1754a9bSJohn Baldwin 	struct mii_data		*mii;
1811ed510fb0SBill Paul 	struct ifnet		*ifp;
1812a94100faSBill Paul 
1813a94100faSBill Paul 	sc = xsc;
1814ed510fb0SBill Paul 	ifp = sc->rl_ifp;
181597b9d4baSJohn-Mark Gurney 
181697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
181797b9d4baSJohn-Mark Gurney 
1818a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
1819a94100faSBill Paul 
1820a94100faSBill Paul 	mii_tick(mii);
1821ed510fb0SBill Paul 	if (sc->rl_link) {
1822ed510fb0SBill Paul 		if (!(mii->mii_media_status & IFM_ACTIVE))
1823ed510fb0SBill Paul 			sc->rl_link = 0;
1824ed510fb0SBill Paul 	} else {
1825ed510fb0SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1826ed510fb0SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1827ed510fb0SBill Paul 			sc->rl_link = 1;
1828ed510fb0SBill Paul 			if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1829ed510fb0SBill Paul 				taskqueue_enqueue_fast(taskqueue_fast,
1830ed510fb0SBill Paul 				    &sc->rl_txtask);
1831ed510fb0SBill Paul 		}
1832ed510fb0SBill Paul 	}
1833a94100faSBill Paul 
1834d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
1835a94100faSBill Paul }
1836a94100faSBill Paul 
1837a94100faSBill Paul #ifdef DEVICE_POLLING
1838a94100faSBill Paul static void
1839a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1840a94100faSBill Paul {
1841a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1842a94100faSBill Paul 
1843a94100faSBill Paul 	RL_LOCK(sc);
184440929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
184597b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, cmd, count);
184697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
184797b9d4baSJohn-Mark Gurney }
184897b9d4baSJohn-Mark Gurney 
184997b9d4baSJohn-Mark Gurney static void
185097b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
185197b9d4baSJohn-Mark Gurney {
185297b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
185397b9d4baSJohn-Mark Gurney 
185497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
185597b9d4baSJohn-Mark Gurney 
1856a94100faSBill Paul 	sc->rxcycles = count;
1857a94100faSBill Paul 	re_rxeof(sc);
1858a94100faSBill Paul 	re_txeof(sc);
1859a94100faSBill Paul 
186037652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1861ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
1862a94100faSBill Paul 
1863a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1864a94100faSBill Paul 		u_int16_t       status;
1865a94100faSBill Paul 
1866a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1867a94100faSBill Paul 		if (status == 0xffff)
186897b9d4baSJohn-Mark Gurney 			return;
1869a94100faSBill Paul 		if (status)
1870a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1871a94100faSBill Paul 
1872a94100faSBill Paul 		/*
1873a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
1874a94100faSBill Paul 		 */
1875a94100faSBill Paul 
1876a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1877a94100faSBill Paul 			re_reset(sc);
187897b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
1879a94100faSBill Paul 		}
1880a94100faSBill Paul 	}
1881a94100faSBill Paul }
1882a94100faSBill Paul #endif /* DEVICE_POLLING */
1883a94100faSBill Paul 
1884a94100faSBill Paul static void
1885a94100faSBill Paul re_intr(arg)
1886a94100faSBill Paul 	void			*arg;
1887a94100faSBill Paul {
1888a94100faSBill Paul 	struct rl_softc		*sc;
1889a94100faSBill Paul 	struct ifnet		*ifp;
1890ed510fb0SBill Paul 	uint16_t		status;
1891a94100faSBill Paul 
1892a94100faSBill Paul 	sc = arg;
1893ed510fb0SBill Paul 	ifp = sc->rl_ifp;
1894ed510fb0SBill Paul 
1895ed510fb0SBill Paul 	mtx_lock_spin(&sc->rl_intlock);
1896ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
1897ed510fb0SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0) {
1898ed510fb0SBill Paul 		mtx_unlock_spin(&sc->rl_intlock);
1899ed510fb0SBill Paul                 return;
1900ed510fb0SBill Paul 	}
1901ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
1902ed510fb0SBill Paul 	mtx_unlock_spin(&sc->rl_intlock);
1903ed510fb0SBill Paul 
1904ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
1905ed510fb0SBill Paul 
1906ed510fb0SBill Paul 	return;
1907ed510fb0SBill Paul }
1908ed510fb0SBill Paul 
1909ed510fb0SBill Paul static void
1910ed510fb0SBill Paul re_int_task(arg, npending)
1911ed510fb0SBill Paul 	void			*arg;
1912ed510fb0SBill Paul 	int			npending;
1913ed510fb0SBill Paul {
1914ed510fb0SBill Paul 	struct rl_softc		*sc;
1915ed510fb0SBill Paul 	struct ifnet		*ifp;
1916ed510fb0SBill Paul 	u_int16_t		status;
1917ed510fb0SBill Paul 	int			rval = 0;
1918ed510fb0SBill Paul 
1919ed510fb0SBill Paul 	sc = arg;
1920ed510fb0SBill Paul 	ifp = sc->rl_ifp;
1921a94100faSBill Paul 
1922a94100faSBill Paul 	RL_LOCK(sc);
192397b9d4baSJohn-Mark Gurney 
1924a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
1925a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
1926a94100faSBill Paul 
1927ed510fb0SBill Paul 	if (sc->suspended || !(ifp->if_flags & IFF_UP)) {
1928ed510fb0SBill Paul 		RL_UNLOCK(sc);
1929ed510fb0SBill Paul 		return;
1930ed510fb0SBill Paul 	}
1931a94100faSBill Paul 
1932ed510fb0SBill Paul #ifdef DEVICE_POLLING
1933ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
1934ed510fb0SBill Paul 		RL_UNLOCK(sc);
1935ed510fb0SBill Paul 		return;
1936ed510fb0SBill Paul 	}
1937ed510fb0SBill Paul #endif
1938a94100faSBill Paul 
1939ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
1940ed510fb0SBill Paul 		rval = re_rxeof(sc);
1941ed510fb0SBill Paul 
1942ed510fb0SBill Paul #ifdef RE_TX_MODERATION
1943ed510fb0SBill Paul 	if (status & (RL_ISR_TIMEOUT_EXPIRED|
1944ed510fb0SBill Paul #else
1945ed510fb0SBill Paul 	if (status & (RL_ISR_TX_OK|
1946ed510fb0SBill Paul #endif
1947ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
1948a94100faSBill Paul 		re_txeof(sc);
1949a94100faSBill Paul 
1950a94100faSBill Paul 	if (status & RL_ISR_SYSTEM_ERR) {
1951a94100faSBill Paul 		re_reset(sc);
195297b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
1953a94100faSBill Paul 	}
1954a94100faSBill Paul 
1955a94100faSBill Paul 	if (status & RL_ISR_LINKCHG) {
1956d1754a9bSJohn Baldwin 		callout_stop(&sc->rl_stat_callout);
1957d1754a9bSJohn Baldwin 		re_tick(sc);
1958a94100faSBill Paul 	}
1959a94100faSBill Paul 
196052732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1961ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
1962a94100faSBill Paul 
1963a94100faSBill Paul 	RL_UNLOCK(sc);
1964ed510fb0SBill Paul 
1965ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
1966ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
1967ed510fb0SBill Paul 		return;
1968ed510fb0SBill Paul 	}
1969ed510fb0SBill Paul 
1970ed510fb0SBill Paul 	mtx_lock_spin(&sc->rl_intlock);
1971ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
1972ed510fb0SBill Paul 	mtx_unlock_spin(&sc->rl_intlock);
1973ed510fb0SBill Paul 
1974ed510fb0SBill Paul 	return;
1975a94100faSBill Paul }
1976a94100faSBill Paul 
1977a94100faSBill Paul static int
1978a94100faSBill Paul re_encap(sc, m_head, idx)
1979a94100faSBill Paul 	struct rl_softc		*sc;
198080a2a305SJohn-Mark Gurney 	struct mbuf		**m_head;
1981a94100faSBill Paul 	int			*idx;
1982a94100faSBill Paul {
1983a94100faSBill Paul 	struct mbuf		*m_new = NULL;
1984a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1985a94100faSBill Paul 	bus_dmamap_t		map;
1986a94100faSBill Paul 	int			error;
1987a94100faSBill Paul 	struct m_tag		*mtag;
1988a94100faSBill Paul 
198997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
199097b9d4baSJohn-Mark Gurney 
19917cae6651SBill Paul 	if (sc->rl_ldata.rl_tx_free <= 4)
1992a94100faSBill Paul 		return (EFBIG);
1993a94100faSBill Paul 
1994a94100faSBill Paul 	/*
1995a94100faSBill Paul 	 * Set up checksum offload. Note: checksum offload bits must
1996a94100faSBill Paul 	 * appear in all descriptors of a multi-descriptor transmit
199722a11c96SJohn-Mark Gurney 	 * attempt. This is according to testing done with an 8169
199822a11c96SJohn-Mark Gurney 	 * chip. This is a requirement.
1999a94100faSBill Paul 	 */
2000a94100faSBill Paul 
2001a94100faSBill Paul 	arg.rl_flags = 0;
2002a94100faSBill Paul 
200380a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
2004a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
200580a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
2006a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
200780a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
2008a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
2009a94100faSBill Paul 
2010a94100faSBill Paul 	arg.sc = sc;
2011a94100faSBill Paul 	arg.rl_idx = *idx;
2012a94100faSBill Paul 	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
20137cae6651SBill Paul 	if (arg.rl_maxsegs > 4)
20147cae6651SBill Paul 		arg.rl_maxsegs -= 4;
2015a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_tx_list;
2016a94100faSBill Paul 
2017a94100faSBill Paul 	map = sc->rl_ldata.rl_tx_dmamap[*idx];
2018a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
201980a2a305SJohn-Mark Gurney 	    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2020a94100faSBill Paul 
2021a94100faSBill Paul 	if (error && error != EFBIG) {
2022d1754a9bSJohn Baldwin 		if_printf(sc->rl_ifp, "can't map mbuf (error %d)\n", error);
2023a94100faSBill Paul 		return (ENOBUFS);
2024a94100faSBill Paul 	}
2025a94100faSBill Paul 
2026a94100faSBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
2027a94100faSBill Paul 
2028a94100faSBill Paul 	if (error || arg.rl_maxsegs == 0) {
202980a2a305SJohn-Mark Gurney 		m_new = m_defrag(*m_head, M_DONTWAIT);
2030a94100faSBill Paul 		if (m_new == NULL)
203180a2a305SJohn-Mark Gurney 			return (ENOBUFS);
2032a94100faSBill Paul 		else
203380a2a305SJohn-Mark Gurney 			*m_head = m_new;
2034a94100faSBill Paul 
2035a94100faSBill Paul 		arg.sc = sc;
2036a94100faSBill Paul 		arg.rl_idx = *idx;
2037a94100faSBill Paul 		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
2038a94100faSBill Paul 		arg.rl_ring = sc->rl_ldata.rl_tx_list;
2039a94100faSBill Paul 
2040a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
204180a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
2042a94100faSBill Paul 		if (error) {
2043d1754a9bSJohn Baldwin 			if_printf(sc->rl_ifp, "can't map mbuf (error %d)\n",
2044d1754a9bSJohn Baldwin 			    error);
2045a94100faSBill Paul 			return (EFBIG);
2046a94100faSBill Paul 		}
2047a94100faSBill Paul 	}
2048a94100faSBill Paul 
2049a94100faSBill Paul 	/*
2050a94100faSBill Paul 	 * Insure that the map for this transmission
2051a94100faSBill Paul 	 * is placed at the array index of the last descriptor
205222a11c96SJohn-Mark Gurney 	 * in this chain.  (Swap last and first dmamaps.)
2053a94100faSBill Paul 	 */
2054a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[*idx] =
2055a94100faSBill Paul 	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
2056a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
2057a94100faSBill Paul 
205880a2a305SJohn-Mark Gurney 	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head;
2059a94100faSBill Paul 	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
2060a94100faSBill Paul 
2061a94100faSBill Paul 	/*
2062a94100faSBill Paul 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2063a94100faSBill Paul 	 * appear in the first descriptor of a multi-descriptor
2064a94100faSBill Paul 	 * transmission attempt.
2065a94100faSBill Paul 	 */
2066a94100faSBill Paul 
2067fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->rl_ifp, *m_head);
2068a94100faSBill Paul 	if (mtag != NULL)
2069a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
2070a94100faSBill Paul 		    htole32(htons(VLAN_TAG_VALUE(mtag)) | RL_TDESC_VLANCTL_TAG);
2071a94100faSBill Paul 
2072a94100faSBill Paul 	/* Transfer ownership of packet to the chip. */
2073a94100faSBill Paul 
2074a94100faSBill Paul 	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
2075a94100faSBill Paul 	    htole32(RL_TDESC_CMD_OWN);
2076a94100faSBill Paul 	if (*idx != arg.rl_idx)
2077a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
2078a94100faSBill Paul 		    htole32(RL_TDESC_CMD_OWN);
2079a94100faSBill Paul 
2080a94100faSBill Paul         RL_DESC_INC(arg.rl_idx);
2081a94100faSBill Paul 	*idx = arg.rl_idx;
2082a94100faSBill Paul 
2083a94100faSBill Paul 	return (0);
2084a94100faSBill Paul }
2085a94100faSBill Paul 
208697b9d4baSJohn-Mark Gurney static void
2087ed510fb0SBill Paul re_tx_task(arg, npending)
2088ed510fb0SBill Paul 	void			*arg;
2089ed510fb0SBill Paul 	int			npending;
209097b9d4baSJohn-Mark Gurney {
2091ed510fb0SBill Paul 	struct ifnet		*ifp;
209297b9d4baSJohn-Mark Gurney 
2093ed510fb0SBill Paul 	ifp = arg;
2094ed510fb0SBill Paul 	re_start(ifp);
2095ed510fb0SBill Paul 
2096ed510fb0SBill Paul 	return;
209797b9d4baSJohn-Mark Gurney }
209897b9d4baSJohn-Mark Gurney 
2099a94100faSBill Paul /*
2100a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2101a94100faSBill Paul  */
2102a94100faSBill Paul static void
2103ed510fb0SBill Paul re_start(ifp)
2104a94100faSBill Paul 	struct ifnet		*ifp;
2105a94100faSBill Paul {
2106a94100faSBill Paul 	struct rl_softc		*sc;
2107a94100faSBill Paul 	struct mbuf		*m_head = NULL;
210852732175SMax Laier 	int			idx, queued = 0;
2109a94100faSBill Paul 
2110a94100faSBill Paul 	sc = ifp->if_softc;
211197b9d4baSJohn-Mark Gurney 
2112ed510fb0SBill Paul 	RL_LOCK(sc);
2113ed510fb0SBill Paul 
2114ed510fb0SBill Paul 	if (!sc->rl_link || ifp->if_drv_flags & IFF_DRV_OACTIVE) {
2115ed510fb0SBill Paul 		RL_UNLOCK(sc);
2116ed510fb0SBill Paul 		return;
2117ed510fb0SBill Paul 	}
2118a94100faSBill Paul 
2119a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_prodidx;
2120a94100faSBill Paul 
2121a94100faSBill Paul 	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
212252732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2123a94100faSBill Paul 		if (m_head == NULL)
2124a94100faSBill Paul 			break;
2125a94100faSBill Paul 
212680a2a305SJohn-Mark Gurney 		if (re_encap(sc, &m_head, &idx)) {
212752732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
212813f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2129a94100faSBill Paul 			break;
2130a94100faSBill Paul 		}
2131a94100faSBill Paul 
2132a94100faSBill Paul 		/*
2133a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2134a94100faSBill Paul 		 * to him.
2135a94100faSBill Paul 		 */
2136a94100faSBill Paul 		BPF_MTAP(ifp, m_head);
213752732175SMax Laier 
213852732175SMax Laier 		queued++;
2139a94100faSBill Paul 	}
2140a94100faSBill Paul 
2141ed510fb0SBill Paul 	if (queued == 0) {
2142ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2143ed510fb0SBill Paul 		if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
2144ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2145ed510fb0SBill Paul #endif
2146ed510fb0SBill Paul 		RL_UNLOCK(sc);
214752732175SMax Laier 		return;
2148ed510fb0SBill Paul 	}
214952732175SMax Laier 
2150a94100faSBill Paul 	/* Flush the TX descriptors */
2151a94100faSBill Paul 
2152a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2153a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2154a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2155a94100faSBill Paul 
2156a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = idx;
2157a94100faSBill Paul 
2158a94100faSBill Paul 	/*
2159a94100faSBill Paul 	 * RealTek put the TX poll request register in a different
2160a94100faSBill Paul 	 * location on the 8169 gigE chip. I don't know why.
2161a94100faSBill Paul 	 */
2162a94100faSBill Paul 
2163ed510fb0SBill Paul 	CSR_WRITE_2(sc, sc->rl_txstart, RL_TXSTART_START);
2164a94100faSBill Paul 
2165ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2166a94100faSBill Paul 	/*
2167a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2168a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2169a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2170a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2171a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2172a94100faSBill Paul 	 * the timer count is reset to 0.
2173a94100faSBill Paul 	 */
2174a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2175ed510fb0SBill Paul #endif
2176a94100faSBill Paul 
2177a94100faSBill Paul 	/*
2178a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2179a94100faSBill Paul 	 */
2180ed510fb0SBill Paul 
2181a94100faSBill Paul 	ifp->if_timer = 5;
2182ed510fb0SBill Paul 
2183ed510fb0SBill Paul 	RL_UNLOCK(sc);
2184ed510fb0SBill Paul 
2185ed510fb0SBill Paul 	return;
2186a94100faSBill Paul }
2187a94100faSBill Paul 
2188a94100faSBill Paul static void
2189a94100faSBill Paul re_init(xsc)
2190a94100faSBill Paul 	void			*xsc;
2191a94100faSBill Paul {
2192a94100faSBill Paul 	struct rl_softc		*sc = xsc;
219397b9d4baSJohn-Mark Gurney 
219497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
219597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
219697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
219797b9d4baSJohn-Mark Gurney }
219897b9d4baSJohn-Mark Gurney 
219997b9d4baSJohn-Mark Gurney static void
220097b9d4baSJohn-Mark Gurney re_init_locked(sc)
220197b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
220297b9d4baSJohn-Mark Gurney {
2203fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2204a94100faSBill Paul 	struct mii_data		*mii;
2205a94100faSBill Paul 	u_int32_t		rxcfg = 0;
22064d3d7085SBernd Walter 	union {
22074d3d7085SBernd Walter 		uint32_t align_dummy;
22084d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
22094d3d7085SBernd Walter         } eaddr;
2210a94100faSBill Paul 
221197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
221297b9d4baSJohn-Mark Gurney 
2213a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2214a94100faSBill Paul 
2215a94100faSBill Paul 	/*
2216a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2217a94100faSBill Paul 	 */
2218a94100faSBill Paul 	re_stop(sc);
2219a94100faSBill Paul 
2220a94100faSBill Paul 	/*
2221c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2222edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2223c2c6548bSBill Paul 	 * before all others.
2224c2c6548bSBill Paul 	 */
2225c2c6548bSBill Paul 	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2226c2c6548bSBill Paul 	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2227ed510fb0SBill Paul 	    RL_CPLUSCMD_VLANSTRIP|RL_CPLUSCMD_RXCSUM_ENB);
2228c2c6548bSBill Paul 
2229c2c6548bSBill Paul 	/*
2230a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2231a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2232a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2233a94100faSBill Paul 	 */
22344d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
22354d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2236a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2237ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2238ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2239ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2240ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2241a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2242a94100faSBill Paul 
2243a94100faSBill Paul 	/*
2244a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2245a94100faSBill Paul 	 */
2246a94100faSBill Paul 	re_rx_list_init(sc);
2247a94100faSBill Paul 	re_tx_list_init(sc);
2248a94100faSBill Paul 
2249a94100faSBill Paul 	/*
2250a94100faSBill Paul 	 * Enable transmit and receive.
2251a94100faSBill Paul 	 */
2252a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2253a94100faSBill Paul 
2254a94100faSBill Paul 	/*
2255a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2256a94100faSBill Paul 	 */
2257abc8ff44SBill Paul 	if (sc->rl_testmode) {
2258abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2259abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2260abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2261a94100faSBill Paul 		else
2262abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2263abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2264abc8ff44SBill Paul 	} else
2265a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2266a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2267a94100faSBill Paul 
2268a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2269a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2270a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2271a94100faSBill Paul 
2272a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
227361021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2274a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
227561021536SJohn-Mark Gurney 	else
2276a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2277a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2278a94100faSBill Paul 
2279a94100faSBill Paul 	/*
2280a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2281a94100faSBill Paul 	 */
228261021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2283a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
228461021536SJohn-Mark Gurney 	else
2285a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2286a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2287a94100faSBill Paul 
2288a94100faSBill Paul 	/*
2289a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2290a94100faSBill Paul 	 */
2291a94100faSBill Paul 	re_setmulti(sc);
2292a94100faSBill Paul 
2293a94100faSBill Paul #ifdef DEVICE_POLLING
2294a94100faSBill Paul 	/*
2295a94100faSBill Paul 	 * Disable interrupts if we are polling.
2296a94100faSBill Paul 	 */
229740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2298a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2299a94100faSBill Paul 	else	/* otherwise ... */
230040929967SGleb Smirnoff #endif
2301ed510fb0SBill Paul 
2302a94100faSBill Paul 	/*
2303a94100faSBill Paul 	 * Enable interrupts.
2304a94100faSBill Paul 	 */
2305ed510fb0SBill Paul 	mtx_lock_spin(&sc->rl_intlock);
2306a94100faSBill Paul 	if (sc->rl_testmode)
2307a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2308a94100faSBill Paul 	else
2309a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2310ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2311ed510fb0SBill Paul 	mtx_unlock_spin(&sc->rl_intlock);
2312a94100faSBill Paul 
2313a94100faSBill Paul 	/* Set initial TX threshold */
2314a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2315a94100faSBill Paul 
2316a94100faSBill Paul 	/* Start RX/TX process. */
2317a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2318a94100faSBill Paul #ifdef notdef
2319a94100faSBill Paul 	/* Enable receiver and transmitter. */
2320a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2321a94100faSBill Paul #endif
2322a94100faSBill Paul 	/*
2323c2c6548bSBill Paul 	 * Load the addresses of the RX and TX lists into the chip.
2324a94100faSBill Paul 	 */
2325a94100faSBill Paul 
2326a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2327a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2328a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2329a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2330a94100faSBill Paul 
2331a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2332a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2333a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2334a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2335a94100faSBill Paul 
2336a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2337a94100faSBill Paul 
2338ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2339a94100faSBill Paul 	/*
2340a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2341a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2342a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2343a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2344a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2345a94100faSBill Paul 	 */
2346a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2347a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2348a94100faSBill Paul 	else
2349a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2350ed510fb0SBill Paul #endif
2351a94100faSBill Paul 
2352a94100faSBill Paul 	/*
2353a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2354a94100faSBill Paul 	 * size so we can receive jumbo frames.
2355a94100faSBill Paul 	 */
2356a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2357a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2358a94100faSBill Paul 
235997b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2360a94100faSBill Paul 		return;
2361a94100faSBill Paul 
2362a94100faSBill Paul 	mii_mediachg(mii);
2363a94100faSBill Paul 
2364a94100faSBill Paul 	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
2365a94100faSBill Paul 
236613f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
236713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2368a94100faSBill Paul 
2369ed510fb0SBill Paul 
2370ed510fb0SBill Paul 	sc->rl_link = 0;
2371ed510fb0SBill Paul 
2372d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2373a94100faSBill Paul }
2374a94100faSBill Paul 
2375a94100faSBill Paul /*
2376a94100faSBill Paul  * Set media options.
2377a94100faSBill Paul  */
2378a94100faSBill Paul static int
2379a94100faSBill Paul re_ifmedia_upd(ifp)
2380a94100faSBill Paul 	struct ifnet		*ifp;
2381a94100faSBill Paul {
2382a94100faSBill Paul 	struct rl_softc		*sc;
2383a94100faSBill Paul 	struct mii_data		*mii;
2384a94100faSBill Paul 
2385a94100faSBill Paul 	sc = ifp->if_softc;
2386a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2387d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2388a94100faSBill Paul 	mii_mediachg(mii);
2389d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2390a94100faSBill Paul 
2391a94100faSBill Paul 	return (0);
2392a94100faSBill Paul }
2393a94100faSBill Paul 
2394a94100faSBill Paul /*
2395a94100faSBill Paul  * Report current media status.
2396a94100faSBill Paul  */
2397a94100faSBill Paul static void
2398a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2399a94100faSBill Paul 	struct ifnet		*ifp;
2400a94100faSBill Paul 	struct ifmediareq	*ifmr;
2401a94100faSBill Paul {
2402a94100faSBill Paul 	struct rl_softc		*sc;
2403a94100faSBill Paul 	struct mii_data		*mii;
2404a94100faSBill Paul 
2405a94100faSBill Paul 	sc = ifp->if_softc;
2406a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2407a94100faSBill Paul 
2408d1754a9bSJohn Baldwin 	RL_LOCK(sc);
2409a94100faSBill Paul 	mii_pollstat(mii);
2410d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
2411a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2412a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2413a94100faSBill Paul }
2414a94100faSBill Paul 
2415a94100faSBill Paul static int
2416a94100faSBill Paul re_ioctl(ifp, command, data)
2417a94100faSBill Paul 	struct ifnet		*ifp;
2418a94100faSBill Paul 	u_long			command;
2419a94100faSBill Paul 	caddr_t			data;
2420a94100faSBill Paul {
2421a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2422a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2423a94100faSBill Paul 	struct mii_data		*mii;
242440929967SGleb Smirnoff 	int			error = 0;
2425a94100faSBill Paul 
2426a94100faSBill Paul 	switch (command) {
2427a94100faSBill Paul 	case SIOCSIFMTU:
2428d1754a9bSJohn Baldwin 		RL_LOCK(sc);
2429a94100faSBill Paul 		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2430a94100faSBill Paul 			error = EINVAL;
2431a94100faSBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2432d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
2433a94100faSBill Paul 		break;
2434a94100faSBill Paul 	case SIOCSIFFLAGS:
243597b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
243697b9d4baSJohn-Mark Gurney 		if (ifp->if_flags & IFF_UP)
243797b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
243813f4c340SRobert Watson 		else if (ifp->if_drv_flags & IFF_DRV_RUNNING)
2439a94100faSBill Paul 			re_stop(sc);
244097b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2441a94100faSBill Paul 		break;
2442a94100faSBill Paul 	case SIOCADDMULTI:
2443a94100faSBill Paul 	case SIOCDELMULTI:
244497b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2445a94100faSBill Paul 		re_setmulti(sc);
244697b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2447a94100faSBill Paul 		break;
2448a94100faSBill Paul 	case SIOCGIFMEDIA:
2449a94100faSBill Paul 	case SIOCSIFMEDIA:
2450a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2451a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2452a94100faSBill Paul 		break;
2453a94100faSBill Paul 	case SIOCSIFCAP:
245440929967SGleb Smirnoff 	    {
2455f051cb85SGleb Smirnoff 		int mask, reinit;
2456f051cb85SGleb Smirnoff 
2457f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2458f051cb85SGleb Smirnoff 		reinit = 0;
245940929967SGleb Smirnoff #ifdef DEVICE_POLLING
246040929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
246140929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
246240929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
246340929967SGleb Smirnoff 				if (error)
246440929967SGleb Smirnoff 					return(error);
2465d1754a9bSJohn Baldwin 				RL_LOCK(sc);
246640929967SGleb Smirnoff 				/* Disable interrupts */
246740929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
246840929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
246940929967SGleb Smirnoff 				RL_UNLOCK(sc);
247040929967SGleb Smirnoff 
247140929967SGleb Smirnoff 			} else {
247240929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
247340929967SGleb Smirnoff 				/* Enable interrupts. */
247440929967SGleb Smirnoff 				RL_LOCK(sc);
247540929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
247640929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
247740929967SGleb Smirnoff 				RL_UNLOCK(sc);
247840929967SGleb Smirnoff 			}
247940929967SGleb Smirnoff 		}
248040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
248140929967SGleb Smirnoff 		if (mask & IFCAP_HWCSUM) {
2482f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_HWCSUM;
2483a94100faSBill Paul 			if (ifp->if_capenable & IFCAP_TXCSUM)
2484a94100faSBill Paul 				ifp->if_hwassist = RE_CSUM_FEATURES;
2485a94100faSBill Paul 			else
2486a94100faSBill Paul 				ifp->if_hwassist = 0;
2487f051cb85SGleb Smirnoff 			reinit = 1;
248840929967SGleb Smirnoff 		}
2489f051cb85SGleb Smirnoff 		if (mask & IFCAP_VLAN_HWTAGGING) {
2490f051cb85SGleb Smirnoff 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2491f051cb85SGleb Smirnoff 			reinit = 1;
2492f051cb85SGleb Smirnoff 		}
2493f051cb85SGleb Smirnoff 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING)
2494f051cb85SGleb Smirnoff 			re_init(sc);
249540929967SGleb Smirnoff 	    }
2496a94100faSBill Paul 		break;
2497a94100faSBill Paul 	default:
2498a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2499a94100faSBill Paul 		break;
2500a94100faSBill Paul 	}
2501a94100faSBill Paul 
2502a94100faSBill Paul 	return (error);
2503a94100faSBill Paul }
2504a94100faSBill Paul 
2505a94100faSBill Paul static void
2506a94100faSBill Paul re_watchdog(ifp)
2507a94100faSBill Paul 	struct ifnet		*ifp;
2508a94100faSBill Paul {
2509a94100faSBill Paul 	struct rl_softc		*sc;
2510a94100faSBill Paul 
2511a94100faSBill Paul 	sc = ifp->if_softc;
2512a94100faSBill Paul 	RL_LOCK(sc);
2513d1754a9bSJohn Baldwin 	if_printf(ifp, "watchdog timeout\n");
2514a94100faSBill Paul 	ifp->if_oerrors++;
2515a94100faSBill Paul 
2516a94100faSBill Paul 	re_txeof(sc);
2517a94100faSBill Paul 	re_rxeof(sc);
251897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2519a94100faSBill Paul 
2520a94100faSBill Paul 	RL_UNLOCK(sc);
2521a94100faSBill Paul }
2522a94100faSBill Paul 
2523a94100faSBill Paul /*
2524a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2525a94100faSBill Paul  * RX and TX lists.
2526a94100faSBill Paul  */
2527a94100faSBill Paul static void
2528a94100faSBill Paul re_stop(sc)
2529a94100faSBill Paul 	struct rl_softc		*sc;
2530a94100faSBill Paul {
2531a94100faSBill Paul 	register int		i;
2532a94100faSBill Paul 	struct ifnet		*ifp;
2533a94100faSBill Paul 
253497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
253597b9d4baSJohn-Mark Gurney 
2536fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2537a94100faSBill Paul 	ifp->if_timer = 0;
2538a94100faSBill Paul 
2539d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
254013f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2541a94100faSBill Paul 
2542a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2543a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2544ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
2545a94100faSBill Paul 
2546a94100faSBill Paul 	if (sc->rl_head != NULL) {
2547a94100faSBill Paul 		m_freem(sc->rl_head);
2548a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2549a94100faSBill Paul 	}
2550a94100faSBill Paul 
2551a94100faSBill Paul 	/* Free the TX list buffers. */
2552a94100faSBill Paul 
2553a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2554a94100faSBill Paul 		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2555a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2556a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
2557a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2558a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2559a94100faSBill Paul 		}
2560a94100faSBill Paul 	}
2561a94100faSBill Paul 
2562a94100faSBill Paul 	/* Free the RX list buffers. */
2563a94100faSBill Paul 
2564a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2565a94100faSBill Paul 		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2566a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2567a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
2568a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2569a94100faSBill Paul 			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2570a94100faSBill Paul 		}
2571a94100faSBill Paul 	}
2572a94100faSBill Paul }
2573a94100faSBill Paul 
2574a94100faSBill Paul /*
2575a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2576a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2577a94100faSBill Paul  * resume.
2578a94100faSBill Paul  */
2579a94100faSBill Paul static int
2580a94100faSBill Paul re_suspend(dev)
2581a94100faSBill Paul 	device_t		dev;
2582a94100faSBill Paul {
2583a94100faSBill Paul 	struct rl_softc		*sc;
2584a94100faSBill Paul 
2585a94100faSBill Paul 	sc = device_get_softc(dev);
2586a94100faSBill Paul 
258797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2588a94100faSBill Paul 	re_stop(sc);
2589a94100faSBill Paul 	sc->suspended = 1;
259097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2591a94100faSBill Paul 
2592a94100faSBill Paul 	return (0);
2593a94100faSBill Paul }
2594a94100faSBill Paul 
2595a94100faSBill Paul /*
2596a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2597a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2598a94100faSBill Paul  * appropriate.
2599a94100faSBill Paul  */
2600a94100faSBill Paul static int
2601a94100faSBill Paul re_resume(dev)
2602a94100faSBill Paul 	device_t		dev;
2603a94100faSBill Paul {
2604a94100faSBill Paul 	struct rl_softc		*sc;
2605a94100faSBill Paul 	struct ifnet		*ifp;
2606a94100faSBill Paul 
2607a94100faSBill Paul 	sc = device_get_softc(dev);
260897b9d4baSJohn-Mark Gurney 
260997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
261097b9d4baSJohn-Mark Gurney 
2611fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2612a94100faSBill Paul 
2613a94100faSBill Paul 	/* reinitialize interface if necessary */
2614a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
261597b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2616a94100faSBill Paul 
2617a94100faSBill Paul 	sc->suspended = 0;
261897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2619a94100faSBill Paul 
2620a94100faSBill Paul 	return (0);
2621a94100faSBill Paul }
2622a94100faSBill Paul 
2623a94100faSBill Paul /*
2624a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2625a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2626a94100faSBill Paul  */
2627a94100faSBill Paul static void
2628a94100faSBill Paul re_shutdown(dev)
2629a94100faSBill Paul 	device_t		dev;
2630a94100faSBill Paul {
2631a94100faSBill Paul 	struct rl_softc		*sc;
2632a94100faSBill Paul 
2633a94100faSBill Paul 	sc = device_get_softc(dev);
2634a94100faSBill Paul 
263597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2636a94100faSBill Paul 	re_stop(sc);
2637536fde34SMaxim Sobolev 	/*
2638536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
2639536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
264072293673SRuslan Ermilov 	 * cases.
2641536fde34SMaxim Sobolev 	 */
2642536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
264397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2644a94100faSBill Paul }
2645