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

Cache object: 662bbeace4d6d9bc677c48b86d8b305b


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