xref: /freebsd/sys/dev/nge/if_nge.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*
2  * Copyright (c) 2001 Wind River Systems
3  * Copyright (c) 1997, 1998, 1999, 2000, 2001
4  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Bill Paul.
17  * 4. Neither the name of the author nor the names of any co-contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 /*
38  * National Semiconductor DP83820/DP83821 gigabit ethernet driver
39  * for FreeBSD. Datasheets are available from:
40  *
41  * http://www.national.com/ds/DP/DP83820.pdf
42  * http://www.national.com/ds/DP/DP83821.pdf
43  *
44  * These chips are used on several low cost gigabit ethernet NICs
45  * sold by D-Link, Addtron, SMC and Asante. Both parts are
46  * virtually the same, except the 83820 is a 64-bit/32-bit part,
47  * while the 83821 is 32-bit only.
48  *
49  * Many cards also use National gigE transceivers, such as the
50  * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet
51  * contains a full register description that applies to all of these
52  * components:
53  *
54  * http://www.national.com/ds/DP/DP83861.pdf
55  *
56  * Written by Bill Paul <wpaul@bsdi.com>
57  * BSDi Open Source Solutions
58  */
59 
60 /*
61  * The NatSemi DP83820 and 83821 controllers are enhanced versions
62  * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100
63  * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII
64  * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP
65  * hardware checksum offload (IPv4 only), VLAN tagging and filtering,
66  * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern
67  * matching buffers, one perfect address filter buffer and interrupt
68  * moderation. The 83820 supports both 64-bit and 32-bit addressing
69  * and data transfers: the 64-bit support can be toggled on or off
70  * via software. This affects the size of certain fields in the DMA
71  * descriptors.
72  *
73  * There are two bugs/misfeatures in the 83820/83821 that I have
74  * discovered so far:
75  *
76  * - Receive buffers must be aligned on 64-bit boundaries, which means
77  *   you must resort to copying data in order to fix up the payload
78  *   alignment.
79  *
80  * - In order to transmit jumbo frames larger than 8170 bytes, you have
81  *   to turn off transmit checksum offloading, because the chip can't
82  *   compute the checksum on an outgoing frame unless it fits entirely
83  *   within the TX FIFO, which is only 8192 bytes in size. If you have
84  *   TX checksum offload enabled and you transmit attempt to transmit a
85  *   frame larger than 8170 bytes, the transmitter will wedge.
86  *
87  * To work around the latter problem, TX checksum offload is disabled
88  * if the user selects an MTU larger than 8152 (8170 - 18).
89  */
90 
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/sockio.h>
94 #include <sys/mbuf.h>
95 #include <sys/malloc.h>
96 #include <sys/kernel.h>
97 #include <sys/socket.h>
98 
99 #include <net/if.h>
100 #include <net/if_arp.h>
101 #include <net/ethernet.h>
102 #include <net/if_dl.h>
103 #include <net/if_media.h>
104 #include <net/if_types.h>
105 #include <net/if_vlan_var.h>
106 
107 #include <net/bpf.h>
108 
109 #include <vm/vm.h>              /* for vtophys */
110 #include <vm/pmap.h>            /* for vtophys */
111 #include <machine/clock.h>      /* for DELAY */
112 #include <machine/bus_pio.h>
113 #include <machine/bus_memio.h>
114 #include <machine/bus.h>
115 #include <machine/resource.h>
116 #include <sys/bus.h>
117 #include <sys/rman.h>
118 
119 #include <dev/mii/mii.h>
120 #include <dev/mii/miivar.h>
121 
122 #include <dev/pci/pcireg.h>
123 #include <dev/pci/pcivar.h>
124 
125 #define NGE_USEIOSPACE
126 
127 #include <dev/nge/if_ngereg.h>
128 
129 MODULE_DEPEND(nge, pci, 1, 1, 1);
130 MODULE_DEPEND(nge, ether, 1, 1, 1);
131 MODULE_DEPEND(nge, miibus, 1, 1, 1);
132 
133 /* "controller miibus0" required.  See GENERIC if you get errors here. */
134 #include "miibus_if.h"
135 
136 #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
137 
138 /*
139  * Various supported device vendors/types and their names.
140  */
141 static struct nge_type nge_devs[] = {
142 	{ NGE_VENDORID, NGE_DEVICEID,
143 	    "National Semiconductor Gigabit Ethernet" },
144 	{ 0, 0, NULL }
145 };
146 
147 static int nge_probe(device_t);
148 static int nge_attach(device_t);
149 static int nge_detach(device_t);
150 
151 static int nge_alloc_jumbo_mem(struct nge_softc *);
152 static void nge_free_jumbo_mem(struct nge_softc *);
153 static void *nge_jalloc(struct nge_softc *);
154 static void nge_jfree(void *, void *);
155 
156 static int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *);
157 static int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *);
158 static void nge_rxeof(struct nge_softc *);
159 static void nge_txeof(struct nge_softc *);
160 static void nge_intr(void *);
161 static void nge_tick(void *);
162 static void nge_start(struct ifnet *);
163 static int nge_ioctl(struct ifnet *, u_long, caddr_t);
164 static void nge_init(void *);
165 static void nge_stop(struct nge_softc *);
166 static void nge_watchdog(struct ifnet *);
167 static void nge_shutdown(device_t);
168 static int nge_ifmedia_upd(struct ifnet *);
169 static void nge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
170 
171 static void nge_delay(struct nge_softc *);
172 static void nge_eeprom_idle(struct nge_softc *);
173 static void nge_eeprom_putbyte(struct nge_softc *, int);
174 static void nge_eeprom_getword(struct nge_softc *, int, u_int16_t *);
175 static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int, int);
176 
177 static void nge_mii_sync(struct nge_softc *);
178 static void nge_mii_send(struct nge_softc *, u_int32_t, int);
179 static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *);
180 static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *);
181 
182 static int nge_miibus_readreg(device_t, int, int);
183 static int nge_miibus_writereg(device_t, int, int, int);
184 static void nge_miibus_statchg(device_t);
185 
186 static void nge_setmulti(struct nge_softc *);
187 static uint32_t nge_mchash(const uint8_t *);
188 static void nge_reset(struct nge_softc *);
189 static int nge_list_rx_init(struct nge_softc *);
190 static int nge_list_tx_init(struct nge_softc *);
191 
192 #ifdef NGE_USEIOSPACE
193 #define NGE_RES			SYS_RES_IOPORT
194 #define NGE_RID			NGE_PCI_LOIO
195 #else
196 #define NGE_RES			SYS_RES_MEMORY
197 #define NGE_RID			NGE_PCI_LOMEM
198 #endif
199 
200 static device_method_t nge_methods[] = {
201 	/* Device interface */
202 	DEVMETHOD(device_probe,		nge_probe),
203 	DEVMETHOD(device_attach,	nge_attach),
204 	DEVMETHOD(device_detach,	nge_detach),
205 	DEVMETHOD(device_shutdown,	nge_shutdown),
206 
207 	/* bus interface */
208 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
209 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
210 
211 	/* MII interface */
212 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
213 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
214 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
215 
216 	{ 0, 0 }
217 };
218 
219 static driver_t nge_driver = {
220 	"nge",
221 	nge_methods,
222 	sizeof(struct nge_softc)
223 };
224 
225 static devclass_t nge_devclass;
226 
227 DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
228 DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
229 
230 #define NGE_SETBIT(sc, reg, x)				\
231 	CSR_WRITE_4(sc, reg,				\
232 		CSR_READ_4(sc, reg) | (x))
233 
234 #define NGE_CLRBIT(sc, reg, x)				\
235 	CSR_WRITE_4(sc, reg,				\
236 		CSR_READ_4(sc, reg) & ~(x))
237 
238 #define SIO_SET(x)					\
239 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
240 
241 #define SIO_CLR(x)					\
242 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
243 
244 static void
245 nge_delay(sc)
246 	struct nge_softc	*sc;
247 {
248 	int			idx;
249 
250 	for (idx = (300 / 33) + 1; idx > 0; idx--)
251 		CSR_READ_4(sc, NGE_CSR);
252 
253 	return;
254 }
255 
256 static void
257 nge_eeprom_idle(sc)
258 	struct nge_softc	*sc;
259 {
260 	register int		i;
261 
262 	SIO_SET(NGE_MEAR_EE_CSEL);
263 	nge_delay(sc);
264 	SIO_SET(NGE_MEAR_EE_CLK);
265 	nge_delay(sc);
266 
267 	for (i = 0; i < 25; i++) {
268 		SIO_CLR(NGE_MEAR_EE_CLK);
269 		nge_delay(sc);
270 		SIO_SET(NGE_MEAR_EE_CLK);
271 		nge_delay(sc);
272 	}
273 
274 	SIO_CLR(NGE_MEAR_EE_CLK);
275 	nge_delay(sc);
276 	SIO_CLR(NGE_MEAR_EE_CSEL);
277 	nge_delay(sc);
278 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
279 
280 	return;
281 }
282 
283 /*
284  * Send a read command and address to the EEPROM, check for ACK.
285  */
286 static void
287 nge_eeprom_putbyte(sc, addr)
288 	struct nge_softc	*sc;
289 	int			addr;
290 {
291 	register int		d, i;
292 
293 	d = addr | NGE_EECMD_READ;
294 
295 	/*
296 	 * Feed in each bit and stobe the clock.
297 	 */
298 	for (i = 0x400; i; i >>= 1) {
299 		if (d & i) {
300 			SIO_SET(NGE_MEAR_EE_DIN);
301 		} else {
302 			SIO_CLR(NGE_MEAR_EE_DIN);
303 		}
304 		nge_delay(sc);
305 		SIO_SET(NGE_MEAR_EE_CLK);
306 		nge_delay(sc);
307 		SIO_CLR(NGE_MEAR_EE_CLK);
308 		nge_delay(sc);
309 	}
310 
311 	return;
312 }
313 
314 /*
315  * Read a word of data stored in the EEPROM at address 'addr.'
316  */
317 static void
318 nge_eeprom_getword(sc, addr, dest)
319 	struct nge_softc	*sc;
320 	int			addr;
321 	u_int16_t		*dest;
322 {
323 	register int		i;
324 	u_int16_t		word = 0;
325 
326 	/* Force EEPROM to idle state. */
327 	nge_eeprom_idle(sc);
328 
329 	/* Enter EEPROM access mode. */
330 	nge_delay(sc);
331 	SIO_CLR(NGE_MEAR_EE_CLK);
332 	nge_delay(sc);
333 	SIO_SET(NGE_MEAR_EE_CSEL);
334 	nge_delay(sc);
335 
336 	/*
337 	 * Send address of word we want to read.
338 	 */
339 	nge_eeprom_putbyte(sc, addr);
340 
341 	/*
342 	 * Start reading bits from EEPROM.
343 	 */
344 	for (i = 0x8000; i; i >>= 1) {
345 		SIO_SET(NGE_MEAR_EE_CLK);
346 		nge_delay(sc);
347 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
348 			word |= i;
349 		nge_delay(sc);
350 		SIO_CLR(NGE_MEAR_EE_CLK);
351 		nge_delay(sc);
352 	}
353 
354 	/* Turn off EEPROM access mode. */
355 	nge_eeprom_idle(sc);
356 
357 	*dest = word;
358 
359 	return;
360 }
361 
362 /*
363  * Read a sequence of words from the EEPROM.
364  */
365 static void
366 nge_read_eeprom(sc, dest, off, cnt, swap)
367 	struct nge_softc	*sc;
368 	caddr_t			dest;
369 	int			off;
370 	int			cnt;
371 	int			swap;
372 {
373 	int			i;
374 	u_int16_t		word = 0, *ptr;
375 
376 	for (i = 0; i < cnt; i++) {
377 		nge_eeprom_getword(sc, off + i, &word);
378 		ptr = (u_int16_t *)(dest + (i * 2));
379 		if (swap)
380 			*ptr = ntohs(word);
381 		else
382 			*ptr = word;
383 	}
384 
385 	return;
386 }
387 
388 /*
389  * Sync the PHYs by setting data bit and strobing the clock 32 times.
390  */
391 static void
392 nge_mii_sync(sc)
393 	struct nge_softc		*sc;
394 {
395 	register int		i;
396 
397 	SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA);
398 
399 	for (i = 0; i < 32; i++) {
400 		SIO_SET(NGE_MEAR_MII_CLK);
401 		DELAY(1);
402 		SIO_CLR(NGE_MEAR_MII_CLK);
403 		DELAY(1);
404 	}
405 
406 	return;
407 }
408 
409 /*
410  * Clock a series of bits through the MII.
411  */
412 static void
413 nge_mii_send(sc, bits, cnt)
414 	struct nge_softc		*sc;
415 	u_int32_t		bits;
416 	int			cnt;
417 {
418 	int			i;
419 
420 	SIO_CLR(NGE_MEAR_MII_CLK);
421 
422 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
423                 if (bits & i) {
424 			SIO_SET(NGE_MEAR_MII_DATA);
425                 } else {
426 			SIO_CLR(NGE_MEAR_MII_DATA);
427                 }
428 		DELAY(1);
429 		SIO_CLR(NGE_MEAR_MII_CLK);
430 		DELAY(1);
431 		SIO_SET(NGE_MEAR_MII_CLK);
432 	}
433 }
434 
435 /*
436  * Read an PHY register through the MII.
437  */
438 static int
439 nge_mii_readreg(sc, frame)
440 	struct nge_softc		*sc;
441 	struct nge_mii_frame	*frame;
442 
443 {
444 	int			i, ack, s;
445 
446 	s = splimp();
447 
448 	/*
449 	 * Set up frame for RX.
450 	 */
451 	frame->mii_stdelim = NGE_MII_STARTDELIM;
452 	frame->mii_opcode = NGE_MII_READOP;
453 	frame->mii_turnaround = 0;
454 	frame->mii_data = 0;
455 
456 	CSR_WRITE_4(sc, NGE_MEAR, 0);
457 
458 	/*
459  	 * Turn on data xmit.
460 	 */
461 	SIO_SET(NGE_MEAR_MII_DIR);
462 
463 	nge_mii_sync(sc);
464 
465 	/*
466 	 * Send command/address info.
467 	 */
468 	nge_mii_send(sc, frame->mii_stdelim, 2);
469 	nge_mii_send(sc, frame->mii_opcode, 2);
470 	nge_mii_send(sc, frame->mii_phyaddr, 5);
471 	nge_mii_send(sc, frame->mii_regaddr, 5);
472 
473 	/* Idle bit */
474 	SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA));
475 	DELAY(1);
476 	SIO_SET(NGE_MEAR_MII_CLK);
477 	DELAY(1);
478 
479 	/* Turn off xmit. */
480 	SIO_CLR(NGE_MEAR_MII_DIR);
481 	/* Check for ack */
482 	SIO_CLR(NGE_MEAR_MII_CLK);
483 	DELAY(1);
484 	ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA;
485 	SIO_SET(NGE_MEAR_MII_CLK);
486 	DELAY(1);
487 
488 	/*
489 	 * Now try reading data bits. If the ack failed, we still
490 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
491 	 */
492 	if (ack) {
493 		for(i = 0; i < 16; i++) {
494 			SIO_CLR(NGE_MEAR_MII_CLK);
495 			DELAY(1);
496 			SIO_SET(NGE_MEAR_MII_CLK);
497 			DELAY(1);
498 		}
499 		goto fail;
500 	}
501 
502 	for (i = 0x8000; i; i >>= 1) {
503 		SIO_CLR(NGE_MEAR_MII_CLK);
504 		DELAY(1);
505 		if (!ack) {
506 			if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA)
507 				frame->mii_data |= i;
508 			DELAY(1);
509 		}
510 		SIO_SET(NGE_MEAR_MII_CLK);
511 		DELAY(1);
512 	}
513 
514 fail:
515 
516 	SIO_CLR(NGE_MEAR_MII_CLK);
517 	DELAY(1);
518 	SIO_SET(NGE_MEAR_MII_CLK);
519 	DELAY(1);
520 
521 	splx(s);
522 
523 	if (ack)
524 		return(1);
525 	return(0);
526 }
527 
528 /*
529  * Write to a PHY register through the MII.
530  */
531 static int
532 nge_mii_writereg(sc, frame)
533 	struct nge_softc		*sc;
534 	struct nge_mii_frame	*frame;
535 
536 {
537 	int			s;
538 
539 	s = splimp();
540 	/*
541 	 * Set up frame for TX.
542 	 */
543 
544 	frame->mii_stdelim = NGE_MII_STARTDELIM;
545 	frame->mii_opcode = NGE_MII_WRITEOP;
546 	frame->mii_turnaround = NGE_MII_TURNAROUND;
547 
548 	/*
549  	 * Turn on data output.
550 	 */
551 	SIO_SET(NGE_MEAR_MII_DIR);
552 
553 	nge_mii_sync(sc);
554 
555 	nge_mii_send(sc, frame->mii_stdelim, 2);
556 	nge_mii_send(sc, frame->mii_opcode, 2);
557 	nge_mii_send(sc, frame->mii_phyaddr, 5);
558 	nge_mii_send(sc, frame->mii_regaddr, 5);
559 	nge_mii_send(sc, frame->mii_turnaround, 2);
560 	nge_mii_send(sc, frame->mii_data, 16);
561 
562 	/* Idle bit. */
563 	SIO_SET(NGE_MEAR_MII_CLK);
564 	DELAY(1);
565 	SIO_CLR(NGE_MEAR_MII_CLK);
566 	DELAY(1);
567 
568 	/*
569 	 * Turn off xmit.
570 	 */
571 	SIO_CLR(NGE_MEAR_MII_DIR);
572 
573 	splx(s);
574 
575 	return(0);
576 }
577 
578 static int
579 nge_miibus_readreg(dev, phy, reg)
580 	device_t		dev;
581 	int			phy, reg;
582 {
583 	struct nge_softc	*sc;
584 	struct nge_mii_frame	frame;
585 
586 	sc = device_get_softc(dev);
587 
588 	bzero((char *)&frame, sizeof(frame));
589 
590 	frame.mii_phyaddr = phy;
591 	frame.mii_regaddr = reg;
592 	nge_mii_readreg(sc, &frame);
593 
594 	return(frame.mii_data);
595 }
596 
597 static int
598 nge_miibus_writereg(dev, phy, reg, data)
599 	device_t		dev;
600 	int			phy, reg, data;
601 {
602 	struct nge_softc	*sc;
603 	struct nge_mii_frame	frame;
604 
605 	sc = device_get_softc(dev);
606 
607 	bzero((char *)&frame, sizeof(frame));
608 
609 	frame.mii_phyaddr = phy;
610 	frame.mii_regaddr = reg;
611 	frame.mii_data = data;
612 	nge_mii_writereg(sc, &frame);
613 
614 	return(0);
615 }
616 
617 static void
618 nge_miibus_statchg(dev)
619 	device_t		dev;
620 {
621 	int			status;
622 	struct nge_softc	*sc;
623 	struct mii_data		*mii;
624 
625 	sc = device_get_softc(dev);
626 	if (sc->nge_tbi) {
627 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
628 		    == IFM_AUTO) {
629 			status = CSR_READ_4(sc, NGE_TBI_ANLPAR);
630 			if (status == 0 || status & NGE_TBIANAR_FDX) {
631 				NGE_SETBIT(sc, NGE_TX_CFG,
632 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
633 				NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
634 			} else {
635 				NGE_CLRBIT(sc, NGE_TX_CFG,
636 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
637 				NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
638 			}
639 
640 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
641 			!= IFM_FDX) {
642 			NGE_CLRBIT(sc, NGE_TX_CFG,
643 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
644 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
645 		} else {
646 			NGE_SETBIT(sc, NGE_TX_CFG,
647 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
648 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
649 		}
650 	} else {
651 		mii = device_get_softc(sc->nge_miibus);
652 
653 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
654 		        NGE_SETBIT(sc, NGE_TX_CFG,
655 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
656 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
657 		} else {
658 			NGE_CLRBIT(sc, NGE_TX_CFG,
659 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
660 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
661 		}
662 
663 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
664 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
665 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
666 			NGE_SETBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
667 		} else {
668 			NGE_CLRBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
669 		}
670 	}
671 	return;
672 }
673 
674 static u_int32_t
675 nge_mchash(addr)
676 	const uint8_t *addr;
677 {
678 	uint32_t crc, carry;
679 	int idx, bit;
680 	uint8_t data;
681 
682 	/* Compute CRC for the address value. */
683 	crc = 0xFFFFFFFF; /* initial value */
684 
685 	for (idx = 0; idx < 6; idx++) {
686 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1) {
687 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (data & 0x01);
688 			crc <<= 1;
689 			if (carry)
690 				crc = (crc ^ 0x04c11db6) | carry;
691 		}
692 	}
693 
694 	/*
695 	 * return the filter bit position
696 	 */
697 
698 	return((crc >> 21) & 0x00000FFF);
699 }
700 
701 static void
702 nge_setmulti(sc)
703 	struct nge_softc	*sc;
704 {
705 	struct ifnet		*ifp;
706 	struct ifmultiaddr	*ifma;
707 	u_int32_t		h = 0, i, filtsave;
708 	int			bit, index;
709 
710 	ifp = &sc->arpcom.ac_if;
711 
712 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
713 		NGE_CLRBIT(sc, NGE_RXFILT_CTL,
714 		    NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH);
715 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI);
716 		return;
717 	}
718 
719 	/*
720 	 * We have to explicitly enable the multicast hash table
721 	 * on the NatSemi chip if we want to use it, which we do.
722 	 * We also have to tell it that we don't want to use the
723 	 * hash table for matching unicast addresses.
724 	 */
725 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH);
726 	NGE_CLRBIT(sc, NGE_RXFILT_CTL,
727 	    NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH);
728 
729 	filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL);
730 
731 	/* first, zot all the existing hash bits */
732 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
733 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
734 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
735 	}
736 
737 	/*
738 	 * From the 11 bits returned by the crc routine, the top 7
739 	 * bits represent the 16-bit word in the mcast hash table
740 	 * that needs to be updated, and the lower 4 bits represent
741 	 * which bit within that byte needs to be set.
742 	 */
743 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
744 		if (ifma->ifma_addr->sa_family != AF_LINK)
745 			continue;
746 		h = nge_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
747 		index = (h >> 4) & 0x7F;
748 		bit = h & 0xF;
749 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
750 		    NGE_FILTADDR_MCAST_LO + (index * 2));
751 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
752 	}
753 
754 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave);
755 
756 	return;
757 }
758 
759 static void
760 nge_reset(sc)
761 	struct nge_softc	*sc;
762 {
763 	register int		i;
764 
765 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
766 
767 	for (i = 0; i < NGE_TIMEOUT; i++) {
768 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
769 			break;
770 	}
771 
772 	if (i == NGE_TIMEOUT)
773 		printf("nge%d: reset never completed\n", sc->nge_unit);
774 
775 	/* Wait a little while for the chip to get its brains in order. */
776 	DELAY(1000);
777 
778 	/*
779 	 * If this is a NetSemi chip, make sure to clear
780 	 * PME mode.
781 	 */
782 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
783 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
784 
785         return;
786 }
787 
788 /*
789  * Probe for a NatSemi chip. Check the PCI vendor and device
790  * IDs against our list and return a device name if we find a match.
791  */
792 static int
793 nge_probe(dev)
794 	device_t		dev;
795 {
796 	struct nge_type		*t;
797 
798 	t = nge_devs;
799 
800 	while(t->nge_name != NULL) {
801 		if ((pci_get_vendor(dev) == t->nge_vid) &&
802 		    (pci_get_device(dev) == t->nge_did)) {
803 			device_set_desc(dev, t->nge_name);
804 			return(0);
805 		}
806 		t++;
807 	}
808 
809 	return(ENXIO);
810 }
811 
812 /*
813  * Attach the interface. Allocate softc structures, do ifmedia
814  * setup and ethernet/BPF attach.
815  */
816 static int
817 nge_attach(dev)
818 	device_t		dev;
819 {
820 	int			s;
821 	u_char			eaddr[ETHER_ADDR_LEN];
822 	struct nge_softc	*sc;
823 	struct ifnet		*ifp;
824 	int			unit, error = 0, rid;
825 	const char		*sep = "";
826 
827 	s = splimp();
828 
829 	sc = device_get_softc(dev);
830 	unit = device_get_unit(dev);
831 	bzero(sc, sizeof(struct nge_softc));
832 
833 	mtx_init(&sc->nge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
834 	    MTX_DEF | MTX_RECURSE);
835 #ifndef BURN_BRIDGES
836 	/*
837 	 * Handle power management nonsense.
838 	 */
839 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
840 		u_int32_t		iobase, membase, irq;
841 
842 		/* Save important PCI config data. */
843 		iobase = pci_read_config(dev, NGE_PCI_LOIO, 4);
844 		membase = pci_read_config(dev, NGE_PCI_LOMEM, 4);
845 		irq = pci_read_config(dev, NGE_PCI_INTLINE, 4);
846 
847 		/* Reset the power state. */
848 		printf("nge%d: chip is in D%d power mode "
849 		    "-- setting to D0\n", unit,
850 		    pci_get_powerstate(dev));
851 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
852 
853 		/* Restore PCI config data. */
854 		pci_write_config(dev, NGE_PCI_LOIO, iobase, 4);
855 		pci_write_config(dev, NGE_PCI_LOMEM, membase, 4);
856 		pci_write_config(dev, NGE_PCI_INTLINE, irq, 4);
857 	}
858 #endif
859 	/*
860 	 * Map control/status registers.
861 	 */
862 	pci_enable_busmaster(dev);
863 
864 	rid = NGE_RID;
865 	sc->nge_res = bus_alloc_resource_any(dev, NGE_RES, &rid, RF_ACTIVE);
866 
867 	if (sc->nge_res == NULL) {
868 		printf("nge%d: couldn't map ports/memory\n", unit);
869 		error = ENXIO;
870 		goto fail;
871 	}
872 
873 	sc->nge_btag = rman_get_bustag(sc->nge_res);
874 	sc->nge_bhandle = rman_get_bushandle(sc->nge_res);
875 
876 	/* Allocate interrupt */
877 	rid = 0;
878 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
879 	    RF_SHAREABLE | RF_ACTIVE);
880 
881 	if (sc->nge_irq == NULL) {
882 		printf("nge%d: couldn't map interrupt\n", unit);
883 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
884 		error = ENXIO;
885 		goto fail;
886 	}
887 
888 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET,
889 	    nge_intr, sc, &sc->nge_intrhand);
890 
891 	if (error) {
892 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
893 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
894 		printf("nge%d: couldn't set up irq\n", unit);
895 		goto fail;
896 	}
897 
898 	/* Reset the adapter. */
899 	nge_reset(sc);
900 
901 	/*
902 	 * Get station address from the EEPROM.
903 	 */
904 	nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0);
905 	nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0);
906 	nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0);
907 
908 	sc->nge_unit = unit;
909 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
910 
911 	sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF,
912 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
913 
914 	if (sc->nge_ldata == NULL) {
915 		printf("nge%d: no memory for list buffers!\n", unit);
916 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
917 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
918 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
919 		error = ENXIO;
920 		goto fail;
921 	}
922 	bzero(sc->nge_ldata, sizeof(struct nge_list_data));
923 
924 	/* Try to allocate memory for jumbo buffers. */
925 	if (nge_alloc_jumbo_mem(sc)) {
926 		printf("nge%d: jumbo buffer allocation failed\n",
927                     sc->nge_unit);
928 		contigfree(sc->nge_ldata,
929 		    sizeof(struct nge_list_data), M_DEVBUF);
930 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
931 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
932 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
933 		error = ENXIO;
934 		goto fail;
935 	}
936 
937 	ifp = &sc->arpcom.ac_if;
938 	ifp->if_softc = sc;
939 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
940 	ifp->if_mtu = ETHERMTU;
941 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
942 	ifp->if_ioctl = nge_ioctl;
943 	ifp->if_output = ether_output;
944 	ifp->if_start = nge_start;
945 	ifp->if_watchdog = nge_watchdog;
946 	ifp->if_init = nge_init;
947 	ifp->if_baudrate = 1000000000;
948 	ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1;
949 	ifp->if_hwassist = NGE_CSUM_FEATURES;
950 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING;
951 #ifdef DEVICE_POLLING
952 	ifp->if_capabilities |= IFCAP_POLLING;
953 #endif
954 	ifp->if_capenable = ifp->if_capabilities;
955 
956 	/*
957 	 * Do MII setup.
958 	 */
959 	if (mii_phy_probe(dev, &sc->nge_miibus,
960 			  nge_ifmedia_upd, nge_ifmedia_sts)) {
961 		if (CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) {
962 			sc->nge_tbi = 1;
963 			device_printf(dev, "Using TBI\n");
964 
965 			sc->nge_miibus = dev;
966 
967 			ifmedia_init(&sc->nge_ifmedia, 0, nge_ifmedia_upd,
968 				nge_ifmedia_sts);
969 #define	ADD(m, c)	ifmedia_add(&sc->nge_ifmedia, (m), (c), NULL)
970 #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
971 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, 0), 0);
972 			device_printf(dev, " ");
973 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 0), 0);
974 			PRINT("1000baseSX");
975 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 0),0);
976 			PRINT("1000baseSX-FDX");
977 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0);
978 			PRINT("auto");
979 
980 			printf("\n");
981 #undef ADD
982 #undef PRINT
983 			ifmedia_set(&sc->nge_ifmedia,
984 				IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0));
985 
986 			CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
987 				| NGE_GPIO_GP4_OUT
988 				| NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
989 				| NGE_GPIO_GP3_OUTENB
990 				| NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
991 
992 		} else {
993 			printf("nge%d: MII without any PHY!\n", sc->nge_unit);
994 			nge_free_jumbo_mem(sc);
995 			bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
996 			bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
997 			bus_release_resource(dev, NGE_RES, NGE_RID,
998 					 sc->nge_res);
999 			error = ENXIO;
1000 			goto fail;
1001 		}
1002 	}
1003 
1004 	/*
1005 	 * Call MI attach routine.
1006 	 */
1007 	ether_ifattach(ifp, eaddr);
1008 	callout_handle_init(&sc->nge_stat_ch);
1009 
1010 fail:
1011 
1012 	splx(s);
1013 	mtx_destroy(&sc->nge_mtx);
1014 	return(error);
1015 }
1016 
1017 static int
1018 nge_detach(dev)
1019 	device_t		dev;
1020 {
1021 	struct nge_softc	*sc;
1022 	struct ifnet		*ifp;
1023 	int			s;
1024 
1025 	s = splimp();
1026 
1027 	sc = device_get_softc(dev);
1028 	ifp = &sc->arpcom.ac_if;
1029 
1030 	nge_reset(sc);
1031 	nge_stop(sc);
1032 	ether_ifdetach(ifp);
1033 
1034 	bus_generic_detach(dev);
1035 	if (!sc->nge_tbi) {
1036 		device_delete_child(dev, sc->nge_miibus);
1037 	}
1038 	bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1039 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
1040 	bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
1041 
1042 	contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF);
1043 	nge_free_jumbo_mem(sc);
1044 
1045 	splx(s);
1046 	mtx_destroy(&sc->nge_mtx);
1047 
1048 	return(0);
1049 }
1050 
1051 /*
1052  * Initialize the transmit descriptors.
1053  */
1054 static int
1055 nge_list_tx_init(sc)
1056 	struct nge_softc	*sc;
1057 {
1058 	struct nge_list_data	*ld;
1059 	struct nge_ring_data	*cd;
1060 	int			i;
1061 
1062 	cd = &sc->nge_cdata;
1063 	ld = sc->nge_ldata;
1064 
1065 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
1066 		if (i == (NGE_TX_LIST_CNT - 1)) {
1067 			ld->nge_tx_list[i].nge_nextdesc =
1068 			    &ld->nge_tx_list[0];
1069 			ld->nge_tx_list[i].nge_next =
1070 			    vtophys(&ld->nge_tx_list[0]);
1071 		} else {
1072 			ld->nge_tx_list[i].nge_nextdesc =
1073 			    &ld->nge_tx_list[i + 1];
1074 			ld->nge_tx_list[i].nge_next =
1075 			    vtophys(&ld->nge_tx_list[i + 1]);
1076 		}
1077 		ld->nge_tx_list[i].nge_mbuf = NULL;
1078 		ld->nge_tx_list[i].nge_ptr = 0;
1079 		ld->nge_tx_list[i].nge_ctl = 0;
1080 	}
1081 
1082 	cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0;
1083 
1084 	return(0);
1085 }
1086 
1087 
1088 /*
1089  * Initialize the RX descriptors and allocate mbufs for them. Note that
1090  * we arrange the descriptors in a closed ring, so that the last descriptor
1091  * points back to the first.
1092  */
1093 static int
1094 nge_list_rx_init(sc)
1095 	struct nge_softc	*sc;
1096 {
1097 	struct nge_list_data	*ld;
1098 	struct nge_ring_data	*cd;
1099 	int			i;
1100 
1101 	ld = sc->nge_ldata;
1102 	cd = &sc->nge_cdata;
1103 
1104 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
1105 		if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS)
1106 			return(ENOBUFS);
1107 		if (i == (NGE_RX_LIST_CNT - 1)) {
1108 			ld->nge_rx_list[i].nge_nextdesc =
1109 			    &ld->nge_rx_list[0];
1110 			ld->nge_rx_list[i].nge_next =
1111 			    vtophys(&ld->nge_rx_list[0]);
1112 		} else {
1113 			ld->nge_rx_list[i].nge_nextdesc =
1114 			    &ld->nge_rx_list[i + 1];
1115 			ld->nge_rx_list[i].nge_next =
1116 			    vtophys(&ld->nge_rx_list[i + 1]);
1117 		}
1118 	}
1119 
1120 	cd->nge_rx_prod = 0;
1121 
1122 	return(0);
1123 }
1124 
1125 /*
1126  * Initialize an RX descriptor and attach an MBUF cluster.
1127  */
1128 static int
1129 nge_newbuf(sc, c, m)
1130 	struct nge_softc	*sc;
1131 	struct nge_desc		*c;
1132 	struct mbuf		*m;
1133 {
1134 	struct mbuf		*m_new = NULL;
1135 	caddr_t			*buf = NULL;
1136 
1137 	if (m == NULL) {
1138 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1139 		if (m_new == NULL) {
1140 			printf("nge%d: no memory for rx list "
1141 			    "-- packet dropped!\n", sc->nge_unit);
1142 			return(ENOBUFS);
1143 		}
1144 
1145 		/* Allocate the jumbo buffer */
1146 		buf = nge_jalloc(sc);
1147 		if (buf == NULL) {
1148 #ifdef NGE_VERBOSE
1149 			printf("nge%d: jumbo allocation failed "
1150 			    "-- packet dropped!\n", sc->nge_unit);
1151 #endif
1152 			m_freem(m_new);
1153 			return(ENOBUFS);
1154 		}
1155 		/* Attach the buffer to the mbuf */
1156 		m_new->m_data = (void *)buf;
1157 		m_new->m_len = m_new->m_pkthdr.len = NGE_JUMBO_FRAMELEN;
1158 		MEXTADD(m_new, buf, NGE_JUMBO_FRAMELEN, nge_jfree,
1159 		    (struct nge_softc *)sc, 0, EXT_NET_DRV);
1160 	} else {
1161 		m_new = m;
1162 		m_new->m_len = m_new->m_pkthdr.len = NGE_JUMBO_FRAMELEN;
1163 		m_new->m_data = m_new->m_ext.ext_buf;
1164 	}
1165 
1166 	m_adj(m_new, sizeof(u_int64_t));
1167 
1168 	c->nge_mbuf = m_new;
1169 	c->nge_ptr = vtophys(mtod(m_new, caddr_t));
1170 	c->nge_ctl = m_new->m_len;
1171 	c->nge_extsts = 0;
1172 
1173 	return(0);
1174 }
1175 
1176 static int
1177 nge_alloc_jumbo_mem(sc)
1178 	struct nge_softc	*sc;
1179 {
1180 	caddr_t			ptr;
1181 	register int		i;
1182 	struct nge_jpool_entry   *entry;
1183 
1184 	/* Grab a big chunk o' storage. */
1185 	sc->nge_cdata.nge_jumbo_buf = contigmalloc(NGE_JMEM, M_DEVBUF,
1186 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1187 
1188 	if (sc->nge_cdata.nge_jumbo_buf == NULL) {
1189 		printf("nge%d: no memory for jumbo buffers!\n", sc->nge_unit);
1190 		return(ENOBUFS);
1191 	}
1192 
1193 	SLIST_INIT(&sc->nge_jfree_listhead);
1194 	SLIST_INIT(&sc->nge_jinuse_listhead);
1195 
1196 	/*
1197 	 * Now divide it up into 9K pieces and save the addresses
1198 	 * in an array.
1199 	 */
1200 	ptr = sc->nge_cdata.nge_jumbo_buf;
1201 	for (i = 0; i < NGE_JSLOTS; i++) {
1202 		sc->nge_cdata.nge_jslots[i] = ptr;
1203 		ptr += NGE_JLEN;
1204 		entry = malloc(sizeof(struct nge_jpool_entry),
1205 		    M_DEVBUF, M_NOWAIT);
1206 		if (entry == NULL) {
1207 			printf("nge%d: no memory for jumbo "
1208 			    "buffer queue!\n", sc->nge_unit);
1209 			return(ENOBUFS);
1210 		}
1211 		entry->slot = i;
1212 		SLIST_INSERT_HEAD(&sc->nge_jfree_listhead,
1213 		    entry, jpool_entries);
1214 	}
1215 
1216 	return(0);
1217 }
1218 
1219 static void
1220 nge_free_jumbo_mem(sc)
1221 	struct nge_softc	*sc;
1222 {
1223 	register int		i;
1224 	struct nge_jpool_entry   *entry;
1225 
1226 	for (i = 0; i < NGE_JSLOTS; i++) {
1227 		entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1228 		SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
1229 		free(entry, M_DEVBUF);
1230 	}
1231 
1232 	contigfree(sc->nge_cdata.nge_jumbo_buf, NGE_JMEM, M_DEVBUF);
1233 
1234 	return;
1235 }
1236 
1237 /*
1238  * Allocate a jumbo buffer.
1239  */
1240 static void *
1241 nge_jalloc(sc)
1242 	struct nge_softc	*sc;
1243 {
1244 	struct nge_jpool_entry   *entry;
1245 
1246 	entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1247 
1248 	if (entry == NULL) {
1249 #ifdef NGE_VERBOSE
1250 		printf("nge%d: no free jumbo buffers\n", sc->nge_unit);
1251 #endif
1252 		return(NULL);
1253 	}
1254 
1255 	SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
1256 	SLIST_INSERT_HEAD(&sc->nge_jinuse_listhead, entry, jpool_entries);
1257 	return(sc->nge_cdata.nge_jslots[entry->slot]);
1258 }
1259 
1260 /*
1261  * Release a jumbo buffer.
1262  */
1263 static void
1264 nge_jfree(buf, args)
1265 	void			*buf;
1266 	void			*args;
1267 {
1268 	struct nge_softc	*sc;
1269 	int		        i;
1270 	struct nge_jpool_entry   *entry;
1271 
1272 	/* Extract the softc struct pointer. */
1273 	sc = args;
1274 
1275 	if (sc == NULL)
1276 		panic("nge_jfree: can't find softc pointer!");
1277 
1278 	/* calculate the slot this buffer belongs to */
1279 	i = ((vm_offset_t)buf
1280 	     - (vm_offset_t)sc->nge_cdata.nge_jumbo_buf) / NGE_JLEN;
1281 
1282 	if ((i < 0) || (i >= NGE_JSLOTS))
1283 		panic("nge_jfree: asked to free buffer that we don't manage!");
1284 
1285 	entry = SLIST_FIRST(&sc->nge_jinuse_listhead);
1286 	if (entry == NULL)
1287 		panic("nge_jfree: buffer not in use!");
1288 	entry->slot = i;
1289 	SLIST_REMOVE_HEAD(&sc->nge_jinuse_listhead, jpool_entries);
1290 	SLIST_INSERT_HEAD(&sc->nge_jfree_listhead, entry, jpool_entries);
1291 
1292 	return;
1293 }
1294 /*
1295  * A frame has been uploaded: pass the resulting mbuf chain up to
1296  * the higher level protocols.
1297  */
1298 static void
1299 nge_rxeof(sc)
1300 	struct nge_softc	*sc;
1301 {
1302         struct mbuf		*m;
1303         struct ifnet		*ifp;
1304 	struct nge_desc		*cur_rx;
1305 	int			i, total_len = 0;
1306 	u_int32_t		rxstat;
1307 
1308 	ifp = &sc->arpcom.ac_if;
1309 	i = sc->nge_cdata.nge_rx_prod;
1310 
1311 	while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) {
1312 		struct mbuf		*m0 = NULL;
1313 		u_int32_t		extsts;
1314 
1315 #ifdef DEVICE_POLLING
1316 		if (ifp->if_flags & IFF_POLLING) {
1317 			if (sc->rxcycles <= 0)
1318 				break;
1319 			sc->rxcycles--;
1320 		}
1321 #endif /* DEVICE_POLLING */
1322 
1323 		cur_rx = &sc->nge_ldata->nge_rx_list[i];
1324 		rxstat = cur_rx->nge_rxstat;
1325 		extsts = cur_rx->nge_extsts;
1326 		m = cur_rx->nge_mbuf;
1327 		cur_rx->nge_mbuf = NULL;
1328 		total_len = NGE_RXBYTES(cur_rx);
1329 		NGE_INC(i, NGE_RX_LIST_CNT);
1330 		/*
1331 		 * If an error occurs, update stats, clear the
1332 		 * status word and leave the mbuf cluster in place:
1333 		 * it should simply get re-used next time this descriptor
1334 	 	 * comes up in the ring.
1335 		 */
1336 		if (!(rxstat & NGE_CMDSTS_PKT_OK)) {
1337 			ifp->if_ierrors++;
1338 			nge_newbuf(sc, cur_rx, m);
1339 			continue;
1340 		}
1341 
1342 		/*
1343 		 * Ok. NatSemi really screwed up here. This is the
1344 		 * only gigE chip I know of with alignment constraints
1345 		 * on receive buffers. RX buffers must be 64-bit aligned.
1346 		 */
1347 #ifdef __i386__
1348 		/*
1349 		 * By popular demand, ignore the alignment problems
1350 		 * on the Intel x86 platform. The performance hit
1351 		 * incurred due to unaligned accesses is much smaller
1352 		 * than the hit produced by forcing buffer copies all
1353 		 * the time, especially with jumbo frames. We still
1354 		 * need to fix up the alignment everywhere else though.
1355 		 */
1356 		if (nge_newbuf(sc, cur_rx, NULL) == ENOBUFS) {
1357 #endif
1358 			m0 = m_devget(mtod(m, char *), total_len,
1359 			    ETHER_ALIGN, ifp, NULL);
1360 			nge_newbuf(sc, cur_rx, m);
1361 			if (m0 == NULL) {
1362 				printf("nge%d: no receive buffers "
1363 				    "available -- packet dropped!\n",
1364 				    sc->nge_unit);
1365 				ifp->if_ierrors++;
1366 				continue;
1367 			}
1368 			m = m0;
1369 #ifdef __i386__
1370 		} else {
1371 			m->m_pkthdr.rcvif = ifp;
1372 			m->m_pkthdr.len = m->m_len = total_len;
1373 		}
1374 #endif
1375 
1376 		ifp->if_ipackets++;
1377 
1378 		/* Do IP checksum checking. */
1379 		if (extsts & NGE_RXEXTSTS_IPPKT)
1380 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1381 		if (!(extsts & NGE_RXEXTSTS_IPCSUMERR))
1382 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
1383 		if ((extsts & NGE_RXEXTSTS_TCPPKT &&
1384 		    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
1385 		    (extsts & NGE_RXEXTSTS_UDPPKT &&
1386 		    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
1387 			m->m_pkthdr.csum_flags |=
1388 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1389 			m->m_pkthdr.csum_data = 0xffff;
1390 		}
1391 
1392 		/*
1393 		 * If we received a packet with a vlan tag, pass it
1394 		 * to vlan_input() instead of ether_input().
1395 		 */
1396 		if (extsts & NGE_RXEXTSTS_VLANPKT) {
1397 			VLAN_INPUT_TAG(ifp, m,
1398 			    ntohs(extsts & NGE_RXEXTSTS_VTCI), continue);
1399 		}
1400 
1401 		(*ifp->if_input)(ifp, m);
1402 	}
1403 
1404 	sc->nge_cdata.nge_rx_prod = i;
1405 
1406 	return;
1407 }
1408 
1409 /*
1410  * A frame was downloaded to the chip. It's safe for us to clean up
1411  * the list buffers.
1412  */
1413 
1414 static void
1415 nge_txeof(sc)
1416 	struct nge_softc	*sc;
1417 {
1418 	struct nge_desc		*cur_tx;
1419 	struct ifnet		*ifp;
1420 	u_int32_t		idx;
1421 
1422 	ifp = &sc->arpcom.ac_if;
1423 
1424 	/*
1425 	 * Go through our tx list and free mbufs for those
1426 	 * frames that have been transmitted.
1427 	 */
1428 	idx = sc->nge_cdata.nge_tx_cons;
1429 	while (idx != sc->nge_cdata.nge_tx_prod) {
1430 		cur_tx = &sc->nge_ldata->nge_tx_list[idx];
1431 
1432 		if (NGE_OWNDESC(cur_tx))
1433 			break;
1434 
1435 		if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) {
1436 			sc->nge_cdata.nge_tx_cnt--;
1437 			NGE_INC(idx, NGE_TX_LIST_CNT);
1438 			continue;
1439 		}
1440 
1441 		if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) {
1442 			ifp->if_oerrors++;
1443 			if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS)
1444 				ifp->if_collisions++;
1445 			if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL)
1446 				ifp->if_collisions++;
1447 		}
1448 
1449 		ifp->if_collisions +=
1450 		    (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16;
1451 
1452 		ifp->if_opackets++;
1453 		if (cur_tx->nge_mbuf != NULL) {
1454 			m_freem(cur_tx->nge_mbuf);
1455 			cur_tx->nge_mbuf = NULL;
1456 			ifp->if_flags &= ~IFF_OACTIVE;
1457 		}
1458 
1459 		sc->nge_cdata.nge_tx_cnt--;
1460 		NGE_INC(idx, NGE_TX_LIST_CNT);
1461 	}
1462 
1463 	sc->nge_cdata.nge_tx_cons = idx;
1464 
1465 	if (idx == sc->nge_cdata.nge_tx_prod)
1466 		ifp->if_timer = 0;
1467 
1468 	return;
1469 }
1470 
1471 static void
1472 nge_tick(xsc)
1473 	void			*xsc;
1474 {
1475 	struct nge_softc	*sc;
1476 	struct mii_data		*mii;
1477 	struct ifnet		*ifp;
1478 	int			s;
1479 
1480 	s = splimp();
1481 
1482 	sc = xsc;
1483 	ifp = &sc->arpcom.ac_if;
1484 
1485 	if (sc->nge_tbi) {
1486 		if (!sc->nge_link) {
1487 			if (CSR_READ_4(sc, NGE_TBI_BMSR)
1488 			    & NGE_TBIBMSR_ANEG_DONE) {
1489 				printf("nge%d: gigabit link up\n",
1490 				    sc->nge_unit);
1491 				nge_miibus_statchg(sc->nge_miibus);
1492 				sc->nge_link++;
1493 				if (ifp->if_snd.ifq_head != NULL)
1494 					nge_start(ifp);
1495 			}
1496 		}
1497 	} else {
1498 		mii = device_get_softc(sc->nge_miibus);
1499 		mii_tick(mii);
1500 
1501 		if (!sc->nge_link) {
1502 			if (mii->mii_media_status & IFM_ACTIVE &&
1503 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1504 				sc->nge_link++;
1505 				if (IFM_SUBTYPE(mii->mii_media_active)
1506 				    == IFM_1000_T)
1507 					printf("nge%d: gigabit link up\n",
1508 					    sc->nge_unit);
1509 				if (ifp->if_snd.ifq_head != NULL)
1510 					nge_start(ifp);
1511 			}
1512 		}
1513 	}
1514 	sc->nge_stat_ch = timeout(nge_tick, sc, hz);
1515 
1516 	splx(s);
1517 
1518 	return;
1519 }
1520 
1521 #ifdef DEVICE_POLLING
1522 static poll_handler_t nge_poll;
1523 
1524 static void
1525 nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1526 {
1527 	struct  nge_softc *sc = ifp->if_softc;
1528 
1529 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
1530 		ether_poll_deregister(ifp);
1531 		cmd = POLL_DEREGISTER;
1532 	}
1533 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1534 		CSR_WRITE_4(sc, NGE_IER, 1);
1535 		return;
1536 	}
1537 
1538 	/*
1539 	 * On the nge, reading the status register also clears it.
1540 	 * So before returning to intr mode we must make sure that all
1541 	 * possible pending sources of interrupts have been served.
1542 	 * In practice this means run to completion the *eof routines,
1543 	 * and then call the interrupt routine
1544 	 */
1545 	sc->rxcycles = count;
1546 	nge_rxeof(sc);
1547 	nge_txeof(sc);
1548 	if (ifp->if_snd.ifq_head != NULL)
1549 		nge_start(ifp);
1550 
1551 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1552 		u_int32_t	status;
1553 
1554 		/* Reading the ISR register clears all interrupts. */
1555 		status = CSR_READ_4(sc, NGE_ISR);
1556 
1557 		if (status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW))
1558 			nge_rxeof(sc);
1559 
1560 		if (status & (NGE_ISR_RX_IDLE))
1561 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1562 
1563 		if (status & NGE_ISR_SYSERR) {
1564 			nge_reset(sc);
1565 			nge_init(sc);
1566 		}
1567 	}
1568 }
1569 #endif /* DEVICE_POLLING */
1570 
1571 static void
1572 nge_intr(arg)
1573 	void			*arg;
1574 {
1575 	struct nge_softc	*sc;
1576 	struct ifnet		*ifp;
1577 	u_int32_t		status;
1578 
1579 	sc = arg;
1580 	ifp = &sc->arpcom.ac_if;
1581 
1582 #ifdef DEVICE_POLLING
1583 	if (ifp->if_flags & IFF_POLLING)
1584 		return;
1585 	if ((ifp->if_capenable & IFCAP_POLLING) &&
1586 	    ether_poll_register(nge_poll, ifp)) { /* ok, disable interrupts */
1587 		CSR_WRITE_4(sc, NGE_IER, 0);
1588 		nge_poll(ifp, 0, 1);
1589 		return;
1590 	}
1591 #endif /* DEVICE_POLLING */
1592 
1593 	/* Supress unwanted interrupts */
1594 	if (!(ifp->if_flags & IFF_UP)) {
1595 		nge_stop(sc);
1596 		return;
1597 	}
1598 
1599 	/* Disable interrupts. */
1600 	CSR_WRITE_4(sc, NGE_IER, 0);
1601 
1602 	/* Data LED on for TBI mode */
1603 	if(sc->nge_tbi)
1604 		 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
1605 			     | NGE_GPIO_GP3_OUT);
1606 
1607 	for (;;) {
1608 		/* Reading the ISR register clears all interrupts. */
1609 		status = CSR_READ_4(sc, NGE_ISR);
1610 
1611 		if ((status & NGE_INTRS) == 0)
1612 			break;
1613 
1614 		if ((status & NGE_ISR_TX_DESC_OK) ||
1615 		    (status & NGE_ISR_TX_ERR) ||
1616 		    (status & NGE_ISR_TX_OK) ||
1617 		    (status & NGE_ISR_TX_IDLE))
1618 			nge_txeof(sc);
1619 
1620 		if ((status & NGE_ISR_RX_DESC_OK) ||
1621 		    (status & NGE_ISR_RX_ERR) ||
1622 		    (status & NGE_ISR_RX_OFLOW) ||
1623 		    (status & NGE_ISR_RX_FIFO_OFLOW) ||
1624 		    (status & NGE_ISR_RX_IDLE) ||
1625 		    (status & NGE_ISR_RX_OK))
1626 			nge_rxeof(sc);
1627 
1628 		if ((status & NGE_ISR_RX_IDLE))
1629 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1630 
1631 		if (status & NGE_ISR_SYSERR) {
1632 			nge_reset(sc);
1633 			ifp->if_flags &= ~IFF_RUNNING;
1634 			nge_init(sc);
1635 		}
1636 
1637 #if 0
1638 		/*
1639 		 * XXX: nge_tick() is not ready to be called this way
1640 		 * it screws up the aneg timeout because mii_tick() is
1641 		 * only to be called once per second.
1642 		 */
1643 		if (status & NGE_IMR_PHY_INTR) {
1644 			sc->nge_link = 0;
1645 			nge_tick(sc);
1646 		}
1647 #endif
1648 	}
1649 
1650 	/* Re-enable interrupts. */
1651 	CSR_WRITE_4(sc, NGE_IER, 1);
1652 
1653 	if (ifp->if_snd.ifq_head != NULL)
1654 		nge_start(ifp);
1655 
1656 	/* Data LED off for TBI mode */
1657 
1658 	if(sc->nge_tbi)
1659 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
1660 			    & ~NGE_GPIO_GP3_OUT);
1661 
1662 	return;
1663 }
1664 
1665 /*
1666  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1667  * pointers to the fragment pointers.
1668  */
1669 static int
1670 nge_encap(sc, m_head, txidx)
1671 	struct nge_softc	*sc;
1672 	struct mbuf		*m_head;
1673 	u_int32_t		*txidx;
1674 {
1675 	struct nge_desc		*f = NULL;
1676 	struct mbuf		*m;
1677 	int			frag, cur, cnt = 0;
1678 	struct m_tag		*mtag;
1679 
1680 	/*
1681  	 * Start packing the mbufs in this chain into
1682 	 * the fragment pointers. Stop when we run out
1683  	 * of fragments or hit the end of the mbuf chain.
1684 	 */
1685 	m = m_head;
1686 	cur = frag = *txidx;
1687 
1688 	for (m = m_head; m != NULL; m = m->m_next) {
1689 		if (m->m_len != 0) {
1690 			if ((NGE_TX_LIST_CNT -
1691 			    (sc->nge_cdata.nge_tx_cnt + cnt)) < 2)
1692 				return(ENOBUFS);
1693 			f = &sc->nge_ldata->nge_tx_list[frag];
1694 			f->nge_ctl = NGE_CMDSTS_MORE | m->m_len;
1695 			f->nge_ptr = vtophys(mtod(m, vm_offset_t));
1696 			if (cnt != 0)
1697 				f->nge_ctl |= NGE_CMDSTS_OWN;
1698 			cur = frag;
1699 			NGE_INC(frag, NGE_TX_LIST_CNT);
1700 			cnt++;
1701 		}
1702 	}
1703 
1704 	if (m != NULL)
1705 		return(ENOBUFS);
1706 
1707 	sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0;
1708 	if (m_head->m_pkthdr.csum_flags) {
1709 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
1710 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1711 			    NGE_TXEXTSTS_IPCSUM;
1712 		if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
1713 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1714 			    NGE_TXEXTSTS_TCPCSUM;
1715 		if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
1716 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1717 			    NGE_TXEXTSTS_UDPCSUM;
1718 	}
1719 
1720 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m);
1721 	if (mtag != NULL) {
1722 		sc->nge_ldata->nge_tx_list[cur].nge_extsts |=
1723 		    (NGE_TXEXTSTS_VLANPKT|htons(VLAN_TAG_VALUE(mtag)));
1724 	}
1725 
1726 	sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head;
1727 	sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE;
1728 	sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN;
1729 	sc->nge_cdata.nge_tx_cnt += cnt;
1730 	*txidx = frag;
1731 
1732 	return(0);
1733 }
1734 
1735 /*
1736  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1737  * to the mbuf data regions directly in the transmit lists. We also save a
1738  * copy of the pointers since the transmit list fragment pointers are
1739  * physical addresses.
1740  */
1741 
1742 static void
1743 nge_start(ifp)
1744 	struct ifnet		*ifp;
1745 {
1746 	struct nge_softc	*sc;
1747 	struct mbuf		*m_head = NULL;
1748 	u_int32_t		idx;
1749 
1750 	sc = ifp->if_softc;
1751 
1752 	if (!sc->nge_link)
1753 		return;
1754 
1755 	idx = sc->nge_cdata.nge_tx_prod;
1756 
1757 	if (ifp->if_flags & IFF_OACTIVE)
1758 		return;
1759 
1760 	while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) {
1761 		IF_DEQUEUE(&ifp->if_snd, m_head);
1762 		if (m_head == NULL)
1763 			break;
1764 
1765 		if (nge_encap(sc, m_head, &idx)) {
1766 			IF_PREPEND(&ifp->if_snd, m_head);
1767 			ifp->if_flags |= IFF_OACTIVE;
1768 			break;
1769 		}
1770 
1771 		/*
1772 		 * If there's a BPF listener, bounce a copy of this frame
1773 		 * to him.
1774 		 */
1775 		BPF_MTAP(ifp, m_head);
1776 
1777 	}
1778 
1779 	/* Transmit */
1780 	sc->nge_cdata.nge_tx_prod = idx;
1781 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
1782 
1783 	/*
1784 	 * Set a timeout in case the chip goes out to lunch.
1785 	 */
1786 	ifp->if_timer = 5;
1787 
1788 	return;
1789 }
1790 
1791 static void
1792 nge_init(xsc)
1793 	void			*xsc;
1794 {
1795 	struct nge_softc	*sc = xsc;
1796 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1797 	struct mii_data		*mii;
1798 	int			s;
1799 
1800 	if (ifp->if_flags & IFF_RUNNING)
1801 		return;
1802 
1803 	s = splimp();
1804 
1805 	/*
1806 	 * Cancel pending I/O and free all RX/TX buffers.
1807 	 */
1808 	nge_stop(sc);
1809 
1810 	if (sc->nge_tbi) {
1811 		mii = NULL;
1812 	} else {
1813 		mii = device_get_softc(sc->nge_miibus);
1814 	}
1815 
1816 	/* Set MAC address */
1817 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
1818 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1819 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1820 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
1821 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1822 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1823 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
1824 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1825 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1826 
1827 	/* Init circular RX list. */
1828 	if (nge_list_rx_init(sc) == ENOBUFS) {
1829 		printf("nge%d: initialization failed: no "
1830 			"memory for rx buffers\n", sc->nge_unit);
1831 		nge_stop(sc);
1832 		(void)splx(s);
1833 		return;
1834 	}
1835 
1836 	/*
1837 	 * Init tx descriptors.
1838 	 */
1839 	nge_list_tx_init(sc);
1840 
1841 	/*
1842 	 * For the NatSemi chip, we have to explicitly enable the
1843 	 * reception of ARP frames, as well as turn on the 'perfect
1844 	 * match' filter where we store the station address, otherwise
1845 	 * we won't receive unicasts meant for this host.
1846 	 */
1847 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
1848 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
1849 
1850 	 /* If we want promiscuous mode, set the allframes bit. */
1851 	if (ifp->if_flags & IFF_PROMISC) {
1852 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1853 	} else {
1854 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1855 	}
1856 
1857 	/*
1858 	 * Set the capture broadcast bit to capture broadcast frames.
1859 	 */
1860 	if (ifp->if_flags & IFF_BROADCAST) {
1861 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1862 	} else {
1863 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1864 	}
1865 
1866 	/*
1867 	 * Load the multicast filter.
1868 	 */
1869 	nge_setmulti(sc);
1870 
1871 	/* Turn the receive filter on */
1872 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
1873 
1874 	/*
1875 	 * Load the address of the RX and TX lists.
1876 	 */
1877 	CSR_WRITE_4(sc, NGE_RX_LISTPTR,
1878 	    vtophys(&sc->nge_ldata->nge_rx_list[0]));
1879 	CSR_WRITE_4(sc, NGE_TX_LISTPTR,
1880 	    vtophys(&sc->nge_ldata->nge_tx_list[0]));
1881 
1882 	/* Set RX configuration */
1883 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
1884 	/*
1885 	 * Enable hardware checksum validation for all IPv4
1886 	 * packets, do not reject packets with bad checksums.
1887 	 */
1888 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
1889 
1890 	/*
1891 	 * Tell the chip to detect and strip VLAN tag info from
1892 	 * received frames. The tag will be provided in the extsts
1893 	 * field in the RX descriptors.
1894 	 */
1895 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL,
1896 	    NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB);
1897 
1898 	/* Set TX configuration */
1899 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
1900 
1901 	/*
1902 	 * Enable TX IPv4 checksumming on a per-packet basis.
1903 	 */
1904 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
1905 
1906 	/*
1907 	 * Tell the chip to insert VLAN tags on a per-packet basis as
1908 	 * dictated by the code in the frame encapsulation routine.
1909 	 */
1910 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
1911 
1912 	/* Set full/half duplex mode. */
1913 	if (sc->nge_tbi) {
1914 		if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
1915 		    == IFM_FDX) {
1916 			NGE_SETBIT(sc, NGE_TX_CFG,
1917 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1918 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1919 		} else {
1920 			NGE_CLRBIT(sc, NGE_TX_CFG,
1921 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1922 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1923 		}
1924 	} else {
1925 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1926 			NGE_SETBIT(sc, NGE_TX_CFG,
1927 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1928 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1929 		} else {
1930 			NGE_CLRBIT(sc, NGE_TX_CFG,
1931 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1932 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1933 		}
1934 	}
1935 
1936 	nge_tick(sc);
1937 
1938 	/*
1939 	 * Enable the delivery of PHY interrupts based on
1940 	 * link/speed/duplex status changes. Also enable the
1941 	 * extsts field in the DMA descriptors (needed for
1942 	 * TCP/IP checksum offload on transmit).
1943 	 */
1944 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD|
1945 	    NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB);
1946 
1947 	/*
1948 	 * Configure interrupt holdoff (moderation). We can
1949 	 * have the chip delay interrupt delivery for a certain
1950 	 * period. Units are in 100us, and the max setting
1951 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
1952 	 */
1953 	CSR_WRITE_4(sc, NGE_IHR, 0x01);
1954 
1955 	/*
1956 	 * Enable interrupts.
1957 	 */
1958 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
1959 #ifdef DEVICE_POLLING
1960 	/*
1961 	 * ... only enable interrupts if we are not polling, make sure
1962 	 * they are off otherwise.
1963 	 */
1964 	if (ifp->if_flags & IFF_POLLING)
1965 		CSR_WRITE_4(sc, NGE_IER, 0);
1966 	else
1967 #endif /* DEVICE_POLLING */
1968 	CSR_WRITE_4(sc, NGE_IER, 1);
1969 
1970 	/* Enable receiver and transmitter. */
1971 	NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1972 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1973 
1974 	nge_ifmedia_upd(ifp);
1975 
1976 	ifp->if_flags |= IFF_RUNNING;
1977 	ifp->if_flags &= ~IFF_OACTIVE;
1978 
1979 	(void)splx(s);
1980 
1981 	return;
1982 }
1983 
1984 /*
1985  * Set media options.
1986  */
1987 static int
1988 nge_ifmedia_upd(ifp)
1989 	struct ifnet		*ifp;
1990 {
1991 	struct nge_softc	*sc;
1992 	struct mii_data		*mii;
1993 
1994 	sc = ifp->if_softc;
1995 
1996 	if (sc->nge_tbi) {
1997 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
1998 		     == IFM_AUTO) {
1999 			CSR_WRITE_4(sc, NGE_TBI_ANAR,
2000 				CSR_READ_4(sc, NGE_TBI_ANAR)
2001 					| NGE_TBIANAR_HDX | NGE_TBIANAR_FDX
2002 					| NGE_TBIANAR_PS1 | NGE_TBIANAR_PS2);
2003 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG
2004 				| NGE_TBIBMCR_RESTART_ANEG);
2005 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG);
2006 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media
2007 			    & IFM_GMASK) == IFM_FDX) {
2008 			NGE_SETBIT(sc, NGE_TX_CFG,
2009 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
2010 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
2011 
2012 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
2013 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
2014 		} else {
2015 			NGE_CLRBIT(sc, NGE_TX_CFG,
2016 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
2017 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
2018 
2019 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
2020 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
2021 		}
2022 
2023 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
2024 			    & ~NGE_GPIO_GP3_OUT);
2025 	} else {
2026 		mii = device_get_softc(sc->nge_miibus);
2027 		sc->nge_link = 0;
2028 		if (mii->mii_instance) {
2029 			struct mii_softc	*miisc;
2030 			for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
2031 			    miisc = LIST_NEXT(miisc, mii_list))
2032 				mii_phy_reset(miisc);
2033 		}
2034 		mii_mediachg(mii);
2035 	}
2036 
2037 	return(0);
2038 }
2039 
2040 /*
2041  * Report current media status.
2042  */
2043 static void
2044 nge_ifmedia_sts(ifp, ifmr)
2045 	struct ifnet		*ifp;
2046 	struct ifmediareq	*ifmr;
2047 {
2048 	struct nge_softc	*sc;
2049 	struct mii_data		*mii;
2050 
2051 	sc = ifp->if_softc;
2052 
2053 	if (sc->nge_tbi) {
2054 		ifmr->ifm_status = IFM_AVALID;
2055 		ifmr->ifm_active = IFM_ETHER;
2056 
2057 		if (CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
2058 			ifmr->ifm_status |= IFM_ACTIVE;
2059 		}
2060 		if (CSR_READ_4(sc, NGE_TBI_BMCR) & NGE_TBIBMCR_LOOPBACK)
2061 			ifmr->ifm_active |= IFM_LOOP;
2062 		if (!CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
2063 			ifmr->ifm_active |= IFM_NONE;
2064 			ifmr->ifm_status = 0;
2065 			return;
2066 		}
2067 		ifmr->ifm_active |= IFM_1000_SX;
2068 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
2069 		    == IFM_AUTO) {
2070 			ifmr->ifm_active |= IFM_AUTO;
2071 			if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
2072 			    & NGE_TBIANAR_FDX) {
2073 				ifmr->ifm_active |= IFM_FDX;
2074 			}else if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
2075 				  & NGE_TBIANAR_HDX) {
2076 				ifmr->ifm_active |= IFM_HDX;
2077 			}
2078 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
2079 			== IFM_FDX)
2080 			ifmr->ifm_active |= IFM_FDX;
2081 		else
2082 			ifmr->ifm_active |= IFM_HDX;
2083 
2084 	} else {
2085 		mii = device_get_softc(sc->nge_miibus);
2086 		mii_pollstat(mii);
2087 		ifmr->ifm_active = mii->mii_media_active;
2088 		ifmr->ifm_status = mii->mii_media_status;
2089 	}
2090 
2091 	return;
2092 }
2093 
2094 static int
2095 nge_ioctl(ifp, command, data)
2096 	struct ifnet		*ifp;
2097 	u_long			command;
2098 	caddr_t			data;
2099 {
2100 	struct nge_softc	*sc = ifp->if_softc;
2101 	struct ifreq		*ifr = (struct ifreq *) data;
2102 	struct mii_data		*mii;
2103 	int			s, error = 0;
2104 
2105 	s = splimp();
2106 
2107 	switch(command) {
2108 	case SIOCSIFMTU:
2109 		if (ifr->ifr_mtu > NGE_JUMBO_MTU)
2110 			error = EINVAL;
2111 		else {
2112 			ifp->if_mtu = ifr->ifr_mtu;
2113 			/*
2114 			 * Workaround: if the MTU is larger than
2115 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
2116 			 * TX checksum offloading.
2117 			 */
2118 			if (ifr->ifr_mtu >= 8152)
2119 				ifp->if_hwassist = 0;
2120 			else
2121 				ifp->if_hwassist = NGE_CSUM_FEATURES;
2122 		}
2123 		break;
2124 	case SIOCSIFFLAGS:
2125 		if (ifp->if_flags & IFF_UP) {
2126 			if (ifp->if_flags & IFF_RUNNING &&
2127 			    ifp->if_flags & IFF_PROMISC &&
2128 			    !(sc->nge_if_flags & IFF_PROMISC)) {
2129 				NGE_SETBIT(sc, NGE_RXFILT_CTL,
2130 				    NGE_RXFILTCTL_ALLPHYS|
2131 				    NGE_RXFILTCTL_ALLMULTI);
2132 			} else if (ifp->if_flags & IFF_RUNNING &&
2133 			    !(ifp->if_flags & IFF_PROMISC) &&
2134 			    sc->nge_if_flags & IFF_PROMISC) {
2135 				NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2136 				    NGE_RXFILTCTL_ALLPHYS);
2137 				if (!(ifp->if_flags & IFF_ALLMULTI))
2138 					NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2139 					    NGE_RXFILTCTL_ALLMULTI);
2140 			} else {
2141 				ifp->if_flags &= ~IFF_RUNNING;
2142 				nge_init(sc);
2143 			}
2144 		} else {
2145 			if (ifp->if_flags & IFF_RUNNING)
2146 				nge_stop(sc);
2147 		}
2148 		sc->nge_if_flags = ifp->if_flags;
2149 		error = 0;
2150 		break;
2151 	case SIOCADDMULTI:
2152 	case SIOCDELMULTI:
2153 		nge_setmulti(sc);
2154 		error = 0;
2155 		break;
2156 	case SIOCGIFMEDIA:
2157 	case SIOCSIFMEDIA:
2158 		if (sc->nge_tbi) {
2159 			error = ifmedia_ioctl(ifp, ifr, &sc->nge_ifmedia,
2160 					      command);
2161 		} else {
2162 			mii = device_get_softc(sc->nge_miibus);
2163 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
2164 					      command);
2165 		}
2166 		break;
2167 	case SIOCSIFCAP:
2168 		ifp->if_capenable = ifr->ifr_reqcap;
2169 		break;
2170 	default:
2171 		error = ether_ioctl(ifp, command, data);
2172 		break;
2173 	}
2174 
2175 	(void)splx(s);
2176 
2177 	return(error);
2178 }
2179 
2180 static void
2181 nge_watchdog(ifp)
2182 	struct ifnet		*ifp;
2183 {
2184 	struct nge_softc	*sc;
2185 
2186 	sc = ifp->if_softc;
2187 
2188 	ifp->if_oerrors++;
2189 	printf("nge%d: watchdog timeout\n", sc->nge_unit);
2190 
2191 	nge_stop(sc);
2192 	nge_reset(sc);
2193 	ifp->if_flags &= ~IFF_RUNNING;
2194 	nge_init(sc);
2195 
2196 	if (ifp->if_snd.ifq_head != NULL)
2197 		nge_start(ifp);
2198 
2199 	return;
2200 }
2201 
2202 /*
2203  * Stop the adapter and free any mbufs allocated to the
2204  * RX and TX lists.
2205  */
2206 static void
2207 nge_stop(sc)
2208 	struct nge_softc	*sc;
2209 {
2210 	register int		i;
2211 	struct ifnet		*ifp;
2212 	struct mii_data		*mii;
2213 
2214 	ifp = &sc->arpcom.ac_if;
2215 	ifp->if_timer = 0;
2216 	if (sc->nge_tbi) {
2217 		mii = NULL;
2218 	} else {
2219 		mii = device_get_softc(sc->nge_miibus);
2220 	}
2221 
2222 	untimeout(nge_tick, sc, sc->nge_stat_ch);
2223 #ifdef DEVICE_POLLING
2224 	ether_poll_deregister(ifp);
2225 #endif
2226 	CSR_WRITE_4(sc, NGE_IER, 0);
2227 	CSR_WRITE_4(sc, NGE_IMR, 0);
2228 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
2229 	DELAY(1000);
2230 	CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0);
2231 	CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0);
2232 
2233 	if (!sc->nge_tbi)
2234 		mii_down(mii);
2235 
2236 	sc->nge_link = 0;
2237 
2238 	/*
2239 	 * Free data in the RX lists.
2240 	 */
2241 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
2242 		if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) {
2243 			m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf);
2244 			sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL;
2245 		}
2246 	}
2247 	bzero((char *)&sc->nge_ldata->nge_rx_list,
2248 		sizeof(sc->nge_ldata->nge_rx_list));
2249 
2250 	/*
2251 	 * Free the TX list buffers.
2252 	 */
2253 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
2254 		if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) {
2255 			m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf);
2256 			sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL;
2257 		}
2258 	}
2259 
2260 	bzero((char *)&sc->nge_ldata->nge_tx_list,
2261 		sizeof(sc->nge_ldata->nge_tx_list));
2262 
2263 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2264 
2265 	return;
2266 }
2267 
2268 /*
2269  * Stop all chip I/O so that the kernel's probe routines don't
2270  * get confused by errant DMAs when rebooting.
2271  */
2272 static void
2273 nge_shutdown(dev)
2274 	device_t		dev;
2275 {
2276 	struct nge_softc	*sc;
2277 
2278 	sc = device_get_softc(dev);
2279 
2280 	nge_reset(sc);
2281 	nge_stop(sc);
2282 
2283 	return;
2284 }
2285