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