Merge from vendor branch GROFF:
[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.23 2005/07/04 07:32:37 joerg Exp $
32  *
33  */
34
35 /*
36  * Created from if_ep.c driver by Fred Gray (fgray@rice.edu) to support
37  * the 3c590 family.
38  */
39
40 /*
41  *      Modified from the FreeBSD 1.1.5.1 version by:
42  *                      Andres Vega Garcia
43  *                      INRIA - Sophia Antipolis, France
44  *                      avega@sophia.inria.fr
45  */
46
47 /*
48  *  Promiscuous mode added and interrupt logic slightly changed
49  *  to reduce the number of adapter failures. Transceiver select
50  *  logic changed to use value from EEPROM. Autoconfiguration
51  *  features added.
52  *  Done by:
53  *          Serge Babkin
54  *          Chelindbank (Chelyabinsk, Russia)
55  *          babkin@hq.icb.chel.su
56  */
57
58 #include <sys/param.h>
59 #include <sys/bus.h>
60 #include <sys/systm.h>
61 #include <sys/sockio.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/socket.h>
65 #include <sys/linker_set.h>
66 #include <sys/module.h>
67 #include <sys/thread2.h>
68
69 #include <net/if.h>
70 #include <net/ifq_var.h>
71
72 #include <net/ethernet.h>
73 #include <net/if_arp.h>
74
75 #include <machine/bus_pio.h>
76 #include <machine/bus.h>
77
78 #include <net/bpf.h>
79
80 #include "if_vxreg.h"
81
82 DECLARE_DUMMY_MODULE(if_vx);
83
84 static struct connector_entry {
85   int bit;
86   const char *name;
87 } conn_tab[VX_CONNECTORS] = {
88 #define CONNECTOR_UTP   0
89   { 0x08, "utp"},
90 #define CONNECTOR_AUI   1
91   { 0x20, "aui"},
92 /* dummy */
93   { 0, "???"},
94 #define CONNECTOR_BNC   3
95   { 0x10, "bnc"},
96 #define CONNECTOR_TX    4
97   { 0x02, "tx"},
98 #define CONNECTOR_FX    5
99   { 0x04, "fx"},
100 #define CONNECTOR_MII   6
101   { 0x40, "mii"},
102   { 0, "???"}
103 };
104
105 /* int vxattach (struct vx_softc *); */
106 static void vxtxstat (struct vx_softc *);
107 static int vxstatus (struct vx_softc *);
108 static void vxinit (void *);
109 static int vxioctl (struct ifnet *, u_long, caddr_t, struct ucred *);
110 static void vxstart (struct ifnet *ifp);
111 static void vxwatchdog (struct ifnet *);
112 static void vxreset (struct vx_softc *);
113 /* void vxstop (struct vx_softc *); */
114 static void vxread (struct vx_softc *);
115 static struct mbuf *vxget (struct vx_softc *, u_int);
116 static void vxmbuffill (void *);
117 static void vxmbufempty (struct vx_softc *);
118 static void vxsetfilter (struct vx_softc *);
119 static void vxgetlink (struct vx_softc *);
120 static void vxsetlink (struct vx_softc *);
121 /* int vxbusyeeprom (struct vx_softc *); */
122
123 int
124 vxattach(device_t dev)
125 {
126     struct vx_softc *sc;
127     struct ifnet *ifp;
128     uint8_t eaddr[ETHER_ADDR_LEN];
129     int i;
130
131     sc = device_get_softc(dev);
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     ifp = &sc->arpcom.ac_if;
139     if_initname(ifp, device_get_name(dev), device_get_unit(dev));
140
141     vxgetlink(sc);
142
143     /*
144      * Read the station address from the eeprom
145      */
146     GO_WINDOW(0);
147     for (i = 0; i < 3; i++) {
148         int x;
149         if (vxbusyeeprom(sc))
150             return 0;
151         CSR_WRITE_2(sc, VX_W0_EEPROM_COMMAND, EEPROM_CMD_RD
152              | (EEPROM_OEM_ADDR_0 + i));
153         if (vxbusyeeprom(sc))
154             return 0;
155         x = CSR_READ_2(sc, VX_W0_EEPROM_DATA);
156         eaddr[(i << 1)] = x >> 8;
157         eaddr[(i << 1) + 1] = x;
158     }
159
160     ifp->if_mtu = ETHERMTU;
161     ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
162     ifq_set_ready(&ifp->if_snd);
163     ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
164     ifp->if_start = vxstart;
165     ifp->if_ioctl = vxioctl;
166     ifp->if_init = vxinit;
167     ifp->if_watchdog = vxwatchdog;
168     ifp->if_softc = sc;
169
170     ether_ifattach(ifp, eaddr);
171
172     sc->tx_start_thresh = 20;   /* probably a good starting point. */
173
174     vxstop(sc);
175
176     return 1;
177 }
178
179
180
181 /*
182  * The order in here seems important. Otherwise we may not receive
183  * interrupts. ?!
184  */
185 static void
186 vxinit(xsc)
187         void *xsc;
188 {
189     struct vx_softc *sc = (struct vx_softc *) xsc;
190     struct ifnet *ifp = &sc->arpcom.ac_if;
191     int i;
192
193     VX_BUSY_WAIT;
194
195     GO_WINDOW(2);
196
197     for (i = 0; i < 6; i++) /* Reload the ether_addr. */
198         CSR_WRITE_1(sc, VX_W2_ADDR_0 + i, sc->arpcom.ac_enaddr[i]);
199
200     CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
201     VX_BUSY_WAIT;
202     CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
203     VX_BUSY_WAIT;
204
205     GO_WINDOW(1);       /* Window 1 is operating window */
206     for (i = 0; i < 31; i++)
207         CSR_READ_1(sc, VX_W1_TX_STATUS);
208
209     CSR_WRITE_2(sc, VX_COMMAND,SET_RD_0_MASK | S_CARD_FAILURE |
210                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
211     CSR_WRITE_2(sc, VX_COMMAND,SET_INTR_MASK | S_CARD_FAILURE |
212                         S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
213
214     /*
215      * Attempt to get rid of any stray interrupts that occured during
216      * configuration.  On the i386 this isn't possible because one may
217      * already be queued.  However, a single stray interrupt is
218      * unimportant.
219      */
220     CSR_WRITE_2(sc, VX_COMMAND, ACK_INTR | 0xff);
221
222     vxsetfilter(sc);
223     vxsetlink(sc);
224
225     CSR_WRITE_2(sc, VX_COMMAND, RX_ENABLE);
226     CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE);
227
228     vxmbuffill((caddr_t) sc);
229
230     /* Interface is now `running', with no output active. */
231     ifp->if_flags |= IFF_RUNNING;
232     ifp->if_flags &= ~IFF_OACTIVE;
233
234     /* Attempt to start output, if any. */
235     vxstart(ifp);
236 }
237
238 static void
239 vxsetfilter(sc)
240     struct vx_softc *sc;
241 {
242     struct ifnet *ifp = &sc->arpcom.ac_if;  
243     
244     GO_WINDOW(1);           /* Window 1 is operating window */
245     CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
246          FIL_MULTICAST |
247          ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
248 }               
249
250 static void            
251 vxgetlink(sc)
252     struct vx_softc *sc;
253 {
254     int n, k;
255
256     GO_WINDOW(3);
257     sc->vx_connectors = CSR_READ_2(sc, VX_W3_RESET_OPT) & 0x7f;
258     for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
259       if (sc->vx_connectors & conn_tab[k].bit) {
260         if (n == 0)
261           if_printf(&sc->arpcom.ac_if, "%s", conn_tab[k].name);
262         else
263           printf("/%s", conn_tab[k].name);
264         n++;
265       }
266     }
267     if (n == 0) {
268         if_printf(&sc->arpcom.ac_if, "no connectors!\n");
269         return;
270     }
271     GO_WINDOW(3);
272     sc->vx_connector = (CSR_READ_4(sc, VX_W3_INTERNAL_CFG) 
273                         & INTERNAL_CONNECTOR_MASK) 
274                         >> INTERNAL_CONNECTOR_BITS;
275     if (sc->vx_connector & 0x10) {
276         sc->vx_connector &= 0x0f;
277         printf("[*%s*]", conn_tab[(int)sc->vx_connector].name);
278         printf(": disable 'auto select' with DOS util!\n");
279     } else {
280         printf("[*%s*]\n", conn_tab[(int)sc->vx_connector].name);
281     }
282 }
283
284 static void            
285 vxsetlink(sc)
286     struct vx_softc *sc;
287 {       
288     struct ifnet *ifp = &sc->arpcom.ac_if;  
289     int i, j, k;
290     const char *reason, *warning;
291     static short prev_flags;
292     static char prev_conn = -1;
293
294     if (prev_conn == -1) {
295         prev_conn = sc->vx_connector;
296     }
297
298     /*
299      * S.B.
300      *
301      * Now behavior was slightly changed:
302      *
303      * if any of flags link[0-2] is used and its connector is
304      * physically present the following connectors are used:
305      *
306      *   link0 - AUI * highest precedence
307      *   link1 - BNC
308      *   link2 - UTP * lowest precedence
309      *
310      * If none of them is specified then
311      * connector specified in the EEPROM is used
312      * (if present on card or UTP if not).
313      */
314
315     i = sc->vx_connector;       /* default in EEPROM */
316     reason = "default";
317     warning = NULL;
318
319     if (ifp->if_flags & IFF_LINK0) {
320         if (sc->vx_connectors & conn_tab[CONNECTOR_AUI].bit) {
321             i = CONNECTOR_AUI;
322             reason = "link0";
323         } else {
324             warning = "aui not present! (link0)";
325         }
326     } else if (ifp->if_flags & IFF_LINK1) {
327         if (sc->vx_connectors & conn_tab[CONNECTOR_BNC].bit) {
328             i = CONNECTOR_BNC;
329             reason = "link1";
330         } else {
331             warning = "bnc not present! (link1)";
332         }
333     } else if (ifp->if_flags & IFF_LINK2) {
334         if (sc->vx_connectors & conn_tab[CONNECTOR_UTP].bit) {
335             i = CONNECTOR_UTP;
336             reason = "link2";
337         } else {
338             warning = "utp not present! (link2)";
339         }
340     } else if ((sc->vx_connectors & conn_tab[(int)sc->vx_connector].bit) == 0) {
341         warning = "strange connector type in EEPROM.";
342         reason = "forced";
343         i = CONNECTOR_UTP;
344     }
345
346     /* Avoid unnecessary message. */
347     k = (prev_flags ^ ifp->if_flags) & (IFF_LINK0 | IFF_LINK1 | IFF_LINK2);
348     if ((k != 0) || (prev_conn != i)) {
349         if (warning != NULL) {
350             if_printf(ifp, "warning: %s\n", warning);
351         }
352         if_printf(ifp, "selected %s. (%s)\n", 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 len, pad;
397
398     /* Don't transmit if interface is busy or not running */
399     if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
400         return;
401
402 startagain:
403     /* Sneak a peek at the next packet */
404     m0 = ifq_poll(&ifp->if_snd);
405     if (m0 == NULL)
406         return;
407     /* We need to use m->m_pkthdr.len, so require the header */
408      M_ASSERTPKTHDR(m0);
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(ifp, m0);
443
444     /*
445      * Do the output in a critical section so that an interrupt from
446      * another device won't cause a FIFO underrun.
447      */
448     crit_enter();
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     crit_exit();
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             /* Got an interrupt, return so that it gets serviced. */
477             return;
478         }
479         /*
480          * No interrupt, read the packet and continue
481          * Is  this supposed to happen? Is my motherboard
482          * completely busted?
483          */
484         vxread(sc);
485     } else {
486         /* Check if we are stuck and reset [see XXX comment] */
487         if (vxstatus(sc)) {
488             if (ifp->if_flags & IFF_DEBUG)
489                if_printf(ifp, "adapter reset\n");
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     struct ifnet *ifp = &sc->arpcom.ac_if;
510
511     /*
512      * Check the FIFO status and act accordingly
513      */
514     GO_WINDOW(4);
515     fifost = CSR_READ_2(sc, VX_W4_FIFO_DIAG);
516     GO_WINDOW(1);
517
518     if (fifost & FIFOS_RX_UNDERRUN) {
519         if (ifp->if_flags & IFF_DEBUG)
520             if_printf(ifp, "RX underrun\n");
521         vxreset(sc);
522         return 0;
523     }
524
525     if (fifost & FIFOS_RX_STATUS_OVERRUN) {
526         if (ifp->if_flags & IFF_DEBUG)
527             if_printf(ifp, "RX Status overrun\n");
528         return 1;
529     }
530
531     if (fifost & FIFOS_RX_OVERRUN) {
532         if (ifp->if_flags & IFF_DEBUG)
533             if_printf(ifp, "RX overrun\n");
534         return 1;
535     }
536
537     if (fifost & FIFOS_TX_OVERRUN) {
538         if (ifp->if_flags & IFF_DEBUG)
539             if_printf(ifp, "TX overrun\n");
540         vxreset(sc);
541     }
542
543     return 0;
544 }
545
546 static void     
547 vxtxstat(struct vx_softc *sc)
548 {
549         int i;
550         struct ifnet *ifp = &sc->arpcom.ac_if;
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                         ++ifp->if_oerrors;
561                         if (ifp->if_flags & IFF_DEBUG)
562                                 if_printf(ifp, "jabber (%x)\n", i);
563                         vxreset(sc);
564                 } else if (i & TXS_UNDERRUN) {
565                         ++ifp->if_oerrors;
566                         if (ifp->if_flags & IFF_DEBUG) {
567                                 if_printf(ifp, "fifo underrun (%x) @%d\n",
568                                           i, sc->tx_start_thresh);
569                         }
570                         if (sc->tx_succ_ok < 100) {
571                                 sc->tx_start_thresh = min(
572                                     ETHER_MAX_LEN, sc->tx_start_thresh + 20);
573                         }
574                         sc->tx_succ_ok = 0;
575                         vxreset(sc);
576                 } else if (i & TXS_MAX_COLLISION) {
577                         ++ifp->if_collisions;
578                         CSR_WRITE_2(sc, VX_COMMAND, TX_ENABLE);
579                         ifp->if_flags &= ~IFF_OACTIVE;
580                 } else {
581                         sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
582                 }
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             ifp->if_flags &= ~IFF_OACTIVE;
616             vxstart(ifp);
617         }
618         if (status & S_CARD_FAILURE) {
619             if_printf(ifp, "adapter failure (%x)\n", status);
620             ifp->if_timer = 0;
621             vxreset(sc);
622             return;
623         }
624         if (status & S_TX_COMPLETE) {
625             ifp->if_timer = 0;
626             vxtxstat(sc);
627             vxstart(ifp);
628         }
629     }
630     /* no more interrupts */
631 }
632
633 static void
634 vxread(sc)
635     struct vx_softc *sc;
636 {
637     struct ifnet *ifp = &sc->arpcom.ac_if;
638     struct mbuf *m;
639     struct ether_header *eh;
640     u_int len;
641
642     len = CSR_READ_2(sc, VX_W1_RX_STATUS);
643
644 again:
645
646     if (ifp->if_flags & IFF_DEBUG) {
647         int err = len & ERR_MASK;
648         const char *s = NULL;
649
650         if (len & ERR_INCOMPLETE)
651             s = "incomplete packet";
652         else if (err == ERR_OVERRUN)
653             s = "packet overrun";
654         else if (err == ERR_RUNT)
655             s = "runt packet";
656         else if (err == ERR_ALIGNMENT)
657             s = "bad alignment";
658         else if (err == ERR_CRC)
659             s = "bad crc";
660         else if (err == ERR_OVERSIZE)
661             s = "oversized packet";
662         else if (err == ERR_DRIBBLE)
663             s = "dribble bits";
664
665         if (s)
666             if_printf(ifp, "%s\n", s);
667     }
668
669     if (len & ERR_INCOMPLETE)
670         return;
671
672     if (len & ERR_RX) {
673         ++ifp->if_ierrors;
674         goto abort;
675     }
676
677     len &= RX_BYTES_MASK;       /* Lower 11 bits = RX bytes. */
678
679     /* Pull packet off interface. */
680     m = vxget(sc, len);
681     if (m == NULL) {
682         ifp->if_ierrors++;
683         goto abort;
684     }
685
686     ++ifp->if_ipackets;
687
688     /* We assume the header fit entirely in one mbuf. */
689     eh = mtod(m, struct ether_header *);
690
691     /*
692      * XXX: Some cards seem to be in promiscous mode all the time.
693      * we need to make sure we only get our own stuff always.
694      * bleah!
695      */
696
697     if ((eh->ether_dhost[0] & 1) == 0           /* !mcast and !bcast */
698       && bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr, ETHER_ADDR_LEN) != 0) {
699         m_freem(m);
700         return;
701     }
702
703     (*ifp->if_input)(ifp, m);
704
705     /*
706     * In periods of high traffic we can actually receive enough
707     * packets so that the fifo overrun bit will be set at this point,
708     * even though we just read a packet. In this case we
709     * are not going to receive any more interrupts. We check for
710     * this condition and read again until the fifo is not full.
711     * We could simplify this test by not using vxstatus(), but
712     * rechecking the RX_STATUS register directly. This test could
713     * result in unnecessary looping in cases where there is a new
714     * packet but the fifo is not full, but it will not fix the
715     * stuck behavior.
716     *
717     * Even with this improvement, we still get packet overrun errors
718     * which are hurting performance. Maybe when I get some more time
719     * I'll modify vxread() so that it can handle RX_EARLY interrupts.
720     */
721     if (vxstatus(sc)) {
722         len = CSR_READ_2(sc, VX_W1_RX_STATUS);
723         /* Check if we are stuck and reset [see XXX comment] */
724         if (len & ERR_INCOMPLETE) {
725             if (ifp->if_flags & IFF_DEBUG)
726                 if_printf(ifp, "adapter reset\n");
727             vxreset(sc);
728             return;
729         }
730         goto again;
731     }
732
733     return;
734
735 abort:
736     CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
737 }
738
739 static struct mbuf *
740 vxget(sc, totlen)
741     struct vx_softc *sc;
742     u_int totlen;
743 {
744     struct ifnet *ifp = &sc->arpcom.ac_if;
745     struct mbuf *top, **mp, *m;
746     int len;
747
748     m = sc->mb[sc->next_mb];
749     sc->mb[sc->next_mb] = NULL;
750     if (m == NULL) {
751         MGETHDR(m, MB_DONTWAIT, MT_DATA);
752         if (m == NULL)
753             return NULL;
754     } else {
755         /* If the queue is no longer full, refill. */
756         if (sc->last_mb == sc->next_mb && sc->buffill_pending == 0) {
757             callout_reset(&sc->vx_timer, 1, vxmbuffill, sc);
758             sc->buffill_pending = 1;
759         }
760         /* Convert one of our saved mbuf's. */
761         sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
762         m->m_data = m->m_pktdat;
763         m->m_flags = M_PKTHDR;
764         bzero(&m->m_pkthdr, sizeof(m->m_pkthdr));
765     }
766     m->m_pkthdr.rcvif = ifp;
767     m->m_pkthdr.len = totlen;
768     len = MHLEN;
769     top = NULL;
770     mp = &top;
771
772     /*
773      * We read the packet in a critical section so that an interrupt
774      * from another device doesn't cause the card's buffer to overflow
775      * while we're reading it.  We may still lose packets at other times.
776      */
777     crit_enter();
778
779     /*
780      * Since we don't set allowLargePackets bit in MacControl register,
781      * we can assume that totlen <= 1500bytes.
782      * The while loop will be performed if we have a packet with
783      * MLEN < m_len < MINCLSIZE.
784      */
785     while (totlen > 0) {
786         if (top) {
787             m = sc->mb[sc->next_mb];
788             sc->mb[sc->next_mb] = NULL;
789             if (m == NULL) {
790                 MGET(m, MB_DONTWAIT, MT_DATA);
791                 if (m == NULL) {
792                     crit_exit();
793                     m_freem(top);
794                     return NULL;
795                 }
796             } else {
797                 sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
798             }
799             len = MLEN;
800         }
801         if (totlen >= MINCLSIZE) {
802             MCLGET(m, MB_DONTWAIT);
803             if (m->m_flags & M_EXT)
804                 len = MCLBYTES;
805         }
806         len = min(totlen, len);
807         if (len > 3)
808             bus_space_read_multi_4(sc->vx_btag, sc->vx_bhandle,
809                 VX_W1_RX_PIO_RD_1, mtod(m, u_int32_t *), len / 4);
810         if (len & 3) {
811             bus_space_read_multi_1(sc->vx_btag, sc->vx_bhandle,
812                 VX_W1_RX_PIO_RD_1, mtod(m, u_int8_t *) + (len & ~3),
813                 len & 3);
814         }
815         m->m_len = len;
816         totlen -= len;
817         *mp = m;
818         mp = &m->m_next;
819     }
820
821     CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
822
823     crit_exit();
824
825     return top;
826 }
827
828
829 static int
830 vxioctl(ifp, cmd, data, cr)
831     struct ifnet *ifp;
832     u_long cmd;
833     caddr_t data;
834     struct ucred *cr;
835 {
836     struct vx_softc *sc = ifp->if_softc;
837     struct ifreq *ifr = (struct ifreq *) data;
838     int error = 0;
839
840     crit_enter();
841
842     switch (cmd) {
843     case SIOCSIFFLAGS:
844         if ((ifp->if_flags & IFF_UP) == 0 &&
845             (ifp->if_flags & IFF_RUNNING) != 0) {
846             /*
847              * If interface is marked up and it is stopped, then
848              * start it.
849              */
850             vxstop(sc);
851             ifp->if_flags &= ~IFF_RUNNING;
852         } else 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             vxinit(sc);
859         } else {
860             /*
861              * deal with flags changes:
862              * IFF_MULTICAST, IFF_PROMISC,
863              * IFF_LINK0, IFF_LINK1,
864              */
865             vxsetfilter(sc);
866             vxsetlink(sc);
867         }
868         break;
869
870     case SIOCSIFMTU:
871         /*
872          * Set the interface MTU.
873          */
874         if (ifr->ifr_mtu > ETHERMTU) {
875             error = EINVAL;
876         } else {
877             ifp->if_mtu = ifr->ifr_mtu;
878         }
879         break;
880
881     case SIOCADDMULTI:
882     case SIOCDELMULTI:
883         /*
884          * Multicast list has changed; set the hardware filter
885          * accordingly.
886          */
887         vxreset(sc);
888         error = 0;
889         break;
890
891
892     default:
893         ether_ioctl(ifp, cmd, data);
894         break;
895     }
896
897     crit_exit();
898
899     return (error);
900 }
901
902 static void
903 vxreset(sc)
904     struct vx_softc *sc;
905 {
906
907     crit_enter();
908
909     vxstop(sc);
910     vxinit(sc);
911     crit_exit();
912 }
913
914 static void
915 vxwatchdog(ifp)
916     struct ifnet *ifp;
917 {
918     struct vx_softc *sc = ifp->if_softc;
919
920     if (ifp->if_flags & IFF_DEBUG)
921         if_printf(ifp, "device timeout\n");
922     ifp->if_flags &= ~IFF_OACTIVE;
923     vxstart(ifp);
924     vxintr(sc);
925 }
926
927 void
928 vxstop(sc)
929     struct vx_softc *sc;
930 {
931     struct ifnet *ifp = &sc->arpcom.ac_if;
932
933     ifp->if_timer = 0;
934
935     CSR_WRITE_2(sc, VX_COMMAND, RX_DISABLE);
936     CSR_WRITE_2(sc, VX_COMMAND, RX_DISCARD_TOP_PACK);
937     VX_BUSY_WAIT;
938     CSR_WRITE_2(sc, VX_COMMAND, TX_DISABLE);
939     CSR_WRITE_2(sc, VX_COMMAND, STOP_TRANSCEIVER);
940     DELAY(800);
941     CSR_WRITE_2(sc, VX_COMMAND, RX_RESET);
942     VX_BUSY_WAIT;
943     CSR_WRITE_2(sc, VX_COMMAND, TX_RESET);
944     VX_BUSY_WAIT;
945     CSR_WRITE_2(sc, VX_COMMAND, C_INTR_LATCH);
946     CSR_WRITE_2(sc, VX_COMMAND, SET_RD_0_MASK);
947     CSR_WRITE_2(sc, VX_COMMAND, SET_INTR_MASK);
948     CSR_WRITE_2(sc, VX_COMMAND, SET_RX_FILTER);
949
950     vxmbufempty(sc);
951 }
952
953 int
954 vxbusyeeprom(sc)
955     struct vx_softc *sc;
956 {
957     int j, i = 100;
958
959     while (i--) {
960         j = CSR_READ_2(sc, VX_W0_EEPROM_COMMAND);
961         if (j & EEPROM_BUSY)
962             DELAY(100);
963         else
964             break;
965     }
966     if (!i) {
967         if_printf(&sc->arpcom.ac_if, "eeprom failed to come ready\n");
968         return (1);
969     }
970     return (0);
971 }
972
973 static void
974 vxmbuffill(sp)
975     void *sp;
976 {
977     struct vx_softc *sc = (struct vx_softc *) sp;
978     int i;
979
980     crit_enter();
981     i = sc->last_mb;
982     do {
983         if (sc->mb[i] == NULL)
984             MGET(sc->mb[i], MB_DONTWAIT, MT_DATA);
985         if (sc->mb[i] == NULL)
986             break;
987         i = (i + 1) % MAX_MBS;
988     } while (i != sc->next_mb);
989     sc->last_mb = i;
990     /* If the queue was not filled, try again. */
991     if (sc->last_mb != sc->next_mb) {
992         callout_reset(&sc->vx_timer, 1, vxmbuffill, sc);
993         sc->buffill_pending = 1;
994     } else {
995         sc->buffill_pending = 0;
996     }
997     crit_exit();
998 }
999
1000 static void
1001 vxmbufempty(sc)
1002     struct vx_softc *sc;
1003 {
1004     int i;
1005
1006     crit_enter();
1007     for (i = 0; i < MAX_MBS; i++) {
1008         if (sc->mb[i]) {
1009             m_freem(sc->mb[i]);
1010             sc->mb[i] = NULL;
1011         }
1012     }
1013     sc->last_mb = sc->next_mb = 0;
1014     if (sc->buffill_pending != 0)
1015         callout_stop(&sc->vx_timer);
1016     crit_exit();
1017 }