xref: /freebsd/sys/dev/re/if_re.c (revision 098ca2bda93c701c5331d4e6aace072495b4caaa)
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 /*
37a94100faSBill Paul  * RealTek 8139C+/8169/8169S/8110S 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
47a94100faSBill Paul  * four devices in this family: the RTL8139C+, the RTL8169, the RTL8169S
48a94100faSBill Paul  * and the RTL8110S.
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 
111a94100faSBill Paul #include <sys/param.h>
112a94100faSBill Paul #include <sys/endian.h>
113a94100faSBill Paul #include <sys/systm.h>
114a94100faSBill Paul #include <sys/sockio.h>
115a94100faSBill Paul #include <sys/mbuf.h>
116a94100faSBill Paul #include <sys/malloc.h>
117fe12f24bSPoul-Henning Kamp #include <sys/module.h>
118a94100faSBill Paul #include <sys/kernel.h>
119a94100faSBill Paul #include <sys/socket.h>
120a94100faSBill Paul 
121a94100faSBill Paul #include <net/if.h>
122a94100faSBill Paul #include <net/if_arp.h>
123a94100faSBill Paul #include <net/ethernet.h>
124a94100faSBill Paul #include <net/if_dl.h>
125a94100faSBill Paul #include <net/if_media.h>
126a94100faSBill Paul #include <net/if_vlan_var.h>
127a94100faSBill Paul 
128a94100faSBill Paul #include <net/bpf.h>
129a94100faSBill Paul 
130a94100faSBill Paul #include <machine/bus_pio.h>
131a94100faSBill Paul #include <machine/bus_memio.h>
132a94100faSBill Paul #include <machine/bus.h>
133a94100faSBill Paul #include <machine/resource.h>
134a94100faSBill Paul #include <sys/bus.h>
135a94100faSBill Paul #include <sys/rman.h>
136a94100faSBill Paul 
137a94100faSBill Paul #include <dev/mii/mii.h>
138a94100faSBill Paul #include <dev/mii/miivar.h>
139a94100faSBill Paul 
140a94100faSBill Paul #include <dev/pci/pcireg.h>
141a94100faSBill Paul #include <dev/pci/pcivar.h>
142a94100faSBill Paul 
143a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
144a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
145a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
146a94100faSBill Paul 
147a94100faSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
148a94100faSBill Paul #include "miibus_if.h"
149a94100faSBill Paul 
150a94100faSBill Paul /*
151a94100faSBill Paul  * Default to using PIO access for this driver.
152a94100faSBill Paul  */
153a94100faSBill Paul #define RE_USEIOSPACE
154a94100faSBill Paul 
155a94100faSBill Paul #include <pci/if_rlreg.h>
156a94100faSBill Paul 
157a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
158a94100faSBill Paul 
159a94100faSBill Paul /*
160a94100faSBill Paul  * Various supported device vendors/types and their names.
161a94100faSBill Paul  */
162a94100faSBill Paul static struct rl_type re_devs[] = {
163a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
164a94100faSBill Paul 		"RealTek 8139C+ 10/100BaseTX" },
165a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
166a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
16769a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169S,
16869a6b7fbSBill Paul 		"RealTek 8169S Single-chip Gigabit Ethernet" },
16969a6b7fbSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110S,
17069a6b7fbSBill Paul 		"RealTek 8110S Single-chip Gigabit Ethernet" },
171ea263191SMIHIRA Sanpei Yoshiro 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, RL_HWREV_8169S,
172ea263191SMIHIRA Sanpei Yoshiro 		"Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
173a94100faSBill Paul 	{ 0, 0, 0, NULL }
174a94100faSBill Paul };
175a94100faSBill Paul 
176a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
177a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
178a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
179a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
180a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
181a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
182a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
183a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
184a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
185a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
18669a6b7fbSBill Paul 	{ RL_HWREV_8169S, RL_8169, "8169S"},
18769a6b7fbSBill Paul 	{ RL_HWREV_8110S, RL_8169, "8110S"},
188a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
189a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
190a94100faSBill Paul 	{ 0, 0, NULL }
191a94100faSBill Paul };
192a94100faSBill Paul 
193a94100faSBill Paul static int re_probe		(device_t);
194a94100faSBill Paul static int re_attach		(device_t);
195a94100faSBill Paul static int re_detach		(device_t);
196a94100faSBill Paul 
19780a2a305SJohn-Mark Gurney static int re_encap		(struct rl_softc *, struct mbuf **, int *);
198a94100faSBill Paul 
199a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
200a94100faSBill Paul static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
201a94100faSBill Paul 				    bus_size_t, int);
202a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
203a94100faSBill Paul static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
204a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
205a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
20622a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
20722a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
20822a11c96SJohn-Mark Gurney 				(struct mbuf *);
20922a11c96SJohn-Mark Gurney #endif
210a94100faSBill Paul static void re_rxeof		(struct rl_softc *);
211a94100faSBill Paul static void re_txeof		(struct rl_softc *);
21297b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2130187838bSRuslan Ermilov static void re_poll		(struct ifnet *, enum poll_cmd, int);
2140187838bSRuslan Ermilov static void re_poll_locked	(struct ifnet *, enum poll_cmd, int);
21597b9d4baSJohn-Mark Gurney #endif
216a94100faSBill Paul static void re_intr		(void *);
217a94100faSBill Paul static void re_tick		(void *);
21897b9d4baSJohn-Mark Gurney static void re_tick_locked	(struct rl_softc *);
219a94100faSBill Paul static void re_start		(struct ifnet *);
22097b9d4baSJohn-Mark Gurney static void re_start_locked	(struct ifnet *);
221a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
222a94100faSBill Paul static void re_init		(void *);
22397b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
224a94100faSBill Paul static void re_stop		(struct rl_softc *);
225a94100faSBill Paul static void re_watchdog		(struct ifnet *);
226a94100faSBill Paul static int re_suspend		(device_t);
227a94100faSBill Paul static int re_resume		(device_t);
228a94100faSBill Paul static void re_shutdown		(device_t);
229a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
230a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
231a94100faSBill Paul 
232a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
233a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
234a94100faSBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int, int);
235a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
236a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
237a94100faSBill Paul 
238a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
239a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
240a94100faSBill Paul static void re_miibus_statchg	(device_t);
241a94100faSBill Paul 
242a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
243a94100faSBill Paul static void re_reset		(struct rl_softc *);
244a94100faSBill Paul 
245a94100faSBill Paul static int re_diag		(struct rl_softc *);
246a94100faSBill Paul 
247a94100faSBill Paul #ifdef RE_USEIOSPACE
248a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
249a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
250a94100faSBill Paul #else
251a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
252a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
253a94100faSBill Paul #endif
254a94100faSBill Paul 
255a94100faSBill Paul static device_method_t re_methods[] = {
256a94100faSBill Paul 	/* Device interface */
257a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
258a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
259a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
260a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
261a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
262a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
263a94100faSBill Paul 
264a94100faSBill Paul 	/* bus interface */
265a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
266a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
267a94100faSBill Paul 
268a94100faSBill Paul 	/* MII interface */
269a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
270a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
271a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
272a94100faSBill Paul 
273a94100faSBill Paul 	{ 0, 0 }
274a94100faSBill Paul };
275a94100faSBill Paul 
276a94100faSBill Paul static driver_t re_driver = {
277a94100faSBill Paul 	"re",
278a94100faSBill Paul 	re_methods,
279a94100faSBill Paul 	sizeof(struct rl_softc)
280a94100faSBill Paul };
281a94100faSBill Paul 
282a94100faSBill Paul static devclass_t re_devclass;
283a94100faSBill Paul 
284a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
285347934faSWarner Losh DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
286a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
287a94100faSBill Paul 
288a94100faSBill Paul #define EE_SET(x)					\
289a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
290a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
291a94100faSBill Paul 
292a94100faSBill Paul #define EE_CLR(x)					\
293a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
294a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
295a94100faSBill Paul 
296a94100faSBill Paul /*
297a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
298a94100faSBill Paul  */
299a94100faSBill Paul static void
300a94100faSBill Paul re_eeprom_putbyte(sc, addr)
301a94100faSBill Paul 	struct rl_softc		*sc;
302a94100faSBill Paul 	int			addr;
303a94100faSBill Paul {
304a94100faSBill Paul 	register int		d, i;
305a94100faSBill Paul 
306a94100faSBill Paul 	d = addr | sc->rl_eecmd_read;
307a94100faSBill Paul 
308a94100faSBill Paul 	/*
309a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
310a94100faSBill Paul 	 */
311a94100faSBill Paul 	for (i = 0x400; i; i >>= 1) {
312a94100faSBill Paul 		if (d & i) {
313a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
314a94100faSBill Paul 		} else {
315a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
316a94100faSBill Paul 		}
317a94100faSBill Paul 		DELAY(100);
318a94100faSBill Paul 		EE_SET(RL_EE_CLK);
319a94100faSBill Paul 		DELAY(150);
320a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
321a94100faSBill Paul 		DELAY(100);
322a94100faSBill Paul 	}
323a94100faSBill Paul }
324a94100faSBill Paul 
325a94100faSBill Paul /*
326a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
327a94100faSBill Paul  */
328a94100faSBill Paul static void
329a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
330a94100faSBill Paul 	struct rl_softc		*sc;
331a94100faSBill Paul 	int			addr;
332a94100faSBill Paul 	u_int16_t		*dest;
333a94100faSBill Paul {
334a94100faSBill Paul 	register int		i;
335a94100faSBill Paul 	u_int16_t		word = 0;
336a94100faSBill Paul 
337a94100faSBill Paul 	/* Enter EEPROM access mode. */
338a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
339a94100faSBill Paul 
340a94100faSBill Paul 	/*
341a94100faSBill Paul 	 * Send address of word we want to read.
342a94100faSBill Paul 	 */
343a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
344a94100faSBill Paul 
345a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
346a94100faSBill Paul 
347a94100faSBill Paul 	/*
348a94100faSBill Paul 	 * Start reading bits from EEPROM.
349a94100faSBill Paul 	 */
350a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
351a94100faSBill Paul 		EE_SET(RL_EE_CLK);
352a94100faSBill Paul 		DELAY(100);
353a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
354a94100faSBill Paul 			word |= i;
355a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
356a94100faSBill Paul 		DELAY(100);
357a94100faSBill Paul 	}
358a94100faSBill Paul 
359a94100faSBill Paul 	/* Turn off EEPROM access mode. */
360a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
361a94100faSBill Paul 
362a94100faSBill Paul 	*dest = word;
363a94100faSBill Paul }
364a94100faSBill Paul 
365a94100faSBill Paul /*
366a94100faSBill Paul  * Read a sequence of words from the EEPROM.
367a94100faSBill Paul  */
368a94100faSBill Paul static void
369a94100faSBill Paul re_read_eeprom(sc, dest, off, cnt, swap)
370a94100faSBill Paul 	struct rl_softc		*sc;
371a94100faSBill Paul 	caddr_t			dest;
372a94100faSBill Paul 	int			off;
373a94100faSBill Paul 	int			cnt;
374a94100faSBill Paul 	int			swap;
375a94100faSBill Paul {
376a94100faSBill Paul 	int			i;
377a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
378a94100faSBill Paul 
379a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
380a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
381a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
382a94100faSBill Paul 		if (swap)
383a94100faSBill Paul 			*ptr = ntohs(word);
384a94100faSBill Paul 		else
385a94100faSBill Paul 			*ptr = word;
386a94100faSBill Paul 	}
387a94100faSBill Paul }
388a94100faSBill Paul 
389a94100faSBill Paul static int
390a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
391a94100faSBill Paul 	device_t		dev;
392a94100faSBill Paul 	int			phy, reg;
393a94100faSBill Paul {
394a94100faSBill Paul 	struct rl_softc		*sc;
395a94100faSBill Paul 	u_int32_t		rval;
396a94100faSBill Paul 	int			i;
397a94100faSBill Paul 
398a94100faSBill Paul 	if (phy != 1)
399a94100faSBill Paul 		return (0);
400a94100faSBill Paul 
401a94100faSBill Paul 	sc = device_get_softc(dev);
402a94100faSBill Paul 
4039bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4049bac70b8SBill Paul 
4059bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4069bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4079bac70b8SBill Paul 		return (rval);
4089bac70b8SBill Paul 	}
4099bac70b8SBill Paul 
410a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
411a94100faSBill Paul 	DELAY(1000);
412a94100faSBill Paul 
413a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
414a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
415a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
416a94100faSBill Paul 			break;
417a94100faSBill Paul 		DELAY(100);
418a94100faSBill Paul 	}
419a94100faSBill Paul 
420a94100faSBill Paul 	if (i == RL_TIMEOUT) {
421a94100faSBill Paul 		printf ("re%d: PHY read failed\n", sc->rl_unit);
422a94100faSBill Paul 		return (0);
423a94100faSBill Paul 	}
424a94100faSBill Paul 
425a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
426a94100faSBill Paul }
427a94100faSBill Paul 
428a94100faSBill Paul static int
429a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
430a94100faSBill Paul 	device_t		dev;
431a94100faSBill Paul 	int			phy, reg, data;
432a94100faSBill Paul {
433a94100faSBill Paul 	struct rl_softc		*sc;
434a94100faSBill Paul 	u_int32_t		rval;
435a94100faSBill Paul 	int			i;
436a94100faSBill Paul 
437a94100faSBill Paul 	sc = device_get_softc(dev);
438a94100faSBill Paul 
439a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4409bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
441a94100faSBill Paul 	DELAY(1000);
442a94100faSBill Paul 
443a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
444a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
445a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
446a94100faSBill Paul 			break;
447a94100faSBill Paul 		DELAY(100);
448a94100faSBill Paul 	}
449a94100faSBill Paul 
450a94100faSBill Paul 	if (i == RL_TIMEOUT) {
451a94100faSBill Paul 		printf ("re%d: PHY write failed\n", sc->rl_unit);
452a94100faSBill Paul 		return (0);
453a94100faSBill Paul 	}
454a94100faSBill Paul 
455a94100faSBill Paul 	return (0);
456a94100faSBill Paul }
457a94100faSBill Paul 
458a94100faSBill Paul static int
459a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
460a94100faSBill Paul 	device_t		dev;
461a94100faSBill Paul 	int			phy, reg;
462a94100faSBill Paul {
463a94100faSBill Paul 	struct rl_softc		*sc;
464a94100faSBill Paul 	u_int16_t		rval = 0;
465a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
466a94100faSBill Paul 
467a94100faSBill Paul 	sc = device_get_softc(dev);
468a94100faSBill Paul 
469a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
470a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
471a94100faSBill Paul 		return (rval);
472a94100faSBill Paul 	}
473a94100faSBill Paul 
474a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
475a94100faSBill Paul 	if (phy) {
476a94100faSBill Paul 		return (0);
477a94100faSBill Paul 	}
478a94100faSBill Paul 	switch (reg) {
479a94100faSBill Paul 	case MII_BMCR:
480a94100faSBill Paul 		re8139_reg = RL_BMCR;
481a94100faSBill Paul 		break;
482a94100faSBill Paul 	case MII_BMSR:
483a94100faSBill Paul 		re8139_reg = RL_BMSR;
484a94100faSBill Paul 		break;
485a94100faSBill Paul 	case MII_ANAR:
486a94100faSBill Paul 		re8139_reg = RL_ANAR;
487a94100faSBill Paul 		break;
488a94100faSBill Paul 	case MII_ANER:
489a94100faSBill Paul 		re8139_reg = RL_ANER;
490a94100faSBill Paul 		break;
491a94100faSBill Paul 	case MII_ANLPAR:
492a94100faSBill Paul 		re8139_reg = RL_LPAR;
493a94100faSBill Paul 		break;
494a94100faSBill Paul 	case MII_PHYIDR1:
495a94100faSBill Paul 	case MII_PHYIDR2:
496a94100faSBill Paul 		return (0);
497a94100faSBill Paul 	/*
498a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
499a94100faSBill Paul 	 * register. If we have a link partner which does not
500a94100faSBill Paul 	 * support NWAY, this is the register which will tell
501a94100faSBill Paul 	 * us the results of parallel detection.
502a94100faSBill Paul 	 */
503a94100faSBill Paul 	case RL_MEDIASTAT:
504a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
505a94100faSBill Paul 		return (rval);
506a94100faSBill Paul 	default:
507a94100faSBill Paul 		printf("re%d: bad phy register\n", sc->rl_unit);
508a94100faSBill Paul 		return (0);
509a94100faSBill Paul 	}
510a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
511a94100faSBill Paul 	return (rval);
512a94100faSBill Paul }
513a94100faSBill Paul 
514a94100faSBill Paul static int
515a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
516a94100faSBill Paul 	device_t		dev;
517a94100faSBill Paul 	int			phy, reg, data;
518a94100faSBill Paul {
519a94100faSBill Paul 	struct rl_softc		*sc;
520a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
521a94100faSBill Paul 	int			rval = 0;
522a94100faSBill Paul 
523a94100faSBill Paul 	sc = device_get_softc(dev);
524a94100faSBill Paul 
525a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
526a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
527a94100faSBill Paul 		return (rval);
528a94100faSBill Paul 	}
529a94100faSBill Paul 
530a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
53197b9d4baSJohn-Mark Gurney 	if (phy)
532a94100faSBill Paul 		return (0);
53397b9d4baSJohn-Mark Gurney 
534a94100faSBill Paul 	switch (reg) {
535a94100faSBill Paul 	case MII_BMCR:
536a94100faSBill Paul 		re8139_reg = RL_BMCR;
537a94100faSBill Paul 		break;
538a94100faSBill Paul 	case MII_BMSR:
539a94100faSBill Paul 		re8139_reg = RL_BMSR;
540a94100faSBill Paul 		break;
541a94100faSBill Paul 	case MII_ANAR:
542a94100faSBill Paul 		re8139_reg = RL_ANAR;
543a94100faSBill Paul 		break;
544a94100faSBill Paul 	case MII_ANER:
545a94100faSBill Paul 		re8139_reg = RL_ANER;
546a94100faSBill Paul 		break;
547a94100faSBill Paul 	case MII_ANLPAR:
548a94100faSBill Paul 		re8139_reg = RL_LPAR;
549a94100faSBill Paul 		break;
550a94100faSBill Paul 	case MII_PHYIDR1:
551a94100faSBill Paul 	case MII_PHYIDR2:
552a94100faSBill Paul 		return (0);
553a94100faSBill Paul 		break;
554a94100faSBill Paul 	default:
555a94100faSBill Paul 		printf("re%d: bad phy register\n", sc->rl_unit);
556a94100faSBill Paul 		return (0);
557a94100faSBill Paul 	}
558a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
559a94100faSBill Paul 	return (0);
560a94100faSBill Paul }
561a94100faSBill Paul 
562a94100faSBill Paul static void
563a94100faSBill Paul re_miibus_statchg(dev)
564a94100faSBill Paul 	device_t		dev;
565a94100faSBill Paul {
566a11e2f18SBruce M Simpson 
567a94100faSBill Paul }
568a94100faSBill Paul 
569a94100faSBill Paul /*
570a94100faSBill Paul  * Program the 64-bit multicast hash filter.
571a94100faSBill Paul  */
572a94100faSBill Paul static void
573a94100faSBill Paul re_setmulti(sc)
574a94100faSBill Paul 	struct rl_softc		*sc;
575a94100faSBill Paul {
576a94100faSBill Paul 	struct ifnet		*ifp;
577a94100faSBill Paul 	int			h = 0;
578a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
579a94100faSBill Paul 	struct ifmultiaddr	*ifma;
580a94100faSBill Paul 	u_int32_t		rxfilt;
581a94100faSBill Paul 	int			mcnt = 0;
582a94100faSBill Paul 
58397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
58497b9d4baSJohn-Mark Gurney 
585a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
586a94100faSBill Paul 
587a94100faSBill Paul 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
588a94100faSBill Paul 
589a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
590a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
591a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
592a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
593a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
594a94100faSBill Paul 		return;
595a94100faSBill Paul 	}
596a94100faSBill Paul 
597a94100faSBill Paul 	/* first, zot all the existing hash bits */
598a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
599a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
600a94100faSBill Paul 
601a94100faSBill Paul 	/* now program new ones */
602a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
603a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
604a94100faSBill Paul 			continue;
6050e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6060e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
607a94100faSBill Paul 		if (h < 32)
608a94100faSBill Paul 			hashes[0] |= (1 << h);
609a94100faSBill Paul 		else
610a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
611a94100faSBill Paul 		mcnt++;
612a94100faSBill Paul 	}
613a94100faSBill Paul 
614a94100faSBill Paul 	if (mcnt)
615a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
616a94100faSBill Paul 	else
617a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
618a94100faSBill Paul 
619a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
620a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
621a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
622a94100faSBill Paul }
623a94100faSBill Paul 
624a94100faSBill Paul static void
625a94100faSBill Paul re_reset(sc)
626a94100faSBill Paul 	struct rl_softc		*sc;
627a94100faSBill Paul {
628a94100faSBill Paul 	register int		i;
629a94100faSBill Paul 
63097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
63197b9d4baSJohn-Mark Gurney 
632a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
633a94100faSBill Paul 
634a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
635a94100faSBill Paul 		DELAY(10);
636a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
637a94100faSBill Paul 			break;
638a94100faSBill Paul 	}
639a94100faSBill Paul 	if (i == RL_TIMEOUT)
640a94100faSBill Paul 		printf("re%d: reset never completed!\n", sc->rl_unit);
641a94100faSBill Paul 
642a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
643a94100faSBill Paul }
644a94100faSBill Paul 
645a94100faSBill Paul /*
646a94100faSBill Paul  * The following routine is designed to test for a defect on some
647a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
648a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
649a94100faSBill Paul  * should be pulled high. The result of this defect is that the
650a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
651a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
652a94100faSBill Paul  * because the 64-bit data lines aren't connected.
653a94100faSBill Paul  *
654a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
655a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
656a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
657a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
658a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
659a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
660a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
661a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
662a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
663a94100faSBill Paul  */
664a94100faSBill Paul 
665a94100faSBill Paul static int
666a94100faSBill Paul re_diag(sc)
667a94100faSBill Paul 	struct rl_softc		*sc;
668a94100faSBill Paul {
669a94100faSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
670a94100faSBill Paul 	struct mbuf		*m0;
671a94100faSBill Paul 	struct ether_header	*eh;
672a94100faSBill Paul 	struct rl_desc		*cur_rx;
673a94100faSBill Paul 	u_int16_t		status;
674a94100faSBill Paul 	u_int32_t		rxstat;
675a94100faSBill Paul 	int			total_len, i, error = 0;
676a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
677a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
678a94100faSBill Paul 
679a94100faSBill Paul 	/* Allocate a single mbuf */
680a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
681a94100faSBill Paul 	if (m0 == NULL)
682a94100faSBill Paul 		return (ENOBUFS);
683a94100faSBill Paul 
68497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
68597b9d4baSJohn-Mark Gurney 
686a94100faSBill Paul 	/*
687a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
688a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
689a94100faSBill Paul 	 * following special functions:
690a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
691a94100faSBill Paul 	 * - Enables digital loopback mode
692a94100faSBill Paul 	 * - Leaves interrupts turned off
693a94100faSBill Paul 	 */
694a94100faSBill Paul 
695a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
696a94100faSBill Paul 	sc->rl_testmode = 1;
69797b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
698804af9a1SBill Paul 	re_stop(sc);
699804af9a1SBill Paul 	DELAY(100000);
70097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
701a94100faSBill Paul 
702a94100faSBill Paul 	/* Put some data in the mbuf */
703a94100faSBill Paul 
704a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
705a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
706a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
707a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
708a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
709a94100faSBill Paul 
7107cae6651SBill Paul 	/*
7117cae6651SBill Paul 	 * Queue the packet, start transmission.
7127cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7137cae6651SBill Paul 	 */
714a94100faSBill Paul 
715abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
71697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
7177cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
71897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
719a94100faSBill Paul 	m0 = NULL;
720a94100faSBill Paul 
721a94100faSBill Paul 	/* Wait for it to propagate through the chip */
722a94100faSBill Paul 
723abc8ff44SBill Paul 	DELAY(100000);
724a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
725a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
726abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
727abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
728a94100faSBill Paul 			break;
729a94100faSBill Paul 		DELAY(10);
730a94100faSBill Paul 	}
731a94100faSBill Paul 
732a94100faSBill Paul 	if (i == RL_TIMEOUT) {
733a94100faSBill Paul 		printf("re%d: diagnostic failed, failed to receive packet "
734a94100faSBill Paul 		    "in loopback mode\n", sc->rl_unit);
735a94100faSBill Paul 		error = EIO;
736a94100faSBill Paul 		goto done;
737a94100faSBill Paul 	}
738a94100faSBill Paul 
739a94100faSBill Paul 	/*
740a94100faSBill Paul 	 * The packet should have been dumped into the first
741a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
742a94100faSBill Paul 	 */
743a94100faSBill Paul 
744a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
745a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
746a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
747a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
748a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0],
749a94100faSBill Paul 	    BUS_DMASYNC_POSTWRITE);
750a94100faSBill Paul 	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
751a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0]);
752a94100faSBill Paul 
753a94100faSBill Paul 	m0 = sc->rl_ldata.rl_rx_mbuf[0];
754a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
755a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
756a94100faSBill Paul 
757a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
758a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
759a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
760a94100faSBill Paul 
761a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
762a94100faSBill Paul 		printf("re%d: diagnostic failed, received short packet\n",
763a94100faSBill Paul 		    sc->rl_unit);
764a94100faSBill Paul 		error = EIO;
765a94100faSBill Paul 		goto done;
766a94100faSBill Paul 	}
767a94100faSBill Paul 
768a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
769a94100faSBill Paul 
770a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
771a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
772a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
773a94100faSBill Paul 		printf("re%d: WARNING, DMA FAILURE!\n", sc->rl_unit);
774a94100faSBill Paul 		printf("re%d: expected TX data: %6D/%6D/0x%x\n", sc->rl_unit,
775a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
776a94100faSBill Paul 		printf("re%d: received RX data: %6D/%6D/0x%x\n", sc->rl_unit,
777a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
778a94100faSBill Paul 		    ntohs(eh->ether_type));
779a94100faSBill Paul 		printf("re%d: You may have a defective 32-bit NIC plugged "
780a94100faSBill Paul 		    "into a 64-bit PCI slot.\n", sc->rl_unit);
781a94100faSBill Paul 		printf("re%d: Please re-install the NIC in a 32-bit slot "
782a94100faSBill Paul 		    "for proper operation.\n", sc->rl_unit);
783a94100faSBill Paul 		printf("re%d: Read the re(4) man page for more details.\n",
784a94100faSBill Paul 		    sc->rl_unit);
785a94100faSBill Paul 		error = EIO;
786a94100faSBill Paul 	}
787a94100faSBill Paul 
788a94100faSBill Paul done:
789a94100faSBill Paul 	/* Turn interface off, release resources */
790a94100faSBill Paul 
791a94100faSBill Paul 	sc->rl_testmode = 0;
792a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
793a94100faSBill Paul 	re_stop(sc);
794a94100faSBill Paul 	if (m0 != NULL)
795a94100faSBill Paul 		m_freem(m0);
796a94100faSBill Paul 
79797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79897b9d4baSJohn-Mark Gurney 
799a94100faSBill Paul 	return (error);
800a94100faSBill Paul }
801a94100faSBill Paul 
802a94100faSBill Paul /*
803a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
804a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
805a94100faSBill Paul  */
806a94100faSBill Paul static int
807a94100faSBill Paul re_probe(dev)
808a94100faSBill Paul 	device_t		dev;
809a94100faSBill Paul {
810a94100faSBill Paul 	struct rl_type		*t;
811a94100faSBill Paul 	struct rl_softc		*sc;
812a94100faSBill Paul 	int			rid;
813a94100faSBill Paul 	u_int32_t		hwrev;
814a94100faSBill Paul 
815a94100faSBill Paul 	t = re_devs;
816a94100faSBill Paul 	sc = device_get_softc(dev);
817a94100faSBill Paul 
818a94100faSBill Paul 	while (t->rl_name != NULL) {
819a94100faSBill Paul 		if ((pci_get_vendor(dev) == t->rl_vid) &&
820a94100faSBill Paul 		    (pci_get_device(dev) == t->rl_did)) {
821a94100faSBill Paul 
822a94100faSBill Paul 			/*
823a94100faSBill Paul 			 * Temporarily map the I/O space
824a94100faSBill Paul 			 * so we can read the chip ID register.
825a94100faSBill Paul 			 */
826a94100faSBill Paul 			rid = RL_RID;
8275f96beb9SNate Lawson 			sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
8285f96beb9SNate Lawson 			    RF_ACTIVE);
829a94100faSBill Paul 			if (sc->rl_res == NULL) {
830a94100faSBill Paul 				device_printf(dev,
831a94100faSBill Paul 				    "couldn't map ports/memory\n");
832a94100faSBill Paul 				return (ENXIO);
833a94100faSBill Paul 			}
834a94100faSBill Paul 			sc->rl_btag = rman_get_bustag(sc->rl_res);
835a94100faSBill Paul 			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
836a94100faSBill Paul 			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
837a94100faSBill Paul 			bus_release_resource(dev, RL_RES,
838a94100faSBill Paul 			    RL_RID, sc->rl_res);
839a94100faSBill Paul 			if (t->rl_basetype == hwrev) {
840a94100faSBill Paul 				device_set_desc(dev, t->rl_name);
841a94100faSBill Paul 				return (0);
842a94100faSBill Paul 			}
843a94100faSBill Paul 		}
844a94100faSBill Paul 		t++;
845a94100faSBill Paul 	}
846a94100faSBill Paul 
847a94100faSBill Paul 	return (ENXIO);
848a94100faSBill Paul }
849a94100faSBill Paul 
850a94100faSBill Paul /*
851a94100faSBill Paul  * This routine takes the segment list provided as the result of
852a94100faSBill Paul  * a bus_dma_map_load() operation and assigns the addresses/lengths
853a94100faSBill Paul  * to RealTek DMA descriptors. This can be called either by the RX
854a94100faSBill Paul  * code or the TX code. In the RX case, we'll probably wind up mapping
855a94100faSBill Paul  * at most one segment. For the TX case, there could be any number of
856a94100faSBill Paul  * segments since TX packets may span multiple mbufs. In either case,
857a94100faSBill Paul  * if the number of segments is larger than the rl_maxsegs limit
858a94100faSBill Paul  * specified by the caller, we abort the mapping operation. Sadly,
859a94100faSBill Paul  * whoever designed the buffer mapping API did not provide a way to
860a94100faSBill Paul  * return an error from here, so we have to fake it a bit.
861a94100faSBill Paul  */
862a94100faSBill Paul 
863a94100faSBill Paul static void
864a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error)
865a94100faSBill Paul 	void			*arg;
866a94100faSBill Paul 	bus_dma_segment_t	*segs;
867a94100faSBill Paul 	int			nseg;
868a94100faSBill Paul 	bus_size_t		mapsize;
869a94100faSBill Paul 	int			error;
870a94100faSBill Paul {
871a94100faSBill Paul 	struct rl_dmaload_arg	*ctx;
872a94100faSBill Paul 	struct rl_desc		*d = NULL;
873a94100faSBill Paul 	int			i = 0, idx;
874a94100faSBill Paul 
875a94100faSBill Paul 	if (error)
876a94100faSBill Paul 		return;
877a94100faSBill Paul 
878a94100faSBill Paul 	ctx = arg;
879a94100faSBill Paul 
880a94100faSBill Paul 	/* Signal error to caller if there's too many segments */
881a94100faSBill Paul 	if (nseg > ctx->rl_maxsegs) {
882a94100faSBill Paul 		ctx->rl_maxsegs = 0;
883a94100faSBill Paul 		return;
884a94100faSBill Paul 	}
885a94100faSBill Paul 
886a94100faSBill Paul 	/*
887a94100faSBill Paul 	 * Map the segment array into descriptors. Note that we set the
888a94100faSBill Paul 	 * start-of-frame and end-of-frame markers for either TX or RX, but
889a94100faSBill Paul 	 * they really only have meaning in the TX case. (In the RX case,
890a94100faSBill Paul 	 * it's the chip that tells us where packets begin and end.)
891a94100faSBill Paul 	 * We also keep track of the end of the ring and set the
892a94100faSBill Paul 	 * end-of-ring bits as needed, and we set the ownership bits
893a94100faSBill Paul 	 * in all except the very first descriptor. (The caller will
894a94100faSBill Paul 	 * set this descriptor later when it start transmission or
895a94100faSBill Paul 	 * reception.)
896a94100faSBill Paul 	 */
897a94100faSBill Paul 	idx = ctx->rl_idx;
89859b5d934SBruce M Simpson 	for (;;) {
899a94100faSBill Paul 		u_int32_t		cmdstat;
900a94100faSBill Paul 		d = &ctx->rl_ring[idx];
901a94100faSBill Paul 		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
902a94100faSBill Paul 			ctx->rl_maxsegs = 0;
903a94100faSBill Paul 			return;
904a94100faSBill Paul 		}
905a94100faSBill Paul 		cmdstat = segs[i].ds_len;
906a94100faSBill Paul 		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
907a94100faSBill Paul 		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
908a94100faSBill Paul 		if (i == 0)
909a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_SOF;
910a94100faSBill Paul 		else
911a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_OWN;
912a94100faSBill Paul 		if (idx == (RL_RX_DESC_CNT - 1))
913a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_EOR;
914a94100faSBill Paul 		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
915a94100faSBill Paul 		i++;
916a94100faSBill Paul 		if (i == nseg)
917a94100faSBill Paul 			break;
918a94100faSBill Paul 		RL_DESC_INC(idx);
919a94100faSBill Paul 	}
920a94100faSBill Paul 
921a94100faSBill Paul 	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
922a94100faSBill Paul 	ctx->rl_maxsegs = nseg;
923a94100faSBill Paul 	ctx->rl_idx = idx;
924a94100faSBill Paul }
925a94100faSBill Paul 
926a94100faSBill Paul /*
927a94100faSBill Paul  * Map a single buffer address.
928a94100faSBill Paul  */
929a94100faSBill Paul 
930a94100faSBill Paul static void
931a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
932a94100faSBill Paul 	void			*arg;
933a94100faSBill Paul 	bus_dma_segment_t	*segs;
934a94100faSBill Paul 	int			nseg;
935a94100faSBill Paul 	int			error;
936a94100faSBill Paul {
937a94100faSBill Paul 	u_int32_t		*addr;
938a94100faSBill Paul 
939a94100faSBill Paul 	if (error)
940a94100faSBill Paul 		return;
941a94100faSBill Paul 
942a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
943a94100faSBill Paul 	addr = arg;
944a94100faSBill Paul 	*addr = segs->ds_addr;
945a94100faSBill Paul }
946a94100faSBill Paul 
947a94100faSBill Paul static int
948a94100faSBill Paul re_allocmem(dev, sc)
949a94100faSBill Paul 	device_t		dev;
950a94100faSBill Paul 	struct rl_softc		*sc;
951a94100faSBill Paul {
952a94100faSBill Paul 	int			error;
953a94100faSBill Paul 	int			nseg;
954a94100faSBill Paul 	int			i;
955a94100faSBill Paul 
956a94100faSBill Paul 	/*
957a94100faSBill Paul 	 * Allocate map for RX mbufs.
958a94100faSBill Paul 	 */
959a94100faSBill Paul 	nseg = 32;
960a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
961a94100faSBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
9626110675fSBill Paul 	    NULL, MCLBYTES * nseg, nseg, MCLBYTES, BUS_DMA_ALLOCNOW,
963a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_mtag);
964a94100faSBill Paul 	if (error) {
965a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
966a94100faSBill Paul 		return (ENOMEM);
967a94100faSBill Paul 	}
968a94100faSBill Paul 
969a94100faSBill Paul 	/*
970a94100faSBill Paul 	 * Allocate map for TX descriptor list.
971a94100faSBill Paul 	 */
972a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
973a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
974a94100faSBill Paul 	    NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
975a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
976a94100faSBill Paul 	if (error) {
977a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
978a94100faSBill Paul 		return (ENOMEM);
979a94100faSBill Paul 	}
980a94100faSBill Paul 
981a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
982a94100faSBill Paul 
983a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
984a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
985a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
986a94100faSBill Paul 	if (error)
987a94100faSBill Paul 		return (ENOMEM);
988a94100faSBill Paul 
989a94100faSBill Paul 	/* Load the map for the TX ring. */
990a94100faSBill Paul 
991a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
992a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
993a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
994a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
995a94100faSBill Paul 
996a94100faSBill Paul 	/* Create DMA maps for TX buffers */
997a94100faSBill Paul 
998a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
999a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1000a94100faSBill Paul 			    &sc->rl_ldata.rl_tx_dmamap[i]);
1001a94100faSBill Paul 		if (error) {
1002a94100faSBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
1003a94100faSBill Paul 			return (ENOMEM);
1004a94100faSBill Paul 		}
1005a94100faSBill Paul 	}
1006a94100faSBill Paul 
1007a94100faSBill Paul 	/*
1008a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1009a94100faSBill Paul 	 */
1010a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1011a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
101261021536SJohn-Mark Gurney 	    NULL, RL_RX_LIST_SZ, 1, RL_RX_LIST_SZ, BUS_DMA_ALLOCNOW,
1013a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1014a94100faSBill Paul 	if (error) {
1015a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1016a94100faSBill Paul 		return (ENOMEM);
1017a94100faSBill Paul 	}
1018a94100faSBill Paul 
1019a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1020a94100faSBill Paul 
1021a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1022a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1023a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1024a94100faSBill Paul 	if (error)
1025a94100faSBill Paul 		return (ENOMEM);
1026a94100faSBill Paul 
1027a94100faSBill Paul 	/* Load the map for the RX ring. */
1028a94100faSBill Paul 
1029a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1030a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
103161021536SJohn-Mark Gurney 	     RL_RX_LIST_SZ, re_dma_map_addr,
1032a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1033a94100faSBill Paul 
1034a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1035a94100faSBill Paul 
1036a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1037a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1038a94100faSBill Paul 			    &sc->rl_ldata.rl_rx_dmamap[i]);
1039a94100faSBill Paul 		if (error) {
1040a94100faSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1041a94100faSBill Paul 			return (ENOMEM);
1042a94100faSBill Paul 		}
1043a94100faSBill Paul 	}
1044a94100faSBill Paul 
1045a94100faSBill Paul 	return (0);
1046a94100faSBill Paul }
1047a94100faSBill Paul 
1048a94100faSBill Paul /*
1049a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1050a94100faSBill Paul  * setup and ethernet/BPF attach.
1051a94100faSBill Paul  */
1052a94100faSBill Paul static int
1053a94100faSBill Paul re_attach(dev)
1054a94100faSBill Paul 	device_t		dev;
1055a94100faSBill Paul {
1056a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1057a94100faSBill Paul 	u_int16_t		as[3];
1058a94100faSBill Paul 	struct rl_softc		*sc;
1059a94100faSBill Paul 	struct ifnet		*ifp;
1060a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1061a94100faSBill Paul 	int			hwrev;
1062a94100faSBill Paul 	u_int16_t		re_did = 0;
1063a94100faSBill Paul 	int			unit, error = 0, rid, i;
1064a94100faSBill Paul 
1065a94100faSBill Paul 	sc = device_get_softc(dev);
1066a94100faSBill Paul 	unit = device_get_unit(dev);
1067a94100faSBill Paul 
1068a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
106997b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1070a94100faSBill Paul 	/*
1071a94100faSBill Paul 	 * Map control/status registers.
1072a94100faSBill Paul 	 */
1073a94100faSBill Paul 	pci_enable_busmaster(dev);
1074a94100faSBill Paul 
1075a94100faSBill Paul 	rid = RL_RID;
10765f96beb9SNate Lawson 	sc->rl_res = bus_alloc_resource_any(dev, RL_RES, &rid,
10775f96beb9SNate Lawson 	    RF_ACTIVE);
1078a94100faSBill Paul 
1079a94100faSBill Paul 	if (sc->rl_res == NULL) {
1080a94100faSBill Paul 		printf ("re%d: couldn't map ports/memory\n", unit);
1081a94100faSBill Paul 		error = ENXIO;
1082a94100faSBill Paul 		goto fail;
1083a94100faSBill Paul 	}
1084a94100faSBill Paul 
1085a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1086a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1087a94100faSBill Paul 
1088a94100faSBill Paul 	/* Allocate interrupt */
1089a94100faSBill Paul 	rid = 0;
10905f96beb9SNate Lawson 	sc->rl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1091a94100faSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
1092a94100faSBill Paul 
1093a94100faSBill Paul 	if (sc->rl_irq == NULL) {
1094a94100faSBill Paul 		printf("re%d: couldn't map interrupt\n", unit);
1095a94100faSBill Paul 		error = ENXIO;
1096a94100faSBill Paul 		goto fail;
1097a94100faSBill Paul 	}
1098a94100faSBill Paul 
1099a94100faSBill Paul 	/* Reset the adapter. */
110097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
1101a94100faSBill Paul 	re_reset(sc);
110297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
1103abc8ff44SBill Paul 
1104abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1105abc8ff44SBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1106abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1107abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1108abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
1109abc8ff44SBill Paul 			break;
1110abc8ff44SBill Paul 		}
1111abc8ff44SBill Paul 		hw_rev++;
1112abc8ff44SBill Paul 	}
1113abc8ff44SBill Paul 
1114abc8ff44SBill Paul 	if (sc->rl_type == RL_8169) {
1115abc8ff44SBill Paul 
1116abc8ff44SBill Paul 		/* Set RX length mask */
1117abc8ff44SBill Paul 
1118abc8ff44SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1119abc8ff44SBill Paul 
1120abc8ff44SBill Paul 		/* Force station address autoload from the EEPROM */
1121abc8ff44SBill Paul 
1122abc8ff44SBill Paul 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_AUTOLOAD);
1123abc8ff44SBill Paul 		for (i = 0; i < RL_TIMEOUT; i++) {
1124abc8ff44SBill Paul 			if (!(CSR_READ_1(sc, RL_EECMD) & RL_EEMODE_AUTOLOAD))
1125abc8ff44SBill Paul 				break;
1126abc8ff44SBill Paul 			DELAY(100);
1127abc8ff44SBill Paul 		}
1128abc8ff44SBill Paul 		if (i == RL_TIMEOUT)
1129abc8ff44SBill Paul 			printf ("re%d: eeprom autoload timed out\n", unit);
1130abc8ff44SBill Paul 
1131abc8ff44SBill Paul 			for (i = 0; i < ETHER_ADDR_LEN; i++)
1132abc8ff44SBill Paul 				eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1133abc8ff44SBill Paul 	} else {
1134abc8ff44SBill Paul 
1135abc8ff44SBill Paul 		/* Set RX length mask */
1136abc8ff44SBill Paul 
1137abc8ff44SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1138abc8ff44SBill Paul 
1139a94100faSBill Paul 		sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
1140a94100faSBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1, 0);
1141a94100faSBill Paul 		if (re_did != 0x8129)
1142a94100faSBill Paul 			sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
1143a94100faSBill Paul 
1144a94100faSBill Paul 		/*
1145a94100faSBill Paul 		 * Get station address from the EEPROM.
1146a94100faSBill Paul 		 */
1147a94100faSBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3, 0);
1148a94100faSBill Paul 		for (i = 0; i < 3; i++) {
1149a94100faSBill Paul 			eaddr[(i * 2) + 0] = as[i] & 0xff;
1150a94100faSBill Paul 			eaddr[(i * 2) + 1] = as[i] >> 8;
1151a94100faSBill Paul 		}
1152abc8ff44SBill Paul 	}
11539bac70b8SBill Paul 
1154a94100faSBill Paul 	sc->rl_unit = unit;
1155a94100faSBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1156a94100faSBill Paul 
1157a94100faSBill Paul 	/*
1158a94100faSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1159a94100faSBill Paul 	 */
1160a94100faSBill Paul #define RL_NSEG_NEW 32
1161a94100faSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1162a94100faSBill Paul 			1, 0,			/* alignment, boundary */
1163a94100faSBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1164a94100faSBill Paul 			BUS_SPACE_MAXADDR,	/* highaddr */
1165a94100faSBill Paul 			NULL, NULL,		/* filter, filterarg */
1166a94100faSBill Paul 			MAXBSIZE, RL_NSEG_NEW,	/* maxsize, nsegments */
1167a94100faSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1168a94100faSBill Paul 			BUS_DMA_ALLOCNOW,	/* flags */
1169a94100faSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1170a94100faSBill Paul 			&sc->rl_parent_tag);
1171a94100faSBill Paul 	if (error)
1172a94100faSBill Paul 		goto fail;
1173a94100faSBill Paul 
1174a94100faSBill Paul 	error = re_allocmem(dev, sc);
1175a94100faSBill Paul 
1176a94100faSBill Paul 	if (error)
1177a94100faSBill Paul 		goto fail;
1178a94100faSBill Paul 
1179a94100faSBill Paul 	/* Do MII setup */
1180a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1181a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1182a94100faSBill Paul 		printf("re%d: MII without any phy!\n", sc->rl_unit);
1183a94100faSBill Paul 		error = ENXIO;
1184a94100faSBill Paul 		goto fail;
1185a94100faSBill Paul 	}
1186a94100faSBill Paul 
1187a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1188a94100faSBill Paul 	ifp->if_softc = sc;
11899bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1190a94100faSBill Paul 	ifp->if_mtu = ETHERMTU;
1191a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1192a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1193a94100faSBill Paul 	ifp->if_capabilities = IFCAP_VLAN_MTU;
1194a94100faSBill Paul 	ifp->if_start = re_start;
1195328b4b87SOlivier Houchard 	ifp->if_hwassist = /*RE_CSUM_FEATURES*/0;
1196a94100faSBill Paul 	ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING;
1197f4ab22c9SRuslan Ermilov #ifdef DEVICE_POLLING
1198f4ab22c9SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
1199f4ab22c9SRuslan Ermilov #endif
1200a94100faSBill Paul 	ifp->if_watchdog = re_watchdog;
1201a94100faSBill Paul 	ifp->if_init = re_init;
1202a94100faSBill Paul 	if (sc->rl_type == RL_8169)
1203a94100faSBill Paul 		ifp->if_baudrate = 1000000000;
1204a94100faSBill Paul 	else
1205a94100faSBill Paul 		ifp->if_baudrate = 100000000;
1206a94100faSBill Paul 	ifp->if_snd.ifq_maxlen = RL_IFQ_MAXLEN;
1207328b4b87SOlivier Houchard 	ifp->if_capenable = ifp->if_capabilities & ~IFCAP_HWCSUM;
1208a94100faSBill Paul 
1209a94100faSBill Paul 	callout_handle_init(&sc->rl_stat_ch);
1210a94100faSBill Paul 
1211a94100faSBill Paul 	/*
1212a94100faSBill Paul 	 * Call MI attach routine.
1213a94100faSBill Paul 	 */
1214a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1215a94100faSBill Paul 
1216a94100faSBill Paul 	/* Perform hardware diagnostic. */
1217a94100faSBill Paul 	error = re_diag(sc);
1218a94100faSBill Paul 
1219a94100faSBill Paul 	if (error) {
1220a94100faSBill Paul 		printf("re%d: attach aborted due to hardware diag failure\n",
1221a94100faSBill Paul 		    unit);
1222a94100faSBill Paul 		ether_ifdetach(ifp);
1223a94100faSBill Paul 		goto fail;
1224a94100faSBill Paul 	}
1225a94100faSBill Paul 
1226a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
122797b9d4baSJohn-Mark Gurney 	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET | INTR_MPSAFE,
1228a94100faSBill Paul 	    re_intr, sc, &sc->rl_intrhand);
1229a94100faSBill Paul 	if (error) {
1230a94100faSBill Paul 		printf("re%d: couldn't set up irq\n", unit);
1231a94100faSBill Paul 		ether_ifdetach(ifp);
1232a94100faSBill Paul 	}
1233a94100faSBill Paul 
1234a94100faSBill Paul fail:
1235a94100faSBill Paul 	if (error)
1236a94100faSBill Paul 		re_detach(dev);
1237a94100faSBill Paul 
1238a94100faSBill Paul 	return (error);
1239a94100faSBill Paul }
1240a94100faSBill Paul 
1241a94100faSBill Paul /*
1242a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1243a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1244a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1245a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1246a94100faSBill Paul  * allocated.
1247a94100faSBill Paul  */
1248a94100faSBill Paul static int
1249a94100faSBill Paul re_detach(dev)
1250a94100faSBill Paul 	device_t		dev;
1251a94100faSBill Paul {
1252a94100faSBill Paul 	struct rl_softc		*sc;
1253a94100faSBill Paul 	struct ifnet		*ifp;
1254a94100faSBill Paul 	int			i;
125597b9d4baSJohn-Mark Gurney 	int			attached;
1256a94100faSBill Paul 
1257a94100faSBill Paul 	sc = device_get_softc(dev);
1258a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1259aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
126097b9d4baSJohn-Mark Gurney 
126197b9d4baSJohn-Mark Gurney 	attached = device_is_attached(dev);
126297b9d4baSJohn-Mark Gurney 	/* These should only be active if attach succeeded */
126397b9d4baSJohn-Mark Gurney 	if (attached)
126497b9d4baSJohn-Mark Gurney 		ether_ifdetach(ifp);
126597b9d4baSJohn-Mark Gurney 
126697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
126797b9d4baSJohn-Mark Gurney #if 0
126897b9d4baSJohn-Mark Gurney 	sc->suspended = 1;
126997b9d4baSJohn-Mark Gurney #endif
1270a94100faSBill Paul 
1271a94100faSBill Paul 	/* These should only be active if attach succeeded */
127297b9d4baSJohn-Mark Gurney 	if (attached) {
1273a94100faSBill Paul 		re_stop(sc);
1274a94100faSBill Paul 		/*
1275a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1276a94100faSBill Paul 		 * still had a BPF descriptor attached to this
127797b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1278a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1279a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1280a94100faSBill Paul 		 * which will try to call re_init() again. This will
1281a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1282a94100faSBill Paul 		 * which will panic the system when the kernel tries
1283a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1284a94100faSBill Paul 		 * anymore.
1285a94100faSBill Paul 		 */
1286a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1287a94100faSBill Paul 	}
1288a94100faSBill Paul 	if (sc->rl_miibus)
1289a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1290a94100faSBill Paul 	bus_generic_detach(dev);
1291a94100faSBill Paul 
129297b9d4baSJohn-Mark Gurney 	/*
129397b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
129497b9d4baSJohn-Mark Gurney 	 * stopped here.
129597b9d4baSJohn-Mark Gurney 	 */
129697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
129797b9d4baSJohn-Mark Gurney 
1298a94100faSBill Paul 	if (sc->rl_intrhand)
1299a94100faSBill Paul 		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1300a94100faSBill Paul 	if (sc->rl_irq)
1301a94100faSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1302a94100faSBill Paul 	if (sc->rl_res)
1303a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1304a94100faSBill Paul 
1305a94100faSBill Paul 
1306a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1307a94100faSBill Paul 
1308a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1309a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1310a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1311a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1312a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1313a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1314a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1315a94100faSBill Paul 	}
1316a94100faSBill Paul 
1317a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1318a94100faSBill Paul 
1319a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1320a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1321a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1322a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1323a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1324a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1325a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1326a94100faSBill Paul 	}
1327a94100faSBill Paul 
1328a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1329a94100faSBill Paul 
1330a94100faSBill Paul 	if (sc->rl_ldata.rl_mtag) {
1331a94100faSBill Paul 		for (i = 0; i < RL_TX_DESC_CNT; i++)
1332a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1333a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
1334a94100faSBill Paul 		for (i = 0; i < RL_RX_DESC_CNT; i++)
1335a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1336a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
1337a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1338a94100faSBill Paul 	}
1339a94100faSBill Paul 
1340a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1341a94100faSBill Paul 
1342a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1343a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1344a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1345a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1346a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1347a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1348a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1349a94100faSBill Paul 	}
1350a94100faSBill Paul 
1351a94100faSBill Paul 	if (sc->rl_parent_tag)
1352a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1353a94100faSBill Paul 
1354a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1355a94100faSBill Paul 
1356a94100faSBill Paul 	return (0);
1357a94100faSBill Paul }
1358a94100faSBill Paul 
1359a94100faSBill Paul static int
1360a94100faSBill Paul re_newbuf(sc, idx, m)
1361a94100faSBill Paul 	struct rl_softc		*sc;
1362a94100faSBill Paul 	int			idx;
1363a94100faSBill Paul 	struct mbuf		*m;
1364a94100faSBill Paul {
1365a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1366a94100faSBill Paul 	struct mbuf		*n = NULL;
1367a94100faSBill Paul 	int			error;
1368a94100faSBill Paul 
1369a94100faSBill Paul 	if (m == NULL) {
1370a94100faSBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1371a94100faSBill Paul 		if (n == NULL)
1372a94100faSBill Paul 			return (ENOBUFS);
1373a94100faSBill Paul 		m = n;
1374a94100faSBill Paul 	} else
1375a94100faSBill Paul 		m->m_data = m->m_ext.ext_buf;
1376a94100faSBill Paul 
1377a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
137822a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
137922a11c96SJohn-Mark Gurney 	/*
138022a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
138122a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
138222a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
138322a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
138422a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
138522a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
138622a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
138722a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
138822a11c96SJohn-Mark Gurney 	 */
138922a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
139022a11c96SJohn-Mark Gurney #endif
1391a94100faSBill Paul 	arg.sc = sc;
1392a94100faSBill Paul 	arg.rl_idx = idx;
1393a94100faSBill Paul 	arg.rl_maxsegs = 1;
1394a94100faSBill Paul 	arg.rl_flags = 0;
1395a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1396a94100faSBill Paul 
1397a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1398a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1399a94100faSBill Paul 	    &arg, BUS_DMA_NOWAIT);
1400a94100faSBill Paul 	if (error || arg.rl_maxsegs != 1) {
1401a94100faSBill Paul 		if (n != NULL)
1402a94100faSBill Paul 			m_freem(n);
1403a94100faSBill Paul 		return (ENOMEM);
1404a94100faSBill Paul 	}
1405a94100faSBill Paul 
1406a94100faSBill Paul 	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1407a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1408a94100faSBill Paul 
1409a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1410a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx],
1411a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1412a94100faSBill Paul 
1413a94100faSBill Paul 	return (0);
1414a94100faSBill Paul }
1415a94100faSBill Paul 
141622a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
141722a11c96SJohn-Mark Gurney static __inline void
141822a11c96SJohn-Mark Gurney re_fixup_rx(m)
141922a11c96SJohn-Mark Gurney 	struct mbuf		*m;
142022a11c96SJohn-Mark Gurney {
142122a11c96SJohn-Mark Gurney 	int                     i;
142222a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
142322a11c96SJohn-Mark Gurney 
142422a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
142522a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
142622a11c96SJohn-Mark Gurney 
142722a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
142822a11c96SJohn-Mark Gurney 		*dst++ = *src++;
142922a11c96SJohn-Mark Gurney 
143022a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
143122a11c96SJohn-Mark Gurney 
143222a11c96SJohn-Mark Gurney 	return;
143322a11c96SJohn-Mark Gurney }
143422a11c96SJohn-Mark Gurney #endif
143522a11c96SJohn-Mark Gurney 
1436a94100faSBill Paul static int
1437a94100faSBill Paul re_tx_list_init(sc)
1438a94100faSBill Paul 	struct rl_softc		*sc;
1439a94100faSBill Paul {
144097b9d4baSJohn-Mark Gurney 
144197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
144297b9d4baSJohn-Mark Gurney 
1443a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1444a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1445a94100faSBill Paul 	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1446a94100faSBill Paul 
1447a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1448a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1449a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1450a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1451a94100faSBill Paul 	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1452a94100faSBill Paul 
1453a94100faSBill Paul 	return (0);
1454a94100faSBill Paul }
1455a94100faSBill Paul 
1456a94100faSBill Paul static int
1457a94100faSBill Paul re_rx_list_init(sc)
1458a94100faSBill Paul 	struct rl_softc		*sc;
1459a94100faSBill Paul {
1460a94100faSBill Paul 	int			i;
1461a94100faSBill Paul 
1462a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1463a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1464a94100faSBill Paul 	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1465a94100faSBill Paul 
1466a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1467a94100faSBill Paul 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1468a94100faSBill Paul 			return (ENOBUFS);
1469a94100faSBill Paul 	}
1470a94100faSBill Paul 
1471a94100faSBill Paul 	/* Flush the RX descriptors */
1472a94100faSBill Paul 
1473a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1474a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1475a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1476a94100faSBill Paul 
1477a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1478a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1479a94100faSBill Paul 
1480a94100faSBill Paul 	return (0);
1481a94100faSBill Paul }
1482a94100faSBill Paul 
1483a94100faSBill Paul /*
1484a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1485a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1486a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1487a94100faSBill Paul  */
1488a94100faSBill Paul static void
1489a94100faSBill Paul re_rxeof(sc)
1490a94100faSBill Paul 	struct rl_softc		*sc;
1491a94100faSBill Paul {
1492a94100faSBill Paul 	struct mbuf		*m;
1493a94100faSBill Paul 	struct ifnet		*ifp;
1494a94100faSBill Paul 	int			i, total_len;
1495a94100faSBill Paul 	struct rl_desc		*cur_rx;
1496a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1497a94100faSBill Paul 
14985120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
14995120abbfSSam Leffler 
1500a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1501a94100faSBill Paul 	i = sc->rl_ldata.rl_rx_prodidx;
1502a94100faSBill Paul 
1503a94100faSBill Paul 	/* Invalidate the descriptor memory */
1504a94100faSBill Paul 
1505a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1506a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1507a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1508a94100faSBill Paul 
1509a94100faSBill Paul 	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i])) {
1510a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1511a94100faSBill Paul 		m = sc->rl_ldata.rl_rx_mbuf[i];
1512a94100faSBill Paul 		total_len = RL_RXBYTES(cur_rx);
1513a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1514a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1515a94100faSBill Paul 
1516a94100faSBill Paul 		/* Invalidate the RX mbuf and unload its map */
1517a94100faSBill Paul 
1518a94100faSBill Paul 		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1519a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i],
1520a94100faSBill Paul 		    BUS_DMASYNC_POSTWRITE);
1521a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1522a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i]);
1523a94100faSBill Paul 
1524a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
152522a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
1526a94100faSBill Paul 			if (sc->rl_head == NULL)
1527a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1528a94100faSBill Paul 			else {
1529a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1530a94100faSBill Paul 				sc->rl_tail->m_next = m;
1531a94100faSBill Paul 				sc->rl_tail = m;
1532a94100faSBill Paul 			}
1533a94100faSBill Paul 			re_newbuf(sc, i, NULL);
1534a94100faSBill Paul 			RL_DESC_INC(i);
1535a94100faSBill Paul 			continue;
1536a94100faSBill Paul 		}
1537a94100faSBill Paul 
1538a94100faSBill Paul 		/*
1539a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1540a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1541a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1542a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1543a94100faSBill Paul 		 * were already used, so to make room for the extra
1544a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1545a94100faSBill Paul 		 * error' bit and shifted the other status bits
1546a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1547a94100faSBill Paul 		 * still in the same places. We have already extracted
1548a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1549a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1550a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1551a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1552a94100faSBill Paul 		 * same format as that of the 8139C+.
1553a94100faSBill Paul 		 */
1554a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1555a94100faSBill Paul 			rxstat >>= 1;
1556a94100faSBill Paul 
155722a11c96SJohn-Mark Gurney 		/*
155822a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
155922a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
156022a11c96SJohn-Mark Gurney 		 */
156122a11c96SJohn-Mark Gurney 		if (rxstat & RL_RDESC_STAT_RXERRSUM && !(total_len > 8191 &&
156222a11c96SJohn-Mark Gurney 		    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)) {
1563a94100faSBill Paul 			ifp->if_ierrors++;
1564a94100faSBill Paul 			/*
1565a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1566a94100faSBill Paul 			 * discard all the pieces.
1567a94100faSBill Paul 			 */
1568a94100faSBill Paul 			if (sc->rl_head != NULL) {
1569a94100faSBill Paul 				m_freem(sc->rl_head);
1570a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1571a94100faSBill Paul 			}
1572a94100faSBill Paul 			re_newbuf(sc, i, m);
1573a94100faSBill Paul 			RL_DESC_INC(i);
1574a94100faSBill Paul 			continue;
1575a94100faSBill Paul 		}
1576a94100faSBill Paul 
1577a94100faSBill Paul 		/*
1578a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1579a94100faSBill Paul 		 * reload the current one.
1580a94100faSBill Paul 		 */
1581a94100faSBill Paul 
1582a94100faSBill Paul 		if (re_newbuf(sc, i, NULL)) {
1583a94100faSBill Paul 			ifp->if_ierrors++;
1584a94100faSBill Paul 			if (sc->rl_head != NULL) {
1585a94100faSBill Paul 				m_freem(sc->rl_head);
1586a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1587a94100faSBill Paul 			}
1588a94100faSBill Paul 			re_newbuf(sc, i, m);
1589a94100faSBill Paul 			RL_DESC_INC(i);
1590a94100faSBill Paul 			continue;
1591a94100faSBill Paul 		}
1592a94100faSBill Paul 
1593a94100faSBill Paul 		RL_DESC_INC(i);
1594a94100faSBill Paul 
1595a94100faSBill Paul 		if (sc->rl_head != NULL) {
159622a11c96SJohn-Mark Gurney 			m->m_len = total_len % RE_RX_DESC_BUFLEN;
159722a11c96SJohn-Mark Gurney 			if (m->m_len == 0)
159822a11c96SJohn-Mark Gurney 				m->m_len = RE_RX_DESC_BUFLEN;
1599a94100faSBill Paul 			/*
1600a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1601a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1602a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1603a94100faSBill Paul 			 * care about anyway.
1604a94100faSBill Paul 			 */
1605a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1606a94100faSBill Paul 				sc->rl_tail->m_len -=
1607a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1608a94100faSBill Paul 				m_freem(m);
1609a94100faSBill Paul 			} else {
1610a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1611a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1612a94100faSBill Paul 				sc->rl_tail->m_next = m;
1613a94100faSBill Paul 			}
1614a94100faSBill Paul 			m = sc->rl_head;
1615a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1616a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1617a94100faSBill Paul 		} else
1618a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1619a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1620a94100faSBill Paul 
162122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
162222a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
162322a11c96SJohn-Mark Gurney #endif
1624a94100faSBill Paul 		ifp->if_ipackets++;
1625a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1626a94100faSBill Paul 
1627a94100faSBill Paul 		/* Do RX checksumming if enabled */
1628a94100faSBill Paul 
1629a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1630a94100faSBill Paul 
1631a94100faSBill Paul 			/* Check IP header checksum */
1632a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1633a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1634a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1635a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1636a94100faSBill Paul 
1637a94100faSBill Paul 			/* Check TCP/UDP checksum */
1638a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1639a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1640a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1641a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1642a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1643a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1644a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1645a94100faSBill Paul 			}
1646a94100faSBill Paul 		}
1647a94100faSBill Paul 
1648a94100faSBill Paul 		if (rxvlan & RL_RDESC_VLANCTL_TAG)
1649a94100faSBill Paul 			VLAN_INPUT_TAG(ifp, m,
1650a94100faSBill Paul 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)), continue);
16515120abbfSSam Leffler 		RL_UNLOCK(sc);
1652a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
16535120abbfSSam Leffler 		RL_LOCK(sc);
1654a94100faSBill Paul 	}
1655a94100faSBill Paul 
1656a94100faSBill Paul 	/* Flush the RX DMA ring */
1657a94100faSBill Paul 
1658a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1659a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1660a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1661a94100faSBill Paul 
1662a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1663a94100faSBill Paul }
1664a94100faSBill Paul 
1665a94100faSBill Paul static void
1666a94100faSBill Paul re_txeof(sc)
1667a94100faSBill Paul 	struct rl_softc		*sc;
1668a94100faSBill Paul {
1669a94100faSBill Paul 	struct ifnet		*ifp;
1670a94100faSBill Paul 	u_int32_t		txstat;
1671a94100faSBill Paul 	int			idx;
1672a94100faSBill Paul 
1673a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1674a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_considx;
1675a94100faSBill Paul 
1676a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1677a94100faSBill Paul 
1678a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1679a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1680a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1681a94100faSBill Paul 
1682a94100faSBill Paul 	while (idx != sc->rl_ldata.rl_tx_prodidx) {
1683a94100faSBill Paul 
1684a94100faSBill Paul 		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1685a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_OWN)
1686a94100faSBill Paul 			break;
1687a94100faSBill Paul 
1688a94100faSBill Paul 		/*
1689a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1690a94100faSBill Paul 		 * in a fragment chain, which also happens to
1691a94100faSBill Paul 		 * be the only place where the TX status bits
1692a94100faSBill Paul 		 * are valid.
1693a94100faSBill Paul 		 */
1694a94100faSBill Paul 
1695a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1696a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1697a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1698a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1699a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[idx]);
1700a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1701a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1702a94100faSBill Paul 				ifp->if_collisions++;
1703a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1704a94100faSBill Paul 				ifp->if_oerrors++;
1705a94100faSBill Paul 			else
1706a94100faSBill Paul 				ifp->if_opackets++;
1707a94100faSBill Paul 		}
1708a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1709a94100faSBill Paul 		RL_DESC_INC(idx);
1710a94100faSBill Paul 	}
1711a94100faSBill Paul 
1712a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1713a94100faSBill Paul 
1714a94100faSBill Paul 	if (idx != sc->rl_ldata.rl_tx_considx) {
1715a94100faSBill Paul 		sc->rl_ldata.rl_tx_considx = idx;
1716a94100faSBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1717a94100faSBill Paul 		ifp->if_timer = 0;
1718a94100faSBill Paul 	}
1719a94100faSBill Paul 
1720a94100faSBill Paul 	/*
1721a94100faSBill Paul 	 * If not all descriptors have been released reaped yet,
1722a94100faSBill Paul 	 * reload the timer so that we will eventually get another
1723a94100faSBill Paul 	 * interrupt that will cause us to re-enter this routine.
1724a94100faSBill Paul 	 * This is done in case the transmitter has gone idle.
1725a94100faSBill Paul 	 */
1726a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
1727a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1728a94100faSBill Paul }
1729a94100faSBill Paul 
1730a94100faSBill Paul static void
1731a94100faSBill Paul re_tick(xsc)
1732a94100faSBill Paul 	void			*xsc;
1733a94100faSBill Paul {
1734a94100faSBill Paul 	struct rl_softc		*sc;
1735a94100faSBill Paul 
1736a94100faSBill Paul 	sc = xsc;
1737a94100faSBill Paul 	RL_LOCK(sc);
173897b9d4baSJohn-Mark Gurney 	re_tick_locked(sc);
173997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
174097b9d4baSJohn-Mark Gurney }
174197b9d4baSJohn-Mark Gurney 
174297b9d4baSJohn-Mark Gurney static void
174397b9d4baSJohn-Mark Gurney re_tick_locked(sc)
174497b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
174597b9d4baSJohn-Mark Gurney {
174697b9d4baSJohn-Mark Gurney 	struct mii_data		*mii;
174797b9d4baSJohn-Mark Gurney 
174897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
174997b9d4baSJohn-Mark Gurney 
1750a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
1751a94100faSBill Paul 
1752a94100faSBill Paul 	mii_tick(mii);
1753a94100faSBill Paul 
1754a94100faSBill Paul 	sc->rl_stat_ch = timeout(re_tick, sc, hz);
1755a94100faSBill Paul }
1756a94100faSBill Paul 
1757a94100faSBill Paul #ifdef DEVICE_POLLING
1758a94100faSBill Paul static void
1759a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1760a94100faSBill Paul {
1761a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1762a94100faSBill Paul 
1763a94100faSBill Paul 	RL_LOCK(sc);
176497b9d4baSJohn-Mark Gurney 	re_poll_locked(ifp, cmd, count);
176597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
176697b9d4baSJohn-Mark Gurney }
176797b9d4baSJohn-Mark Gurney 
176897b9d4baSJohn-Mark Gurney static void
176997b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
177097b9d4baSJohn-Mark Gurney {
177197b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
177297b9d4baSJohn-Mark Gurney 
177397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
177497b9d4baSJohn-Mark Gurney 
1775f4ab22c9SRuslan Ermilov 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
1776f4ab22c9SRuslan Ermilov 		ether_poll_deregister(ifp);
1777f4ab22c9SRuslan Ermilov 		cmd = POLL_DEREGISTER;
1778f4ab22c9SRuslan Ermilov 	}
1779a94100faSBill Paul 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1780a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
178197b9d4baSJohn-Mark Gurney 		return;
1782a94100faSBill Paul 	}
1783a94100faSBill Paul 
1784a94100faSBill Paul 	sc->rxcycles = count;
1785a94100faSBill Paul 	re_rxeof(sc);
1786a94100faSBill Paul 	re_txeof(sc);
1787a94100faSBill Paul 
1788a94100faSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
178997b9d4baSJohn-Mark Gurney 		re_start_locked(ifp);
1790a94100faSBill Paul 
1791a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1792a94100faSBill Paul 		u_int16_t       status;
1793a94100faSBill Paul 
1794a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1795a94100faSBill Paul 		if (status == 0xffff)
179697b9d4baSJohn-Mark Gurney 			return;
1797a94100faSBill Paul 		if (status)
1798a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1799a94100faSBill Paul 
1800a94100faSBill Paul 		/*
1801a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
1802a94100faSBill Paul 		 */
1803a94100faSBill Paul 
1804a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1805a94100faSBill Paul 			re_reset(sc);
180697b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
1807a94100faSBill Paul 		}
1808a94100faSBill Paul 	}
1809a94100faSBill Paul }
1810a94100faSBill Paul #endif /* DEVICE_POLLING */
1811a94100faSBill Paul 
1812a94100faSBill Paul static void
1813a94100faSBill Paul re_intr(arg)
1814a94100faSBill Paul 	void			*arg;
1815a94100faSBill Paul {
1816a94100faSBill Paul 	struct rl_softc		*sc;
1817a94100faSBill Paul 	struct ifnet		*ifp;
1818a94100faSBill Paul 	u_int16_t		status;
1819a94100faSBill Paul 
1820a94100faSBill Paul 	sc = arg;
1821a94100faSBill Paul 
1822a94100faSBill Paul 	RL_LOCK(sc);
182397b9d4baSJohn-Mark Gurney 
1824a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1825a94100faSBill Paul 
182697b9d4baSJohn-Mark Gurney 	if (sc->suspended || !(ifp->if_flags & IFF_UP))
182797b9d4baSJohn-Mark Gurney 		goto done_locked;
18289bac70b8SBill Paul 
1829a94100faSBill Paul #ifdef DEVICE_POLLING
1830a94100faSBill Paul 	if  (ifp->if_flags & IFF_POLLING)
183197b9d4baSJohn-Mark Gurney 		goto done_locked;
1832f4ab22c9SRuslan Ermilov 	if ((ifp->if_capenable & IFCAP_POLLING) &&
1833f4ab22c9SRuslan Ermilov 	    ether_poll_register(re_poll, ifp)) { /* ok, disable interrupts */
1834a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0x0000);
183597b9d4baSJohn-Mark Gurney 		re_poll_locked(ifp, 0, 1);
183697b9d4baSJohn-Mark Gurney 		goto done_locked;
1837a94100faSBill Paul 	}
1838a94100faSBill Paul #endif /* DEVICE_POLLING */
1839a94100faSBill Paul 
1840a94100faSBill Paul 	for (;;) {
1841a94100faSBill Paul 
1842a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1843a94100faSBill Paul 		/* If the card has gone away the read returns 0xffff. */
1844a94100faSBill Paul 		if (status == 0xffff)
1845a94100faSBill Paul 			break;
1846a94100faSBill Paul 		if (status)
1847a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1848a94100faSBill Paul 
1849a94100faSBill Paul 		if ((status & RL_INTRS_CPLUS) == 0)
1850a94100faSBill Paul 			break;
1851a94100faSBill Paul 
185261021536SJohn-Mark Gurney 		if ((status & RL_ISR_RX_OK) ||
185361021536SJohn-Mark Gurney 		    (status & RL_ISR_RX_ERR))
1854a94100faSBill Paul 			re_rxeof(sc);
1855a94100faSBill Paul 
1856a94100faSBill Paul 		if ((status & RL_ISR_TIMEOUT_EXPIRED) ||
1857a94100faSBill Paul 		    (status & RL_ISR_TX_ERR) ||
1858a94100faSBill Paul 		    (status & RL_ISR_TX_DESC_UNAVAIL))
1859a94100faSBill Paul 			re_txeof(sc);
1860a94100faSBill Paul 
1861a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1862a94100faSBill Paul 			re_reset(sc);
186397b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
1864a94100faSBill Paul 		}
1865a94100faSBill Paul 
1866a94100faSBill Paul 		if (status & RL_ISR_LINKCHG) {
1867a94100faSBill Paul 			untimeout(re_tick, sc, sc->rl_stat_ch);
186897b9d4baSJohn-Mark Gurney 			re_tick_locked(sc);
1869a94100faSBill Paul 		}
1870a94100faSBill Paul 	}
1871a94100faSBill Paul 
1872a94100faSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
187397b9d4baSJohn-Mark Gurney 		re_start_locked(ifp);
1874a94100faSBill Paul 
187597b9d4baSJohn-Mark Gurney done_locked:
1876a94100faSBill Paul 	RL_UNLOCK(sc);
1877a94100faSBill Paul }
1878a94100faSBill Paul 
1879a94100faSBill Paul static int
1880a94100faSBill Paul re_encap(sc, m_head, idx)
1881a94100faSBill Paul 	struct rl_softc		*sc;
188280a2a305SJohn-Mark Gurney 	struct mbuf		**m_head;
1883a94100faSBill Paul 	int			*idx;
1884a94100faSBill Paul {
1885a94100faSBill Paul 	struct mbuf		*m_new = NULL;
1886a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1887a94100faSBill Paul 	bus_dmamap_t		map;
1888a94100faSBill Paul 	int			error;
1889a94100faSBill Paul 	struct m_tag		*mtag;
1890a94100faSBill Paul 
189197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
189297b9d4baSJohn-Mark Gurney 
18937cae6651SBill Paul 	if (sc->rl_ldata.rl_tx_free <= 4)
1894a94100faSBill Paul 		return (EFBIG);
1895a94100faSBill Paul 
1896a94100faSBill Paul 	/*
1897a94100faSBill Paul 	 * Set up checksum offload. Note: checksum offload bits must
1898a94100faSBill Paul 	 * appear in all descriptors of a multi-descriptor transmit
189922a11c96SJohn-Mark Gurney 	 * attempt. This is according to testing done with an 8169
190022a11c96SJohn-Mark Gurney 	 * chip. This is a requirement.
1901a94100faSBill Paul 	 */
1902a94100faSBill Paul 
1903a94100faSBill Paul 	arg.rl_flags = 0;
1904a94100faSBill Paul 
190580a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_IP)
1906a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
190780a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_TCP)
1908a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
190980a2a305SJohn-Mark Gurney 	if ((*m_head)->m_pkthdr.csum_flags & CSUM_UDP)
1910a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
1911a94100faSBill Paul 
1912a94100faSBill Paul 	arg.sc = sc;
1913a94100faSBill Paul 	arg.rl_idx = *idx;
1914a94100faSBill Paul 	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
19157cae6651SBill Paul 	if (arg.rl_maxsegs > 4)
19167cae6651SBill Paul 		arg.rl_maxsegs -= 4;
1917a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_tx_list;
1918a94100faSBill Paul 
1919a94100faSBill Paul 	map = sc->rl_ldata.rl_tx_dmamap[*idx];
1920a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
192180a2a305SJohn-Mark Gurney 	    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1922a94100faSBill Paul 
1923a94100faSBill Paul 	if (error && error != EFBIG) {
1924a94100faSBill Paul 		printf("re%d: can't map mbuf (error %d)\n", sc->rl_unit, error);
1925a94100faSBill Paul 		return (ENOBUFS);
1926a94100faSBill Paul 	}
1927a94100faSBill Paul 
1928a94100faSBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
1929a94100faSBill Paul 
1930a94100faSBill Paul 	if (error || arg.rl_maxsegs == 0) {
193180a2a305SJohn-Mark Gurney 		m_new = m_defrag(*m_head, M_DONTWAIT);
1932a94100faSBill Paul 		if (m_new == NULL)
193380a2a305SJohn-Mark Gurney 			return (ENOBUFS);
1934a94100faSBill Paul 		else
193580a2a305SJohn-Mark Gurney 			*m_head = m_new;
1936a94100faSBill Paul 
1937a94100faSBill Paul 		arg.sc = sc;
1938a94100faSBill Paul 		arg.rl_idx = *idx;
1939a94100faSBill Paul 		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
1940a94100faSBill Paul 		arg.rl_ring = sc->rl_ldata.rl_tx_list;
1941a94100faSBill Paul 
1942a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
194380a2a305SJohn-Mark Gurney 		    *m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1944a94100faSBill Paul 		if (error) {
1945a94100faSBill Paul 			printf("re%d: can't map mbuf (error %d)\n",
1946a94100faSBill Paul 			    sc->rl_unit, error);
1947a94100faSBill Paul 			return (EFBIG);
1948a94100faSBill Paul 		}
1949a94100faSBill Paul 	}
1950a94100faSBill Paul 
1951a94100faSBill Paul 	/*
1952a94100faSBill Paul 	 * Insure that the map for this transmission
1953a94100faSBill Paul 	 * is placed at the array index of the last descriptor
195422a11c96SJohn-Mark Gurney 	 * in this chain.  (Swap last and first dmamaps.)
1955a94100faSBill Paul 	 */
1956a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[*idx] =
1957a94100faSBill Paul 	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
1958a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
1959a94100faSBill Paul 
196080a2a305SJohn-Mark Gurney 	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = *m_head;
1961a94100faSBill Paul 	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
1962a94100faSBill Paul 
1963a94100faSBill Paul 	/*
1964a94100faSBill Paul 	 * Set up hardware VLAN tagging. Note: vlan tag info must
1965a94100faSBill Paul 	 * appear in the first descriptor of a multi-descriptor
1966a94100faSBill Paul 	 * transmission attempt.
1967a94100faSBill Paul 	 */
1968a94100faSBill Paul 
196980a2a305SJohn-Mark Gurney 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, *m_head);
1970a94100faSBill Paul 	if (mtag != NULL)
1971a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
1972a94100faSBill Paul 		    htole32(htons(VLAN_TAG_VALUE(mtag)) | RL_TDESC_VLANCTL_TAG);
1973a94100faSBill Paul 
1974a94100faSBill Paul 	/* Transfer ownership of packet to the chip. */
1975a94100faSBill Paul 
1976a94100faSBill Paul 	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
1977a94100faSBill Paul 	    htole32(RL_TDESC_CMD_OWN);
1978a94100faSBill Paul 	if (*idx != arg.rl_idx)
1979a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
1980a94100faSBill Paul 		    htole32(RL_TDESC_CMD_OWN);
1981a94100faSBill Paul 
1982a94100faSBill Paul 	RL_DESC_INC(arg.rl_idx);
1983a94100faSBill Paul 	*idx = arg.rl_idx;
1984a94100faSBill Paul 
1985a94100faSBill Paul 	return (0);
1986a94100faSBill Paul }
1987a94100faSBill Paul 
198897b9d4baSJohn-Mark Gurney static void
198997b9d4baSJohn-Mark Gurney re_start(ifp)
199097b9d4baSJohn-Mark Gurney 	struct ifnet		*ifp;
199197b9d4baSJohn-Mark Gurney {
199297b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
199397b9d4baSJohn-Mark Gurney 
199497b9d4baSJohn-Mark Gurney 	sc = ifp->if_softc;
199597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
199697b9d4baSJohn-Mark Gurney 	re_start_locked(ifp);
199797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
199897b9d4baSJohn-Mark Gurney }
199997b9d4baSJohn-Mark Gurney 
2000a94100faSBill Paul /*
2001a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2002a94100faSBill Paul  */
2003a94100faSBill Paul static void
200497b9d4baSJohn-Mark Gurney re_start_locked(ifp)
2005a94100faSBill Paul 	struct ifnet		*ifp;
2006a94100faSBill Paul {
2007a94100faSBill Paul 	struct rl_softc		*sc;
2008a94100faSBill Paul 	struct mbuf		*m_head = NULL;
2009a94100faSBill Paul 	int			idx;
2010a94100faSBill Paul 
2011a94100faSBill Paul 	sc = ifp->if_softc;
201297b9d4baSJohn-Mark Gurney 
201397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
2014a94100faSBill Paul 
2015a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_prodidx;
2016a94100faSBill Paul 
2017a94100faSBill Paul 	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
2018a94100faSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
2019a94100faSBill Paul 		if (m_head == NULL)
2020a94100faSBill Paul 			break;
2021a94100faSBill Paul 
202280a2a305SJohn-Mark Gurney 		if (re_encap(sc, &m_head, &idx)) {
2023a94100faSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
2024a94100faSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
2025a94100faSBill Paul 			break;
2026a94100faSBill Paul 		}
2027a94100faSBill Paul 
2028a94100faSBill Paul 		/*
2029a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2030a94100faSBill Paul 		 * to him.
2031a94100faSBill Paul 		 */
2032a94100faSBill Paul 		BPF_MTAP(ifp, m_head);
2033a94100faSBill Paul 	}
2034a94100faSBill Paul 
2035a94100faSBill Paul 	/* Flush the TX descriptors */
2036a94100faSBill Paul 
2037a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2038a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2039a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2040a94100faSBill Paul 
2041a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = idx;
2042a94100faSBill Paul 
2043a94100faSBill Paul 	/*
2044a94100faSBill Paul 	 * RealTek put the TX poll request register in a different
2045a94100faSBill Paul 	 * location on the 8169 gigE chip. I don't know why.
2046a94100faSBill Paul 	 */
2047a94100faSBill Paul 
2048a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2049a94100faSBill Paul 		CSR_WRITE_2(sc, RL_GTXSTART, RL_TXSTART_START);
2050a94100faSBill Paul 	else
2051a94100faSBill Paul 		CSR_WRITE_2(sc, RL_TXSTART, RL_TXSTART_START);
2052a94100faSBill Paul 
2053a94100faSBill Paul 	/*
2054a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2055a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2056a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2057a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2058a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2059a94100faSBill Paul 	 * the timer count is reset to 0.
2060a94100faSBill Paul 	 */
2061a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2062a94100faSBill Paul 
2063a94100faSBill Paul 	/*
2064a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2065a94100faSBill Paul 	 */
2066a94100faSBill Paul 	ifp->if_timer = 5;
2067a94100faSBill Paul }
2068a94100faSBill Paul 
2069a94100faSBill Paul static void
2070a94100faSBill Paul re_init(xsc)
2071a94100faSBill Paul 	void			*xsc;
2072a94100faSBill Paul {
2073a94100faSBill Paul 	struct rl_softc		*sc = xsc;
207497b9d4baSJohn-Mark Gurney 
207597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
207697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
207797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
207897b9d4baSJohn-Mark Gurney }
207997b9d4baSJohn-Mark Gurney 
208097b9d4baSJohn-Mark Gurney static void
208197b9d4baSJohn-Mark Gurney re_init_locked(sc)
208297b9d4baSJohn-Mark Gurney 	struct rl_softc		*sc;
208397b9d4baSJohn-Mark Gurney {
2084a94100faSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
2085a94100faSBill Paul 	struct mii_data		*mii;
2086a94100faSBill Paul 	u_int32_t		rxcfg = 0;
2087a94100faSBill Paul 
208897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
208997b9d4baSJohn-Mark Gurney 
2090a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2091a94100faSBill Paul 
2092a94100faSBill Paul 	/*
2093a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2094a94100faSBill Paul 	 */
2095a94100faSBill Paul 	re_stop(sc);
2096a94100faSBill Paul 
2097a94100faSBill Paul 	/*
2098c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2099edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2100c2c6548bSBill Paul 	 * before all others.
2101c2c6548bSBill Paul 	 */
2102c2c6548bSBill Paul 	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2103c2c6548bSBill Paul 	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2104edd03374SBill Paul 	    RL_CPLUSCMD_VLANSTRIP|
2105c2c6548bSBill Paul 	    (ifp->if_capenable & IFCAP_RXCSUM ?
2106c2c6548bSBill Paul 	    RL_CPLUSCMD_RXCSUM_ENB : 0));
2107c2c6548bSBill Paul 
2108c2c6548bSBill Paul 	/*
2109a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2110a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2111a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2112a94100faSBill Paul 	 */
2113a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2114a94100faSBill Paul 	CSR_WRITE_STREAM_4(sc, RL_IDR0,
2115a94100faSBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
2116a94100faSBill Paul 	CSR_WRITE_STREAM_4(sc, RL_IDR4,
2117a94100faSBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
2118a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2119a94100faSBill Paul 
2120a94100faSBill Paul 	/*
2121a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2122a94100faSBill Paul 	 */
2123a94100faSBill Paul 	re_rx_list_init(sc);
2124a94100faSBill Paul 	re_tx_list_init(sc);
2125a94100faSBill Paul 
2126a94100faSBill Paul 	/*
2127a94100faSBill Paul 	 * Enable transmit and receive.
2128a94100faSBill Paul 	 */
2129a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2130a94100faSBill Paul 
2131a94100faSBill Paul 	/*
2132a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2133a94100faSBill Paul 	 */
2134abc8ff44SBill Paul 	if (sc->rl_testmode) {
2135abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2136abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2137abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2138a94100faSBill Paul 		else
2139abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2140abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2141abc8ff44SBill Paul 	} else
2142a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2143a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2144a94100faSBill Paul 
2145a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2146a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2147a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2148a94100faSBill Paul 
2149a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
215061021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_PROMISC)
2151a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
215261021536SJohn-Mark Gurney 	else
2153a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2154a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2155a94100faSBill Paul 
2156a94100faSBill Paul 	/*
2157a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2158a94100faSBill Paul 	 */
215961021536SJohn-Mark Gurney 	if (ifp->if_flags & IFF_BROADCAST)
2160a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
216161021536SJohn-Mark Gurney 	else
2162a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2163a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2164a94100faSBill Paul 
2165a94100faSBill Paul 	/*
2166a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2167a94100faSBill Paul 	 */
2168a94100faSBill Paul 	re_setmulti(sc);
2169a94100faSBill Paul 
2170a94100faSBill Paul #ifdef DEVICE_POLLING
2171a94100faSBill Paul 	/*
2172a94100faSBill Paul 	 * Disable interrupts if we are polling.
2173a94100faSBill Paul 	 */
2174a94100faSBill Paul 	if (ifp->if_flags & IFF_POLLING)
2175a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2176a94100faSBill Paul 	else	/* otherwise ... */
2177a94100faSBill Paul #endif /* DEVICE_POLLING */
2178a94100faSBill Paul 	/*
2179a94100faSBill Paul 	 * Enable interrupts.
2180a94100faSBill Paul 	 */
2181a94100faSBill Paul 	if (sc->rl_testmode)
2182a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2183a94100faSBill Paul 	else
2184a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2185a94100faSBill Paul 
2186a94100faSBill Paul 	/* Set initial TX threshold */
2187a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2188a94100faSBill Paul 
2189a94100faSBill Paul 	/* Start RX/TX process. */
2190a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2191a94100faSBill Paul #ifdef notdef
2192a94100faSBill Paul 	/* Enable receiver and transmitter. */
2193a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2194a94100faSBill Paul #endif
2195a94100faSBill Paul 	/*
2196c2c6548bSBill Paul 	 * Load the addresses of the RX and TX lists into the chip.
2197a94100faSBill Paul 	 */
2198a94100faSBill Paul 
2199a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2200a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2201a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2202a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2203a94100faSBill Paul 
2204a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2205a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2206a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2207a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2208a94100faSBill Paul 
2209a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2210a94100faSBill Paul 
2211a94100faSBill Paul 	/*
2212a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2213a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2214a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2215a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2216a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2217a94100faSBill Paul 	 */
2218a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2219a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2220a94100faSBill Paul 	else
2221a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2222a94100faSBill Paul 
2223a94100faSBill Paul 	/*
2224a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2225a94100faSBill Paul 	 * size so we can receive jumbo frames.
2226a94100faSBill Paul 	 */
2227a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2228a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2229a94100faSBill Paul 
223097b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
2231a94100faSBill Paul 		return;
2232a94100faSBill Paul 
2233a94100faSBill Paul 	mii_mediachg(mii);
2234a94100faSBill Paul 
2235a94100faSBill Paul 	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
2236a94100faSBill Paul 
2237a94100faSBill Paul 	ifp->if_flags |= IFF_RUNNING;
2238a94100faSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
2239a94100faSBill Paul 
2240a94100faSBill Paul 	sc->rl_stat_ch = timeout(re_tick, sc, hz);
2241a94100faSBill Paul }
2242a94100faSBill Paul 
2243a94100faSBill Paul /*
2244a94100faSBill Paul  * Set media options.
2245a94100faSBill Paul  */
2246a94100faSBill Paul static int
2247a94100faSBill Paul re_ifmedia_upd(ifp)
2248a94100faSBill Paul 	struct ifnet		*ifp;
2249a94100faSBill Paul {
2250a94100faSBill Paul 	struct rl_softc		*sc;
2251a94100faSBill Paul 	struct mii_data		*mii;
2252a94100faSBill Paul 
2253a94100faSBill Paul 	sc = ifp->if_softc;
2254a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2255a94100faSBill Paul 	mii_mediachg(mii);
2256a94100faSBill Paul 
2257a94100faSBill Paul 	return (0);
2258a94100faSBill Paul }
2259a94100faSBill Paul 
2260a94100faSBill Paul /*
2261a94100faSBill Paul  * Report current media status.
2262a94100faSBill Paul  */
2263a94100faSBill Paul static void
2264a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2265a94100faSBill Paul 	struct ifnet		*ifp;
2266a94100faSBill Paul 	struct ifmediareq	*ifmr;
2267a94100faSBill Paul {
2268a94100faSBill Paul 	struct rl_softc		*sc;
2269a94100faSBill Paul 	struct mii_data		*mii;
2270a94100faSBill Paul 
2271a94100faSBill Paul 	sc = ifp->if_softc;
2272a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2273a94100faSBill Paul 
2274a94100faSBill Paul 	mii_pollstat(mii);
2275a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2276a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2277a94100faSBill Paul }
2278a94100faSBill Paul 
2279a94100faSBill Paul static int
2280a94100faSBill Paul re_ioctl(ifp, command, data)
2281a94100faSBill Paul 	struct ifnet		*ifp;
2282a94100faSBill Paul 	u_long			command;
2283a94100faSBill Paul 	caddr_t			data;
2284a94100faSBill Paul {
2285a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2286a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2287a94100faSBill Paul 	struct mii_data		*mii;
2288a94100faSBill Paul 	int			error = 0;
2289a94100faSBill Paul 
2290a94100faSBill Paul 	switch (command) {
2291a94100faSBill Paul 	case SIOCSIFMTU:
2292a94100faSBill Paul 		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2293a94100faSBill Paul 			error = EINVAL;
2294a94100faSBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2295a94100faSBill Paul 		break;
2296a94100faSBill Paul 	case SIOCSIFFLAGS:
229797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
229897b9d4baSJohn-Mark Gurney 		if (ifp->if_flags & IFF_UP)
229997b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
230097b9d4baSJohn-Mark Gurney 		else if (ifp->if_flags & IFF_RUNNING)
2301a94100faSBill Paul 			re_stop(sc);
230297b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2303a94100faSBill Paul 		error = 0;
2304a94100faSBill Paul 		break;
2305a94100faSBill Paul 	case SIOCADDMULTI:
2306a94100faSBill Paul 	case SIOCDELMULTI:
230797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
2308a94100faSBill Paul 		re_setmulti(sc);
230997b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
2310a94100faSBill Paul 		error = 0;
2311a94100faSBill Paul 		break;
2312a94100faSBill Paul 	case SIOCGIFMEDIA:
2313a94100faSBill Paul 	case SIOCSIFMEDIA:
2314a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2315a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2316a94100faSBill Paul 		break;
2317a94100faSBill Paul 	case SIOCSIFCAP:
231825fbb2c3SYaroslav Tykhiy 		ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_POLLING);
231925fbb2c3SYaroslav Tykhiy 		ifp->if_capenable |=
232025fbb2c3SYaroslav Tykhiy 		    ifr->ifr_reqcap & (IFCAP_HWCSUM | IFCAP_POLLING);
2321a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_TXCSUM)
2322a94100faSBill Paul 			ifp->if_hwassist = RE_CSUM_FEATURES;
2323a94100faSBill Paul 		else
2324a94100faSBill Paul 			ifp->if_hwassist = 0;
2325a94100faSBill Paul 		if (ifp->if_flags & IFF_RUNNING)
2326a94100faSBill Paul 			re_init(sc);
2327a94100faSBill Paul 		break;
2328a94100faSBill Paul 	default:
2329a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2330a94100faSBill Paul 		break;
2331a94100faSBill Paul 	}
2332a94100faSBill Paul 
2333a94100faSBill Paul 	return (error);
2334a94100faSBill Paul }
2335a94100faSBill Paul 
2336a94100faSBill Paul static void
2337a94100faSBill Paul re_watchdog(ifp)
2338a94100faSBill Paul 	struct ifnet		*ifp;
2339a94100faSBill Paul {
2340a94100faSBill Paul 	struct rl_softc		*sc;
2341a94100faSBill Paul 
2342a94100faSBill Paul 	sc = ifp->if_softc;
2343a94100faSBill Paul 	RL_LOCK(sc);
2344a94100faSBill Paul 	printf("re%d: watchdog timeout\n", sc->rl_unit);
2345a94100faSBill Paul 	ifp->if_oerrors++;
2346a94100faSBill Paul 
2347a94100faSBill Paul 	re_txeof(sc);
2348a94100faSBill Paul 	re_rxeof(sc);
234997b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
2350a94100faSBill Paul 
2351a94100faSBill Paul 	RL_UNLOCK(sc);
2352a94100faSBill Paul }
2353a94100faSBill Paul 
2354a94100faSBill Paul /*
2355a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2356a94100faSBill Paul  * RX and TX lists.
2357a94100faSBill Paul  */
2358a94100faSBill Paul static void
2359a94100faSBill Paul re_stop(sc)
2360a94100faSBill Paul 	struct rl_softc		*sc;
2361a94100faSBill Paul {
2362a94100faSBill Paul 	register int		i;
2363a94100faSBill Paul 	struct ifnet		*ifp;
2364a94100faSBill Paul 
236597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
236697b9d4baSJohn-Mark Gurney 
2367a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
2368a94100faSBill Paul 	ifp->if_timer = 0;
2369a94100faSBill Paul 
2370a94100faSBill Paul 	untimeout(re_tick, sc, sc->rl_stat_ch);
2371a94100faSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2372a94100faSBill Paul #ifdef DEVICE_POLLING
2373a94100faSBill Paul 	ether_poll_deregister(ifp);
2374a94100faSBill Paul #endif /* DEVICE_POLLING */
2375a94100faSBill Paul 
2376a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2377a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2378a94100faSBill Paul 
2379a94100faSBill Paul 	if (sc->rl_head != NULL) {
2380a94100faSBill Paul 		m_freem(sc->rl_head);
2381a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2382a94100faSBill Paul 	}
2383a94100faSBill Paul 
2384a94100faSBill Paul 	/* Free the TX list buffers. */
2385a94100faSBill Paul 
2386a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2387a94100faSBill Paul 		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2388a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2389a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
2390a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2391a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2392a94100faSBill Paul 		}
2393a94100faSBill Paul 	}
2394a94100faSBill Paul 
2395a94100faSBill Paul 	/* Free the RX list buffers. */
2396a94100faSBill Paul 
2397a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2398a94100faSBill Paul 		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2399a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2400a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
2401a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2402a94100faSBill Paul 			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2403a94100faSBill Paul 		}
2404a94100faSBill Paul 	}
2405a94100faSBill Paul }
2406a94100faSBill Paul 
2407a94100faSBill Paul /*
2408a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2409a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2410a94100faSBill Paul  * resume.
2411a94100faSBill Paul  */
2412a94100faSBill Paul static int
2413a94100faSBill Paul re_suspend(dev)
2414a94100faSBill Paul 	device_t		dev;
2415a94100faSBill Paul {
2416a94100faSBill Paul 	struct rl_softc		*sc;
2417a94100faSBill Paul 
2418a94100faSBill Paul 	sc = device_get_softc(dev);
2419a94100faSBill Paul 
242097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2421a94100faSBill Paul 	re_stop(sc);
2422a94100faSBill Paul 	sc->suspended = 1;
242397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2424a94100faSBill Paul 
2425a94100faSBill Paul 	return (0);
2426a94100faSBill Paul }
2427a94100faSBill Paul 
2428a94100faSBill Paul /*
2429a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2430a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2431a94100faSBill Paul  * appropriate.
2432a94100faSBill Paul  */
2433a94100faSBill Paul static int
2434a94100faSBill Paul re_resume(dev)
2435a94100faSBill Paul 	device_t		dev;
2436a94100faSBill Paul {
2437a94100faSBill Paul 	struct rl_softc		*sc;
2438a94100faSBill Paul 	struct ifnet		*ifp;
2439a94100faSBill Paul 
2440a94100faSBill Paul 	sc = device_get_softc(dev);
244197b9d4baSJohn-Mark Gurney 
244297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
244397b9d4baSJohn-Mark Gurney 
2444a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
2445a94100faSBill Paul 
2446a94100faSBill Paul 	/* reinitialize interface if necessary */
2447a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
244897b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
2449a94100faSBill Paul 
2450a94100faSBill Paul 	sc->suspended = 0;
245197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2452a94100faSBill Paul 
2453a94100faSBill Paul 	return (0);
2454a94100faSBill Paul }
2455a94100faSBill Paul 
2456a94100faSBill Paul /*
2457a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2458a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2459a94100faSBill Paul  */
2460a94100faSBill Paul static void
2461a94100faSBill Paul re_shutdown(dev)
2462a94100faSBill Paul 	device_t		dev;
2463a94100faSBill Paul {
2464a94100faSBill Paul 	struct rl_softc		*sc;
2465a94100faSBill Paul 
2466a94100faSBill Paul 	sc = device_get_softc(dev);
2467a94100faSBill Paul 
246897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
2469a94100faSBill Paul 	re_stop(sc);
247097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
2471a94100faSBill Paul }
2472