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