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