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