kernel tree reorganization stage 1: Major cvs repository work (not logged as
[dragonfly.git] / sys / dev / netif / vx / if_vx.c
1 /*
2  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3  * 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 Herb Peyerl.
16  * 4. The name of Herb Peyerl may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/vx/if_vx.c,v 1.25.2.6 2002/02/13 00:43:10 dillon Exp $
31  * $DragonFly: src/sys/dev/netif/vx/if_vx.c,v 1.4 2003/08/07 21:17:06 dillon Exp $
32  *
33  */
34
35 /*
36  * Created from if_ep.c driver by Fred Gray (fgray@rice.edu) to support
37  * the 3c590 family.
38  */
39
40 /*
41  *      Modified from the FreeBSD 1.1.5.1 version by:
42  *                      Andres Vega Garcia
43  *                      INRIA - Sophia Antipolis, France
44  *                      avega@sophia.inria.fr
45  */
46
47 /*
48  *  Promiscuous mode added and interrupt logic slightly changed
49  *  to reduce the number of adapter failures. Transceiver select
50  *  logic changed to use value from EEPROM. Autoconfiguration
51  *  features added.
52  *  Done by:
53  *          Serge Babkin
54  *          Chelindbank (Chelyabinsk, Russia)
55  *          babkin@hq.icb.chel.su
56  */
57
58 #include "use_vx.h"
59
60 #if NVX < 4     /* These cost 4 bytes apiece, so give us 4 */
61 #undef NVX
62 #define NVX 4
63 #endif
64
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/sockio.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/socket.h>
72
73 #include <net/if.h>
74
75 #include <net/ethernet.h>
76 #include <net/if_arp.h>
77
78 #include <net/bpf.h>
79
80 #include <machine/clock.h>
81
82 #include "if_vxreg.h"
83
84 #define ETHER_MAX_LEN   1518
85 #define ETHER_ADDR_LEN  6
86
87 struct vx_softc *vx_softc[NVX];
88
89 u_long vx_count;        /* both PCI and EISA */
90
91 static struct connector_entry {
92   int bit;
93   char *name;
94 } conn_tab[VX_CONNECTORS] = {
95 #define CONNECTOR_UTP   0
96   { 0x08, "utp"},
97 #define CONNECTOR_AUI   1
98   { 0x20, "aui"},
99 /* dummy */
100   { 0, "???"},
101 #define CONNECTOR_BNC   3
102   { 0x10, "bnc"},
103 #define CONNECTOR_TX    4
104   { 0x02, "tx"},
105 #define CONNECTOR_FX    5
106   { 0x04, "fx"},
107 #define CONNECTOR_MII   6
108   { 0x40, "mii"},
109   { 0, "???"}
110 };
111
112 /* struct vx_softc *vxalloc __P((int)); */
113 /* void *vxfree __P((struct vx_softc *)); */
114 /* int vxattach __P((struct vx_softc *)); */
115 static void vxtxstat __P((struct vx_softc *));
116 static int vxstatus __P((struct vx_softc *));
117 static void vxinit __P((void *));
118 static int vxioctl __P((struct ifnet *, u_long, caddr_t)); 
119 static void vxstart __P((struct ifnet *ifp));
120 static void vxwatchdog __P((struct ifnet *));
121 static void vxreset __P((struct vx_softc *));
122 /* void vxstop __P((struct vx_softc *)); */
123 static void vxread __P((struct vx_softc *));
124 static struct mbuf *vxget __P((struct vx_softc *, u_int));
125 static void vxmbuffill __P((void *));
126 static void vxmbufempty __P((struct vx_softc *));
127 static void vxsetfilter __P((struct vx_softc *));
128 static void vxgetlink __P((struct vx_softc *));
129 static void vxsetlink __P((struct vx_softc *));
130 /* int vxbusyeeprom __P((struct vx_softc *)); */
131
132 struct vx_softc *
133 vxalloc(unit)
134     int unit;
135 {
136     struct vx_softc *sc;
137
138     if (unit >= NVX) {
139         printf("vx%d: unit number too high.\n", unit);
140         return NULL;
141     }
142
143     if (vx_softc[unit]) {
144         printf("vx%d: already allocated.\n", unit);
145         return NULL;
146     }
147
148     sc = malloc(sizeof(struct vx_softc), M_DEVBUF, M_NOWAIT);
149     if (sc == NULL) {
150         printf("vx%d: cannot malloc.\n", unit);
151         return NULL;
152     }
153     bzero(sc, sizeof(struct vx_softc));
154     callout_handle_init(&sc->ch);
155
156     vx_softc[unit] = sc;
157     sc->unit = unit;
158     return (sc);    
159 }
160
161 void
162 vxfree(sc)
163     struct vx_softc *sc;
164 {
165     vx_softc[sc->unit] = NULL;
166     free(sc, M_DEVBUF);
167     return;
168 }
169
170 int
171 vxattach(sc)
172     struct vx_softc *sc;
173 {
174     struct ifnet *ifp = &sc->arpcom.ac_if;
175     int i;
176
177     GO_WINDOW(0);
178     outw(VX_COMMAND, GLOBAL_RESET);
179     VX_BUSY_WAIT;
180
181     vxgetlink(sc);
182
183     /*
184      * Read the station address from the eeprom
185      */
186     GO_WINDOW(0);
187     for (i = 0; i < 3; i++) {
188         int x;
189         if (vxbusyeeprom(sc))
190             return 0;
191         outw(BASE + VX_W0_EEPROM_COMMAND, EEPROM_CMD_RD
192              | (EEPROM_OEM_ADDR_0 + i));
193         if (vxbusyeeprom(sc))
194             return 0;
195         x = inw(BASE + VX_W0_EEPROM_DATA);
196         sc->arpcom.ac_enaddr[(i << 1)] = x >> 8;
197         sc->arpcom.ac_enaddr[(i << 1) + 1] = x;
198     }
199
200     printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
201
202     ifp->if_unit = sc->unit;
203     ifp->if_name = "vx";
204     ifp->if_mtu = ETHERMTU;
205     ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
206     ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
207     ifp->if_output = ether_output;
208     ifp->if_start = vxstart;
209     ifp->if_ioctl = vxioctl;
210     ifp->if_init = vxinit;
211     ifp->if_watchdog = vxwatchdog;
212     ifp->if_softc = sc;
213
214     ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
215
216     sc->tx_start_thresh = 20;   /* probably a good starting point. */
217
218     vxstop(sc);
219
220     return 1;
221 }
222
223
224
225 /*
226  * The order in here seems important. Otherwise we may not receive
227  * interrupts. ?!
228  */
229 static void
230 vxinit(xsc)
231         void *xsc;
232 {
233     struct vx_softc *sc = (struct vx_softc *) xsc;
234     struct ifnet *ifp = &sc->arpcom.ac_if;
235     int i;
236
237     VX_BUSY_WAIT;
238
239     GO_WINDOW(2);
240
241     for (i = 0; i < 6; i++) /* Reload the ether_addr. */
242         outb(BASE + VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
243
244     outw(BASE + VX_COMMAND, RX_RESET);
245     VX_BUSY_WAIT;
246     outw(BASE + VX_COMMAND, TX_RESET);
247     VX_BUSY_WAIT;
248
249     GO_WINDOW(1);       /* Window 1 is operating window */
250     for (i = 0; i < 31; i++)
251         inb(BASE + VX_W1_TX_STATUS);
252
253     outw(BASE + VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE |
254                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
255     outw(BASE + VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE |
256                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
257
258     /*
259      * Attempt to get rid of any stray interrupts that occured during
260      * configuration.  On the i386 this isn't possible because one may
261      * already be queued.  However, a single stray interrupt is
262      * unimportant.
263      */
264     outw(BASE + VX_COMMAND, ACK_INTR | 0xff);
265
266     vxsetfilter(sc);
267     vxsetlink(sc);
268
269     outw(BASE + VX_COMMAND, RX_ENABLE);
270     outw(BASE + VX_COMMAND, TX_ENABLE);
271
272     vxmbuffill((caddr_t) sc);
273
274     /* Interface is now `running', with no output active. */
275     ifp->if_flags |= IFF_RUNNING;
276     ifp->if_flags &= ~IFF_OACTIVE;
277
278     /* Attempt to start output, if any. */
279     vxstart(ifp);
280 }
281
282 static void
283 vxsetfilter(sc)
284     struct vx_softc *sc;
285 {
286     struct ifnet *ifp = &sc->arpcom.ac_if;  
287     
288     GO_WINDOW(1);           /* Window 1 is operating window */
289     outw(BASE + VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
290          FIL_MULTICAST |
291          ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
292 }               
293
294 static void            
295 vxgetlink(sc)
296     struct vx_softc *sc;
297 {
298     int n, k;
299
300     GO_WINDOW(3);
301     sc->vx_connectors = inw(BASE + VX_W3_RESET_OPT) & 0x7f;
302     for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
303       if (sc->vx_connectors & conn_tab[k].bit) {
304         if (n > 0) {
305           printf("/");
306         }
307         printf("%s", conn_tab[k].name);
308         n++;
309       }
310     }
311     if (sc->vx_connectors == 0) {
312         printf("no connectors!");
313         return;
314     }
315     GO_WINDOW(3);
316     sc->vx_connector = (inl(BASE + VX_W3_INTERNAL_CFG) 
317                         & INTERNAL_CONNECTOR_MASK) 
318                         >> INTERNAL_CONNECTOR_BITS;
319     if (sc->vx_connector & 0x10) {
320         sc->vx_connector &= 0x0f;
321         printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
322         printf(": disable 'auto select' with DOS util!");
323     } else {
324         printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
325     }
326 }
327
328 static void            
329 vxsetlink(sc)
330     struct vx_softc *sc;
331 {       
332     struct ifnet *ifp = &sc->arpcom.ac_if;  
333     int i, j, k;
334     char *reason, *warning;
335     static short prev_flags;
336     static char prev_conn = -1;
337
338     if (prev_conn == -1) {
339         prev_conn = sc->vx_connector;
340     }
341
342     /*
343      * S.B.
344      *
345      * Now behavior was slightly changed:
346      *
347      * if any of flags link[0-2] is used and its connector is
348      * physically present the following connectors are used:
349      *
350      *   link0 - AUI * highest precedence
351      *   link1 - BNC
352      *   link2 - UTP * lowest precedence
353      *
354      * If none of them is specified then
355      * connector specified in the EEPROM is used
356      * (if present on card or UTP if not).
357      */
358
359     i = sc->vx_connector;       /* default in EEPROM */
360     reason = "default";
361     warning = 0;
362
363     if (ifp->if_flags & IFF_LINK0) {
364         if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) {
365             i = CONNECTOR_AUI;
366             reason = "link0";
367         } else {
368             warning = "aui not present! (link0)";
369         }
370     } else if (ifp->if_flags & IFF_LINK1) {
371         if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) {
372             i = CONNECTOR_BNC;
373             reason = "link1";
374         } else {
375             warning = "bnc not present! (link1)";
376         }
377     } else if (ifp->if_flags & IFF_LINK2) {
378         if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) {
379             i = CONNECTOR_UTP;
380             reason = "link2";
381         } else {
382             warning = "utp not present! (link2)";
383         }
384     } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) {
385         warning = "strange connector type in EEPROM.";
386         reason = "forced";
387         i = CONNECTOR_UTP;
388     }
389
390     /* Avoid unnecessary message. */
391     k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2);
392     if ((k != 0) || (prev_conn != i)) {
393         if (warning != 0) {
394             printf("vx%d: warning: %s\n", sc->unit, warning);
395         }
396         printf("vx%d: selected %s. (%s)\n",
397                sc->unit, conn_tab[i].name, reason);
398     }
399
400     /* Set the selected connector. */
401     GO_WINDOW(3);
402     j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
403     outl(BASE + VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
404
405     /* First, disable all. */
406     outw(BASE + VX_COMMAND, STOP_TRANSCEIVER);
407     DELAY(800);
408     GO_WINDOW(4);
409     outw(BASE + VX_W4_MEDIA_TYPE, 0);
410
411     /* Second, enable the selected one. */
412     switch(i) {
413       case CONNECTOR_UTP:
414         GO_WINDOW(4);
415         outw(BASE + VX_W4_MEDIA_TYPE, ENABLE_UTP);
416         break;
417       case CONNECTOR_BNC:
418         outw(BASE + VX_COMMAND, START_TRANSCEIVER);
419         DELAY(800);
420         break;
421       case CONNECTOR_TX:
422       case CONNECTOR_FX:
423         GO_WINDOW(4);
424         outw(BASE + VX_W4_MEDIA_TYPE, LINKBEAT_ENABLE);
425         break;
426       default:  /* AUI and MII fall here */
427         break;
428     }
429     GO_WINDOW(1); 
430
431     prev_flags = ifp->if_flags;
432     prev_conn = i;
433 }
434
435 static void
436 vxstart(ifp)
437     struct ifnet *ifp;
438 {
439     struct vx_softc *sc = vx_softc[ifp->if_unit];
440     struct mbuf *m0;
441     int sh, len, pad;
442
443     /* Don't transmit if interface is busy or not running */
444     if ((sc->arpcom.ac_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
445         return;
446
447 startagain:
448     /* Sneak a peek at the next packet */
449     m0 = ifp->if_snd.ifq_head;
450     if (m0 == 0) {
451         return;
452     }
453     /* We need to use m->m_pkthdr.len, so require the header */
454      if ((m0->m_flags & M_PKTHDR) == 0)
455         panic("vxstart: no header mbuf");
456      len = m0->m_pkthdr.len;
457
458      pad = (4 - len) & 3;
459
460     /*
461      * The 3c509 automatically pads short packets to minimum ethernet length,
462      * but we drop packets that are too large. Perhaps we should truncate
463      * them instead?
464      */
465     if (len + pad > ETHER_MAX_LEN) {
466         /* packet is obviously too large: toss it */
467         ++ifp->if_oerrors;
468         IF_DEQUEUE(&ifp->if_snd, m0);
469         m_freem(m0);
470         goto readcheck;
471     }
472     VX_BUSY_WAIT;
473     if (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) {
474         outw(BASE + VX_COMMAND, SET_TX_AVAIL_THRESH | ((len + pad + 4) >> 2));
475         /* not enough room in FIFO */
476         if (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) { /* make sure */
477             ifp->if_flags |= IFF_OACTIVE;
478             ifp->if_timer = 1;
479             return;
480         }
481     }
482     outw(BASE + VX_COMMAND, SET_TX_AVAIL_THRESH | (8188 >> 2));
483     IF_DEQUEUE(&ifp->if_snd, m0);
484     if (m0 == NULL) {           /* not really needed */
485         return;
486     }
487
488     VX_BUSY_WAIT;
489     outw(BASE + VX_COMMAND, SET_TX_START_THRESH |
490         ((len / 4 + sc->tx_start_thresh) >> 2));
491
492     if (sc->arpcom.ac_if.if_bpf) {
493         bpf_mtap(&sc->arpcom.ac_if, m0);
494     }
495
496     /*
497      * Do the output at splhigh() so that an interrupt from another device
498      * won't cause a FIFO underrun.
499      */
500     sh = splhigh();
501
502     outl(BASE + VX_W1_TX_PIO_WR_1, len | TX_INDICATE);
503
504     while (m0) {
505         if (m0->m_len > 3)
506             outsl(BASE + VX_W1_TX_PIO_WR_1, mtod(m0, caddr_t), m0->m_len / 4);
507         if (m0->m_len & 3)
508             outsb(BASE + VX_W1_TX_PIO_WR_1,
509               mtod(m0, caddr_t) + (m0->m_len & ~3) , m0->m_len & 3);
510         m0 = m_free(m0);
511     }
512     while (pad--)
513         outb(BASE + VX_W1_TX_PIO_WR_1, 0);      /* Padding */
514
515     splx(sh);
516
517     ++ifp->if_opackets;
518     ifp->if_timer = 1;
519
520 readcheck:
521     if ((inw(BASE + VX_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
522         /* We received a complete packet. */
523         
524         if ((inw(BASE + VX_STATUS) & S_INTR_LATCH) == 0) {
525             /*
526              * No interrupt, read the packet and continue
527              * Is  this supposed to happen? Is my motherboard
528              * completely busted?
529              */
530             vxread(sc);
531         } else
532             /* Got an interrupt, return so that it gets serviced. */
533             return;
534     } else {
535         /* Check if we are stuck and reset [see XXX comment] */
536         if (vxstatus(sc)) {
537             if (ifp->if_flags & IFF_DEBUG)
538                printf("vx%d: adapter reset\n", ifp->if_unit);
539             vxreset(sc);
540         }
541     }
542
543     goto startagain;
544 }
545
546 /*
547  * XXX: The 3c509 card can get in a mode where both the fifo status bit
548  *      FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
549  *      We detect this situation and we reset the adapter.
550  *      It happens at times when there is a lot of broadcast traffic
551  *      on the cable (once in a blue moon).
552  */
553 static int
554 vxstatus(sc)
555     struct vx_softc *sc;
556 {
557     int fifost;
558
559     /*
560      * Check the FIFO status and act accordingly
561      */
562     GO_WINDOW(4);
563     fifost = inw(BASE + VX_W4_FIFO_DIAG);
564     GO_WINDOW(1);
565
566     if (fifost & FIFOS_RX_UNDERRUN) {
567         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
568             printf("vx%d: RX underrun\n", sc->unit);
569         vxreset(sc);
570         return 0;
571     }
572
573     if (fifost & FIFOS_RX_STATUS_OVERRUN) {
574         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
575             printf("vx%d: RX Status overrun\n", sc->unit);
576         return 1;
577     }
578
579     if (fifost & FIFOS_RX_OVERRUN) {
580         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
581             printf("vx%d: RX overrun\n", sc->unit);
582         return 1;
583     }
584
585     if (fifost & FIFOS_TX_OVERRUN) {
586         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
587             printf("vx%d: TX overrun\n", sc->unit);
588         vxreset(sc);
589         return 0;
590     }
591
592     return 0;
593 }
594
595 static void     
596 vxtxstat(sc)
597     struct vx_softc *sc;
598 {
599     int i;
600
601     /*
602     * We need to read+write TX_STATUS until we get a 0 status
603     * in order to turn off the interrupt flag.
604     */
605     while ((i = inb(BASE + VX_W1_TX_STATUS)) & TXS_COMPLETE) {
606         outb(BASE + VX_W1_TX_STATUS, 0x0);
607
608     if (i & TXS_JABBER) {
609         ++sc->arpcom.ac_if.if_oerrors;
610         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
611             printf("vx%d: jabber (%x)\n", sc->unit, i);
612         vxreset(sc);
613     } else if (i & TXS_UNDERRUN) {
614         ++sc->arpcom.ac_if.if_oerrors;
615         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
616             printf("vx%d: fifo underrun (%x) @%d\n",
617                 sc->unit, i, sc->tx_start_thresh);
618         if (sc->tx_succ_ok < 100)
619             sc->tx_start_thresh = min(ETHER_MAX_LEN, sc->tx_start_thresh + 20);
620         sc->tx_succ_ok = 0;
621         vxreset(sc);
622     } else if (i & TXS_MAX_COLLISION) {
623         ++sc->arpcom.ac_if.if_collisions;
624         outw(BASE + VX_COMMAND, TX_ENABLE);
625         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
626     } else
627         sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
628     }
629 }
630
631 void
632 vxintr(voidsc)
633     void *voidsc;
634 {
635     short status;
636     struct vx_softc *sc = voidsc;
637     struct ifnet *ifp = &sc->arpcom.ac_if;
638
639     for (;;) {
640         outw(BASE + VX_COMMAND, C_INTR_LATCH);
641
642         status = inw(BASE + VX_STATUS);
643
644         if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
645                 S_RX_COMPLETE | S_CARD_FAILURE)) == 0)
646             break;
647
648         /*
649          * Acknowledge any interrupts.  It's important that we do this
650          * first, since there would otherwise be a race condition.
651          * Due to the i386 interrupt queueing, we may get spurious
652          * interrupts occasionally.
653          */
654         outw(BASE + VX_COMMAND, ACK_INTR | status);
655
656         if (status & S_RX_COMPLETE)
657             vxread(sc);
658         if (status & S_TX_AVAIL) {
659             ifp->if_timer = 0;
660             sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
661             vxstart(&sc->arpcom.ac_if);
662         }
663         if (status & S_CARD_FAILURE) {
664             printf("vx%d: adapter failure (%x)\n", sc->unit, status);
665             ifp->if_timer = 0;
666             vxreset(sc);
667             return;
668         }
669         if (status & S_TX_COMPLETE) {
670             ifp->if_timer = 0;
671             vxtxstat(sc);
672             vxstart(ifp);
673         }
674     }
675
676     /* no more interrupts */
677     return;
678 }
679
680 static void
681 vxread(sc)
682     struct vx_softc *sc;
683 {
684     struct ifnet *ifp = &sc->arpcom.ac_if;
685     struct mbuf *m;
686     struct ether_header *eh;
687     u_int len;
688
689     len = inw(BASE + VX_W1_RX_STATUS);
690
691 again:
692
693     if (ifp->if_flags & IFF_DEBUG) {
694         int err = len & ERR_MASK;
695         char *s = NULL;
696
697         if (len & ERR_INCOMPLETE)
698             s = "incomplete packet";
699         else if (err == ERR_OVERRUN)
700             s = "packet overrun";
701         else if (err == ERR_RUNT)
702             s = "runt packet";
703         else if (err == ERR_ALIGNMENT)
704             s = "bad alignment";
705         else if (err == ERR_CRC)
706             s = "bad crc";
707         else if (err == ERR_OVERSIZE)
708             s = "oversized packet";
709         else if (err == ERR_DRIBBLE)
710             s = "dribble bits";
711
712         if (s)
713         printf("vx%d: %s\n", sc->unit, s);
714     }
715
716     if (len & ERR_INCOMPLETE)
717         return;
718
719     if (len & ERR_RX) {
720         ++ifp->if_ierrors;
721         goto abort;
722     }
723
724     len &= RX_BYTES_MASK;       /* Lower 11 bits = RX bytes. */
725
726     /* Pull packet off interface. */
727     m = vxget(sc, len);
728     if (m == 0) {
729         ifp->if_ierrors++;
730         goto abort;
731     }
732
733     ++ifp->if_ipackets;
734
735     /* We assume the header fit entirely in one mbuf. */
736     eh = mtod(m, struct ether_header *);
737
738     /*
739      * XXX: Some cards seem to be in promiscous mode all the time.
740      * we need to make sure we only get our own stuff always.
741      * bleah!
742      */
743
744     if ((eh->ether_dhost[0] & 1) == 0           /* !mcast and !bcast */
745       && bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) != 0) {
746         m_freem(m);
747         return;
748     }
749
750     m_adj(m, sizeof(struct ether_header));
751     ether_input(ifp, eh, m);
752
753     /*
754     * In periods of high traffic we can actually receive enough
755     * packets so that the fifo overrun bit will be set at this point,
756     * even though we just read a packet. In this case we
757     * are not going to receive any more interrupts. We check for
758     * this condition and read again until the fifo is not full.
759     * We could simplify this test by not using vxstatus(), but
760     * rechecking the RX_STATUS register directly. This test could
761     * result in unnecessary looping in cases where there is a new
762     * packet but the fifo is not full, but it will not fix the
763     * stuck behavior.
764     *
765     * Even with this improvement, we still get packet overrun errors
766     * which are hurting performance. Maybe when I get some more time
767     * I'll modify vxread() so that it can handle RX_EARLY interrupts.
768     */
769     if (vxstatus(sc)) {
770         len = inw(BASE + VX_W1_RX_STATUS);
771         /* Check if we are stuck and reset [see XXX comment] */
772         if (len & ERR_INCOMPLETE) {
773             if (ifp->if_flags & IFF_DEBUG)
774                 printf("vx%d: adapter reset\n", sc->unit);
775             vxreset(sc);
776             return;
777         }
778         goto again;
779     }
780
781     return;
782
783 abort:
784     outw(BASE + VX_COMMAND, RX_DISCARD_TOP_PACK);
785 }
786
787 static struct mbuf *
788 vxget(sc, totlen)
789     struct vx_softc *sc;
790     u_int totlen;
791 {
792     struct ifnet *ifp = &sc->arpcom.ac_if;
793     struct mbuf *top, **mp, *m;
794     int len;
795     int sh;
796
797     m = sc->mb[sc->next_mb];
798     sc->mb[sc->next_mb] = 0;
799     if (m == 0) {
800         MGETHDR(m, M_DONTWAIT, MT_DATA);
801         if (m == 0)
802             return 0;
803     } else {
804         /* If the queue is no longer full, refill. */
805         if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) {
806             sc->ch = timeout(vxmbuffill, sc, 1);
807             sc->buffill_pending = 1;
808         }
809         /* Convert one of our saved mbuf's. */
810         sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
811         m->m_data = m->m_pktdat;
812         m->m_flags = M_PKTHDR;
813         bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
814     }
815     m->m_pkthdr.rcvif = ifp;
816     m->m_pkthdr.len = totlen;
817     len = MHLEN;
818     top = 0;
819     mp = &top;
820
821     /*
822      * We read the packet at splhigh() so that an interrupt from another
823      * device doesn't cause the card's buffer to overflow while we're
824      * reading it.  We may still lose packets at other times.
825      */
826     sh = splhigh();
827
828     /*
829      * Since we don't set allowLargePackets bit in MacControl register,
830      * we can assume that totlen <= 1500bytes.
831      * The while loop will be performed iff we have a packet with
832      * MLEN < m_len < MINCLSIZE.
833      */
834     while (totlen > 0) {
835         if (top) {
836             m = sc->mb[sc->next_mb];
837             sc->mb[sc->next_mb] = 0;
838             if (m == 0) {
839                 MGET(m, M_DONTWAIT, MT_DATA);
840                 if (m == 0) {
841                     splx(sh);
842                     m_freem(top);
843                     return 0;
844                 }
845             } else {
846                 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
847             }
848             len = MLEN;
849         }
850         if (totlen >= MINCLSIZE) {
851             MCLGET(m, M_DONTWAIT);
852             if (m->m_flags & M_EXT)
853                 len = MCLBYTES;
854         }
855         len = min(totlen, len);
856         if (len > 3)
857             insl(BASE + VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4);
858         if (len & 3) {
859             insb(BASE + VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3),
860                 len & 3);
861         }
862         m->m_len = len;
863         totlen -= len;
864         *mp = m;
865         mp = &m->m_next;
866     }
867
868     outw(BASE +VX_COMMAND, RX_DISCARD_TOP_PACK);
869
870     splx(sh);
871
872     return top;
873 }
874
875
876 static int
877 vxioctl(ifp, cmd, data)
878     struct ifnet *ifp;
879     u_long cmd;
880     caddr_t data;
881 {
882     struct vx_softc *sc = vx_softc[ifp->if_unit];
883     struct ifreq *ifr = (struct ifreq *) data;
884     int s, error = 0;
885
886     s = splimp();
887
888     switch (cmd) {
889     case SIOCSIFADDR:
890     case SIOCGIFADDR:
891         ether_ioctl(ifp, cmd, data);
892         break;
893
894     case SIOCSIFFLAGS:
895         if ((ifp->if_flags & IFF_UP) == 0 &&
896             (ifp->if_flags & IFF_RUNNING) != 0) {
897             /*
898              * If interface is marked up and it is stopped, then
899              * start it.
900              */
901             vxstop(sc);
902             ifp->if_flags &= ~IFF_RUNNING;
903         } else if ((ifp->if_flags & IFF_UP) != 0 &&
904                    (ifp->if_flags & IFF_RUNNING) == 0) {
905             /*
906              * If interface is marked up and it is stopped, then
907              * start it.
908              */
909             vxinit(sc);
910         } else {
911             /*
912              * deal with flags changes:
913              * IFF_MULTICAST, IFF_PROMISC,
914              * IFF_LINK0, IFF_LINK1,
915              */
916             vxsetfilter(sc);
917             vxsetlink(sc);
918         }
919         break;
920
921     case SIOCSIFMTU:
922         /*
923          * Set the interface MTU.
924          */
925         if (ifr->ifr_mtu > ETHERMTU) {
926             error = EINVAL;
927         } else {
928             ifp->if_mtu = ifr->ifr_mtu;
929         }
930         break;
931
932     case SIOCADDMULTI:
933     case SIOCDELMULTI:
934         /*
935          * Multicast list has changed; set the hardware filter
936          * accordingly.
937          */
938         vxreset(sc);
939         error = 0;
940         break;
941
942
943     default:
944         error = EINVAL;
945     }
946
947     splx(s);
948
949     return (error);
950 }
951
952 static void
953 vxreset(sc)
954     struct vx_softc *sc;
955 {
956     int s;
957     s = splimp();
958
959     vxstop(sc);
960     vxinit(sc);
961     splx(s);
962 }
963
964 static void
965 vxwatchdog(ifp)
966     struct ifnet *ifp;
967 {
968     struct vx_softc *sc = vx_softc[ifp->if_unit];
969
970     if (ifp->if_flags & IFF_DEBUG)
971         printf("vx%d: device timeout\n", ifp->if_unit);
972     ifp->if_flags &= ~IFF_OACTIVE;
973     vxstart(ifp);
974     vxintr(sc);
975 }
976
977 void
978 vxstop(sc)
979     struct vx_softc *sc;
980 {
981     struct ifnet *ifp = &sc->arpcom.ac_if;
982
983     ifp->if_timer = 0;
984
985     outw(BASE + VX_COMMAND, RX_DISABLE);
986     outw(BASE + VX_COMMAND, RX_DISCARD_TOP_PACK);
987     VX_BUSY_WAIT;
988     outw(BASE + VX_COMMAND, TX_DISABLE);
989     outw(BASE + VX_COMMAND, STOP_TRANSCEIVER);
990     DELAY(800);
991     outw(BASE + VX_COMMAND, RX_RESET);
992     VX_BUSY_WAIT;
993     outw(BASE + VX_COMMAND, TX_RESET);
994     VX_BUSY_WAIT;
995     outw(BASE + VX_COMMAND, C_INTR_LATCH);
996     outw(BASE + VX_COMMAND, SET_RD_0_MASK);
997     outw(BASE + VX_COMMAND, SET_INTR_MASK);
998     outw(BASE + VX_COMMAND, SET_RX_FILTER);
999
1000     vxmbufempty(sc);
1001 }
1002
1003 int
1004 vxbusyeeprom(sc)
1005     struct vx_softc *sc;
1006 {
1007     int j, i = 100;
1008
1009     while (i--) {
1010         j = inw(BASE + VX_W0_EEPROM_COMMAND);
1011         if (j & EEPROM_BUSY)
1012             DELAY(100);
1013         else
1014             break;
1015     }
1016     if (!i) {
1017         printf("vx%d: eeprom failed to come ready\n", sc->unit);
1018         return (1);
1019     }
1020     return (0);
1021 }
1022
1023 static void
1024 vxmbuffill(sp)
1025     void *sp;
1026 {
1027     struct vx_softc *sc = (struct vx_softc *) sp;
1028     int s, i;
1029
1030     s = splimp();
1031     i = sc->last_mb;
1032     do {
1033         if (sc->mb[i] == NULL)
1034             MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
1035         if (sc->mb[i] == NULL)
1036             break;
1037         i = (i + 1) % MAX_MBS;
1038     } while (i != sc->next_mb);
1039     sc->last_mb = i;
1040     /* If the queue was not filled, try again. */
1041     if (sc->last_mb != sc->next_mb) {
1042         sc->ch = timeout(vxmbuffill, sc, 1);
1043         sc->buffill_pending = 1;
1044     } else {
1045         sc->buffill_pending = 0;
1046     }
1047     splx(s);
1048 }
1049
1050 static void
1051 vxmbufempty(sc)
1052     struct vx_softc *sc;
1053 {
1054     int s, i;
1055
1056     s = splimp();
1057     for (i = 0; i < MAX_MBS; i++) {
1058         if (sc->mb[i]) {
1059             m_freem(sc->mb[i]);
1060             sc->mb[i] = NULL;
1061         }
1062     }
1063     sc->last_mb = sc->next_mb = 0;
1064     if (sc->buffill_pending != 0)
1065         untimeout(vxmbuffill, sc, sc->ch);
1066     splx(s);
1067 }