The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/vx/if_vx.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 32c1184bf478309acf50f2047fae6602


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.