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