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