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/i386/isa/if_el.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 /* Copyright (c) 1994, Matthew E. Kimmel.  Permission is hereby granted
    2  * to use, copy, modify and distribute this software provided that both
    3  * the copyright notice and this permission notice appear in all copies
    4  * of the software, derivative works or modified versions, and any
    5  * portions thereof.
    6  *
    7  * Questions, comments, bug reports and fixes to kimmel@cs.umass.edu.
    8  *
    9  * $FreeBSD: releng/5.1/sys/i386/isa/if_el.c 111119 2003-02-19 05:47:46Z imp $
   10  */
   11 /* Except of course for the portions of code lifted from other FreeBSD
   12  * drivers (mainly elread, elget and el_ioctl)
   13  */
   14 /* 3COM Etherlink 3C501 device driver for FreeBSD */
   15 /* Yeah, I know these cards suck, but you can also get them for free
   16  * really easily...
   17  */
   18 /* Bugs/possible improvements:
   19  *      - Does not currently support DMA
   20  *      - Does not currently support multicasts
   21  */
   22 #include "opt_inet.h"
   23 #include "opt_ipx.h"
   24 
   25 #include <sys/param.h>
   26 #include <sys/systm.h>
   27 #include <sys/kernel.h>
   28 #include <sys/sockio.h>
   29 #include <sys/mbuf.h>
   30 #include <sys/socket.h>
   31 #include <sys/syslog.h>
   32 #include <sys/bus.h>
   33 
   34 #include <net/ethernet.h>
   35 #include <net/if.h>
   36 
   37 #include <netinet/in.h>
   38 #include <netinet/if_ether.h>
   39 
   40 #include <net/bpf.h>
   41 
   42 #include <machine/bus_pio.h>
   43 #include <machine/bus.h>
   44 #include <machine/resource.h>
   45 #include <sys/bus.h>
   46 #include <sys/rman.h>
   47 
   48 #include <isa/isavar.h>
   49 
   50 #include <i386/isa/if_elreg.h>
   51 
   52 /* For debugging convenience */
   53 #ifdef EL_DEBUG
   54 #define dprintf(x) printf x
   55 #else
   56 #define dprintf(x)
   57 #endif
   58 
   59 /* el_softc: per line info and status */
   60 struct el_softc {
   61         struct arpcom arpcom;   /* Ethernet common */
   62         bus_space_handle_t      el_bhandle;
   63         bus_space_tag_t         el_btag;
   64         void                    *el_intrhand;
   65         struct resource         *el_irq;
   66         struct resource         *el_res;
   67         struct mtx              el_mtx;
   68         char el_pktbuf[EL_BUFSIZ];      /* Frame buffer */
   69 };
   70 
   71 /* Prototypes */
   72 static int el_attach(device_t);
   73 static int el_detach(device_t);
   74 static void el_init(void *);
   75 static int el_ioctl(struct ifnet *,u_long,caddr_t);
   76 static int el_probe(device_t);
   77 static void el_start(struct ifnet *);
   78 static void el_reset(void *);
   79 static void el_watchdog(struct ifnet *);
   80 static int el_shutdown(device_t);
   81 
   82 static void el_stop(void *);
   83 static int el_xmit(struct el_softc *,int);
   84 static void elintr(void *);
   85 static __inline void elread(struct el_softc *,caddr_t,int);
   86 static struct mbuf *elget(caddr_t,int,struct ifnet *);
   87 static __inline void el_hardreset(void *);
   88 
   89 static device_method_t el_methods[] = {
   90         /* Device interface */
   91         DEVMETHOD(device_probe,         el_probe),
   92         DEVMETHOD(device_attach,        el_attach),
   93         DEVMETHOD(device_detach,        el_detach),
   94         DEVMETHOD(device_shutdown,      el_shutdown),
   95         { 0, 0 }
   96 };
   97 
   98 static driver_t el_driver = {
   99         "el",
  100         el_methods,
  101         sizeof(struct el_softc)
  102 };
  103 
  104 static devclass_t el_devclass;
  105 
  106 DRIVER_MODULE(if_el, isa, el_driver, el_devclass, 0, 0);
  107 
  108 #define CSR_WRITE_1(sc, reg, val)       \
  109         bus_space_write_1(sc->el_btag, sc->el_bhandle, reg, val)
  110 #define CSR_READ_1(sc, reg)             \
  111         bus_space_read_1(sc->el_btag, sc->el_bhandle, reg)
  112 
  113 #define EL_LOCK(_sc)            mtx_lock(&(_sc)->el_mtx)
  114 #define EL_UNLOCK(_sc)          mtx_unlock(&(_sc)->el_mtx)
  115 
  116 /* Probe routine.  See if the card is there and at the right place. */
  117 static int
  118 el_probe(device_t dev)
  119 {
  120         struct el_softc *sc;
  121         u_short base; /* Just for convenience */
  122         u_char station_addr[ETHER_ADDR_LEN];
  123         int i, rid;
  124 
  125         /* Grab some info for our structure */
  126         sc = device_get_softc(dev);
  127 
  128         if (isa_get_logicalid(dev))             /* skip PnP probes */
  129                 return (ENXIO);
  130 
  131         if ((base = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0)
  132                 return (ENXIO);
  133 
  134         /* First check the base */
  135         if((base < 0x280) || (base > 0x3f0)) {
  136                 device_printf(dev,
  137                     "ioaddr must be between 0x280 and 0x3f0\n");
  138                 return(ENXIO);
  139         }
  140 
  141         /* Temporarily map the resources. */
  142         rid = 0;
  143         sc->el_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
  144             0, ~0, EL_IOSIZ, RF_ACTIVE);
  145 
  146         if (sc->el_res == NULL)
  147                 return(ENXIO);
  148 
  149         sc->el_btag = rman_get_bustag(sc->el_res);
  150         sc->el_bhandle = rman_get_bushandle(sc->el_res);
  151         mtx_init(&sc->el_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
  152             MTX_DEF | MTX_RECURSE);
  153         EL_LOCK(sc);
  154 
  155         /* Now attempt to grab the station address from the PROM
  156          * and see if it contains the 3com vendor code.
  157          */
  158         dprintf(("Probing 3c501 at 0x%x...\n",base));
  159 
  160         /* Reset the board */
  161         dprintf(("Resetting board...\n"));
  162         CSR_WRITE_1(sc,EL_AC,EL_AC_RESET);
  163         DELAY(5);
  164         CSR_WRITE_1(sc,EL_AC,0);
  165         dprintf(("Reading station address...\n"));
  166         /* Now read the address */
  167         for(i=0;i<ETHER_ADDR_LEN;i++) {
  168                 CSR_WRITE_1(sc,EL_GPBL,i);
  169                 station_addr[i] = CSR_READ_1(sc,EL_EAW);
  170         }
  171 
  172         /* Now release resources */
  173         bus_release_resource(dev, SYS_RES_IOPORT, rid, sc->el_res);
  174         EL_UNLOCK(sc);
  175         mtx_destroy(&sc->el_mtx);
  176 
  177         dprintf(("Address is %6D\n",station_addr, ":"));
  178 
  179         /* If the vendor code is ok, return a 1.  We'll assume that
  180          * whoever configured this system is right about the IRQ.
  181          */
  182         if((station_addr[0] != 0x02) || (station_addr[1] != 0x60)
  183            || (station_addr[2] != 0x8c)) {
  184                 dprintf(("Bad vendor code.\n"));
  185                 return(ENXIO);
  186         } else {
  187                 dprintf(("Vendor code ok.\n"));
  188                 /* Copy the station address into the arpcom structure */
  189                 bcopy(station_addr,sc->arpcom.ac_enaddr,ETHER_ADDR_LEN);
  190         }
  191 
  192         device_set_desc(dev, "3Com 3c501 Ethernet");
  193 
  194         return(0);
  195 }
  196 
  197 /* Do a hardware reset of the 3c501.  Do not call until after el_probe()! */
  198 static __inline void
  199 el_hardreset(xsc)
  200         void *xsc;
  201 {
  202         register struct el_softc *sc = xsc;
  203         register int j;
  204 
  205         /* First reset the board */
  206         CSR_WRITE_1(sc,EL_AC,EL_AC_RESET);
  207         DELAY(5);
  208         CSR_WRITE_1(sc,EL_AC,0);
  209 
  210         /* Then give it back its ethernet address.  Thanks to the mach
  211          * source code for this undocumented goodie...
  212          */
  213         for(j=0;j<ETHER_ADDR_LEN;j++)
  214                 CSR_WRITE_1(sc,j,sc->arpcom.ac_enaddr[j]);
  215 }
  216 
  217 /* Attach the interface to the kernel data structures.  By the time
  218  * this is called, we know that the card exists at the given I/O address.
  219  * We still assume that the IRQ given is correct.
  220  */
  221 static int
  222 el_attach(device_t dev)
  223 {
  224         struct el_softc *sc;
  225         struct ifnet *ifp;
  226         int rid, error;
  227 
  228         dprintf(("Attaching el%d...\n",device_get_unit(dev)));
  229 
  230         /* Get things pointing to the right places. */
  231         sc = device_get_softc(dev);
  232         ifp = &sc->arpcom.ac_if;
  233 
  234         rid = 0;
  235         sc->el_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
  236             0, ~0, EL_IOSIZ, RF_ACTIVE);
  237 
  238         if (sc->el_res == NULL)
  239                 return(ENXIO);
  240 
  241         rid = 0;
  242         sc->el_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
  243             RF_SHAREABLE | RF_ACTIVE);
  244 
  245         if (sc->el_irq == NULL) {
  246                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
  247                 return(ENXIO);
  248         }
  249 
  250         error = bus_setup_intr(dev, sc->el_irq, INTR_TYPE_NET,
  251                 elintr, sc, &sc->el_intrhand);
  252 
  253         if (error) {
  254                 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->el_irq);
  255                 bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
  256                 return(ENXIO);
  257         }
  258 
  259         /* Now reset the board */
  260         dprintf(("Resetting board...\n"));
  261         el_hardreset(sc);
  262 
  263         /* Initialize ifnet structure */
  264         ifp->if_softc = sc;
  265         ifp->if_unit = device_get_unit(dev);;
  266         ifp->if_name = "el";
  267         ifp->if_mtu = ETHERMTU;
  268         ifp->if_start = el_start;
  269         ifp->if_ioctl = el_ioctl;
  270         ifp->if_watchdog = el_watchdog;
  271         ifp->if_init = el_init;
  272         ifp->if_flags = (IFF_BROADCAST | IFF_MULTICAST | IFF_SIMPLEX);
  273         ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  274 
  275         /* Now we can attach the interface */
  276         dprintf(("Attaching interface...\n"));
  277         ether_ifattach(ifp, sc->arpcom.ac_enaddr);
  278 
  279         /* Print out some information for the user */
  280         device_printf(dev, "3c501 address %6D\n",
  281           sc->arpcom.ac_enaddr, ":");
  282 
  283         dprintf(("el_attach() finished.\n"));
  284         return(0);
  285 }
  286 
  287 static int el_detach(dev)
  288         device_t dev;
  289 {
  290         struct el_softc *sc;
  291         struct ifnet *ifp;
  292 
  293         sc = device_get_softc(dev);
  294         ifp = &sc->arpcom.ac_if;
  295 
  296         el_stop(sc);
  297         EL_LOCK(sc);
  298         ether_ifdetach(ifp);
  299         bus_teardown_intr(dev, sc->el_irq, sc->el_intrhand);
  300         bus_release_resource(dev, SYS_RES_IRQ, 0, sc->el_irq);
  301         bus_release_resource(dev, SYS_RES_IOPORT, 0, sc->el_res);
  302         EL_UNLOCK(sc);
  303         mtx_destroy(&sc->el_mtx);
  304 
  305         return(0);
  306 }
  307 
  308 static int
  309 el_shutdown(dev)
  310         device_t dev;
  311 {
  312         struct el_softc *sc;
  313 
  314         sc = device_get_softc(dev);
  315         el_stop(sc);
  316         return(0);
  317 }
  318 
  319 /* This routine resets the interface. */
  320 static void 
  321 el_reset(xsc)
  322         void *xsc;
  323 {
  324         struct el_softc *sc = xsc;
  325 
  326         dprintf(("elreset()\n"));
  327         el_stop(sc);
  328         el_init(sc);
  329 }
  330 
  331 static void el_stop(xsc)
  332         void *xsc;
  333 {
  334         struct el_softc *sc = xsc;
  335 
  336         EL_LOCK(sc);
  337         CSR_WRITE_1(sc,EL_AC,0);
  338         el_hardreset(sc);
  339         EL_UNLOCK(sc);
  340 }
  341 
  342 /* Initialize interface.  */
  343 static void 
  344 el_init(xsc)
  345         void *xsc;
  346 {
  347         struct el_softc *sc = xsc;
  348         struct ifnet *ifp;
  349 
  350         /* Set up pointers */
  351         ifp = &sc->arpcom.ac_if;
  352 
  353         /* If address not known, do nothing. */
  354         if(TAILQ_EMPTY(&ifp->if_addrhead)) /* XXX unlikely */
  355                 return;
  356 
  357         EL_LOCK(sc);
  358 
  359         /* First, reset the board. */
  360         dprintf(("Resetting board...\n"));
  361         el_hardreset(sc);
  362 
  363         /* Configure rx */
  364         dprintf(("Configuring rx...\n"));
  365         if(ifp->if_flags & IFF_PROMISC)
  366                 CSR_WRITE_1(sc,EL_RXC,
  367                     (EL_RXC_PROMISC|EL_RXC_ABROAD|EL_RXC_AMULTI|
  368                     EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
  369         else
  370                 CSR_WRITE_1(sc,EL_RXC,
  371                     (EL_RXC_ABROAD|EL_RXC_AMULTI|
  372                     EL_RXC_AGF|EL_RXC_DSHORT|EL_RXC_DDRIB|EL_RXC_DOFLOW));
  373         CSR_WRITE_1(sc,EL_RBC,0);
  374 
  375         /* Configure TX */
  376         dprintf(("Configuring tx...\n"));
  377         CSR_WRITE_1(sc,EL_TXC,0);
  378 
  379         /* Start reception */
  380         dprintf(("Starting reception...\n"));
  381         CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  382 
  383         /* Set flags appropriately */
  384         ifp->if_flags |= IFF_RUNNING;
  385         ifp->if_flags &= ~IFF_OACTIVE;
  386 
  387         /* And start output. */
  388         el_start(ifp);
  389 
  390         EL_UNLOCK(sc);
  391 }
  392 
  393 /* Start output on interface.  Get datagrams from the queue and output
  394  * them, giving the receiver a chance between datagrams.  Call only
  395  * from splimp or interrupt level!
  396  */
  397 static void
  398 el_start(struct ifnet *ifp)
  399 {
  400         struct el_softc *sc;
  401         struct mbuf *m, *m0;
  402         int i, len, retries, done;
  403 
  404         /* Get things pointing in the right directions */
  405         sc = ifp->if_softc;
  406 
  407         dprintf(("el_start()...\n"));
  408         EL_LOCK(sc);
  409 
  410         /* Don't do anything if output is active */
  411         if(sc->arpcom.ac_if.if_flags & IFF_OACTIVE)
  412                 return;
  413         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
  414 
  415         /* The main loop.  They warned me against endless loops, but
  416          * would I listen?  NOOO....
  417          */
  418         while(1) {
  419                 /* Dequeue the next datagram */
  420                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd,m0);
  421 
  422                 /* If there's nothing to send, return. */
  423                 if(m0 == NULL) {
  424                         sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
  425                         EL_UNLOCK(sc);
  426                         return;
  427                 }
  428 
  429                 /* Disable the receiver */
  430                 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
  431                 CSR_WRITE_1(sc,EL_RBC,0);
  432 
  433                 /* Copy the datagram to the buffer. */
  434                 len = 0;
  435                 for(m = m0; m != NULL; m = m->m_next) {
  436                         if(m->m_len == 0)
  437                                 continue;
  438                         bcopy(mtod(m,caddr_t),sc->el_pktbuf+len,m->m_len);
  439                         len += m->m_len;
  440                 }
  441                 m_freem(m0);
  442 
  443                 len = max(len,ETHER_MIN_LEN);
  444 
  445                 /* Give the packet to the bpf, if any */
  446                 BPF_TAP(&sc->arpcom.ac_if, sc->el_pktbuf, len);
  447 
  448                 /* Transfer datagram to board */
  449                 dprintf(("el: xfr pkt length=%d...\n",len));
  450                 i = EL_BUFSIZ - len;
  451                 CSR_WRITE_1(sc,EL_GPBL,(i & 0xff));
  452                 CSR_WRITE_1(sc,EL_GPBH,((i>>8)&0xff));
  453                 bus_space_write_multi_1(sc->el_btag, sc->el_bhandle,
  454                     EL_BUF, sc->el_pktbuf, len);
  455 
  456                 /* Now transmit the datagram */
  457                 retries=0;
  458                 done=0;
  459                 while(!done) {
  460                         if(el_xmit(sc,len)) { /* Something went wrong */
  461                                 done = -1;
  462                                 break;
  463                         }
  464                         /* Check out status */
  465                         i = CSR_READ_1(sc,EL_TXS);
  466                         dprintf(("tx status=0x%x\n",i));
  467                         if(!(i & EL_TXS_READY)) {
  468                                 dprintf(("el: err txs=%x\n",i));
  469                                 sc->arpcom.ac_if.if_oerrors++;
  470                                 if(i & (EL_TXS_COLL|EL_TXS_COLL16)) {
  471                                         if((!(i & EL_TXC_DCOLL16)) && retries < 15) {
  472                                                 retries++;
  473                                                 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
  474                                         }
  475                                 }
  476                                 else
  477                                         done = 1;
  478                         }
  479                         else {
  480                                 sc->arpcom.ac_if.if_opackets++;
  481                                 done = 1;
  482                         }
  483                 }
  484                 if(done == -1)  /* Packet not transmitted */
  485                         continue;
  486 
  487                 /* Now give the card a chance to receive.
  488                  * Gotta love 3c501s...
  489                  */
  490                 (void)CSR_READ_1(sc,EL_AS);
  491                 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  492                 EL_UNLOCK(sc);
  493                 /* Interrupt here */
  494                 EL_LOCK(sc);
  495         }
  496 }
  497 
  498 /* This function actually attempts to transmit a datagram downloaded
  499  * to the board.  Call at splimp or interrupt, after downloading data!
  500  * Returns 0 on success, non-0 on failure
  501  */
  502 static int
  503 el_xmit(struct el_softc *sc,int len)
  504 {
  505         int gpl;
  506         int i;
  507 
  508         gpl = EL_BUFSIZ - len;
  509         dprintf(("el: xmit..."));
  510         CSR_WRITE_1(sc,EL_GPBL,(gpl & 0xff));
  511         CSR_WRITE_1(sc,EL_GPBH,((gpl>>8)&0xff));
  512         CSR_WRITE_1(sc,EL_AC,EL_AC_TXFRX);
  513         i = 20000;
  514         while((CSR_READ_1(sc,EL_AS) & EL_AS_TXBUSY) && (i>0))
  515                 i--;
  516         if(i == 0) {
  517                 dprintf(("tx not ready\n"));
  518                 sc->arpcom.ac_if.if_oerrors++;
  519                 return(-1);
  520         }
  521         dprintf(("%d cycles.\n",(20000-i)));
  522         return(0);
  523 }
  524 
  525 /* Pass a packet up to the higher levels. */
  526 static __inline void
  527 elread(struct el_softc *sc,caddr_t buf,int len)
  528 {
  529         struct ifnet *ifp = &sc->arpcom.ac_if;
  530         struct mbuf *m;
  531 
  532         /*
  533          * Put packet into an mbuf chain
  534          */
  535         m = elget(buf,len,ifp);
  536         if(m == 0)
  537                 return;
  538 
  539         (*ifp->if_input)(ifp,m);
  540 }
  541 
  542 /* controller interrupt */
  543 static void
  544 elintr(void *xsc)
  545 {
  546         register struct el_softc *sc;
  547         int stat, rxstat, len, done;
  548 
  549 
  550         /* Get things pointing properly */
  551         sc = xsc;
  552         EL_LOCK(sc);
  553         dprintf(("elintr: "));
  554 
  555         /* Check board status */
  556         stat = CSR_READ_1(sc,EL_AS);
  557         if(stat & EL_AS_RXBUSY) {
  558                 (void)CSR_READ_1(sc,EL_RXC);
  559                 CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  560                 EL_UNLOCK(sc);
  561                 return;
  562         }
  563 
  564         done = 0;
  565         while(!done) {
  566                 rxstat = CSR_READ_1(sc,EL_RXS);
  567                 if(rxstat & EL_RXS_STALE) {
  568                         (void)CSR_READ_1(sc,EL_RXC);
  569                         CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  570                         EL_UNLOCK(sc);
  571                         return;
  572                 }
  573 
  574                 /* If there's an overflow, reinit the board. */
  575                 if(!(rxstat & EL_RXS_NOFLOW)) {
  576                         dprintf(("overflow.\n"));
  577                         el_hardreset(sc);
  578                         /* Put board back into receive mode */
  579                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
  580                                 CSR_WRITE_1(sc,EL_RXC,
  581                                     (EL_RXC_PROMISC|EL_RXC_ABROAD|
  582                                     EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
  583                                     EL_RXC_DDRIB|EL_RXC_DOFLOW));
  584                         else
  585                                 CSR_WRITE_1(sc,EL_RXC,
  586                                     (EL_RXC_ABROAD|
  587                                     EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
  588                                     EL_RXC_DDRIB|EL_RXC_DOFLOW));
  589                         (void)CSR_READ_1(sc,EL_AS);
  590                         CSR_WRITE_1(sc,EL_RBC,0);
  591                         (void)CSR_READ_1(sc,EL_RXC);
  592                         CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  593                         EL_UNLOCK(sc);
  594                         return;
  595                 }
  596 
  597                 /* Incoming packet */
  598                 len = CSR_READ_1(sc,EL_RBL);
  599                 len |= CSR_READ_1(sc,EL_RBH) << 8;
  600                 dprintf(("receive len=%d rxstat=%x ",len,rxstat));
  601                 CSR_WRITE_1(sc,EL_AC,EL_AC_HOST);
  602 
  603                 /* If packet too short or too long, restore rx mode and return
  604                  */
  605                 if((len <= sizeof(struct ether_header)) || (len > ETHER_MAX_LEN)) {
  606                         if(sc->arpcom.ac_if.if_flags & IFF_PROMISC)
  607                                 CSR_WRITE_1(sc,EL_RXC,
  608                                     (EL_RXC_PROMISC|EL_RXC_ABROAD|
  609                                     EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
  610                                     EL_RXC_DDRIB|EL_RXC_DOFLOW));
  611                         else
  612                                 CSR_WRITE_1(sc,EL_RXC,
  613                                     (EL_RXC_ABROAD|
  614                                     EL_RXC_AMULTI|EL_RXC_AGF|EL_RXC_DSHORT|
  615                                     EL_RXC_DDRIB|EL_RXC_DOFLOW));
  616                         (void)CSR_READ_1(sc,EL_AS);
  617                         CSR_WRITE_1(sc,EL_RBC,0);
  618                         (void)CSR_READ_1(sc,EL_RXC);
  619                         CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  620                         EL_UNLOCK(sc);
  621                         return;
  622                 }
  623 
  624                 sc->arpcom.ac_if.if_ipackets++;
  625 
  626                 /* Copy the data into our buffer */
  627                 CSR_WRITE_1(sc,EL_GPBL,0);
  628                 CSR_WRITE_1(sc,EL_GPBH,0);
  629                 bus_space_read_multi_1(sc->el_btag, sc->el_bhandle,
  630                     EL_BUF, sc->el_pktbuf, len);
  631                 CSR_WRITE_1(sc,EL_RBC,0);
  632                 CSR_WRITE_1(sc,EL_AC,EL_AC_RX);
  633                 dprintf(("%6D-->",sc->el_pktbuf+6,":"));
  634                 dprintf(("%6D\n",sc->el_pktbuf,":"));
  635 
  636                 /* Pass data up to upper levels */
  637                 elread(sc,(caddr_t)(sc->el_pktbuf),len);
  638 
  639                 /* Is there another packet? */
  640                 stat = CSR_READ_1(sc,EL_AS);
  641 
  642                 /* If so, do it all again (i.e. don't set done to 1) */
  643                 if(!(stat & EL_AS_RXBUSY))
  644                         dprintf(("<rescan> "));
  645                 else
  646                         done = 1;
  647         }
  648 
  649         (void)CSR_READ_1(sc,EL_RXC);
  650         CSR_WRITE_1(sc,EL_AC,(EL_AC_IRQE|EL_AC_RX));
  651         EL_UNLOCK(sc);
  652         return;
  653 }
  654 
  655 /*
  656  * Pull read data off an interface.
  657  * Len is length of data, with local net header stripped.
  658  */
  659 static struct mbuf *
  660 elget(buf, totlen, ifp)
  661         caddr_t buf;
  662         int totlen;
  663         struct ifnet *ifp;
  664 {
  665         struct mbuf *top, **mp, *m;
  666         int len;
  667         register caddr_t cp;
  668         char *epkt;
  669 
  670         buf += sizeof(struct ether_header);
  671         cp = buf;
  672         epkt = cp + totlen;
  673 
  674         MGETHDR(m, M_DONTWAIT, MT_DATA);
  675         if (m == 0)
  676                 return (0);
  677         m->m_pkthdr.rcvif = ifp;
  678         m->m_pkthdr.len = totlen;
  679         m->m_len = MHLEN;
  680         top = 0;
  681         mp = &top;
  682         while (totlen > 0) {
  683                 if (top) {
  684                         MGET(m, M_DONTWAIT, MT_DATA);
  685                         if (m == 0) {
  686                                 m_freem(top);
  687                                 return (0);
  688                         }
  689                         m->m_len = MLEN;
  690                 }
  691                 len = min(totlen, epkt - cp);
  692                 if (len >= MINCLSIZE) {
  693                         MCLGET(m, M_DONTWAIT);
  694                         if (m->m_flags & M_EXT)
  695                                 m->m_len = len = min(len, MCLBYTES);
  696                         else
  697                                 len = m->m_len;
  698                 } else {
  699                         /*
  700                          * Place initial small packet/header at end of mbuf.
  701                          */
  702                         if (len < m->m_len) {
  703                                 if (top == 0 && len + max_linkhdr <= m->m_len)
  704                                         m->m_data += max_linkhdr;
  705                                 m->m_len = len;
  706                         } else
  707                                 len = m->m_len;
  708                 }
  709                 bcopy(cp, mtod(m, caddr_t), (unsigned)len);
  710                 cp += len;
  711                 *mp = m;
  712                 mp = &m->m_next;
  713                 totlen -= len;
  714                 if (cp == epkt)
  715                         cp = buf;
  716         }
  717         return (top);
  718 }
  719 
  720 /*
  721  * Process an ioctl request. This code needs some work - it looks
  722  *      pretty ugly.
  723  */
  724 static int
  725 el_ioctl(ifp, command, data)
  726         register struct ifnet *ifp;
  727         u_long command;
  728         caddr_t data;
  729 {
  730         int error = 0;
  731         struct el_softc *sc;
  732 
  733         sc = ifp->if_softc;
  734         EL_LOCK(sc);
  735 
  736         switch (command) {
  737         case SIOCSIFFLAGS:
  738                 /*
  739                  * If interface is marked down and it is running, then stop it
  740                  */
  741                 if (((ifp->if_flags & IFF_UP) == 0) &&
  742                     (ifp->if_flags & IFF_RUNNING)) {
  743                         el_stop(ifp->if_softc);
  744                         ifp->if_flags &= ~IFF_RUNNING;
  745                 } else {
  746                 /*
  747                  * If interface is marked up and it is stopped, then start it
  748                  */
  749                         if ((ifp->if_flags & IFF_UP) &&
  750                             ((ifp->if_flags & IFF_RUNNING) == 0))
  751                                 el_init(ifp->if_softc);
  752                 }
  753                 break;
  754         default:
  755                 error = ether_ioctl(ifp, command, data);
  756                 break;
  757         }
  758         EL_UNLOCK(sc);
  759         return (error);
  760 }
  761 
  762 /* Device timeout routine */
  763 static void
  764 el_watchdog(struct ifnet *ifp)
  765 {
  766         log(LOG_ERR,"el%d: device timeout\n", ifp->if_unit);
  767         ifp->if_oerrors++;
  768         el_reset(ifp->if_softc);
  769 }

Cache object: 99fe149258a89c91dfda00c0eb3af980


[ 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.