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