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_lnc.c

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

    1 /*-
    2  * Copyright (c) 1994-1998
    3  *      Paul Richards.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer,
   10  *    verbatim and that no modifications are made prior to this
   11  *    point in the file.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. All advertising materials mentioning features or use of this software
   16  *    must display the following acknowledgement:
   17  *      This product includes software developed by Paul Richards.
   18  * 4. The name Paul Richards may not be used to endorse or promote products
   19  *    derived from this software without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY PAUL RICHARDS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL PAUL RICHARDS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  * $FreeBSD$
   34  */
   35 
   36 /*
   37 #define DIAGNOSTIC
   38 #define DEBUG
   39  *
   40  * TODO ----
   41  *
   42  * This driver will need bounce buffer support when dma'ing to mbufs above the
   43  * 16Mb mark.
   44  *
   45  * Check all the XXX comments -- some of them are just things I've left
   46  * unfinished rather than "difficult" problems that were hacked around.
   47  *
   48  * Check log settings.
   49  *
   50  * Check how all the arpcom flags get set and used.
   51  *
   52  * Re-inline and re-static all routines after debugging.
   53  *
   54  * Remember to assign iobase in SHMEM probe routines.
   55  *
   56  * Replace all occurences of LANCE-controller-card etc in prints by the name
   57  * strings of the appropriate type -- nifty window dressing
   58  *
   59  * Add DEPCA support -- mostly done.
   60  *
   61  */
   62 
   63 #include "pci.h"
   64 #include "lnc.h"
   65 #if NLNC > 0
   66 
   67 #include "bpfilter.h"
   68 #include "opt_inet.h"
   69 
   70 /* Some defines that should really be in generic locations */
   71 #define FCS_LEN 4
   72 #define MULTICAST_FILTER_LEN 8
   73 
   74 #include <sys/param.h>
   75 #include <sys/systm.h>
   76 #include <sys/conf.h>
   77 #include <sys/sockio.h>
   78 #include <sys/malloc.h>
   79 #include <sys/mbuf.h>
   80 #include <sys/socket.h>
   81 #include <sys/syslog.h>
   82 
   83 #include <net/if.h>
   84 #include <net/if_dl.h>
   85 #include <net/if_types.h>
   86 #ifdef INET
   87 #include <netinet/in.h>
   88 #include <netinet/if_ether.h>
   89 #endif
   90 
   91 #if NBPFILTER > 0
   92 #include <net/bpf.h>
   93 #endif
   94 
   95 #include "opt_bdg.h"
   96 #ifdef BRIDGE
   97 #include <net/bridge.h>
   98 #endif
   99 
  100 #ifdef PC98
  101 #include <machine/clock.h>
  102 #endif
  103 #include <machine/md_var.h>
  104 
  105 #include <i386/isa/isa_device.h>
  106 #include <i386/isa/if_lnc.h>
  107 
  108 struct lnc_softc {
  109         struct arpcom arpcom;               /* see ../../net/if_arp.h */
  110         struct nic_info nic;                /* NIC specific info */
  111         int nrdre;
  112         struct host_ring_entry *recv_ring;  /* start of alloc'd mem */
  113         int recv_next;
  114         int ntdre;
  115         struct host_ring_entry *trans_ring;
  116         int trans_next;
  117         struct init_block *init_block;      /* Initialisation block */
  118         int pending_transmits;        /* No. of transmit descriptors in use */
  119         int next_to_send;
  120         struct mbuf *mbufs;
  121         int mbuf_count;
  122         int flags;
  123         int rap;
  124         int rdp;
  125         int bdp;
  126 #ifdef DEBUG
  127         int lnc_debug;
  128 #endif
  129         LNCSTATS_STRUCT
  130 };
  131 
  132 static struct lnc_softc lnc_softc[NLNC];
  133 
  134 static char const * const nic_ident[] = {
  135         "Unknown",
  136         "BICC",
  137         "NE2100",
  138         "DEPCA",
  139         "CNET98S",      /* PC-98 */
  140 };
  141 
  142 static char const * const ic_ident[] = {
  143         "Unknown",
  144         "LANCE",
  145         "C-LANCE",
  146         "PCnet-ISA",
  147         "PCnet-ISA+",
  148         "PCnet-ISA II",
  149         "PCnet-32 VL-Bus",
  150         "PCnet-PCI",
  151         "PCnet-PCI II",
  152         "PCnet-FAST",
  153         "PCnet-FAST+",
  154         "PCnet-Home",
  155         "PCnet-FAST III",
  156 };
  157 
  158 static void lnc_setladrf __P((struct lnc_softc *sc));
  159 static void lnc_stop __P((struct lnc_softc *sc));
  160 static void lnc_reset __P((struct lnc_softc *sc));
  161 static void lnc_free_mbufs __P((struct lnc_softc *sc));
  162 static __inline int alloc_mbuf_cluster __P((struct lnc_softc *sc,
  163                                             struct host_ring_entry *desc));
  164 static __inline struct mbuf *chain_mbufs __P((struct lnc_softc *sc,
  165                                               int start_of_packet,
  166                                               int pkt_len));
  167 static __inline struct mbuf *mbuf_packet __P((struct lnc_softc *sc,
  168                                               int start_of_packet,
  169                                               int pkt_len));
  170 static __inline void lnc_rint __P((struct lnc_softc *sc));
  171 static __inline void lnc_tint __P((struct lnc_softc *sc));
  172 static int lnc_probe __P((struct isa_device *isa_dev));
  173 #ifdef PC98
  174 static int cnet98s_probe __P((struct lnc_softc *sc, unsigned iobase));
  175 #endif
  176 static int ne2100_probe __P((struct lnc_softc *sc, unsigned iobase));
  177 static int bicc_probe __P((struct lnc_softc *sc, unsigned iobase));
  178 static int dec_macaddr_extract __P((u_char ring[], struct lnc_softc *sc));
  179 static int depca_probe __P((struct lnc_softc *sc, unsigned iobase));
  180 static int lance_probe __P((struct lnc_softc *sc));
  181 static int pcnet_probe __P((struct lnc_softc *sc));
  182 static int lnc_attach_sc __P((struct lnc_softc *sc, int unit));
  183 static int lnc_attach __P((struct isa_device *isa_dev));
  184 static void lnc_init __P((struct lnc_softc *sc));
  185 static ointhand2_t lncintr;
  186 static __inline int mbuf_to_buffer __P((struct mbuf *m, char *buffer));
  187 static __inline struct mbuf *chain_to_cluster __P((struct mbuf *m));
  188 static void lnc_start __P((struct ifnet *ifp));
  189 static int lnc_ioctl __P((struct ifnet *ifp, u_long command, caddr_t data));
  190 static void lnc_watchdog __P((struct ifnet *ifp));
  191 #ifdef DEBUG
  192 void lnc_dump_state __P((struct lnc_softc *sc));
  193 void mbuf_dump_chain __P((struct mbuf *m));
  194 #endif
  195 
  196 #if NPCI > 0
  197 void *lnc_attach_ne2100_pci __P((int unit, unsigned iobase));
  198 #endif
  199 void lncintr_sc __P((struct lnc_softc *sc));
  200 
  201 struct isa_driver lncdriver = {lnc_probe, lnc_attach, "lnc"};
  202 
  203 static __inline void
  204 write_csr(struct lnc_softc *sc, u_short port, u_short val)
  205 {
  206         outw(sc->rap, port);
  207         outw(sc->rdp, val);
  208 }
  209 
  210 static __inline u_short
  211 read_csr(struct lnc_softc *sc, u_short port)
  212 {
  213         outw(sc->rap, port);
  214         return (inw(sc->rdp));
  215 }
  216 
  217 static __inline void
  218 write_bcr(struct lnc_softc *sc, u_short port, u_short val)
  219 {
  220         outw(sc->rap, port);
  221         outw(sc->bdp, val);
  222 }
  223 
  224 static __inline u_short
  225 read_bcr(struct lnc_softc *sc, u_short port)
  226 {
  227         outw(sc->rap, port);
  228         return (inw(sc->bdp));
  229 }
  230 
  231 static __inline u_long
  232 ether_crc(const u_char *ether_addr)
  233 {
  234 #define POLYNOMIAL           0xEDB88320UL
  235     u_char i, j, addr;
  236     u_int crc = 0xFFFFFFFFUL;
  237 
  238     for (i = 0; i < ETHER_ADDR_LEN; i++) {
  239         addr = *ether_addr++;
  240         for (j = 0; j < MULTICAST_FILTER_LEN; j++) {
  241             crc = (crc >> 1) ^ (((crc ^ addr) & 1) ? POLYNOMIAL : 0);   
  242             addr >>= 1;
  243         }
  244     }
  245     return crc;
  246 #undef POLYNOMIAL
  247 }
  248 
  249 /*
  250  * Set up the logical address filter for multicast packets
  251  */
  252 static __inline void
  253 lnc_setladrf(struct lnc_softc *sc)
  254 {
  255         struct ifnet *ifp = &sc->arpcom.ac_if;
  256         struct ifmultiaddr *ifma;
  257         u_long index;
  258         int i;
  259 
  260         if (sc->flags & IFF_ALLMULTI) {
  261             for (i=0; i < MULTICAST_FILTER_LEN; i++)
  262                 sc->init_block->ladrf[i] = 0xFF;
  263             return;
  264         }
  265 
  266         /*
  267          * For each multicast address, calculate a crc for that address and
  268          * then use the high order 6 bits of the crc as a hash code where
  269          * bits 3-5 select the byte of the address filter and bits 0-2 select
  270          * the bit within that byte.
  271          */
  272 
  273         bzero(sc->init_block->ladrf, MULTICAST_FILTER_LEN);
  274         for (ifma = ifp->if_multiaddrs.lh_first; ifma;
  275              ifma = ifma->ifma_link.le_next) {
  276                 if (ifma->ifma_addr->sa_family != AF_LINK)
  277                         continue;
  278 
  279                 index = ether_crc(LLADDR((struct sockaddr_dl *)ifma->ifma_addr))
  280                                 >> 26;
  281                 sc->init_block->ladrf[index >> 3] |= 1 << (index & 7);
  282         }
  283 }
  284 
  285 static void
  286 lnc_stop(struct lnc_softc *sc)
  287 {
  288         write_csr(sc, CSR0, STOP);
  289 }
  290 
  291 static void
  292 lnc_reset(struct lnc_softc *sc)
  293 {
  294         lnc_init(sc);
  295 }
  296 
  297 static void
  298 lnc_free_mbufs(struct lnc_softc *sc)
  299 {
  300         int i;
  301 
  302         /*
  303          * We rely on other routines to keep the buff.mbuf field valid. If
  304          * it's not NULL then we assume it points to an allocated mbuf.
  305          */
  306 
  307         for (i = 0; i < NDESC(sc->nrdre); i++)
  308                 if ((sc->recv_ring + i)->buff.mbuf)
  309                         m_free((sc->recv_ring + i)->buff.mbuf);
  310 
  311         for (i = 0; i < NDESC(sc->ntdre); i++)
  312                 if ((sc->trans_ring + i)->buff.mbuf)
  313                         m_free((sc->trans_ring + i)->buff.mbuf);
  314 
  315         if (sc->mbuf_count)
  316                 m_freem(sc->mbufs);
  317 }
  318 
  319 static __inline int
  320 alloc_mbuf_cluster(struct lnc_softc *sc, struct host_ring_entry *desc)
  321 {
  322         register struct mds *md = desc->md;
  323         struct mbuf *m=0;
  324         int addr;
  325 
  326         /* Try and get cluster off local cache */
  327         if (sc->mbuf_count) {
  328                 sc->mbuf_count--;
  329                 m = sc->mbufs;
  330                 sc->mbufs = m->m_next;
  331                 /* XXX m->m_data = m->m_ext.ext_buf;*/
  332         } else {
  333                 MGET(m, M_DONTWAIT, MT_DATA);
  334         if (!m)
  335                         return(1);
  336       MCLGET(m, M_DONTWAIT);
  337         if (!m->m_ext.ext_buf) {
  338                         m_free(m);
  339                         return(1);
  340                 }
  341         }
  342 
  343         desc->buff.mbuf = m;
  344         addr = kvtop(m->m_data);
  345         md->md0 = addr;
  346         md->md1= ((addr >> 16) & 0xff) | OWN;
  347         md->md2 = -(short)(MCLBYTES - sizeof(struct pkthdr));
  348         md->md3 = 0;
  349         return(0);
  350 }
  351 
  352 static __inline struct mbuf *
  353 chain_mbufs(struct lnc_softc *sc, int start_of_packet, int pkt_len)
  354 {
  355         struct mbuf *head, *m;
  356         struct host_ring_entry *desc;
  357 
  358         /*
  359          * Turn head into a pkthdr mbuf --
  360          * assumes a pkthdr type mbuf was
  361          * allocated to the descriptor
  362          * originally.
  363          */
  364 
  365         desc = sc->recv_ring + start_of_packet;
  366 
  367         head = desc->buff.mbuf;
  368         head->m_flags |= M_PKTHDR;
  369 
  370         m = head;
  371         do {
  372                 m = desc->buff.mbuf;
  373                 m->m_len = min((MCLBYTES - sizeof(struct pkthdr)), pkt_len);
  374                 pkt_len -= m->m_len;
  375                 if (alloc_mbuf_cluster(sc, desc))
  376                         return((struct mbuf *)NULL);
  377                 INC_MD_PTR(start_of_packet, sc->nrdre)
  378                 desc = sc->recv_ring + start_of_packet;
  379                 m->m_next = desc->buff.mbuf;
  380         } while (start_of_packet != sc->recv_next);
  381 
  382         m->m_next = 0;
  383         return(head);
  384 }
  385 
  386 static __inline struct mbuf *
  387 mbuf_packet(struct lnc_softc *sc, int start_of_packet, int pkt_len)
  388 {
  389 
  390         struct host_ring_entry *start;
  391         struct mbuf *head,*m,*m_prev;
  392         char *data,*mbuf_data;
  393         short blen;
  394         int amount;
  395 
  396         /* Get a pkthdr mbuf for the start of packet */
  397         MGETHDR(head, M_DONTWAIT, MT_DATA);
  398         if (!head) {
  399                 LNCSTATS(drop_packet)
  400                 return(0);
  401         }
  402 
  403         m = head;
  404         m->m_len = 0;
  405         start = sc->recv_ring + start_of_packet;
  406         /*blen = -(start->md->md2);*/
  407         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
  408         data = start->buff.data;
  409         mbuf_data = m->m_data;
  410 
  411         while (start_of_packet != sc->recv_next) {
  412                 /*
  413                  * If the data left fits in a single buffer then set
  414                  * blen to the size of the data left.
  415                  */
  416                 if (pkt_len < blen)
  417                         blen = pkt_len;
  418 
  419                 /*
  420                  * amount is least of data in current ring buffer and
  421                  * amount of space left in current mbuf.
  422                  */
  423                 amount = min(blen, M_TRAILINGSPACE(m));
  424                 if (amount == 0) {
  425                         /* mbuf must be empty */
  426                         m_prev = m;
  427                         MGET(m, M_DONTWAIT, MT_DATA);
  428                         if (!m) {
  429                                 m_freem(head);
  430                                 return(0);
  431                         }
  432                         if (pkt_len >= MINCLSIZE)
  433                                 MCLGET(m, M_DONTWAIT);
  434                         m->m_len = 0;
  435                         m_prev->m_next = m;
  436                         amount = min(blen, M_TRAILINGSPACE(m));
  437                         mbuf_data = m->m_data;
  438                 }
  439                 bcopy(data, mbuf_data, amount);
  440                 blen -= amount;
  441                 pkt_len -= amount;
  442                 m->m_len += amount;
  443                 data += amount;
  444                 mbuf_data += amount;
  445 
  446                 if (blen == 0) {
  447                         start->md->md1 &= HADR;
  448                         start->md->md1 |= OWN;
  449                         start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
  450                         INC_MD_PTR(start_of_packet, sc->nrdre)
  451                         start = sc->recv_ring + start_of_packet;
  452                         data = start->buff.data;
  453                         /*blen = -(start->md->md2);*/
  454                         blen = RECVBUFSIZE; /* XXX More PCnet-32 crap */
  455                 }
  456         }
  457         return(head);
  458 }
  459 
  460 
  461 static __inline void
  462 lnc_rint(struct lnc_softc *sc)
  463 {
  464         struct host_ring_entry *next, *start;
  465         int start_of_packet;
  466         struct mbuf *head;
  467         struct ether_header *eh;
  468         int lookahead;
  469         int flags;
  470         int pkt_len;
  471 
  472         /*
  473          * The LANCE will issue a RINT interrupt when the ownership of the
  474          * last buffer of a receive packet has been relinquished by the LANCE.
  475          * Therefore, it can be assumed that a complete packet can be found
  476          * before hitting buffers that are still owned by the LANCE, if not
  477          * then there is a bug in the driver that is causing the descriptors
  478          * to get out of sync.
  479          */
  480 
  481 #ifdef DIAGNOSTIC
  482         if ((sc->recv_ring + sc->recv_next)->md->md1 & OWN) {
  483                 int unit = sc->arpcom.ac_if.if_unit;
  484                 log(LOG_ERR, "lnc%d: Receive interrupt with buffer still owned by controller -- Resetting\n", unit);
  485                 lnc_reset(sc);
  486                 return;
  487         }
  488         if (!((sc->recv_ring + sc->recv_next)->md->md1 & STP)) {
  489                 int unit = sc->arpcom.ac_if.if_unit;
  490                 log(LOG_ERR, "lnc%d: Receive interrupt but not start of packet -- Resetting\n", unit);
  491                 lnc_reset(sc);
  492                 return;
  493         }
  494 #endif
  495 
  496         lookahead = 0;
  497         next = sc->recv_ring + sc->recv_next;
  498         while ((flags = next->md->md1) & STP) {
  499 
  500                 /* Make a note of the start of the packet */
  501                 start_of_packet = sc->recv_next;
  502 
  503                 /*
  504                  * Find the end of the packet. Even if not data chaining,
  505                  * jabber packets can overrun into a second descriptor.
  506                  * If there is no error, then the ENP flag is set in the last
  507                  * descriptor of the packet. If there is an error then the ERR
  508                  * flag will be set in the descriptor where the error occured.
  509                  * Therefore, to find the last buffer of a packet we search for
  510                  * either ERR or ENP.
  511                  */
  512 
  513                 if (!(flags & (ENP | MDERR))) {
  514                         do {
  515                                 INC_MD_PTR(sc->recv_next, sc->nrdre)
  516                                 next = sc->recv_ring + sc->recv_next;
  517                                 flags = next->md->md1;
  518                         } while (!(flags & (STP | OWN | ENP | MDERR)));
  519 
  520                         if (flags & STP) {
  521                                 int unit = sc->arpcom.ac_if.if_unit;
  522                                 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in receive ring -- Resetting\n", unit);
  523                                 lnc_reset(sc);
  524                                 return;
  525                         }
  526                         if (flags & OWN) {
  527                                 if (lookahead) {
  528                                         /*
  529                                          * Looked ahead into a packet still
  530                                          * being received
  531                                          */
  532                                         sc->recv_next = start_of_packet;
  533                                         break;
  534                                 } else {
  535                                         int unit = sc->arpcom.ac_if.if_unit;
  536                                         log(LOG_ERR, "lnc%d: End of received packet not found-- Resetting\n", unit);
  537                                         lnc_reset(sc);
  538                                         return;
  539                                 }
  540                         }
  541                 }
  542 
  543                 pkt_len = (next->md->md3 & MCNT) - FCS_LEN;
  544 
  545                 /* Move pointer onto start of next packet */
  546                 INC_MD_PTR(sc->recv_next, sc->nrdre)
  547                 next = sc->recv_ring + sc->recv_next;
  548 
  549                 if (flags & MDERR) {
  550                         int unit = sc->arpcom.ac_if.if_unit;
  551                         if (flags & RBUFF) {
  552                                 LNCSTATS(rbuff)
  553                                 log(LOG_ERR, "lnc%d: Receive buffer error\n", unit);
  554                         }
  555                         if (flags & OFLO) {
  556                                 /* OFLO only valid if ENP is not set */
  557                                 if (!(flags & ENP)) {
  558                                         LNCSTATS(oflo)
  559                                         log(LOG_ERR, "lnc%d: Receive overflow error \n", unit);
  560                                 }
  561                         } else if (flags & ENP) {
  562                             if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC)==0) {
  563                                 /*
  564                                  * FRAM and CRC are valid only if ENP
  565                                  * is set and OFLO is not.
  566                                  */
  567                                 if (flags & FRAM) {
  568                                         LNCSTATS(fram)
  569                                         log(LOG_ERR, "lnc%d: Framing error\n", unit);
  570                                         /*
  571                                          * FRAM is only set if there's a CRC
  572                                          * error so avoid multiple messages
  573                                          */
  574                                 } else if (flags & CRC) {
  575                                         LNCSTATS(crc)
  576                                         log(LOG_ERR, "lnc%d: Receive CRC error\n", unit);
  577                                 }
  578                             }
  579                         }
  580 
  581                         /* Drop packet */
  582                         LNCSTATS(rerr)
  583                         sc->arpcom.ac_if.if_ierrors++;
  584                         while (start_of_packet != sc->recv_next) {
  585                                 start = sc->recv_ring + start_of_packet;
  586                                 start->md->md2 = -RECVBUFSIZE; /* XXX - shouldn't be necessary */
  587                                 start->md->md1 &= HADR;
  588                                 start->md->md1 |= OWN;
  589                                 INC_MD_PTR(start_of_packet, sc->nrdre)
  590                         }
  591                 } else { /* Valid packet */
  592 
  593                         sc->arpcom.ac_if.if_ipackets++;
  594 
  595 
  596                         if (sc->nic.mem_mode == DMA_MBUF)
  597                                 head = chain_mbufs(sc, start_of_packet, pkt_len);
  598                         else
  599                                 head = mbuf_packet(sc, start_of_packet, pkt_len);
  600 
  601                         if (head) {
  602                                 /*
  603                                  * First mbuf in packet holds the
  604                                  * ethernet and packet headers
  605                                  */
  606                                 head->m_pkthdr.rcvif = &sc->arpcom.ac_if;
  607                                 head->m_pkthdr.len = pkt_len ;
  608 
  609                                 /*
  610                                  * BPF expects the ether header to be in the first
  611                                  * mbuf of the chain so point eh at the right place
  612                                  * but don't increment the mbuf pointers before
  613                                  * the bpf tap.
  614                                  */
  615 
  616                                 eh = (struct ether_header *) head->m_data;
  617 
  618 #if NBPFILTER > 0
  619                                 if (sc->arpcom.ac_if.if_bpf)
  620                                         bpf_mtap(&sc->arpcom.ac_if, head);
  621 #endif
  622 #ifdef BRIDGE
  623                                 if (do_bridge) {
  624                                     struct ifnet *bdg_ifp ;
  625 
  626                                     bdg_ifp = bridge_in(head);
  627                                     if (bdg_ifp == BDG_DROP)
  628                                         m_freem(head);
  629                                     else {
  630                                         if (bdg_ifp != BDG_LOCAL)
  631                                             bdg_forward(&head, bdg_ifp);
  632                                         if (  bdg_ifp == BDG_LOCAL ||
  633                                               bdg_ifp == BDG_BCAST ||
  634                                               bdg_ifp == BDG_MCAST )
  635                                             goto getit;
  636                                         else if (head)
  637                                             m_freem(head);
  638                                     }
  639                                 } else
  640 #endif
  641                                 /* Check this packet is really for us */
  642 
  643                                 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
  644                                         !(eh->ether_dhost[0] & 1) && /* Broadcast and multicast */
  645                                         (bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
  646                                                         sizeof(eh->ether_dhost))))
  647                                                 m_freem(head);
  648                                 else
  649                                 {
  650 #ifdef BRIDGE
  651 getit:
  652 #endif
  653                                         /* Skip over the ether header */
  654                                         head->m_data += sizeof *eh;
  655                                         head->m_len -= sizeof *eh;
  656                                         head->m_pkthdr.len -= sizeof *eh;
  657 
  658                                         ether_input(&sc->arpcom.ac_if, eh, head);
  659                                 }
  660 
  661                         } else {
  662                                 int unit = sc->arpcom.ac_if.if_unit;
  663                                 log(LOG_ERR,"lnc%d: Packet dropped, no mbufs\n",unit);
  664                                 LNCSTATS(drop_packet)
  665                         }
  666                 }
  667 
  668                 lookahead++;
  669         }
  670 
  671         /*
  672          * At this point all completely received packets have been processed
  673          * so clear RINT since any packets that have arrived while we were in
  674          * here have been dealt with.
  675          */
  676 
  677         outw(sc->rdp, RINT | INEA);
  678 }
  679 
  680 static __inline void
  681 lnc_tint(struct lnc_softc *sc)
  682 {
  683         struct host_ring_entry *next, *start;
  684         int start_of_packet;
  685         int lookahead;
  686 
  687         /*
  688          * If the driver is reset in this routine then we return immediately to
  689          * the interrupt driver routine. Any interrupts that have occured
  690          * since the reset will be dealt with there. sc->trans_next
  691          * should point to the start of the first packet that was awaiting
  692          * transmission after the last transmit interrupt was dealt with. The
  693          * LANCE should have relinquished ownership of that descriptor before
  694          * the interrupt. Therefore, sc->trans_next should point to a
  695          * descriptor with STP set and OWN cleared. If not then the driver's
  696          * pointers are out of sync with the LANCE, which signifies a bug in
  697          * the driver. Therefore, the following two checks are really
  698          * diagnostic, since if the driver is working correctly they should
  699          * never happen.
  700          */
  701 
  702 #ifdef DIAGNOSTIC
  703         if ((sc->trans_ring + sc->trans_next)->md->md1 & OWN) {
  704                 int unit = sc->arpcom.ac_if.if_unit;
  705                 log(LOG_ERR, "lnc%d: Transmit interrupt with buffer still owned by controller -- Resetting\n", unit);
  706                 lnc_reset(sc);
  707                 return;
  708         }
  709 #endif
  710 
  711 
  712         /*
  713          * The LANCE will write the status information for the packet it just
  714          * tried to transmit in one of two places. If the packet was
  715          * transmitted successfully then the status will be written into the
  716          * last descriptor of the packet. If the transmit failed then the
  717          * status will be written into the descriptor that was being accessed
  718          * when the error occured and all subsequent descriptors in that
  719          * packet will have been relinquished by the LANCE.
  720          *
  721          * At this point we know that sc->trans_next points to the start
  722          * of a packet that the LANCE has just finished trying to transmit.
  723          * We now search for a buffer with either ENP or ERR set.
  724          */
  725 
  726         lookahead = 0;
  727 
  728         do {
  729                 start_of_packet = sc->trans_next;
  730                 next = sc->trans_ring + sc->trans_next;
  731 
  732 #ifdef DIAGNOSTIC
  733         if (!(next->md->md1 & STP)) {
  734                 int unit = sc->arpcom.ac_if.if_unit;
  735                 log(LOG_ERR, "lnc%d: Transmit interrupt but not start of packet -- Resetting\n", unit);
  736                 lnc_reset(sc);
  737                 return;
  738         }
  739 #endif
  740 
  741                 /*
  742                  * Find end of packet.
  743                  */
  744 
  745                 if (!(next->md->md1 & (ENP | MDERR))) {
  746                         do {
  747                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
  748                                 next = sc->trans_ring + sc->trans_next;
  749                         } while (!(next->md->md1 & (STP | OWN | ENP | MDERR)));
  750 
  751                         if (next->md->md1 & STP) {
  752                                 int unit = sc->arpcom.ac_if.if_unit;
  753                                 log(LOG_ERR, "lnc%d: Start of packet found before end of previous in transmit ring -- Resetting\n", unit);
  754                                 lnc_reset(sc);
  755                                 return;
  756                         }
  757                         if (next->md->md1 & OWN) {
  758                                 if (lookahead) {
  759                                         /*
  760                                          * Looked ahead into a packet still
  761                                          * being transmitted
  762                                          */
  763                                         sc->trans_next = start_of_packet;
  764                                         break;
  765                                 } else {
  766                                         int unit = sc->arpcom.ac_if.if_unit;
  767                                         log(LOG_ERR, "lnc%d: End of transmitted packet not found -- Resetting\n", unit);
  768                                         lnc_reset(sc);
  769                                         return;
  770                                 }
  771                         }
  772                 }
  773                 /*
  774                  * Check for ERR first since other flags are irrelevant if an
  775                  * error occurred.
  776                  */
  777                 if (next->md->md1 & MDERR) {
  778 
  779                         int unit = sc->arpcom.ac_if.if_unit;
  780 
  781                         LNCSTATS(terr)
  782                                 sc->arpcom.ac_if.if_oerrors++;
  783 
  784                         if (next->md->md3 & LCOL) {
  785                                 LNCSTATS(lcol)
  786                                 log(LOG_ERR, "lnc%d: Transmit late collision  -- Net error?\n", unit);
  787                                 sc->arpcom.ac_if.if_collisions++;
  788                                 /*
  789                                  * Clear TBUFF since it's not valid when LCOL
  790                                  * set
  791                                  */
  792                                 next->md->md3 &= ~TBUFF;
  793                         }
  794                         if (next->md->md3 & LCAR) {
  795                                 LNCSTATS(lcar)
  796                                 log(LOG_ERR, "lnc%d: Loss of carrier during transmit -- Net error?\n", unit);
  797                         }
  798                         if (next->md->md3 & RTRY) {
  799                                 LNCSTATS(rtry)
  800                                 log(LOG_ERR, "lnc%d: Transmit of packet failed after 16 attempts -- TDR = %d\n", unit, ((sc->trans_ring + sc->trans_next)->md->md3 & TDR));
  801                                 sc->arpcom.ac_if.if_collisions += 16;
  802                                 /*
  803                                  * Clear TBUFF since it's not valid when RTRY
  804                                  * set
  805                                  */
  806                                 next->md->md3 &= ~TBUFF;
  807                         }
  808                         /*
  809                          * TBUFF is only valid if neither LCOL nor RTRY are set.
  810                          * We need to check UFLO after LCOL and RTRY so that we
  811                          * know whether or not TBUFF is valid. If either are
  812                          * set then TBUFF will have been cleared above. A
  813                          * UFLO error will turn off the transmitter so we
  814                          * have to reset.
  815                          *
  816                          */
  817 
  818                         if (next->md->md3 & UFLO) {
  819                                 LNCSTATS(uflo)
  820                                 /*
  821                                  * If an UFLO has occured it's possibly due
  822                                  * to a TBUFF error
  823                                  */
  824                                 if (next->md->md3 & TBUFF) {
  825                                         LNCSTATS(tbuff)
  826                                         log(LOG_ERR, "lnc%d: Transmit buffer error -- Resetting\n", unit);
  827                                 } else
  828                                         log(LOG_ERR, "lnc%d: Transmit underflow error -- Resetting\n", unit);
  829                                 lnc_reset(sc);
  830                                 return;
  831                         }
  832                         do {
  833                                 INC_MD_PTR(sc->trans_next, sc->ntdre)
  834                                 next = sc->trans_ring + sc->trans_next;
  835                         } while (!(next->md->md1 & STP) && (sc->trans_next != sc->next_to_send));
  836 
  837                 } else {
  838                         /*
  839                          * Since we check for ERR first then if we get here
  840                          * the packet was transmitted correctly. There may
  841                          * still have been non-fatal errors though.
  842                          * Don't bother checking for DEF, waste of time.
  843                          */
  844 
  845                         sc->arpcom.ac_if.if_opackets++;
  846 
  847                         if (next->md->md1 & MORE) {
  848                                 LNCSTATS(more)
  849                                 sc->arpcom.ac_if.if_collisions += 2;
  850                         }
  851 
  852                         /*
  853                          * ONE is invalid if LCOL is set. If LCOL was set then
  854                          * ERR would have also been set and we would have
  855                          * returned from lnc_tint above. Therefore we can
  856                          * assume if we arrive here that ONE is valid.
  857                          *
  858                          */
  859 
  860                         if (next->md->md1 & ONE) {
  861                                 LNCSTATS(one)
  862                                 sc->arpcom.ac_if.if_collisions++;
  863                         }
  864                         INC_MD_PTR(sc->trans_next, sc->ntdre)
  865                         next = sc->trans_ring + sc->trans_next;
  866                 }
  867 
  868                 /*
  869                  * Clear descriptors and free any mbufs.
  870                  */
  871 
  872                 do {
  873                         start = sc->trans_ring + start_of_packet;
  874                         start->md->md1 &= HADR;
  875                         if (sc->nic.mem_mode == DMA_MBUF) {
  876                                 /* Cache clusters on a local queue */
  877                                 if ((start->buff.mbuf->m_flags & M_EXT) && (sc->mbuf_count < MBUF_CACHE_LIMIT)) {
  878                                         if (sc->mbuf_count) {
  879                                                 start->buff.mbuf->m_next = sc->mbufs;
  880                                                 sc->mbufs = start->buff.mbuf;
  881                                         } else
  882                                                 sc->mbufs = start->buff.mbuf;
  883                                         sc->mbuf_count++;
  884                                         start->buff.mbuf = 0;
  885                                 } else {
  886                                         struct mbuf *junk;
  887                                         MFREE(start->buff.mbuf, junk);
  888                                         start->buff.mbuf = 0;
  889                                 }
  890                         }
  891                         sc->pending_transmits--;
  892                         INC_MD_PTR(start_of_packet, sc->ntdre)
  893                 }while (start_of_packet != sc->trans_next);
  894 
  895                 /*
  896                  * There's now at least one free descriptor
  897                  * in the ring so indicate that we can accept
  898                  * more packets again.
  899                  */
  900 
  901                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
  902 
  903                 lookahead++;
  904 
  905         } while (sc->pending_transmits && !(next->md->md1 & OWN));
  906 
  907         /*
  908          * Clear TINT since we've dealt with all
  909          * the completed transmissions.
  910          */
  911 
  912         outw(sc->rdp, TINT | INEA);
  913 
  914         /* XXX only while doing if_is comparisons */
  915         if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
  916                 lnc_start(&sc->arpcom.ac_if);
  917 
  918 }
  919 
  920 static int
  921 lnc_probe(struct isa_device * isa_dev)
  922 {
  923         int nports;
  924         int unit = isa_dev->id_unit;
  925         struct lnc_softc *sc = &lnc_softc[unit];
  926         unsigned iobase = isa_dev->id_iobase;
  927 
  928 #ifdef DIAGNOSTIC
  929         int vsw;
  930         vsw = inw(isa_dev->id_iobase + PCNET_VSW);
  931         printf("Vendor Specific Word = %x\n", vsw);
  932 #endif
  933 
  934         nports = bicc_probe(sc, iobase);
  935         if (nports == 0)
  936                 nports = ne2100_probe(sc, iobase);
  937         if (nports == 0)
  938                 nports = depca_probe(sc, iobase);
  939 #ifdef PC98
  940         if (nports == 0)
  941                 nports = cnet98s_probe(sc, iobase);
  942 #endif
  943         return (nports);
  944 }
  945 
  946 #ifdef PC98
  947 /* ISA Bus Configuration Registers */
  948 /* XXX - Should be in ic/Am7990.h */
  949 #define MSRDA   0x0000  /* ISACSR0: Master Mode Read Activity */
  950 #define MSWRA   0x0001  /* ISACSR1: Master Mode Write Activity */
  951 #define MC      0x0002  /* ISACSR2: Miscellaneous Configuration */
  952 
  953 #define LED1    0x0005  /* ISACSR5: LED1 Status */
  954 #define LED2    0x0006  /* ISACSR6: LED2 Status */
  955 #define LED3    0x0007  /* ISACSR7: LED3 Status */
  956 
  957 #define LED_PSE         0x0080  /* Pulse Stretcher */
  958 #define LED_XMTE        0x0010  /* Transmit Status */
  959 #define LED_RVPOLE      0x0008  /* Receive Polarity */
  960 #define LED_RCVE        0x0004  /* Receive Status */
  961 #define LED_JABE        0x0002  /* Jabber */
  962 #define LED_COLE        0x0001  /* Collision */
  963 
  964 static int
  965 cnet98s_probe(struct lnc_softc *sc, unsigned iobase)
  966 {
  967         int i;
  968         ushort tmp;
  969 
  970         sc->rap = iobase + CNET98S_RAP;
  971         sc->rdp = iobase + CNET98S_RDP;
  972 
  973         /* Reset */
  974         tmp = inw(iobase + CNET98S_RESET);
  975         outw(iobase + CNET98S_RESET, tmp);
  976         DELAY(500);
  977 
  978         sc->nic.ic = pcnet_probe(sc);
  979         if ((sc->nic.ic == UNKNOWN) || (sc->nic.ic > PCnet_32)) {
  980                 return (0);
  981         }
  982 
  983         sc->nic.ident = CNET98S;
  984         sc->nic.mem_mode = DMA_FIXED;
  985 
  986         /* XXX - For now just use the defines */
  987         sc->nrdre = NRDRE;
  988         sc->ntdre = NTDRE;
  989 
  990         /* Extract MAC address from PROM */
  991         for (i = 0; i < ETHER_ADDR_LEN; i++) {
  992                 sc->arpcom.ac_enaddr[i] = inb(iobase + (i * 2));
  993         }
  994 
  995         /*
  996          * ISA Configuration
  997          *
  998          * XXX - Following parameters are Contec C-NET(98)S only.
  999          *       So, check the Ethernet address here.
 1000          *
 1001          *       Contec uses 00 80 4c ?? ?? ??
 1002          */ 
 1003         if (sc->arpcom.ac_enaddr[0] == (u_char)0x00
 1004         &&  sc->arpcom.ac_enaddr[1] == (u_char)0x80
 1005         &&  sc->arpcom.ac_enaddr[2] == (u_char)0x4c) {
 1006                 outw(sc->rap, MSRDA);
 1007                 outw(iobase + CNET98S_IDP, 0x0006);
 1008                 outw(sc->rap, MSWRA);
 1009                 outw(iobase + CNET98S_IDP, 0x0006);
 1010 #ifdef DIAGNOSTIC
 1011                 outw(sc->rap, MC);
 1012                 printf("ISACSR2 = %x\n", inw(iobase + CNET98S_IDP));
 1013 #endif
 1014                 outw(sc->rap, LED1);
 1015                 outw(iobase + CNET98S_IDP, LED_PSE | LED_XMTE);
 1016                 outw(sc->rap, LED2);
 1017                 outw(iobase + CNET98S_IDP, LED_PSE | LED_RCVE);
 1018                 outw(sc->rap, LED3);
 1019                 outw(iobase + CNET98S_IDP, LED_PSE | LED_COLE);
 1020         }
 1021                 
 1022         return (CNET98S_IOSIZE);
 1023 }
 1024 #endif
 1025 
 1026 static int
 1027 ne2100_probe(struct lnc_softc *sc, unsigned iobase)
 1028 {
 1029         int i;
 1030 
 1031         sc->rap = iobase + PCNET_RAP;
 1032         sc->rdp = iobase + PCNET_RDP;
 1033 
 1034         sc->nic.ic = pcnet_probe(sc);
 1035         if ((sc->nic.ic > 0) && (sc->nic.ic < PCnet_PCI)) {
 1036                 sc->nic.ident = NE2100;
 1037                 sc->nic.mem_mode = DMA_FIXED;
 1038 
 1039                 /* XXX - For now just use the defines */
 1040                 sc->nrdre = NRDRE;
 1041                 sc->ntdre = NTDRE;
 1042 
 1043                 /* Extract MAC address from PROM */
 1044                 for (i = 0; i < ETHER_ADDR_LEN; i++)
 1045                         sc->arpcom.ac_enaddr[i] = inb(iobase + i);
 1046                 return (NE2100_IOSIZE);
 1047         } else {
 1048                 return (0);
 1049         }
 1050 }
 1051 
 1052 static int
 1053 bicc_probe(struct lnc_softc *sc, unsigned iobase)
 1054 {
 1055         int i;
 1056 
 1057         /*
 1058          * There isn't any way to determine if a NIC is a BICC. Basically, if
 1059          * the lance probe succeeds using the i/o addresses of the BICC then
 1060          * we assume it's a BICC.
 1061          *
 1062          */
 1063 
 1064         sc->rap = iobase + BICC_RAP;
 1065         sc->rdp = iobase + BICC_RDP;
 1066 
 1067         /* I think all these cards us the Am7990 */
 1068 
 1069         if ((sc->nic.ic = lance_probe(sc))) {
 1070                 sc->nic.ident = BICC;
 1071                 sc->nic.mem_mode = DMA_FIXED;
 1072 
 1073                 /* XXX - For now just use the defines */
 1074                 sc->nrdre = NRDRE;
 1075                 sc->ntdre = NTDRE;
 1076 
 1077                 /* Extract MAC address from PROM */
 1078                 for (i = 0; i < ETHER_ADDR_LEN; i++)
 1079                         sc->arpcom.ac_enaddr[i] = inb(iobase + (i * 2));
 1080 
 1081                 return (BICC_IOSIZE);
 1082         } else {
 1083                 return (0);
 1084         }
 1085 }
 1086 
 1087 /*
 1088  * I don't have data sheets for the dec cards but it looks like the mac
 1089  * address is contained in a 32 byte ring. Each time you read from the port
 1090  * you get the next byte in the ring. The mac address is stored after a
 1091  * signature so keep searching for the signature first.
 1092  */
 1093 static int
 1094 dec_macaddr_extract(u_char ring[], struct lnc_softc * sc)
 1095 {
 1096         const unsigned char signature[] = {0xff, 0x00, 0x55, 0xaa, 0xff, 0x00, 0x55, 0xaa};
 1097 
 1098         int i, j, rindex;
 1099 
 1100         for (i = 0; i < sizeof ring; i++) {
 1101                 for (j = 0, rindex = i; j < sizeof signature; j++) {
 1102                         if (ring[rindex] != signature[j])
 1103                                 break;
 1104                         if (++rindex > sizeof ring)
 1105                                 rindex = 0;
 1106                 }
 1107                 if (j == sizeof signature) {
 1108                         for (j = 0, rindex = i; j < ETHER_ADDR_LEN; j++) {
 1109                                 sc->arpcom.ac_enaddr[j] = ring[rindex];
 1110                                 if (++rindex > sizeof ring)
 1111                                         rindex = 0;
 1112                         }
 1113                         return (1);
 1114                 }
 1115         }
 1116         return (0);
 1117 }
 1118 
 1119 static int
 1120 depca_probe(struct lnc_softc *sc, unsigned iobase)
 1121 {
 1122         int i;
 1123         unsigned char maddr_ring[DEPCA_ADDR_ROM_SIZE];
 1124 
 1125         sc->rap = iobase + DEPCA_RAP;
 1126         sc->rdp = iobase + DEPCA_RDP;
 1127 
 1128         if ((sc->nic.ic = lance_probe(sc))) {
 1129                 sc->nic.ident = DEPCA;
 1130                 sc->nic.mem_mode = SHMEM;
 1131 
 1132                 /* Extract MAC address from PROM */
 1133                 for (i = 0; i < DEPCA_ADDR_ROM_SIZE; i++)
 1134                         maddr_ring[i] = inb(iobase + DEPCA_ADP);
 1135                 if (dec_macaddr_extract(maddr_ring, sc)) {
 1136                         return (DEPCA_IOSIZE);
 1137                 }
 1138         }
 1139         return (0);
 1140 }
 1141 
 1142 static int
 1143 lance_probe(struct lnc_softc *sc)
 1144 {
 1145         write_csr(sc, CSR0, STOP);
 1146 
 1147         if ((inw(sc->rdp) & STOP) && !(read_csr(sc, CSR3))) {
 1148                 /*
 1149                  * Check to see if it's a C-LANCE. For the LANCE the INEA bit
 1150                  * cannot be set while the STOP bit is. This restriction is
 1151                  * removed for the C-LANCE.
 1152                  */
 1153                 write_csr(sc, CSR0, INEA);
 1154                 if (read_csr(sc, CSR0) & INEA)
 1155                         return (C_LANCE);
 1156                 else
 1157                         return (LANCE);
 1158         } else
 1159                 return (UNKNOWN);
 1160 }
 1161 
 1162 static int
 1163 pcnet_probe(struct lnc_softc *sc)
 1164 {
 1165         u_long chip_id;
 1166         int type;
 1167 
 1168         /*
 1169          * The PCnet family don't reset the RAP register on reset so we'll
 1170          * have to write during the probe :-) It does have an ID register
 1171          * though so the probe is just a matter of reading it.
 1172          */
 1173 
 1174         if ((type = lance_probe(sc))) {
 1175                 chip_id = read_csr(sc, CSR89);
 1176                 chip_id <<= 16;
 1177                 chip_id |= read_csr(sc, CSR88);
 1178                 if (chip_id & AMD_MASK) {
 1179                         chip_id >>= 12;
 1180                         switch (chip_id & PART_MASK) {
 1181                         case Am79C960:
 1182                                 return (PCnet_ISA);
 1183                         case Am79C961:
 1184                                 return (PCnet_ISAplus);
 1185                         case Am79C961A:
 1186                                 return (PCnet_ISA_II);
 1187                         case Am79C965:
 1188                                 return (PCnet_32);
 1189                         case Am79C970:
 1190                                 return (PCnet_PCI);
 1191                         case Am79C970A:
 1192                                 return (PCnet_PCI_II);
 1193                         case Am79C971:
 1194                                 return (PCnet_FAST);
 1195                         case Am79C972:
 1196                                 return (PCnet_FASTplus);
 1197                         case Am79C978:
 1198                                 return (PCnet_Home);
 1199                         case Am79C973:
 1200                         case Am79C975:
 1201                                 return (PCnet_FAST_III);
 1202                         default:
 1203                                 break;
 1204                         }
 1205                 }
 1206         }
 1207         return (type);
 1208 }
 1209 
 1210 static int
 1211 lnc_attach_sc(struct lnc_softc *sc, int unit)
 1212 {
 1213         int lnc_mem_size;
 1214 
 1215         /*
 1216          * Allocate memory for use by the controller.
 1217          *
 1218          * XXX -- the Am7990 and Am79C960 only have 24 address lines and so can
 1219          * only access the lower 16Mb of physical memory. For the moment we
 1220          * assume that malloc will allocate memory within the lower 16Mb
 1221          * range. This is not a very valid assumption but there's nothing
 1222          * that can be done about it yet. For shared memory NICs this isn't
 1223          * relevant.
 1224          *
 1225          */
 1226 
 1227         lnc_mem_size = ((NDESC(sc->nrdre) + NDESC(sc->ntdre)) *
 1228                  sizeof(struct host_ring_entry));
 1229 
 1230         if (sc->nic.mem_mode != SHMEM)
 1231                 lnc_mem_size += sizeof(struct init_block) + (sizeof(struct mds) *
 1232                             (NDESC(sc->nrdre) + NDESC(sc->ntdre))) +
 1233                         MEM_SLEW;
 1234 
 1235         /* If using DMA to fixed host buffers then allocate memory for them */
 1236 
 1237         if (sc->nic.mem_mode == DMA_FIXED)
 1238                 lnc_mem_size += (NDESC(sc->nrdre) * RECVBUFSIZE) + (NDESC(sc->ntdre) * TRANSBUFSIZE);
 1239 
 1240         if (sc->nic.mem_mode != SHMEM) {
 1241                 if (sc->nic.ic < PCnet_32) {
 1242                         /* ISA based cards */
 1243                         sc->recv_ring = contigmalloc(lnc_mem_size, M_DEVBUF, M_NOWAIT,
 1244                                                                                  0ul, 0xfffffful, 4ul, 0x1000000);
 1245                 } else {
 1246                         /* Non-ISA based cards, 32 bit capable */
 1247 #ifdef notyet
 1248                         /*
 1249                          * For the 32 bit driver we're not fussed where we DMA to
 1250                          * though it'll still need to be contiguous
 1251                          */
 1252                         sc->recv_ring = malloc(lnc_mem_size, M_DEVBUF, M_NOWAIT);
 1253 #else
 1254                         /*
 1255                          * For now it still needs to be below 16MB because the
 1256                          * descriptor's can only hold 16 bit addresses.
 1257                          */
 1258                         sc->recv_ring = contigmalloc(lnc_mem_size, M_DEVBUF, M_NOWAIT,
 1259                                                                                  0ul, 0xfffffful, 4ul, 0x1000000);
 1260 #endif
 1261                 }       
 1262         }
 1263 
 1264         if (!sc->recv_ring) {
 1265                 log(LOG_ERR, "lnc%d: Couldn't allocate memory for NIC\n", unit);
 1266                 return (0);     /* XXX -- attach failed -- not tested in
 1267                                  * calling routines */
 1268         }
 1269 
 1270         /* Set default mode */
 1271         sc->nic.mode = NORMAL;
 1272 
 1273         /* Fill in arpcom structure entries */
 1274 
 1275         sc->arpcom.ac_if.if_softc = sc;
 1276         sc->arpcom.ac_if.if_name = lncdriver.name;
 1277         sc->arpcom.ac_if.if_unit = unit;
 1278         sc->arpcom.ac_if.if_mtu = ETHERMTU;
 1279         sc->arpcom.ac_if.if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
 1280         sc->arpcom.ac_if.if_timer = 0;
 1281         sc->arpcom.ac_if.if_output = ether_output;
 1282         sc->arpcom.ac_if.if_start = lnc_start;
 1283         sc->arpcom.ac_if.if_ioctl = lnc_ioctl;
 1284         sc->arpcom.ac_if.if_watchdog = lnc_watchdog;
 1285         sc->arpcom.ac_if.if_type = IFT_ETHER;
 1286         sc->arpcom.ac_if.if_addrlen = ETHER_ADDR_LEN;
 1287         sc->arpcom.ac_if.if_hdrlen = ETHER_HDR_LEN;
 1288         sc->arpcom.ac_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
 1289 
 1290         /*
 1291          * XXX -- should check return status of if_attach
 1292          */
 1293 
 1294         if_attach(&sc->arpcom.ac_if);
 1295         ether_ifattach(&sc->arpcom.ac_if);
 1296 
 1297         printf("lnc%d: ", unit);
 1298         if (sc->nic.ic == LANCE || sc->nic.ic == C_LANCE)
 1299                 printf("%s (%s)",
 1300                        nic_ident[sc->nic.ident], ic_ident[sc->nic.ic]);
 1301         else
 1302                 printf("%s", ic_ident[sc->nic.ic]);
 1303         printf(" address %6D\n", sc->arpcom.ac_enaddr, ":");
 1304 
 1305 #if NBPFILTER > 0
 1306         bpfattach(&sc->arpcom.ac_if, DLT_EN10MB, sizeof(struct ether_header));
 1307 #endif
 1308 
 1309         return (1);
 1310 }
 1311 
 1312 static int
 1313 lnc_attach(struct isa_device * isa_dev)
 1314 {
 1315         int unit = isa_dev->id_unit;
 1316         struct lnc_softc *sc = &lnc_softc[unit];
 1317         int result;
 1318 
 1319         isa_dev->id_ointr = lncintr;
 1320         result = lnc_attach_sc (sc, unit);
 1321         if (result == 0)
 1322                 return (0);
 1323 
 1324 #ifndef PC98
 1325         /*
 1326          * XXX - is it safe to call isa_dmacascade() after if_attach() 
 1327          *       and ether_ifattach() have been called in lnc_attach() ???
 1328          */
 1329         if ((sc->nic.mem_mode != SHMEM) &&
 1330             (sc->nic.ic < PCnet_32))
 1331                 isa_dmacascade(isa_dev->id_drq);
 1332 #endif
 1333 
 1334         return result;
 1335 }
 1336 
 1337 #if NPCI > 0
 1338 void *
 1339 lnc_attach_ne2100_pci(int unit, unsigned iobase)
 1340 {
 1341         int i;
 1342         struct lnc_softc *sc = malloc(sizeof *sc, M_DEVBUF, M_NOWAIT);
 1343 
 1344         if (sc) {
 1345                 bzero (sc, sizeof *sc);
 1346 
 1347                 sc->rap = iobase + PCNET_RAP;
 1348                 sc->rdp = iobase + PCNET_RDP;
 1349                 sc->bdp = iobase + PCNET_BDP;
 1350 
 1351                 sc->nic.ic = pcnet_probe(sc);
 1352                 if (sc->nic.ic >= PCnet_32) {
 1353                         sc->nic.ident = NE2100;
 1354                         sc->nic.mem_mode = DMA_FIXED;
 1355   
 1356                         /* XXX - For now just use the defines */
 1357                         sc->nrdre = NRDRE;
 1358                         sc->ntdre = NTDRE;
 1359 
 1360                         /* Extract MAC address from PROM */
 1361                         for (i = 0; i < ETHER_ADDR_LEN; i++)
 1362                                 sc->arpcom.ac_enaddr[i] = inb(iobase + i);
 1363 
 1364                         if (lnc_attach_sc(sc, unit) == 0) {
 1365                                 free(sc, M_DEVBUF);
 1366                                 sc = NULL;
 1367                         }
 1368                 }
 1369                 else {
 1370                         free(sc, M_DEVBUF);
 1371                         sc = NULL;
 1372                 }
 1373         }
 1374         return sc;
 1375 }
 1376 #endif
 1377 
 1378 static void
 1379 lnc_init(struct lnc_softc *sc)
 1380 {
 1381         int s, i;
 1382         char *lnc_mem;
 1383 
 1384         /* Check that interface has valid address */
 1385 
 1386         if (TAILQ_EMPTY(&sc->arpcom.ac_if.if_addrhead)) /* XXX unlikely */
 1387                 return;
 1388 
 1389         /* Shut down interface */
 1390 
 1391         s = splimp();
 1392         lnc_stop(sc);
 1393         sc->arpcom.ac_if.if_flags |= IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; /* XXX??? */
 1394 
 1395         /*
 1396          * This sets up the memory area for the controller. Memory is set up for
 1397          * the initialisation block (12 words of contiguous memory starting
 1398          * on a word boundary),the transmit and receive ring structures (each
 1399          * entry is 4 words long and must start on a quadword boundary) and
 1400          * the data buffers.
 1401          *
 1402          * The alignment tests are particularly paranoid.
 1403          */
 1404 
 1405 
 1406 
 1407         sc->recv_next = 0;
 1408         sc->trans_ring = sc->recv_ring + NDESC(sc->nrdre);
 1409         sc->trans_next = 0;
 1410 
 1411         if (sc->nic.mem_mode == SHMEM)
 1412                 lnc_mem = (char *) sc->nic.iobase;
 1413         else
 1414                 lnc_mem = (char *) (sc->trans_ring + NDESC(sc->ntdre));
 1415 
 1416         lnc_mem = (char *)(((int)lnc_mem + 1) & ~1);
 1417         sc->init_block = (struct init_block *) ((int) lnc_mem & ~1);
 1418         lnc_mem = (char *) (sc->init_block + 1);
 1419         lnc_mem = (char *)(((int)lnc_mem + 7) & ~7);
 1420 
 1421         /* Initialise pointers to descriptor entries */
 1422         for (i = 0; i < NDESC(sc->nrdre); i++) {
 1423                 (sc->recv_ring + i)->md = (struct mds *) lnc_mem;
 1424                 lnc_mem += sizeof(struct mds);
 1425         }
 1426         for (i = 0; i < NDESC(sc->ntdre); i++) {
 1427                 (sc->trans_ring + i)->md = (struct mds *) lnc_mem;
 1428                 lnc_mem += sizeof(struct mds);
 1429         }
 1430 
 1431         /* Initialise the remaining ring entries */
 1432 
 1433         if (sc->nic.mem_mode == DMA_MBUF) {
 1434 
 1435                 sc->mbufs = 0;
 1436                 sc->mbuf_count = 0;
 1437 
 1438                 /* Free previously allocated mbufs */
 1439                 if (sc->flags & LNC_INITIALISED)
 1440                         lnc_free_mbufs(sc);
 1441 
 1442 
 1443                 for (i = 0; i < NDESC(sc->nrdre); i++) {
 1444                         if (alloc_mbuf_cluster(sc, sc->recv_ring+i)) {
 1445                                 log(LOG_ERR, "Initialisation failed -- no mbufs\n");
 1446                                 splx(s);
 1447                                 return;
 1448                         }
 1449                 }
 1450 
 1451                 for (i = 0; i < NDESC(sc->ntdre); i++) {
 1452                         (sc->trans_ring + i)->buff.mbuf = 0;
 1453                         (sc->trans_ring + i)->md->md0 = 0;
 1454                         (sc->trans_ring + i)->md->md1 = 0;
 1455                         (sc->trans_ring + i)->md->md2 = 0;
 1456                         (sc->trans_ring + i)->md->md3 = 0;
 1457                 }
 1458         } else {
 1459                 for (i = 0; i < NDESC(sc->nrdre); i++) {
 1460                         (sc->recv_ring + i)->md->md0 = kvtop(lnc_mem);
 1461                         (sc->recv_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff) | OWN;
 1462                         (sc->recv_ring + i)->md->md2 = -RECVBUFSIZE;
 1463                         (sc->recv_ring + i)->md->md3 = 0;
 1464                         (sc->recv_ring + i)->buff.data = lnc_mem;
 1465                         lnc_mem += RECVBUFSIZE;
 1466                 }
 1467                 for (i = 0; i < NDESC(sc->ntdre); i++) {
 1468                         (sc->trans_ring + i)->md->md0 = kvtop(lnc_mem);
 1469                         (sc->trans_ring + i)->md->md1 = ((kvtop(lnc_mem) >> 16) & 0xff);
 1470                         (sc->trans_ring + i)->md->md2 = 0;
 1471                         (sc->trans_ring + i)->md->md3 = 0;
 1472                         (sc->trans_ring + i)->buff.data = lnc_mem;
 1473                         lnc_mem += TRANSBUFSIZE;
 1474                 }
 1475         }
 1476 
 1477         sc->next_to_send = 0;
 1478 
 1479         /* Set up initialisation block */
 1480 
 1481         sc->init_block->mode = sc->nic.mode;
 1482 
 1483         for (i = 0; i < ETHER_ADDR_LEN; i++)
 1484                 sc->init_block->padr[i] = sc->arpcom.ac_enaddr[i];
 1485 
 1486         lnc_setladrf(sc);
 1487 
 1488         sc->init_block->rdra = kvtop(sc->recv_ring->md);
 1489         sc->init_block->rlen = ((kvtop(sc->recv_ring->md) >> 16) & 0xff) | (sc->nrdre << 13);
 1490         sc->init_block->tdra = kvtop(sc->trans_ring->md);
 1491         sc->init_block->tlen = ((kvtop(sc->trans_ring->md) >> 16) & 0xff) | (sc->ntdre << 13);
 1492 
 1493 
 1494         /* Set flags to show that the memory area is valid */
 1495         sc->flags |= LNC_INITIALISED;
 1496 
 1497         sc->pending_transmits = 0;
 1498 
 1499         /* Give the LANCE the physical address of the initialisation block */
 1500 
 1501         if (sc->nic.ic == PCnet_Home) {
 1502                 u_short media;
 1503                 /* Set PHY_SEL to HomeRun */
 1504                 media = read_bcr(sc, BCR49);
 1505                 media &= ~3;
 1506                 media |= 1;
 1507                 write_bcr(sc, BCR49, media);
 1508         }
 1509 
 1510         write_csr(sc, CSR1, kvtop(sc->init_block));
 1511         write_csr(sc, CSR2, (kvtop(sc->init_block) >> 16) & 0xff);
 1512 
 1513         /*
 1514          * Depending on which controller this is, CSR3 has different meanings.
 1515          * For the Am7990 it controls DMA operations, for the Am79C960 it
 1516          * controls interrupt masks and transmitter algorithms. In either
 1517          * case, none of the flags are set.
 1518          *
 1519          */
 1520 
 1521         write_csr(sc, CSR3, 0);
 1522 
 1523         /* Let's see if it starts */
 1524 
 1525         write_csr(sc, CSR0, INIT);
 1526         for (i = 0; i < 1000; i++)
 1527                 if (read_csr(sc, CSR0) & IDON)
 1528                         break;
 1529 
 1530         /*
 1531          * Now that the initialisation is complete there's no reason to
 1532          * access anything except CSR0, so we leave RAP pointing there
 1533          * so we can just access RDP from now on, saving an outw each
 1534          * time.
 1535          */
 1536 
 1537         if (read_csr(sc, CSR0) & IDON) {
 1538                 /*
 1539                  * Enable interrupts, start the LANCE, mark the interface as
 1540                  * running and transmit any pending packets.
 1541                  */
 1542                 write_csr(sc, CSR0, STRT | INEA);
 1543                 sc->arpcom.ac_if.if_flags |= IFF_RUNNING;
 1544                 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
 1545                 lnc_start(&sc->arpcom.ac_if);
 1546         } else
 1547                 log(LOG_ERR, "lnc%d: Initialisation failed\n", 
 1548                     sc->arpcom.ac_if.if_unit);
 1549 
 1550         splx(s);
 1551 }
 1552 
 1553 /*
 1554  * The interrupt flag (INTR) will be set and provided that the interrupt enable
 1555  * flag (INEA) is also set, the interrupt pin will be driven low when any of
 1556  * the following occur:
 1557  *
 1558  * 1) Completion of the initialisation routine (IDON). 2) The reception of a
 1559  * packet (RINT). 3) The transmission of a packet (TINT). 4) A transmitter
 1560  * timeout error (BABL). 5) A missed packet (MISS). 6) A memory error (MERR).
 1561  *
 1562  * The interrupt flag is cleared when all of the above conditions are cleared.
 1563  *
 1564  * If the driver is reset from this routine then it first checks to see if any
 1565  * interrupts have ocurred since the reset and handles them before returning.
 1566  * This is because the NIC may signify a pending interrupt in CSR0 using the
 1567  * INTR flag even if a hardware interrupt is currently inhibited (at least I
 1568  * think it does from reading the data sheets). We may as well deal with
 1569  * these pending interrupts now rather than get the overhead of another
 1570  * hardware interrupt immediately upon returning from the interrupt handler.
 1571  *
 1572  */
 1573 
 1574 void
 1575 lncintr_sc(struct lnc_softc *sc)
 1576 {
 1577         int unit = sc->arpcom.ac_if.if_unit;
 1578         u_short csr0;
 1579 
 1580         /*
 1581          * INEA is the only bit that can be cleared by writing a 0 to it so
 1582          * we have to include it in any writes that clear other flags.
 1583          */
 1584 
 1585         while ((csr0 = inw(sc->rdp)) & INTR) {
 1586 
 1587                 /*
 1588                  * Clear interrupt flags early to avoid race conditions. The
 1589                  * controller can still set these flags even while we're in
 1590                  * this interrupt routine. If the flag is still set from the
 1591                  * event that caused this interrupt any new events will
 1592                  * be missed.
 1593                  */
 1594 
 1595 /*              outw(sc->rdp, IDON | CERR | BABL | MISS | MERR | RINT | TINT | INEA); */
 1596                 outw(sc->rdp, csr0);
 1597 
 1598                 /* We don't do anything with the IDON flag */
 1599 
 1600                 if (csr0 & ERR) {
 1601                         if (csr0 & CERR) {
 1602                                 log(LOG_ERR, "lnc%d: Heartbeat error -- SQE test failed\n", unit);
 1603                                 LNCSTATS(cerr)
 1604                         }
 1605                         if (csr0 & BABL) {
 1606                                 log(LOG_ERR, "lnc%d: Babble error - more than 1519 bytes transmitted\n", unit);
 1607                                 LNCSTATS(babl)
 1608                                 sc->arpcom.ac_if.if_oerrors++;
 1609                         }
 1610                         if (csr0 & MISS) {
 1611                                 log(LOG_ERR, "lnc%d: Missed packet -- no receive buffer\n", unit);
 1612                                 LNCSTATS(miss)
 1613                                 sc->arpcom.ac_if.if_ierrors++;
 1614                         }
 1615                         if (csr0 & MERR) {
 1616                                 log(LOG_ERR, "lnc%d: Memory error  -- Resetting\n", unit);
 1617                                 LNCSTATS(merr)
 1618                                 lnc_reset(sc);
 1619                                 continue;
 1620                         }
 1621                 }
 1622                 if (csr0 & RINT) {
 1623                         LNCSTATS(rint)
 1624                         lnc_rint(sc);
 1625                 }
 1626                 if (csr0 & TINT) {
 1627                         LNCSTATS(tint)
 1628                         sc->arpcom.ac_if.if_timer = 0;
 1629                         lnc_tint(sc);
 1630                 }
 1631 
 1632                 /*
 1633                  * If there's room in the transmit descriptor ring then queue
 1634                  * some more transmit packets.
 1635                  */
 1636 
 1637                 if (!(sc->arpcom.ac_if.if_flags & IFF_OACTIVE))
 1638                         lnc_start(&sc->arpcom.ac_if);
 1639         }
 1640 }
 1641 
 1642 static void
 1643 lncintr(int unit)
 1644 {
 1645         struct lnc_softc *sc = &lnc_softc[unit];
 1646         lncintr_sc (sc);
 1647 }
 1648 
 1649 static __inline int
 1650 mbuf_to_buffer(struct mbuf *m, char *buffer)
 1651 {
 1652 
 1653         int len=0;
 1654 
 1655         for( ; m; m = m->m_next) {
 1656                 bcopy(mtod(m, caddr_t), buffer, m->m_len);
 1657                 buffer += m->m_len;
 1658                 len += m->m_len;
 1659         }
 1660 
 1661         return(len);
 1662 }
 1663 
 1664 static __inline struct mbuf *
 1665 chain_to_cluster(struct mbuf *m)
 1666 {
 1667         struct mbuf *new;
 1668 
 1669         MGET(new, M_DONTWAIT, MT_DATA);
 1670         if (new) {
 1671                 MCLGET(new, M_DONTWAIT);
 1672                 if (new->m_ext.ext_buf) {
 1673                         new->m_len = mbuf_to_buffer(m, new->m_data);
 1674                         m_freem(m);
 1675                         return(new);
 1676                 } else
 1677                         m_free(new);
 1678         }
 1679         return(0);
 1680 }
 1681 
 1682 /*
 1683  * IFF_OACTIVE and IFF_RUNNING are checked in ether_output so it's redundant
 1684  * to check them again since we wouldn't have got here if they were not
 1685  * appropriately set. This is also called from lnc_init and lncintr but the
 1686  * flags should be ok at those points too.
 1687  */
 1688 
 1689 static void
 1690 lnc_start(struct ifnet *ifp)
 1691 {
 1692 
 1693         struct lnc_softc *sc = ifp->if_softc;
 1694         struct host_ring_entry *desc;
 1695         int tmp;
 1696         int end_of_packet;
 1697         struct mbuf *head, *m;
 1698         int len, chunk;
 1699         int addr;
 1700         int no_entries_needed;
 1701 
 1702         do {
 1703 
 1704                 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, head);
 1705                 if (!head)
 1706                         return;
 1707 
 1708                 if (sc->nic.mem_mode == DMA_MBUF) {
 1709 
 1710                         no_entries_needed = 0;
 1711                         for (m=head; m; m = m->m_next)
 1712                                 no_entries_needed++;
 1713 
 1714                         /*
 1715                          * We try and avoid bcopy as much as possible
 1716                          * but there are two cases when we use it.
 1717                          *
 1718                          * 1) If there are not enough free entries in the ring
 1719                          * to hold each mbuf in the chain then compact the
 1720                          * chain into a single cluster.
 1721                          *
 1722                          * 2) The Am7990 and Am79C90 must not have less than
 1723                          * 100 bytes in the first descriptor of a chained
 1724                          * packet so it's necessary to shuffle the mbuf
 1725                          * contents to ensure this.
 1726                          */
 1727 
 1728 
 1729                         if (no_entries_needed > (NDESC(sc->ntdre) - sc->pending_transmits)) {
 1730                                 if (!(head = chain_to_cluster(head))) {
 1731                                         log(LOG_ERR, "lnc%d: Couldn't get mbuf for transmit packet -- Resetting \n ",ifp->if_unit);
 1732                                         lnc_reset(sc);
 1733                                         return;
 1734                                 }
 1735                         } else if ((sc->nic.ic == LANCE) || (sc->nic.ic == C_LANCE)) {
 1736                                 if ((head->m_len < 100) && (head->m_next)) {
 1737                                         len = 100 - head->m_len;
 1738                                         if (M_TRAILINGSPACE(head) < len) {
 1739                                                 /*
 1740                                                  * Move data to start of data
 1741                                                  * area. We assume the first
 1742                                                  * mbuf has a packet header
 1743                                                  * and is not a cluster.
 1744                                                  */
 1745                                                 bcopy((caddr_t)head->m_data, (caddr_t)head->m_pktdat, head->m_len);
 1746                                                 head->m_data = head->m_pktdat;
 1747                                         }
 1748                                         m = head->m_next;
 1749                                         while (m && (len > 0)) {
 1750                                                 chunk = min(len, m->m_len);
 1751                                                 bcopy(mtod(m, caddr_t), mtod(head, caddr_t) + head->m_len, chunk);
 1752                                                 len -= chunk;
 1753                                                 head->m_len += chunk;
 1754                                                 m->m_len -= chunk;
 1755                                                 m->m_data += chunk;
 1756                                                 if (m->m_len <= 0) {
 1757                                                         MFREE(m, head->m_next);
 1758                                                         m = head->m_next;
 1759                                                 }
 1760                                         }
 1761                                 }
 1762                         }
 1763 
 1764                         tmp = sc->next_to_send;
 1765 
 1766                         /*
 1767                          * On entering this loop we know that tmp points to a
 1768                          * descriptor with a clear OWN bit.
 1769                          */
 1770 
 1771                         desc = sc->trans_ring + tmp;
 1772                         len = ETHER_MIN_LEN;
 1773                         for (m = head; m; m = m->m_next) {
 1774                                 desc->buff.mbuf = m;
 1775                                 addr = kvtop(m->m_data);
 1776                                 desc->md->md0 = addr;
 1777                                 desc->md->md1 = ((addr >> 16) & 0xff);
 1778                                 desc->md->md3 = 0;
 1779                                 desc->md->md2 = -m->m_len;
 1780                                 sc->pending_transmits++;
 1781                                 len -= m->m_len;
 1782 
 1783                                 INC_MD_PTR(tmp, sc->ntdre)
 1784                                 desc = sc->trans_ring + tmp;
 1785                         }
 1786 
 1787                         end_of_packet = tmp;
 1788                         DEC_MD_PTR(tmp, sc->ntdre)
 1789                         desc = sc->trans_ring + tmp;
 1790                         desc->md->md1 |= ENP;
 1791 
 1792                         if (len > 0)
 1793                                 desc->md->md2 -= len;
 1794 
 1795                         /*
 1796                          * Set OWN bits in reverse order, otherwise the Lance
 1797                          * could start sending the packet before all the
 1798                          * buffers have been relinquished by the host.
 1799                          */
 1800 
 1801                         while (tmp != sc->next_to_send) {
 1802                                 desc->md->md1 |= OWN;
 1803                                 DEC_MD_PTR(tmp, sc->ntdre)
 1804                                 desc = sc->trans_ring + tmp;
 1805                         }
 1806                         sc->next_to_send = end_of_packet;
 1807                         desc->md->md1 |= STP | OWN;
 1808                 } else {
 1809                         sc->pending_transmits++;
 1810                         desc = sc->trans_ring + sc->next_to_send;
 1811                         len = mbuf_to_buffer(head, desc->buff.data);
 1812                         desc->md->md3 = 0;
 1813                         desc->md->md2 = -max(len, ETHER_MIN_LEN - ETHER_CRC_LEN);
 1814                         desc->md->md1 |= OWN | STP | ENP;
 1815                         INC_MD_PTR(sc->next_to_send, sc->ntdre)
 1816                 }
 1817 
 1818                 /* Force an immediate poll of the transmit ring */
 1819                 outw(sc->rdp, TDMD | INEA);
 1820 
 1821                 /*
 1822                  * Set a timer so if the buggy Am7990.h shuts
 1823                  * down we can wake it up.
 1824                  */
 1825 
 1826                 ifp->if_timer = 2;
 1827 
 1828 #if NBPFILTER > 0
 1829                 if (sc->arpcom.ac_if.if_bpf)
 1830                         bpf_mtap(&sc->arpcom.ac_if, head);
 1831 #endif
 1832 
 1833                 if (sc->nic.mem_mode != DMA_MBUF)
 1834                         m_freem(head);
 1835 
 1836         } while (sc->pending_transmits < NDESC(sc->ntdre));
 1837 
 1838         /*
 1839          * Transmit ring is full so set IFF_OACTIVE
 1840          * since we can't buffer any more packets.
 1841          */
 1842 
 1843         sc->arpcom.ac_if.if_flags |= IFF_OACTIVE;
 1844         LNCSTATS(trans_ring_full)
 1845 }
 1846 
 1847 static int
 1848 lnc_ioctl(struct ifnet * ifp, u_long command, caddr_t data)
 1849 {
 1850 
 1851         struct lnc_softc *sc = ifp->if_softc;
 1852         struct ifaddr  *ifa = (struct ifaddr *) data;
 1853         struct ifreq *ifr = (struct ifreq *) data;
 1854         int s, error = 0;
 1855 
 1856         s = splimp();
 1857 
 1858         switch (command) {
 1859         case SIOCSIFADDR:
 1860                 ifp->if_flags |= IFF_UP;
 1861 
 1862                 switch (ifa->ifa_addr->sa_family) {
 1863 #ifdef INET
 1864                 case AF_INET:
 1865                         lnc_init(sc);
 1866                         arp_ifinit((struct arpcom *)ifp, ifa);
 1867                         break;
 1868 #endif
 1869                 default:
 1870                         lnc_init(sc);
 1871                         break;
 1872                 }
 1873                 break;
 1874 
 1875         case SIOCSIFFLAGS:
 1876 #ifdef DEBUG
 1877                 if (ifp->if_flags & IFF_DEBUG)
 1878                         sc->lnc_debug = 1;
 1879                 else
 1880                         sc->lnc_debug = 0;
 1881 #endif
 1882                 if (ifp->if_flags & IFF_PROMISC) {
 1883                         if (!(sc->nic.mode & PROM)) {
 1884                                 sc->nic.mode |= PROM;
 1885                                 lnc_init(sc);
 1886                         }
 1887                 } else if (sc->nic.mode & PROM) {
 1888                         sc->nic.mode &= ~PROM;
 1889                         lnc_init(sc);
 1890                 }
 1891 
 1892                 if ((ifp->if_flags & IFF_ALLMULTI) &&
 1893                     !(sc->flags & LNC_ALLMULTI)) {
 1894                         sc->flags |= LNC_ALLMULTI;
 1895                         lnc_init(sc);
 1896                 } else if (!(ifp->if_flags & IFF_ALLMULTI) &&
 1897                             (sc->flags & LNC_ALLMULTI)) {
 1898                         sc->flags &= ~LNC_ALLMULTI;
 1899                         lnc_init(sc);
 1900                 }
 1901 
 1902                 if ((ifp->if_flags & IFF_UP) == 0 &&
 1903                     (ifp->if_flags & IFF_RUNNING) != 0) {
 1904                         /*
 1905                          * If interface is marked down and it is running,
 1906                          * then stop it.
 1907                          */
 1908                         lnc_stop(sc);
 1909                         ifp->if_flags &= ~IFF_RUNNING;
 1910                 } else if ((ifp->if_flags & IFF_UP) != 0 &&
 1911                            (ifp->if_flags & IFF_RUNNING) == 0) {
 1912                         /*
 1913                          * If interface is marked up and it is stopped, then
 1914                          * start it.
 1915                          */
 1916                         lnc_init(sc);
 1917                 }
 1918                 break;
 1919         case SIOCADDMULTI:
 1920         case SIOCDELMULTI:
 1921                 lnc_init(sc);
 1922                 error = 0;
 1923                 break;
 1924         case SIOCSIFMTU:
 1925                 /*
 1926                  * Set the interface MTU.
 1927                  */
 1928 
 1929                 if (ifr->ifr_mtu > ETHERMTU) {
 1930                         error = EINVAL;
 1931                 } else
 1932                         ifp->if_mtu = ifr->ifr_mtu;
 1933                 break;
 1934         default:
 1935                 error = EINVAL;
 1936         }
 1937         (void) splx(s);
 1938         return error;
 1939 }
 1940 
 1941 static void
 1942 lnc_watchdog(struct ifnet *ifp)
 1943 {
 1944         log(LOG_ERR, "lnc%d: Device timeout -- Resetting\n", ifp->if_unit);
 1945         ifp->if_oerrors++;
 1946         lnc_reset(ifp->if_softc);
 1947 }
 1948 
 1949 #ifdef DEBUG
 1950 void
 1951 lnc_dump_state(struct lnc_softc *sc)
 1952 {
 1953         int             i;
 1954 
 1955         printf("\nDriver/NIC [%d] state dump\n", sc->arpcom.ac_if.if_unit);
 1956         printf("Memory access mode: %b\n", sc->nic.mem_mode, MEM_MODES);
 1957         printf("Host memory\n");
 1958         printf("-----------\n");
 1959 
 1960         printf("Receive ring: base = %p, next = %p\n",
 1961             (void *)sc->recv_ring, (void *)(sc->recv_ring + sc->recv_next));
 1962         for (i = 0; i < NDESC(sc->nrdre); i++)
 1963                 printf("\t%d:%p md = %p buff = %p\n",
 1964                     i, (void *)(sc->recv_ring + i),
 1965                     (void *)(sc->recv_ring + i)->md,
 1966                     (void *)(sc->recv_ring + i)->buff.data);
 1967 
 1968         printf("Transmit ring: base = %p, next = %p\n",
 1969             (void *)sc->trans_ring, (void *)(sc->trans_ring + sc->trans_next));
 1970         for (i = 0; i < NDESC(sc->ntdre); i++)
 1971                 printf("\t%d:%p md = %p buff = %p\n",
 1972                     i, (void *)(sc->trans_ring + i),
 1973                     (void *)(sc->trans_ring + i)->md,
 1974                     (void *)(sc->trans_ring + i)->buff.data);
 1975         printf("Lance memory (may be on host(DMA) or card(SHMEM))\n");
 1976         printf("Init block = %p\n", (void *)sc->init_block);
 1977         printf("\tmode = %b rlen:rdra = %x:%x tlen:tdra = %x:%x\n",
 1978             sc->init_block->mode, INIT_MODE, sc->init_block->rlen,
 1979           sc->init_block->rdra, sc->init_block->tlen, sc->init_block->tdra);
 1980         printf("Receive descriptor ring\n");
 1981         for (i = 0; i < NDESC(sc->nrdre); i++)
 1982                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tMCNT = %u,\tflags = %b\n",
 1983                     i, ((sc->recv_ring + i)->md->md1 & HADR),
 1984                     (sc->recv_ring + i)->md->md0,
 1985                     -(short) (sc->recv_ring + i)->md->md2,
 1986                     (sc->recv_ring + i)->md->md3,
 1987                     (((sc->recv_ring + i)->md->md1 & ~HADR) >> 8), RECV_MD1);
 1988         printf("Transmit descriptor ring\n");
 1989         for (i = 0; i < NDESC(sc->ntdre); i++)
 1990                 printf("\t%d buffer = 0x%x%x, BCNT = %d,\tflags = %b %b\n",
 1991                     i, ((sc->trans_ring + i)->md->md1 & HADR),
 1992                     (sc->trans_ring + i)->md->md0,
 1993                     -(short) (sc->trans_ring + i)->md->md2,
 1994                     ((sc->trans_ring + i)->md->md1 >> 8), TRANS_MD1,
 1995                     ((sc->trans_ring + i)->md->md3 >> 10), TRANS_MD3);
 1996         printf("\nnext_to_send = %x\n", sc->next_to_send);
 1997         printf("\n CSR0 = %b CSR1 = %x CSR2 = %x CSR3 = %x\n\n",
 1998             read_csr(sc, CSR0), CSR0_FLAGS, read_csr(sc, CSR1),
 1999             read_csr(sc, CSR2), read_csr(sc, CSR3));
 2000 
 2001         /* Set RAP back to CSR0 */
 2002         outw(sc->rap, CSR0);
 2003 }
 2004 
 2005 void
 2006 mbuf_dump_chain(struct mbuf * m)
 2007 {
 2008 
 2009 #define MBUF_FLAGS \
 2010         "\2\1M_EXT\2M_PKTHDR\3M_EOR\4UNKNOWN\5M_BCAST\6M_MCAST"
 2011 
 2012         if (!m)
 2013                 log(LOG_DEBUG, "m == NULL\n");
 2014         do {
 2015                 log(LOG_DEBUG, "m = %p\n", (void *)m);
 2016                 log(LOG_DEBUG, "m_hdr.mh_next = %p\n",
 2017                     (void *)m->m_hdr.mh_next);
 2018                 log(LOG_DEBUG, "m_hdr.mh_nextpkt = %p\n",
 2019                     (void *)m->m_hdr.mh_nextpkt);
 2020                 log(LOG_DEBUG, "m_hdr.mh_len = %d\n", m->m_hdr.mh_len);
 2021                 log(LOG_DEBUG, "m_hdr.mh_data = %p\n",
 2022                     (void *)m->m_hdr.mh_data);
 2023                 log(LOG_DEBUG, "m_hdr.mh_type = %d\n", m->m_hdr.mh_type);
 2024                 log(LOG_DEBUG, "m_hdr.mh_flags = %b\n", m->m_hdr.mh_flags,
 2025                     MBUF_FLAGS);
 2026                 if (!(m->m_hdr.mh_flags & (M_PKTHDR | M_EXT)))
 2027                         log(LOG_DEBUG, "M_dat.M_databuf = %p\n",
 2028                             (void *)m->M_dat.M_databuf);
 2029                 else {
 2030                         if (m->m_hdr.mh_flags & M_PKTHDR) {
 2031                                 log(LOG_DEBUG, "M_dat.MH.MH_pkthdr.len = %d\n",
 2032                                     m->M_dat.MH.MH_pkthdr.len);
 2033                                 log(LOG_DEBUG,
 2034                                     "M_dat.MH.MH_pkthdr.rcvif = %p\n",
 2035                                     (void *)m->M_dat.MH.MH_pkthdr.rcvif);
 2036                                 if (!(m->m_hdr.mh_flags & M_EXT))
 2037                                         log(LOG_DEBUG,
 2038                                             "M_dat.MH.MH_dat.MH_databuf = %p\n",
 2039                                         (void *)m->M_dat.MH.MH_dat.MH_databuf);
 2040                         }
 2041                         if (m->m_hdr.mh_flags & M_EXT) {
 2042                                 log(LOG_DEBUG,
 2043                                     "M_dat.MH.MH_dat.MH_ext.ext_buff %p\n",
 2044                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_buf);
 2045                                 log(LOG_DEBUG,
 2046                                     "M_dat.MH.MH_dat.MH_ext.ext_free %p\n",
 2047                                     (void *)m->M_dat.MH.MH_dat.MH_ext.ext_free);
 2048                                 log(LOG_DEBUG,
 2049                                     "M_dat.MH.MH_dat.MH_ext.ext_size %d\n",
 2050                                     m->M_dat.MH.MH_dat.MH_ext.ext_size);
 2051                         }
 2052                 }
 2053         } while ((m = m->m_next) != NULL);
 2054 }
 2055 #endif
 2056 
 2057 #endif

Cache object: b902da7894f7c138129b7d8a1821f919


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