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/ic/dp8390.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 /*      $NetBSD: dp8390.c,v 1.68.14.1 2010/11/20 00:33:45 riz Exp $     */
    2 
    3 /*
    4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
    5  * adapters.
    6  *
    7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
    8  *
    9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
   10  * copied, distributed, and sold, in both source and binary form provided that
   11  * the above copyright and these terms are retained.  Under no circumstances is
   12  * the author responsible for the proper functioning of this software, nor does
   13  * the author assume any responsibility for damages incurred with its use.
   14  */
   15 
   16 #include <sys/cdefs.h>
   17 __KERNEL_RCSID(0, "$NetBSD: dp8390.c,v 1.68.14.1 2010/11/20 00:33:45 riz Exp $");
   18 
   19 #include "opt_ipkdb.h"
   20 #include "opt_inet.h"
   21 #include "bpfilter.h"
   22 #include "rnd.h"
   23 
   24 #include <sys/param.h>
   25 #include <sys/systm.h>
   26 #include <sys/device.h>
   27 #include <sys/errno.h>
   28 #include <sys/ioctl.h>
   29 #include <sys/mbuf.h>
   30 #include <sys/socket.h>
   31 #include <sys/syslog.h>
   32 
   33 #if NRND > 0
   34 #include <sys/rnd.h>
   35 #endif
   36 
   37 #include <net/if.h>
   38 #include <net/if_dl.h>
   39 #include <net/if_types.h>
   40 #include <net/if_media.h>
   41 #include <net/if_ether.h>
   42 
   43 #ifdef INET
   44 #include <netinet/in.h>
   45 #include <netinet/in_systm.h>
   46 #include <netinet/in_var.h>
   47 #include <netinet/ip.h>
   48 #include <netinet/if_inarp.h>
   49 #endif
   50 
   51 
   52 #if NBPFILTER > 0
   53 #include <net/bpf.h>
   54 #include <net/bpfdesc.h>
   55 #endif
   56 
   57 #include <sys/bus.h>
   58 
   59 #ifdef IPKDB_DP8390
   60 #include <ipkdb/ipkdb.h>
   61 #endif
   62 
   63 #include <dev/ic/dp8390reg.h>
   64 #include <dev/ic/dp8390var.h>
   65 
   66 #ifdef DEBUG
   67 #define inline  /* XXX for debugging porpoises */
   68 int     dp8390_debug = 0;
   69 #endif
   70 
   71 static inline void      dp8390_xmit(struct dp8390_softc *);
   72 
   73 static inline void      dp8390_read_hdr(struct dp8390_softc *,
   74                             int, struct dp8390_ring *);
   75 static inline int       dp8390_ring_copy(struct dp8390_softc *,
   76                             int, void *, u_short);
   77 static inline int       dp8390_write_mbuf(struct dp8390_softc *,
   78                             struct mbuf *, int);
   79 
   80 static int              dp8390_test_mem(struct dp8390_softc *);
   81 
   82 /*
   83  * Standard media init routine for the dp8390.
   84  */
   85 void
   86 dp8390_media_init(struct dp8390_softc *sc)
   87 {
   88 
   89         ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
   90         ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
   91         ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
   92 }
   93 
   94 /*
   95  * Do bus-independent setup.
   96  */
   97 int
   98 dp8390_config(sc)
   99         struct dp8390_softc *sc;
  100 {
  101         struct ifnet *ifp = &sc->sc_ec.ec_if;
  102         int rv;
  103 
  104         rv = 1;
  105 
  106         if (!sc->test_mem)
  107                 sc->test_mem = dp8390_test_mem;
  108 
  109         /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
  110         if ((sc->mem_size < 16384) ||
  111             (sc->sc_flags & DP8390_NO_MULTI_BUFFERING))
  112                 sc->txb_cnt = 1;
  113         else if (sc->mem_size < 8192 * 3)
  114                 sc->txb_cnt = 2;
  115         else
  116                 sc->txb_cnt = 3;
  117 
  118         sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
  119         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
  120         sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
  121         sc->mem_ring = sc->mem_start +
  122             ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
  123         sc->mem_end = sc->mem_start + sc->mem_size;
  124 
  125         /* Now zero memory and verify that it is clear. */
  126         if ((*sc->test_mem)(sc))
  127                 goto out;
  128 
  129         /* Set interface to stopped condition (reset). */
  130         dp8390_stop(sc);
  131 
  132         /* Initialize ifnet structure. */
  133         strcpy(ifp->if_xname, device_xname(sc->sc_dev));
  134         ifp->if_softc = sc;
  135         ifp->if_start = dp8390_start;
  136         ifp->if_ioctl = dp8390_ioctl;
  137         if (!ifp->if_watchdog)
  138                 ifp->if_watchdog = dp8390_watchdog;
  139         ifp->if_flags =
  140             IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
  141         IFQ_SET_READY(&ifp->if_snd);
  142 
  143         /* Print additional info when attached. */
  144         aprint_normal_dev(sc->sc_dev, "Ethernet address %s\n",
  145             ether_sprintf(sc->sc_enaddr));
  146 
  147         /* Initialize media goo. */
  148         (*sc->sc_media_init)(sc);
  149 
  150         /*
  151          * We can support 802.1Q VLAN-sized frames.
  152          */
  153         sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
  154 
  155         /* Attach the interface. */
  156         if_attach(ifp);
  157         ether_ifattach(ifp, sc->sc_enaddr);
  158 
  159 #if NRND > 0
  160         rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
  161             RND_TYPE_NET, 0);
  162 #endif
  163 
  164         /* The attach is successful. */
  165         sc->sc_flags |= DP8390_ATTACHED;
  166 
  167         rv = 0;
  168 out:
  169         return (rv);
  170 }
  171 
  172 /*
  173  * Media change callback.
  174  */
  175 int
  176 dp8390_mediachange(ifp)
  177         struct ifnet *ifp;
  178 {
  179         struct dp8390_softc *sc = ifp->if_softc;
  180 
  181         if (sc->sc_mediachange)
  182                 return ((*sc->sc_mediachange)(sc));
  183         return (0);
  184 }
  185 
  186 /*
  187  * Media status callback.
  188  */
  189 void
  190 dp8390_mediastatus(ifp, ifmr)
  191         struct ifnet *ifp;
  192         struct ifmediareq *ifmr;
  193 {
  194         struct dp8390_softc *sc = ifp->if_softc;
  195 
  196         if (sc->sc_enabled == 0) {
  197                 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
  198                 ifmr->ifm_status = 0;
  199                 return;
  200         }
  201 
  202         if (sc->sc_mediastatus)
  203                 (*sc->sc_mediastatus)(sc, ifmr);
  204 }
  205 
  206 /*
  207  * Reset interface.
  208  */
  209 void
  210 dp8390_reset(sc)
  211         struct dp8390_softc *sc;
  212 {
  213         int     s;
  214 
  215         s = splnet();
  216         dp8390_stop(sc);
  217         dp8390_init(sc);
  218         splx(s);
  219 }
  220 
  221 /*
  222  * Take interface offline.
  223  */
  224 void
  225 dp8390_stop(sc)
  226         struct dp8390_softc *sc;
  227 {
  228         bus_space_tag_t regt = sc->sc_regt;
  229         bus_space_handle_t regh = sc->sc_regh;
  230         int n = 5000;
  231 
  232         /* Stop everything on the interface, and select page 0 registers. */
  233         NIC_BARRIER(regt, regh);
  234         NIC_PUT(regt, regh, ED_P0_CR,
  235             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
  236         NIC_BARRIER(regt, regh);
  237 
  238         /*
  239          * Wait for interface to enter stopped state, but limit # of checks to
  240          * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
  241          * just in case it's an old one.
  242          */
  243         while (((NIC_GET(regt, regh,
  244             ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
  245                 DELAY(1);
  246 
  247         if (sc->stop_card != NULL)
  248                 (*sc->stop_card)(sc);
  249 }
  250 
  251 /*
  252  * Device timeout/watchdog routine.  Entered if the device neglects to generate
  253  * an interrupt after a transmit has been started on it.
  254  */
  255 
  256 void
  257 dp8390_watchdog(ifp)
  258         struct ifnet *ifp;
  259 {
  260         struct dp8390_softc *sc = ifp->if_softc;
  261 
  262         log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
  263         ++sc->sc_ec.ec_if.if_oerrors;
  264 
  265         dp8390_reset(sc);
  266 }
  267 
  268 /*
  269  * Initialize device.
  270  */
  271 void
  272 dp8390_init(sc)
  273         struct dp8390_softc *sc;
  274 {
  275         bus_space_tag_t regt = sc->sc_regt;
  276         bus_space_handle_t regh = sc->sc_regh;
  277         struct ifnet *ifp = &sc->sc_ec.ec_if;
  278         u_int8_t mcaf[8];
  279         int i;
  280 
  281         /*
  282          * Initialize the NIC in the exact order outlined in the NS manual.
  283          * This init procedure is "mandatory"...don't change what or when
  284          * things happen.
  285          */
  286 
  287         /* Reset transmitter flags. */
  288         ifp->if_timer = 0;
  289 
  290         sc->txb_inuse = 0;
  291         sc->txb_new = 0;
  292         sc->txb_next_tx = 0;
  293 
  294         /* Set interface for page 0, remote DMA complete, stopped. */
  295         NIC_BARRIER(regt, regh);
  296         NIC_PUT(regt, regh, ED_P0_CR,
  297             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
  298         NIC_BARRIER(regt, regh);
  299 
  300         if (sc->dcr_reg & ED_DCR_LS) {
  301                 NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
  302         } else {
  303                 /*
  304                  * Set FIFO threshold to 8, No auto-init Remote DMA, byte
  305                  * order=80x86, byte-wide DMA xfers,
  306                  */
  307                 NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
  308         }
  309 
  310         /* Clear remote byte count registers. */
  311         NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
  312         NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
  313 
  314         /* Tell RCR to do nothing for now. */
  315         NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
  316 
  317         /* Place NIC in internal loopback mode. */
  318         NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
  319 
  320         /* Set lower bits of byte addressable framing to 0. */
  321         if (sc->is790)
  322                 NIC_PUT(regt, regh, 0x09, 0);
  323 
  324         /* Initialize receive buffer ring. */
  325         NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
  326         NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
  327         NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
  328 
  329         /*
  330          * Enable the following interrupts: receive/transmit complete,
  331          * receive/transmit error, and Receiver OverWrite.
  332          *
  333          * Counter overflow and Remote DMA complete are *not* enabled.
  334          */
  335         NIC_PUT(regt, regh, ED_P0_IMR,
  336             ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
  337             ED_IMR_OVWE);
  338 
  339         /*
  340          * Clear all interrupts.  A '1' in each bit position clears the
  341          * corresponding flag.
  342          */
  343         NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
  344 
  345         /* Program command register for page 1. */
  346         NIC_BARRIER(regt, regh);
  347         NIC_PUT(regt, regh, ED_P0_CR,
  348             sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
  349         NIC_BARRIER(regt, regh);
  350 
  351         /* Copy out our station address. */
  352         for (i = 0; i < ETHER_ADDR_LEN; ++i)
  353                 NIC_PUT(regt, regh, ED_P1_PAR0 + i,
  354                     CLLADDR(ifp->if_sadl)[i]);
  355 
  356         /* Set multicast filter on chip. */
  357         dp8390_getmcaf(&sc->sc_ec, mcaf);
  358         for (i = 0; i < 8; i++)
  359                 NIC_PUT(regt, regh, ED_P1_MAR0 + i, mcaf[i]);
  360 
  361         /*
  362          * Set current page pointer to one page after the boundary pointer, as
  363          * recommended in the National manual.
  364          */
  365         sc->next_packet = sc->rec_page_start + 1;
  366         NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
  367 
  368         /* Program command register for page 0. */
  369         NIC_BARRIER(regt, regh);
  370         NIC_PUT(regt, regh, ED_P1_CR,
  371             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
  372         NIC_BARRIER(regt, regh);
  373 
  374         /* Accept broadcast and multicast packets by default. */
  375         i = ED_RCR_AB | ED_RCR_AM | sc->rcr_proto;
  376         if (ifp->if_flags & IFF_PROMISC) {
  377                 /*
  378                  * Set promiscuous mode.  Multicast filter was set earlier so
  379                  * that we should receive all multicast packets.
  380                  */
  381                 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
  382         }
  383         NIC_PUT(regt, regh, ED_P0_RCR, i);
  384 
  385         /* Take interface out of loopback. */
  386         NIC_PUT(regt, regh, ED_P0_TCR, 0);
  387 
  388         /* Do any card-specific initialization, if applicable. */
  389         if (sc->init_card)
  390                 (*sc->init_card)(sc);
  391 
  392         /* Fire up the interface. */
  393         NIC_BARRIER(regt, regh);
  394         NIC_PUT(regt, regh, ED_P0_CR,
  395             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
  396 
  397         /* Set 'running' flag, and clear output active flag. */
  398         ifp->if_flags |= IFF_RUNNING;
  399         ifp->if_flags &= ~IFF_OACTIVE;
  400 
  401         /* ...and attempt to start output. */
  402         dp8390_start(ifp);
  403 }
  404 
  405 /*
  406  * This routine actually starts the transmission on the interface.
  407  */
  408 static inline void
  409 dp8390_xmit(sc)
  410         struct dp8390_softc *sc;
  411 {
  412         bus_space_tag_t regt = sc->sc_regt;
  413         bus_space_handle_t regh = sc->sc_regh;
  414         struct ifnet *ifp = &sc->sc_ec.ec_if;
  415         u_short len;
  416 
  417 #ifdef DIAGNOSTIC
  418         if ((sc->txb_next_tx + sc->txb_inuse) % sc->txb_cnt != sc->txb_new)
  419                 panic("dp8390_xmit: desync, next_tx=%d inuse=%d cnt=%d new=%d",
  420                     sc->txb_next_tx, sc->txb_inuse, sc->txb_cnt, sc->txb_new);
  421 
  422         if (sc->txb_inuse == 0)
  423                 panic("dp8390_xmit: no packets to xmit");
  424 #endif
  425 
  426         len = sc->txb_len[sc->txb_next_tx];
  427 
  428         /* Set NIC for page 0 register access. */
  429         NIC_BARRIER(regt, regh);
  430         NIC_PUT(regt, regh, ED_P0_CR,
  431             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
  432         NIC_BARRIER(regt, regh);
  433 
  434         /* Set TX buffer start page. */
  435         NIC_PUT(regt, regh, ED_P0_TPSR, sc->tx_page_start +
  436             sc->txb_next_tx * ED_TXBUF_SIZE);
  437 
  438         /* Set TX length. */
  439         NIC_PUT(regt, regh, ED_P0_TBCR0, len);
  440         NIC_PUT(regt, regh, ED_P0_TBCR1, len >> 8);
  441 
  442         /* Set page 0, remote DMA complete, transmit packet, and *start*. */
  443         NIC_BARRIER(regt, regh);
  444         NIC_PUT(regt, regh, ED_P0_CR,
  445             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
  446 
  447         /* Point to next transmit buffer slot and wrap if necessary. */
  448         if (++sc->txb_next_tx == sc->txb_cnt)
  449                 sc->txb_next_tx = 0;
  450 
  451         /* Set a timer just in case we never hear from the board again. */
  452         ifp->if_timer = 2;
  453 }
  454 
  455 /*
  456  * Start output on interface.
  457  * We make two assumptions here:
  458  *  1) that the current priority is set to splnet _before_ this code
  459  *     is called *and* is returned to the appropriate priority after
  460  *     return
  461  *  2) that the IFF_OACTIVE flag is checked before this code is called
  462  *     (i.e. that the output part of the interface is idle)
  463  */
  464 void
  465 dp8390_start(ifp)
  466         struct ifnet *ifp;
  467 {
  468         struct dp8390_softc *sc = ifp->if_softc;
  469         struct mbuf *m0;
  470         int buffer;
  471         int len;
  472 
  473         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
  474                 return;
  475 
  476 outloop:
  477         /* See if there is room to put another packet in the buffer. */
  478         if (sc->txb_inuse == sc->txb_cnt) {
  479                 /* No room.  Indicate this to the outside world and exit. */
  480                 ifp->if_flags |= IFF_OACTIVE;
  481                 return;
  482         }
  483         IFQ_DEQUEUE(&ifp->if_snd, m0);
  484         if (m0 == 0)
  485                 return;
  486 
  487         /* We need to use m->m_pkthdr.len, so require the header */
  488         if ((m0->m_flags & M_PKTHDR) == 0)
  489                 panic("dp8390_start: no header mbuf");
  490 
  491 #if NBPFILTER > 0
  492         /* Tap off here if there is a BPF listener. */
  493         if (ifp->if_bpf)
  494                 bpf_mtap(ifp->if_bpf, m0);
  495 #endif
  496 
  497         /* txb_new points to next open buffer slot. */
  498         buffer = sc->mem_start +
  499             ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
  500 
  501         if (sc->write_mbuf)
  502                 len = (*sc->write_mbuf)(sc, m0, buffer);
  503         else
  504                 len = dp8390_write_mbuf(sc, m0, buffer);
  505 
  506         m_freem(m0);
  507         sc->txb_len[sc->txb_new] = len;
  508 
  509         /* Point to next buffer slot and wrap if necessary. */
  510         if (++sc->txb_new == sc->txb_cnt)
  511                 sc->txb_new = 0;
  512 
  513         /* Start the first packet transmitting. */
  514         if (sc->txb_inuse++ == 0)
  515                 dp8390_xmit(sc);
  516 
  517         /* Loop back to the top to possibly buffer more packets. */
  518         goto outloop;
  519 }
  520 
  521 /*
  522  * Ethernet interface receiver interrupt.
  523  */
  524 void
  525 dp8390_rint(sc)
  526         struct dp8390_softc *sc;
  527 {
  528         bus_space_tag_t regt = sc->sc_regt;
  529         bus_space_handle_t regh = sc->sc_regh;
  530         struct dp8390_ring packet_hdr;
  531         int packet_ptr;
  532         u_short len;
  533         u_char boundary, current;
  534         u_char nlen;
  535 
  536 loop:
  537         /* Set NIC to page 1 registers to get 'current' pointer. */
  538         NIC_BARRIER(regt, regh);
  539         NIC_PUT(regt, regh, ED_P0_CR,
  540             sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
  541         NIC_BARRIER(regt, regh);
  542 
  543         /*
  544          * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
  545          * it points to where new data has been buffered.  The 'CURR' (current)
  546          * register points to the logical end of the ring-buffer - i.e. it
  547          * points to where additional new data will be added.  We loop here
  548          * until the logical beginning equals the logical end (or in other
  549          * words, until the ring-buffer is empty).
  550          */
  551         current = NIC_GET(regt, regh, ED_P1_CURR);
  552         if (sc->next_packet == current)
  553                 return;
  554 
  555         /* Set NIC to page 0 registers to update boundary register. */
  556         NIC_BARRIER(regt, regh);
  557         NIC_PUT(regt, regh, ED_P1_CR,
  558             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
  559         NIC_BARRIER(regt, regh);
  560 
  561         do {
  562                 /* Get pointer to this buffer's header structure. */
  563                 packet_ptr = sc->mem_ring +
  564                     ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
  565 
  566                 if (sc->read_hdr)
  567                         (*sc->read_hdr)(sc, packet_ptr, &packet_hdr);
  568                 else
  569                         dp8390_read_hdr(sc, packet_ptr, &packet_hdr);
  570                 len = packet_hdr.count;
  571 
  572                 /*
  573                  * Try do deal with old, buggy chips that sometimes duplicate
  574                  * the low byte of the length into the high byte.  We do this
  575                  * by simply ignoring the high byte of the length and always
  576                  * recalculating it.
  577                  *
  578                  * NOTE: sc->next_packet is pointing at the current packet.
  579                  */
  580                 if (packet_hdr.next_packet >= sc->next_packet)
  581                         nlen = (packet_hdr.next_packet - sc->next_packet);
  582                 else
  583                         nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
  584                             (sc->rec_page_stop - sc->next_packet));
  585                 --nlen;
  586                 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
  587                         --nlen;
  588                 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
  589 #ifdef DIAGNOSTIC
  590                 if (len != packet_hdr.count) {
  591                         aprint_verbose_dev(sc->sc_dev, "length does not match "
  592                             "next packet pointer\n");
  593                         aprint_verbose_dev(sc->sc_dev, "len %04x nlen %04x "
  594                             "start %02x first %02x curr %02x next %02x "
  595                             "stop %02x\n", packet_hdr.count, len,
  596                             sc->rec_page_start, sc->next_packet, current,
  597                             packet_hdr.next_packet, sc->rec_page_stop);
  598                 }
  599 #endif
  600 
  601                 /*
  602                  * Be fairly liberal about what we allow as a "reasonable"
  603                  * length so that a [crufty] packet will make it to BPF (and
  604                  * can thus be analyzed).  Note that all that is really
  605                  * important is that we have a length that will fit into one
  606                  * mbuf cluster or less; the upper layer protocols can then
  607                  * figure out the length from their own length field(s).
  608                  */
  609                 if (len <= MCLBYTES &&
  610                     packet_hdr.next_packet >= sc->rec_page_start &&
  611                     packet_hdr.next_packet < sc->rec_page_stop) {
  612                         /* Go get packet. */
  613                         dp8390_read(sc,
  614                             packet_ptr + sizeof(struct dp8390_ring),
  615                             len - sizeof(struct dp8390_ring));
  616                 } else {
  617                         /* Really BAD.  The ring pointers are corrupted. */
  618                         log(LOG_ERR, "%s: NIC memory corrupt - "
  619                             "invalid packet length %d\n",
  620                             device_xname(sc->sc_dev), len);
  621                         ++sc->sc_ec.ec_if.if_ierrors;
  622                         dp8390_reset(sc);
  623                         return;
  624                 }
  625 
  626                 /* Update next packet pointer. */
  627                 sc->next_packet = packet_hdr.next_packet;
  628 
  629                 /*
  630                  * Update NIC boundary pointer - being careful to keep it one
  631                  * buffer behind (as recommended by NS databook).
  632                  */
  633                 boundary = sc->next_packet - 1;
  634                 if (boundary < sc->rec_page_start)
  635                         boundary = sc->rec_page_stop - 1;
  636                 NIC_PUT(regt, regh, ED_P0_BNRY, boundary);
  637         } while (sc->next_packet != current);
  638 
  639         goto loop;
  640 }
  641 
  642 /* Ethernet interface interrupt processor. */
  643 int
  644 dp8390_intr(arg)
  645         void *arg;
  646 {
  647         struct dp8390_softc *sc = (struct dp8390_softc *)arg;
  648         bus_space_tag_t regt = sc->sc_regt;
  649         bus_space_handle_t regh = sc->sc_regh;
  650         struct ifnet *ifp = &sc->sc_ec.ec_if;
  651         u_char isr;
  652 #if NRND > 0
  653         u_char rndisr;
  654 #endif
  655 
  656         if (sc->sc_enabled == 0 ||
  657             !device_is_active(sc->sc_dev))
  658                 return (0);
  659 
  660         /* Set NIC to page 0 registers. */
  661         NIC_BARRIER(regt, regh);
  662         NIC_PUT(regt, regh, ED_P0_CR,
  663             sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
  664         NIC_BARRIER(regt, regh);
  665 
  666         isr = NIC_GET(regt, regh, ED_P0_ISR);
  667         if (!isr)
  668                 return (0);
  669 
  670 #if NRND > 0
  671         rndisr = isr;
  672 #endif
  673 
  674         /* Loop until there are no more new interrupts. */
  675         for (;;) {
  676                 /*
  677                  * Reset all the bits that we are 'acknowledging' by writing a
  678                  * '1' to each bit position that was set.
  679                  * (Writing a '1' *clears* the bit.)
  680                  */
  681                 NIC_PUT(regt, regh, ED_P0_ISR, isr);
  682 
  683                 /* Work around for AX88190 bug */
  684                 if ((sc->sc_flags & DP8390_DO_AX88190_WORKAROUND) != 0)
  685                         while ((NIC_GET(regt, regh, ED_P0_ISR) & isr) != 0) {
  686                                 NIC_PUT(regt, regh, ED_P0_ISR, 0);
  687                                 NIC_PUT(regt, regh, ED_P0_ISR, isr);
  688                         }
  689 
  690                 /*
  691                  * Handle transmitter interrupts.  Handle these first because
  692                  * the receiver will reset the board under some conditions.
  693                  *
  694                  * If the chip was reset while a packet was transmitting, it
  695                  * may still deliver a TX interrupt.  In this case, just ignore
  696                  * the interrupt.
  697                  */
  698                 if (isr & (ED_ISR_PTX | ED_ISR_TXE) &&
  699                     sc->txb_inuse != 0) {
  700                         u_char collisions =
  701                             NIC_GET(regt, regh, ED_P0_NCR) & 0x0f;
  702 
  703                         /*
  704                          * Check for transmit error.  If a TX completed with an
  705                          * error, we end up throwing the packet away.  Really
  706                          * the only error that is possible is excessive
  707                          * collisions, and in this case it is best to allow the
  708                          * automatic mechanisms of TCP to backoff the flow.  Of
  709                          * course, with UDP we're screwed, but this is expected
  710                          * when a network is heavily loaded.
  711                          */
  712                         if (isr & ED_ISR_TXE) {
  713                                 /*
  714                                  * Excessive collisions (16).
  715                                  */
  716                                 if ((NIC_GET(regt, regh, ED_P0_TSR)
  717                                     & ED_TSR_ABT) && (collisions == 0)) {
  718                                         /*
  719                                          * When collisions total 16, the P0_NCR
  720                                          * will indicate 0, and the TSR_ABT is
  721                                          * set.
  722                                          */
  723                                         collisions = 16;
  724                                 }
  725 
  726                                 /* Update output errors counter. */
  727                                 ++ifp->if_oerrors;
  728                         } else {
  729                                 /*
  730                                  * Throw away the non-error status bits.
  731                                  *
  732                                  * XXX
  733                                  * It may be useful to detect loss of carrier
  734                                  * and late collisions here.
  735                                  */
  736                                 (void)NIC_GET(regt, regh, ED_P0_TSR);
  737 
  738                                 /*
  739                                  * Update total number of successfully
  740                                  * transmitted packets.
  741                                  */
  742                                 ++ifp->if_opackets;
  743                         }
  744 
  745                         /* Clear watchdog timer. */
  746                         ifp->if_timer = 0;
  747                         ifp->if_flags &= ~IFF_OACTIVE;
  748 
  749                         /*
  750                          * Add in total number of collisions on last
  751                          * transmission.
  752                          */
  753                         ifp->if_collisions += collisions;
  754 
  755                         /*
  756                          * Decrement buffer in-use count if not zero (can only
  757                          * be zero if a transmitter interrupt occurred while not
  758                          * actually transmitting).
  759                          * If data is ready to transmit, start it transmitting,
  760                          * otherwise defer until after handling receiver.
  761                          */
  762                         if (--sc->txb_inuse != 0)
  763                                 dp8390_xmit(sc);
  764                 }
  765 
  766                 /* Handle receiver interrupts. */
  767                 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
  768                         /*
  769                          * Overwrite warning.  In order to make sure that a
  770                          * lockup of the local DMA hasn't occurred, we reset
  771                          * and re-init the NIC.  The NSC manual suggests only a
  772                          * partial reset/re-init is necessary - but some chips
  773                          * seem to want more.  The DMA lockup has been seen
  774                          * only with early rev chips - Methinks this bug was
  775                          * fixed in later revs.  -DG
  776                          */
  777                         if (isr & ED_ISR_OVW) {
  778                                 ++ifp->if_ierrors;
  779 #ifdef DIAGNOSTIC
  780                                 log(LOG_WARNING, "%s: warning - receiver "
  781                                     "ring buffer overrun\n",
  782                                     device_xname(sc->sc_dev));
  783 #endif
  784                                 /* Stop/reset/re-init NIC. */
  785                                 dp8390_reset(sc);
  786                         } else {
  787                                 /*
  788                                  * Receiver Error.  One or more of: CRC error,
  789                                  * frame alignment error FIFO overrun, or
  790                                  * missed packet.
  791                                  */
  792                                 if (isr & ED_ISR_RXE) {
  793                                         ++ifp->if_ierrors;
  794 #ifdef DEBUG
  795                                         if (dp8390_debug) {
  796                                                 printf("%s: receive error %x\n",
  797                                                     device_xname(sc->sc_dev),
  798                                                     NIC_GET(regt, regh,
  799                                                         ED_P0_RSR));
  800                                         }
  801 #endif
  802                                 }
  803 
  804                                 /*
  805                                  * Go get the packet(s)
  806                                  * XXX - Doing this on an error is dubious
  807                                  * because there shouldn't be any data to get
  808                                  * (we've configured the interface to not
  809                                  * accept packets with errors).
  810                                  */
  811                                 if (sc->recv_int)
  812                                         (*sc->recv_int)(sc);
  813                                 else
  814                                         dp8390_rint(sc);
  815                         }
  816                 }
  817 
  818                 /*
  819                  * If it looks like the transmitter can take more data, attempt
  820                  * to start output on the interface.  This is done after
  821                  * handling the receiver to give the receiver priority.
  822                  */
  823                 dp8390_start(ifp);
  824 
  825                 /*
  826                  * Return NIC CR to standard state: page 0, remote DMA
  827                  * complete, start (toggling the TXP bit off, even if was just
  828                  * set in the transmit routine, is *okay* - it is 'edge'
  829                  * triggered from low to high).
  830                  */
  831                 NIC_BARRIER(regt, regh);
  832                 NIC_PUT(regt, regh, ED_P0_CR,
  833                     sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
  834                 NIC_BARRIER(regt, regh);
  835 
  836                 /*
  837                  * If the Network Talley Counters overflow, read them to reset
  838                  * them.  It appears that old 8390's won't clear the ISR flag
  839                  * otherwise - resulting in an infinite loop.
  840                  */
  841                 if (isr & ED_ISR_CNT) {
  842                         (void)NIC_GET(regt, regh, ED_P0_CNTR0);
  843                         (void)NIC_GET(regt, regh, ED_P0_CNTR1);
  844                         (void)NIC_GET(regt, regh, ED_P0_CNTR2);
  845                 }
  846 
  847                 isr = NIC_GET(regt, regh, ED_P0_ISR);
  848                 if (!isr)
  849                         goto out;
  850         }
  851 
  852  out:
  853 #if NRND > 0
  854         rnd_add_uint32(&sc->rnd_source, rndisr);
  855 #endif
  856         return (1);
  857 }
  858 
  859 /*
  860  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
  861  */
  862 int
  863 dp8390_ioctl(ifp, cmd, data)
  864         struct ifnet *ifp;
  865         u_long cmd;
  866         void *data;
  867 {
  868         struct dp8390_softc *sc = ifp->if_softc;
  869         struct ifaddr *ifa = (struct ifaddr *) data;
  870         struct ifreq *ifr = (struct ifreq *) data;
  871         int s, error = 0;
  872 
  873         s = splnet();
  874 
  875         switch (cmd) {
  876 
  877         case SIOCSIFADDR:
  878                 if ((error = dp8390_enable(sc)) != 0)
  879                         break;
  880                 ifp->if_flags |= IFF_UP;
  881 
  882                 switch (ifa->ifa_addr->sa_family) {
  883 #ifdef INET
  884                 case AF_INET:
  885                         dp8390_init(sc);
  886                         arp_ifinit(ifp, ifa);
  887                         break;
  888 #endif
  889                 default:
  890                         dp8390_init(sc);
  891                         break;
  892                 }
  893                 break;
  894 
  895         case SIOCSIFFLAGS:
  896                 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
  897                 case IFF_RUNNING:
  898                         /*
  899                          * If interface is marked down and it is running, then
  900                          * stop it.
  901                          */
  902                         dp8390_stop(sc);
  903                         ifp->if_flags &= ~IFF_RUNNING;
  904                         dp8390_disable(sc);
  905                         break;
  906                 case IFF_UP:
  907                         /*
  908                          * If interface is marked up and it is stopped, then
  909                          * start it.
  910                          */
  911                         if ((error = dp8390_enable(sc)) != 0)
  912                                 break;
  913                         dp8390_init(sc);
  914                         break;
  915                 case IFF_UP|IFF_RUNNING:
  916                         /*
  917                          * Reset the interface to pick up changes in any other
  918                          * flags that affect hardware registers.
  919                          */
  920                         dp8390_stop(sc);
  921                         dp8390_init(sc);
  922                         break;
  923                 default:
  924                         break;
  925                 }
  926                 break;
  927 
  928         case SIOCADDMULTI:
  929         case SIOCDELMULTI:
  930                 if (sc->sc_enabled == 0) {
  931                         error = EIO;
  932                         break;
  933                 }
  934 
  935                 /* Update our multicast list. */
  936                 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
  937                         /*
  938                          * Multicast list has changed; set the hardware filter
  939                          * accordingly.
  940                          */
  941                         if (ifp->if_flags & IFF_RUNNING) {
  942                                 dp8390_stop(sc); /* XXX for ds_setmcaf? */
  943                                 dp8390_init(sc);
  944                         }
  945                         error = 0;
  946                 }
  947                 break;
  948 
  949         case SIOCGIFMEDIA:
  950         case SIOCSIFMEDIA:
  951                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
  952                 break;
  953 
  954         default:
  955                 error = ENODEV;
  956                 break;
  957         }
  958 
  959         splx(s);
  960         return (error);
  961 }
  962 
  963 /*
  964  * Retrieve packet from buffer memory and send to the next level up via
  965  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
  966  */
  967 void
  968 dp8390_read(sc, buf, len)
  969         struct dp8390_softc *sc;
  970         int buf;
  971         u_short len;
  972 {
  973         struct ifnet *ifp = &sc->sc_ec.ec_if;
  974         struct mbuf *m;
  975 
  976         /* Pull packet off interface. */
  977         m = dp8390_get(sc, buf, len);
  978         if (m == 0) {
  979                 ifp->if_ierrors++;
  980                 return;
  981         }
  982 
  983         ifp->if_ipackets++;
  984 
  985 #if NBPFILTER > 0
  986         /*
  987          * Check if there's a BPF listener on this interface.
  988          * If so, hand off the raw packet to bpf.
  989          */
  990         if (ifp->if_bpf)
  991                 bpf_mtap(ifp->if_bpf, m);
  992 #endif
  993 
  994         (*ifp->if_input)(ifp, m);
  995 }
  996 
  997 
  998 /*
  999  * Supporting routines.
 1000  */
 1001 
 1002 /*
 1003  * Compute the multicast address filter from the list of multicast addresses we
 1004  * need to listen to.
 1005  */
 1006 void
 1007 dp8390_getmcaf(ec, af)
 1008         struct ethercom *ec;
 1009         u_int8_t *af;
 1010 {
 1011         struct ifnet *ifp = &ec->ec_if;
 1012         struct ether_multi *enm;
 1013         u_int32_t crc;
 1014         int i;
 1015         struct ether_multistep step;
 1016 
 1017         /*
 1018          * Set up multicast address filter by passing all multicast addresses
 1019          * through a crc generator, and then using the high order 6 bits as an
 1020          * index into the 64 bit logical address filter.  The high order bit
 1021          * selects the word, while the rest of the bits select the bit within
 1022          * the word.
 1023          */
 1024 
 1025         if (ifp->if_flags & IFF_PROMISC) {
 1026                 ifp->if_flags |= IFF_ALLMULTI;
 1027                 for (i = 0; i < 8; i++)
 1028                         af[i] = 0xff;
 1029                 return;
 1030         }
 1031         for (i = 0; i < 8; i++)
 1032                 af[i] = 0;
 1033         ETHER_FIRST_MULTI(step, ec, enm);
 1034         while (enm != NULL) {
 1035                 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
 1036                     sizeof(enm->enm_addrlo)) != 0) {
 1037                         /*
 1038                          * We must listen to a range of multicast addresses.
 1039                          * For now, just accept all multicasts, rather than
 1040                          * trying to set only those filter bits needed to match
 1041                          * the range.  (At this time, the only use of address
 1042                          * ranges is for IP multicast routing, for which the
 1043                          * range is big enough to require all bits set.)
 1044                          */
 1045                         ifp->if_flags |= IFF_ALLMULTI;
 1046                         for (i = 0; i < 8; i++)
 1047                                 af[i] = 0xff;
 1048                         return;
 1049                 }
 1050 
 1051                 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
 1052 
 1053                 /* Just want the 6 most significant bits. */
 1054                 crc >>= 26;
 1055 
 1056                 /* Turn on the corresponding bit in the filter. */
 1057                 af[crc >> 3] |= 1 << (crc & 0x7);
 1058 
 1059                 ETHER_NEXT_MULTI(step, enm);
 1060         }
 1061         ifp->if_flags &= ~IFF_ALLMULTI;
 1062 }
 1063 
 1064 /*
 1065  * Copy data from receive buffer to a new mbuf chain allocating mbufs
 1066  * as needed.  Return pointer to first mbuf in chain.
 1067  * sc = dp8390 info (softc)
 1068  * src = pointer in dp8390 ring buffer
 1069  * total_len = amount of data to copy
 1070  */
 1071 struct mbuf *
 1072 dp8390_get(sc, src, total_len)
 1073         struct dp8390_softc *sc;
 1074         int src;
 1075         u_short total_len;
 1076 {
 1077         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1078         struct mbuf *m, *m0, *newm;
 1079         u_short len;
 1080 
 1081         MGETHDR(m0, M_DONTWAIT, MT_DATA);
 1082         if (m0 == 0)
 1083                 return (0);
 1084         m0->m_pkthdr.rcvif = ifp;
 1085         m0->m_pkthdr.len = total_len;
 1086         len = MHLEN;
 1087         m = m0;
 1088 
 1089         while (total_len > 0) {
 1090                 if (total_len >= MINCLSIZE) {
 1091                         MCLGET(m, M_DONTWAIT);
 1092                         if ((m->m_flags & M_EXT) == 0)
 1093                                 goto bad;
 1094                         len = MCLBYTES;
 1095                 }
 1096 
 1097                 /*
 1098                  * Make sure the data after the Ethernet header is aligned.
 1099                  */
 1100                 if (m == m0) {
 1101                         char *newdata = (char *)
 1102                             ALIGN(m->m_data + sizeof(struct ether_header)) -
 1103                             sizeof(struct ether_header);
 1104                         len -= newdata - m->m_data;
 1105                         m->m_data = newdata;
 1106                 }
 1107 
 1108                 m->m_len = len = min(total_len, len);
 1109                 if (sc->ring_copy)
 1110                         src = (*sc->ring_copy)(sc, src, mtod(m, void *), len);
 1111                 else
 1112                         src = dp8390_ring_copy(sc, src, mtod(m, void *), len);
 1113 
 1114                 total_len -= len;
 1115                 if (total_len > 0) {
 1116                         MGET(newm, M_DONTWAIT, MT_DATA);
 1117                         if (newm == 0)
 1118                                 goto bad;
 1119                         len = MLEN;
 1120                         m = m->m_next = newm;
 1121                 }
 1122         }
 1123 
 1124         return (m0);
 1125 
 1126 bad:
 1127         m_freem(m0);
 1128         return (0);
 1129 }
 1130 
 1131 
 1132 /*
 1133  * Default driver support functions.
 1134  *
 1135  * NOTE: all support functions assume 8-bit shared memory.
 1136  */
 1137 /*
 1138  * Zero NIC buffer memory and verify that it is clear.
 1139  */
 1140 static int
 1141 dp8390_test_mem(sc)
 1142         struct dp8390_softc *sc;
 1143 {
 1144         bus_space_tag_t buft = sc->sc_buft;
 1145         bus_space_handle_t bufh = sc->sc_bufh;
 1146         int i;
 1147 
 1148         bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
 1149 
 1150         for (i = 0; i < sc->mem_size; ++i) {
 1151                 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
 1152                         printf(": failed to clear NIC buffer at offset %x - "
 1153                             "check configuration\n", (sc->mem_start + i));
 1154                         return 1;
 1155                 }
 1156         }
 1157 
 1158         return 0;
 1159 }
 1160 
 1161 /*
 1162  * Read a packet header from the ring, given the source offset.
 1163  */
 1164 static inline void
 1165 dp8390_read_hdr(sc, src, hdrp)
 1166         struct dp8390_softc *sc;
 1167         int src;
 1168         struct dp8390_ring *hdrp;
 1169 {
 1170         bus_space_tag_t buft = sc->sc_buft;
 1171         bus_space_handle_t bufh = sc->sc_bufh;
 1172 
 1173         /*
 1174          * The byte count includes a 4 byte header that was added by
 1175          * the NIC.
 1176          */
 1177         hdrp->rsr = bus_space_read_1(buft, bufh, src);
 1178         hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
 1179         hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
 1180             (bus_space_read_1(buft, bufh, src + 3) << 8);
 1181 }
 1182 
 1183 /*
 1184  * Copy `amount' bytes from a packet in the ring buffer to a linear
 1185  * destination buffer, given a source offset and destination address.
 1186  * Takes into account ring-wrap.
 1187  */
 1188 static inline int
 1189 dp8390_ring_copy(sc, src, dst, amount)
 1190         struct dp8390_softc *sc;
 1191         int src;
 1192         void *dst;
 1193         u_short amount;
 1194 {
 1195         bus_space_tag_t buft = sc->sc_buft;
 1196         bus_space_handle_t bufh = sc->sc_bufh;
 1197         u_short tmp_amount;
 1198 
 1199         /* Does copy wrap to lower addr in ring buffer? */
 1200         if (src + amount > sc->mem_end) {
 1201                 tmp_amount = sc->mem_end - src;
 1202 
 1203                 /* Copy amount up to end of NIC memory. */
 1204                 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
 1205 
 1206                 amount -= tmp_amount;
 1207                 src = sc->mem_ring;
 1208                 dst = (char *)dst + tmp_amount;
 1209         }
 1210         bus_space_read_region_1(buft, bufh, src, dst, amount);
 1211 
 1212         return (src + amount);
 1213 }
 1214 
 1215 /*
 1216  * Copy a packet from an mbuf to the transmit buffer on the card.
 1217  *
 1218  * Currently uses an extra buffer/extra memory copy, unless the whole
 1219  * packet fits in one mbuf.
 1220  */
 1221 static inline int
 1222 dp8390_write_mbuf(sc, m, buf)
 1223         struct dp8390_softc *sc;
 1224         struct mbuf *m;
 1225         int buf;
 1226 {
 1227         bus_space_tag_t buft = sc->sc_buft;
 1228         bus_space_handle_t bufh = sc->sc_bufh;
 1229         u_char *data;
 1230         int len, totlen = 0;
 1231 
 1232         for (; m ; m = m->m_next) {
 1233                 data = mtod(m, u_char *);
 1234                 len = m->m_len;
 1235                 if (len > 0) {
 1236                         bus_space_write_region_1(buft, bufh, buf, data, len);
 1237                         totlen += len;
 1238                         buf += len;
 1239                 }
 1240         }
 1241         if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
 1242                 bus_space_set_region_1(buft, bufh, buf, 0,
 1243                     ETHER_MIN_LEN - ETHER_CRC_LEN - totlen);
 1244                 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
 1245         }
 1246         return (totlen);
 1247 }
 1248 
 1249 /*
 1250  * Enable power on the interface.
 1251  */
 1252 int
 1253 dp8390_enable(sc)
 1254         struct dp8390_softc *sc;
 1255 {
 1256 
 1257         if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
 1258                 if ((*sc->sc_enable)(sc) != 0) {
 1259                         aprint_error_dev(sc->sc_dev,
 1260                             "device enable failed\n");
 1261                         return (EIO);
 1262                 }
 1263         }
 1264 
 1265         sc->sc_enabled = 1;
 1266         return (0);
 1267 }
 1268 
 1269 /*
 1270  * Disable power on the interface.
 1271  */
 1272 void
 1273 dp8390_disable(sc)
 1274         struct dp8390_softc *sc;
 1275 {
 1276 
 1277         if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
 1278                 (*sc->sc_disable)(sc);
 1279                 sc->sc_enabled = 0;
 1280         }
 1281 }
 1282 
 1283 int
 1284 dp8390_activate(self, act)
 1285         struct device *self;
 1286         enum devact act;
 1287 {
 1288         struct dp8390_softc *sc = (struct dp8390_softc *)self;
 1289         int rv = 0, s;
 1290 
 1291         s = splnet();
 1292         switch (act) {
 1293         case DVACT_ACTIVATE:
 1294                 rv = EOPNOTSUPP;
 1295                 break;
 1296 
 1297         case DVACT_DEACTIVATE:
 1298                 if_deactivate(&sc->sc_ec.ec_if);
 1299                 break;
 1300         }
 1301         splx(s);
 1302         return (rv);
 1303 }
 1304 
 1305 int
 1306 dp8390_detach(struct dp8390_softc *sc, int flags)
 1307 {
 1308         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1309 
 1310         /* Succeed now if there's no work to do. */
 1311         if ((sc->sc_flags & DP8390_ATTACHED) == 0)
 1312                 return (0);
 1313 
 1314         /* dp8390_disable() checks sc->sc_enabled */
 1315         dp8390_disable(sc);
 1316 
 1317         if (sc->sc_media_fini != NULL)
 1318                 (*sc->sc_media_fini)(sc);
 1319 
 1320         /* Delete all remaining media. */
 1321         ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
 1322 
 1323 #if NRND > 0
 1324         rnd_detach_source(&sc->rnd_source);
 1325 #endif
 1326         ether_ifdetach(ifp);
 1327         if_detach(ifp);
 1328 
 1329         return (0);
 1330 }
 1331 
 1332 #ifdef IPKDB_DP8390
 1333 static void dp8390_ipkdb_hwinit(struct ipkdb_if *);
 1334 static void dp8390_ipkdb_init(struct ipkdb_if *);
 1335 static void dp8390_ipkdb_leave(struct ipkdb_if *);
 1336 static int dp8390_ipkdb_rcv(struct ipkdb_if *, u_char *, int);
 1337 static void dp8390_ipkdb_send(struct ipkdb_if *, u_char *, int);
 1338 
 1339 /*
 1340  * This is essentially similar to dp8390_config above.
 1341  */
 1342 int
 1343 dp8390_ipkdb_attach(kip)
 1344         struct ipkdb_if *kip;
 1345 {
 1346         struct dp8390_softc *sc = kip->port;
 1347 
 1348         if (sc->mem_size < 8192 * 2)
 1349                 sc->txb_cnt = 1;
 1350         else if (sc->mem_size < 8192 * 3)
 1351                 sc->txb_cnt = 2;
 1352         else
 1353                 sc->txb_cnt = 3;
 1354 
 1355         sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
 1356         sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
 1357         sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
 1358         sc->mem_ring = sc->mem_start +
 1359             ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
 1360         sc->mem_end = sc->mem_start + sc->mem_size;
 1361 
 1362         dp8390_stop(sc);
 1363 
 1364         kip->start = dp8390_ipkdb_init;
 1365         kip->leave = dp8390_ipkdb_leave;
 1366         kip->receive = dp8390_ipkdb_rcv;
 1367         kip->send = dp8390_ipkdb_send;
 1368 
 1369         return 0;
 1370 }
 1371 
 1372 /*
 1373  * Similar to dp8390_init above.
 1374  */
 1375 static void
 1376 dp8390_ipkdb_hwinit(kip)
 1377         struct ipkdb_if *kip;
 1378 {
 1379         struct dp8390_softc *sc = kip->port;
 1380         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1381         bus_space_tag_t regt = sc->sc_regt;
 1382         bus_space_handle_t regh = sc->sc_regh;
 1383         int i;
 1384 
 1385         sc->txb_inuse = 0;
 1386         sc->txb_new = 0;
 1387         sc->txb_next_tx = 0;
 1388         dp8390_stop(sc);
 1389 
 1390         if (sc->dcr_reg & ED_DCR_LS)
 1391                 NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
 1392         else
 1393                 NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
 1394         NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
 1395         NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
 1396         NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
 1397         NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
 1398         if (sc->is790)
 1399                 NIC_PUT(regt, regh, 0x09, 0);
 1400         NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
 1401         NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
 1402         NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
 1403         NIC_PUT(regt, regh, ED_P0_IMR, 0);
 1404         NIC_BARRIER(regt, regh);
 1405         NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
 1406 
 1407         NIC_BARRIER(regt, regh);
 1408         NIC_PUT(regt, regh, ED_P0_CR,
 1409                 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
 1410         NIC_BARRIER(regt, regh);
 1411 
 1412         for (i = 0; i < sizeof kip->myenetaddr; i++)
 1413                 NIC_PUT(regt, regh, ED_P1_PAR0 + i, kip->myenetaddr[i]);
 1414         /* multicast filter? */
 1415 
 1416         sc->next_packet = sc->rec_page_start + 1;
 1417         NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
 1418 
 1419         NIC_BARRIER(regt, regh);
 1420         NIC_PUT(regt, regh, ED_P1_CR,
 1421                 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
 1422         NIC_BARRIER(regt, regh);
 1423 
 1424         /* promiscuous mode? */
 1425         NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_AB | ED_RCR_AM | sc->rcr_proto);
 1426         NIC_PUT(regt, regh, ED_P0_TCR, 0);
 1427 
 1428         /* card-specific initialization? */
 1429 
 1430         NIC_BARRIER(regt, regh);
 1431         NIC_PUT(regt, regh, ED_P0_CR,
 1432                 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
 1433 
 1434         ifp->if_flags &= ~IFF_OACTIVE;
 1435 }
 1436 
 1437 static void
 1438 dp8390_ipkdb_init(kip)
 1439         struct ipkdb_if *kip;
 1440 {
 1441         struct dp8390_softc *sc = kip->port;
 1442         bus_space_tag_t regt = sc->sc_regt;
 1443         bus_space_handle_t regh = sc->sc_regh;
 1444         u_char cmd;
 1445 
 1446         cmd = NIC_GET(regt, regh, ED_P0_CR) & ~(ED_CR_PAGE_3 | ED_CR_STA);
 1447 
 1448         /* Select page 0 */
 1449         NIC_BARRIER(regt, regh);
 1450         NIC_PUT(regt, regh, ED_P0_CR, cmd | ED_CR_PAGE_0 | ED_CR_STP);
 1451         NIC_BARRIER(regt, regh);
 1452 
 1453         /* If not started, init chip */
 1454         if (cmd & ED_CR_STP)
 1455                 dp8390_ipkdb_hwinit(kip);
 1456 
 1457         /* If output active, wait for packets to drain */
 1458         while (sc->txb_inuse) {
 1459                 while (!(cmd = (NIC_GET(regt, regh, ED_P0_ISR)
 1460                                 & (ED_ISR_PTX | ED_ISR_TXE))))
 1461                         DELAY(1);
 1462                 NIC_PUT(regt, regh, ED_P0_ISR, cmd);
 1463                 if (--sc->txb_inuse)
 1464                         dp8390_xmit(sc);
 1465         }
 1466 }
 1467 
 1468 static void
 1469 dp8390_ipkdb_leave(kip)
 1470         struct ipkdb_if *kip;
 1471 {
 1472         struct dp8390_softc *sc = kip->port;
 1473         struct ifnet *ifp = &sc->sc_ec.ec_if;
 1474 
 1475         ifp->if_timer = 0;
 1476 }
 1477 
 1478 /*
 1479  * Similar to dp8390_intr above.
 1480  */
 1481 static int
 1482 dp8390_ipkdb_rcv(kip, buf, poll)
 1483         struct ipkdb_if *kip;
 1484         u_char *buf;
 1485         int poll;
 1486 {
 1487         struct dp8390_softc *sc = kip->port;
 1488         bus_space_tag_t regt = sc->sc_regt;
 1489         bus_space_handle_t regh = sc->sc_regh;
 1490         u_char bnry, current, isr;
 1491         int len, nlen, packet_ptr;
 1492         struct dp8390_ring packet_hdr;
 1493 
 1494         /* Switch to page 0. */
 1495         NIC_BARRIER(regt, regh);
 1496         NIC_PUT(regt, regh, ED_P0_CR,
 1497                 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
 1498         NIC_BARRIER(regt, regh);
 1499 
 1500         while (1) {
 1501                 isr = NIC_GET(regt, regh, ED_P0_ISR);
 1502                 NIC_PUT(regt, regh, ED_P0_ISR, isr);
 1503 
 1504                 if (isr & (ED_ISR_PRX | ED_ISR_TXE)) {
 1505                         NIC_GET(regt, regh, ED_P0_NCR);
 1506                         NIC_GET(regt, regh, ED_P0_TSR);
 1507                 }
 1508 
 1509                 if (isr & ED_ISR_OVW) {
 1510                         dp8390_ipkdb_hwinit(kip);
 1511                         continue;
 1512                 }
 1513 
 1514                 if (isr & ED_ISR_CNT) {
 1515                         NIC_GET(regt, regh, ED_P0_CNTR0);
 1516                         NIC_GET(regt, regh, ED_P0_CNTR1);
 1517                         NIC_GET(regt, regh, ED_P0_CNTR2);
 1518                 }
 1519 
 1520                 /* Similar to dp8390_rint above. */
 1521                 NIC_BARRIER(regt, regh);
 1522                 NIC_PUT(regt, regh, ED_P0_CR,
 1523                         sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
 1524                 NIC_BARRIER(regt, regh);
 1525 
 1526                 current = NIC_GET(regt, regh, ED_P1_CURR);
 1527 
 1528                 NIC_BARRIER(regt, regh);
 1529                 NIC_PUT(regt, regh, ED_P1_CR,
 1530                         sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
 1531                 NIC_BARRIER(regt, regh);
 1532 
 1533                 if (sc->next_packet == current) {
 1534                         if (poll)
 1535                                 return 0;
 1536                         continue;
 1537                 }
 1538 
 1539                 packet_ptr = sc->mem_ring
 1540                         + ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
 1541                 sc->read_hdr(sc, packet_ptr, &packet_hdr);
 1542                 len = packet_hdr.count;
 1543                 nlen = packet_hdr.next_packet - sc->next_packet;
 1544                 if (nlen < 0)
 1545                         nlen += sc->rec_page_stop - sc->rec_page_start;
 1546                 nlen--;
 1547                 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
 1548                         nlen--;
 1549                 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
 1550                 len -= sizeof(packet_hdr);
 1551 
 1552                 if (len <= ETHERMTU
 1553                     && packet_hdr.next_packet >= sc->rec_page_start
 1554                     && packet_hdr.next_packet < sc->rec_page_stop) {
 1555                         sc->ring_copy(sc, packet_ptr + sizeof(packet_hdr),
 1556                                 buf, len);
 1557                         sc->next_packet = packet_hdr.next_packet;
 1558                         bnry = sc->next_packet - 1;
 1559                         if (bnry < sc->rec_page_start)
 1560                                 bnry = sc->rec_page_stop - 1;
 1561                         NIC_PUT(regt, regh, ED_P0_BNRY, bnry);
 1562                         return len;
 1563                 }
 1564 
 1565                 dp8390_ipkdb_hwinit(kip);
 1566         }
 1567 }
 1568 
 1569 static void
 1570 dp8390_ipkdb_send(kip, buf, l)
 1571         struct ipkdb_if *kip;
 1572         u_char *buf;
 1573         int l;
 1574 {
 1575         struct dp8390_softc *sc = kip->port;
 1576         bus_space_tag_t regt = sc->sc_regt;
 1577         bus_space_handle_t regh = sc->sc_regh;
 1578         struct mbuf mb;
 1579 
 1580         mb.m_next = NULL;
 1581         mb.m_pkthdr.len = mb.m_len = l;
 1582         mb.m_data = buf;
 1583         mb.m_flags = M_EXT | M_PKTHDR;
 1584         mb.m_type = MT_DATA;
 1585 
 1586         l = sc->write_mbuf(sc, &mb,
 1587             sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT));
 1588         sc->txb_len[sc->txb_new] = max(l, ETHER_MIN_LEN - ETHER_CRC_LEN);
 1589 
 1590         if (++sc->txb_new == sc->txb_cnt)
 1591                 sc->txb_new = 0;
 1592 
 1593         sc->txb_inuse++;
 1594         dp8390_xmit(sc);
 1595 
 1596         while (!(NIC_GET(regt, regh, ED_P0_ISR) & (ED_ISR_PTX | ED_ISR_TXE)))
 1597                 DELAY(1);
 1598 
 1599         sc->txb_inuse--;
 1600 }
 1601 #endif

Cache object: 5b8f83e60465ce76b0ace32c3631284a


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