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