xref: /freebsd/sys/dev/re/if_re.c (revision a94100fa9b9ddccb74164cb9407e69fba557dbac)
1a94100faSBill Paul /*
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 
33a94100faSBill Paul /*
34a94100faSBill Paul  * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
35a94100faSBill Paul  *
36a94100faSBill Paul  * Written by Bill Paul <wpaul@windriver.com>
37a94100faSBill Paul  * Senior Networking Software Engineer
38a94100faSBill Paul  * Wind River Systems
39a94100faSBill Paul  */
40a94100faSBill Paul 
41a94100faSBill Paul /*
42a94100faSBill Paul  * This driver is designed to support RealTek's next generation of
43a94100faSBill Paul  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
44a94100faSBill Paul  * four devices in this family: the RTL8139C+, the RTL8169, the RTL8169S
45a94100faSBill Paul  * and the RTL8110S.
46a94100faSBill Paul  *
47a94100faSBill Paul  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
48a94100faSBill Paul  * with the older 8139 family, however it also supports a special
49a94100faSBill Paul  * C+ mode of operation that provides several new performance enhancing
50a94100faSBill Paul  * features. These include:
51a94100faSBill Paul  *
52a94100faSBill Paul  *	o Descriptor based DMA mechanism. Each descriptor represents
53a94100faSBill Paul  *	  a single packet fragment. Data buffers may be aligned on
54a94100faSBill Paul  *	  any byte boundary.
55a94100faSBill Paul  *
56a94100faSBill Paul  *	o 64-bit DMA
57a94100faSBill Paul  *
58a94100faSBill Paul  *	o TCP/IP checksum offload for both RX and TX
59a94100faSBill Paul  *
60a94100faSBill Paul  *	o High and normal priority transmit DMA rings
61a94100faSBill Paul  *
62a94100faSBill Paul  *	o VLAN tag insertion and extraction
63a94100faSBill Paul  *
64a94100faSBill Paul  *	o TCP large send (segmentation offload)
65a94100faSBill Paul  *
66a94100faSBill Paul  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
67a94100faSBill Paul  * programming API is fairly straightforward. The RX filtering, EEPROM
68a94100faSBill Paul  * access and PHY access is the same as it is on the older 8139 series
69a94100faSBill Paul  * chips.
70a94100faSBill Paul  *
71a94100faSBill Paul  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
72a94100faSBill Paul  * same programming API and feature set as the 8139C+ with the following
73a94100faSBill Paul  * differences and additions:
74a94100faSBill Paul  *
75a94100faSBill Paul  *	o 1000Mbps mode
76a94100faSBill Paul  *
77a94100faSBill Paul  *	o Jumbo frames
78a94100faSBill Paul  *
79a94100faSBill Paul  * 	o GMII and TBI ports/registers for interfacing with copper
80a94100faSBill Paul  *	  or fiber PHYs
81a94100faSBill Paul  *
82a94100faSBill Paul  *      o RX and TX DMA rings can have up to 1024 descriptors
83a94100faSBill Paul  *        (the 8139C+ allows a maximum of 64)
84a94100faSBill Paul  *
85a94100faSBill Paul  *	o Slight differences in register layout from the 8139C+
86a94100faSBill Paul  *
87a94100faSBill Paul  * The TX start and timer interrupt registers are at different locations
88a94100faSBill Paul  * on the 8169 than they are on the 8139C+. Also, the status word in the
89a94100faSBill Paul  * RX descriptor has a slightly different bit layout. The 8169 does not
90a94100faSBill Paul  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
91a94100faSBill Paul  * copper gigE PHY.
92a94100faSBill Paul  *
93a94100faSBill Paul  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
94a94100faSBill Paul  * (the 'S' stands for 'single-chip'). These devices have the same
95a94100faSBill Paul  * programming API as the older 8169, but also have some vendor-specific
96a94100faSBill Paul  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
97a94100faSBill Paul  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
98a94100faSBill Paul  *
99a94100faSBill Paul  * This driver takes advantage of the RX and TX checksum offload and
100a94100faSBill Paul  * VLAN tag insertion/extraction features. It also implements TX
101a94100faSBill Paul  * interrupt moderation using the timer interrupt registers, which
102a94100faSBill Paul  * significantly reduces TX interrupt load. There is also support
103a94100faSBill Paul  * for jumbo frames, however the 8169/8169S/8110S can not transmit
104a94100faSBill Paul  * jumbo frames larger than 7.5K, so the max MTU possible with this
105a94100faSBill Paul  * driver is 7500 bytes.
106a94100faSBill Paul  */
107a94100faSBill Paul 
108a94100faSBill Paul #include <sys/cdefs.h>
109a94100faSBill Paul __FBSDID("$FreeBSD$");
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>
117a94100faSBill Paul #include <sys/kernel.h>
118a94100faSBill Paul #include <sys/socket.h>
119a94100faSBill Paul 
120a94100faSBill Paul #include <net/if.h>
121a94100faSBill Paul #include <net/if_arp.h>
122a94100faSBill Paul #include <net/ethernet.h>
123a94100faSBill Paul #include <net/if_dl.h>
124a94100faSBill Paul #include <net/if_media.h>
125a94100faSBill Paul #include <net/if_vlan_var.h>
126a94100faSBill Paul 
127a94100faSBill Paul #include <net/bpf.h>
128a94100faSBill Paul 
129a94100faSBill Paul #include <machine/bus_pio.h>
130a94100faSBill Paul #include <machine/bus_memio.h>
131a94100faSBill Paul #include <machine/bus.h>
132a94100faSBill Paul #include <machine/resource.h>
133a94100faSBill Paul #include <sys/bus.h>
134a94100faSBill Paul #include <sys/rman.h>
135a94100faSBill Paul 
136a94100faSBill Paul #include <dev/mii/mii.h>
137a94100faSBill Paul #include <dev/mii/miivar.h>
138a94100faSBill Paul 
139a94100faSBill Paul #include <dev/pci/pcireg.h>
140a94100faSBill Paul #include <dev/pci/pcivar.h>
141a94100faSBill Paul 
142a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
143a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
144a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
145a94100faSBill Paul 
146a94100faSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
147a94100faSBill Paul #include "miibus_if.h"
148a94100faSBill Paul 
149a94100faSBill Paul /*
150a94100faSBill Paul  * Default to using PIO access for this driver.
151a94100faSBill Paul  */
152a94100faSBill Paul #define RE_USEIOSPACE
153a94100faSBill Paul 
154a94100faSBill Paul #include <pci/if_rlreg.h>
155a94100faSBill Paul 
156a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
157a94100faSBill Paul 
158a94100faSBill Paul /*
159a94100faSBill Paul  * Various supported device vendors/types and their names.
160a94100faSBill Paul  */
161a94100faSBill Paul static struct rl_type re_devs[] = {
162a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8139, RL_HWREV_8139CPLUS,
163a94100faSBill Paul 		"RealTek 8139C+ 10/100BaseTX" },
164a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8169,
165a94100faSBill Paul 		"RealTek 8169 Gigabit Ethernet" },
166a94100faSBill Paul 	{ RT_VENDORID, RT_DEVICEID_8169, RL_HWREV_8110,
167a94100faSBill Paul 		"RealTek 8169S/8110S Single-chip Gigabit Ethernet" },
168a94100faSBill Paul 	{ 0, 0, 0, NULL }
169a94100faSBill Paul };
170a94100faSBill Paul 
171a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
172a94100faSBill Paul 	{ RL_HWREV_8139, RL_8139,  "" },
173a94100faSBill Paul 	{ RL_HWREV_8139A, RL_8139, "A" },
174a94100faSBill Paul 	{ RL_HWREV_8139AG, RL_8139, "A-G" },
175a94100faSBill Paul 	{ RL_HWREV_8139B, RL_8139, "B" },
176a94100faSBill Paul 	{ RL_HWREV_8130, RL_8139, "8130" },
177a94100faSBill Paul 	{ RL_HWREV_8139C, RL_8139, "C" },
178a94100faSBill Paul 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C" },
179a94100faSBill Paul 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+"},
180a94100faSBill Paul 	{ RL_HWREV_8169, RL_8169, "8169"},
181a94100faSBill Paul 	{ RL_HWREV_8110, RL_8169, "8169S/8110S"},
182a94100faSBill Paul 	{ RL_HWREV_8100, RL_8139, "8100"},
183a94100faSBill Paul 	{ RL_HWREV_8101, RL_8139, "8101"},
184a94100faSBill Paul 	{ 0, 0, NULL }
185a94100faSBill Paul };
186a94100faSBill Paul 
187a94100faSBill Paul static int re_probe		(device_t);
188a94100faSBill Paul static int re_attach		(device_t);
189a94100faSBill Paul static int re_detach		(device_t);
190a94100faSBill Paul 
191a94100faSBill Paul static int re_encap		(struct rl_softc *, struct mbuf *, int *);
192a94100faSBill Paul 
193a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
194a94100faSBill Paul static void re_dma_map_desc	(void *, bus_dma_segment_t *, int,
195a94100faSBill Paul 				    bus_size_t, int);
196a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
197a94100faSBill Paul static int re_newbuf		(struct rl_softc *, int, struct mbuf *);
198a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
199a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
200a94100faSBill Paul static void re_rxeof		(struct rl_softc *);
201a94100faSBill Paul static void re_txeof		(struct rl_softc *);
202a94100faSBill Paul static void re_intr		(void *);
203a94100faSBill Paul static void re_tick		(void *);
204a94100faSBill Paul static void re_start		(struct ifnet *);
205a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
206a94100faSBill Paul static void re_init		(void *);
207a94100faSBill Paul static void re_stop		(struct rl_softc *);
208a94100faSBill Paul static void re_watchdog		(struct ifnet *);
209a94100faSBill Paul static int re_suspend		(device_t);
210a94100faSBill Paul static int re_resume		(device_t);
211a94100faSBill Paul static void re_shutdown		(device_t);
212a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
213a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
214a94100faSBill Paul 
215a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
216a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
217a94100faSBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int, int);
218a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
219a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
220a94100faSBill Paul 
221a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
222a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
223a94100faSBill Paul static void re_miibus_statchg	(device_t);
224a94100faSBill Paul 
225a94100faSBill Paul static u_int8_t re_calchash	(caddr_t);
226a94100faSBill Paul static void re_setmulti		(struct rl_softc *);
227a94100faSBill Paul static void re_reset		(struct rl_softc *);
228a94100faSBill Paul 
229a94100faSBill Paul static int re_diag		(struct rl_softc *);
230a94100faSBill Paul 
231a94100faSBill Paul #ifdef RE_USEIOSPACE
232a94100faSBill Paul #define RL_RES			SYS_RES_IOPORT
233a94100faSBill Paul #define RL_RID			RL_PCI_LOIO
234a94100faSBill Paul #else
235a94100faSBill Paul #define RL_RES			SYS_RES_MEMORY
236a94100faSBill Paul #define RL_RID			RL_PCI_LOMEM
237a94100faSBill Paul #endif
238a94100faSBill Paul 
239a94100faSBill Paul static device_method_t re_methods[] = {
240a94100faSBill Paul 	/* Device interface */
241a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
242a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
243a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
244a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
245a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
246a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
247a94100faSBill Paul 
248a94100faSBill Paul 	/* bus interface */
249a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
250a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
251a94100faSBill Paul 
252a94100faSBill Paul 	/* MII interface */
253a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
254a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
255a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
256a94100faSBill Paul 
257a94100faSBill Paul 	{ 0, 0 }
258a94100faSBill Paul };
259a94100faSBill Paul 
260a94100faSBill Paul static driver_t re_driver = {
261a94100faSBill Paul 	"re",
262a94100faSBill Paul 	re_methods,
263a94100faSBill Paul 	sizeof(struct rl_softc)
264a94100faSBill Paul };
265a94100faSBill Paul 
266a94100faSBill Paul static devclass_t re_devclass;
267a94100faSBill Paul 
268a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
269a94100faSBill Paul DRIVER_MODULE(re, cardbus, re_driver, re_devclass, 0, 0);
270a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
271a94100faSBill Paul 
272a94100faSBill Paul #define EE_SET(x)					\
273a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
274a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
275a94100faSBill Paul 
276a94100faSBill Paul #define EE_CLR(x)					\
277a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
278a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
279a94100faSBill Paul 
280a94100faSBill Paul /*
281a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
282a94100faSBill Paul  */
283a94100faSBill Paul static void
284a94100faSBill Paul re_eeprom_putbyte(sc, addr)
285a94100faSBill Paul 	struct rl_softc		*sc;
286a94100faSBill Paul 	int			addr;
287a94100faSBill Paul {
288a94100faSBill Paul 	register int		d, i;
289a94100faSBill Paul 
290a94100faSBill Paul 	d = addr | sc->rl_eecmd_read;
291a94100faSBill Paul 
292a94100faSBill Paul 	/*
293a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
294a94100faSBill Paul 	 */
295a94100faSBill Paul 	for (i = 0x400; i; i >>= 1) {
296a94100faSBill Paul 		if (d & i) {
297a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
298a94100faSBill Paul 		} else {
299a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
300a94100faSBill Paul 		}
301a94100faSBill Paul 		DELAY(100);
302a94100faSBill Paul 		EE_SET(RL_EE_CLK);
303a94100faSBill Paul 		DELAY(150);
304a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
305a94100faSBill Paul 		DELAY(100);
306a94100faSBill Paul 	}
307a94100faSBill Paul 
308a94100faSBill Paul 	return;
309a94100faSBill Paul }
310a94100faSBill Paul 
311a94100faSBill Paul /*
312a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
313a94100faSBill Paul  */
314a94100faSBill Paul static void
315a94100faSBill Paul re_eeprom_getword(sc, addr, dest)
316a94100faSBill Paul 	struct rl_softc		*sc;
317a94100faSBill Paul 	int			addr;
318a94100faSBill Paul 	u_int16_t		*dest;
319a94100faSBill Paul {
320a94100faSBill Paul 	register int		i;
321a94100faSBill Paul 	u_int16_t		word = 0;
322a94100faSBill Paul 
323a94100faSBill Paul 	/* Enter EEPROM access mode. */
324a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
325a94100faSBill Paul 
326a94100faSBill Paul 	/*
327a94100faSBill Paul 	 * Send address of word we want to read.
328a94100faSBill Paul 	 */
329a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
330a94100faSBill Paul 
331a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_PROGRAM|RL_EE_SEL);
332a94100faSBill Paul 
333a94100faSBill Paul 	/*
334a94100faSBill Paul 	 * Start reading bits from EEPROM.
335a94100faSBill Paul 	 */
336a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
337a94100faSBill Paul 		EE_SET(RL_EE_CLK);
338a94100faSBill Paul 		DELAY(100);
339a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
340a94100faSBill Paul 			word |= i;
341a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
342a94100faSBill Paul 		DELAY(100);
343a94100faSBill Paul 	}
344a94100faSBill Paul 
345a94100faSBill Paul 	/* Turn off EEPROM access mode. */
346a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
347a94100faSBill Paul 
348a94100faSBill Paul 	*dest = word;
349a94100faSBill Paul 
350a94100faSBill Paul 	return;
351a94100faSBill Paul }
352a94100faSBill Paul 
353a94100faSBill Paul /*
354a94100faSBill Paul  * Read a sequence of words from the EEPROM.
355a94100faSBill Paul  */
356a94100faSBill Paul static void
357a94100faSBill Paul re_read_eeprom(sc, dest, off, cnt, swap)
358a94100faSBill Paul 	struct rl_softc		*sc;
359a94100faSBill Paul 	caddr_t			dest;
360a94100faSBill Paul 	int			off;
361a94100faSBill Paul 	int			cnt;
362a94100faSBill Paul 	int			swap;
363a94100faSBill Paul {
364a94100faSBill Paul 	int			i;
365a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
366a94100faSBill Paul 
367a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
368a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
369a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
370a94100faSBill Paul 		if (swap)
371a94100faSBill Paul 			*ptr = ntohs(word);
372a94100faSBill Paul 		else
373a94100faSBill Paul 			*ptr = word;
374a94100faSBill Paul 	}
375a94100faSBill Paul 
376a94100faSBill Paul 	return;
377a94100faSBill Paul }
378a94100faSBill Paul 
379a94100faSBill Paul static int
380a94100faSBill Paul re_gmii_readreg(dev, phy, reg)
381a94100faSBill Paul 	device_t		dev;
382a94100faSBill Paul 	int			phy, reg;
383a94100faSBill Paul {
384a94100faSBill Paul 	struct rl_softc		*sc;
385a94100faSBill Paul 	u_int32_t		rval;
386a94100faSBill Paul 	int			i;
387a94100faSBill Paul 
388a94100faSBill Paul 	if (phy != 1)
389a94100faSBill Paul 		return(0);
390a94100faSBill Paul 
391a94100faSBill Paul 	sc = device_get_softc(dev);
392a94100faSBill Paul 
393a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
394a94100faSBill Paul 	DELAY(1000);
395a94100faSBill Paul 
396a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
397a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
398a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
399a94100faSBill Paul 			break;
400a94100faSBill Paul 		DELAY(100);
401a94100faSBill Paul 	}
402a94100faSBill Paul 
403a94100faSBill Paul 	if (i == RL_TIMEOUT) {
404a94100faSBill Paul 		printf ("re%d: PHY read failed\n", sc->rl_unit);
405a94100faSBill Paul 		return (0);
406a94100faSBill Paul 	}
407a94100faSBill Paul 
408a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
409a94100faSBill Paul }
410a94100faSBill Paul 
411a94100faSBill Paul static int
412a94100faSBill Paul re_gmii_writereg(dev, phy, reg, data)
413a94100faSBill Paul 	device_t		dev;
414a94100faSBill Paul 	int			phy, reg, data;
415a94100faSBill Paul {
416a94100faSBill Paul 	struct rl_softc		*sc;
417a94100faSBill Paul 	u_int32_t		rval;
418a94100faSBill Paul 	int			i;
419a94100faSBill Paul 
420a94100faSBill Paul 	if (phy > 0)
421a94100faSBill Paul 		return(0);
422a94100faSBill Paul 
423a94100faSBill Paul 	sc = device_get_softc(dev);
424a94100faSBill Paul 
425a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
426a94100faSBill Paul 	    (data | RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
427a94100faSBill Paul 	DELAY(1000);
428a94100faSBill Paul 
429a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
430a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
431a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
432a94100faSBill Paul 			break;
433a94100faSBill Paul 		DELAY(100);
434a94100faSBill Paul 	}
435a94100faSBill Paul 
436a94100faSBill Paul 	if (i == RL_TIMEOUT) {
437a94100faSBill Paul 		printf ("re%d: PHY write failed\n", sc->rl_unit);
438a94100faSBill Paul 		return (0);
439a94100faSBill Paul 	}
440a94100faSBill Paul 
441a94100faSBill Paul 	return (0);
442a94100faSBill Paul }
443a94100faSBill Paul 
444a94100faSBill Paul static int
445a94100faSBill Paul re_miibus_readreg(dev, phy, reg)
446a94100faSBill Paul 	device_t		dev;
447a94100faSBill Paul 	int			phy, reg;
448a94100faSBill Paul {
449a94100faSBill Paul 	struct rl_softc		*sc;
450a94100faSBill Paul 	u_int16_t		rval = 0;
451a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
452a94100faSBill Paul 
453a94100faSBill Paul 	sc = device_get_softc(dev);
454a94100faSBill Paul 	RL_LOCK(sc);
455a94100faSBill Paul 
456a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
457a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
458a94100faSBill Paul 		RL_UNLOCK(sc);
459a94100faSBill Paul 		return (rval);
460a94100faSBill Paul 	}
461a94100faSBill Paul 
462a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
463a94100faSBill Paul 	if (phy) {
464a94100faSBill Paul 		RL_UNLOCK(sc);
465a94100faSBill Paul 		return(0);
466a94100faSBill Paul 	}
467a94100faSBill Paul 	switch(reg) {
468a94100faSBill Paul 	case MII_BMCR:
469a94100faSBill Paul 		re8139_reg = RL_BMCR;
470a94100faSBill Paul 		break;
471a94100faSBill Paul 	case MII_BMSR:
472a94100faSBill Paul 		re8139_reg = RL_BMSR;
473a94100faSBill Paul 		break;
474a94100faSBill Paul 	case MII_ANAR:
475a94100faSBill Paul 		re8139_reg = RL_ANAR;
476a94100faSBill Paul 		break;
477a94100faSBill Paul 	case MII_ANER:
478a94100faSBill Paul 		re8139_reg = RL_ANER;
479a94100faSBill Paul 		break;
480a94100faSBill Paul 	case MII_ANLPAR:
481a94100faSBill Paul 		re8139_reg = RL_LPAR;
482a94100faSBill Paul 		break;
483a94100faSBill Paul 	case MII_PHYIDR1:
484a94100faSBill Paul 	case MII_PHYIDR2:
485a94100faSBill Paul 		RL_UNLOCK(sc);
486a94100faSBill Paul 		return(0);
487a94100faSBill Paul 	/*
488a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
489a94100faSBill Paul 	 * register. If we have a link partner which does not
490a94100faSBill Paul 	 * support NWAY, this is the register which will tell
491a94100faSBill Paul 	 * us the results of parallel detection.
492a94100faSBill Paul 	 */
493a94100faSBill Paul 	case RL_MEDIASTAT:
494a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
495a94100faSBill Paul 		RL_UNLOCK(sc);
496a94100faSBill Paul 		return(rval);
497a94100faSBill Paul 	default:
498a94100faSBill Paul 		printf("re%d: bad phy register\n", sc->rl_unit);
499a94100faSBill Paul 		RL_UNLOCK(sc);
500a94100faSBill Paul 		return(0);
501a94100faSBill Paul 	}
502a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
503a94100faSBill Paul 	RL_UNLOCK(sc);
504a94100faSBill Paul 	return(rval);
505a94100faSBill Paul }
506a94100faSBill Paul 
507a94100faSBill Paul static int
508a94100faSBill Paul re_miibus_writereg(dev, phy, reg, data)
509a94100faSBill Paul 	device_t		dev;
510a94100faSBill Paul 	int			phy, reg, data;
511a94100faSBill Paul {
512a94100faSBill Paul 	struct rl_softc		*sc;
513a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
514a94100faSBill Paul 	int			rval = 0;
515a94100faSBill Paul 
516a94100faSBill Paul 	sc = device_get_softc(dev);
517a94100faSBill Paul 	RL_LOCK(sc);
518a94100faSBill Paul 
519a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
520a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
521a94100faSBill Paul 		RL_UNLOCK(sc);
522a94100faSBill Paul 		return (rval);
523a94100faSBill Paul 	}
524a94100faSBill Paul 
525a94100faSBill Paul 	/* Pretend the internal PHY is only at address 0 */
526a94100faSBill Paul 	if (phy) {
527a94100faSBill Paul 		RL_UNLOCK(sc);
528a94100faSBill Paul 		return(0);
529a94100faSBill Paul 	}
530a94100faSBill Paul 	switch(reg) {
531a94100faSBill Paul 	case MII_BMCR:
532a94100faSBill Paul 		re8139_reg = RL_BMCR;
533a94100faSBill Paul 		break;
534a94100faSBill Paul 	case MII_BMSR:
535a94100faSBill Paul 		re8139_reg = RL_BMSR;
536a94100faSBill Paul 		break;
537a94100faSBill Paul 	case MII_ANAR:
538a94100faSBill Paul 		re8139_reg = RL_ANAR;
539a94100faSBill Paul 		break;
540a94100faSBill Paul 	case MII_ANER:
541a94100faSBill Paul 		re8139_reg = RL_ANER;
542a94100faSBill Paul 		break;
543a94100faSBill Paul 	case MII_ANLPAR:
544a94100faSBill Paul 		re8139_reg = RL_LPAR;
545a94100faSBill Paul 		break;
546a94100faSBill Paul 	case MII_PHYIDR1:
547a94100faSBill Paul 	case MII_PHYIDR2:
548a94100faSBill Paul 		RL_UNLOCK(sc);
549a94100faSBill Paul 		return(0);
550a94100faSBill Paul 		break;
551a94100faSBill Paul 	default:
552a94100faSBill Paul 		printf("re%d: bad phy register\n", sc->rl_unit);
553a94100faSBill Paul 		RL_UNLOCK(sc);
554a94100faSBill Paul 		return(0);
555a94100faSBill Paul 	}
556a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
557a94100faSBill Paul 	RL_UNLOCK(sc);
558a94100faSBill Paul 	return(0);
559a94100faSBill Paul }
560a94100faSBill Paul 
561a94100faSBill Paul static void
562a94100faSBill Paul re_miibus_statchg(dev)
563a94100faSBill Paul 	device_t		dev;
564a94100faSBill Paul {
565a94100faSBill Paul 	return;
566a94100faSBill Paul }
567a94100faSBill Paul 
568a94100faSBill Paul /*
569a94100faSBill Paul  * Calculate CRC of a multicast group address, return the upper 6 bits.
570a94100faSBill Paul  */
571a94100faSBill Paul static u_int8_t
572a94100faSBill Paul re_calchash(addr)
573a94100faSBill Paul 	caddr_t			addr;
574a94100faSBill Paul {
575a94100faSBill Paul 	u_int32_t		crc, carry;
576a94100faSBill Paul 	int			i, j;
577a94100faSBill Paul 	u_int8_t		c;
578a94100faSBill Paul 
579a94100faSBill Paul 	/* Compute CRC for the address value. */
580a94100faSBill Paul 	crc = 0xFFFFFFFF; /* initial value */
581a94100faSBill Paul 
582a94100faSBill Paul 	for (i = 0; i < 6; i++) {
583a94100faSBill Paul 		c = *(addr + i);
584a94100faSBill Paul 		for (j = 0; j < 8; j++) {
585a94100faSBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
586a94100faSBill Paul 			crc <<= 1;
587a94100faSBill Paul 			c >>= 1;
588a94100faSBill Paul 			if (carry)
589a94100faSBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
590a94100faSBill Paul 		}
591a94100faSBill Paul 	}
592a94100faSBill Paul 
593a94100faSBill Paul 	/* return the filter bit position */
594a94100faSBill Paul 	return(crc >> 26);
595a94100faSBill Paul }
596a94100faSBill Paul 
597a94100faSBill Paul /*
598a94100faSBill Paul  * Program the 64-bit multicast hash filter.
599a94100faSBill Paul  */
600a94100faSBill Paul static void
601a94100faSBill Paul re_setmulti(sc)
602a94100faSBill Paul 	struct rl_softc		*sc;
603a94100faSBill Paul {
604a94100faSBill Paul 	struct ifnet		*ifp;
605a94100faSBill Paul 	int			h = 0;
606a94100faSBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
607a94100faSBill Paul 	struct ifmultiaddr	*ifma;
608a94100faSBill Paul 	u_int32_t		rxfilt;
609a94100faSBill Paul 	int			mcnt = 0;
610a94100faSBill Paul 
611a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
612a94100faSBill Paul 
613a94100faSBill Paul 	rxfilt = CSR_READ_4(sc, RL_RXCFG);
614a94100faSBill Paul 
615a94100faSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
616a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
617a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
618a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR0, 0xFFFFFFFF);
619a94100faSBill Paul 		CSR_WRITE_4(sc, RL_MAR4, 0xFFFFFFFF);
620a94100faSBill Paul 		return;
621a94100faSBill Paul 	}
622a94100faSBill Paul 
623a94100faSBill Paul 	/* first, zot all the existing hash bits */
624a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, 0);
625a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, 0);
626a94100faSBill Paul 
627a94100faSBill Paul 	/* now program new ones */
628a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
629a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
630a94100faSBill Paul 			continue;
631a94100faSBill Paul 		h = re_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
632a94100faSBill Paul 		if (h < 32)
633a94100faSBill Paul 			hashes[0] |= (1 << h);
634a94100faSBill Paul 		else
635a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
636a94100faSBill Paul 		mcnt++;
637a94100faSBill Paul 	}
638a94100faSBill Paul 
639a94100faSBill Paul 	if (mcnt)
640a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
641a94100faSBill Paul 	else
642a94100faSBill Paul 		rxfilt &= ~RL_RXCFG_RX_MULTI;
643a94100faSBill Paul 
644a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
645a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
646a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
647a94100faSBill Paul 
648a94100faSBill Paul 	return;
649a94100faSBill Paul }
650a94100faSBill Paul 
651a94100faSBill Paul static void
652a94100faSBill Paul re_reset(sc)
653a94100faSBill Paul 	struct rl_softc		*sc;
654a94100faSBill Paul {
655a94100faSBill Paul 	register int		i;
656a94100faSBill Paul 
657a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
658a94100faSBill Paul 
659a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
660a94100faSBill Paul 		DELAY(10);
661a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
662a94100faSBill Paul 			break;
663a94100faSBill Paul 	}
664a94100faSBill Paul 	if (i == RL_TIMEOUT)
665a94100faSBill Paul 		printf("re%d: reset never completed!\n", sc->rl_unit);
666a94100faSBill Paul 
667a94100faSBill Paul 	CSR_WRITE_1(sc, 0x82, 1);
668a94100faSBill Paul 
669a94100faSBill Paul 	return;
670a94100faSBill Paul }
671a94100faSBill Paul 
672a94100faSBill Paul /*
673a94100faSBill Paul  * The following routine is designed to test for a defect on some
674a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
675a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
676a94100faSBill Paul  * should be pulled high. The result of this defect is that the
677a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
678a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
679a94100faSBill Paul  * because the 64-bit data lines aren't connected.
680a94100faSBill Paul  *
681a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
682a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
683a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
684a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
685a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
686a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
687a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
688a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
689a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
690a94100faSBill Paul  */
691a94100faSBill Paul 
692a94100faSBill Paul static int
693a94100faSBill Paul re_diag(sc)
694a94100faSBill Paul 	struct rl_softc		*sc;
695a94100faSBill Paul {
696a94100faSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
697a94100faSBill Paul 	struct mbuf		*m0;
698a94100faSBill Paul 	struct ether_header	*eh;
699a94100faSBill Paul 	struct rl_desc		*cur_rx;
700a94100faSBill Paul 	u_int16_t		status;
701a94100faSBill Paul 	u_int32_t		rxstat;
702a94100faSBill Paul 	int			total_len, i, error = 0;
703a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
704a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
705a94100faSBill Paul 
706a94100faSBill Paul 	/* Allocate a single mbuf */
707a94100faSBill Paul 
708a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
709a94100faSBill Paul 	if (m0 == NULL)
710a94100faSBill Paul 		return(ENOBUFS);
711a94100faSBill Paul 
712a94100faSBill Paul 	/*
713a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
714a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
715a94100faSBill Paul 	 * following special functions:
716a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
717a94100faSBill Paul 	 * - Enables digital loopback mode
718a94100faSBill Paul 	 * - Leaves interrupts turned off
719a94100faSBill Paul 	 */
720a94100faSBill Paul 
721a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
722a94100faSBill Paul 	sc->rl_testmode = 1;
723a94100faSBill Paul 	re_init(sc);
724a94100faSBill Paul 
725a94100faSBill Paul 	/* Put some data in the mbuf */
726a94100faSBill Paul 
727a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
728a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
729a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
730a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
731a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
732a94100faSBill Paul 
733a94100faSBill Paul 	/* Queue the packet, start transmission */
734a94100faSBill Paul 
735a94100faSBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
736a94100faSBill Paul 	re_start(ifp);
737a94100faSBill Paul 	m0 = NULL;
738a94100faSBill Paul 
739a94100faSBill Paul 	/* Wait for it to propagate through the chip */
740a94100faSBill Paul 
741a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
742a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
743a94100faSBill Paul 		if (status & RL_ISR_RX_OK)
744a94100faSBill Paul 			break;
745a94100faSBill Paul 		DELAY(10);
746a94100faSBill Paul 	}
747a94100faSBill Paul 
748a94100faSBill Paul 	if (i == RL_TIMEOUT) {
749a94100faSBill Paul 		printf("re%d: diagnostic failed, failed to receive packet "
750a94100faSBill Paul 		    "in loopback mode\n", sc->rl_unit);
751a94100faSBill Paul 		error = EIO;
752a94100faSBill Paul 		goto done;
753a94100faSBill Paul 	}
754a94100faSBill Paul 
755a94100faSBill Paul 	/*
756a94100faSBill Paul 	 * The packet should have been dumped into the first
757a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
758a94100faSBill Paul 	 */
759a94100faSBill Paul 
760a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
761a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
762a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
763a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_mtag,
764a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0],
765a94100faSBill Paul 	    BUS_DMASYNC_POSTWRITE);
766a94100faSBill Paul 	bus_dmamap_unload(sc->rl_ldata.rl_mtag,
767a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[0]);
768a94100faSBill Paul 
769a94100faSBill Paul 	m0 = sc->rl_ldata.rl_rx_mbuf[0];
770a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[0] = NULL;
771a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
772a94100faSBill Paul 
773a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
774a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
775a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
776a94100faSBill Paul 
777a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
778a94100faSBill Paul 		printf("re%d: diagnostic failed, received short packet\n",
779a94100faSBill Paul 		    sc->rl_unit);
780a94100faSBill Paul 		error = EIO;
781a94100faSBill Paul 		goto done;
782a94100faSBill Paul 	}
783a94100faSBill Paul 
784a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
785a94100faSBill Paul 
786a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
787a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
788a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
789a94100faSBill Paul 		printf("re%d: WARNING, DMA FAILURE!\n", sc->rl_unit);
790a94100faSBill Paul 		printf("re%d: expected TX data: %6D/%6D/0x%x\n", sc->rl_unit,
791a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
792a94100faSBill Paul 		printf("re%d: received RX data: %6D/%6D/0x%x\n", sc->rl_unit,
793a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
794a94100faSBill Paul 		    ntohs(eh->ether_type));
795a94100faSBill Paul 		printf("re%d: You may have a defective 32-bit NIC plugged "
796a94100faSBill Paul 		    "into a 64-bit PCI slot.\n", sc->rl_unit);
797a94100faSBill Paul 		printf("re%d: Please re-install the NIC in a 32-bit slot "
798a94100faSBill Paul 		    "for proper operation.\n", sc->rl_unit);
799a94100faSBill Paul 		printf("re%d: Read the re(4) man page for more details.\n",
800a94100faSBill Paul 		    sc->rl_unit);
801a94100faSBill Paul 		error = EIO;
802a94100faSBill Paul 	}
803a94100faSBill Paul 
804a94100faSBill Paul done:
805a94100faSBill Paul 	/* Turn interface off, release resources */
806a94100faSBill Paul 
807a94100faSBill Paul 	sc->rl_testmode = 0;
808a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
809a94100faSBill Paul 	re_stop(sc);
810a94100faSBill Paul 	if (m0 != NULL)
811a94100faSBill Paul 		m_freem(m0);
812a94100faSBill Paul 
813a94100faSBill Paul 	return (error);
814a94100faSBill Paul }
815a94100faSBill Paul 
816a94100faSBill Paul /*
817a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
818a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
819a94100faSBill Paul  */
820a94100faSBill Paul static int
821a94100faSBill Paul re_probe(dev)
822a94100faSBill Paul 	device_t		dev;
823a94100faSBill Paul {
824a94100faSBill Paul 	struct rl_type		*t;
825a94100faSBill Paul 	struct rl_softc		*sc;
826a94100faSBill Paul 	int			rid;
827a94100faSBill Paul 	u_int32_t		hwrev;
828a94100faSBill Paul 
829a94100faSBill Paul 	t = re_devs;
830a94100faSBill Paul 	sc = device_get_softc(dev);
831a94100faSBill Paul 
832a94100faSBill Paul 	while(t->rl_name != NULL) {
833a94100faSBill Paul 		if ((pci_get_vendor(dev) == t->rl_vid) &&
834a94100faSBill Paul 		    (pci_get_device(dev) == t->rl_did)) {
835a94100faSBill Paul 
836a94100faSBill Paul 			/*
837a94100faSBill Paul 			 * Temporarily map the I/O space
838a94100faSBill Paul 			 * so we can read the chip ID register.
839a94100faSBill Paul 			 */
840a94100faSBill Paul 			rid = RL_RID;
841a94100faSBill Paul 			sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid,
842a94100faSBill Paul 			    0, ~0, 1, RF_ACTIVE);
843a94100faSBill Paul 			if (sc->rl_res == NULL) {
844a94100faSBill Paul 				device_printf(dev,
845a94100faSBill Paul 				    "couldn't map ports/memory\n");
846a94100faSBill Paul 				return(ENXIO);
847a94100faSBill Paul 			}
848a94100faSBill Paul 			sc->rl_btag = rman_get_bustag(sc->rl_res);
849a94100faSBill Paul 			sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
850a94100faSBill Paul 			mtx_init(&sc->rl_mtx,
851a94100faSBill Paul 			    device_get_nameunit(dev),
852a94100faSBill Paul 			    MTX_NETWORK_LOCK, MTX_DEF);
853a94100faSBill Paul 			RL_LOCK(sc);
854a94100faSBill Paul 			hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
855a94100faSBill Paul 			bus_release_resource(dev, RL_RES,
856a94100faSBill Paul 			    RL_RID, sc->rl_res);
857a94100faSBill Paul 			RL_UNLOCK(sc);
858a94100faSBill Paul 			mtx_destroy(&sc->rl_mtx);
859a94100faSBill Paul 			if (t->rl_basetype == hwrev) {
860a94100faSBill Paul 				device_set_desc(dev, t->rl_name);
861a94100faSBill Paul 				return(0);
862a94100faSBill Paul 			}
863a94100faSBill Paul 		}
864a94100faSBill Paul 		t++;
865a94100faSBill Paul 	}
866a94100faSBill Paul 
867a94100faSBill Paul 	return(ENXIO);
868a94100faSBill Paul }
869a94100faSBill Paul 
870a94100faSBill Paul /*
871a94100faSBill Paul  * This routine takes the segment list provided as the result of
872a94100faSBill Paul  * a bus_dma_map_load() operation and assigns the addresses/lengths
873a94100faSBill Paul  * to RealTek DMA descriptors. This can be called either by the RX
874a94100faSBill Paul  * code or the TX code. In the RX case, we'll probably wind up mapping
875a94100faSBill Paul  * at most one segment. For the TX case, there could be any number of
876a94100faSBill Paul  * segments since TX packets may span multiple mbufs. In either case,
877a94100faSBill Paul  * if the number of segments is larger than the rl_maxsegs limit
878a94100faSBill Paul  * specified by the caller, we abort the mapping operation. Sadly,
879a94100faSBill Paul  * whoever designed the buffer mapping API did not provide a way to
880a94100faSBill Paul  * return an error from here, so we have to fake it a bit.
881a94100faSBill Paul  */
882a94100faSBill Paul 
883a94100faSBill Paul static void
884a94100faSBill Paul re_dma_map_desc(arg, segs, nseg, mapsize, error)
885a94100faSBill Paul 	void			*arg;
886a94100faSBill Paul 	bus_dma_segment_t	*segs;
887a94100faSBill Paul 	int			nseg;
888a94100faSBill Paul 	bus_size_t		mapsize;
889a94100faSBill Paul 	int			error;
890a94100faSBill Paul {
891a94100faSBill Paul 	struct rl_dmaload_arg	*ctx;
892a94100faSBill Paul 	struct rl_desc		*d = NULL;
893a94100faSBill Paul 	int			i = 0, idx;
894a94100faSBill Paul 
895a94100faSBill Paul 	if (error)
896a94100faSBill Paul 		return;
897a94100faSBill Paul 
898a94100faSBill Paul 	ctx = arg;
899a94100faSBill Paul 
900a94100faSBill Paul 	/* Signal error to caller if there's too many segments */
901a94100faSBill Paul 	if (nseg > ctx->rl_maxsegs) {
902a94100faSBill Paul 		ctx->rl_maxsegs = 0;
903a94100faSBill Paul 		return;
904a94100faSBill Paul 	}
905a94100faSBill Paul 
906a94100faSBill Paul 	/*
907a94100faSBill Paul 	 * Map the segment array into descriptors. Note that we set the
908a94100faSBill Paul 	 * start-of-frame and end-of-frame markers for either TX or RX, but
909a94100faSBill Paul 	 * they really only have meaning in the TX case. (In the RX case,
910a94100faSBill Paul 	 * it's the chip that tells us where packets begin and end.)
911a94100faSBill Paul 	 * We also keep track of the end of the ring and set the
912a94100faSBill Paul 	 * end-of-ring bits as needed, and we set the ownership bits
913a94100faSBill Paul 	 * in all except the very first descriptor. (The caller will
914a94100faSBill Paul 	 * set this descriptor later when it start transmission or
915a94100faSBill Paul 	 * reception.)
916a94100faSBill Paul 	 */
917a94100faSBill Paul 	idx = ctx->rl_idx;
918a94100faSBill Paul 	while(1) {
919a94100faSBill Paul 		u_int32_t		cmdstat;
920a94100faSBill Paul 		d = &ctx->rl_ring[idx];
921a94100faSBill Paul 		if (le32toh(d->rl_cmdstat) & RL_RDESC_STAT_OWN) {
922a94100faSBill Paul 			ctx->rl_maxsegs = 0;
923a94100faSBill Paul 			return;
924a94100faSBill Paul 		}
925a94100faSBill Paul 		cmdstat = segs[i].ds_len;
926a94100faSBill Paul 		d->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
927a94100faSBill Paul 		d->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
928a94100faSBill Paul 		if (i == 0)
929a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_SOF;
930a94100faSBill Paul 		else
931a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_OWN;
932a94100faSBill Paul 		if (idx == (RL_RX_DESC_CNT - 1))
933a94100faSBill Paul 			cmdstat |= RL_TDESC_CMD_EOR;
934a94100faSBill Paul 		d->rl_cmdstat = htole32(cmdstat | ctx->rl_flags);
935a94100faSBill Paul 		i++;
936a94100faSBill Paul 		if (i == nseg)
937a94100faSBill Paul 			break;
938a94100faSBill Paul 		RL_DESC_INC(idx);
939a94100faSBill Paul 	}
940a94100faSBill Paul 
941a94100faSBill Paul 	d->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
942a94100faSBill Paul 	ctx->rl_maxsegs = nseg;
943a94100faSBill Paul 	ctx->rl_idx = idx;
944a94100faSBill Paul 
945a94100faSBill Paul 	return;
946a94100faSBill Paul }
947a94100faSBill Paul 
948a94100faSBill Paul /*
949a94100faSBill Paul  * Map a single buffer address.
950a94100faSBill Paul  */
951a94100faSBill Paul 
952a94100faSBill Paul static void
953a94100faSBill Paul re_dma_map_addr(arg, segs, nseg, error)
954a94100faSBill Paul 	void			*arg;
955a94100faSBill Paul 	bus_dma_segment_t	*segs;
956a94100faSBill Paul 	int			nseg;
957a94100faSBill Paul 	int			error;
958a94100faSBill Paul {
959a94100faSBill Paul 	u_int32_t		*addr;
960a94100faSBill Paul 
961a94100faSBill Paul 	if (error)
962a94100faSBill Paul 		return;
963a94100faSBill Paul 
964a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
965a94100faSBill Paul 	addr = arg;
966a94100faSBill Paul 	*addr = segs->ds_addr;
967a94100faSBill Paul 
968a94100faSBill Paul 	return;
969a94100faSBill Paul }
970a94100faSBill Paul 
971a94100faSBill Paul static int
972a94100faSBill Paul re_allocmem(dev, sc)
973a94100faSBill Paul 	device_t		dev;
974a94100faSBill Paul 	struct rl_softc		*sc;
975a94100faSBill Paul {
976a94100faSBill Paul 	int			error;
977a94100faSBill Paul 	int			nseg;
978a94100faSBill Paul 	int			i;
979a94100faSBill Paul 
980a94100faSBill Paul 	/*
981a94100faSBill Paul 	 * Allocate map for RX mbufs.
982a94100faSBill Paul 	 */
983a94100faSBill Paul 	nseg = 32;
984a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, ETHER_ALIGN, 0,
985a94100faSBill Paul 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
986a94100faSBill Paul 	    NULL, MCLBYTES * nseg, nseg, RL_JLEN, BUS_DMA_ALLOCNOW,
987a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_mtag);
988a94100faSBill Paul 	if (error) {
989a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
990a94100faSBill Paul 		return (ENOMEM);
991a94100faSBill Paul 	}
992a94100faSBill Paul 
993a94100faSBill Paul 	/*
994a94100faSBill Paul 	 * Allocate map for TX descriptor list.
995a94100faSBill Paul 	 */
996a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
997a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
998a94100faSBill Paul             NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
999a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1000a94100faSBill Paul 	if (error) {
1001a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1002a94100faSBill Paul 		return (ENOMEM);
1003a94100faSBill Paul 	}
1004a94100faSBill Paul 
1005a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1006a94100faSBill Paul 
1007a94100faSBill Paul         error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1008a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_tx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1009a94100faSBill Paul             &sc->rl_ldata.rl_tx_list_map);
1010a94100faSBill Paul         if (error)
1011a94100faSBill Paul                 return (ENOMEM);
1012a94100faSBill Paul 
1013a94100faSBill Paul 	/* Load the map for the TX ring. */
1014a94100faSBill Paul 
1015a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1016a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1017a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
1018a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1019a94100faSBill Paul 
1020a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1021a94100faSBill Paul 
1022a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
1023a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1024a94100faSBill Paul 			    &sc->rl_ldata.rl_tx_dmamap[i]);
1025a94100faSBill Paul 		if (error) {
1026a94100faSBill Paul 			device_printf(dev, "can't create DMA map for TX\n");
1027a94100faSBill Paul 			return(ENOMEM);
1028a94100faSBill Paul 		}
1029a94100faSBill Paul 	}
1030a94100faSBill Paul 
1031a94100faSBill Paul 	/*
1032a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1033a94100faSBill Paul 	 */
1034a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1035a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1036a94100faSBill Paul             NULL, RL_TX_LIST_SZ, 1, RL_TX_LIST_SZ, BUS_DMA_ALLOCNOW,
1037a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1038a94100faSBill Paul 	if (error) {
1039a94100faSBill Paul 		device_printf(dev, "could not allocate dma tag\n");
1040a94100faSBill Paul 		return (ENOMEM);
1041a94100faSBill Paul 	}
1042a94100faSBill Paul 
1043a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1044a94100faSBill Paul 
1045a94100faSBill Paul         error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1046a94100faSBill Paul 	    (void **)&sc->rl_ldata.rl_rx_list, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1047a94100faSBill Paul             &sc->rl_ldata.rl_rx_list_map);
1048a94100faSBill Paul         if (error)
1049a94100faSBill Paul                 return (ENOMEM);
1050a94100faSBill Paul 
1051a94100faSBill Paul 	/* Load the map for the RX ring. */
1052a94100faSBill Paul 
1053a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1054a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1055a94100faSBill Paul 	     RL_TX_LIST_SZ, re_dma_map_addr,
1056a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1057a94100faSBill Paul 
1058a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1059a94100faSBill Paul 
1060a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1061a94100faSBill Paul 		error = bus_dmamap_create(sc->rl_ldata.rl_mtag, 0,
1062a94100faSBill Paul 			    &sc->rl_ldata.rl_rx_dmamap[i]);
1063a94100faSBill Paul 		if (error) {
1064a94100faSBill Paul 			device_printf(dev, "can't create DMA map for RX\n");
1065a94100faSBill Paul 			return(ENOMEM);
1066a94100faSBill Paul 		}
1067a94100faSBill Paul 	}
1068a94100faSBill Paul 
1069a94100faSBill Paul 	return(0);
1070a94100faSBill Paul }
1071a94100faSBill Paul 
1072a94100faSBill Paul /*
1073a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1074a94100faSBill Paul  * setup and ethernet/BPF attach.
1075a94100faSBill Paul  */
1076a94100faSBill Paul static int
1077a94100faSBill Paul re_attach(dev)
1078a94100faSBill Paul 	device_t		dev;
1079a94100faSBill Paul {
1080a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1081a94100faSBill Paul 	u_int16_t		as[3];
1082a94100faSBill Paul 	struct rl_softc		*sc;
1083a94100faSBill Paul 	struct ifnet		*ifp;
1084a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1085a94100faSBill Paul 	int			hwrev;
1086a94100faSBill Paul 	u_int16_t		re_did = 0;
1087a94100faSBill Paul 	int			unit, error = 0, rid, i;
1088a94100faSBill Paul 
1089a94100faSBill Paul 	sc = device_get_softc(dev);
1090a94100faSBill Paul 	unit = device_get_unit(dev);
1091a94100faSBill Paul 
1092a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1093a94100faSBill Paul 	    MTX_DEF | MTX_RECURSE);
1094a94100faSBill Paul #ifndef BURN_BRIDGES
1095a94100faSBill Paul 	/*
1096a94100faSBill Paul 	 * Handle power management nonsense.
1097a94100faSBill Paul 	 */
1098a94100faSBill Paul 
1099a94100faSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1100a94100faSBill Paul 		u_int32_t		iobase, membase, irq;
1101a94100faSBill Paul 
1102a94100faSBill Paul 		/* Save important PCI config data. */
1103a94100faSBill Paul 		iobase = pci_read_config(dev, RL_PCI_LOIO, 4);
1104a94100faSBill Paul 		membase = pci_read_config(dev, RL_PCI_LOMEM, 4);
1105a94100faSBill Paul 		irq = pci_read_config(dev, RL_PCI_INTLINE, 4);
1106a94100faSBill Paul 
1107a94100faSBill Paul 		/* Reset the power state. */
1108a94100faSBill Paul 		printf("re%d: chip is is in D%d power mode "
1109a94100faSBill Paul 		    "-- setting to D0\n", unit,
1110a94100faSBill Paul 		    pci_get_powerstate(dev));
1111a94100faSBill Paul 
1112a94100faSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1113a94100faSBill Paul 
1114a94100faSBill Paul 		/* Restore PCI config data. */
1115a94100faSBill Paul 		pci_write_config(dev, RL_PCI_LOIO, iobase, 4);
1116a94100faSBill Paul 		pci_write_config(dev, RL_PCI_LOMEM, membase, 4);
1117a94100faSBill Paul 		pci_write_config(dev, RL_PCI_INTLINE, irq, 4);
1118a94100faSBill Paul 	}
1119a94100faSBill Paul #endif
1120a94100faSBill Paul 	/*
1121a94100faSBill Paul 	 * Map control/status registers.
1122a94100faSBill Paul 	 */
1123a94100faSBill Paul 	pci_enable_busmaster(dev);
1124a94100faSBill Paul 
1125a94100faSBill Paul 	rid = RL_RID;
1126a94100faSBill Paul 	sc->rl_res = bus_alloc_resource(dev, RL_RES, &rid,
1127a94100faSBill Paul 	    0, ~0, 1, RF_ACTIVE);
1128a94100faSBill Paul 
1129a94100faSBill Paul 	if (sc->rl_res == NULL) {
1130a94100faSBill Paul 		printf ("re%d: couldn't map ports/memory\n", unit);
1131a94100faSBill Paul 		error = ENXIO;
1132a94100faSBill Paul 		goto fail;
1133a94100faSBill Paul 	}
1134a94100faSBill Paul 
1135a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1136a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1137a94100faSBill Paul 
1138a94100faSBill Paul 	/* Allocate interrupt */
1139a94100faSBill Paul 	rid = 0;
1140a94100faSBill Paul 	sc->rl_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
1141a94100faSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
1142a94100faSBill Paul 
1143a94100faSBill Paul 	if (sc->rl_irq == NULL) {
1144a94100faSBill Paul 		printf("re%d: couldn't map interrupt\n", unit);
1145a94100faSBill Paul 		error = ENXIO;
1146a94100faSBill Paul 		goto fail;
1147a94100faSBill Paul 	}
1148a94100faSBill Paul 
1149a94100faSBill Paul 	/* Reset the adapter. */
1150a94100faSBill Paul 	re_reset(sc);
1151a94100faSBill Paul 	sc->rl_eecmd_read = RL_EECMD_READ_6BIT;
1152a94100faSBill Paul 	re_read_eeprom(sc, (caddr_t)&re_did, 0, 1, 0);
1153a94100faSBill Paul 	if (re_did != 0x8129)
1154a94100faSBill Paul 		sc->rl_eecmd_read = RL_EECMD_READ_8BIT;
1155a94100faSBill Paul 
1156a94100faSBill Paul 	/*
1157a94100faSBill Paul 	 * Get station address from the EEPROM.
1158a94100faSBill Paul 	 */
1159a94100faSBill Paul 	re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3, 0);
1160a94100faSBill Paul 	for (i = 0; i < 3; i++) {
1161a94100faSBill Paul 		eaddr[(i * 2) + 0] = as[i] & 0xff;
1162a94100faSBill Paul 		eaddr[(i * 2) + 1] = as[i] >> 8;
1163a94100faSBill Paul 	}
1164a94100faSBill Paul 
1165a94100faSBill Paul 	/*
1166a94100faSBill Paul 	 * A RealTek chip was detected. Inform the world.
1167a94100faSBill Paul 	 */
1168a94100faSBill Paul 	printf("re%d: Ethernet address: %6D\n", unit, eaddr, ":");
1169a94100faSBill Paul 
1170a94100faSBill Paul 	sc->rl_unit = unit;
1171a94100faSBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
1172a94100faSBill Paul 
1173a94100faSBill Paul 	hw_rev = re_hwrevs;
1174a94100faSBill Paul 	hwrev = CSR_READ_4(sc, RL_TXCFG) & RL_TXCFG_HWREV;
1175a94100faSBill Paul 	while (hw_rev->rl_desc != NULL) {
1176a94100faSBill Paul 		if (hw_rev->rl_rev == hwrev) {
1177a94100faSBill Paul 			sc->rl_type = hw_rev->rl_type;
1178a94100faSBill Paul 			break;
1179a94100faSBill Paul 		}
1180a94100faSBill Paul 		hw_rev++;
1181a94100faSBill Paul 	}
1182a94100faSBill Paul 
1183a94100faSBill Paul 	/*
1184a94100faSBill Paul 	 * Allocate the parent bus DMA tag appropriate for PCI.
1185a94100faSBill Paul 	 */
1186a94100faSBill Paul #define RL_NSEG_NEW 32
1187a94100faSBill Paul 	error = bus_dma_tag_create(NULL,	/* parent */
1188a94100faSBill Paul 			1, 0,			/* alignment, boundary */
1189a94100faSBill Paul 			BUS_SPACE_MAXADDR_32BIT,/* lowaddr */
1190a94100faSBill Paul 			BUS_SPACE_MAXADDR,	/* highaddr */
1191a94100faSBill Paul 			NULL, NULL,		/* filter, filterarg */
1192a94100faSBill Paul 			MAXBSIZE, RL_NSEG_NEW,	/* maxsize, nsegments */
1193a94100faSBill Paul 			BUS_SPACE_MAXSIZE_32BIT,/* maxsegsize */
1194a94100faSBill Paul 			BUS_DMA_ALLOCNOW,	/* flags */
1195a94100faSBill Paul 			NULL, NULL,		/* lockfunc, lockarg */
1196a94100faSBill Paul 			&sc->rl_parent_tag);
1197a94100faSBill Paul 	if (error)
1198a94100faSBill Paul 		goto fail;
1199a94100faSBill Paul 
1200a94100faSBill Paul 	error = re_allocmem(dev, sc);
1201a94100faSBill Paul 
1202a94100faSBill Paul 	if (error)
1203a94100faSBill Paul 		goto fail;
1204a94100faSBill Paul 
1205a94100faSBill Paul 	/* Do MII setup */
1206a94100faSBill Paul 	if (mii_phy_probe(dev, &sc->rl_miibus,
1207a94100faSBill Paul 	    re_ifmedia_upd, re_ifmedia_sts)) {
1208a94100faSBill Paul 		printf("re%d: MII without any phy!\n", sc->rl_unit);
1209a94100faSBill Paul 		error = ENXIO;
1210a94100faSBill Paul 		goto fail;
1211a94100faSBill Paul 	}
1212a94100faSBill Paul 
1213a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1214a94100faSBill Paul 	ifp->if_softc = sc;
1215a94100faSBill Paul 	ifp->if_unit = unit;
1216a94100faSBill Paul 	ifp->if_name = "re";
1217a94100faSBill Paul 	ifp->if_mtu = ETHERMTU;
1218a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1219a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1220a94100faSBill Paul 	ifp->if_output = ether_output;
1221a94100faSBill Paul 	ifp->if_capabilities = IFCAP_VLAN_MTU;
1222a94100faSBill Paul 	ifp->if_start = re_start;
1223a94100faSBill Paul 	ifp->if_hwassist = RE_CSUM_FEATURES;
1224a94100faSBill Paul 	ifp->if_capabilities |= IFCAP_HWCSUM|IFCAP_VLAN_HWTAGGING;
1225a94100faSBill Paul 	ifp->if_watchdog = re_watchdog;
1226a94100faSBill Paul 	ifp->if_init = re_init;
1227a94100faSBill Paul 	if (sc->rl_type == RL_8169)
1228a94100faSBill Paul 		ifp->if_baudrate = 1000000000;
1229a94100faSBill Paul 	else
1230a94100faSBill Paul 		ifp->if_baudrate = 100000000;
1231a94100faSBill Paul 	ifp->if_snd.ifq_maxlen = RL_IFQ_MAXLEN;
1232a94100faSBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1233a94100faSBill Paul 
1234a94100faSBill Paul 	callout_handle_init(&sc->rl_stat_ch);
1235a94100faSBill Paul 
1236a94100faSBill Paul 	/*
1237a94100faSBill Paul 	 * Call MI attach routine.
1238a94100faSBill Paul 	 */
1239a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1240a94100faSBill Paul 
1241a94100faSBill Paul 	/* Perform hardware diagnostic. */
1242a94100faSBill Paul 	error = re_diag(sc);
1243a94100faSBill Paul 
1244a94100faSBill Paul 	if (error) {
1245a94100faSBill Paul 		printf("re%d: attach aborted due to hardware diag failure\n",
1246a94100faSBill Paul 		    unit);
1247a94100faSBill Paul 		ether_ifdetach(ifp);
1248a94100faSBill Paul 		goto fail;
1249a94100faSBill Paul 	}
1250a94100faSBill Paul 
1251a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1252a94100faSBill Paul 	error = bus_setup_intr(dev, sc->rl_irq, INTR_TYPE_NET,
1253a94100faSBill Paul 	    re_intr, sc, &sc->rl_intrhand);
1254a94100faSBill Paul 
1255a94100faSBill Paul 	if (error) {
1256a94100faSBill Paul 		printf("re%d: couldn't set up irq\n", unit);
1257a94100faSBill Paul 		ether_ifdetach(ifp);
1258a94100faSBill Paul 		goto fail;
1259a94100faSBill Paul 	}
1260a94100faSBill Paul 
1261a94100faSBill Paul fail:
1262a94100faSBill Paul 	if (error)
1263a94100faSBill Paul 		re_detach(dev);
1264a94100faSBill Paul 
1265a94100faSBill Paul 	return (error);
1266a94100faSBill Paul }
1267a94100faSBill Paul 
1268a94100faSBill Paul /*
1269a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1270a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1271a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1272a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1273a94100faSBill Paul  * allocated.
1274a94100faSBill Paul  */
1275a94100faSBill Paul static int
1276a94100faSBill Paul re_detach(dev)
1277a94100faSBill Paul 	device_t		dev;
1278a94100faSBill Paul {
1279a94100faSBill Paul 	struct rl_softc		*sc;
1280a94100faSBill Paul 	struct ifnet		*ifp;
1281a94100faSBill Paul 	int			i;
1282a94100faSBill Paul 
1283a94100faSBill Paul 	sc = device_get_softc(dev);
1284a94100faSBill Paul 	KASSERT(mtx_initialized(&sc->rl_mtx), ("rl mutex not initialized"));
1285a94100faSBill Paul 	RL_LOCK(sc);
1286a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1287a94100faSBill Paul 
1288a94100faSBill Paul 	/* These should only be active if attach succeeded */
1289a94100faSBill Paul 	if (device_is_attached(dev)) {
1290a94100faSBill Paul 		re_stop(sc);
1291a94100faSBill Paul 		/*
1292a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1293a94100faSBill Paul 		 * still had a BPF descriptor attached to this
1294a94100faSBill Paul 		 * interface. If they do, ether_ifattach() will cause
1295a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1296a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1297a94100faSBill Paul 		 * which will try to call re_init() again. This will
1298a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1299a94100faSBill Paul 		 * which will panic the system when the kernel tries
1300a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1301a94100faSBill Paul 		 * anymore.
1302a94100faSBill Paul 		 */
1303a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1304a94100faSBill Paul 		ether_ifdetach(ifp);
1305a94100faSBill Paul 	}
1306a94100faSBill Paul 	if (sc->rl_miibus)
1307a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1308a94100faSBill Paul 	bus_generic_detach(dev);
1309a94100faSBill Paul 
1310a94100faSBill Paul 	if (sc->rl_intrhand)
1311a94100faSBill Paul 		bus_teardown_intr(dev, sc->rl_irq, sc->rl_intrhand);
1312a94100faSBill Paul 	if (sc->rl_irq)
1313a94100faSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->rl_irq);
1314a94100faSBill Paul 	if (sc->rl_res)
1315a94100faSBill Paul 		bus_release_resource(dev, RL_RES, RL_RID, sc->rl_res);
1316a94100faSBill Paul 
1317a94100faSBill Paul 
1318a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1319a94100faSBill Paul 
1320a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1321a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1322a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1323a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1324a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list,
1325a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1326a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1327a94100faSBill Paul 	}
1328a94100faSBill Paul 
1329a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1330a94100faSBill Paul 
1331a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1332a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1333a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1334a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1335a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list,
1336a94100faSBill Paul 		    sc->rl_ldata.rl_tx_list_map);
1337a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1338a94100faSBill Paul 	}
1339a94100faSBill Paul 
1340a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1341a94100faSBill Paul 
1342a94100faSBill Paul 	if (sc->rl_ldata.rl_mtag) {
1343a94100faSBill Paul 		for (i = 0; i < RL_TX_DESC_CNT; i++)
1344a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1345a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
1346a94100faSBill Paul 		for (i = 0; i < RL_RX_DESC_CNT; i++)
1347a94100faSBill Paul 			bus_dmamap_destroy(sc->rl_ldata.rl_mtag,
1348a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
1349a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_mtag);
1350a94100faSBill Paul 	}
1351a94100faSBill Paul 
1352a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1353a94100faSBill Paul 
1354a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1355a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_stag,
1356a94100faSBill Paul 		    sc->rl_ldata.rl_rx_list_map);
1357a94100faSBill Paul 		bus_dmamem_free(sc->rl_ldata.rl_stag,
1358a94100faSBill Paul 		    sc->rl_ldata.rl_stats,
1359a94100faSBill Paul 		    sc->rl_ldata.rl_smap);
1360a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1361a94100faSBill Paul 	}
1362a94100faSBill Paul 
1363a94100faSBill Paul 	if (sc->rl_parent_tag)
1364a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1365a94100faSBill Paul 
1366a94100faSBill Paul 	RL_UNLOCK(sc);
1367a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1368a94100faSBill Paul 
1369a94100faSBill Paul 	return(0);
1370a94100faSBill Paul }
1371a94100faSBill Paul 
1372a94100faSBill Paul static int
1373a94100faSBill Paul re_newbuf(sc, idx, m)
1374a94100faSBill Paul 	struct rl_softc		*sc;
1375a94100faSBill Paul 	int			idx;
1376a94100faSBill Paul 	struct mbuf		*m;
1377a94100faSBill Paul {
1378a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1379a94100faSBill Paul 	struct mbuf		*n = NULL;
1380a94100faSBill Paul 	int			error;
1381a94100faSBill Paul 
1382a94100faSBill Paul 	if (m == NULL) {
1383a94100faSBill Paul 		n = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1384a94100faSBill Paul 		if (n == NULL)
1385a94100faSBill Paul 			return(ENOBUFS);
1386a94100faSBill Paul 		m = n;
1387a94100faSBill Paul 	} else
1388a94100faSBill Paul 		m->m_data = m->m_ext.ext_buf;
1389a94100faSBill Paul 
1390a94100faSBill Paul 	/*
1391a94100faSBill Paul 	 * Initialize mbuf length fields and fixup
1392a94100faSBill Paul 	 * alignment so that the frame payload is
1393a94100faSBill Paul 	 * longword aligned.
1394a94100faSBill Paul 	 */
1395a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1396a94100faSBill Paul 	m_adj(m, ETHER_ALIGN);
1397a94100faSBill Paul 
1398a94100faSBill Paul 	arg.sc = sc;
1399a94100faSBill Paul 	arg.rl_idx = idx;
1400a94100faSBill Paul 	arg.rl_maxsegs = 1;
1401a94100faSBill Paul 	arg.rl_flags = 0;
1402a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_rx_list;
1403a94100faSBill Paul 
1404a94100faSBill Paul         error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag,
1405a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx], m, re_dma_map_desc,
1406a94100faSBill Paul 	    &arg, BUS_DMA_NOWAIT);
1407a94100faSBill Paul 	if (error || arg.rl_maxsegs != 1) {
1408a94100faSBill Paul 		if (n != NULL)
1409a94100faSBill Paul 			m_freem(n);
1410a94100faSBill Paul 		return (ENOMEM);
1411a94100faSBill Paul 	}
1412a94100faSBill Paul 
1413a94100faSBill Paul 	sc->rl_ldata.rl_rx_list[idx].rl_cmdstat |= htole32(RL_RDESC_CMD_OWN);
1414a94100faSBill Paul 	sc->rl_ldata.rl_rx_mbuf[idx] = m;
1415a94100faSBill Paul 
1416a94100faSBill Paul         bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1417a94100faSBill Paul 	    sc->rl_ldata.rl_rx_dmamap[idx],
1418a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1419a94100faSBill Paul 
1420a94100faSBill Paul 	return(0);
1421a94100faSBill Paul }
1422a94100faSBill Paul 
1423a94100faSBill Paul static int
1424a94100faSBill Paul re_tx_list_init(sc)
1425a94100faSBill Paul 	struct rl_softc		*sc;
1426a94100faSBill Paul {
1427a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_tx_list, RL_TX_LIST_SZ);
1428a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_tx_mbuf,
1429a94100faSBill Paul 	    (RL_TX_DESC_CNT * sizeof(struct mbuf *)));
1430a94100faSBill Paul 
1431a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1432a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map, BUS_DMASYNC_PREWRITE);
1433a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1434a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1435a94100faSBill Paul 	sc->rl_ldata.rl_tx_free = RL_TX_DESC_CNT;
1436a94100faSBill Paul 
1437a94100faSBill Paul 	return(0);
1438a94100faSBill Paul }
1439a94100faSBill Paul 
1440a94100faSBill Paul static int
1441a94100faSBill Paul re_rx_list_init(sc)
1442a94100faSBill Paul 	struct rl_softc		*sc;
1443a94100faSBill Paul {
1444a94100faSBill Paul 	int			i;
1445a94100faSBill Paul 
1446a94100faSBill Paul 	bzero ((char *)sc->rl_ldata.rl_rx_list, RL_RX_LIST_SZ);
1447a94100faSBill Paul 	bzero ((char *)&sc->rl_ldata.rl_rx_mbuf,
1448a94100faSBill Paul 	    (RL_RX_DESC_CNT * sizeof(struct mbuf *)));
1449a94100faSBill Paul 
1450a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
1451a94100faSBill Paul 		if (re_newbuf(sc, i, NULL) == ENOBUFS)
1452a94100faSBill Paul 			return(ENOBUFS);
1453a94100faSBill Paul 	}
1454a94100faSBill Paul 
1455a94100faSBill Paul 	/* Flush the RX descriptors */
1456a94100faSBill Paul 
1457a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1458a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1459a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1460a94100faSBill Paul 
1461a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1462a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1463a94100faSBill Paul 
1464a94100faSBill Paul 	return(0);
1465a94100faSBill Paul }
1466a94100faSBill Paul 
1467a94100faSBill Paul /*
1468a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1469a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1470a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1471a94100faSBill Paul  */
1472a94100faSBill Paul static void
1473a94100faSBill Paul re_rxeof(sc)
1474a94100faSBill Paul 	struct rl_softc		*sc;
1475a94100faSBill Paul {
1476a94100faSBill Paul 	struct mbuf		*m;
1477a94100faSBill Paul 	struct ifnet		*ifp;
1478a94100faSBill Paul 	int			i, total_len;
1479a94100faSBill Paul 	struct rl_desc		*cur_rx;
1480a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
1481a94100faSBill Paul 
1482a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1483a94100faSBill Paul 	i = sc->rl_ldata.rl_rx_prodidx;
1484a94100faSBill Paul 
1485a94100faSBill Paul 	/* Invalidate the descriptor memory */
1486a94100faSBill Paul 
1487a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1488a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1489a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1490a94100faSBill Paul 
1491a94100faSBill Paul 	while (!RL_OWN(&sc->rl_ldata.rl_rx_list[i])) {
1492a94100faSBill Paul 
1493a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1494a94100faSBill Paul 		m = sc->rl_ldata.rl_rx_mbuf[i];
1495a94100faSBill Paul 		total_len = RL_RXBYTES(cur_rx);
1496a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1497a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
1498a94100faSBill Paul 
1499a94100faSBill Paul 		/* Invalidate the RX mbuf and unload its map */
1500a94100faSBill Paul 
1501a94100faSBill Paul 		bus_dmamap_sync(sc->rl_ldata.rl_mtag,
1502a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i],
1503a94100faSBill Paul 		    BUS_DMASYNC_POSTWRITE);
1504a94100faSBill Paul 		bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1505a94100faSBill Paul 		    sc->rl_ldata.rl_rx_dmamap[i]);
1506a94100faSBill Paul 
1507a94100faSBill Paul 		if (!(rxstat & RL_RDESC_STAT_EOF)) {
1508a94100faSBill Paul 			m->m_len = MCLBYTES - ETHER_ALIGN;
1509a94100faSBill Paul 			if (sc->rl_head == NULL)
1510a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
1511a94100faSBill Paul 			else {
1512a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1513a94100faSBill Paul 				sc->rl_tail->m_next = m;
1514a94100faSBill Paul 				sc->rl_tail = m;
1515a94100faSBill Paul 			}
1516a94100faSBill Paul 			re_newbuf(sc, i, NULL);
1517a94100faSBill Paul 			RL_DESC_INC(i);
1518a94100faSBill Paul 			continue;
1519a94100faSBill Paul 		}
1520a94100faSBill Paul 
1521a94100faSBill Paul 		/*
1522a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
1523a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
1524a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
1525a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
1526a94100faSBill Paul 		 * were already used, so to make room for the extra
1527a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
1528a94100faSBill Paul 		 * error' bit and shifted the other status bits
1529a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
1530a94100faSBill Paul 		 * still in the same places. We have already extracted
1531a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
1532a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
1533a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
1534a94100faSBill Paul 		 * them using the 8169 status as though it was in the
1535a94100faSBill Paul 		 * same format as that of the 8139C+.
1536a94100faSBill Paul 		 */
1537a94100faSBill Paul 		if (sc->rl_type == RL_8169)
1538a94100faSBill Paul 			rxstat >>= 1;
1539a94100faSBill Paul 
1540a94100faSBill Paul 		if (rxstat & RL_RDESC_STAT_RXERRSUM) {
1541a94100faSBill Paul 			ifp->if_ierrors++;
1542a94100faSBill Paul 			/*
1543a94100faSBill Paul 			 * If this is part of a multi-fragment packet,
1544a94100faSBill Paul 			 * discard all the pieces.
1545a94100faSBill Paul 			 */
1546a94100faSBill Paul 			if (sc->rl_head != NULL) {
1547a94100faSBill Paul 				m_freem(sc->rl_head);
1548a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1549a94100faSBill Paul 			}
1550a94100faSBill Paul 			re_newbuf(sc, i, m);
1551a94100faSBill Paul 			RL_DESC_INC(i);
1552a94100faSBill Paul 			continue;
1553a94100faSBill Paul 		}
1554a94100faSBill Paul 
1555a94100faSBill Paul 		/*
1556a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
1557a94100faSBill Paul 		 * reload the current one.
1558a94100faSBill Paul 		 */
1559a94100faSBill Paul 
1560a94100faSBill Paul 		if (re_newbuf(sc, i, NULL)) {
1561a94100faSBill Paul 			ifp->if_ierrors++;
1562a94100faSBill Paul 			if (sc->rl_head != NULL) {
1563a94100faSBill Paul 				m_freem(sc->rl_head);
1564a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
1565a94100faSBill Paul 			}
1566a94100faSBill Paul 			re_newbuf(sc, i, m);
1567a94100faSBill Paul 			RL_DESC_INC(i);
1568a94100faSBill Paul 			continue;
1569a94100faSBill Paul 		}
1570a94100faSBill Paul 
1571a94100faSBill Paul 		RL_DESC_INC(i);
1572a94100faSBill Paul 
1573a94100faSBill Paul 		if (sc->rl_head != NULL) {
1574a94100faSBill Paul 			m->m_len = total_len % (MCLBYTES - ETHER_ALIGN);
1575a94100faSBill Paul 			/*
1576a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
1577a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
1578a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
1579a94100faSBill Paul 			 * care about anyway.
1580a94100faSBill Paul 			 */
1581a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
1582a94100faSBill Paul 				sc->rl_tail->m_len -=
1583a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
1584a94100faSBill Paul 				m_freem(m);
1585a94100faSBill Paul 			} else {
1586a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
1587a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
1588a94100faSBill Paul 				sc->rl_tail->m_next = m;
1589a94100faSBill Paul 			}
1590a94100faSBill Paul 			m = sc->rl_head;
1591a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
1592a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
1593a94100faSBill Paul 		} else
1594a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
1595a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
1596a94100faSBill Paul 
1597a94100faSBill Paul 		ifp->if_ipackets++;
1598a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
1599a94100faSBill Paul 
1600a94100faSBill Paul 		/* Do RX checksumming if enabled */
1601a94100faSBill Paul 
1602a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
1603a94100faSBill Paul 
1604a94100faSBill Paul 			/* Check IP header checksum */
1605a94100faSBill Paul 			if (rxstat & RL_RDESC_STAT_PROTOID)
1606a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1607a94100faSBill Paul 			if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
1608a94100faSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1609a94100faSBill Paul 
1610a94100faSBill Paul 			/* Check TCP/UDP checksum */
1611a94100faSBill Paul 			if ((RL_TCPPKT(rxstat) &&
1612a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
1613a94100faSBill Paul 			    (RL_UDPPKT(rxstat) &&
1614a94100faSBill Paul 			    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
1615a94100faSBill Paul 				m->m_pkthdr.csum_flags |=
1616a94100faSBill Paul 				    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1617a94100faSBill Paul 				m->m_pkthdr.csum_data = 0xffff;
1618a94100faSBill Paul 			}
1619a94100faSBill Paul 		}
1620a94100faSBill Paul 
1621a94100faSBill Paul 		if (rxvlan & RL_RDESC_VLANCTL_TAG)
1622a94100faSBill Paul 			VLAN_INPUT_TAG(ifp, m,
1623a94100faSBill Paul 			    ntohs((rxvlan & RL_RDESC_VLANCTL_DATA)), continue);
1624a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
1625a94100faSBill Paul 	}
1626a94100faSBill Paul 
1627a94100faSBill Paul 	/* Flush the RX DMA ring */
1628a94100faSBill Paul 
1629a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1630a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1631a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1632a94100faSBill Paul 
1633a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
1634a94100faSBill Paul 
1635a94100faSBill Paul 	return;
1636a94100faSBill Paul }
1637a94100faSBill Paul 
1638a94100faSBill Paul static void
1639a94100faSBill Paul re_txeof(sc)
1640a94100faSBill Paul 	struct rl_softc		*sc;
1641a94100faSBill Paul {
1642a94100faSBill Paul 	struct ifnet		*ifp;
1643a94100faSBill Paul 	u_int32_t		txstat;
1644a94100faSBill Paul 	int			idx;
1645a94100faSBill Paul 
1646a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1647a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_considx;
1648a94100faSBill Paul 
1649a94100faSBill Paul 	/* Invalidate the TX descriptor list */
1650a94100faSBill Paul 
1651a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1652a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1653a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
1654a94100faSBill Paul 
1655a94100faSBill Paul 	while (idx != sc->rl_ldata.rl_tx_prodidx) {
1656a94100faSBill Paul 
1657a94100faSBill Paul 		txstat = le32toh(sc->rl_ldata.rl_tx_list[idx].rl_cmdstat);
1658a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_OWN)
1659a94100faSBill Paul 			break;
1660a94100faSBill Paul 
1661a94100faSBill Paul 		/*
1662a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
1663a94100faSBill Paul 		 * in a fragment chain, which also happens to
1664a94100faSBill Paul 		 * be the only place where the TX status bits
1665a94100faSBill Paul 		 * are valid.
1666a94100faSBill Paul 		 */
1667a94100faSBill Paul 
1668a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
1669a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[idx]);
1670a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[idx] = NULL;
1671a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
1672a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[idx]);
1673a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
1674a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
1675a94100faSBill Paul 				ifp->if_collisions++;
1676a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
1677a94100faSBill Paul 				ifp->if_oerrors++;
1678a94100faSBill Paul 			else
1679a94100faSBill Paul 				ifp->if_opackets++;
1680a94100faSBill Paul 		}
1681a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
1682a94100faSBill Paul 		RL_DESC_INC(idx);
1683a94100faSBill Paul 	}
1684a94100faSBill Paul 
1685a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
1686a94100faSBill Paul 
1687a94100faSBill Paul 	if (idx != sc->rl_ldata.rl_tx_considx) {
1688a94100faSBill Paul 		sc->rl_ldata.rl_tx_considx = idx;
1689a94100faSBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1690a94100faSBill Paul 		ifp->if_timer = 0;
1691a94100faSBill Paul 	}
1692a94100faSBill Paul 
1693a94100faSBill Paul 	/*
1694a94100faSBill Paul 	 * If not all descriptors have been released reaped yet,
1695a94100faSBill Paul 	 * reload the timer so that we will eventually get another
1696a94100faSBill Paul 	 * interrupt that will cause us to re-enter this routine.
1697a94100faSBill Paul 	 * This is done in case the transmitter has gone idle.
1698a94100faSBill Paul 	 */
1699a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_free != RL_TX_DESC_CNT)
1700a94100faSBill Paul                 CSR_WRITE_4(sc, RL_TIMERCNT, 1);
1701a94100faSBill Paul 
1702a94100faSBill Paul 	return;
1703a94100faSBill Paul }
1704a94100faSBill Paul 
1705a94100faSBill Paul static void
1706a94100faSBill Paul re_tick(xsc)
1707a94100faSBill Paul 	void			*xsc;
1708a94100faSBill Paul {
1709a94100faSBill Paul 	struct rl_softc		*sc;
1710a94100faSBill Paul 	struct mii_data		*mii;
1711a94100faSBill Paul 
1712a94100faSBill Paul 	sc = xsc;
1713a94100faSBill Paul 	RL_LOCK(sc);
1714a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
1715a94100faSBill Paul 
1716a94100faSBill Paul 	mii_tick(mii);
1717a94100faSBill Paul 
1718a94100faSBill Paul 	sc->rl_stat_ch = timeout(re_tick, sc, hz);
1719a94100faSBill Paul 	RL_UNLOCK(sc);
1720a94100faSBill Paul 
1721a94100faSBill Paul 	return;
1722a94100faSBill Paul }
1723a94100faSBill Paul 
1724a94100faSBill Paul #ifdef DEVICE_POLLING
1725a94100faSBill Paul static void
1726a94100faSBill Paul re_poll (struct ifnet *ifp, enum poll_cmd cmd, int count)
1727a94100faSBill Paul {
1728a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
1729a94100faSBill Paul 
1730a94100faSBill Paul 	RL_LOCK(sc);
1731a94100faSBill Paul 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
1732a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
1733a94100faSBill Paul 		goto done;
1734a94100faSBill Paul 	}
1735a94100faSBill Paul 
1736a94100faSBill Paul 	sc->rxcycles = count;
1737a94100faSBill Paul 	re_rxeof(sc);
1738a94100faSBill Paul 	re_txeof(sc);
1739a94100faSBill Paul 
1740a94100faSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1741a94100faSBill Paul 		(*ifp->if_start)(ifp);
1742a94100faSBill Paul 
1743a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
1744a94100faSBill Paul 		u_int16_t       status;
1745a94100faSBill Paul 
1746a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1747a94100faSBill Paul 		if (status == 0xffff)
1748a94100faSBill Paul 			goto done;
1749a94100faSBill Paul 		if (status)
1750a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1751a94100faSBill Paul 
1752a94100faSBill Paul 		/*
1753a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
1754a94100faSBill Paul 		 */
1755a94100faSBill Paul 
1756a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1757a94100faSBill Paul 			re_reset(sc);
1758a94100faSBill Paul 			re_init(sc);
1759a94100faSBill Paul 		}
1760a94100faSBill Paul 	}
1761a94100faSBill Paul done:
1762a94100faSBill Paul 	RL_UNLOCK(sc);
1763a94100faSBill Paul }
1764a94100faSBill Paul #endif /* DEVICE_POLLING */
1765a94100faSBill Paul 
1766a94100faSBill Paul static void
1767a94100faSBill Paul re_intr(arg)
1768a94100faSBill Paul 	void			*arg;
1769a94100faSBill Paul {
1770a94100faSBill Paul 	struct rl_softc		*sc;
1771a94100faSBill Paul 	struct ifnet		*ifp;
1772a94100faSBill Paul 	u_int16_t		status;
1773a94100faSBill Paul 
1774a94100faSBill Paul 	sc = arg;
1775a94100faSBill Paul 
1776a94100faSBill Paul 	if (sc->suspended) {
1777a94100faSBill Paul 		return;
1778a94100faSBill Paul 	}
1779a94100faSBill Paul 
1780a94100faSBill Paul 	RL_LOCK(sc);
1781a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
1782a94100faSBill Paul 
1783a94100faSBill Paul #ifdef DEVICE_POLLING
1784a94100faSBill Paul 	if  (ifp->if_flags & IFF_POLLING)
1785a94100faSBill Paul 		goto done;
1786a94100faSBill Paul 	if (ether_poll_register(re_poll, ifp)) { /* ok, disable interrupts */
1787a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0x0000);
1788a94100faSBill Paul 		re_poll(ifp, 0, 1);
1789a94100faSBill Paul 		goto done;
1790a94100faSBill Paul 	}
1791a94100faSBill Paul #endif /* DEVICE_POLLING */
1792a94100faSBill Paul 
1793a94100faSBill Paul 	for (;;) {
1794a94100faSBill Paul 
1795a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
1796a94100faSBill Paul 		/* If the card has gone away the read returns 0xffff. */
1797a94100faSBill Paul 		if (status == 0xffff)
1798a94100faSBill Paul 			break;
1799a94100faSBill Paul 		if (status)
1800a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
1801a94100faSBill Paul 
1802a94100faSBill Paul 		if ((status & RL_INTRS_CPLUS) == 0)
1803a94100faSBill Paul 			break;
1804a94100faSBill Paul 
1805a94100faSBill Paul 		if (status & RL_ISR_RX_OK)
1806a94100faSBill Paul 			re_rxeof(sc);
1807a94100faSBill Paul 
1808a94100faSBill Paul 		if (status & RL_ISR_RX_ERR)
1809a94100faSBill Paul 			re_rxeof(sc);
1810a94100faSBill Paul 
1811a94100faSBill Paul 		if ((status & RL_ISR_TIMEOUT_EXPIRED) ||
1812a94100faSBill Paul 		    (status & RL_ISR_TX_ERR) ||
1813a94100faSBill Paul 		    (status & RL_ISR_TX_DESC_UNAVAIL))
1814a94100faSBill Paul 			re_txeof(sc);
1815a94100faSBill Paul 
1816a94100faSBill Paul 		if (status & RL_ISR_SYSTEM_ERR) {
1817a94100faSBill Paul 			re_reset(sc);
1818a94100faSBill Paul 			re_init(sc);
1819a94100faSBill Paul 		}
1820a94100faSBill Paul 
1821a94100faSBill Paul 		if (status & RL_ISR_LINKCHG) {
1822a94100faSBill Paul 			untimeout(re_tick, sc, sc->rl_stat_ch);
1823a94100faSBill Paul 			re_tick(sc);
1824a94100faSBill Paul 		}
1825a94100faSBill Paul 	}
1826a94100faSBill Paul 
1827a94100faSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1828a94100faSBill Paul 		(*ifp->if_start)(ifp);
1829a94100faSBill Paul 
1830a94100faSBill Paul #ifdef DEVICE_POLLING
1831a94100faSBill Paul done:
1832a94100faSBill Paul #endif
1833a94100faSBill Paul 	RL_UNLOCK(sc);
1834a94100faSBill Paul 
1835a94100faSBill Paul 	return;
1836a94100faSBill Paul }
1837a94100faSBill Paul 
1838a94100faSBill Paul static int
1839a94100faSBill Paul re_encap(sc, m_head, idx)
1840a94100faSBill Paul 	struct rl_softc		*sc;
1841a94100faSBill Paul 	struct mbuf		*m_head;
1842a94100faSBill Paul 	int			*idx;
1843a94100faSBill Paul {
1844a94100faSBill Paul 	struct mbuf		*m_new = NULL;
1845a94100faSBill Paul 	struct rl_dmaload_arg	arg;
1846a94100faSBill Paul 	bus_dmamap_t		map;
1847a94100faSBill Paul 	int			error;
1848a94100faSBill Paul 	struct m_tag		*mtag;
1849a94100faSBill Paul 
1850a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_free < 4)
1851a94100faSBill Paul 		return(EFBIG);
1852a94100faSBill Paul 
1853a94100faSBill Paul 	/*
1854a94100faSBill Paul 	 * Set up checksum offload. Note: checksum offload bits must
1855a94100faSBill Paul 	 * appear in all descriptors of a multi-descriptor transmit
1856a94100faSBill Paul 	 * attempt. (This is according to testing done with an 8169
1857a94100faSBill Paul 	 * chip. I'm not sure if this is a requirement or a bug.)
1858a94100faSBill Paul 	 */
1859a94100faSBill Paul 
1860a94100faSBill Paul 	arg.rl_flags = 0;
1861a94100faSBill Paul 
1862a94100faSBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_IP)
1863a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_IPCSUM;
1864a94100faSBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
1865a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_TCPCSUM;
1866a94100faSBill Paul 	if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
1867a94100faSBill Paul 		arg.rl_flags |= RL_TDESC_CMD_UDPCSUM;
1868a94100faSBill Paul 
1869a94100faSBill Paul 	arg.sc = sc;
1870a94100faSBill Paul 	arg.rl_idx = *idx;
1871a94100faSBill Paul 	arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
1872a94100faSBill Paul 	arg.rl_ring = sc->rl_ldata.rl_tx_list;
1873a94100faSBill Paul 
1874a94100faSBill Paul 	map = sc->rl_ldata.rl_tx_dmamap[*idx];
1875a94100faSBill Paul 	error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
1876a94100faSBill Paul 	    m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1877a94100faSBill Paul 
1878a94100faSBill Paul 	if (error && error != EFBIG) {
1879a94100faSBill Paul 		printf("re%d: can't map mbuf (error %d)\n", sc->rl_unit, error);
1880a94100faSBill Paul 		return(ENOBUFS);
1881a94100faSBill Paul 	}
1882a94100faSBill Paul 
1883a94100faSBill Paul 	/* Too many segments to map, coalesce into a single mbuf */
1884a94100faSBill Paul 
1885a94100faSBill Paul 	if (error || arg.rl_maxsegs == 0) {
1886a94100faSBill Paul 		m_new = m_defrag(m_head, M_DONTWAIT);
1887a94100faSBill Paul 		if (m_new == NULL)
1888a94100faSBill Paul 			return(1);
1889a94100faSBill Paul 		else
1890a94100faSBill Paul 			m_head = m_new;
1891a94100faSBill Paul 
1892a94100faSBill Paul 		arg.sc = sc;
1893a94100faSBill Paul 		arg.rl_idx = *idx;
1894a94100faSBill Paul 		arg.rl_maxsegs = sc->rl_ldata.rl_tx_free;
1895a94100faSBill Paul 		arg.rl_ring = sc->rl_ldata.rl_tx_list;
1896a94100faSBill Paul 
1897a94100faSBill Paul 		error = bus_dmamap_load_mbuf(sc->rl_ldata.rl_mtag, map,
1898a94100faSBill Paul 		    m_head, re_dma_map_desc, &arg, BUS_DMA_NOWAIT);
1899a94100faSBill Paul 		if (error) {
1900a94100faSBill Paul 			printf("re%d: can't map mbuf (error %d)\n",
1901a94100faSBill Paul 			    sc->rl_unit, error);
1902a94100faSBill Paul 			return(EFBIG);
1903a94100faSBill Paul 		}
1904a94100faSBill Paul 	}
1905a94100faSBill Paul 
1906a94100faSBill Paul 	/*
1907a94100faSBill Paul 	 * Insure that the map for this transmission
1908a94100faSBill Paul 	 * is placed at the array index of the last descriptor
1909a94100faSBill Paul 	 * in this chain.
1910a94100faSBill Paul 	 */
1911a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[*idx] =
1912a94100faSBill Paul 	    sc->rl_ldata.rl_tx_dmamap[arg.rl_idx];
1913a94100faSBill Paul 	sc->rl_ldata.rl_tx_dmamap[arg.rl_idx] = map;
1914a94100faSBill Paul 
1915a94100faSBill Paul 	sc->rl_ldata.rl_tx_mbuf[arg.rl_idx] = m_head;
1916a94100faSBill Paul 	sc->rl_ldata.rl_tx_free -= arg.rl_maxsegs;
1917a94100faSBill Paul 
1918a94100faSBill Paul 	/*
1919a94100faSBill Paul 	 * Set up hardware VLAN tagging. Note: vlan tag info must
1920a94100faSBill Paul 	 * appear in the first descriptor of a multi-descriptor
1921a94100faSBill Paul 	 * transmission attempt.
1922a94100faSBill Paul 	 */
1923a94100faSBill Paul 
1924a94100faSBill Paul 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m_head);
1925a94100faSBill Paul 	if (mtag != NULL)
1926a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_vlanctl =
1927a94100faSBill Paul 		    htole32(htons(VLAN_TAG_VALUE(mtag)) | RL_TDESC_VLANCTL_TAG);
1928a94100faSBill Paul 
1929a94100faSBill Paul 	/* Transfer ownership of packet to the chip. */
1930a94100faSBill Paul 
1931a94100faSBill Paul 	sc->rl_ldata.rl_tx_list[arg.rl_idx].rl_cmdstat |=
1932a94100faSBill Paul 	    htole32(RL_TDESC_CMD_OWN);
1933a94100faSBill Paul 	if (*idx != arg.rl_idx)
1934a94100faSBill Paul 		sc->rl_ldata.rl_tx_list[*idx].rl_cmdstat |=
1935a94100faSBill Paul 		    htole32(RL_TDESC_CMD_OWN);
1936a94100faSBill Paul 
1937a94100faSBill Paul 	RL_DESC_INC(arg.rl_idx);
1938a94100faSBill Paul 	*idx = arg.rl_idx;
1939a94100faSBill Paul 
1940a94100faSBill Paul 	return(0);
1941a94100faSBill Paul }
1942a94100faSBill Paul 
1943a94100faSBill Paul /*
1944a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
1945a94100faSBill Paul  */
1946a94100faSBill Paul 
1947a94100faSBill Paul static void
1948a94100faSBill Paul re_start(ifp)
1949a94100faSBill Paul 	struct ifnet		*ifp;
1950a94100faSBill Paul {
1951a94100faSBill Paul 	struct rl_softc		*sc;
1952a94100faSBill Paul 	struct mbuf		*m_head = NULL;
1953a94100faSBill Paul 	int			idx;
1954a94100faSBill Paul 
1955a94100faSBill Paul 	sc = ifp->if_softc;
1956a94100faSBill Paul 	RL_LOCK(sc);
1957a94100faSBill Paul 
1958a94100faSBill Paul 	idx = sc->rl_ldata.rl_tx_prodidx;
1959a94100faSBill Paul 
1960a94100faSBill Paul 	while (sc->rl_ldata.rl_tx_mbuf[idx] == NULL) {
1961a94100faSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1962a94100faSBill Paul 		if (m_head == NULL)
1963a94100faSBill Paul 			break;
1964a94100faSBill Paul 
1965a94100faSBill Paul 		if (re_encap(sc, m_head, &idx)) {
1966a94100faSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1967a94100faSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1968a94100faSBill Paul 			break;
1969a94100faSBill Paul 		}
1970a94100faSBill Paul 
1971a94100faSBill Paul 		/*
1972a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1973a94100faSBill Paul 		 * to him.
1974a94100faSBill Paul 		 */
1975a94100faSBill Paul 		BPF_MTAP(ifp, m_head);
1976a94100faSBill Paul 	}
1977a94100faSBill Paul 
1978a94100faSBill Paul 	/* Flush the TX descriptors */
1979a94100faSBill Paul 
1980a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1981a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
1982a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1983a94100faSBill Paul 
1984a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = idx;
1985a94100faSBill Paul 
1986a94100faSBill Paul 	/*
1987a94100faSBill Paul 	 * RealTek put the TX poll request register in a different
1988a94100faSBill Paul 	 * location on the 8169 gigE chip. I don't know why.
1989a94100faSBill Paul 	 */
1990a94100faSBill Paul 
1991a94100faSBill Paul 	if (sc->rl_type == RL_8169)
1992a94100faSBill Paul 		CSR_WRITE_2(sc, RL_GTXSTART, RL_TXSTART_START);
1993a94100faSBill Paul 	else
1994a94100faSBill Paul 		CSR_WRITE_2(sc, RL_TXSTART, RL_TXSTART_START);
1995a94100faSBill Paul 
1996a94100faSBill Paul 	/*
1997a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
1998a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
1999a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2000a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2001a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2002a94100faSBill Paul 	 * the timer count is reset to 0.
2003a94100faSBill Paul 	 */
2004a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2005a94100faSBill Paul 
2006a94100faSBill Paul 	RL_UNLOCK(sc);
2007a94100faSBill Paul 
2008a94100faSBill Paul 	/*
2009a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2010a94100faSBill Paul 	 */
2011a94100faSBill Paul 	ifp->if_timer = 5;
2012a94100faSBill Paul 
2013a94100faSBill Paul 	return;
2014a94100faSBill Paul }
2015a94100faSBill Paul 
2016a94100faSBill Paul static void
2017a94100faSBill Paul re_init(xsc)
2018a94100faSBill Paul 	void			*xsc;
2019a94100faSBill Paul {
2020a94100faSBill Paul 	struct rl_softc		*sc = xsc;
2021a94100faSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
2022a94100faSBill Paul 	struct mii_data		*mii;
2023a94100faSBill Paul 	u_int32_t		rxcfg = 0;
2024a94100faSBill Paul 
2025a94100faSBill Paul 	RL_LOCK(sc);
2026a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2027a94100faSBill Paul 
2028a94100faSBill Paul 	/*
2029a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2030a94100faSBill Paul 	 */
2031a94100faSBill Paul 	re_stop(sc);
2032a94100faSBill Paul 
2033a94100faSBill Paul 	/*
2034a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2035a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2036a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2037a94100faSBill Paul 	 */
2038a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2039a94100faSBill Paul 	CSR_WRITE_STREAM_4(sc, RL_IDR0,
2040a94100faSBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
2041a94100faSBill Paul 	CSR_WRITE_STREAM_4(sc, RL_IDR4,
2042a94100faSBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
2043a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2044a94100faSBill Paul 
2045a94100faSBill Paul 	/*
2046a94100faSBill Paul 	 * For C+ mode, initialize the RX descriptors and mbufs.
2047a94100faSBill Paul 	 */
2048a94100faSBill Paul 	re_rx_list_init(sc);
2049a94100faSBill Paul 	re_tx_list_init(sc);
2050a94100faSBill Paul 
2051a94100faSBill Paul 	/*
2052a94100faSBill Paul 	 * Enable transmit and receive.
2053a94100faSBill Paul 	 */
2054a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2055a94100faSBill Paul 
2056a94100faSBill Paul 	/*
2057a94100faSBill Paul 	 * Set the initial TX and RX configuration.
2058a94100faSBill Paul 	 */
2059a94100faSBill Paul 	if (sc->rl_testmode)
2060a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2061a94100faSBill Paul 	else
2062a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2063a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXCFG, RL_RXCFG_CONFIG);
2064a94100faSBill Paul 
2065a94100faSBill Paul 	/* Set the individual bit to receive frames for this host only. */
2066a94100faSBill Paul 	rxcfg = CSR_READ_4(sc, RL_RXCFG);
2067a94100faSBill Paul 	rxcfg |= RL_RXCFG_RX_INDIV;
2068a94100faSBill Paul 
2069a94100faSBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
2070a94100faSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
2071a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_ALLPHYS;
2072a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2073a94100faSBill Paul 	} else {
2074a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_ALLPHYS;
2075a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2076a94100faSBill Paul 	}
2077a94100faSBill Paul 
2078a94100faSBill Paul 	/*
2079a94100faSBill Paul 	 * Set capture broadcast bit to capture broadcast frames.
2080a94100faSBill Paul 	 */
2081a94100faSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
2082a94100faSBill Paul 		rxcfg |= RL_RXCFG_RX_BROAD;
2083a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2084a94100faSBill Paul 	} else {
2085a94100faSBill Paul 		rxcfg &= ~RL_RXCFG_RX_BROAD;
2086a94100faSBill Paul 		CSR_WRITE_4(sc, RL_RXCFG, rxcfg);
2087a94100faSBill Paul 	}
2088a94100faSBill Paul 
2089a94100faSBill Paul 	/*
2090a94100faSBill Paul 	 * Program the multicast filter, if necessary.
2091a94100faSBill Paul 	 */
2092a94100faSBill Paul 	re_setmulti(sc);
2093a94100faSBill Paul 
2094a94100faSBill Paul #ifdef DEVICE_POLLING
2095a94100faSBill Paul 	/*
2096a94100faSBill Paul 	 * Disable interrupts if we are polling.
2097a94100faSBill Paul 	 */
2098a94100faSBill Paul 	if (ifp->if_flags & IFF_POLLING)
2099a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2100a94100faSBill Paul 	else	/* otherwise ... */
2101a94100faSBill Paul #endif /* DEVICE_POLLING */
2102a94100faSBill Paul 	/*
2103a94100faSBill Paul 	 * Enable interrupts.
2104a94100faSBill Paul 	 */
2105a94100faSBill Paul 	if (sc->rl_testmode)
2106a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2107a94100faSBill Paul 	else
2108a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2109a94100faSBill Paul 
2110a94100faSBill Paul 	/* Set initial TX threshold */
2111a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2112a94100faSBill Paul 
2113a94100faSBill Paul 	/* Start RX/TX process. */
2114a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2115a94100faSBill Paul #ifdef notdef
2116a94100faSBill Paul 	/* Enable receiver and transmitter. */
2117a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2118a94100faSBill Paul #endif
2119a94100faSBill Paul 	/*
2120a94100faSBill Paul 	 * If this is a C+ capable chip, enable C+ RX and TX mode,
2121a94100faSBill Paul 	 * and load the addresses of the RX and TX lists into the chip.
2122a94100faSBill Paul 	 */
2123a94100faSBill Paul 	CSR_WRITE_2(sc, RL_CPLUS_CMD, RL_CPLUSCMD_RXENB|
2124a94100faSBill Paul 	    RL_CPLUSCMD_TXENB|RL_CPLUSCMD_PCI_MRW|
2125a94100faSBill Paul 	    RL_CPLUSCMD_VLANSTRIP|
2126a94100faSBill Paul 	    (ifp->if_capenable & IFCAP_RXCSUM ?
2127a94100faSBill Paul 	    RL_CPLUSCMD_RXCSUM_ENB : 0));
2128a94100faSBill Paul 
2129a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2130a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2131a94100faSBill Paul 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2132a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2133a94100faSBill Paul 
2134a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2135a94100faSBill Paul 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2136a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2137a94100faSBill Paul 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2138a94100faSBill Paul 
2139a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2140a94100faSBill Paul 
2141a94100faSBill Paul 	/*
2142a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2143a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2144a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2145a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2146a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2147a94100faSBill Paul 	 */
2148a94100faSBill Paul 
2149a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2150a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
2151a94100faSBill Paul 	else
2152a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
2153a94100faSBill Paul 
2154a94100faSBill Paul 	/*
2155a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
2156a94100faSBill Paul 	 * size so we can receive jumbo frames.
2157a94100faSBill Paul 	 */
2158a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2159a94100faSBill Paul 		CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
2160a94100faSBill Paul 
2161a94100faSBill Paul 	if (sc->rl_testmode) {
2162a94100faSBill Paul 		RL_UNLOCK(sc);
2163a94100faSBill Paul 		return;
2164a94100faSBill Paul 	}
2165a94100faSBill Paul 
2166a94100faSBill Paul 	mii_mediachg(mii);
2167a94100faSBill Paul 
2168a94100faSBill Paul 	CSR_WRITE_1(sc, RL_CFG1, RL_CFG1_DRVLOAD|RL_CFG1_FULLDUPLEX);
2169a94100faSBill Paul 
2170a94100faSBill Paul 	ifp->if_flags |= IFF_RUNNING;
2171a94100faSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
2172a94100faSBill Paul 
2173a94100faSBill Paul 	sc->rl_stat_ch = timeout(re_tick, sc, hz);
2174a94100faSBill Paul 	RL_UNLOCK(sc);
2175a94100faSBill Paul 
2176a94100faSBill Paul 	return;
2177a94100faSBill Paul }
2178a94100faSBill Paul 
2179a94100faSBill Paul /*
2180a94100faSBill Paul  * Set media options.
2181a94100faSBill Paul  */
2182a94100faSBill Paul static int
2183a94100faSBill Paul re_ifmedia_upd(ifp)
2184a94100faSBill Paul 	struct ifnet		*ifp;
2185a94100faSBill Paul {
2186a94100faSBill Paul 	struct rl_softc		*sc;
2187a94100faSBill Paul 	struct mii_data		*mii;
2188a94100faSBill Paul 
2189a94100faSBill Paul 	sc = ifp->if_softc;
2190a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2191a94100faSBill Paul 	mii_mediachg(mii);
2192a94100faSBill Paul 
2193a94100faSBill Paul 	return(0);
2194a94100faSBill Paul }
2195a94100faSBill Paul 
2196a94100faSBill Paul /*
2197a94100faSBill Paul  * Report current media status.
2198a94100faSBill Paul  */
2199a94100faSBill Paul static void
2200a94100faSBill Paul re_ifmedia_sts(ifp, ifmr)
2201a94100faSBill Paul 	struct ifnet		*ifp;
2202a94100faSBill Paul 	struct ifmediareq	*ifmr;
2203a94100faSBill Paul {
2204a94100faSBill Paul 	struct rl_softc		*sc;
2205a94100faSBill Paul 	struct mii_data		*mii;
2206a94100faSBill Paul 
2207a94100faSBill Paul 	sc = ifp->if_softc;
2208a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2209a94100faSBill Paul 
2210a94100faSBill Paul 	mii_pollstat(mii);
2211a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2212a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
2213a94100faSBill Paul 
2214a94100faSBill Paul 	return;
2215a94100faSBill Paul }
2216a94100faSBill Paul 
2217a94100faSBill Paul static int
2218a94100faSBill Paul re_ioctl(ifp, command, data)
2219a94100faSBill Paul 	struct ifnet		*ifp;
2220a94100faSBill Paul 	u_long			command;
2221a94100faSBill Paul 	caddr_t			data;
2222a94100faSBill Paul {
2223a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
2224a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2225a94100faSBill Paul 	struct mii_data		*mii;
2226a94100faSBill Paul 	int			error = 0;
2227a94100faSBill Paul 
2228a94100faSBill Paul 	RL_LOCK(sc);
2229a94100faSBill Paul 
2230a94100faSBill Paul 	switch(command) {
2231a94100faSBill Paul 	case SIOCSIFMTU:
2232a94100faSBill Paul 		if (ifr->ifr_mtu > RL_JUMBO_MTU)
2233a94100faSBill Paul 			error = EINVAL;
2234a94100faSBill Paul 		ifp->if_mtu = ifr->ifr_mtu;
2235a94100faSBill Paul 		break;
2236a94100faSBill Paul 	case SIOCSIFFLAGS:
2237a94100faSBill Paul 		if (ifp->if_flags & IFF_UP) {
2238a94100faSBill Paul 			re_init(sc);
2239a94100faSBill Paul 		} else {
2240a94100faSBill Paul 			if (ifp->if_flags & IFF_RUNNING)
2241a94100faSBill Paul 				re_stop(sc);
2242a94100faSBill Paul 		}
2243a94100faSBill Paul 		error = 0;
2244a94100faSBill Paul 		break;
2245a94100faSBill Paul 	case SIOCADDMULTI:
2246a94100faSBill Paul 	case SIOCDELMULTI:
2247a94100faSBill Paul 		re_setmulti(sc);
2248a94100faSBill Paul 		error = 0;
2249a94100faSBill Paul 		break;
2250a94100faSBill Paul 	case SIOCGIFMEDIA:
2251a94100faSBill Paul 	case SIOCSIFMEDIA:
2252a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
2253a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2254a94100faSBill Paul 		break;
2255a94100faSBill Paul 	case SIOCSIFCAP:
2256a94100faSBill Paul 		ifp->if_capenable = ifr->ifr_reqcap;
2257a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_TXCSUM)
2258a94100faSBill Paul 			ifp->if_hwassist = RE_CSUM_FEATURES;
2259a94100faSBill Paul 		else
2260a94100faSBill Paul 			ifp->if_hwassist = 0;
2261a94100faSBill Paul 		if (ifp->if_flags & IFF_RUNNING)
2262a94100faSBill Paul 			re_init(sc);
2263a94100faSBill Paul 		break;
2264a94100faSBill Paul 	default:
2265a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
2266a94100faSBill Paul 		break;
2267a94100faSBill Paul 	}
2268a94100faSBill Paul 
2269a94100faSBill Paul 	RL_UNLOCK(sc);
2270a94100faSBill Paul 
2271a94100faSBill Paul 	return(error);
2272a94100faSBill Paul }
2273a94100faSBill Paul 
2274a94100faSBill Paul static void
2275a94100faSBill Paul re_watchdog(ifp)
2276a94100faSBill Paul 	struct ifnet		*ifp;
2277a94100faSBill Paul {
2278a94100faSBill Paul 	struct rl_softc		*sc;
2279a94100faSBill Paul 
2280a94100faSBill Paul 	sc = ifp->if_softc;
2281a94100faSBill Paul 	RL_LOCK(sc);
2282a94100faSBill Paul 	printf("re%d: watchdog timeout\n", sc->rl_unit);
2283a94100faSBill Paul 	ifp->if_oerrors++;
2284a94100faSBill Paul 
2285a94100faSBill Paul 	re_txeof(sc);
2286a94100faSBill Paul 	re_rxeof(sc);
2287a94100faSBill Paul 
2288a94100faSBill Paul 	re_init(sc);
2289a94100faSBill Paul 
2290a94100faSBill Paul 	RL_UNLOCK(sc);
2291a94100faSBill Paul 
2292a94100faSBill Paul 	return;
2293a94100faSBill Paul }
2294a94100faSBill Paul 
2295a94100faSBill Paul /*
2296a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
2297a94100faSBill Paul  * RX and TX lists.
2298a94100faSBill Paul  */
2299a94100faSBill Paul static void
2300a94100faSBill Paul re_stop(sc)
2301a94100faSBill Paul 	struct rl_softc		*sc;
2302a94100faSBill Paul {
2303a94100faSBill Paul 	register int		i;
2304a94100faSBill Paul 	struct ifnet		*ifp;
2305a94100faSBill Paul 
2306a94100faSBill Paul 	RL_LOCK(sc);
2307a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
2308a94100faSBill Paul 	ifp->if_timer = 0;
2309a94100faSBill Paul 
2310a94100faSBill Paul 	untimeout(re_tick, sc, sc->rl_stat_ch);
2311a94100faSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2312a94100faSBill Paul #ifdef DEVICE_POLLING
2313a94100faSBill Paul 	ether_poll_deregister(ifp);
2314a94100faSBill Paul #endif /* DEVICE_POLLING */
2315a94100faSBill Paul 
2316a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, 0x00);
2317a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
2318a94100faSBill Paul 
2319a94100faSBill Paul 	if (sc->rl_head != NULL) {
2320a94100faSBill Paul 		m_freem(sc->rl_head);
2321a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
2322a94100faSBill Paul 	}
2323a94100faSBill Paul 
2324a94100faSBill Paul 	/* Free the TX list buffers. */
2325a94100faSBill Paul 
2326a94100faSBill Paul 	for (i = 0; i < RL_TX_DESC_CNT; i++) {
2327a94100faSBill Paul 		if (sc->rl_ldata.rl_tx_mbuf[i] != NULL) {
2328a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2329a94100faSBill Paul 			    sc->rl_ldata.rl_tx_dmamap[i]);
2330a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_tx_mbuf[i]);
2331a94100faSBill Paul 			sc->rl_ldata.rl_tx_mbuf[i] = NULL;
2332a94100faSBill Paul 		}
2333a94100faSBill Paul 	}
2334a94100faSBill Paul 
2335a94100faSBill Paul 	/* Free the RX list buffers. */
2336a94100faSBill Paul 
2337a94100faSBill Paul 	for (i = 0; i < RL_RX_DESC_CNT; i++) {
2338a94100faSBill Paul 		if (sc->rl_ldata.rl_rx_mbuf[i] != NULL) {
2339a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_mtag,
2340a94100faSBill Paul 			    sc->rl_ldata.rl_rx_dmamap[i]);
2341a94100faSBill Paul 			m_freem(sc->rl_ldata.rl_rx_mbuf[i]);
2342a94100faSBill Paul 			sc->rl_ldata.rl_rx_mbuf[i] = NULL;
2343a94100faSBill Paul 		}
2344a94100faSBill Paul 	}
2345a94100faSBill Paul 
2346a94100faSBill Paul 	RL_UNLOCK(sc);
2347a94100faSBill Paul 	return;
2348a94100faSBill Paul }
2349a94100faSBill Paul 
2350a94100faSBill Paul /*
2351a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
2352a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
2353a94100faSBill Paul  * resume.
2354a94100faSBill Paul  */
2355a94100faSBill Paul static int
2356a94100faSBill Paul re_suspend(dev)
2357a94100faSBill Paul 	device_t		dev;
2358a94100faSBill Paul {
2359a94100faSBill Paul 	register int		i;
2360a94100faSBill Paul 	struct rl_softc		*sc;
2361a94100faSBill Paul 
2362a94100faSBill Paul 	sc = device_get_softc(dev);
2363a94100faSBill Paul 
2364a94100faSBill Paul 	re_stop(sc);
2365a94100faSBill Paul 
2366a94100faSBill Paul 	for (i = 0; i < 5; i++)
2367a94100faSBill Paul 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
2368a94100faSBill Paul 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
2369a94100faSBill Paul 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
2370a94100faSBill Paul 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
2371a94100faSBill Paul 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
2372a94100faSBill Paul 
2373a94100faSBill Paul 	sc->suspended = 1;
2374a94100faSBill Paul 
2375a94100faSBill Paul 	return (0);
2376a94100faSBill Paul }
2377a94100faSBill Paul 
2378a94100faSBill Paul /*
2379a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
2380a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
2381a94100faSBill Paul  * appropriate.
2382a94100faSBill Paul  */
2383a94100faSBill Paul static int
2384a94100faSBill Paul re_resume(dev)
2385a94100faSBill Paul 	device_t		dev;
2386a94100faSBill Paul {
2387a94100faSBill Paul 	register int		i;
2388a94100faSBill Paul 	struct rl_softc		*sc;
2389a94100faSBill Paul 	struct ifnet		*ifp;
2390a94100faSBill Paul 
2391a94100faSBill Paul 	sc = device_get_softc(dev);
2392a94100faSBill Paul 	ifp = &sc->arpcom.ac_if;
2393a94100faSBill Paul 
2394a94100faSBill Paul 	/* better way to do this? */
2395a94100faSBill Paul 	for (i = 0; i < 5; i++)
2396a94100faSBill Paul 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
2397a94100faSBill Paul 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
2398a94100faSBill Paul 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
2399a94100faSBill Paul 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
2400a94100faSBill Paul 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
2401a94100faSBill Paul 
2402a94100faSBill Paul 	/* reenable busmastering */
2403a94100faSBill Paul 	pci_enable_busmaster(dev);
2404a94100faSBill Paul 	pci_enable_io(dev, RL_RES);
2405a94100faSBill Paul 
2406a94100faSBill Paul 	/* reinitialize interface if necessary */
2407a94100faSBill Paul 	if (ifp->if_flags & IFF_UP)
2408a94100faSBill Paul 		re_init(sc);
2409a94100faSBill Paul 
2410a94100faSBill Paul 	sc->suspended = 0;
2411a94100faSBill Paul 
2412a94100faSBill Paul 	return (0);
2413a94100faSBill Paul }
2414a94100faSBill Paul 
2415a94100faSBill Paul /*
2416a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2417a94100faSBill Paul  * get confused by errant DMAs when rebooting.
2418a94100faSBill Paul  */
2419a94100faSBill Paul static void
2420a94100faSBill Paul re_shutdown(dev)
2421a94100faSBill Paul 	device_t		dev;
2422a94100faSBill Paul {
2423a94100faSBill Paul 	struct rl_softc		*sc;
2424a94100faSBill Paul 
2425a94100faSBill Paul 	sc = device_get_softc(dev);
2426a94100faSBill Paul 
2427a94100faSBill Paul 	re_stop(sc);
2428a94100faSBill Paul 
2429a94100faSBill Paul 	return;
2430a94100faSBill Paul }
2431