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