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