Merge from vendor branch OPENSSH:
[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.13 2004/07/23 07:16:29 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_start = vxstart;
161     ifp->if_ioctl = vxioctl;
162     ifp->if_init = vxinit;
163     ifp->if_watchdog = vxwatchdog;
164     ifp->if_softc = sc;
165
166     ether_ifattach(ifp, sc->arpcom.ac_enaddr);
167
168     sc->tx_start_thresh = 20;   /* probably a good starting point. */
169
170     vxstop(sc);
171
172     return 1;
173 }
174
175
176
177 /*
178  * The order in here seems important. Otherwise we may not receive
179  * interrupts. ?!
180  */
181 static void
182 vxinit(xsc)
183         void *xsc;
184 {
185     struct vx_softc *sc = (struct vx_softc *) xsc;
186     struct ifnet *ifp = &sc->arpcom.ac_if;
187     int i;
188
189     VX_BUSY_WAIT;
190
191     GO_WINDOW(2);
192
193     for (i = 0; i < 6; i++) /* Reload the ether_addr. */
194         CSR_WRITE_1(sc, VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
195
196     CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
197     VX_BUSY_WAIT;
198     CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
199     VX_BUSY_WAIT;
200
201     GO_WINDOW(1);       /* Window 1 is operating window */
202     for (i = 0; i < 31; i++)
203         CSR_READ_1(sc, VX_W1_TX_STATUS);
204
205     CSR_WRITE_2(sc, VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE |
206                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
207     CSR_WRITE_2(sc, VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE |
208                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
209
210     /*
211      * Attempt to get rid of any stray interrupts that occured during
212      * configuration.  On the i386 this isn't possible because one may
213      * already be queued.  However, a single stray interrupt is
214      * unimportant.
215      */
216     CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | 0xff);
217
218     vxsetfilter(sc);
219     vxsetlink(sc);
220
221     CSR_WRITE_2(sc, VX_COMMAND, RX_ENABLE);
222     CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE);
223
224     vxmbuffill((caddr_t) sc);
225
226     /* Interface is now `running', with no output active. */
227     ifp->if_flags |= IFF_RUNNING;
228     ifp->if_flags &= ~IFF_OACTIVE;
229
230     /* Attempt to start output, if any. */
231     vxstart(ifp);
232 }
233
234 static void
235 vxsetfilter(sc)
236     struct vx_softc *sc;
237 {
238     struct ifnet *ifp = &sc->arpcom.ac_if;  
239     
240     GO_WINDOW(1);           /* Window 1 is operating window */
241     CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
242          FIL_MULTICAST |
243          ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
244 }               
245
246 static void            
247 vxgetlink(sc)
248     struct vx_softc *sc;
249 {
250     int n, k;
251
252     GO_WINDOW(3);
253     sc->vx_connectors = CSR_READ_2(sc, VX_W3_RESET_OPT) & 0x7f;
254     for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
255       if (sc->vx_connectors & conn_tab[k].bit) {
256         if (n > 0) {
257           printf("/");
258         }
259         printf("%s", conn_tab[k].name);
260         n++;
261       }
262     }
263     if (sc->vx_connectors == 0) {
264         printf("no connectors!");
265         return;
266     }
267     GO_WINDOW(3);
268     sc->vx_connector = (CSR_READ_4(sc, VX_W3_INTERNAL_CFG) 
269                         & INTERNAL_CONNECTOR_MASK) 
270                         >> INTERNAL_CONNECTOR_BITS;
271     if (sc->vx_connector & 0x10) {
272         sc->vx_connector &= 0x0f;
273         printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
274         printf(": disable 'auto select' with DOS util!");
275     } else {
276         printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
277     }
278 }
279
280 static void            
281 vxsetlink(sc)
282     struct vx_softc *sc;
283 {       
284     struct ifnet *ifp = &sc->arpcom.ac_if;  
285     int i, j, k;
286     char *reason, *warning;
287     static short prev_flags;
288     static char prev_conn = -1;
289
290     if (prev_conn == -1) {
291         prev_conn = sc->vx_connector;
292     }
293
294     /*
295      * S.B.
296      *
297      * Now behavior was slightly changed:
298      *
299      * if any of flags link[0-2] is used and its connector is
300      * physically present the following connectors are used:
301      *
302      *   link0 - AUI * highest precedence
303      *   link1 - BNC
304      *   link2 - UTP * lowest precedence
305      *
306      * If none of them is specified then
307      * connector specified in the EEPROM is used
308      * (if present on card or UTP if not).
309      */
310
311     i = sc->vx_connector;       /* default in EEPROM */
312     reason = "default";
313     warning = 0;
314
315     if (ifp->if_flags & IFF_LINK0) {
316         if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) {
317             i = CONNECTOR_AUI;
318             reason = "link0";
319         } else {
320             warning = "aui not present! (link0)";
321         }
322     } else if (ifp->if_flags & IFF_LINK1) {
323         if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) {
324             i = CONNECTOR_BNC;
325             reason = "link1";
326         } else {
327             warning = "bnc not present! (link1)";
328         }
329     } else if (ifp->if_flags & IFF_LINK2) {
330         if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) {
331             i = CONNECTOR_UTP;
332             reason = "link2";
333         } else {
334             warning = "utp not present! (link2)";
335         }
336     } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) {
337         warning = "strange connector type in EEPROM.";
338         reason = "forced";
339         i = CONNECTOR_UTP;
340     }
341
342     /* Avoid unnecessary message. */
343     k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2);
344     if ((k != 0) || (prev_conn != i)) {
345         if (warning != 0) {
346             printf("vx%d: warning: %s\n", sc->unit, warning);
347         }
348         printf("vx%d: selected %s. (%s)\n",
349                sc->unit, conn_tab[i].name, reason);
350     }
351
352     /* Set the selected connector. */
353     GO_WINDOW(3);
354     j = CSR_READ_4(sc, VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
355     CSR_WRITE_4(sc, VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
356
357     /* First, disable all. */
358     CSR_WRITE_2(sc,VX_COMMAND, STOP_TRANSCEIVER);
359     DELAY(800);
360     GO_WINDOW(4);
361     CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, 0);
362
363     /* Second, enable the selected one. */
364     switch(i) {
365       case CONNECTOR_UTP:
366         GO_WINDOW(4);
367         CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, ENABLE_UTP);
368         break;
369       case CONNECTOR_BNC:
370         CSR_WRITE_2(sc, VX_COMMAND, START_TRANSCEIVER);
371         DELAY(800);
372         break;
373       case CONNECTOR_TX:
374       case CONNECTOR_FX:
375         GO_WINDOW(4);
376         CSR_WRITE_2(sc, VX_W4_MEDIA_TYPE, LINKBEAT_ENABLE);
377         break;
378       default:  /* AUI and MII fall here */
379         break;
380     }
381     GO_WINDOW(1); 
382
383     prev_flags = ifp->if_flags;
384     prev_conn = i;
385 }
386
387 static void
388 vxstart(ifp)
389     struct ifnet *ifp;
390 {
391     struct vx_softc *sc = ifp->if_softc;
392     struct mbuf *m0;
393     int sh, len, pad;
394
395     /* Don't transmit if interface is busy or not running */
396     if ((sc->arpcom.ac_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
397         return;
398
399 startagain:
400     /* Sneak a peek at the next packet */
401     m0 = ifp->if_snd.ifq_head;
402     if (m0 == 0) {
403         return;
404     }
405     /* We need to use m->m_pkthdr.len, so require the header */
406      if ((m0->m_flags & M_PKTHDR) == 0)
407         panic("vxstart: no header mbuf");
408      len = m0->m_pkthdr.len;
409
410      pad = (4 - len) & 3;
411
412     /*
413      * The 3c509 automatically pads short packets to minimum ethernet length,
414      * but we drop packets that are too large. Perhaps we should truncate
415      * them instead?
416      */
417     if (len + pad > ETHER_MAX_LEN) {
418         /* packet is obviously too large: toss it */
419         ++ifp->if_oerrors;
420         IF_DEQUEUE(&ifp->if_snd, m0);
421         m_freem(m0);
422         goto readcheck;
423     }
424     VX_BUSY_WAIT;
425     if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) {
426         CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | ((len + pad + 4) >> 2));
427         /* not enough room in FIFO */
428         if (CSR_READ_2(sc, VX_W1_FREE_TX) < len + pad + 4) { /* make sure */
429             ifp->if_flags |= IFF_OACTIVE;
430             ifp->if_timer = 1;
431             return;
432         }
433     }
434     CSR_WRITE_2(sc, VX_COMMAND, SET_TX_AVAIL_THRESH | (8188 >> 2));
435     IF_DEQUEUE(&ifp->if_snd, m0);
436     if (m0 == NULL) {           /* not really needed */
437         return;
438     }
439
440     VX_BUSY_WAIT;
441     CSR_WRITE_2(sc, VX_COMMAND, SET_TX_START_THRESH |
442         ((len / 4 + sc->tx_start_thresh) >> 2));
443
444     if (sc->arpcom.ac_if.if_bpf) {
445         bpf_mtap(&sc->arpcom.ac_if, m0);
446     }
447
448     /*
449      * Do the output at splhigh() so that an interrupt from another device
450      * won't cause a FIFO underrun.
451      */
452     sh = splhigh();
453
454     CSR_WRITE_4(sc, VX_W1_TX_PIO_WR_1, len | TX_INDICATE);
455
456     while (m0) {
457         if (m0->m_len > 3)
458             bus_space_write_multi_4(sc->vx_btag, sc->vx_bhandle,
459                 VX_W1_TX_PIO_WR_1,
460                 (u_int32_t *)mtod(m0, caddr_t), m0->m_len / 4);
461         if (m0->m_len & 3)
462             bus_space_write_multi_1(sc->vx_btag, sc->vx_bhandle,
463                 VX_W1_TX_PIO_WR_1,
464                 mtod(m0, caddr_t) + (m0->m_len & ~3), m0->m_len & 3);
465         m0 = m_free(m0);
466     }
467     while (pad--)
468         CSR_WRITE_1(sc, VX_W1_TX_PIO_WR_1, 0);  /* Padding */
469
470     splx(sh);
471
472     ++ifp->if_opackets;
473     ifp->if_timer = 1;
474
475 readcheck:
476     if ((CSR_READ_2(sc, VX_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
477         /* We received a complete packet. */
478         
479         if ((CSR_READ_2(sc, VX_STATUS) & S_INTR_LATCH) == 0) {
480             /*
481              * No interrupt, read the packet and continue
482              * Is  this supposed to happen? Is my motherboard
483              * completely busted?
484              */
485             vxread(sc);
486         } else
487             /* Got an interrupt, return so that it gets serviced. */
488             return;
489     } else {
490         /* Check if we are stuck and reset [see XXX comment] */
491         if (vxstatus(sc)) {
492             if (ifp->if_flags & IFF_DEBUG)
493                printf("%s: adapter reset\n", ifp->if_xname);
494             vxreset(sc);
495         }
496     }
497
498     goto startagain;
499 }
500
501 /*
502  * XXX: The 3c509 card can get in a mode where both the fifo status bit
503  *      FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
504  *      We detect this situation and we reset the adapter.
505  *      It happens at times when there is a lot of broadcast traffic
506  *      on the cable (once in a blue moon).
507  */
508 static int
509 vxstatus(sc)
510     struct vx_softc *sc;
511 {
512     int fifost;
513
514     /*
515      * Check the FIFO status and act accordingly
516      */
517     GO_WINDOW(4);
518     fifost = CSR_READ_2(sc, VX_W4_FIFO_DIAG);
519     GO_WINDOW(1);
520
521     if (fifost & FIFOS_RX_UNDERRUN) {
522         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
523             printf("vx%d: RX underrun\n", sc->unit);
524         vxreset(sc);
525         return 0;
526     }
527
528     if (fifost & FIFOS_RX_STATUS_OVERRUN) {
529         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
530             printf("vx%d: RX Status overrun\n", sc->unit);
531         return 1;
532     }
533
534     if (fifost & FIFOS_RX_OVERRUN) {
535         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
536             printf("vx%d: RX overrun\n", sc->unit);
537         return 1;
538     }
539
540     if (fifost & FIFOS_TX_OVERRUN) {
541         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
542             printf("vx%d: TX overrun\n", sc->unit);
543         vxreset(sc);
544         return 0;
545     }
546
547     return 0;
548 }
549
550 static void     
551 vxtxstat(sc)
552     struct vx_softc *sc;
553 {
554     int i;
555
556     /*
557     * We need to read+write TX_STATUS until we get a 0 status
558     * in order to turn off the interrupt flag.
559     */
560     while ((i = CSR_READ_1(sc, VX_W1_TX_STATUS)) & TXS_COMPLETE) {
561         CSR_WRITE_1(sc, VX_W1_TX_STATUS, 0x0);
562
563     if (i & TXS_JABBER) {
564         ++sc->arpcom.ac_if.if_oerrors;
565         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
566             printf("vx%d: jabber (%x)\n", sc->unit, i);
567         vxreset(sc);
568     } else if (i & TXS_UNDERRUN) {
569         ++sc->arpcom.ac_if.if_oerrors;
570         if (sc->arpcom.ac_if.if_flags & IFF_DEBUG)
571             printf("vx%d: fifo underrun (%x) @%d\n",
572                 sc->unit, i, sc->tx_start_thresh);
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             printf("vx%d: adapter failure (%x)\n", sc->unit, 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         printf("vx%d: %s\n", sc->unit, 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                 printf("vx%d: adapter reset\n", sc->unit);
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     int sh;
750
751     m = sc->mb[sc->next_mb];
752     sc->mb[sc->next_mb] = 0;
753     if (m == 0) {
754         MGETHDR(m, MB_DONTWAIT, MT_DATA);
755         if (m == 0)
756             return 0;
757     } else {
758         /* If the queue is no longer full, refill. */
759         if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) {
760             sc->ch = timeout(vxmbuffill, sc, 1);
761             sc->buffill_pending = 1;
762         }
763         /* Convert one of our saved mbuf's. */
764         sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
765         m->m_data = m->m_pktdat;
766         m->m_flags = M_PKTHDR;
767         bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
768     }
769     m->m_pkthdr.rcvif = ifp;
770     m->m_pkthdr.len = totlen;
771     len = MHLEN;
772     top = 0;
773     mp = &top;
774
775     /*
776      * We read the packet at splhigh() so that an interrupt from another
777      * device doesn't cause the card's buffer to overflow while we're
778      * reading it.  We may still lose packets at other times.
779      */
780     sh = splhigh();
781
782     /*
783      * Since we don't set allowLargePackets bit in MacControl register,
784      * we can assume that totlen <= 1500bytes.
785      * The while loop will be performed iff we have a packet with
786      * MLEN < m_len < MINCLSIZE.
787      */
788     while (totlen > 0) {
789         if (top) {
790             m = sc->mb[sc->next_mb];
791             sc->mb[sc->next_mb] = 0;
792             if (m == 0) {
793                 MGET(m, MB_DONTWAIT, MT_DATA);
794                 if (m == 0) {
795                     splx(sh);
796                     m_freem(top);
797                     return 0;
798                 }
799             } else {
800                 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
801             }
802             len = MLEN;
803         }
804         if (totlen >= MINCLSIZE) {
805             MCLGET(m, MB_DONTWAIT);
806             if (m->m_flags & M_EXT)
807                 len = MCLBYTES;
808         }
809         len = min(totlen, len);
810         if (len > 3)
811             bus_space_read_multi_4(sc->vx_btag, sc->vx_bhandle,
812                 VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4);
813         if (len & 3) {
814             bus_space_read_multi_1(sc->vx_btag, sc->vx_bhandle,
815                 VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3),
816                 len & 3);
817         }
818         m->m_len = len;
819         totlen -= len;
820         *mp = m;
821         mp = &m->m_next;
822     }
823
824     CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
825
826     splx(sh);
827
828     return top;
829 }
830
831
832 static int
833 vxioctl(ifp, cmd, data, cr)
834     struct ifnet *ifp;
835     u_long cmd;
836     caddr_t data;
837     struct ucred *cr;
838 {
839     struct vx_softc *sc = ifp->if_softc;
840     struct ifreq *ifr = (struct ifreq *) data;
841     int s, error = 0;
842
843     s = splimp();
844
845     switch (cmd) {
846     case SIOCSIFADDR:
847     case SIOCGIFADDR:
848         ether_ioctl(ifp, cmd, data);
849         break;
850
851     case SIOCSIFFLAGS:
852         if ((ifp->if_flags & IFF_UP) == 0 &&
853             (ifp->if_flags & IFF_RUNNING) != 0) {
854             /*
855              * If interface is marked up and it is stopped, then
856              * start it.
857              */
858             vxstop(sc);
859             ifp->if_flags &= ~IFF_RUNNING;
860         } else if ((ifp->if_flags & IFF_UP) != 0 &&
861                    (ifp->if_flags & IFF_RUNNING) == 0) {
862             /*
863              * If interface is marked up and it is stopped, then
864              * start it.
865              */
866             vxinit(sc);
867         } else {
868             /*
869              * deal with flags changes:
870              * IFF_MULTICAST, IFF_PROMISC,
871              * IFF_LINK0, IFF_LINK1,
872              */
873             vxsetfilter(sc);
874             vxsetlink(sc);
875         }
876         break;
877
878     case SIOCSIFMTU:
879         /*
880          * Set the interface MTU.
881          */
882         if (ifr->ifr_mtu > ETHERMTU) {
883             error = EINVAL;
884         } else {
885             ifp->if_mtu = ifr->ifr_mtu;
886         }
887         break;
888
889     case SIOCADDMULTI:
890     case SIOCDELMULTI:
891         /*
892          * Multicast list has changed; set the hardware filter
893          * accordingly.
894          */
895         vxreset(sc);
896         error = 0;
897         break;
898
899
900     default:
901         error = EINVAL;
902     }
903
904     splx(s);
905
906     return (error);
907 }
908
909 static void
910 vxreset(sc)
911     struct vx_softc *sc;
912 {
913     int s;
914     s = splimp();
915
916     vxstop(sc);
917     vxinit(sc);
918     splx(s);
919 }
920
921 static void
922 vxwatchdog(ifp)
923     struct ifnet *ifp;
924 {
925     struct vx_softc *sc = ifp->if_softc;
926
927     if (ifp->if_flags & IFF_DEBUG)
928         printf("%s: device timeout\n", ifp->if_xname);
929     ifp->if_flags &= ~IFF_OACTIVE;
930     vxstart(ifp);
931     vxintr(sc);
932 }
933
934 void
935 vxstop(sc)
936     struct vx_softc *sc;
937 {
938     struct ifnet *ifp = &sc->arpcom.ac_if;
939
940     ifp->if_timer = 0;
941
942     CSR_WRITE_2(sc, VX_COMMAND, RX_DISABLE);
943     CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
944     VX_BUSY_WAIT;
945     CSR_WRITE_2(sc, VX_COMMAND, TX_DISABLE);
946     CSR_WRITE_2(sc, VX_COMMAND, STOP_TRANSCEIVER);
947     DELAY(800);
948     CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
949     VX_BUSY_WAIT;
950     CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
951     VX_BUSY_WAIT;
952     CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH);
953     CSR_WRITE_2(sc, VX_COMMAND, SET_RD_0_MASK);
954     CSR_WRITE_2(sc, VX_COMMAND, SET_INTR_MASK);
955     CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER);
956
957     vxmbufempty(sc);
958 }
959
960 int
961 vxbusyeeprom(sc)
962     struct vx_softc *sc;
963 {
964     int j, i = 100;
965
966     while (i--) {
967         j = CSR_READ_2(sc, VX_W0_EEPROM_COMMAND);
968         if (j & EEPROM_BUSY)
969             DELAY(100);
970         else
971             break;
972     }
973     if (!i) {
974         printf("vx%d: eeprom failed to come ready\n", sc->unit);
975         return (1);
976     }
977     return (0);
978 }
979
980 static void
981 vxmbuffill(sp)
982     void *sp;
983 {
984     struct vx_softc *sc = (struct vx_softc *) sp;
985     int s, i;
986
987     s = splimp();
988     i = sc->last_mb;
989     do {
990         if (sc->mb[i] == NULL)
991             MGET(sc->mb[i], MB_DONTWAIT, MT_DATA);
992         if (sc->mb[i] == NULL)
993             break;
994         i = (i + 1) % MAX_MBS;
995     } while (i != sc->next_mb);
996     sc->last_mb = i;
997     /* If the queue was not filled, try again. */
998     if (sc->last_mb != sc->next_mb) {
999         sc->ch = timeout(vxmbuffill, sc, 1);
1000         sc->buffill_pending = 1;
1001     } else {
1002         sc->buffill_pending = 0;
1003     }
1004     splx(s);
1005 }
1006
1007 static void
1008 vxmbufempty(sc)
1009     struct vx_softc *sc;
1010 {
1011     int s, i;
1012
1013     s = splimp();
1014     for (i = 0; i < MAX_MBS; i++) {
1015         if (sc->mb[i]) {
1016             m_freem(sc->mb[i]);
1017             sc->mb[i] = NULL;
1018         }
1019     }
1020     sc->last_mb = sc->next_mb = 0;
1021     if (sc->buffill_pending != 0)
1022         untimeout(vxmbuffill, sc, sc->ch);
1023     splx(s);
1024 }