ALTQ support.
[dragonfly.git] / sys / dev / netif / wb / if_wb.c
1 /*
2  * Copyright (c) 1997, 1998
3  *      Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * $FreeBSD: src/sys/pci/if_wb.c,v 1.26.2.6 2003/03/05 18:42:34 njl Exp $
33  * $DragonFly: src/sys/dev/netif/wb/if_wb.c,v 1.16 2005/02/20 03:55:14 joerg Exp $
34  *
35  * $FreeBSD: src/sys/pci/if_wb.c,v 1.26.2.6 2003/03/05 18:42:34 njl Exp $
36  */
37
38 /*
39  * Winbond fast ethernet PCI NIC driver
40  *
41  * Supports various cheap network adapters based on the Winbond W89C840F
42  * fast ethernet controller chip. This includes adapters manufactured by
43  * Winbond itself and some made by Linksys.
44  *
45  * Written by Bill Paul <wpaul@ctr.columbia.edu>
46  * Electrical Engineering Department
47  * Columbia University, New York City
48  */
49
50 /*
51  * The Winbond W89C840F chip is a bus master; in some ways it resembles
52  * a DEC 'tulip' chip, only not as complicated. Unfortunately, it has
53  * one major difference which is that while the registers do many of
54  * the same things as a tulip adapter, the offsets are different: where
55  * tulip registers are typically spaced 8 bytes apart, the Winbond
56  * registers are spaced 4 bytes apart. The receiver filter is also
57  * programmed differently.
58  * 
59  * Like the tulip, the Winbond chip uses small descriptors containing
60  * a status word, a control word and 32-bit areas that can either be used
61  * to point to two external data blocks, or to point to a single block
62  * and another descriptor in a linked list. Descriptors can be grouped
63  * together in blocks to form fixed length rings or can be chained
64  * together in linked lists. A single packet may be spread out over
65  * several descriptors if necessary.
66  *
67  * For the receive ring, this driver uses a linked list of descriptors,
68  * each pointing to a single mbuf cluster buffer, which us large enough
69  * to hold an entire packet. The link list is looped back to created a
70  * closed ring.
71  *
72  * For transmission, the driver creates a linked list of 'super descriptors'
73  * which each contain several individual descriptors linked toghether.
74  * Each 'super descriptor' contains WB_MAXFRAGS descriptors, which we
75  * abuse as fragment pointers. This allows us to use a buffer managment
76  * scheme very similar to that used in the ThunderLAN and Etherlink XL
77  * drivers.
78  *
79  * Autonegotiation is performed using the external PHY via the MII bus.
80  * The sample boards I have all use a Davicom PHY.
81  *
82  * Note: the author of the Linux driver for the Winbond chip alludes
83  * to some sort of flaw in the chip's design that seems to mandate some
84  * drastic workaround which signigicantly impairs transmit performance.
85  * I have no idea what he's on about: transmit performance with all
86  * three of my test boards seems fine.
87  */
88
89 #include "opt_bdg.h"
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 #include <sys/queue.h>
99
100 #include <net/if.h>
101 #include <net/ifq_var.h>
102 #include <net/if_arp.h>
103 #include <net/ethernet.h>
104 #include <net/if_dl.h>
105 #include <net/if_media.h>
106
107 #include <net/bpf.h>
108
109 #include <vm/vm.h>              /* for vtophys */
110 #include <vm/pmap.h>            /* for vtophys */
111 #include <machine/clock.h>      /* for DELAY */
112 #include <machine/bus_memio.h>
113 #include <machine/bus_pio.h>
114 #include <machine/bus.h>
115 #include <machine/resource.h>
116 #include <sys/bus.h>
117 #include <sys/rman.h>
118
119 #include <bus/pci/pcireg.h>
120 #include <bus/pci/pcivar.h>
121
122 #include "../mii_layer/mii.h"
123 #include "../mii_layer/miivar.h"
124
125 /* "controller miibus0" required.  See GENERIC if you get errors here. */
126 #include "miibus_if.h"
127
128 #define WB_USEIOSPACE
129
130 #include "if_wbreg.h"
131
132 /*
133  * Various supported device vendors/types and their names.
134  */
135 static struct wb_type wb_devs[] = {
136         { WB_VENDORID, WB_DEVICEID_840F,
137                 "Winbond W89C840F 10/100BaseTX" },
138         { CP_VENDORID, CP_DEVICEID_RL100,
139                 "Compex RL100-ATX 10/100baseTX" },
140         { 0, 0, NULL }
141 };
142
143 static int wb_probe             (device_t);
144 static int wb_attach            (device_t);
145 static int wb_detach            (device_t);
146
147 static void wb_bfree            (caddr_t, u_int);
148 static int wb_newbuf            (struct wb_softc *,
149                                         struct wb_chain_onefrag *,
150                                         struct mbuf *);
151 static int wb_encap             (struct wb_softc *, struct wb_chain *,
152                                         struct mbuf *);
153
154 static void wb_rxeof            (struct wb_softc *);
155 static void wb_rxeoc            (struct wb_softc *);
156 static void wb_txeof            (struct wb_softc *);
157 static void wb_txeoc            (struct wb_softc *);
158 static void wb_intr             (void *);
159 static void wb_tick             (void *);
160 static void wb_start            (struct ifnet *);
161 static int wb_ioctl             (struct ifnet *, u_long, caddr_t,
162                                         struct ucred *);
163 static void wb_init             (void *);
164 static void wb_stop             (struct wb_softc *);
165 static void wb_watchdog         (struct ifnet *);
166 static void wb_shutdown         (device_t);
167 static int wb_ifmedia_upd       (struct ifnet *);
168 static void wb_ifmedia_sts      (struct ifnet *, struct ifmediareq *);
169
170 static void wb_eeprom_putbyte   (struct wb_softc *, int);
171 static void wb_eeprom_getword   (struct wb_softc *, int, u_int16_t *);
172 static void wb_read_eeprom      (struct wb_softc *, caddr_t, int,
173                                                         int, int);
174 static void wb_mii_sync         (struct wb_softc *);
175 static void wb_mii_send         (struct wb_softc *, u_int32_t, int);
176 static int wb_mii_readreg       (struct wb_softc *, struct wb_mii_frame *);
177 static int wb_mii_writereg      (struct wb_softc *, struct wb_mii_frame *);
178
179 static void wb_setcfg           (struct wb_softc *, u_int32_t);
180 static u_int8_t wb_calchash     (caddr_t);
181 static void wb_setmulti         (struct wb_softc *);
182 static void wb_reset            (struct wb_softc *);
183 static void wb_fixmedia         (struct wb_softc *);
184 static int wb_list_rx_init      (struct wb_softc *);
185 static int wb_list_tx_init      (struct wb_softc *);
186
187 static int wb_miibus_readreg    (device_t, int, int);
188 static int wb_miibus_writereg   (device_t, int, int, int);
189 static void wb_miibus_statchg   (device_t);
190
191 #ifdef WB_USEIOSPACE
192 #define WB_RES                  SYS_RES_IOPORT
193 #define WB_RID                  WB_PCI_LOIO
194 #else
195 #define WB_RES                  SYS_RES_MEMORY
196 #define WB_RID                  WB_PCI_LOMEM
197 #endif
198
199 static device_method_t wb_methods[] = {
200         /* Device interface */
201         DEVMETHOD(device_probe,         wb_probe),
202         DEVMETHOD(device_attach,        wb_attach),
203         DEVMETHOD(device_detach,        wb_detach),
204         DEVMETHOD(device_shutdown,      wb_shutdown),
205
206         /* bus interface, for miibus */
207         DEVMETHOD(bus_print_child,      bus_generic_print_child),
208         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
209
210         /* MII interface */
211         DEVMETHOD(miibus_readreg,       wb_miibus_readreg),
212         DEVMETHOD(miibus_writereg,      wb_miibus_writereg),
213         DEVMETHOD(miibus_statchg,       wb_miibus_statchg),
214         { 0, 0 }
215 };
216
217 static driver_t wb_driver = {
218         "wb",
219         wb_methods,
220         sizeof(struct wb_softc)
221 };
222
223 static devclass_t wb_devclass;
224
225 DECLARE_DUMMY_MODULE(if_wb);
226 DRIVER_MODULE(if_wb, pci, wb_driver, wb_devclass, 0, 0);
227 DRIVER_MODULE(miibus, wb, miibus_driver, miibus_devclass, 0, 0);
228
229 #define WB_SETBIT(sc, reg, x)                           \
230         CSR_WRITE_4(sc, reg,                            \
231                 CSR_READ_4(sc, reg) | x)
232
233 #define WB_CLRBIT(sc, reg, x)                           \
234         CSR_WRITE_4(sc, reg,                            \
235                 CSR_READ_4(sc, reg) & ~x)
236
237 #define SIO_SET(x)                                      \
238         CSR_WRITE_4(sc, WB_SIO,                         \
239                 CSR_READ_4(sc, WB_SIO) | x)
240
241 #define SIO_CLR(x)                                      \
242         CSR_WRITE_4(sc, WB_SIO,                         \
243                 CSR_READ_4(sc, WB_SIO) & ~x)
244
245 /*
246  * Send a read command and address to the EEPROM, check for ACK.
247  */
248 static void wb_eeprom_putbyte(sc, addr)
249         struct wb_softc         *sc;
250         int                     addr;
251 {
252         int             d, i;
253
254         d = addr | WB_EECMD_READ;
255
256         /*
257          * Feed in each bit and stobe the clock.
258          */
259         for (i = 0x400; i; i >>= 1) {
260                 if (d & i) {
261                         SIO_SET(WB_SIO_EE_DATAIN);
262                 } else {
263                         SIO_CLR(WB_SIO_EE_DATAIN);
264                 }
265                 DELAY(100);
266                 SIO_SET(WB_SIO_EE_CLK);
267                 DELAY(150);
268                 SIO_CLR(WB_SIO_EE_CLK);
269                 DELAY(100);
270         }
271
272         return;
273 }
274
275 /*
276  * Read a word of data stored in the EEPROM at address 'addr.'
277  */
278 static void wb_eeprom_getword(sc, addr, dest)
279         struct wb_softc         *sc;
280         int                     addr;
281         u_int16_t               *dest;
282 {
283         int             i;
284         u_int16_t               word = 0;
285
286         /* Enter EEPROM access mode. */
287         CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
288
289         /*
290          * Send address of word we want to read.
291          */
292         wb_eeprom_putbyte(sc, addr);
293
294         CSR_WRITE_4(sc, WB_SIO, WB_SIO_EESEL|WB_SIO_EE_CS);
295
296         /*
297          * Start reading bits from EEPROM.
298          */
299         for (i = 0x8000; i; i >>= 1) {
300                 SIO_SET(WB_SIO_EE_CLK);
301                 DELAY(100);
302                 if (CSR_READ_4(sc, WB_SIO) & WB_SIO_EE_DATAOUT)
303                         word |= i;
304                 SIO_CLR(WB_SIO_EE_CLK);
305                 DELAY(100);
306         }
307
308         /* Turn off EEPROM access mode. */
309         CSR_WRITE_4(sc, WB_SIO, 0);
310
311         *dest = word;
312
313         return;
314 }
315
316 /*
317  * Read a sequence of words from the EEPROM.
318  */
319 static void wb_read_eeprom(sc, dest, off, cnt, swap)
320         struct wb_softc         *sc;
321         caddr_t                 dest;
322         int                     off;
323         int                     cnt;
324         int                     swap;
325 {
326         int                     i;
327         u_int16_t               word = 0, *ptr;
328
329         for (i = 0; i < cnt; i++) {
330                 wb_eeprom_getword(sc, off + i, &word);
331                 ptr = (u_int16_t *)(dest + (i * 2));
332                 if (swap)
333                         *ptr = ntohs(word);
334                 else
335                         *ptr = word;
336         }
337
338         return;
339 }
340
341 /*
342  * Sync the PHYs by setting data bit and strobing the clock 32 times.
343  */
344 static void wb_mii_sync(sc)
345         struct wb_softc         *sc;
346 {
347         int             i;
348
349         SIO_SET(WB_SIO_MII_DIR|WB_SIO_MII_DATAIN);
350
351         for (i = 0; i < 32; i++) {
352                 SIO_SET(WB_SIO_MII_CLK);
353                 DELAY(1);
354                 SIO_CLR(WB_SIO_MII_CLK);
355                 DELAY(1);
356         }
357
358         return;
359 }
360
361 /*
362  * Clock a series of bits through the MII.
363  */
364 static void wb_mii_send(sc, bits, cnt)
365         struct wb_softc         *sc;
366         u_int32_t               bits;
367         int                     cnt;
368 {
369         int                     i;
370
371         SIO_CLR(WB_SIO_MII_CLK);
372
373         for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
374                 if (bits & i) {
375                         SIO_SET(WB_SIO_MII_DATAIN);
376                 } else {
377                         SIO_CLR(WB_SIO_MII_DATAIN);
378                 }
379                 DELAY(1);
380                 SIO_CLR(WB_SIO_MII_CLK);
381                 DELAY(1);
382                 SIO_SET(WB_SIO_MII_CLK);
383         }
384 }
385
386 /*
387  * Read an PHY register through the MII.
388  */
389 static int wb_mii_readreg(sc, frame)
390         struct wb_softc         *sc;
391         struct wb_mii_frame     *frame;
392         
393 {
394         int                     i, ack, s;
395
396         s = splimp();
397
398         /*
399          * Set up frame for RX.
400          */
401         frame->mii_stdelim = WB_MII_STARTDELIM;
402         frame->mii_opcode = WB_MII_READOP;
403         frame->mii_turnaround = 0;
404         frame->mii_data = 0;
405         
406         CSR_WRITE_4(sc, WB_SIO, 0);
407
408         /*
409          * Turn on data xmit.
410          */
411         SIO_SET(WB_SIO_MII_DIR);
412
413         wb_mii_sync(sc);
414
415         /*
416          * Send command/address info.
417          */
418         wb_mii_send(sc, frame->mii_stdelim, 2);
419         wb_mii_send(sc, frame->mii_opcode, 2);
420         wb_mii_send(sc, frame->mii_phyaddr, 5);
421         wb_mii_send(sc, frame->mii_regaddr, 5);
422
423         /* Idle bit */
424         SIO_CLR((WB_SIO_MII_CLK|WB_SIO_MII_DATAIN));
425         DELAY(1);
426         SIO_SET(WB_SIO_MII_CLK);
427         DELAY(1);
428
429         /* Turn off xmit. */
430         SIO_CLR(WB_SIO_MII_DIR);
431         /* Check for ack */
432         SIO_CLR(WB_SIO_MII_CLK);
433         DELAY(1);
434         ack = CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT;
435         SIO_SET(WB_SIO_MII_CLK);
436         DELAY(1);
437         SIO_CLR(WB_SIO_MII_CLK);
438         DELAY(1);
439         SIO_SET(WB_SIO_MII_CLK);
440         DELAY(1);
441
442         /*
443          * Now try reading data bits. If the ack failed, we still
444          * need to clock through 16 cycles to keep the PHY(s) in sync.
445          */
446         if (ack) {
447                 for(i = 0; i < 16; i++) {
448                         SIO_CLR(WB_SIO_MII_CLK);
449                         DELAY(1);
450                         SIO_SET(WB_SIO_MII_CLK);
451                         DELAY(1);
452                 }
453                 goto fail;
454         }
455
456         for (i = 0x8000; i; i >>= 1) {
457                 SIO_CLR(WB_SIO_MII_CLK);
458                 DELAY(1);
459                 if (!ack) {
460                         if (CSR_READ_4(sc, WB_SIO) & WB_SIO_MII_DATAOUT)
461                                 frame->mii_data |= i;
462                         DELAY(1);
463                 }
464                 SIO_SET(WB_SIO_MII_CLK);
465                 DELAY(1);
466         }
467
468 fail:
469
470         SIO_CLR(WB_SIO_MII_CLK);
471         DELAY(1);
472         SIO_SET(WB_SIO_MII_CLK);
473         DELAY(1);
474
475         splx(s);
476
477         if (ack)
478                 return(1);
479         return(0);
480 }
481
482 /*
483  * Write to a PHY register through the MII.
484  */
485 static int wb_mii_writereg(sc, frame)
486         struct wb_softc         *sc;
487         struct wb_mii_frame     *frame;
488         
489 {
490         int                     s;
491
492         s = splimp();
493         /*
494          * Set up frame for TX.
495          */
496
497         frame->mii_stdelim = WB_MII_STARTDELIM;
498         frame->mii_opcode = WB_MII_WRITEOP;
499         frame->mii_turnaround = WB_MII_TURNAROUND;
500         
501         /*
502          * Turn on data output.
503          */
504         SIO_SET(WB_SIO_MII_DIR);
505
506         wb_mii_sync(sc);
507
508         wb_mii_send(sc, frame->mii_stdelim, 2);
509         wb_mii_send(sc, frame->mii_opcode, 2);
510         wb_mii_send(sc, frame->mii_phyaddr, 5);
511         wb_mii_send(sc, frame->mii_regaddr, 5);
512         wb_mii_send(sc, frame->mii_turnaround, 2);
513         wb_mii_send(sc, frame->mii_data, 16);
514
515         /* Idle bit. */
516         SIO_SET(WB_SIO_MII_CLK);
517         DELAY(1);
518         SIO_CLR(WB_SIO_MII_CLK);
519         DELAY(1);
520
521         /*
522          * Turn off xmit.
523          */
524         SIO_CLR(WB_SIO_MII_DIR);
525
526         splx(s);
527
528         return(0);
529 }
530
531 static int wb_miibus_readreg(dev, phy, reg)
532         device_t                dev;
533         int                     phy, reg;
534 {
535         struct wb_softc         *sc;
536         struct wb_mii_frame     frame;
537
538         sc = device_get_softc(dev);
539
540         bzero((char *)&frame, sizeof(frame));
541
542         frame.mii_phyaddr = phy;
543         frame.mii_regaddr = reg;
544         wb_mii_readreg(sc, &frame);
545
546         return(frame.mii_data);
547 }
548
549 static int wb_miibus_writereg(dev, phy, reg, data)
550         device_t                dev;
551         int                     phy, reg, data;
552 {
553         struct wb_softc         *sc;
554         struct wb_mii_frame     frame;
555
556         sc = device_get_softc(dev);
557
558         bzero((char *)&frame, sizeof(frame));
559
560         frame.mii_phyaddr = phy;
561         frame.mii_regaddr = reg;
562         frame.mii_data = data;
563
564         wb_mii_writereg(sc, &frame);
565
566         return(0);
567 }
568
569 static void wb_miibus_statchg(dev)
570         device_t                dev;
571 {
572         struct wb_softc         *sc;
573         struct mii_data         *mii;
574
575         sc = device_get_softc(dev);
576         mii = device_get_softc(sc->wb_miibus);
577         wb_setcfg(sc, mii->mii_media_active);
578
579         return;
580 }
581
582 static u_int8_t wb_calchash(addr)
583         caddr_t                 addr;
584 {
585         u_int32_t               crc, carry;
586         int                     i, j;
587         u_int8_t                c;
588
589         /* Compute CRC for the address value. */
590         crc = 0xFFFFFFFF; /* initial value */
591
592         for (i = 0; i < 6; i++) {
593                 c = *(addr + i);
594                 for (j = 0; j < 8; j++) {
595                         carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
596                         crc <<= 1;
597                         c >>= 1;
598                         if (carry)
599                                 crc = (crc ^ 0x04c11db6) | carry;
600                 }
601         }
602
603         /*
604          * return the filter bit position
605          * Note: I arrived at the following nonsense
606          * through experimentation. It's not the usual way to
607          * generate the bit position but it's the only thing
608          * I could come up with that works.
609          */
610         return(~(crc >> 26) & 0x0000003F);
611 }
612
613 /*
614  * Program the 64-bit multicast hash filter.
615  */
616 static void wb_setmulti(sc)
617         struct wb_softc         *sc;
618 {
619         struct ifnet            *ifp;
620         int                     h = 0;
621         u_int32_t               hashes[2] = { 0, 0 };
622         struct ifmultiaddr      *ifma;
623         u_int32_t               rxfilt;
624         int                     mcnt = 0;
625
626         ifp = &sc->arpcom.ac_if;
627
628         rxfilt = CSR_READ_4(sc, WB_NETCFG);
629
630         if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
631                 rxfilt |= WB_NETCFG_RX_MULTI;
632                 CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
633                 CSR_WRITE_4(sc, WB_MAR0, 0xFFFFFFFF);
634                 CSR_WRITE_4(sc, WB_MAR1, 0xFFFFFFFF);
635                 return;
636         }
637
638         /* first, zot all the existing hash bits */
639         CSR_WRITE_4(sc, WB_MAR0, 0);
640         CSR_WRITE_4(sc, WB_MAR1, 0);
641
642         /* now program new ones */
643         for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
644                                 ifma = ifma->ifma_link.le_next) {
645                 if (ifma->ifma_addr->sa_family != AF_LINK)
646                         continue;
647                 h = wb_calchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
648                 if (h < 32)
649                         hashes[0] |= (1 << h);
650                 else
651                         hashes[1] |= (1 << (h - 32));
652                 mcnt++;
653         }
654
655         if (mcnt)
656                 rxfilt |= WB_NETCFG_RX_MULTI;
657         else
658                 rxfilt &= ~WB_NETCFG_RX_MULTI;
659
660         CSR_WRITE_4(sc, WB_MAR0, hashes[0]);
661         CSR_WRITE_4(sc, WB_MAR1, hashes[1]);
662         CSR_WRITE_4(sc, WB_NETCFG, rxfilt);
663
664         return;
665 }
666
667 /*
668  * The Winbond manual states that in order to fiddle with the
669  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
670  * first have to put the transmit and/or receive logic in the idle state.
671  */
672 static void wb_setcfg(sc, media)
673         struct wb_softc         *sc;
674         u_int32_t               media;
675 {
676         int                     i, restart = 0;
677
678         if (CSR_READ_4(sc, WB_NETCFG) & (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON)) {
679                 restart = 1;
680                 WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_TX_ON|WB_NETCFG_RX_ON));
681
682                 for (i = 0; i < WB_TIMEOUT; i++) {
683                         DELAY(10);
684                         if ((CSR_READ_4(sc, WB_ISR) & WB_ISR_TX_IDLE) &&
685                                 (CSR_READ_4(sc, WB_ISR) & WB_ISR_RX_IDLE))
686                                 break;
687                 }
688
689                 if (i == WB_TIMEOUT)
690                         printf("wb%d: failed to force tx and "
691                                 "rx to idle state\n", sc->wb_unit);
692         }
693
694         if (IFM_SUBTYPE(media) == IFM_10_T)
695                 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
696         else
697                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_100MBPS);
698
699         if ((media & IFM_GMASK) == IFM_FDX)
700                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
701         else
702                 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_FULLDUPLEX);
703
704         if (restart)
705                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON|WB_NETCFG_RX_ON);
706
707         return;
708 }
709
710 static void wb_reset(sc)
711         struct wb_softc         *sc;
712 {
713         int             i;
714         struct mii_data         *mii;
715
716         CSR_WRITE_4(sc, WB_NETCFG, 0);
717         CSR_WRITE_4(sc, WB_BUSCTL, 0);
718         CSR_WRITE_4(sc, WB_TXADDR, 0);
719         CSR_WRITE_4(sc, WB_RXADDR, 0);
720
721         WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
722         WB_SETBIT(sc, WB_BUSCTL, WB_BUSCTL_RESET);
723
724         for (i = 0; i < WB_TIMEOUT; i++) {
725                 DELAY(10);
726                 if (!(CSR_READ_4(sc, WB_BUSCTL) & WB_BUSCTL_RESET))
727                         break;
728         }
729         if (i == WB_TIMEOUT)
730                 printf("wb%d: reset never completed!\n", sc->wb_unit);
731
732         /* Wait a little while for the chip to get its brains in order. */
733         DELAY(1000);
734
735         if (sc->wb_miibus == NULL)
736                 return;
737
738         mii = device_get_softc(sc->wb_miibus);
739         if (mii == NULL)
740                 return;
741
742         if (mii->mii_instance) {
743                 struct mii_softc        *miisc;
744                 for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
745                                 miisc = LIST_NEXT(miisc, mii_list))
746                         mii_phy_reset(miisc);
747         }
748
749         return;
750 }
751
752 static void wb_fixmedia(sc)
753         struct wb_softc         *sc;
754 {
755         struct mii_data         *mii = NULL;
756         struct ifnet            *ifp;
757         u_int32_t               media;
758
759         if (sc->wb_miibus == NULL)
760                 return;
761
762         mii = device_get_softc(sc->wb_miibus);
763         ifp = &sc->arpcom.ac_if;
764
765         mii_pollstat(mii);
766         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) {
767                 media = mii->mii_media_active & ~IFM_10_T;
768                 media |= IFM_100_TX;
769         } else if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) {
770                 media = mii->mii_media_active & ~IFM_100_TX;
771                 media |= IFM_10_T;
772         } else
773                 return;
774
775         ifmedia_set(&mii->mii_media, media);
776
777         return;
778 }
779
780 /*
781  * Probe for a Winbond 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 wb_probe(dev)
785         device_t                dev;
786 {
787         struct wb_type          *t;
788
789         t = wb_devs;
790
791         while(t->wb_name != NULL) {
792                 if ((pci_get_vendor(dev) == t->wb_vid) &&
793                     (pci_get_device(dev) == t->wb_did)) {
794                         device_set_desc(dev, t->wb_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 wb_attach(dev)
808         device_t                dev;
809 {
810         int                     s;
811         u_char                  eaddr[ETHER_ADDR_LEN];
812         u_int32_t               command;
813         struct wb_softc         *sc;
814         struct ifnet            *ifp;
815         int                     unit, error = 0, rid;
816
817         s = splimp();
818
819         sc = device_get_softc(dev);
820         unit = device_get_unit(dev);
821         callout_init(&sc->wb_stat_timer);
822
823         /*
824          * Handle power management nonsense.
825          */
826
827         command = pci_read_config(dev, WB_PCI_CAPID, 4) & 0x000000FF;
828         if (command == 0x01) {
829
830                 command = pci_read_config(dev, WB_PCI_PWRMGMTCTRL, 4);
831                 if (command & WB_PSTATE_MASK) {
832                         u_int32_t               iobase, membase, irq;
833
834                         /* Save important PCI config data. */
835                         iobase = pci_read_config(dev, WB_PCI_LOIO, 4);
836                         membase = pci_read_config(dev, WB_PCI_LOMEM, 4);
837                         irq = pci_read_config(dev, WB_PCI_INTLINE, 4);
838
839                         /* Reset the power state. */
840                         printf("wb%d: chip is in D%d power mode "
841                         "-- setting to D0\n", unit, command & WB_PSTATE_MASK);
842                         command &= 0xFFFFFFFC;
843                         pci_write_config(dev, WB_PCI_PWRMGMTCTRL, command, 4);
844
845                         /* Restore PCI config data. */
846                         pci_write_config(dev, WB_PCI_LOIO, iobase, 4);
847                         pci_write_config(dev, WB_PCI_LOMEM, membase, 4);
848                         pci_write_config(dev, WB_PCI_INTLINE, irq, 4);
849                 }
850         }
851
852         /*
853          * Map control/status registers.
854          */
855         command = pci_read_config(dev, PCIR_COMMAND, 4);
856         command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
857         pci_write_config(dev, PCIR_COMMAND, command, 4);
858         command = pci_read_config(dev, PCIR_COMMAND, 4);
859
860 #ifdef WB_USEIOSPACE
861         if (!(command & PCIM_CMD_PORTEN)) {
862                 printf("wb%d: failed to enable I/O ports!\n", unit);
863                 error = ENXIO;
864                 goto fail;
865         }
866 #else
867         if (!(command & PCIM_CMD_MEMEN)) {
868                 printf("wb%d: failed to enable memory mapping!\n", unit);
869                 error = ENXIO;
870                 goto fail;
871         }
872 #endif
873
874         rid = WB_RID;
875         sc->wb_res = bus_alloc_resource(dev, WB_RES, &rid,
876             0, ~0, 1, RF_ACTIVE);
877
878         if (sc->wb_res == NULL) {
879                 printf("wb%d: couldn't map ports/memory\n", unit);
880                 error = ENXIO;
881                 goto fail;
882         }
883
884         sc->wb_btag = rman_get_bustag(sc->wb_res);
885         sc->wb_bhandle = rman_get_bushandle(sc->wb_res);
886
887         /* Allocate interrupt */
888         rid = 0;
889         sc->wb_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
890             RF_SHAREABLE | RF_ACTIVE);
891
892         if (sc->wb_irq == NULL) {
893                 printf("wb%d: couldn't map interrupt\n", unit);
894                 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
895                 error = ENXIO;
896                 goto fail;
897         }
898
899         error = bus_setup_intr(dev, sc->wb_irq, INTR_TYPE_NET,
900             wb_intr, sc, &sc->wb_intrhand);
901
902         if (error) {
903                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
904                 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
905                 printf("wb%d: couldn't set up irq\n", unit);
906                 goto fail;
907         }
908
909         /* Save the cache line size. */
910         sc->wb_cachesize = pci_read_config(dev, WB_PCI_CACHELEN, 4) & 0xFF;
911
912         /* Reset the adapter. */
913         wb_reset(sc);
914
915         /*
916          * Get station address from the EEPROM.
917          */
918         wb_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 0);
919
920         sc->wb_unit = unit;
921
922         sc->wb_ldata = contigmalloc(sizeof(struct wb_list_data) + 8, M_DEVBUF,
923             M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
924
925         if (sc->wb_ldata == NULL) {
926                 printf("wb%d: no memory for list buffers!\n", unit);
927                 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand);
928                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
929                 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
930                 error = ENXIO;
931                 goto fail;
932         }
933
934         bzero(sc->wb_ldata, sizeof(struct wb_list_data));
935
936         ifp = &sc->arpcom.ac_if;
937         ifp->if_softc = sc;
938         if_initname(ifp, "wb", unit);
939         ifp->if_mtu = ETHERMTU;
940         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
941         ifp->if_ioctl = wb_ioctl;
942         ifp->if_start = wb_start;
943         ifp->if_watchdog = wb_watchdog;
944         ifp->if_init = wb_init;
945         ifp->if_baudrate = 10000000;
946         ifq_set_maxlen(&ifp->if_snd, WB_TX_LIST_CNT - 1);
947         ifq_set_ready(&ifp->if_snd);
948
949         /*
950          * Do MII setup.
951          */
952         if (mii_phy_probe(dev, &sc->wb_miibus,
953             wb_ifmedia_upd, wb_ifmedia_sts)) {
954                 contigfree(sc->wb_ldata_ptr, sizeof(struct wb_list_data) + 8,
955                     M_DEVBUF);
956                 bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand);
957                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
958                 bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
959                 error = ENXIO;
960                 goto fail;
961         }
962
963         /*
964          * Call MI attach routine.
965          */
966         ether_ifattach(ifp, eaddr);
967
968 fail:
969         if (error)
970                 device_delete_child(dev, sc->wb_miibus);
971         splx(s);
972
973         return(error);
974 }
975
976 static int wb_detach(dev)
977         device_t                dev;
978 {
979         struct wb_softc         *sc;
980         struct ifnet            *ifp;
981         int                     s;
982
983         s = splimp();
984
985         sc = device_get_softc(dev);
986         ifp = &sc->arpcom.ac_if;
987
988         wb_stop(sc);
989         ether_ifdetach(ifp);
990
991         /* Delete any miibus and phy devices attached to this interface */
992         bus_generic_detach(dev);
993         device_delete_child(dev, sc->wb_miibus);
994
995         bus_teardown_intr(dev, sc->wb_irq, sc->wb_intrhand);
996         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->wb_irq);
997         bus_release_resource(dev, WB_RES, WB_RID, sc->wb_res);
998
999         contigfree(sc->wb_ldata_ptr, sizeof(struct wb_list_data) + 8,
1000             M_DEVBUF);
1001
1002         splx(s);
1003
1004         return(0);
1005 }
1006
1007 /*
1008  * Initialize the transmit descriptors.
1009  */
1010 static int wb_list_tx_init(sc)
1011         struct wb_softc         *sc;
1012 {
1013         struct wb_chain_data    *cd;
1014         struct wb_list_data     *ld;
1015         int                     i;
1016
1017         cd = &sc->wb_cdata;
1018         ld = sc->wb_ldata;
1019
1020         for (i = 0; i < WB_TX_LIST_CNT; i++) {
1021                 cd->wb_tx_chain[i].wb_ptr = &ld->wb_tx_list[i];
1022                 if (i == (WB_TX_LIST_CNT - 1)) {
1023                         cd->wb_tx_chain[i].wb_nextdesc =
1024                                 &cd->wb_tx_chain[0];
1025                 } else {
1026                         cd->wb_tx_chain[i].wb_nextdesc =
1027                                 &cd->wb_tx_chain[i + 1];
1028                 }
1029         }
1030
1031         cd->wb_tx_free = &cd->wb_tx_chain[0];
1032         cd->wb_tx_tail = cd->wb_tx_head = NULL;
1033
1034         return(0);
1035 }
1036
1037
1038 /*
1039  * Initialize the RX descriptors and allocate mbufs for them. Note that
1040  * we arrange the descriptors in a closed ring, so that the last descriptor
1041  * points back to the first.
1042  */
1043 static int wb_list_rx_init(sc)
1044         struct wb_softc         *sc;
1045 {
1046         struct wb_chain_data    *cd;
1047         struct wb_list_data     *ld;
1048         int                     i;
1049
1050         cd = &sc->wb_cdata;
1051         ld = sc->wb_ldata;
1052
1053         for (i = 0; i < WB_RX_LIST_CNT; i++) {
1054                 cd->wb_rx_chain[i].wb_ptr =
1055                         (struct wb_desc *)&ld->wb_rx_list[i];
1056                 cd->wb_rx_chain[i].wb_buf = (void *)&ld->wb_rxbufs[i];
1057                 if (wb_newbuf(sc, &cd->wb_rx_chain[i], NULL) == ENOBUFS)
1058                         return(ENOBUFS);
1059                 if (i == (WB_RX_LIST_CNT - 1)) {
1060                         cd->wb_rx_chain[i].wb_nextdesc = &cd->wb_rx_chain[0];
1061                         ld->wb_rx_list[i].wb_next = 
1062                                         vtophys(&ld->wb_rx_list[0]);
1063                 } else {
1064                         cd->wb_rx_chain[i].wb_nextdesc =
1065                                         &cd->wb_rx_chain[i + 1];
1066                         ld->wb_rx_list[i].wb_next =
1067                                         vtophys(&ld->wb_rx_list[i + 1]);
1068                 }
1069         }
1070
1071         cd->wb_rx_head = &cd->wb_rx_chain[0];
1072
1073         return(0);
1074 }
1075
1076 static void wb_bfree(buf, size)
1077         caddr_t                 buf;
1078         u_int                   size;
1079 {
1080         return;
1081 }
1082
1083 /*
1084  * Initialize an RX descriptor and attach an MBUF cluster.
1085  */
1086 static int wb_newbuf(sc, c, m)
1087         struct wb_softc         *sc;
1088         struct wb_chain_onefrag *c;
1089         struct mbuf             *m;
1090 {
1091         struct mbuf             *m_new = NULL;
1092
1093         if (m == NULL) {
1094                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
1095                 if (m_new == NULL)
1096                         return(ENOBUFS);
1097
1098                 m_new->m_data = m_new->m_ext.ext_buf = c->wb_buf;
1099                 m_new->m_flags |= M_EXT | M_EXT_OLD;
1100                 m_new->m_ext.ext_size = m_new->m_pkthdr.len =
1101                     m_new->m_len = WB_BUFBYTES;
1102                 m_new->m_ext.ext_nfree.old = wb_bfree;
1103                 m_new->m_ext.ext_nref.old = wb_bfree;
1104         } else {
1105                 m_new = m;
1106                 m_new->m_len = m_new->m_pkthdr.len = WB_BUFBYTES;
1107                 m_new->m_data = m_new->m_ext.ext_buf;
1108         }
1109
1110         m_adj(m_new, sizeof(u_int64_t));
1111
1112         c->wb_mbuf = m_new;
1113         c->wb_ptr->wb_data = vtophys(mtod(m_new, caddr_t));
1114         c->wb_ptr->wb_ctl = WB_RXCTL_RLINK | 1536;
1115         c->wb_ptr->wb_status = WB_RXSTAT;
1116
1117         return(0);
1118 }
1119
1120 /*
1121  * A frame has been uploaded: pass the resulting mbuf chain up to
1122  * the higher level protocols.
1123  */
1124 static void wb_rxeof(sc)
1125         struct wb_softc         *sc;
1126 {
1127         struct mbuf             *m = NULL;
1128         struct ifnet            *ifp;
1129         struct wb_chain_onefrag *cur_rx;
1130         int                     total_len = 0;
1131         u_int32_t               rxstat;
1132
1133         ifp = &sc->arpcom.ac_if;
1134
1135         while(!((rxstat = sc->wb_cdata.wb_rx_head->wb_ptr->wb_status) &
1136                                                         WB_RXSTAT_OWN)) {
1137                 struct mbuf             *m0 = NULL;
1138
1139                 cur_rx = sc->wb_cdata.wb_rx_head;
1140                 sc->wb_cdata.wb_rx_head = cur_rx->wb_nextdesc;
1141
1142                 m = cur_rx->wb_mbuf;
1143
1144                 if ((rxstat & WB_RXSTAT_MIIERR) ||
1145                     (WB_RXBYTES(cur_rx->wb_ptr->wb_status) < WB_MIN_FRAMELEN) ||
1146                     (WB_RXBYTES(cur_rx->wb_ptr->wb_status) > 1536) ||
1147                     !(rxstat & WB_RXSTAT_LASTFRAG) ||
1148                     !(rxstat & WB_RXSTAT_RXCMP)) {
1149                         ifp->if_ierrors++;
1150                         wb_newbuf(sc, cur_rx, m);
1151                         printf("wb%x: receiver babbling: possible chip "
1152                                 "bug, forcing reset\n", sc->wb_unit);
1153                         wb_fixmedia(sc);
1154                         wb_reset(sc);
1155                         wb_init(sc);
1156                         return;
1157                 }
1158
1159                 if (rxstat & WB_RXSTAT_RXERR) {
1160                         ifp->if_ierrors++;
1161                         wb_newbuf(sc, cur_rx, m);
1162                         break;
1163                 }
1164
1165                 /* No errors; receive the packet. */    
1166                 total_len = WB_RXBYTES(cur_rx->wb_ptr->wb_status);
1167
1168                 /*
1169                  * XXX The Winbond chip includes the CRC with every
1170                  * received frame, and there's no way to turn this
1171                  * behavior off (at least, I can't find anything in
1172                  * the manual that explains how to do it) so we have
1173                  * to trim off the CRC manually.
1174                  */
1175                 total_len -= ETHER_CRC_LEN;
1176
1177                 m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1178                      total_len + ETHER_ALIGN, 0, ifp, NULL);
1179                 wb_newbuf(sc, cur_rx, m);
1180                 if (m0 == NULL) {
1181                         ifp->if_ierrors++;
1182                         break;
1183                 }
1184                 m_adj(m0, ETHER_ALIGN);
1185                 m = m0;
1186
1187                 ifp->if_ipackets++;
1188                 (*ifp->if_input)(ifp, m);
1189         }
1190 }
1191
1192 void wb_rxeoc(sc)
1193         struct wb_softc         *sc;
1194 {
1195         wb_rxeof(sc);
1196
1197         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1198         CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
1199         WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1200         if (CSR_READ_4(sc, WB_ISR) & WB_RXSTATE_SUSPEND)
1201                 CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
1202
1203         return;
1204 }
1205
1206 /*
1207  * A frame was downloaded to the chip. It's safe for us to clean up
1208  * the list buffers.
1209  */
1210 static void wb_txeof(sc)
1211         struct wb_softc         *sc;
1212 {
1213         struct wb_chain         *cur_tx;
1214         struct ifnet            *ifp;
1215
1216         ifp = &sc->arpcom.ac_if;
1217
1218         /* Clear the timeout timer. */
1219         ifp->if_timer = 0;
1220
1221         if (sc->wb_cdata.wb_tx_head == NULL)
1222                 return;
1223
1224         /*
1225          * Go through our tx list and free mbufs for those
1226          * frames that have been transmitted.
1227          */
1228         while(sc->wb_cdata.wb_tx_head->wb_mbuf != NULL) {
1229                 u_int32_t               txstat;
1230
1231                 cur_tx = sc->wb_cdata.wb_tx_head;
1232                 txstat = WB_TXSTATUS(cur_tx);
1233
1234                 if ((txstat & WB_TXSTAT_OWN) || txstat == WB_UNSENT)
1235                         break;
1236
1237                 if (txstat & WB_TXSTAT_TXERR) {
1238                         ifp->if_oerrors++;
1239                         if (txstat & WB_TXSTAT_ABORT)
1240                                 ifp->if_collisions++;
1241                         if (txstat & WB_TXSTAT_LATECOLL)
1242                                 ifp->if_collisions++;
1243                 }
1244
1245                 ifp->if_collisions += (txstat & WB_TXSTAT_COLLCNT) >> 3;
1246
1247                 ifp->if_opackets++;
1248                 m_freem(cur_tx->wb_mbuf);
1249                 cur_tx->wb_mbuf = NULL;
1250
1251                 if (sc->wb_cdata.wb_tx_head == sc->wb_cdata.wb_tx_tail) {
1252                         sc->wb_cdata.wb_tx_head = NULL;
1253                         sc->wb_cdata.wb_tx_tail = NULL;
1254                         break;
1255                 }
1256
1257                 sc->wb_cdata.wb_tx_head = cur_tx->wb_nextdesc;
1258         }
1259
1260         return;
1261 }
1262
1263 /*
1264  * TX 'end of channel' interrupt handler.
1265  */
1266 static void wb_txeoc(sc)
1267         struct wb_softc         *sc;
1268 {
1269         struct ifnet            *ifp;
1270
1271         ifp = &sc->arpcom.ac_if;
1272
1273         ifp->if_timer = 0;
1274
1275         if (sc->wb_cdata.wb_tx_head == NULL) {
1276                 ifp->if_flags &= ~IFF_OACTIVE;
1277                 sc->wb_cdata.wb_tx_tail = NULL;
1278         } else {
1279                 if (WB_TXOWN(sc->wb_cdata.wb_tx_head) == WB_UNSENT) {
1280                         WB_TXOWN(sc->wb_cdata.wb_tx_head) = WB_TXSTAT_OWN;
1281                         ifp->if_timer = 5;
1282                         CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1283                 }
1284         }
1285
1286         return;
1287 }
1288
1289 static void wb_intr(arg)
1290         void                    *arg;
1291 {
1292         struct wb_softc         *sc;
1293         struct ifnet            *ifp;
1294         u_int32_t               status;
1295
1296         sc = arg;
1297         ifp = &sc->arpcom.ac_if;
1298
1299         if (!(ifp->if_flags & IFF_UP))
1300                 return;
1301
1302         /* Disable interrupts. */
1303         CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1304
1305         for (;;) {
1306
1307                 status = CSR_READ_4(sc, WB_ISR);
1308                 if (status)
1309                         CSR_WRITE_4(sc, WB_ISR, status);
1310
1311                 if ((status & WB_INTRS) == 0)
1312                         break;
1313
1314                 if ((status & WB_ISR_RX_NOBUF) || (status & WB_ISR_RX_ERR)) {
1315                         ifp->if_ierrors++;
1316                         wb_reset(sc);
1317                         if (status & WB_ISR_RX_ERR)
1318                                 wb_fixmedia(sc);
1319                         wb_init(sc);
1320                         continue;
1321                 }
1322
1323                 if (status & WB_ISR_RX_OK)
1324                         wb_rxeof(sc);
1325         
1326                 if (status & WB_ISR_RX_IDLE)
1327                         wb_rxeoc(sc);
1328
1329                 if (status & WB_ISR_TX_OK)
1330                         wb_txeof(sc);
1331
1332                 if (status & WB_ISR_TX_NOBUF)
1333                         wb_txeoc(sc);
1334
1335                 if (status & WB_ISR_TX_IDLE) {
1336                         wb_txeof(sc);
1337                         if (sc->wb_cdata.wb_tx_head != NULL) {
1338                                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1339                                 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1340                         }
1341                 }
1342
1343                 if (status & WB_ISR_TX_UNDERRUN) {
1344                         ifp->if_oerrors++;
1345                         wb_txeof(sc);
1346                         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1347                         /* Jack up TX threshold */
1348                         sc->wb_txthresh += WB_TXTHRESH_CHUNK;
1349                         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1350                         WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1351                         WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1352                 }
1353
1354                 if (status & WB_ISR_BUS_ERR) {
1355                         wb_reset(sc);
1356                         wb_init(sc);
1357                 }
1358
1359         }
1360
1361         /* Re-enable interrupts. */
1362         CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1363
1364         if (!ifq_is_empty(&ifp->if_snd)) {
1365                 wb_start(ifp);
1366         }
1367
1368         return;
1369 }
1370
1371 static void wb_tick(xsc)
1372         void                    *xsc;
1373 {
1374         struct wb_softc         *sc;
1375         struct mii_data         *mii;
1376         int                     s;
1377
1378         s = splimp();
1379
1380         sc = xsc;
1381         mii = device_get_softc(sc->wb_miibus);
1382
1383         mii_tick(mii);
1384
1385         callout_reset(&sc->wb_stat_timer, hz, wb_tick, sc);
1386
1387         splx(s);
1388
1389         return;
1390 }
1391
1392 /*
1393  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1394  * pointers to the fragment pointers.
1395  */
1396 static int wb_encap(sc, c, m_head)
1397         struct wb_softc         *sc;
1398         struct wb_chain         *c;
1399         struct mbuf             *m_head;
1400 {
1401         int                     frag = 0;
1402         struct wb_desc          *f = NULL;
1403         int                     total_len;
1404         struct mbuf             *m;
1405
1406         /*
1407          * Start packing the mbufs in this chain into
1408          * the fragment pointers. Stop when we run out
1409          * of fragments or hit the end of the mbuf chain.
1410          */
1411         m = m_head;
1412         total_len = 0;
1413
1414         for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
1415                 if (m->m_len != 0) {
1416                         if (frag == WB_MAXFRAGS)
1417                                 break;
1418                         total_len += m->m_len;
1419                         f = &c->wb_ptr->wb_frag[frag];
1420                         f->wb_ctl = WB_TXCTL_TLINK | m->m_len;
1421                         if (frag == 0) {
1422                                 f->wb_ctl |= WB_TXCTL_FIRSTFRAG;
1423                                 f->wb_status = 0;
1424                         } else
1425                                 f->wb_status = WB_TXSTAT_OWN;
1426                         f->wb_next = vtophys(&c->wb_ptr->wb_frag[frag + 1]);
1427                         f->wb_data = vtophys(mtod(m, vm_offset_t));
1428                         frag++;
1429                 }
1430         }
1431
1432         /*
1433          * Handle special case: we used up all 16 fragments,
1434          * but we have more mbufs left in the chain. Copy the
1435          * data into an mbuf cluster. Note that we don't
1436          * bother clearing the values in the other fragment
1437          * pointers/counters; it wouldn't gain us anything,
1438          * and would waste cycles.
1439          */
1440         if (m != NULL) {
1441                 struct mbuf             *m_new = NULL;
1442
1443                 MGETHDR(m_new, MB_DONTWAIT, MT_DATA);
1444                 if (m_new == NULL)
1445                         return(1);
1446                 if (m_head->m_pkthdr.len > MHLEN) {
1447                         MCLGET(m_new, MB_DONTWAIT);
1448                         if (!(m_new->m_flags & M_EXT)) {
1449                                 m_freem(m_new);
1450                                 return(1);
1451                         }
1452                 }
1453                 m_copydata(m_head, 0, m_head->m_pkthdr.len,     
1454                                         mtod(m_new, caddr_t));
1455                 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
1456                 m_freem(m_head);
1457                 m_head = m_new;
1458                 f = &c->wb_ptr->wb_frag[0];
1459                 f->wb_status = 0;
1460                 f->wb_data = vtophys(mtod(m_new, caddr_t));
1461                 f->wb_ctl = total_len = m_new->m_len;
1462                 f->wb_ctl |= WB_TXCTL_TLINK|WB_TXCTL_FIRSTFRAG;
1463                 frag = 1;
1464         }
1465
1466         if (total_len < WB_MIN_FRAMELEN) {
1467                 f = &c->wb_ptr->wb_frag[frag];
1468                 f->wb_ctl = WB_MIN_FRAMELEN - total_len;
1469                 f->wb_data = vtophys(&sc->wb_cdata.wb_pad);
1470                 f->wb_ctl |= WB_TXCTL_TLINK;
1471                 f->wb_status = WB_TXSTAT_OWN;
1472                 frag++;
1473         }
1474
1475         c->wb_mbuf = m_head;
1476         c->wb_lastdesc = frag - 1;
1477         WB_TXCTL(c) |= WB_TXCTL_LASTFRAG;
1478         WB_TXNEXT(c) = vtophys(&c->wb_nextdesc->wb_ptr->wb_frag[0]);
1479
1480         return(0);
1481 }
1482
1483 /*
1484  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1485  * to the mbuf data regions directly in the transmit lists. We also save a
1486  * copy of the pointers since the transmit list fragment pointers are
1487  * physical addresses.
1488  */
1489
1490 static void wb_start(ifp)
1491         struct ifnet            *ifp;
1492 {
1493         struct wb_softc         *sc;
1494         struct mbuf             *m_head = NULL;
1495         struct wb_chain         *cur_tx = NULL, *start_tx;
1496
1497         sc = ifp->if_softc;
1498
1499         /*
1500          * Check for an available queue slot. If there are none,
1501          * punt.
1502          */
1503         if (sc->wb_cdata.wb_tx_free->wb_mbuf != NULL) {
1504                 ifp->if_flags |= IFF_OACTIVE;
1505                 return;
1506         }
1507
1508         start_tx = sc->wb_cdata.wb_tx_free;
1509
1510         while(sc->wb_cdata.wb_tx_free->wb_mbuf == NULL) {
1511                 m_head = ifq_dequeue(&ifp->if_snd);
1512                 if (m_head == NULL)
1513                         break;
1514
1515                 /* Pick a descriptor off the free list. */
1516                 cur_tx = sc->wb_cdata.wb_tx_free;
1517                 sc->wb_cdata.wb_tx_free = cur_tx->wb_nextdesc;
1518
1519                 /* Pack the data into the descriptor. */
1520                 wb_encap(sc, cur_tx, m_head);
1521
1522                 if (cur_tx != start_tx)
1523                         WB_TXOWN(cur_tx) = WB_TXSTAT_OWN;
1524
1525                 BPF_MTAP(ifp, cur_tx->wb_mbuf);
1526         }
1527
1528         /*
1529          * If there are no packets queued, bail.
1530          */
1531         if (cur_tx == NULL)
1532                 return;
1533
1534         /*
1535          * Place the request for the upload interrupt
1536          * in the last descriptor in the chain. This way, if
1537          * we're chaining several packets at once, we'll only
1538          * get an interupt once for the whole chain rather than
1539          * once for each packet.
1540          */
1541         WB_TXCTL(cur_tx) |= WB_TXCTL_FINT;
1542         cur_tx->wb_ptr->wb_frag[0].wb_ctl |= WB_TXCTL_FINT;
1543         sc->wb_cdata.wb_tx_tail = cur_tx;
1544
1545         if (sc->wb_cdata.wb_tx_head == NULL) {
1546                 sc->wb_cdata.wb_tx_head = start_tx;
1547                 WB_TXOWN(start_tx) = WB_TXSTAT_OWN;
1548                 CSR_WRITE_4(sc, WB_TXSTART, 0xFFFFFFFF);
1549         } else {
1550                 /*
1551                  * We need to distinguish between the case where
1552                  * the own bit is clear because the chip cleared it
1553                  * and where the own bit is clear because we haven't
1554                  * set it yet. The magic value WB_UNSET is just some
1555                  * ramdomly chosen number which doesn't have the own
1556                  * bit set. When we actually transmit the frame, the
1557                  * status word will have _only_ the own bit set, so
1558                  * the txeoc handler will be able to tell if it needs
1559                  * to initiate another transmission to flush out pending
1560                  * frames.
1561                  */
1562                 WB_TXOWN(start_tx) = WB_UNSENT;
1563         }
1564
1565         /*
1566          * Set a timeout in case the chip goes out to lunch.
1567          */
1568         ifp->if_timer = 5;
1569
1570         return;
1571 }
1572
1573 static void wb_init(xsc)
1574         void                    *xsc;
1575 {
1576         struct wb_softc         *sc = xsc;
1577         struct ifnet            *ifp = &sc->arpcom.ac_if;
1578         int                     s, i;
1579         struct mii_data         *mii;
1580
1581         s = splimp();
1582
1583         mii = device_get_softc(sc->wb_miibus);
1584
1585         /*
1586          * Cancel pending I/O and free all RX/TX buffers.
1587          */
1588         wb_stop(sc);
1589         wb_reset(sc);
1590
1591         sc->wb_txthresh = WB_TXTHRESH_INIT;
1592
1593         /*
1594          * Set cache alignment and burst length.
1595          */
1596 #ifdef foo
1597         CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_CONFIG);
1598         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_THRESH);
1599         WB_SETBIT(sc, WB_NETCFG, WB_TXTHRESH(sc->wb_txthresh));
1600 #endif
1601
1602         CSR_WRITE_4(sc, WB_BUSCTL, WB_BUSCTL_MUSTBEONE|WB_BUSCTL_ARBITRATION);
1603         WB_SETBIT(sc, WB_BUSCTL, WB_BURSTLEN_16LONG);
1604         switch(sc->wb_cachesize) {
1605         case 32:
1606                 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_32LONG);
1607                 break;
1608         case 16:
1609                 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_16LONG);
1610                 break;
1611         case 8:
1612                 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_8LONG);
1613                 break;
1614         case 0:
1615         default:
1616                 WB_SETBIT(sc, WB_BUSCTL, WB_CACHEALIGN_NONE);
1617                 break;
1618         }
1619
1620         /* This doesn't tend to work too well at 100Mbps. */
1621         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_EARLY_ON);
1622
1623         /* Init our MAC address */
1624         for (i = 0; i < ETHER_ADDR_LEN; i++) {
1625                 CSR_WRITE_1(sc, WB_NODE0 + i, sc->arpcom.ac_enaddr[i]);
1626         }
1627
1628         /* Init circular RX list. */
1629         if (wb_list_rx_init(sc) == ENOBUFS) {
1630                 printf("wb%d: initialization failed: no "
1631                         "memory for rx buffers\n", sc->wb_unit);
1632                 wb_stop(sc);
1633                 (void)splx(s);
1634                 return;
1635         }
1636
1637         /* Init TX descriptors. */
1638         wb_list_tx_init(sc);
1639
1640         /* If we want promiscuous mode, set the allframes bit. */
1641         if (ifp->if_flags & IFF_PROMISC) {
1642                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1643         } else {
1644                 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ALLPHYS);
1645         }
1646
1647         /*
1648          * Set capture broadcast bit to capture broadcast frames.
1649          */
1650         if (ifp->if_flags & IFF_BROADCAST) {
1651                 WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1652         } else {
1653                 WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_BROAD);
1654         }
1655
1656         /*
1657          * Program the multicast filter, if necessary.
1658          */
1659         wb_setmulti(sc);
1660
1661         /*
1662          * Load the address of the RX list.
1663          */
1664         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1665         CSR_WRITE_4(sc, WB_RXADDR, vtophys(&sc->wb_ldata->wb_rx_list[0]));
1666
1667         /*
1668          * Enable interrupts.
1669          */
1670         CSR_WRITE_4(sc, WB_IMR, WB_INTRS);
1671         CSR_WRITE_4(sc, WB_ISR, 0xFFFFFFFF);
1672
1673         /* Enable receiver and transmitter. */
1674         WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_RX_ON);
1675         CSR_WRITE_4(sc, WB_RXSTART, 0xFFFFFFFF);
1676
1677         WB_CLRBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1678         CSR_WRITE_4(sc, WB_TXADDR, vtophys(&sc->wb_ldata->wb_tx_list[0]));
1679         WB_SETBIT(sc, WB_NETCFG, WB_NETCFG_TX_ON);
1680
1681         mii_mediachg(mii);
1682
1683         ifp->if_flags |= IFF_RUNNING;
1684         ifp->if_flags &= ~IFF_OACTIVE;
1685
1686         (void)splx(s);
1687
1688         callout_reset(&sc->wb_stat_timer, hz, wb_tick, sc);
1689 }
1690
1691 /*
1692  * Set media options.
1693  */
1694 static int wb_ifmedia_upd(ifp)
1695         struct ifnet            *ifp;
1696 {
1697         struct wb_softc         *sc;
1698
1699         sc = ifp->if_softc;
1700
1701         if (ifp->if_flags & IFF_UP)
1702                 wb_init(sc);
1703
1704         return(0);
1705 }
1706
1707 /*
1708  * Report current media status.
1709  */
1710 static void wb_ifmedia_sts(ifp, ifmr)
1711         struct ifnet            *ifp;
1712         struct ifmediareq       *ifmr;
1713 {
1714         struct wb_softc         *sc;
1715         struct mii_data         *mii;
1716
1717         sc = ifp->if_softc;
1718
1719         mii = device_get_softc(sc->wb_miibus);
1720
1721         mii_pollstat(mii);
1722         ifmr->ifm_active = mii->mii_media_active;
1723         ifmr->ifm_status = mii->mii_media_status;
1724
1725         return;
1726 }
1727
1728 static int wb_ioctl(ifp, command, data, cr)
1729         struct ifnet            *ifp;
1730         u_long                  command;
1731         caddr_t                 data;
1732         struct ucred            *cr;
1733 {
1734         struct wb_softc         *sc = ifp->if_softc;
1735         struct mii_data         *mii;
1736         struct ifreq            *ifr = (struct ifreq *) data;
1737         int                     s, error = 0;
1738
1739         s = splimp();
1740
1741         switch(command) {
1742         case SIOCSIFADDR:
1743         case SIOCGIFADDR:
1744         case SIOCSIFMTU:
1745                 error = ether_ioctl(ifp, command, data);
1746                 break;
1747         case SIOCSIFFLAGS:
1748                 if (ifp->if_flags & IFF_UP) {
1749                         wb_init(sc);
1750                 } else {
1751                         if (ifp->if_flags & IFF_RUNNING)
1752                                 wb_stop(sc);
1753                 }
1754                 error = 0;
1755                 break;
1756         case SIOCADDMULTI:
1757         case SIOCDELMULTI:
1758                 wb_setmulti(sc);
1759                 error = 0;
1760                 break;
1761         case SIOCGIFMEDIA:
1762         case SIOCSIFMEDIA:
1763                 mii = device_get_softc(sc->wb_miibus);
1764                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1765                 break;
1766         default:
1767                 error = EINVAL;
1768                 break;
1769         }
1770
1771         (void)splx(s);
1772
1773         return(error);
1774 }
1775
1776 static void wb_watchdog(ifp)
1777         struct ifnet            *ifp;
1778 {
1779         struct wb_softc         *sc;
1780
1781         sc = ifp->if_softc;
1782
1783         ifp->if_oerrors++;
1784         printf("wb%d: watchdog timeout\n", sc->wb_unit);
1785 #ifdef foo
1786         if (!(wb_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT))
1787                 printf("wb%d: no carrier - transceiver cable problem?\n",
1788                                                                 sc->wb_unit);
1789 #endif
1790         wb_stop(sc);
1791         wb_reset(sc);
1792         wb_init(sc);
1793
1794         if (!ifq_is_empty(&ifp->if_snd))
1795                 wb_start(ifp);
1796
1797         return;
1798 }
1799
1800 /*
1801  * Stop the adapter and free any mbufs allocated to the
1802  * RX and TX lists.
1803  */
1804 static void wb_stop(sc)
1805         struct wb_softc         *sc;
1806 {
1807         int             i;
1808         struct ifnet            *ifp;
1809
1810         ifp = &sc->arpcom.ac_if;
1811         ifp->if_timer = 0;
1812
1813         callout_stop(&sc->wb_stat_timer);
1814
1815         WB_CLRBIT(sc, WB_NETCFG, (WB_NETCFG_RX_ON|WB_NETCFG_TX_ON));
1816         CSR_WRITE_4(sc, WB_IMR, 0x00000000);
1817         CSR_WRITE_4(sc, WB_TXADDR, 0x00000000);
1818         CSR_WRITE_4(sc, WB_RXADDR, 0x00000000);
1819
1820         /*
1821          * Free data in the RX lists.
1822          */
1823         for (i = 0; i < WB_RX_LIST_CNT; i++) {
1824                 if (sc->wb_cdata.wb_rx_chain[i].wb_mbuf != NULL) {
1825                         m_freem(sc->wb_cdata.wb_rx_chain[i].wb_mbuf);
1826                         sc->wb_cdata.wb_rx_chain[i].wb_mbuf = NULL;
1827                 }
1828         }
1829         bzero((char *)&sc->wb_ldata->wb_rx_list,
1830                 sizeof(sc->wb_ldata->wb_rx_list));
1831
1832         /*
1833          * Free the TX list buffers.
1834          */
1835         for (i = 0; i < WB_TX_LIST_CNT; i++) {
1836                 if (sc->wb_cdata.wb_tx_chain[i].wb_mbuf != NULL) {
1837                         m_freem(sc->wb_cdata.wb_tx_chain[i].wb_mbuf);
1838                         sc->wb_cdata.wb_tx_chain[i].wb_mbuf = NULL;
1839                 }
1840         }
1841
1842         bzero((char *)&sc->wb_ldata->wb_tx_list,
1843                 sizeof(sc->wb_ldata->wb_tx_list));
1844
1845         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1846
1847         return;
1848 }
1849
1850 /*
1851  * Stop all chip I/O so that the kernel's probe routines don't
1852  * get confused by errant DMAs when rebooting.
1853  */
1854 static void wb_shutdown(dev)
1855         device_t                dev;
1856 {
1857         struct wb_softc         *sc;
1858
1859         sc = device_get_softc(dev);
1860         wb_stop(sc);
1861
1862         return;
1863 }