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


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

FreeBSD/Linux Kernel Cross Reference
sys/dev/de/if_de.c

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

    1 /*      $NetBSD: if_de.c,v 1.86 1999/06/01 19:17:59 thorpej Exp $       */
    2 /*-
    3  * Copyright (c) 1994-1997 Matt Thomas (matt@3am-software.com)
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. The name of the author may not be used to endorse or promote products
   12  *    derived from this software without specific prior written permission
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   24  *
   25  * Id: if_de.c,v 1.94 1997/07/03 16:55:07 thomas Exp
   26  */
   27 
   28 /*
   29  * DEC 21040 PCI Ethernet Controller
   30  *
   31  * Written by Matt Thomas
   32  * BPF support code stolen directly from if_ec.c
   33  *
   34  *   This driver supports the DEC DE435 or any other PCI
   35  *   board which support 21040, 21041, or 21140 (mostly).
   36  */
   37 
   38 #include <sys/cdefs.h>
   39 __FBSDID("$FreeBSD: releng/8.4/sys/dev/de/if_de.c 233025 2012-03-16 09:22:59Z scottl $");
   40 
   41 #define TULIP_HDR_DATA
   42 
   43 #include "opt_ddb.h"
   44 
   45 #include <sys/param.h>
   46 #include <sys/systm.h>
   47 #include <sys/endian.h>
   48 #include <sys/ktr.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/socket.h>
   51 #include <sys/sockio.h>
   52 #include <sys/malloc.h>
   53 #include <sys/kernel.h>
   54 #include <sys/module.h>
   55 #include <sys/eventhandler.h>
   56 #include <machine/bus.h>
   57 #include <machine/bus_dma.h>
   58 #include <machine/resource.h>
   59 #include <sys/bus.h>
   60 #include <sys/rman.h>
   61 
   62 #include <net/if.h>
   63 #include <net/if_arp.h>
   64 #include <net/ethernet.h>
   65 #include <net/if_media.h>
   66 #include <net/if_types.h>
   67 #include <net/if_dl.h>
   68 
   69 #include <net/bpf.h>
   70 
   71 #ifdef INET
   72 #include <netinet/in.h>
   73 #include <netinet/if_ether.h>
   74 #endif
   75 
   76 #include <vm/vm.h>
   77 
   78 #include <net/if_var.h>
   79 #include <vm/pmap.h>
   80 #include <dev/pci/pcivar.h>
   81 #include <dev/pci/pcireg.h>
   82 #include <dev/de/dc21040reg.h>
   83 
   84 #ifdef DDB
   85 #include <ddb/ddb.h>
   86 #endif
   87 
   88 /*
   89  * Intel CPUs should use I/O mapped access.
   90  */
   91 #if defined(__i386__)
   92 #define TULIP_IOMAPPED
   93 #endif
   94 
   95 #if 0
   96 /* This enables KTR traces at KTR_DEV. */
   97 #define KTR_TULIP       KTR_DEV
   98 #else
   99 #define KTR_TULIP       0
  100 #endif
  101 
  102 #if 0
  103 /*
  104  * This turns on all sort of debugging stuff and make the
  105  * driver much larger.
  106  */
  107 #define TULIP_DEBUG
  108 #endif
  109 
  110 #if 0
  111 #define TULIP_PERFSTATS
  112 #endif
  113 
  114 #define TULIP_HZ        10
  115 
  116 #include <dev/de/if_devar.h>
  117 
  118 #define SYNC_NONE       0
  119 #define SYNC_RX         1
  120 #define SYNC_TX         2
  121 
  122 /*
  123  * This module supports
  124  *      the DEC 21040 PCI Ethernet Controller.
  125  *      the DEC 21041 PCI Ethernet Controller.
  126  *      the DEC 21140 PCI Fast Ethernet Controller.
  127  */
  128 static void     tulip_addr_filter(tulip_softc_t * const sc);
  129 static int      tulip_ifmedia_change(struct ifnet * const ifp);
  130 static void     tulip_ifmedia_status(struct ifnet * const ifp,
  131                     struct ifmediareq *req);
  132 static void     tulip_init(void *);
  133 static void     tulip_init_locked(tulip_softc_t * const sc);
  134 static void     tulip_intr_shared(void *arg);
  135 static void     tulip_intr_normal(void *arg);
  136 static void     tulip_mii_autonegotiate(tulip_softc_t * const sc,
  137                     const unsigned phyaddr);
  138 static int      tulip_mii_map_abilities(tulip_softc_t * const sc,
  139                     unsigned abilities);
  140 static tulip_media_t
  141                 tulip_mii_phy_readspecific(tulip_softc_t * const sc);
  142 static unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr,
  143                     unsigned regno);
  144 static void     tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr,
  145                     unsigned regno, unsigned data);
  146 static void     tulip_reset(tulip_softc_t * const sc);
  147 static void     tulip_rx_intr(tulip_softc_t * const sc);
  148 static int      tulip_srom_decode(tulip_softc_t * const sc);
  149 static void     tulip_start(struct ifnet *ifp);
  150 static void     tulip_start_locked(tulip_softc_t * const sc);
  151 static struct mbuf *
  152                 tulip_txput(tulip_softc_t * const sc, struct mbuf *m);
  153 static void     tulip_txput_setup(tulip_softc_t * const sc);
  154 struct mbuf *   tulip_dequeue_mbuf(tulip_ringinfo_t *ri, tulip_descinfo_t *di,
  155                     int sync);
  156 static void     tulip_dma_map_addr(void *, bus_dma_segment_t *, int, int);
  157 static void     tulip_dma_map_rxbuf(void *, bus_dma_segment_t *, int,
  158                     bus_size_t, int);
  159 
  160 static void
  161 tulip_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  162 {
  163     bus_addr_t *paddr;
  164 
  165     if (error)
  166         return;
  167 
  168     paddr = arg;
  169     *paddr = segs->ds_addr;
  170 }
  171 
  172 static void
  173 tulip_dma_map_rxbuf(void *arg, bus_dma_segment_t *segs, int nseg,
  174     bus_size_t mapsize, int error)
  175 {
  176     tulip_desc_t *desc;
  177 
  178     if (error)
  179         return;
  180 
  181     desc = arg;
  182     KASSERT(nseg == 1, ("too many DMA segments"));
  183     KASSERT(segs[0].ds_len >= TULIP_RX_BUFLEN, ("receive buffer too small"));
  184 
  185     desc->d_addr1 = segs[0].ds_addr & 0xffffffff;
  186     desc->d_length1 = TULIP_RX_BUFLEN;
  187 #ifdef not_needed
  188     /* These should already always be zero. */
  189     desc->d_addr2 = 0;
  190     desc->d_length2 = 0;
  191 #endif
  192 }
  193 
  194 struct mbuf *
  195 tulip_dequeue_mbuf(tulip_ringinfo_t *ri, tulip_descinfo_t *di, int sync)
  196 {
  197     struct mbuf *m;
  198 
  199     m = di->di_mbuf;
  200     if (m != NULL) {
  201         switch (sync) {
  202         case SYNC_NONE:
  203             break;
  204         case SYNC_RX:
  205             TULIP_RXMAP_POSTSYNC(ri, di);
  206             break;
  207         case SYNC_TX:
  208             TULIP_TXMAP_POSTSYNC(ri, di);
  209             break;
  210         default:
  211             panic("bad sync flag: %d", sync);
  212         }
  213         bus_dmamap_unload(ri->ri_data_tag, *di->di_map);
  214         di->di_mbuf = NULL;
  215     }
  216     return (m);
  217 }
  218 
  219 static void
  220 tulip_timeout_callback(void *arg)
  221 {
  222     tulip_softc_t * const sc = arg;
  223 
  224     TULIP_PERFSTART(timeout)
  225     TULIP_LOCK_ASSERT(sc);
  226 
  227     sc->tulip_flags &= ~TULIP_TIMEOUTPENDING;
  228     sc->tulip_probe_timeout -= 1000 / TULIP_HZ;
  229     (sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_TIMER);
  230 
  231     TULIP_PERFEND(timeout);
  232 }
  233 
  234 static void
  235 tulip_timeout(tulip_softc_t * const sc)
  236 {
  237     TULIP_LOCK_ASSERT(sc);
  238     if (sc->tulip_flags & TULIP_TIMEOUTPENDING)
  239         return;
  240     sc->tulip_flags |= TULIP_TIMEOUTPENDING;
  241     callout_reset(&sc->tulip_callout, (hz + TULIP_HZ / 2) / TULIP_HZ,
  242         tulip_timeout_callback, sc);
  243 }
  244 
  245 static int
  246 tulip_txprobe(tulip_softc_t * const sc)
  247 {
  248     struct mbuf *m;
  249     u_char *enaddr;
  250 
  251     /*
  252      * Before we are sure this is the right media we need
  253      * to send a small packet to make sure there's carrier.
  254      * Strangely, BNC and AUI will "see" receive data if
  255      * either is connected so the transmit is the only way
  256      * to verify the connectivity.
  257      */
  258     TULIP_LOCK_ASSERT(sc);
  259     MGETHDR(m, M_DONTWAIT, MT_DATA);
  260     if (m == NULL)
  261         return 0;
  262     /*
  263      * Construct a LLC TEST message which will point to ourselves.
  264      */
  265     if (sc->tulip_ifp->if_input != NULL)
  266         enaddr = IF_LLADDR(sc->tulip_ifp);
  267     else
  268         enaddr = sc->tulip_enaddr;
  269     bcopy(enaddr, mtod(m, struct ether_header *)->ether_dhost, ETHER_ADDR_LEN);
  270     bcopy(enaddr, mtod(m, struct ether_header *)->ether_shost, ETHER_ADDR_LEN);
  271     mtod(m, struct ether_header *)->ether_type = htons(3);
  272     mtod(m, unsigned char *)[14] = 0;
  273     mtod(m, unsigned char *)[15] = 0;
  274     mtod(m, unsigned char *)[16] = 0xE3;        /* LLC Class1 TEST (no poll) */
  275     m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
  276     /*
  277      * send it!
  278      */
  279     sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
  280     sc->tulip_intrmask |= TULIP_STS_TXINTR;
  281     sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
  282     TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
  283     TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
  284     if ((m = tulip_txput(sc, m)) != NULL)
  285         m_freem(m);
  286     sc->tulip_probe.probe_txprobes++;
  287     return 1;
  288 }
  289 
  290 static void
  291 tulip_media_set(tulip_softc_t * const sc, tulip_media_t media)
  292 {
  293     const tulip_media_info_t *mi = sc->tulip_mediums[media];
  294 
  295     TULIP_LOCK_ASSERT(sc);
  296     if (mi == NULL)
  297         return;
  298 
  299     /*
  300      * If we are switching media, make sure we don't think there's
  301      * any stale RX activity
  302      */
  303     sc->tulip_flags &= ~TULIP_RXACT;
  304     if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
  305         TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
  306         TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        mi->mi_sia_tx_rx);
  307         if (sc->tulip_features & TULIP_HAVE_SIAGP) {
  308             TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_control|mi->mi_sia_general);
  309             DELAY(50);
  310             TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_gp_data|mi->mi_sia_general);
  311         } else {
  312             TULIP_CSR_WRITE(sc, csr_sia_general,  mi->mi_sia_general);
  313         }
  314         TULIP_CSR_WRITE(sc, csr_sia_connectivity, mi->mi_sia_connectivity);
  315     } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
  316 #define TULIP_GPR_CMDBITS       (TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_TXTHRSHLDCTL)
  317         /*
  318          * If the cmdmode bits don't match the currently operating mode,
  319          * set the cmdmode appropriately and reset the chip.
  320          */
  321         if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
  322             sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
  323             sc->tulip_cmdmode |= mi->mi_cmdmode;
  324             tulip_reset(sc);
  325         }
  326         TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
  327         DELAY(10);
  328         TULIP_CSR_WRITE(sc, csr_gp, (u_int8_t) mi->mi_gpdata);
  329     } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
  330         /*
  331          * If the cmdmode bits don't match the currently operating mode,
  332          * set the cmdmode appropriately and reset the chip.
  333          */
  334         if (((mi->mi_cmdmode ^ TULIP_CSR_READ(sc, csr_command)) & TULIP_GPR_CMDBITS) != 0) {
  335             sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
  336             sc->tulip_cmdmode |= mi->mi_cmdmode;
  337             tulip_reset(sc);
  338         }
  339         TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpcontrol);
  340         TULIP_CSR_WRITE(sc, csr_sia_general, mi->mi_gpdata);
  341     } else if (mi->mi_type == TULIP_MEDIAINFO_MII
  342                && sc->tulip_probe_state != TULIP_PROBE_INACTIVE) {
  343         int idx;
  344         if (sc->tulip_features & TULIP_HAVE_SIAGP) {
  345             const u_int8_t *dp;
  346             dp = &sc->tulip_rombuf[mi->mi_reset_offset];
  347             for (idx = 0; idx < mi->mi_reset_length; idx++, dp += 2) {
  348                 DELAY(10);
  349                 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
  350             }
  351             sc->tulip_phyaddr = mi->mi_phyaddr;
  352             dp = &sc->tulip_rombuf[mi->mi_gpr_offset];
  353             for (idx = 0; idx < mi->mi_gpr_length; idx++, dp += 2) {
  354                 DELAY(10);
  355                 TULIP_CSR_WRITE(sc, csr_sia_general, (dp[0] + 256 * dp[1]) << 16);
  356             }
  357         } else {
  358             for (idx = 0; idx < mi->mi_reset_length; idx++) {
  359                 DELAY(10);
  360                 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx]);
  361             }
  362             sc->tulip_phyaddr = mi->mi_phyaddr;
  363             for (idx = 0; idx < mi->mi_gpr_length; idx++) {
  364                 DELAY(10);
  365                 TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx]);
  366             }
  367         }
  368         if (sc->tulip_flags & TULIP_TRYNWAY) {
  369             tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
  370         } else if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
  371             u_int32_t data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_CONTROL);
  372             data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX|PHYCTL_AUTONEG_ENABLE);
  373             sc->tulip_flags &= ~TULIP_DIDNWAY;
  374             if (TULIP_IS_MEDIA_FD(media))
  375                 data |= PHYCTL_FULL_DUPLEX;
  376             if (TULIP_IS_MEDIA_100MB(media))
  377                 data |= PHYCTL_SELECT_100MB;
  378             tulip_mii_writereg(sc, sc->tulip_phyaddr, PHYREG_CONTROL, data);
  379         }
  380     }
  381 }
  382 
  383 static void
  384 tulip_linkup(tulip_softc_t * const sc, tulip_media_t media)
  385 {
  386     TULIP_LOCK_ASSERT(sc);
  387     if ((sc->tulip_flags & TULIP_LINKUP) == 0)
  388         sc->tulip_flags |= TULIP_PRINTLINKUP;
  389     sc->tulip_flags |= TULIP_LINKUP;
  390     sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
  391 #if 0 /* XXX how does with work with ifmedia? */
  392     if ((sc->tulip_flags & TULIP_DIDNWAY) == 0) {
  393         if (sc->tulip_ifp->if_flags & IFF_FULLDUPLEX) {
  394             if (TULIP_CAN_MEDIA_FD(media)
  395                     && sc->tulip_mediums[TULIP_FD_MEDIA_OF(media)] != NULL)
  396                 media = TULIP_FD_MEDIA_OF(media);
  397         } else {
  398             if (TULIP_IS_MEDIA_FD(media)
  399                     && sc->tulip_mediums[TULIP_HD_MEDIA_OF(media)] != NULL)
  400                 media = TULIP_HD_MEDIA_OF(media);
  401         }
  402     }
  403 #endif
  404     if (sc->tulip_media != media) {
  405 #ifdef TULIP_DEBUG
  406         sc->tulip_dbg.dbg_last_media = sc->tulip_media;
  407 #endif
  408         sc->tulip_media = media;
  409         sc->tulip_flags |= TULIP_PRINTMEDIA;
  410         if (TULIP_IS_MEDIA_FD(sc->tulip_media)) {
  411             sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
  412         } else if (sc->tulip_chipid != TULIP_21041 || (sc->tulip_flags & TULIP_DIDNWAY) == 0) {
  413             sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
  414         }
  415     }
  416     /*
  417      * We could set probe_timeout to 0 but setting to 3000 puts this
  418      * in one central place and the only matters is tulip_link is
  419      * followed by a tulip_timeout.  Therefore setting it should not
  420      * result in aberrant behavour.
  421      */
  422     sc->tulip_probe_timeout = 3000;
  423     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
  424     sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TRYNWAY);
  425     if (sc->tulip_flags & TULIP_INRESET) {
  426         tulip_media_set(sc, sc->tulip_media);
  427     } else if (sc->tulip_probe_media != sc->tulip_media) {
  428         /*
  429          * No reason to change media if we have the right media.
  430          */
  431         tulip_reset(sc);
  432     }
  433     tulip_init_locked(sc);
  434 }
  435 
  436 static void
  437 tulip_media_print(tulip_softc_t * const sc)
  438 {
  439 
  440     TULIP_LOCK_ASSERT(sc);
  441     if ((sc->tulip_flags & TULIP_LINKUP) == 0)
  442         return;
  443     if (sc->tulip_flags & TULIP_PRINTMEDIA) {
  444         device_printf(sc->tulip_dev, "enabling %s port\n",
  445             tulip_mediums[sc->tulip_media]);
  446         sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
  447     } else if (sc->tulip_flags & TULIP_PRINTLINKUP) {
  448         device_printf(sc->tulip_dev, "link up\n");
  449         sc->tulip_flags &= ~TULIP_PRINTLINKUP;
  450     }
  451 }
  452 
  453 #if defined(TULIP_DO_GPR_SENSE)
  454 static tulip_media_t
  455 tulip_21140_gpr_media_sense(tulip_softc_t * const sc)
  456 {
  457     struct ifnet *ifp sc->tulip_ifp;
  458     tulip_media_t maybe_media = TULIP_MEDIA_UNKNOWN;
  459     tulip_media_t last_media = TULIP_MEDIA_UNKNOWN;
  460     tulip_media_t media;
  461 
  462     TULIP_LOCK_ASSERT(sc);
  463 
  464     /*
  465      * If one of the media blocks contained a default media flag,
  466      * use that.
  467      */
  468     for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
  469         const tulip_media_info_t *mi;
  470         /*
  471          * Media is not supported (or is full-duplex).
  472          */
  473         if ((mi = sc->tulip_mediums[media]) == NULL || TULIP_IS_MEDIA_FD(media))
  474             continue;
  475         if (mi->mi_type != TULIP_MEDIAINFO_GPR)
  476             continue;
  477 
  478         /*
  479          * Remember the media is this is the "default" media.
  480          */
  481         if (mi->mi_default && maybe_media == TULIP_MEDIA_UNKNOWN)
  482             maybe_media = media;
  483 
  484         /*
  485          * No activity mask?  Can't see if it is active if there's no mask.
  486          */
  487         if (mi->mi_actmask == 0)
  488             continue;
  489 
  490         /*
  491          * Does the activity data match?
  492          */
  493         if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) != mi->mi_actdata)
  494             continue;
  495 
  496 #if defined(TULIP_DEBUG)
  497         device_printf(sc->tulip_dev, "%s: %s: 0x%02x & 0x%02x == 0x%02x\n",
  498             __func__, tulip_mediums[media], TULIP_CSR_READ(sc, csr_gp) & 0xFF,
  499             mi->mi_actmask, mi->mi_actdata);
  500 #endif
  501         /*
  502          * It does!  If this is the first media we detected, then 
  503          * remember this media.  If isn't the first, then there were
  504          * multiple matches which we equate to no match (since we don't
  505          * which to select (if any).
  506          */
  507         if (last_media == TULIP_MEDIA_UNKNOWN) {
  508             last_media = media;
  509         } else if (last_media != media) {
  510             last_media = TULIP_MEDIA_UNKNOWN;
  511         }
  512     }
  513     return (last_media != TULIP_MEDIA_UNKNOWN) ? last_media : maybe_media;
  514 }
  515 #endif /* TULIP_DO_GPR_SENSE */
  516 
  517 static tulip_link_status_t
  518 tulip_media_link_monitor(tulip_softc_t * const sc)
  519 {
  520     const tulip_media_info_t * const mi = sc->tulip_mediums[sc->tulip_media];
  521     tulip_link_status_t linkup = TULIP_LINK_DOWN;
  522 
  523     TULIP_LOCK_ASSERT(sc);
  524     if (mi == NULL) {
  525 #if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
  526         panic("tulip_media_link_monitor: %s: botch at line %d\n",
  527               tulip_mediums[sc->tulip_media],__LINE__);
  528 #else
  529         return TULIP_LINK_UNKNOWN;
  530 #endif
  531     }
  532 
  533 
  534     /*
  535      * Have we seen some packets?  If so, the link must be good.
  536      */
  537     if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
  538         sc->tulip_flags &= ~TULIP_RXACT;
  539         sc->tulip_probe_timeout = 3000;
  540         return TULIP_LINK_UP;
  541     }
  542 
  543     sc->tulip_flags &= ~TULIP_RXACT;
  544     if (mi->mi_type == TULIP_MEDIAINFO_MII) {
  545         u_int32_t status;
  546         /*
  547          * Read the PHY status register.
  548          */
  549         status = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
  550         if (status & PHYSTS_AUTONEG_DONE) {
  551             /*
  552              * If the PHY has completed autonegotiation, see the if the
  553              * remote systems abilities have changed.  If so, upgrade or
  554              * downgrade as appropriate.
  555              */
  556             u_int32_t abilities = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_AUTONEG_ABILITIES);
  557             abilities = (abilities << 6) & status;
  558             if (abilities != sc->tulip_abilities) {
  559 #if defined(TULIP_DEBUG)
  560                 loudprintf("%s(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
  561                            ifp->if_xname, sc->tulip_phyaddr,
  562                            sc->tulip_abilities, abilities);
  563 #endif
  564                 if (tulip_mii_map_abilities(sc, abilities)) {
  565                     tulip_linkup(sc, sc->tulip_probe_media);
  566                     return TULIP_LINK_UP;
  567                 }
  568                 /*
  569                  * if we had selected media because of autonegotiation,
  570                  * we need to probe for the new media.
  571                  */
  572                 sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
  573                 if (sc->tulip_flags & TULIP_DIDNWAY)
  574                     return TULIP_LINK_DOWN;
  575             }
  576         }
  577         /*
  578          * The link is now up.  If was down, say its back up.
  579          */
  580         if ((status & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP)
  581             linkup = TULIP_LINK_UP;
  582     } else if (mi->mi_type == TULIP_MEDIAINFO_GPR) {
  583         /*
  584          * No activity sensor?  Assume all's well.
  585          */
  586         if (mi->mi_actmask == 0)
  587             return TULIP_LINK_UNKNOWN;
  588         /*
  589          * Does the activity data match?
  590          */
  591         if ((TULIP_CSR_READ(sc, csr_gp) & mi->mi_actmask) == mi->mi_actdata)
  592             linkup = TULIP_LINK_UP;
  593     } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
  594         /*
  595          * Assume non TP ok for now.
  596          */
  597         if (!TULIP_IS_MEDIA_TP(sc->tulip_media))
  598             return TULIP_LINK_UNKNOWN;
  599         if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
  600             linkup = TULIP_LINK_UP;
  601 #if defined(TULIP_DEBUG)
  602         if (sc->tulip_probe_timeout <= 0)
  603             device_printf(sc->tulip_dev, "sia status = 0x%08x\n",
  604                     TULIP_CSR_READ(sc, csr_sia_status));
  605 #endif
  606     } else if (mi->mi_type == TULIP_MEDIAINFO_SYM) {
  607         return TULIP_LINK_UNKNOWN;
  608     }
  609     /*
  610      * We will wait for 3 seconds until the link goes into suspect mode.
  611      */
  612     if (sc->tulip_flags & TULIP_LINKUP) {
  613         if (linkup == TULIP_LINK_UP)
  614             sc->tulip_probe_timeout = 3000;
  615         if (sc->tulip_probe_timeout > 0)
  616             return TULIP_LINK_UP;
  617 
  618         sc->tulip_flags &= ~TULIP_LINKUP;
  619         device_printf(sc->tulip_dev, "link down: cable problem?\n");
  620     }
  621 #if defined(TULIP_DEBUG)
  622     sc->tulip_dbg.dbg_link_downed++;
  623 #endif
  624     return TULIP_LINK_DOWN;
  625 }
  626 
  627 static void
  628 tulip_media_poll(tulip_softc_t * const sc, tulip_mediapoll_event_t event)
  629 {
  630 
  631     TULIP_LOCK_ASSERT(sc);
  632 #if defined(TULIP_DEBUG)
  633     sc->tulip_dbg.dbg_events[event]++;
  634 #endif
  635     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE
  636             && event == TULIP_MEDIAPOLL_TIMER) {
  637         switch (tulip_media_link_monitor(sc)) {
  638             case TULIP_LINK_DOWN: {
  639                 /*
  640                  * Link Monitor failed.  Probe for new media.
  641                  */
  642                 event = TULIP_MEDIAPOLL_LINKFAIL;
  643                 break;
  644             }
  645             case TULIP_LINK_UP: {
  646                 /*
  647                  * Check again soon.
  648                  */
  649                 tulip_timeout(sc);
  650                 return;
  651             }
  652             case TULIP_LINK_UNKNOWN: {
  653                 /*
  654                  * We can't tell so don't bother.
  655                  */
  656                 return;
  657             }
  658         }
  659     }
  660 
  661     if (event == TULIP_MEDIAPOLL_LINKFAIL) {
  662         if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE) {
  663             if (TULIP_DO_AUTOSENSE(sc)) {
  664 #if defined(TULIP_DEBUG)
  665                 sc->tulip_dbg.dbg_link_failures++;
  666 #endif
  667                 sc->tulip_media = TULIP_MEDIA_UNKNOWN;
  668                 if (sc->tulip_ifp->if_flags & IFF_UP)
  669                     tulip_reset(sc);    /* restart probe */
  670             }
  671             return;
  672         }
  673 #if defined(TULIP_DEBUG)
  674         sc->tulip_dbg.dbg_link_pollintrs++;
  675 #endif
  676     }
  677 
  678     if (event == TULIP_MEDIAPOLL_START) {
  679         sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
  680         if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE)
  681             return;
  682         sc->tulip_probe_mediamask = 0;
  683         sc->tulip_probe_passes = 0;
  684 #if defined(TULIP_DEBUG)
  685         sc->tulip_dbg.dbg_media_probes++;
  686 #endif
  687         /*
  688          * If the SROM contained an explicit media to use, use it.
  689          */
  690         sc->tulip_cmdmode &= ~(TULIP_CMD_RXRUN|TULIP_CMD_FULLDUPLEX);
  691         sc->tulip_flags |= TULIP_TRYNWAY|TULIP_PROBE1STPASS;
  692         sc->tulip_flags &= ~(TULIP_DIDNWAY|TULIP_PRINTMEDIA|TULIP_PRINTLINKUP);
  693         /*
  694          * connidx is defaulted to a media_unknown type.
  695          */
  696         sc->tulip_probe_media = tulip_srom_conninfo[sc->tulip_connidx].sc_media;
  697         if (sc->tulip_probe_media != TULIP_MEDIA_UNKNOWN) {
  698             tulip_linkup(sc, sc->tulip_probe_media);
  699             tulip_timeout(sc);
  700             return;
  701         }
  702 
  703         if (sc->tulip_features & TULIP_HAVE_GPR) {
  704             sc->tulip_probe_state = TULIP_PROBE_GPRTEST;
  705             sc->tulip_probe_timeout = 2000;
  706         } else {
  707             sc->tulip_probe_media = TULIP_MEDIA_MAX;
  708             sc->tulip_probe_timeout = 0;
  709             sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
  710         }
  711     }
  712 
  713     /*
  714      * Ignore txprobe failures or spurious callbacks.
  715      */
  716     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED
  717             && sc->tulip_probe_state != TULIP_PROBE_MEDIATEST) {
  718         sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
  719         return;
  720     }
  721 
  722     /*
  723      * If we really transmitted a packet, then that's the media we'll use.
  724      */
  725     if (event == TULIP_MEDIAPOLL_TXPROBE_OK || event == TULIP_MEDIAPOLL_LINKPASS) {
  726         if (event == TULIP_MEDIAPOLL_LINKPASS) {
  727             /* XXX Check media status just to be sure */
  728             sc->tulip_probe_media = TULIP_MEDIA_10BASET;
  729 #if defined(TULIP_DEBUG)
  730         } else {
  731             sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
  732 #endif
  733         }
  734         tulip_linkup(sc, sc->tulip_probe_media);
  735         tulip_timeout(sc);
  736         return;
  737     }
  738 
  739     if (sc->tulip_probe_state == TULIP_PROBE_GPRTEST) {
  740 #if defined(TULIP_DO_GPR_SENSE)
  741         /*
  742          * Check for media via the general purpose register.
  743          *
  744          * Try to sense the media via the GPR.  If the same value
  745          * occurs 3 times in a row then just use that.
  746          */
  747         if (sc->tulip_probe_timeout > 0) {
  748             tulip_media_t new_probe_media = tulip_21140_gpr_media_sense(sc);
  749 #if defined(TULIP_DEBUG)
  750             device_printf(sc->tulip_dev, "%s: gpr sensing = %s\n", __func__,
  751                    tulip_mediums[new_probe_media]);
  752 #endif
  753             if (new_probe_media != TULIP_MEDIA_UNKNOWN) {
  754                 if (new_probe_media == sc->tulip_probe_media) {
  755                     if (--sc->tulip_probe_count == 0)
  756                         tulip_linkup(sc, sc->tulip_probe_media);
  757                 } else {
  758                     sc->tulip_probe_count = 10;
  759                 }
  760             }
  761             sc->tulip_probe_media = new_probe_media;
  762             tulip_timeout(sc);
  763             return;
  764         }
  765 #endif /* TULIP_DO_GPR_SENSE */
  766         /*
  767          * Brute force.  We cycle through each of the media types
  768          * and try to transmit a packet.
  769          */
  770         sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
  771         sc->tulip_probe_media = TULIP_MEDIA_MAX;
  772         sc->tulip_probe_timeout = 0;
  773         tulip_timeout(sc);
  774         return;
  775     }
  776 
  777     if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST
  778            && (sc->tulip_features & TULIP_HAVE_MII)) {
  779         tulip_media_t old_media = sc->tulip_probe_media;
  780         tulip_mii_autonegotiate(sc, sc->tulip_phyaddr);
  781         switch (sc->tulip_probe_state) {
  782             case TULIP_PROBE_FAILED:
  783             case TULIP_PROBE_MEDIATEST: {
  784                 /*
  785                  * Try the next media.
  786                  */
  787                 sc->tulip_probe_mediamask |= sc->tulip_mediums[sc->tulip_probe_media]->mi_mediamask;
  788                 sc->tulip_probe_timeout = 0;
  789 #ifdef notyet
  790                 if (sc->tulip_probe_state == TULIP_PROBE_FAILED)
  791                     break;
  792                 if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
  793                     break;
  794                 sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 300;
  795 #endif
  796                 break;
  797             }
  798             case TULIP_PROBE_PHYAUTONEG: {
  799                 return;
  800             }
  801             case TULIP_PROBE_INACTIVE: {
  802                 /*
  803                  * Only probe if we autonegotiated a media that hasn't failed.
  804                  */
  805                 sc->tulip_probe_timeout = 0;
  806                 if (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media)) {
  807                     sc->tulip_probe_media = old_media;
  808                     break;
  809                 }
  810                 tulip_linkup(sc, sc->tulip_probe_media);
  811                 tulip_timeout(sc);
  812                 return;
  813             }
  814             default: {
  815 #if defined(DIAGNOSTIC) || defined(TULIP_DEBUG)
  816                 panic("tulip_media_poll: botch at line %d\n", __LINE__);
  817 #endif
  818                 break;
  819             }
  820         }
  821     }
  822 
  823     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED) {
  824 #if defined(TULIP_DEBUG)
  825         sc->tulip_dbg.dbg_txprobes_failed[sc->tulip_probe_media]++;
  826 #endif
  827         sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
  828         return;
  829     }
  830 
  831     /*
  832      * switch to another media if we tried this one enough.
  833      */
  834     if (/* event == TULIP_MEDIAPOLL_TXPROBE_FAILED || */ sc->tulip_probe_timeout <= 0) {
  835 #if defined(TULIP_DEBUG)
  836         if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
  837             device_printf(sc->tulip_dev, "poll media unknown!\n");
  838             sc->tulip_probe_media = TULIP_MEDIA_MAX;
  839         }
  840 #endif
  841         /*
  842          * Find the next media type to check for.  Full Duplex
  843          * types are not allowed.
  844          */
  845         do {
  846             sc->tulip_probe_media -= 1;
  847             if (sc->tulip_probe_media == TULIP_MEDIA_UNKNOWN) {
  848                 if (++sc->tulip_probe_passes == 3) {
  849                     device_printf(sc->tulip_dev,
  850                         "autosense failed: cable problem?\n");
  851                     if ((sc->tulip_ifp->if_flags & IFF_UP) == 0) {
  852                         sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
  853                         sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
  854                         return;
  855                     }
  856                 }
  857                 sc->tulip_flags ^= TULIP_TRYNWAY;       /* XXX */
  858                 sc->tulip_probe_mediamask = 0;
  859                 sc->tulip_probe_media = TULIP_MEDIA_MAX - 1;
  860             }
  861         } while (sc->tulip_mediums[sc->tulip_probe_media] == NULL
  862                  || (sc->tulip_probe_mediamask & TULIP_BIT(sc->tulip_probe_media))
  863                  || TULIP_IS_MEDIA_FD(sc->tulip_probe_media));
  864 
  865 #if defined(TULIP_DEBUG)
  866         device_printf(sc->tulip_dev, "%s: probing %s\n",
  867                event == TULIP_MEDIAPOLL_TXPROBE_FAILED ? "txprobe failed" : "timeout",
  868                tulip_mediums[sc->tulip_probe_media]);
  869 #endif
  870         sc->tulip_probe_timeout = TULIP_IS_MEDIA_TP(sc->tulip_probe_media) ? 2500 : 1000;
  871         sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
  872         sc->tulip_probe.probe_txprobes = 0;
  873         tulip_reset(sc);
  874         tulip_media_set(sc, sc->tulip_probe_media);
  875         sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
  876     }
  877     tulip_timeout(sc);
  878 
  879     /*
  880      * If this is hanging off a phy, we know are doing NWAY and we have
  881      * forced the phy to a specific speed.  Wait for link up before
  882      * before sending a packet.
  883      */
  884     switch (sc->tulip_mediums[sc->tulip_probe_media]->mi_type) {
  885         case TULIP_MEDIAINFO_MII: {
  886             if (sc->tulip_probe_media != tulip_mii_phy_readspecific(sc))
  887                 return;
  888             break;
  889         }
  890         case TULIP_MEDIAINFO_SIA: {
  891             if (TULIP_IS_MEDIA_TP(sc->tulip_probe_media)) {
  892                 if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL)
  893                     return;
  894                 tulip_linkup(sc, sc->tulip_probe_media);
  895 #ifdef notyet
  896                 if (sc->tulip_features & TULIP_HAVE_MII)
  897                     tulip_timeout(sc);
  898 #endif
  899                 return;
  900             }
  901             break;
  902         }
  903         case TULIP_MEDIAINFO_RESET:
  904         case TULIP_MEDIAINFO_SYM:
  905         case TULIP_MEDIAINFO_NONE:
  906         case TULIP_MEDIAINFO_GPR: {
  907             break;
  908         }
  909     }
  910     /*
  911      * Try to send a packet.
  912      */
  913     tulip_txprobe(sc);
  914 }
  915 
  916 static void
  917 tulip_media_select(tulip_softc_t * const sc)
  918 {
  919     TULIP_LOCK_ASSERT(sc);
  920     if (sc->tulip_features & TULIP_HAVE_GPR) {
  921         TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET|sc->tulip_gpinit);
  922         DELAY(10);
  923         TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpdata);
  924     }
  925     /*
  926      * If this board has no media, just return
  927      */
  928     if (sc->tulip_features & TULIP_HAVE_NOMEDIA)
  929         return;
  930 
  931     if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
  932         TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
  933         (*sc->tulip_boardsw->bd_media_poll)(sc, TULIP_MEDIAPOLL_START);
  934     } else {
  935         tulip_media_set(sc, sc->tulip_media);
  936     }
  937 }
  938 
  939 static void
  940 tulip_21040_mediainfo_init(tulip_softc_t * const sc, tulip_media_t media)
  941 {
  942     TULIP_LOCK_ASSERT(sc);
  943     sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
  944         |TULIP_CMD_BACKOFFCTR;
  945     sc->tulip_ifp->if_baudrate = 10000000;
  946 
  947     if (media == TULIP_MEDIA_10BASET || media == TULIP_MEDIA_UNKNOWN) {
  948         TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[0], 21040, 10BASET);
  949         TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[1], 21040, 10BASET_FD);
  950         sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
  951     }
  952 
  953     if (media == TULIP_MEDIA_AUIBNC || media == TULIP_MEDIA_UNKNOWN) {
  954         TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[2], 21040, AUIBNC);
  955     }
  956 
  957     if (media == TULIP_MEDIA_UNKNOWN) {
  958         TULIP_MEDIAINFO_SIA_INIT(sc, &sc->tulip_mediainfo[3], 21040, EXTSIA);
  959     }
  960 }
  961 
  962 static void
  963 tulip_21040_media_probe(tulip_softc_t * const sc)
  964 {
  965     TULIP_LOCK_ASSERT(sc);
  966     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_UNKNOWN);
  967     return;
  968 }
  969 
  970 static void
  971 tulip_21040_10baset_only_media_probe(tulip_softc_t * const sc)
  972 {
  973     TULIP_LOCK_ASSERT(sc);
  974     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_10BASET);
  975     tulip_media_set(sc, TULIP_MEDIA_10BASET);
  976     sc->tulip_media = TULIP_MEDIA_10BASET;
  977 }
  978 
  979 static void
  980 tulip_21040_10baset_only_media_select(tulip_softc_t * const sc)
  981 {
  982     TULIP_LOCK_ASSERT(sc);
  983     sc->tulip_flags |= TULIP_LINKUP;
  984     if (sc->tulip_media == TULIP_MEDIA_10BASET_FD) {
  985         sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
  986         sc->tulip_flags &= ~TULIP_SQETEST;
  987     } else {
  988         sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
  989         sc->tulip_flags |= TULIP_SQETEST;
  990     }
  991     tulip_media_set(sc, sc->tulip_media);
  992 }
  993 
  994 static void
  995 tulip_21040_auibnc_only_media_probe(tulip_softc_t * const sc)
  996 {
  997     TULIP_LOCK_ASSERT(sc);
  998     tulip_21040_mediainfo_init(sc, TULIP_MEDIA_AUIBNC);
  999     sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
 1000     tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
 1001     sc->tulip_media = TULIP_MEDIA_AUIBNC;
 1002 }
 1003 
 1004 static void
 1005 tulip_21040_auibnc_only_media_select(tulip_softc_t * const sc)
 1006 {
 1007     TULIP_LOCK_ASSERT(sc);
 1008     tulip_media_set(sc, TULIP_MEDIA_AUIBNC);
 1009     sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
 1010 }
 1011 
 1012 static const tulip_boardsw_t tulip_21040_boardsw = {
 1013     TULIP_21040_GENERIC,
 1014     tulip_21040_media_probe,
 1015     tulip_media_select,
 1016     tulip_media_poll,
 1017 };
 1018 
 1019 static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
 1020     TULIP_21040_GENERIC,
 1021     tulip_21040_10baset_only_media_probe,
 1022     tulip_21040_10baset_only_media_select,
 1023     NULL,
 1024 };
 1025 
 1026 static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
 1027     TULIP_21040_GENERIC,
 1028     tulip_21040_auibnc_only_media_probe,
 1029     tulip_21040_auibnc_only_media_select,
 1030     NULL,
 1031 };
 1032 
 1033 static void
 1034 tulip_21041_mediainfo_init(tulip_softc_t * const sc)
 1035 {
 1036     tulip_media_info_t * const mi = sc->tulip_mediainfo;
 1037 
 1038     TULIP_LOCK_ASSERT(sc);
 1039 #ifdef notyet
 1040     if (sc->tulip_revinfo >= 0x20) {
 1041         TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, 10BASET);
 1042         TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, 10BASET_FD);
 1043         TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041P2, AUI);
 1044         TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041P2, BNC);
 1045         return;
 1046     }
 1047 #endif
 1048     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[0], 21041, 10BASET);
 1049     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[1], 21041, 10BASET_FD);
 1050     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[2], 21041, AUI);
 1051     TULIP_MEDIAINFO_SIA_INIT(sc, &mi[3], 21041, BNC);
 1052 }
 1053 
 1054 static void
 1055 tulip_21041_media_probe(tulip_softc_t * const sc)
 1056 {
 1057     TULIP_LOCK_ASSERT(sc);
 1058     sc->tulip_ifp->if_baudrate = 10000000;
 1059     sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
 1060         |TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
 1061     sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
 1062     tulip_21041_mediainfo_init(sc);
 1063 }
 1064 
 1065 static void
 1066 tulip_21041_media_poll(tulip_softc_t * const sc,
 1067     const tulip_mediapoll_event_t event)
 1068 {
 1069     u_int32_t sia_status;
 1070 
 1071     TULIP_LOCK_ASSERT(sc);
 1072 #if defined(TULIP_DEBUG)
 1073     sc->tulip_dbg.dbg_events[event]++;
 1074 #endif
 1075 
 1076     if (event == TULIP_MEDIAPOLL_LINKFAIL) {
 1077         if (sc->tulip_probe_state != TULIP_PROBE_INACTIVE
 1078                 || !TULIP_DO_AUTOSENSE(sc))
 1079             return;
 1080         sc->tulip_media = TULIP_MEDIA_UNKNOWN;
 1081         tulip_reset(sc);        /* start probe */
 1082         return;
 1083     }
 1084 
 1085     /*
 1086      * If we've been been asked to start a poll or link change interrupt
 1087      * restart the probe (and reset the tulip to a known state).
 1088      */
 1089     if (event == TULIP_MEDIAPOLL_START) {
 1090         sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 1091         sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_RXRUN);
 1092 #ifdef notyet
 1093         if (sc->tulip_revinfo >= 0x20) {
 1094             sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
 1095             sc->tulip_flags |= TULIP_DIDNWAY;
 1096         }
 1097 #endif
 1098         TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 1099         sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
 1100         sc->tulip_probe_media = TULIP_MEDIA_10BASET;
 1101         sc->tulip_probe_timeout = TULIP_21041_PROBE_10BASET_TIMEOUT;
 1102         tulip_media_set(sc, TULIP_MEDIA_10BASET);
 1103         tulip_timeout(sc);
 1104         return;
 1105     }
 1106 
 1107     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
 1108         return;
 1109 
 1110     if (event == TULIP_MEDIAPOLL_TXPROBE_OK) {
 1111 #if defined(TULIP_DEBUG)
 1112         sc->tulip_dbg.dbg_txprobes_ok[sc->tulip_probe_media]++;
 1113 #endif
 1114         tulip_linkup(sc, sc->tulip_probe_media);
 1115         return;
 1116     }
 1117 
 1118     sia_status = TULIP_CSR_READ(sc, csr_sia_status);
 1119     TULIP_CSR_WRITE(sc, csr_sia_status, sia_status);
 1120     if ((sia_status & TULIP_SIASTS_LINKFAIL) == 0) {
 1121         if (sc->tulip_revinfo >= 0x20) {
 1122             if (sia_status & (PHYSTS_10BASET_FD << (16 - 6)))
 1123                 sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
 1124         }
 1125         /*
 1126          * If the link has passed LinkPass, 10baseT is the
 1127          * proper media to use.
 1128          */
 1129         tulip_linkup(sc, sc->tulip_probe_media);
 1130         return;
 1131     }
 1132 
 1133     /*
 1134      * wait for up to 2.4 seconds for the link to reach pass state.
 1135      * Only then start scanning the other media for activity.
 1136      * choose media with receive activity over those without.
 1137      */
 1138     if (sc->tulip_probe_media == TULIP_MEDIA_10BASET) {
 1139         if (event != TULIP_MEDIAPOLL_TIMER)
 1140             return;
 1141         if (sc->tulip_probe_timeout > 0
 1142                 && (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
 1143             tulip_timeout(sc);
 1144             return;
 1145         }
 1146         sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
 1147         sc->tulip_flags |= TULIP_WANTRXACT;
 1148         if (sia_status & TULIP_SIASTS_OTHERRXACTIVITY) {
 1149             sc->tulip_probe_media = TULIP_MEDIA_BNC;
 1150         } else {
 1151             sc->tulip_probe_media = TULIP_MEDIA_AUI;
 1152         }
 1153         tulip_media_set(sc, sc->tulip_probe_media);
 1154         tulip_timeout(sc);
 1155         return;
 1156     }
 1157 
 1158     /*
 1159      * If we failed, clear the txprobe active flag.
 1160      */
 1161     if (event == TULIP_MEDIAPOLL_TXPROBE_FAILED)
 1162         sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
 1163 
 1164 
 1165     if (event == TULIP_MEDIAPOLL_TIMER) {
 1166         /*
 1167          * If we've received something, then that's our link!
 1168          */
 1169         if (sc->tulip_flags & TULIP_RXACT) {
 1170             tulip_linkup(sc, sc->tulip_probe_media);
 1171             return;
 1172         }
 1173         /*
 1174          * if no txprobe active  
 1175          */
 1176         if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0
 1177                 && ((sc->tulip_flags & TULIP_WANTRXACT) == 0
 1178                     || (sia_status & TULIP_SIASTS_RXACTIVITY))) {
 1179             sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
 1180             tulip_txprobe(sc);
 1181             tulip_timeout(sc);
 1182             return;
 1183         }
 1184         /*
 1185          * Take 2 passes through before deciding to not
 1186          * wait for receive activity.  Then take another
 1187          * two passes before spitting out a warning.
 1188          */
 1189         if (sc->tulip_probe_timeout <= 0) {
 1190             if (sc->tulip_flags & TULIP_WANTRXACT) {
 1191                 sc->tulip_flags &= ~TULIP_WANTRXACT;
 1192                 sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
 1193             } else {
 1194                 device_printf(sc->tulip_dev,
 1195                     "autosense failed: cable problem?\n");
 1196                 if ((sc->tulip_ifp->if_flags & IFF_UP) == 0) {
 1197                     sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1198                     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
 1199                     return;
 1200                 }
 1201             }
 1202         }
 1203     }
 1204     
 1205     /*
 1206      * Since this media failed to probe, try the other one.
 1207      */
 1208     sc->tulip_probe_timeout = TULIP_21041_PROBE_AUIBNC_TIMEOUT;
 1209     if (sc->tulip_probe_media == TULIP_MEDIA_AUI) {
 1210         sc->tulip_probe_media = TULIP_MEDIA_BNC;
 1211     } else {
 1212         sc->tulip_probe_media = TULIP_MEDIA_AUI;
 1213     }
 1214     tulip_media_set(sc, sc->tulip_probe_media);
 1215     sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
 1216     tulip_timeout(sc);
 1217 }
 1218 
 1219 static const tulip_boardsw_t tulip_21041_boardsw = {
 1220     TULIP_21041_GENERIC,
 1221     tulip_21041_media_probe,
 1222     tulip_media_select,
 1223     tulip_21041_media_poll
 1224 };
 1225 
 1226 static const tulip_phy_attr_t tulip_mii_phy_attrlist[] = {
 1227     { 0x20005c00, 0,            /* 08-00-17 */
 1228       {
 1229         { 0x19, 0x0040, 0x0040 },       /* 10TX */
 1230         { 0x19, 0x0040, 0x0000 },       /* 100TX */
 1231       },
 1232 #if defined(TULIP_DEBUG)
 1233       "NS DP83840",
 1234 #endif
 1235     },
 1236     { 0x0281F400, 0,            /* 00-A0-7D */
 1237       {
 1238         { 0x12, 0x0010, 0x0000 },       /* 10T */
 1239         { },                            /* 100TX */
 1240         { 0x12, 0x0010, 0x0010 },       /* 100T4 */
 1241         { 0x12, 0x0008, 0x0008 },       /* FULL_DUPLEX */
 1242       },
 1243 #if defined(TULIP_DEBUG)
 1244       "Seeq 80C240"
 1245 #endif
 1246     },
 1247 #if 0
 1248     { 0x0015F420, 0,    /* 00-A0-7D */
 1249       {
 1250         { 0x12, 0x0010, 0x0000 },       /* 10T */
 1251         { },                            /* 100TX */
 1252         { 0x12, 0x0010, 0x0010 },       /* 100T4 */
 1253         { 0x12, 0x0008, 0x0008 },       /* FULL_DUPLEX */
 1254       },
 1255 #if defined(TULIP_DEBUG)
 1256       "Broadcom BCM5000"
 1257 #endif
 1258     },
 1259 #endif
 1260     { 0x0281F400, 0,            /* 00-A0-BE */
 1261       {
 1262         { 0x11, 0x8000, 0x0000 },       /* 10T */
 1263         { 0x11, 0x8000, 0x8000 },       /* 100TX */
 1264         { },                            /* 100T4 */
 1265         { 0x11, 0x4000, 0x4000 },       /* FULL_DUPLEX */
 1266       },
 1267 #if defined(TULIP_DEBUG)
 1268       "ICS 1890"
 1269 #endif 
 1270     },
 1271     { 0 }
 1272 };
 1273 
 1274 static tulip_media_t
 1275 tulip_mii_phy_readspecific(tulip_softc_t * const sc)
 1276 {
 1277     const tulip_phy_attr_t *attr;
 1278     u_int16_t data;
 1279     u_int32_t id;
 1280     unsigned idx = 0;
 1281     static const tulip_media_t table[] = {
 1282         TULIP_MEDIA_UNKNOWN,
 1283         TULIP_MEDIA_10BASET,
 1284         TULIP_MEDIA_100BASETX,
 1285         TULIP_MEDIA_100BASET4,
 1286         TULIP_MEDIA_UNKNOWN,
 1287         TULIP_MEDIA_10BASET_FD,
 1288         TULIP_MEDIA_100BASETX_FD,
 1289         TULIP_MEDIA_UNKNOWN
 1290     };
 1291 
 1292     TULIP_LOCK_ASSERT(sc);
 1293 
 1294     /*
 1295      * Don't read phy specific registers if link is not up.
 1296      */
 1297     data = tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_STATUS);
 1298     if ((data & (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS)) != (PHYSTS_LINK_UP|PHYSTS_EXTENDED_REGS))
 1299         return TULIP_MEDIA_UNKNOWN;
 1300 
 1301     id = (tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDLOW) << 16) |
 1302         tulip_mii_readreg(sc, sc->tulip_phyaddr, PHYREG_IDHIGH);
 1303     for (attr = tulip_mii_phy_attrlist;; attr++) {
 1304         if (attr->attr_id == 0)
 1305             return TULIP_MEDIA_UNKNOWN;
 1306         if ((id & ~0x0F) == attr->attr_id)
 1307             break;
 1308     }
 1309 
 1310     if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
 1311         const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
 1312         data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
 1313         if ((data & pm->pm_mask) == pm->pm_value)
 1314             idx = 2;
 1315     }
 1316     if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
 1317         const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
 1318         data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
 1319         if ((data & pm->pm_mask) == pm->pm_value)
 1320             idx = 3;
 1321     }
 1322     if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
 1323         const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
 1324         data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
 1325         if ((data & pm->pm_mask) == pm->pm_value)
 1326             idx = 1;
 1327     } 
 1328     if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
 1329         const tulip_phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
 1330         data = tulip_mii_readreg(sc, sc->tulip_phyaddr, pm->pm_regno);
 1331         idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
 1332     }
 1333     return table[idx];
 1334 }
 1335 
 1336 static unsigned
 1337 tulip_mii_get_phyaddr(tulip_softc_t * const sc, unsigned offset)
 1338 {
 1339     unsigned phyaddr;
 1340 
 1341     TULIP_LOCK_ASSERT(sc);
 1342     for (phyaddr = 1; phyaddr < 32; phyaddr++) {
 1343         unsigned status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
 1344         if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
 1345             continue;
 1346         if (offset == 0)
 1347             return phyaddr;
 1348         offset--;
 1349     }
 1350     if (offset == 0) {
 1351         unsigned status = tulip_mii_readreg(sc, 0, PHYREG_STATUS);
 1352         if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
 1353             return TULIP_MII_NOPHY;
 1354         return 0;
 1355     }
 1356     return TULIP_MII_NOPHY;
 1357 }
 1358 
 1359 static int
 1360 tulip_mii_map_abilities(tulip_softc_t * const sc, unsigned abilities)
 1361 {
 1362     TULIP_LOCK_ASSERT(sc);
 1363     sc->tulip_abilities = abilities;
 1364     if (abilities & PHYSTS_100BASETX_FD) {
 1365         sc->tulip_probe_media = TULIP_MEDIA_100BASETX_FD;
 1366     } else if (abilities & PHYSTS_100BASET4) {
 1367         sc->tulip_probe_media = TULIP_MEDIA_100BASET4;
 1368     } else if (abilities & PHYSTS_100BASETX) {
 1369         sc->tulip_probe_media = TULIP_MEDIA_100BASETX;
 1370     } else if (abilities & PHYSTS_10BASET_FD) {
 1371         sc->tulip_probe_media = TULIP_MEDIA_10BASET_FD;
 1372     } else if (abilities & PHYSTS_10BASET) {
 1373         sc->tulip_probe_media = TULIP_MEDIA_10BASET;
 1374     } else {
 1375         sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
 1376         return 0;
 1377     }
 1378     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
 1379     return 1;
 1380 }
 1381 
 1382 static void
 1383 tulip_mii_autonegotiate(tulip_softc_t * const sc, const unsigned phyaddr)
 1384 {
 1385     struct ifnet *ifp = sc->tulip_ifp;
 1386 
 1387     TULIP_LOCK_ASSERT(sc);
 1388     switch (sc->tulip_probe_state) {
 1389         case TULIP_PROBE_MEDIATEST:
 1390         case TULIP_PROBE_INACTIVE: {
 1391             sc->tulip_flags |= TULIP_DIDNWAY;
 1392             tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, PHYCTL_RESET);
 1393             sc->tulip_probe_timeout = 3000;
 1394             sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_NORMALINTR;
 1395             sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
 1396         }
 1397         /* FALLTHROUGH */
 1398         case TULIP_PROBE_PHYRESET: {
 1399             u_int32_t status;
 1400             u_int32_t data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
 1401             if (data & PHYCTL_RESET) {
 1402                 if (sc->tulip_probe_timeout > 0) {
 1403                     tulip_timeout(sc);
 1404                     return;
 1405                 }
 1406                 printf("%s(phy%d): error: reset of PHY never completed!\n",
 1407                            ifp->if_xname, phyaddr);
 1408                 sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
 1409                 sc->tulip_probe_state = TULIP_PROBE_FAILED;
 1410                 sc->tulip_ifp->if_flags &= ~IFF_UP;
 1411                 sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 1412                 return;
 1413             }
 1414             status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
 1415             if ((status & PHYSTS_CAN_AUTONEG) == 0) {
 1416 #if defined(TULIP_DEBUG)
 1417                 loudprintf("%s(phy%d): autonegotiation disabled\n",
 1418                            ifp->if_xname, phyaddr);
 1419 #endif
 1420                 sc->tulip_flags &= ~TULIP_DIDNWAY;
 1421                 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
 1422                 return;
 1423             }
 1424             if (tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((status >> 6) | 0x01))
 1425                 tulip_mii_writereg(sc, phyaddr, PHYREG_AUTONEG_ADVERTISEMENT, (status >> 6) | 0x01);
 1426             tulip_mii_writereg(sc, phyaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
 1427             data = tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL);
 1428 #if defined(TULIP_DEBUG)
 1429             if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
 1430                 loudprintf("%s(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
 1431                            ifp->if_xname, phyaddr, data);
 1432             else
 1433                 loudprintf("%s(phy%d): autonegotiation restarted: 0x%04x\n",
 1434                            ifp->if_xname, phyaddr, data);
 1435             sc->tulip_dbg.dbg_nway_starts++;
 1436 #endif
 1437             sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
 1438             sc->tulip_probe_timeout = 3000;
 1439         }
 1440         /* FALLTHROUGH */
 1441         case TULIP_PROBE_PHYAUTONEG: {
 1442             u_int32_t status = tulip_mii_readreg(sc, phyaddr, PHYREG_STATUS);
 1443             u_int32_t data;
 1444             if ((status & PHYSTS_AUTONEG_DONE) == 0) {
 1445                 if (sc->tulip_probe_timeout > 0) {
 1446                     tulip_timeout(sc);
 1447                     return;
 1448                 }
 1449 #if defined(TULIP_DEBUG)
 1450                 loudprintf("%s(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
 1451                            ifp->if_xname, phyaddr, status,
 1452                            tulip_mii_readreg(sc, phyaddr, PHYREG_CONTROL));
 1453 #endif
 1454                 sc->tulip_flags &= ~TULIP_DIDNWAY;
 1455                 sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
 1456                 return;
 1457             }
 1458             data = tulip_mii_readreg(sc, phyaddr, PHYREG_AUTONEG_ABILITIES);
 1459 #if defined(TULIP_DEBUG)
 1460             loudprintf("%s(phy%d): autonegotiation complete: 0x%04x\n",
 1461                        ifp->if_xname, phyaddr, data);
 1462 #endif
 1463             data = (data << 6) & status;
 1464             if (!tulip_mii_map_abilities(sc, data))
 1465                 sc->tulip_flags &= ~TULIP_DIDNWAY;
 1466             return;
 1467         }
 1468         default: {
 1469 #if defined(DIAGNOSTIC)
 1470             panic("tulip_media_poll: botch at line %d\n", __LINE__);
 1471 #endif
 1472             break;
 1473         }
 1474     }
 1475 #if defined(TULIP_DEBUG)
 1476     loudprintf("%s(phy%d): autonegotiation failure: state = %d\n",
 1477                ifp->if_xname, phyaddr, sc->tulip_probe_state);
 1478             sc->tulip_dbg.dbg_nway_failures++;
 1479 #endif
 1480 }
 1481 
 1482 static void
 1483 tulip_2114x_media_preset(tulip_softc_t * const sc)
 1484 {
 1485     const tulip_media_info_t *mi = NULL;
 1486     tulip_media_t media = sc->tulip_media;
 1487 
 1488     TULIP_LOCK_ASSERT(sc);
 1489     if (sc->tulip_probe_state == TULIP_PROBE_INACTIVE)
 1490         media = sc->tulip_media;
 1491     else
 1492         media = sc->tulip_probe_media;
 1493     
 1494     sc->tulip_cmdmode &= ~TULIP_CMD_PORTSELECT;
 1495     sc->tulip_flags &= ~TULIP_SQETEST;
 1496     if (media != TULIP_MEDIA_UNKNOWN && media != TULIP_MEDIA_MAX) {
 1497 #if defined(TULIP_DEBUG)
 1498         if (media < TULIP_MEDIA_MAX && sc->tulip_mediums[media] != NULL) {
 1499 #endif
 1500             mi = sc->tulip_mediums[media];
 1501             if (mi->mi_type == TULIP_MEDIAINFO_MII) {
 1502                 sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
 1503             } else if (mi->mi_type == TULIP_MEDIAINFO_GPR
 1504                        || mi->mi_type == TULIP_MEDIAINFO_SYM) {
 1505                 sc->tulip_cmdmode &= ~TULIP_GPR_CMDBITS;
 1506                 sc->tulip_cmdmode |= mi->mi_cmdmode;
 1507             } else if (mi->mi_type == TULIP_MEDIAINFO_SIA) {
 1508                 TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
 1509             }
 1510 #if defined(TULIP_DEBUG)
 1511         } else {
 1512             device_printf(sc->tulip_dev, "preset: bad media %d!\n", media);
 1513         }
 1514 #endif
 1515     }
 1516     switch (media) {
 1517         case TULIP_MEDIA_BNC:
 1518         case TULIP_MEDIA_AUI:
 1519         case TULIP_MEDIA_10BASET: {
 1520             sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
 1521             sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
 1522             sc->tulip_ifp->if_baudrate = 10000000;
 1523             sc->tulip_flags |= TULIP_SQETEST;
 1524             break;
 1525         }
 1526         case TULIP_MEDIA_10BASET_FD: {
 1527             sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
 1528             sc->tulip_ifp->if_baudrate = 10000000;
 1529             break;
 1530         }
 1531         case TULIP_MEDIA_100BASEFX:
 1532         case TULIP_MEDIA_100BASET4:
 1533         case TULIP_MEDIA_100BASETX: {
 1534             sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
 1535             sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
 1536             sc->tulip_ifp->if_baudrate = 100000000;
 1537             break;
 1538         }
 1539         case TULIP_MEDIA_100BASEFX_FD:
 1540         case TULIP_MEDIA_100BASETX_FD: {
 1541             sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_PORTSELECT;
 1542             sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
 1543             sc->tulip_ifp->if_baudrate = 100000000;
 1544             break;
 1545         }
 1546         default: {
 1547             break;
 1548         }
 1549     }
 1550     TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 1551 }
 1552 
 1553 /*
 1554  ********************************************************************
 1555  *  Start of 21140/21140A support which does not use the MII interface 
 1556  */
 1557 
 1558 static void
 1559 tulip_null_media_poll(tulip_softc_t * const sc, tulip_mediapoll_event_t event)
 1560 {
 1561 #if defined(TULIP_DEBUG)
 1562     sc->tulip_dbg.dbg_events[event]++;
 1563 #endif
 1564 #if defined(DIAGNOSTIC)
 1565     device_printf(sc->tulip_dev, "botch(media_poll) at line %d\n", __LINE__);
 1566 #endif
 1567 }
 1568 
 1569 __inline static void
 1570 tulip_21140_mediainit(tulip_softc_t * const sc, tulip_media_info_t * const mip,
 1571     tulip_media_t const media, unsigned gpdata, unsigned cmdmode)
 1572 {
 1573     TULIP_LOCK_ASSERT(sc);
 1574     sc->tulip_mediums[media] = mip;
 1575     mip->mi_type = TULIP_MEDIAINFO_GPR;
 1576     mip->mi_cmdmode = cmdmode;
 1577     mip->mi_gpdata = gpdata;
 1578 }
 1579 
 1580 static void
 1581 tulip_21140_evalboard_media_probe(tulip_softc_t * const sc)
 1582 {
 1583     tulip_media_info_t *mip = sc->tulip_mediainfo;
 1584 
 1585     TULIP_LOCK_ASSERT(sc);
 1586     sc->tulip_gpinit = TULIP_GP_EB_PINS;
 1587     sc->tulip_gpdata = TULIP_GP_EB_INIT;
 1588     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
 1589     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
 1590     TULIP_CSR_WRITE(sc, csr_command,
 1591         TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
 1592         TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
 1593     TULIP_CSR_WRITE(sc, csr_command,
 1594         TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
 1595     DELAY(1000000);
 1596     if ((TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0) {
 1597         sc->tulip_media = TULIP_MEDIA_10BASET;
 1598     } else {
 1599         sc->tulip_media = TULIP_MEDIA_100BASETX;
 1600     }
 1601     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
 1602                           TULIP_GP_EB_INIT,
 1603                           TULIP_CMD_TXTHRSHLDCTL);
 1604     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
 1605                           TULIP_GP_EB_INIT,
 1606                           TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
 1607     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
 1608                           TULIP_GP_EB_INIT,
 1609                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1610                               |TULIP_CMD_SCRAMBLER);
 1611     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
 1612                           TULIP_GP_EB_INIT,
 1613                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1614                               |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
 1615 }
 1616 
 1617 static const tulip_boardsw_t tulip_21140_eb_boardsw = {
 1618     TULIP_21140_DEC_EB,
 1619     tulip_21140_evalboard_media_probe,
 1620     tulip_media_select,
 1621     tulip_null_media_poll,
 1622     tulip_2114x_media_preset,
 1623 };
 1624 
 1625 static void
 1626 tulip_21140_accton_media_probe(tulip_softc_t * const sc)
 1627 {
 1628     tulip_media_info_t *mip = sc->tulip_mediainfo;
 1629     unsigned gpdata;
 1630 
 1631     TULIP_LOCK_ASSERT(sc);
 1632     sc->tulip_gpinit = TULIP_GP_EB_PINS;
 1633     sc->tulip_gpdata = TULIP_GP_EB_INIT;
 1634     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
 1635     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
 1636     TULIP_CSR_WRITE(sc, csr_command,
 1637         TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
 1638         TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
 1639     TULIP_CSR_WRITE(sc, csr_command,
 1640         TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
 1641     DELAY(1000000);
 1642     gpdata = TULIP_CSR_READ(sc, csr_gp);
 1643     if ((gpdata & TULIP_GP_EN1207_UTP_INIT) == 0) {
 1644         sc->tulip_media = TULIP_MEDIA_10BASET;
 1645     } else {
 1646         if ((gpdata & TULIP_GP_EN1207_BNC_INIT) == 0) {
 1647                 sc->tulip_media = TULIP_MEDIA_BNC;
 1648         } else {
 1649                 sc->tulip_media = TULIP_MEDIA_100BASETX;
 1650         }
 1651     }
 1652     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_BNC,
 1653                           TULIP_GP_EN1207_BNC_INIT,
 1654                           TULIP_CMD_TXTHRSHLDCTL);
 1655     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
 1656                           TULIP_GP_EN1207_UTP_INIT,
 1657                           TULIP_CMD_TXTHRSHLDCTL);
 1658     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
 1659                           TULIP_GP_EN1207_UTP_INIT,
 1660                           TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
 1661     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
 1662                           TULIP_GP_EN1207_100_INIT,
 1663                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1664                               |TULIP_CMD_SCRAMBLER);
 1665     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
 1666                           TULIP_GP_EN1207_100_INIT,
 1667                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1668                               |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
 1669 }
 1670 
 1671 static const tulip_boardsw_t tulip_21140_accton_boardsw = {
 1672     TULIP_21140_EN1207,
 1673     tulip_21140_accton_media_probe,
 1674     tulip_media_select,
 1675     tulip_null_media_poll,
 1676     tulip_2114x_media_preset,
 1677 };
 1678 
 1679 static void
 1680 tulip_21140_smc9332_media_probe(tulip_softc_t * const sc)
 1681 {
 1682     tulip_media_info_t *mip = sc->tulip_mediainfo;
 1683     int idx, cnt = 0;
 1684 
 1685     TULIP_LOCK_ASSERT(sc);
 1686     TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
 1687     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
 1688     DELAY(10);  /* Wait 10 microseconds (actually 50 PCI cycles but at 
 1689                    33MHz that comes to two microseconds but wait a
 1690                    bit longer anyways) */
 1691     TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
 1692         TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
 1693     sc->tulip_gpinit = TULIP_GP_SMC_9332_PINS;
 1694     sc->tulip_gpdata = TULIP_GP_SMC_9332_INIT;
 1695     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS|TULIP_GP_PINSET);
 1696     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
 1697     DELAY(200000);
 1698     for (idx = 1000; idx > 0; idx--) {
 1699         u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
 1700         if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
 1701             if (++cnt > 100)
 1702                 break;
 1703         } else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
 1704             break;
 1705         } else {
 1706             cnt = 0;
 1707         }
 1708         DELAY(1000);
 1709     }
 1710     sc->tulip_media = cnt > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
 1711     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
 1712                           TULIP_GP_SMC_9332_INIT,
 1713                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1714                               |TULIP_CMD_SCRAMBLER);
 1715     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
 1716                           TULIP_GP_SMC_9332_INIT,
 1717                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1718                               |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
 1719     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
 1720                           TULIP_GP_SMC_9332_INIT,
 1721                           TULIP_CMD_TXTHRSHLDCTL);
 1722     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
 1723                           TULIP_GP_SMC_9332_INIT,
 1724                           TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
 1725 }
 1726  
 1727 static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
 1728     TULIP_21140_SMC_9332,
 1729     tulip_21140_smc9332_media_probe,
 1730     tulip_media_select,
 1731     tulip_null_media_poll,
 1732     tulip_2114x_media_preset,
 1733 };
 1734 
 1735 static void
 1736 tulip_21140_cogent_em100_media_probe(tulip_softc_t * const sc)
 1737 {
 1738     tulip_media_info_t *mip = sc->tulip_mediainfo;
 1739     u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
 1740 
 1741     TULIP_LOCK_ASSERT(sc);
 1742     sc->tulip_gpinit = TULIP_GP_EM100_PINS;
 1743     sc->tulip_gpdata = TULIP_GP_EM100_INIT;
 1744     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
 1745     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
 1746 
 1747     cmdmode = TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION|TULIP_CMD_MUSTBEONE;
 1748     cmdmode &= ~(TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_SCRAMBLER);
 1749     if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
 1750         TULIP_CSR_WRITE(sc, csr_command, cmdmode);
 1751         sc->tulip_media = TULIP_MEDIA_100BASEFX;
 1752 
 1753         tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX,
 1754                           TULIP_GP_EM100_INIT,
 1755                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION);
 1756         tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASEFX_FD,
 1757                           TULIP_GP_EM100_INIT,
 1758                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1759                               |TULIP_CMD_FULLDUPLEX);
 1760     } else {
 1761         TULIP_CSR_WRITE(sc, csr_command, cmdmode|TULIP_CMD_SCRAMBLER);
 1762         sc->tulip_media = TULIP_MEDIA_100BASETX;
 1763         tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
 1764                           TULIP_GP_EM100_INIT,
 1765                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1766                               |TULIP_CMD_SCRAMBLER);
 1767         tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
 1768                           TULIP_GP_EM100_INIT,
 1769                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1770                               |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
 1771     }
 1772 }
 1773 
 1774 static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
 1775     TULIP_21140_COGENT_EM100,
 1776     tulip_21140_cogent_em100_media_probe,
 1777     tulip_media_select,
 1778     tulip_null_media_poll,
 1779     tulip_2114x_media_preset
 1780 };
 1781 
 1782 static void
 1783 tulip_21140_znyx_zx34x_media_probe(tulip_softc_t * const sc)
 1784 {
 1785     tulip_media_info_t *mip = sc->tulip_mediainfo;
 1786     int cnt10 = 0, cnt100 = 0, idx;
 1787 
 1788     TULIP_LOCK_ASSERT(sc);
 1789     sc->tulip_gpinit = TULIP_GP_ZX34X_PINS;
 1790     sc->tulip_gpdata = TULIP_GP_ZX34X_INIT;
 1791     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
 1792     TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
 1793     TULIP_CSR_WRITE(sc, csr_command,
 1794         TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
 1795         TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
 1796     TULIP_CSR_WRITE(sc, csr_command,
 1797         TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
 1798 
 1799     DELAY(200000);
 1800     for (idx = 1000; idx > 0; idx--) {
 1801         u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
 1802         if ((csr & (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) == (TULIP_GP_ZX34X_LNKFAIL|TULIP_GP_ZX34X_SYMDET|TULIP_GP_ZX34X_SIGDET)) {
 1803             if (++cnt100 > 100)
 1804                 break;
 1805         } else if ((csr & TULIP_GP_ZX34X_LNKFAIL) == 0) {
 1806             if (++cnt10 > 100)
 1807                 break;
 1808         } else {
 1809             cnt10 = 0;
 1810             cnt100 = 0;
 1811         }
 1812         DELAY(1000);
 1813     }
 1814     sc->tulip_media = cnt100 > 100 ? TULIP_MEDIA_100BASETX : TULIP_MEDIA_10BASET;
 1815     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET,
 1816                           TULIP_GP_ZX34X_INIT,
 1817                           TULIP_CMD_TXTHRSHLDCTL);
 1818     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_10BASET_FD,
 1819                           TULIP_GP_ZX34X_INIT,
 1820                           TULIP_CMD_TXTHRSHLDCTL|TULIP_CMD_FULLDUPLEX);
 1821     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX,
 1822                           TULIP_GP_ZX34X_INIT,
 1823                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1824                               |TULIP_CMD_SCRAMBLER);
 1825     tulip_21140_mediainit(sc, mip++, TULIP_MEDIA_100BASETX_FD,
 1826                           TULIP_GP_ZX34X_INIT,
 1827                           TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
 1828                               |TULIP_CMD_SCRAMBLER|TULIP_CMD_FULLDUPLEX);
 1829 }
 1830 
 1831 static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
 1832     TULIP_21140_ZNYX_ZX34X,
 1833     tulip_21140_znyx_zx34x_media_probe,
 1834     tulip_media_select,
 1835     tulip_null_media_poll,
 1836     tulip_2114x_media_preset,
 1837 };
 1838 
 1839 static void
 1840 tulip_2114x_media_probe(tulip_softc_t * const sc)
 1841 {
 1842     TULIP_LOCK_ASSERT(sc);
 1843     sc->tulip_cmdmode |= TULIP_CMD_MUSTBEONE
 1844         |TULIP_CMD_BACKOFFCTR|TULIP_CMD_THRSHLD72;
 1845 }
 1846 
 1847 static const tulip_boardsw_t tulip_2114x_isv_boardsw = {
 1848     TULIP_21140_ISV,
 1849     tulip_2114x_media_probe,
 1850     tulip_media_select,
 1851     tulip_media_poll,
 1852     tulip_2114x_media_preset,
 1853 };
 1854 
 1855 /*
 1856  * ******** END of chip-specific handlers. ***********
 1857  */
 1858 
 1859 /*
 1860  * Code the read the SROM and MII bit streams (I2C)
 1861  */
 1862 #define EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
 1863 
 1864 static void
 1865 tulip_srom_idle(tulip_softc_t * const sc)
 1866 {
 1867     unsigned bit, csr;
 1868     
 1869     csr  = SROMSEL ; EMIT;
 1870     csr  = SROMSEL | SROMRD; EMIT;  
 1871     csr ^= SROMCS; EMIT;
 1872     csr ^= SROMCLKON; EMIT;
 1873 
 1874     /*
 1875      * Write 25 cycles of 0 which will force the SROM to be idle.
 1876      */
 1877     for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
 1878         csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
 1879         csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
 1880     }
 1881     csr ^= SROMCLKOFF; EMIT;
 1882     csr ^= SROMCS; EMIT;
 1883     csr  = 0; EMIT;
 1884 }
 1885      
 1886 static void
 1887 tulip_srom_read(tulip_softc_t * const sc)
 1888 {   
 1889     unsigned idx; 
 1890     const unsigned bitwidth = SROM_BITWIDTH;
 1891     const unsigned cmdmask = (SROMCMD_RD << bitwidth);
 1892     const unsigned msb = 1 << (bitwidth + 3 - 1);
 1893     unsigned lastidx = (1 << bitwidth) - 1;
 1894 
 1895     tulip_srom_idle(sc);
 1896 
 1897     for (idx = 0; idx <= lastidx; idx++) {
 1898         unsigned lastbit, data, bits, bit, csr;
 1899         csr  = SROMSEL ;                EMIT;
 1900         csr  = SROMSEL | SROMRD;        EMIT;
 1901         csr ^= SROMCSON;                EMIT;
 1902         csr ^=            SROMCLKON;    EMIT;
 1903     
 1904         lastbit = 0;
 1905         for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
 1906             const unsigned thisbit = bits & msb;
 1907             csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
 1908             if (thisbit != lastbit) {
 1909                 csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
 1910             } else {
 1911                 EMIT;
 1912             }
 1913             csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
 1914             lastbit = thisbit;
 1915         }
 1916         csr ^= SROMCLKOFF; EMIT;
 1917 
 1918         for (data = 0, bits = 0; bits < 16; bits++) {
 1919             data <<= 1;
 1920             csr ^= SROMCLKON; EMIT;     /* clock high; data valid */ 
 1921             data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
 1922             csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
 1923         }
 1924         sc->tulip_rombuf[idx*2] = data & 0xFF;
 1925         sc->tulip_rombuf[idx*2+1] = data >> 8;
 1926         csr  = SROMSEL | SROMRD; EMIT;
 1927         csr  = 0; EMIT;
 1928     }
 1929     tulip_srom_idle(sc);
 1930 }
 1931 
 1932 #define MII_EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); DELAY(1); } while (0)
 1933 
 1934 static void
 1935 tulip_mii_writebits(tulip_softc_t * const sc, unsigned data, unsigned bits)
 1936 {
 1937     unsigned msb = 1 << (bits - 1);
 1938     unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
 1939     unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
 1940 
 1941     TULIP_LOCK_ASSERT(sc);
 1942     csr |= MII_WR; MII_EMIT;            /* clock low; assert write */
 1943 
 1944     for (; bits > 0; bits--, data <<= 1) {
 1945         const unsigned thisbit = data & msb;
 1946         if (thisbit != lastbit) {
 1947             csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
 1948         }
 1949         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
 1950         lastbit = thisbit;
 1951         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
 1952     }
 1953 }
 1954 
 1955 static void
 1956 tulip_mii_turnaround(tulip_softc_t * const sc, unsigned cmd)
 1957 {
 1958     unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
 1959 
 1960     TULIP_LOCK_ASSERT(sc);
 1961     if (cmd == MII_WRCMD) {
 1962         csr |= MII_DOUT; MII_EMIT;      /* clock low; change data */
 1963         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
 1964         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
 1965         csr ^= MII_DOUT; MII_EMIT;      /* clock low; change data */
 1966     } else {
 1967         csr |= MII_RD; MII_EMIT;        /* clock low; switch to read */
 1968     }
 1969     csr ^= MII_CLKON; MII_EMIT;         /* clock high; data valid */
 1970     csr ^= MII_CLKOFF; MII_EMIT;        /* clock low; data not valid */
 1971 }
 1972 
 1973 static unsigned
 1974 tulip_mii_readbits(tulip_softc_t * const sc)
 1975 {
 1976     unsigned data;
 1977     unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
 1978     int idx;
 1979 
 1980     TULIP_LOCK_ASSERT(sc);
 1981     for (idx = 0, data = 0; idx < 16; idx++) {
 1982         data <<= 1;     /* this is NOOP on the first pass through */
 1983         csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
 1984         if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
 1985             data |= 1;
 1986         csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
 1987     }
 1988     csr ^= MII_RD; MII_EMIT;            /* clock low; turn off read */
 1989 
 1990     return data;
 1991 }
 1992 
 1993 static unsigned
 1994 tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno)
 1995 {
 1996     unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
 1997     unsigned data;
 1998 
 1999     TULIP_LOCK_ASSERT(sc);
 2000     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
 2001     tulip_mii_writebits(sc, MII_PREAMBLE, 32);
 2002     tulip_mii_writebits(sc, MII_RDCMD, 8);
 2003     tulip_mii_writebits(sc, devaddr, 5);
 2004     tulip_mii_writebits(sc, regno, 5);
 2005     tulip_mii_turnaround(sc, MII_RDCMD);
 2006 
 2007     data = tulip_mii_readbits(sc);
 2008 #if defined(TULIP_DEBUG)
 2009     sc->tulip_dbg.dbg_phyregs[regno][0] = data;
 2010     sc->tulip_dbg.dbg_phyregs[regno][1]++;
 2011 #endif
 2012     return data;
 2013 }
 2014 
 2015 static void
 2016 tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno,
 2017     unsigned data)
 2018 {
 2019     unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
 2020 
 2021     TULIP_LOCK_ASSERT(sc);
 2022     csr &= ~(MII_RD|MII_CLK); MII_EMIT;
 2023     tulip_mii_writebits(sc, MII_PREAMBLE, 32);
 2024     tulip_mii_writebits(sc, MII_WRCMD, 8);
 2025     tulip_mii_writebits(sc, devaddr, 5);
 2026     tulip_mii_writebits(sc, regno, 5);
 2027     tulip_mii_turnaround(sc, MII_WRCMD);
 2028     tulip_mii_writebits(sc, data, 16);
 2029 #if defined(TULIP_DEBUG)
 2030     sc->tulip_dbg.dbg_phyregs[regno][2] = data;
 2031     sc->tulip_dbg.dbg_phyregs[regno][3]++;
 2032 #endif
 2033 }
 2034 
 2035 #define tulip_mchash(mca)       (ether_crc32_le(mca, 6) & 0x1FF)
 2036 #define tulip_srom_crcok(databuf)       ( \
 2037     ((ether_crc32_le(databuf, 126) & 0xFFFFU) ^ 0xFFFFU) == \
 2038      ((databuf)[126] | ((databuf)[127] << 8)))
 2039 
 2040 static void
 2041 tulip_identify_dec_nic(tulip_softc_t * const sc)
 2042 {
 2043     TULIP_LOCK_ASSERT(sc);
 2044     strcpy(sc->tulip_boardid, "DEC ");
 2045 #define D0      4
 2046     if (sc->tulip_chipid <= TULIP_21040)
 2047         return;
 2048     if (bcmp(sc->tulip_rombuf + 29, "DE500", 5) == 0
 2049         || bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
 2050         bcopy(sc->tulip_rombuf + 29, &sc->tulip_boardid[D0], 8);
 2051         sc->tulip_boardid[D0+8] = ' ';
 2052     }
 2053 #undef D0
 2054 }
 2055 
 2056 static void
 2057 tulip_identify_znyx_nic(tulip_softc_t * const sc)
 2058 {
 2059     unsigned id = 0;
 2060 
 2061     TULIP_LOCK_ASSERT(sc);
 2062     strcpy(sc->tulip_boardid, "ZNYX ZX3XX ");
 2063     if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
 2064         unsigned znyx_ptr;
 2065         sc->tulip_boardid[8] = '4';
 2066         znyx_ptr = sc->tulip_rombuf[124] + 256 * sc->tulip_rombuf[125];
 2067         if (znyx_ptr < 26 || znyx_ptr > 116) {
 2068             sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
 2069             return;
 2070         }
 2071         /* ZX344 = 0010 .. 0013FF
 2072          */
 2073         if (sc->tulip_rombuf[znyx_ptr] == 0x4A
 2074                 && sc->tulip_rombuf[znyx_ptr + 1] == 0x52
 2075                 && sc->tulip_rombuf[znyx_ptr + 2] == 0x01) {
 2076             id = sc->tulip_rombuf[znyx_ptr + 5] + 256 * sc->tulip_rombuf[znyx_ptr + 4];
 2077             if ((id >> 8) == (TULIP_ZNYX_ID_ZX342 >> 8)) {
 2078                 sc->tulip_boardid[9] = '2';
 2079                 if (id == TULIP_ZNYX_ID_ZX342B) {
 2080                     sc->tulip_boardid[10] = 'B';
 2081                     sc->tulip_boardid[11] = ' ';
 2082                 }
 2083                 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
 2084             } else if (id == TULIP_ZNYX_ID_ZX344) {
 2085                 sc->tulip_boardid[10] = '4';
 2086                 sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
 2087             } else if (id == TULIP_ZNYX_ID_ZX345) {
 2088                 sc->tulip_boardid[9] = (sc->tulip_rombuf[19] > 1) ? '8' : '5';
 2089             } else if (id == TULIP_ZNYX_ID_ZX346) {
 2090                 sc->tulip_boardid[9] = '6';
 2091             } else if (id == TULIP_ZNYX_ID_ZX351) {
 2092                 sc->tulip_boardid[8] = '5';
 2093                 sc->tulip_boardid[9] = '1';
 2094             }
 2095         }
 2096         if (id == 0) {
 2097             /*
 2098              * Assume it's a ZX342...
 2099              */
 2100             sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
 2101         }
 2102         return;
 2103     }
 2104     sc->tulip_boardid[8] = '1';
 2105     if (sc->tulip_chipid == TULIP_21041) {
 2106         sc->tulip_boardid[10] = '1';
 2107         return;
 2108     }
 2109     if (sc->tulip_rombuf[32] == 0x4A && sc->tulip_rombuf[33] == 0x52) {
 2110         id = sc->tulip_rombuf[37] + 256 * sc->tulip_rombuf[36];
 2111         if (id == TULIP_ZNYX_ID_ZX312T) {
 2112             sc->tulip_boardid[9] = '2';
 2113             sc->tulip_boardid[10] = 'T';
 2114             sc->tulip_boardid[11] = ' ';
 2115             sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
 2116         } else if (id == TULIP_ZNYX_ID_ZX314_INTA) {
 2117             sc->tulip_boardid[9] = '4';
 2118             sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
 2119             sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
 2120         } else if (id == TULIP_ZNYX_ID_ZX314) {
 2121             sc->tulip_boardid[9] = '4';
 2122             sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
 2123             sc->tulip_features |= TULIP_HAVE_BASEROM;
 2124         } else if (id == TULIP_ZNYX_ID_ZX315_INTA) {
 2125             sc->tulip_boardid[9] = '5';
 2126             sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
 2127         } else if (id == TULIP_ZNYX_ID_ZX315) {
 2128             sc->tulip_boardid[9] = '5';
 2129             sc->tulip_features |= TULIP_HAVE_BASEROM;
 2130         } else {
 2131             id = 0;
 2132         }
 2133     }               
 2134     if (id == 0) {
 2135         if ((sc->tulip_enaddr[3] & ~3) == 0xF0 && (sc->tulip_enaddr[5] & 2) == 0) {
 2136             sc->tulip_boardid[9] = '4';
 2137             sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
 2138             sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
 2139         } else if ((sc->tulip_enaddr[3] & ~3) == 0xF4 && (sc->tulip_enaddr[5] & 1) == 0) {
 2140             sc->tulip_boardid[9] = '5';
 2141             sc->tulip_boardsw = &tulip_21040_boardsw;
 2142             sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
 2143         } else if ((sc->tulip_enaddr[3] & ~3) == 0xEC) {
 2144             sc->tulip_boardid[9] = '2';
 2145             sc->tulip_boardsw = &tulip_21040_boardsw;
 2146         }
 2147     }
 2148 }
 2149 
 2150 static void
 2151 tulip_identify_smc_nic(tulip_softc_t * const sc)
 2152 {
 2153     u_int32_t id1, id2, ei;
 2154     int auibnc = 0, utp = 0;
 2155     char *cp;
 2156 
 2157     TULIP_LOCK_ASSERT(sc);
 2158     strcpy(sc->tulip_boardid, "SMC ");
 2159     if (sc->tulip_chipid == TULIP_21041)
 2160         return;
 2161     if (sc->tulip_chipid != TULIP_21040) {
 2162         if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
 2163             strcpy(&sc->tulip_boardid[4], "9332DST ");
 2164             sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
 2165         } else if (sc->tulip_features & (TULIP_HAVE_BASEROM|TULIP_HAVE_SLAVEDROM)) {
 2166             strcpy(&sc->tulip_boardid[4], "9334BDT ");
 2167         } else {
 2168             strcpy(&sc->tulip_boardid[4], "9332BDT ");
 2169         }
 2170         return;
 2171     }
 2172     id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
 2173     id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
 2174     ei  = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
 2175 
 2176     strcpy(&sc->tulip_boardid[4], "8432");
 2177     cp = &sc->tulip_boardid[8];
 2178     if ((id1 & 1) == 0)
 2179         *cp++ = 'B', auibnc = 1;
 2180     if ((id1 & 0xFF) > 0x32)
 2181         *cp++ = 'T', utp = 1;
 2182     if ((id1 & 0x4000) == 0)
 2183         *cp++ = 'A', auibnc = 1;
 2184     if (id2 == 0x15) {
 2185         sc->tulip_boardid[7] = '4';
 2186         *cp++ = '-';
 2187         *cp++ = 'C';
 2188         *cp++ = 'H';
 2189         *cp++ = (ei ? '2' : '1');
 2190     }
 2191     *cp++ = ' ';
 2192     *cp = '\0';
 2193     if (utp && !auibnc)
 2194         sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
 2195     else if (!utp && auibnc)
 2196         sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
 2197 }
 2198 
 2199 static void
 2200 tulip_identify_cogent_nic(tulip_softc_t * const sc)
 2201 {
 2202     TULIP_LOCK_ASSERT(sc);
 2203     strcpy(sc->tulip_boardid, "Cogent ");
 2204     if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
 2205         if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100TX_ID) {
 2206             strcat(sc->tulip_boardid, "EM100TX ");
 2207             sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
 2208 #if defined(TULIP_COGENT_EM110TX_ID)
 2209         } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM110TX_ID) {
 2210             strcat(sc->tulip_boardid, "EM110TX ");
 2211             sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
 2212 #endif
 2213         } else if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100FX_ID) {
 2214             strcat(sc->tulip_boardid, "EM100FX ");
 2215             sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
 2216         }
 2217         /*
 2218          * Magic number (0x24001109U) is the SubVendor (0x2400) and
 2219          * SubDevId (0x1109) for the ANA6944TX (EM440TX).
 2220          */
 2221         if (*(u_int32_t *) sc->tulip_rombuf == 0x24001109U
 2222                 && (sc->tulip_features & TULIP_HAVE_BASEROM)) {
 2223             /*
 2224              * Cogent (Adaptec) is still mapping all INTs to INTA of
 2225              * first 21140.  Dumb!  Dumb!
 2226              */
 2227             strcat(sc->tulip_boardid, "EM440TX ");
 2228             sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
 2229         }
 2230     } else if (sc->tulip_chipid == TULIP_21040) {
 2231         sc->tulip_features |= TULIP_HAVE_SHAREDINTR|TULIP_HAVE_BASEROM;
 2232     }
 2233 }
 2234 
 2235 static void
 2236 tulip_identify_accton_nic(tulip_softc_t * const sc)
 2237 {
 2238     TULIP_LOCK_ASSERT(sc);
 2239     strcpy(sc->tulip_boardid, "ACCTON ");
 2240     switch (sc->tulip_chipid) {
 2241         case TULIP_21140A:
 2242             strcat(sc->tulip_boardid, "EN1207 ");
 2243             if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
 2244                 sc->tulip_boardsw = &tulip_21140_accton_boardsw;
 2245             break;
 2246         case TULIP_21140:
 2247             strcat(sc->tulip_boardid, "EN1207TX ");
 2248             if (sc->tulip_boardsw != &tulip_2114x_isv_boardsw)
 2249                 sc->tulip_boardsw = &tulip_21140_eb_boardsw;
 2250             break;
 2251         case TULIP_21040:
 2252             strcat(sc->tulip_boardid, "EN1203 ");
 2253             sc->tulip_boardsw = &tulip_21040_boardsw;
 2254             break;
 2255         case TULIP_21041:
 2256             strcat(sc->tulip_boardid, "EN1203 ");
 2257             sc->tulip_boardsw = &tulip_21041_boardsw;
 2258             break;
 2259         default:
 2260             sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
 2261             break;
 2262     }
 2263 }
 2264 
 2265 static void
 2266 tulip_identify_asante_nic(tulip_softc_t * const sc)
 2267 {
 2268     TULIP_LOCK_ASSERT(sc);
 2269     strcpy(sc->tulip_boardid, "Asante ");
 2270     if ((sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A)
 2271             && sc->tulip_boardsw != &tulip_2114x_isv_boardsw) {
 2272         tulip_media_info_t *mi = sc->tulip_mediainfo;
 2273         int idx;
 2274         /*
 2275          * The Asante Fast Ethernet doesn't always ship with a valid
 2276          * new format SROM.  So if isn't in the new format, we cheat
 2277          * set it up as if we had.
 2278          */
 2279 
 2280         sc->tulip_gpinit = TULIP_GP_ASANTE_PINS;
 2281         sc->tulip_gpdata = 0;
 2282 
 2283         TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PINS|TULIP_GP_PINSET);
 2284         TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ASANTE_PHYRESET);
 2285         DELAY(100);
 2286         TULIP_CSR_WRITE(sc, csr_gp, 0);
 2287 
 2288         mi->mi_type = TULIP_MEDIAINFO_MII;
 2289         mi->mi_gpr_length = 0;
 2290         mi->mi_gpr_offset = 0;
 2291         mi->mi_reset_length = 0;
 2292         mi->mi_reset_offset = 0;
 2293 
 2294         mi->mi_phyaddr = TULIP_MII_NOPHY;
 2295         for (idx = 20; idx > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx--) {
 2296             DELAY(10000);
 2297             mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, 0);
 2298         }
 2299         if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
 2300             device_printf(sc->tulip_dev, "can't find phy 0\n");
 2301             return;
 2302         }
 2303 
 2304         sc->tulip_features |= TULIP_HAVE_MII;
 2305         mi->mi_capabilities  = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
 2306         mi->mi_advertisement = PHYSTS_10BASET|PHYSTS_10BASET_FD|PHYSTS_100BASETX|PHYSTS_100BASETX_FD;
 2307         mi->mi_full_duplex   = PHYSTS_10BASET_FD|PHYSTS_100BASETX_FD;
 2308         mi->mi_tx_threshold  = PHYSTS_10BASET|PHYSTS_10BASET_FD;
 2309         TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
 2310         TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
 2311         TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
 2312         TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
 2313         TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
 2314         mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
 2315             tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
 2316 
 2317         sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
 2318     }
 2319 }
 2320 
 2321 static void
 2322 tulip_identify_compex_nic(tulip_softc_t * const sc)
 2323 {
 2324     TULIP_LOCK_ASSERT(sc);
 2325     strcpy(sc->tulip_boardid, "COMPEX ");
 2326     if (sc->tulip_chipid == TULIP_21140A) {
 2327         int root_unit;
 2328         tulip_softc_t *root_sc = NULL;
 2329 
 2330         strcat(sc->tulip_boardid, "400TX/PCI ");
 2331         /*
 2332          * All 4 chips on these boards share an interrupt.  This code
 2333          * copied from tulip_read_macaddr.
 2334          */
 2335         sc->tulip_features |= TULIP_HAVE_SHAREDINTR;
 2336         for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
 2337             root_sc = tulips[root_unit];
 2338             if (root_sc == NULL
 2339                 || !(root_sc->tulip_features & TULIP_HAVE_SLAVEDINTR))
 2340                 break;
 2341             root_sc = NULL;
 2342         }
 2343         if (root_sc != NULL
 2344             && root_sc->tulip_chipid == sc->tulip_chipid
 2345             && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
 2346             sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
 2347             sc->tulip_slaves = root_sc->tulip_slaves;
 2348             root_sc->tulip_slaves = sc;
 2349         } else if(sc->tulip_features & TULIP_HAVE_SLAVEDINTR) {
 2350             printf("\nCannot find master device for %s interrupts",
 2351                    sc->tulip_ifp->if_xname);
 2352         }
 2353     } else {
 2354         strcat(sc->tulip_boardid, "unknown ");
 2355     }
 2356     /*      sc->tulip_boardsw = &tulip_21140_eb_boardsw; */
 2357     return;
 2358 }
 2359 
 2360 static int
 2361 tulip_srom_decode(tulip_softc_t * const sc)
 2362 {
 2363     unsigned idx1, idx2, idx3;
 2364 
 2365     const tulip_srom_header_t *shp = (const tulip_srom_header_t *) &sc->tulip_rombuf[0];
 2366     const tulip_srom_adapter_info_t *saip = (const tulip_srom_adapter_info_t *) (shp + 1);
 2367     tulip_srom_media_t srom_media;
 2368     tulip_media_info_t *mi = sc->tulip_mediainfo;
 2369     const u_int8_t *dp;
 2370     u_int32_t leaf_offset, blocks, data;
 2371 
 2372     TULIP_LOCK_ASSERT(sc);
 2373     for (idx1 = 0; idx1 < shp->sh_adapter_count; idx1++, saip++) {
 2374         if (shp->sh_adapter_count == 1)
 2375             break;
 2376         if (saip->sai_device == sc->tulip_pci_devno)
 2377             break;
 2378     }
 2379     /*
 2380      * Didn't find the right media block for this card.
 2381      */
 2382     if (idx1 == shp->sh_adapter_count)
 2383         return 0;
 2384 
 2385     /*
 2386      * Save the hardware address.
 2387      */
 2388     bcopy(shp->sh_ieee802_address, sc->tulip_enaddr, 6);
 2389     /*
 2390      * If this is a multiple port card, add the adapter index to the last
 2391      * byte of the hardware address.  (if it isn't multiport, adding 0
 2392      * won't hurt.
 2393      */
 2394     sc->tulip_enaddr[5] += idx1;
 2395 
 2396     leaf_offset = saip->sai_leaf_offset_lowbyte
 2397         + saip->sai_leaf_offset_highbyte * 256;
 2398     dp = sc->tulip_rombuf + leaf_offset;
 2399         
 2400     sc->tulip_conntype = (tulip_srom_connection_t) (dp[0] + dp[1] * 256); dp += 2;
 2401 
 2402     for (idx2 = 0;; idx2++) {
 2403         if (tulip_srom_conninfo[idx2].sc_type == sc->tulip_conntype
 2404                 || tulip_srom_conninfo[idx2].sc_type == TULIP_SROM_CONNTYPE_NOT_USED)
 2405             break;
 2406     }
 2407     sc->tulip_connidx = idx2;
 2408 
 2409     if (sc->tulip_chipid == TULIP_21041) {
 2410         blocks = *dp++;
 2411         for (idx2 = 0; idx2 < blocks; idx2++) {
 2412             tulip_media_t media;
 2413             data = *dp++;
 2414             srom_media = (tulip_srom_media_t) (data & 0x3F);
 2415             for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
 2416                 if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
 2417                     break;
 2418             }
 2419             media = tulip_srom_mediums[idx3].sm_type;
 2420             if (media != TULIP_MEDIA_UNKNOWN) {
 2421                 if (data & TULIP_SROM_21041_EXTENDED) {
 2422                     mi->mi_type = TULIP_MEDIAINFO_SIA;
 2423                     sc->tulip_mediums[media] = mi;
 2424                     mi->mi_sia_connectivity = dp[0] + dp[1] * 256;
 2425                     mi->mi_sia_tx_rx        = dp[2] + dp[3] * 256;
 2426                     mi->mi_sia_general      = dp[4] + dp[5] * 256;
 2427                     mi++;
 2428                 } else {
 2429                     switch (media) {
 2430                         case TULIP_MEDIA_BNC: {
 2431                             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC);
 2432                             mi++;
 2433                             break;
 2434                         }
 2435                         case TULIP_MEDIA_AUI: {
 2436                             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI);
 2437                             mi++;
 2438                             break;
 2439                         }
 2440                         case TULIP_MEDIA_10BASET: {
 2441                             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET);
 2442                             mi++;
 2443                             break;
 2444                         }
 2445                         case TULIP_MEDIA_10BASET_FD: {
 2446                             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD);
 2447                             mi++;
 2448                             break;
 2449                         }
 2450                         default: {
 2451                             break;
 2452                         }
 2453                     }
 2454                 }
 2455             }
 2456             if (data & TULIP_SROM_21041_EXTENDED)       
 2457                 dp += 6;
 2458         }
 2459 #ifdef notdef
 2460         if (blocks == 0) {
 2461             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, BNC); mi++;
 2462             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, AUI); mi++;
 2463             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET); mi++;
 2464             TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21041, 10BASET_FD); mi++;
 2465         }
 2466 #endif
 2467     } else {
 2468         unsigned length, type;
 2469         tulip_media_t gp_media = TULIP_MEDIA_UNKNOWN;
 2470         if (sc->tulip_features & TULIP_HAVE_GPR)
 2471             sc->tulip_gpinit = *dp++;
 2472         blocks = *dp++;
 2473         for (idx2 = 0; idx2 < blocks; idx2++) {
 2474             const u_int8_t *ep;
 2475             if ((*dp & 0x80) == 0) {
 2476                 length = 4;
 2477                 type = 0;
 2478             } else {
 2479                 length = (*dp++ & 0x7f) - 1;
 2480                 type = *dp++ & 0x3f;
 2481             }
 2482             ep = dp + length;
 2483             switch (type & 0x3f) {
 2484                 case 0: {       /* 21140[A] GPR block */
 2485                     tulip_media_t media;
 2486                     srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
 2487                     for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
 2488                         if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
 2489                             break;
 2490                     }
 2491                     media = tulip_srom_mediums[idx3].sm_type;
 2492                     if (media == TULIP_MEDIA_UNKNOWN)
 2493                         break;
 2494                     mi->mi_type = TULIP_MEDIAINFO_GPR;
 2495                     sc->tulip_mediums[media] = mi;
 2496                     mi->mi_gpdata = dp[1];
 2497                     if (media > gp_media && !TULIP_IS_MEDIA_FD(media)) {
 2498                         sc->tulip_gpdata = mi->mi_gpdata;
 2499                         gp_media = media;
 2500                     }
 2501                     data = dp[2] + dp[3] * 256;
 2502                     mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
 2503                     if (data & TULIP_SROM_2114X_NOINDICATOR) {
 2504                         mi->mi_actmask = 0;
 2505                     } else {
 2506 #if 0
 2507                         mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
 2508 #endif
 2509                         mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
 2510                         mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
 2511                     }
 2512                     mi++;
 2513                     break;
 2514                 }
 2515                 case 1: {       /* 21140[A] MII block */
 2516                     const unsigned phyno = *dp++;
 2517                     mi->mi_type = TULIP_MEDIAINFO_MII;
 2518                     mi->mi_gpr_length = *dp++;
 2519                     mi->mi_gpr_offset = dp - sc->tulip_rombuf;
 2520                     dp += mi->mi_gpr_length;
 2521                     mi->mi_reset_length = *dp++;
 2522                     mi->mi_reset_offset = dp - sc->tulip_rombuf;
 2523                     dp += mi->mi_reset_length;
 2524 
 2525                     /*
 2526                      * Before we probe for a PHY, use the GPR information
 2527                      * to select it.  If we don't, it may be inaccessible.
 2528                      */
 2529                     TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_gpinit|TULIP_GP_PINSET);
 2530                     for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++) {
 2531                         DELAY(10);
 2532                         TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_reset_offset + idx3]);
 2533                     }
 2534                     sc->tulip_phyaddr = mi->mi_phyaddr;
 2535                     for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++) {
 2536                         DELAY(10);
 2537                         TULIP_CSR_WRITE(sc, csr_gp, sc->tulip_rombuf[mi->mi_gpr_offset + idx3]);
 2538                     }
 2539 
 2540                     /*
 2541                      * At least write something!
 2542                      */
 2543                     if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
 2544                         TULIP_CSR_WRITE(sc, csr_gp, 0);
 2545 
 2546                     mi->mi_phyaddr = TULIP_MII_NOPHY;
 2547                     for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
 2548                         DELAY(10000);
 2549                         mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
 2550                     }
 2551                     if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
 2552 #if defined(TULIP_DEBUG)
 2553                         device_printf(sc->tulip_dev, "can't find phy %d\n",
 2554                             phyno);
 2555 #endif
 2556                         break;
 2557                     }
 2558                     sc->tulip_features |= TULIP_HAVE_MII;
 2559                     mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
 2560                     mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
 2561                     mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
 2562                     mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
 2563                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
 2564                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
 2565                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
 2566                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
 2567                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
 2568                     mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
 2569                         tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
 2570                     mi++;
 2571                     break;
 2572                 }
 2573                 case 2: {       /* 2114[23] SIA block */
 2574                     tulip_media_t media;
 2575                     srom_media = (tulip_srom_media_t)(dp[0] & 0x3f);
 2576                     for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
 2577                         if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
 2578                             break;
 2579                     }
 2580                     media = tulip_srom_mediums[idx3].sm_type;
 2581                     if (media == TULIP_MEDIA_UNKNOWN)
 2582                         break;
 2583                     mi->mi_type = TULIP_MEDIAINFO_SIA;
 2584                     sc->tulip_mediums[media] = mi;
 2585                     if (dp[0] & 0x40) {
 2586                         mi->mi_sia_connectivity = dp[1] + dp[2] * 256;
 2587                         mi->mi_sia_tx_rx        = dp[3] + dp[4] * 256;
 2588                         mi->mi_sia_general      = dp[5] + dp[6] * 256;
 2589                         dp += 6;
 2590                     } else {
 2591                         switch (media) {
 2592                             case TULIP_MEDIA_BNC: {
 2593                                 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, BNC);
 2594                                 break;
 2595                             }
 2596                             case TULIP_MEDIA_AUI: {
 2597                                 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, AUI);
 2598                                 break;
 2599                             }
 2600                             case TULIP_MEDIA_10BASET: {
 2601                                 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET);
 2602                                 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
 2603                                 break;
 2604                             }
 2605                             case TULIP_MEDIA_10BASET_FD: {
 2606                                 TULIP_MEDIAINFO_SIA_INIT(sc, mi, 21142, 10BASET_FD);
 2607                                 sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
 2608                                 break;
 2609                             }
 2610                             default: {
 2611                                 goto bad_media;
 2612                             }
 2613                         }
 2614                     }
 2615                     mi->mi_sia_gp_control = (dp[1] + dp[2] * 256) << 16;
 2616                     mi->mi_sia_gp_data    = (dp[3] + dp[4] * 256) << 16;
 2617                     mi++;
 2618                   bad_media:
 2619                     break;
 2620                 }
 2621                 case 3: {       /* 2114[23] MII PHY block */
 2622                     const unsigned phyno = *dp++;
 2623                     const u_int8_t *dp0;
 2624                     mi->mi_type = TULIP_MEDIAINFO_MII;
 2625                     mi->mi_gpr_length = *dp++;
 2626                     mi->mi_gpr_offset = dp - sc->tulip_rombuf;
 2627                     dp += 2 * mi->mi_gpr_length;
 2628                     mi->mi_reset_length = *dp++;
 2629                     mi->mi_reset_offset = dp - sc->tulip_rombuf;
 2630                     dp += 2 * mi->mi_reset_length;
 2631 
 2632                     dp0 = &sc->tulip_rombuf[mi->mi_reset_offset];
 2633                     for (idx3 = 0; idx3 < mi->mi_reset_length; idx3++, dp0 += 2) {
 2634                         DELAY(10);
 2635                         TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
 2636                     }
 2637                     sc->tulip_phyaddr = mi->mi_phyaddr;
 2638                     dp0 = &sc->tulip_rombuf[mi->mi_gpr_offset];
 2639                     for (idx3 = 0; idx3 < mi->mi_gpr_length; idx3++, dp0 += 2) {
 2640                         DELAY(10);
 2641                         TULIP_CSR_WRITE(sc, csr_sia_general, (dp0[0] + 256 * dp0[1]) << 16);
 2642                     }
 2643 
 2644                     if (mi->mi_reset_length == 0 && mi->mi_gpr_length == 0)
 2645                         TULIP_CSR_WRITE(sc, csr_sia_general, 0);
 2646 
 2647                     mi->mi_phyaddr = TULIP_MII_NOPHY;
 2648                     for (idx3 = 20; idx3 > 0 && mi->mi_phyaddr == TULIP_MII_NOPHY; idx3--) {
 2649                         DELAY(10000);
 2650                         mi->mi_phyaddr = tulip_mii_get_phyaddr(sc, phyno);
 2651                     }
 2652                     if (mi->mi_phyaddr == TULIP_MII_NOPHY) {
 2653 #if defined(TULIP_DEBUG)
 2654                         device_printf(sc->tulip_dev, "can't find phy %d\n",
 2655                                phyno);
 2656 #endif
 2657                         break;
 2658                     }
 2659                     sc->tulip_features |= TULIP_HAVE_MII;
 2660                     mi->mi_capabilities  = dp[0] + dp[1] * 256; dp += 2;
 2661                     mi->mi_advertisement = dp[0] + dp[1] * 256; dp += 2;
 2662                     mi->mi_full_duplex   = dp[0] + dp[1] * 256; dp += 2;
 2663                     mi->mi_tx_threshold  = dp[0] + dp[1] * 256; dp += 2;
 2664                     mi->mi_mii_interrupt = dp[0] + dp[1] * 256; dp += 2;
 2665                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX_FD);
 2666                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASETX);
 2667                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 100BASET4);
 2668                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET_FD);
 2669                     TULIP_MEDIAINFO_ADD_CAPABILITY(sc, mi, 10BASET);
 2670                     mi->mi_phyid = (tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDLOW) << 16) |
 2671                         tulip_mii_readreg(sc, mi->mi_phyaddr, PHYREG_IDHIGH);
 2672                     mi++;
 2673                     break;
 2674                 }
 2675                 case 4: {       /* 21143 SYM block */
 2676                     tulip_media_t media;
 2677                     srom_media = (tulip_srom_media_t) dp[0];
 2678                     for (idx3 = 0; tulip_srom_mediums[idx3].sm_type != TULIP_MEDIA_UNKNOWN; idx3++) {
 2679                         if (tulip_srom_mediums[idx3].sm_srom_type == srom_media)
 2680                             break;
 2681                     }
 2682                     media = tulip_srom_mediums[idx3].sm_type;
 2683                     if (media == TULIP_MEDIA_UNKNOWN)
 2684                         break;
 2685                     mi->mi_type = TULIP_MEDIAINFO_SYM;
 2686                     sc->tulip_mediums[media] = mi;
 2687                     mi->mi_gpcontrol = (dp[1] + dp[2] * 256) << 16;
 2688                     mi->mi_gpdata    = (dp[3] + dp[4] * 256) << 16;
 2689                     data = dp[5] + dp[6] * 256;
 2690                     mi->mi_cmdmode = TULIP_SROM_2114X_CMDBITS(data);
 2691                     if (data & TULIP_SROM_2114X_NOINDICATOR) {
 2692                         mi->mi_actmask = 0;
 2693                     } else {
 2694                         mi->mi_default = (data & TULIP_SROM_2114X_DEFAULT) != 0;
 2695                         mi->mi_actmask = TULIP_SROM_2114X_BITPOS(data);
 2696                         mi->mi_actdata = (data & TULIP_SROM_2114X_POLARITY) ? 0 : mi->mi_actmask;
 2697                     }
 2698                     if (TULIP_IS_MEDIA_TP(media))
 2699                         sc->tulip_intrmask |= TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
 2700                     mi++;
 2701                     break;
 2702                 }
 2703 #if 0
 2704                 case 5: {       /* 21143 Reset block */
 2705                     mi->mi_type = TULIP_MEDIAINFO_RESET;
 2706                     mi->mi_reset_length = *dp++;
 2707                     mi->mi_reset_offset = dp - sc->tulip_rombuf;
 2708                     dp += 2 * mi->mi_reset_length;
 2709                     mi++;
 2710                     break;
 2711                 }
 2712 #endif
 2713                 default: {
 2714                 }
 2715             }
 2716             dp = ep;
 2717         }
 2718     }
 2719     return mi - sc->tulip_mediainfo;
 2720 }
 2721 
 2722 static const struct {
 2723     void (*vendor_identify_nic)(tulip_softc_t * const sc);
 2724     unsigned char vendor_oui[3];
 2725 } tulip_vendors[] = {
 2726     { tulip_identify_dec_nic,           { 0x08, 0x00, 0x2B } },
 2727     { tulip_identify_dec_nic,           { 0x00, 0x00, 0xF8 } },
 2728     { tulip_identify_smc_nic,           { 0x00, 0x00, 0xC0 } },
 2729     { tulip_identify_smc_nic,           { 0x00, 0xE0, 0x29 } },
 2730     { tulip_identify_znyx_nic,          { 0x00, 0xC0, 0x95 } },
 2731     { tulip_identify_cogent_nic,        { 0x00, 0x00, 0x92 } },
 2732     { tulip_identify_asante_nic,        { 0x00, 0x00, 0x94 } },
 2733     { tulip_identify_cogent_nic,        { 0x00, 0x00, 0xD1 } },
 2734     { tulip_identify_accton_nic,        { 0x00, 0x00, 0xE8 } },
 2735     { tulip_identify_compex_nic,        { 0x00, 0x80, 0x48 } },
 2736     { NULL }
 2737 };
 2738 
 2739 /*
 2740  * This deals with the vagaries of the address roms and the
 2741  * brain-deadness that various vendors commit in using them.
 2742  */
 2743 static int
 2744 tulip_read_macaddr(tulip_softc_t * const sc)
 2745 {
 2746     unsigned cksum, rom_cksum, idx;
 2747     u_int32_t csr;
 2748     unsigned char tmpbuf[8];
 2749     static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
 2750 
 2751     sc->tulip_connidx = TULIP_SROM_LASTCONNIDX;
 2752 
 2753     if (sc->tulip_chipid == TULIP_21040) {
 2754         TULIP_CSR_WRITE(sc, csr_enetrom, 1);
 2755         for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
 2756             int cnt = 0;
 2757             while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
 2758                 cnt++;
 2759             sc->tulip_rombuf[idx] = csr & 0xFF;
 2760         }
 2761         sc->tulip_boardsw = &tulip_21040_boardsw;
 2762     } else {
 2763         if (sc->tulip_chipid == TULIP_21041) {
 2764             /*
 2765              * Thankfully all 21041's act the same.
 2766              */
 2767             sc->tulip_boardsw = &tulip_21041_boardsw;
 2768         } else {
 2769             /*
 2770              * Assume all 21140 board are compatible with the
 2771              * DEC 10/100 evaluation board.  Not really valid but
 2772              * it's the best we can do until every one switches to
 2773              * the new SROM format.
 2774              */
 2775 
 2776             sc->tulip_boardsw = &tulip_21140_eb_boardsw;
 2777         }
 2778         tulip_srom_read(sc);
 2779         if (tulip_srom_crcok(sc->tulip_rombuf)) {
 2780             /*
 2781              * SROM CRC is valid therefore it must be in the
 2782              * new format.
 2783              */
 2784             sc->tulip_features |= TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM;
 2785         } else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
 2786             /*
 2787              * No checksum is present.  See if the SROM id checks out;
 2788              * the first 18 bytes should be 0 followed by a 1 followed
 2789              * by the number of adapters (which we don't deal with yet).
 2790              */
 2791             for (idx = 0; idx < 18; idx++) {
 2792                 if (sc->tulip_rombuf[idx] != 0)
 2793                     break;
 2794             }
 2795             if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
 2796                 sc->tulip_features |= TULIP_HAVE_ISVSROM;
 2797         } else if (sc->tulip_chipid >= TULIP_21142) {
 2798             sc->tulip_features |= TULIP_HAVE_ISVSROM;
 2799             sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
 2800         }
 2801         if ((sc->tulip_features & TULIP_HAVE_ISVSROM) && tulip_srom_decode(sc)) {
 2802             if (sc->tulip_chipid != TULIP_21041)
 2803                 sc->tulip_boardsw = &tulip_2114x_isv_boardsw;
 2804 
 2805             /*
 2806              * If the SROM specifies more than one adapter, tag this as a
 2807              * BASE rom.
 2808              */
 2809             if (sc->tulip_rombuf[19] > 1)
 2810                 sc->tulip_features |= TULIP_HAVE_BASEROM;
 2811             if (sc->tulip_boardsw == NULL)
 2812                 return -6;
 2813             goto check_oui;
 2814         }
 2815     }
 2816 
 2817 
 2818     if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
 2819         /*
 2820          * Some folks don't use the standard ethernet rom format
 2821          * but instead just put the address in the first 6 bytes
 2822          * of the rom and let the rest be all 0xffs.  (Can we say
 2823          * ZNYX?) (well sometimes they put in a checksum so we'll
 2824          * start at 8).
 2825          */
 2826         for (idx = 8; idx < 32; idx++) {
 2827             if (sc->tulip_rombuf[idx] != 0xFF)
 2828                 return -4;
 2829         }
 2830         /*
 2831          * Make sure the address is not multicast or locally assigned
 2832          * that the OUI is not 00-00-00.
 2833          */
 2834         if ((sc->tulip_rombuf[0] & 3) != 0)
 2835             return -4;
 2836         if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
 2837                 && sc->tulip_rombuf[2] == 0)
 2838             return -4;
 2839         bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
 2840         sc->tulip_features |= TULIP_HAVE_OKROM;
 2841         goto check_oui;
 2842     } else {
 2843         /*
 2844          * A number of makers of multiport boards (ZNYX and Cogent)
 2845          * only put on one address ROM on their 21040 boards.  So
 2846          * if the ROM is all zeros (or all 0xFFs), look at the
 2847          * previous configured boards (as long as they are on the same
 2848          * PCI bus and the bus number is non-zero) until we find the
 2849          * master board with address ROM.  We then use its address ROM
 2850          * as the base for this board.  (we add our relative board
 2851          * to the last byte of its address).
 2852          */
 2853         for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
 2854             if (sc->tulip_rombuf[idx] != 0 && sc->tulip_rombuf[idx] != 0xFF)
 2855                 break;
 2856         }
 2857         if (idx == sizeof(sc->tulip_rombuf)) {
 2858             int root_unit;
 2859             tulip_softc_t *root_sc = NULL;
 2860             for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
 2861                 root_sc = tulips[root_unit];
 2862                 if (root_sc == NULL || (root_sc->tulip_features & (TULIP_HAVE_OKROM|TULIP_HAVE_SLAVEDROM)) == TULIP_HAVE_OKROM)
 2863                     break;
 2864                 root_sc = NULL;
 2865             }
 2866             if (root_sc != NULL && (root_sc->tulip_features & TULIP_HAVE_BASEROM)
 2867                     && root_sc->tulip_chipid == sc->tulip_chipid
 2868                     && root_sc->tulip_pci_busno == sc->tulip_pci_busno) {
 2869                 sc->tulip_features |= TULIP_HAVE_SLAVEDROM;
 2870                 sc->tulip_boardsw = root_sc->tulip_boardsw;
 2871                 strcpy(sc->tulip_boardid, root_sc->tulip_boardid);
 2872                 if (sc->tulip_boardsw->bd_type == TULIP_21140_ISV) {
 2873                     bcopy(root_sc->tulip_rombuf, sc->tulip_rombuf,
 2874                           sizeof(sc->tulip_rombuf));
 2875                     if (!tulip_srom_decode(sc))
 2876                         return -5;
 2877                 } else {
 2878                     bcopy(root_sc->tulip_enaddr, sc->tulip_enaddr, 6);
 2879                     sc->tulip_enaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
 2880                 }
 2881                 /*
 2882                  * Now for a truly disgusting kludge: all 4 21040s on
 2883                  * the ZX314 share the same INTA line so the mapping
 2884                  * setup by the BIOS on the PCI bridge is worthless.
 2885                  * Rather than reprogramming the value in the config
 2886                  * register, we will handle this internally.
 2887                  */
 2888                 if (root_sc->tulip_features & TULIP_HAVE_SHAREDINTR) {
 2889                     sc->tulip_slaves = root_sc->tulip_slaves;
 2890                     root_sc->tulip_slaves = sc;
 2891                     sc->tulip_features |= TULIP_HAVE_SLAVEDINTR;
 2892                 }
 2893                 return 0;
 2894             }
 2895         }
 2896     }
 2897 
 2898     /*
 2899      * This is the standard DEC address ROM test.
 2900      */
 2901 
 2902     if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
 2903         return -3;
 2904 
 2905     tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
 2906     tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
 2907     tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
 2908     tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
 2909     if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
 2910         return -2;
 2911 
 2912     bcopy(sc->tulip_rombuf, sc->tulip_enaddr, 6);
 2913 
 2914     cksum = *(u_int16_t *) &sc->tulip_enaddr[0];
 2915     cksum *= 2;
 2916     if (cksum > 65535) cksum -= 65535;
 2917     cksum += *(u_int16_t *) &sc->tulip_enaddr[2];
 2918     if (cksum > 65535) cksum -= 65535;
 2919     cksum *= 2;
 2920     if (cksum > 65535) cksum -= 65535;
 2921     cksum += *(u_int16_t *) &sc->tulip_enaddr[4];
 2922     if (cksum >= 65535) cksum -= 65535;
 2923 
 2924     rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
 2925         
 2926     if (cksum != rom_cksum)
 2927         return -1;
 2928 
 2929   check_oui:
 2930     /*
 2931      * Check for various boards based on OUI.  Did I say braindead?
 2932      */
 2933     for (idx = 0; tulip_vendors[idx].vendor_identify_nic != NULL; idx++) {
 2934         if (bcmp(sc->tulip_enaddr, tulip_vendors[idx].vendor_oui, 3) == 0) {
 2935             (*tulip_vendors[idx].vendor_identify_nic)(sc);
 2936             break;
 2937         }
 2938     }
 2939 
 2940     sc->tulip_features |= TULIP_HAVE_OKROM;
 2941     return 0;
 2942 }
 2943 
 2944 static void
 2945 tulip_ifmedia_add(tulip_softc_t * const sc)
 2946 {
 2947     tulip_media_t media;
 2948     int medias = 0;
 2949 
 2950     TULIP_LOCK_ASSERT(sc);
 2951     for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
 2952         if (sc->tulip_mediums[media] != NULL) {
 2953             ifmedia_add(&sc->tulip_ifmedia, tulip_media_to_ifmedia[media],
 2954                         0, 0);
 2955             medias++;
 2956         }
 2957     }
 2958     if (medias == 0) {
 2959         sc->tulip_features |= TULIP_HAVE_NOMEDIA;
 2960         ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE, 0, 0);
 2961         ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_NONE);
 2962     } else if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
 2963         ifmedia_add(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO, 0, 0);
 2964         ifmedia_set(&sc->tulip_ifmedia, IFM_ETHER | IFM_AUTO);
 2965     } else {
 2966         ifmedia_set(&sc->tulip_ifmedia, tulip_media_to_ifmedia[sc->tulip_media]);
 2967         sc->tulip_flags |= TULIP_PRINTMEDIA;
 2968         tulip_linkup(sc, sc->tulip_media);
 2969     }
 2970 }
 2971 
 2972 static int
 2973 tulip_ifmedia_change(struct ifnet * const ifp)
 2974 {
 2975     tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
 2976 
 2977     TULIP_LOCK(sc);
 2978     sc->tulip_flags |= TULIP_NEEDRESET;
 2979     sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
 2980     sc->tulip_media = TULIP_MEDIA_UNKNOWN;
 2981     if (IFM_SUBTYPE(sc->tulip_ifmedia.ifm_media) != IFM_AUTO) {
 2982         tulip_media_t media;
 2983         for (media = TULIP_MEDIA_UNKNOWN; media < TULIP_MEDIA_MAX; media++) {
 2984             if (sc->tulip_mediums[media] != NULL
 2985                 && sc->tulip_ifmedia.ifm_media == tulip_media_to_ifmedia[media]) {
 2986                 sc->tulip_flags |= TULIP_PRINTMEDIA;
 2987                 sc->tulip_flags &= ~TULIP_DIDNWAY;
 2988                 tulip_linkup(sc, media);
 2989                 TULIP_UNLOCK(sc);
 2990                 return 0;
 2991             }
 2992         }
 2993     }
 2994     sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_WANTRXACT);
 2995     tulip_reset(sc);
 2996     tulip_init_locked(sc);
 2997     TULIP_UNLOCK(sc);
 2998     return 0;
 2999 }
 3000 
 3001 /*
 3002  * Media status callback
 3003  */
 3004 static void
 3005 tulip_ifmedia_status(struct ifnet * const ifp, struct ifmediareq *req)
 3006 {
 3007     tulip_softc_t *sc = (tulip_softc_t *)ifp->if_softc;
 3008 
 3009     TULIP_LOCK(sc);
 3010     if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
 3011         TULIP_UNLOCK(sc);
 3012         return;
 3013     }
 3014 
 3015     req->ifm_status = IFM_AVALID;
 3016     if (sc->tulip_flags & TULIP_LINKUP)
 3017         req->ifm_status |= IFM_ACTIVE;
 3018 
 3019     req->ifm_active = tulip_media_to_ifmedia[sc->tulip_media];
 3020     TULIP_UNLOCK(sc);
 3021 }
 3022 
 3023 static void
 3024 tulip_addr_filter(tulip_softc_t * const sc)
 3025 {
 3026     struct ifmultiaddr *ifma;
 3027     struct ifnet *ifp;
 3028     u_char *addrp;
 3029     u_int16_t eaddr[ETHER_ADDR_LEN/2];
 3030     int multicnt;
 3031 
 3032     TULIP_LOCK_ASSERT(sc);
 3033     sc->tulip_flags &= ~(TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY|TULIP_ALLMULTI);
 3034     sc->tulip_flags |= TULIP_WANTSETUP|TULIP_WANTTXSTART;
 3035     sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
 3036     sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
 3037 #if defined(IFF_ALLMULTI)    
 3038     if (sc->tulip_ifp->if_flags & IFF_ALLMULTI)
 3039         sc->tulip_flags |= TULIP_ALLMULTI ;
 3040 #endif
 3041 
 3042     multicnt = 0;
 3043     ifp = sc->tulip_ifp;      
 3044     if_maddr_rlock(ifp);
 3045 
 3046     /* Copy MAC address on stack to align. */
 3047     if (ifp->if_input != NULL)
 3048         bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
 3049     else
 3050         bcopy(sc->tulip_enaddr, eaddr, ETHER_ADDR_LEN);
 3051 
 3052     TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 3053 
 3054             if (ifma->ifma_addr->sa_family == AF_LINK)
 3055                 multicnt++;
 3056     }
 3057 
 3058     if (multicnt > 14) {
 3059         u_int32_t *sp = sc->tulip_setupdata;
 3060         unsigned hash;
 3061         /*
 3062          * Some early passes of the 21140 have broken implementations of
 3063          * hash-perfect mode.  When we get too many multicasts for perfect
 3064          * filtering with these chips, we need to switch into hash-only
 3065          * mode (this is better than all-multicast on network with lots
 3066          * of multicast traffic).
 3067          */
 3068         if (sc->tulip_features & TULIP_HAVE_BROKEN_HASH)
 3069             sc->tulip_flags |= TULIP_WANTHASHONLY;
 3070         else
 3071             sc->tulip_flags |= TULIP_WANTHASHPERFECT;
 3072         /*
 3073          * If we have more than 14 multicasts, we have
 3074          * go into hash perfect mode (512 bit multicast
 3075          * hash and one perfect hardware).
 3076          */
 3077         bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
 3078 
 3079         TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 3080 
 3081                 if (ifma->ifma_addr->sa_family != AF_LINK)
 3082                         continue;
 3083 
 3084                 hash = tulip_mchash(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
 3085                 sp[hash >> 4] |= htole32(1 << (hash & 0xF));
 3086         }
 3087         /*
 3088          * No reason to use a hash if we are going to be
 3089          * receiving every multicast.
 3090          */
 3091         if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
 3092             hash = tulip_mchash(ifp->if_broadcastaddr);
 3093             sp[hash >> 4] |= htole32(1 << (hash & 0xF));
 3094             if (sc->tulip_flags & TULIP_WANTHASHONLY) {
 3095                 hash = tulip_mchash((caddr_t)eaddr);
 3096                 sp[hash >> 4] |= htole32(1 << (hash & 0xF));
 3097             } else {
 3098                 sp[39] = TULIP_SP_MAC(eaddr[0]); 
 3099                 sp[40] = TULIP_SP_MAC(eaddr[1]); 
 3100                 sp[41] = TULIP_SP_MAC(eaddr[2]);
 3101             }
 3102         }
 3103     }
 3104     if ((sc->tulip_flags & (TULIP_WANTHASHPERFECT|TULIP_WANTHASHONLY)) == 0) {
 3105         u_int32_t *sp = sc->tulip_setupdata;
 3106         int idx = 0;
 3107         if ((sc->tulip_flags & TULIP_ALLMULTI) == 0) {
 3108             /*
 3109              * Else can get perfect filtering for 16 addresses.
 3110              */
 3111             TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
 3112                     if (ifma->ifma_addr->sa_family != AF_LINK)
 3113                             continue;
 3114                     addrp = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
 3115                     *sp++ = TULIP_SP_MAC(((u_int16_t *)addrp)[0]); 
 3116                     *sp++ = TULIP_SP_MAC(((u_int16_t *)addrp)[1]); 
 3117                     *sp++ = TULIP_SP_MAC(((u_int16_t *)addrp)[2]);
 3118                     idx++;
 3119             }
 3120             /*
 3121              * Add the broadcast address.
 3122              */
 3123             idx++;
 3124             *sp++ = TULIP_SP_MAC(0xFFFF);
 3125             *sp++ = TULIP_SP_MAC(0xFFFF);
 3126             *sp++ = TULIP_SP_MAC(0xFFFF);
 3127         }
 3128         /*
 3129          * Pad the rest with our hardware address
 3130          */
 3131         for (; idx < 16; idx++) {
 3132             *sp++ = TULIP_SP_MAC(eaddr[0]); 
 3133             *sp++ = TULIP_SP_MAC(eaddr[1]); 
 3134             *sp++ = TULIP_SP_MAC(eaddr[2]);
 3135         }
 3136     }
 3137     if_maddr_runlock(ifp);
 3138 }
 3139 
 3140 static void
 3141 tulip_reset(tulip_softc_t * const sc)
 3142 {
 3143     tulip_ringinfo_t *ri;
 3144     tulip_descinfo_t *di;
 3145     struct mbuf *m;
 3146     u_int32_t inreset = (sc->tulip_flags & TULIP_INRESET);
 3147 
 3148     TULIP_LOCK_ASSERT(sc);
 3149 
 3150     CTR1(KTR_TULIP, "tulip_reset: inreset %d", inreset);
 3151 
 3152     /*
 3153      * Brilliant.  Simply brilliant.  When switching modes/speeds
 3154      * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
 3155      * bits in CSR6 and then do a software reset to get the 21140
 3156      * to properly reset its internal pathways to the right places.
 3157      *   Grrrr.
 3158      */
 3159     if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0
 3160             && sc->tulip_boardsw->bd_media_preset != NULL)
 3161         (*sc->tulip_boardsw->bd_media_preset)(sc);
 3162 
 3163     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
 3164     DELAY(10);  /* Wait 10 microseconds (actually 50 PCI cycles but at 
 3165                    33MHz that comes to two microseconds but wait a
 3166                    bit longer anyways) */
 3167 
 3168     if (!inreset) {
 3169         sc->tulip_flags |= TULIP_INRESET;
 3170         sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
 3171         sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 3172     }
 3173 
 3174     TULIP_CSR_WRITE(sc, csr_txlist, sc->tulip_txinfo.ri_dma_addr & 0xffffffff);
 3175     TULIP_CSR_WRITE(sc, csr_rxlist, sc->tulip_rxinfo.ri_dma_addr & 0xffffffff);
 3176     TULIP_CSR_WRITE(sc, csr_busmode,
 3177                     (1 << (3 /*pci_max_burst_len*/ + 8))
 3178                     |TULIP_BUSMODE_CACHE_ALIGN8
 3179                     |TULIP_BUSMODE_READMULTIPLE
 3180                     |(BYTE_ORDER != LITTLE_ENDIAN ?
 3181                       TULIP_BUSMODE_DESC_BIGENDIAN : 0));
 3182 
 3183     sc->tulip_txtimer = 0;
 3184     /*
 3185      * Free all the mbufs that were on the transmit ring.
 3186      */
 3187     CTR0(KTR_TULIP, "tulip_reset: drain transmit ring");
 3188     ri = &sc->tulip_txinfo;
 3189     for (di = ri->ri_first; di < ri->ri_last; di++) {
 3190         m = tulip_dequeue_mbuf(ri, di, SYNC_NONE);
 3191         if (m != NULL)
 3192             m_freem(m);
 3193         di->di_desc->d_status = 0;
 3194     }
 3195 
 3196     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
 3197     ri->ri_free = ri->ri_max;
 3198     TULIP_TXDESC_PRESYNC(ri);
 3199 
 3200     /*
 3201      * We need to collect all the mbufs that were on the 
 3202      * receive ring before we reinit it either to put
 3203      * them back on or to know if we have to allocate
 3204      * more.
 3205      */
 3206     CTR0(KTR_TULIP, "tulip_reset: drain receive ring");
 3207     ri = &sc->tulip_rxinfo;
 3208     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
 3209     ri->ri_free = ri->ri_max;
 3210     for (di = ri->ri_first; di < ri->ri_last; di++) {
 3211         di->di_desc->d_status = 0;
 3212         di->di_desc->d_length1 = 0; di->di_desc->d_addr1 = 0;
 3213         di->di_desc->d_length2 = 0; di->di_desc->d_addr2 = 0;
 3214     }
 3215     TULIP_RXDESC_PRESYNC(ri);
 3216     for (di = ri->ri_first; di < ri->ri_last; di++) {
 3217         m = tulip_dequeue_mbuf(ri, di, SYNC_NONE);
 3218         if (m != NULL)
 3219             m_freem(m);
 3220     }
 3221 
 3222     /*
 3223      * If tulip_reset is being called recursively, exit quickly knowing
 3224      * that when the outer tulip_reset returns all the right stuff will
 3225      * have happened.
 3226      */
 3227     if (inreset)
 3228         return;
 3229 
 3230     sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
 3231         |TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
 3232         |TULIP_STS_TXUNDERFLOW|TULIP_STS_TXBABBLE
 3233         |TULIP_STS_RXSTOPPED;
 3234 
 3235     if ((sc->tulip_flags & TULIP_DEVICEPROBE) == 0)
 3236         (*sc->tulip_boardsw->bd_media_select)(sc);
 3237 #if defined(TULIP_DEBUG)
 3238     if ((sc->tulip_flags & TULIP_NEEDRESET) == TULIP_NEEDRESET)
 3239         device_printf(sc->tulip_dev,
 3240             "tulip_reset: additional reset needed?!?\n");
 3241 #endif
 3242     if (bootverbose)
 3243             tulip_media_print(sc);
 3244     if (sc->tulip_features & TULIP_HAVE_DUALSENSE)
 3245         TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
 3246 
 3247     sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
 3248                          |TULIP_RXACT);
 3249 }
 3250 
 3251 
 3252 static void
 3253 tulip_init(void *arg)
 3254 {
 3255     tulip_softc_t *sc = (tulip_softc_t *)arg;
 3256 
 3257     TULIP_LOCK(sc);
 3258     tulip_init_locked(sc);
 3259     TULIP_UNLOCK(sc);
 3260 }
 3261 
 3262 static void
 3263 tulip_init_locked(tulip_softc_t * const sc)
 3264 {
 3265     CTR0(KTR_TULIP, "tulip_init_locked");
 3266     if (sc->tulip_ifp->if_flags & IFF_UP) {
 3267         if ((sc->tulip_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
 3268             /* initialize the media */
 3269             CTR0(KTR_TULIP, "tulip_init_locked: up but not running, reset chip");
 3270             tulip_reset(sc);
 3271         }
 3272         tulip_addr_filter(sc);
 3273         sc->tulip_ifp->if_drv_flags |= IFF_DRV_RUNNING;
 3274         if (sc->tulip_ifp->if_flags & IFF_PROMISC) {
 3275             sc->tulip_flags |= TULIP_PROMISC;
 3276             sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
 3277             sc->tulip_intrmask |= TULIP_STS_TXINTR;
 3278         } else {
 3279             sc->tulip_flags &= ~TULIP_PROMISC;
 3280             sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
 3281             if (sc->tulip_flags & TULIP_ALLMULTI) {
 3282                 sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
 3283             } else {
 3284                 sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
 3285             }
 3286         }
 3287         sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
 3288         if ((sc->tulip_flags & (TULIP_TXPROBE_ACTIVE|TULIP_WANTSETUP)) == 0) {
 3289             tulip_rx_intr(sc);
 3290             sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
 3291             sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
 3292         } else {
 3293             sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 3294             sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
 3295             sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
 3296         }
 3297         CTR2(KTR_TULIP, "tulip_init_locked: intr mask %08x  cmdmode %08x",
 3298             sc->tulip_intrmask, sc->tulip_cmdmode);
 3299         TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
 3300         TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 3301         CTR1(KTR_TULIP, "tulip_init_locked: status %08x\n",
 3302             TULIP_CSR_READ(sc, csr_status));
 3303         if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
 3304             tulip_txput_setup(sc);
 3305     } else {
 3306         CTR0(KTR_TULIP, "tulip_init_locked: not up, reset chip");
 3307         sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
 3308         tulip_reset(sc);
 3309         tulip_addr_filter(sc);
 3310     }
 3311 }
 3312 
 3313 #define DESC_STATUS(di) (((volatile tulip_desc_t *)((di)->di_desc))->d_status)
 3314 #define DESC_FLAG(di)   ((di)->di_desc->d_flag)
 3315 
 3316 static void
 3317 tulip_rx_intr(tulip_softc_t * const sc)
 3318 {
 3319     TULIP_PERFSTART(rxintr)
 3320     tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
 3321     struct ifnet * const ifp = sc->tulip_ifp;
 3322     int fillok = 1;
 3323 #if defined(TULIP_DEBUG)
 3324     int cnt = 0;
 3325 #endif
 3326 
 3327     TULIP_LOCK_ASSERT(sc);
 3328     CTR0(KTR_TULIP, "tulip_rx_intr: start");
 3329     for (;;) {
 3330         TULIP_PERFSTART(rxget)
 3331         tulip_descinfo_t *eop = ri->ri_nextin, *dip;
 3332         int total_len = 0, last_offset = 0;
 3333         struct mbuf *ms = NULL, *me = NULL;
 3334         int accept = 0;
 3335         int error;
 3336 
 3337         if (fillok && (ri->ri_max - ri->ri_free) < TULIP_RXQ_TARGET)
 3338             goto queue_mbuf;
 3339 
 3340 #if defined(TULIP_DEBUG)
 3341         if (cnt == ri->ri_max)
 3342             break;
 3343 #endif
 3344         /*
 3345          * If the TULIP has no descriptors, there can't be any receive
 3346          * descriptors to process.
 3347          */
 3348         if (eop == ri->ri_nextout)
 3349             break;
 3350 
 3351         /*
 3352          * 90% of the packets will fit in one descriptor.  So we optimize
 3353          * for that case.
 3354          */
 3355         TULIP_RXDESC_POSTSYNC(ri);
 3356         if ((DESC_STATUS(eop) & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
 3357             ms = tulip_dequeue_mbuf(ri, eop, SYNC_RX);
 3358             CTR2(KTR_TULIP,
 3359                 "tulip_rx_intr: single packet mbuf %p from descriptor %td", ms,
 3360                 eop - ri->ri_first);
 3361             me = ms;
 3362             ri->ri_free++;
 3363         } else {
 3364             /*
 3365              * If still owned by the TULIP, don't touch it.
 3366              */
 3367             if (DESC_STATUS(eop) & TULIP_DSTS_OWNER)
 3368                 break;
 3369 
 3370             /*
 3371              * It is possible (though improbable unless MCLBYTES < 1518) for
 3372              * a received packet to cross more than one receive descriptor.
 3373              * We first loop through the descriptor ring making sure we have
 3374              * received a complete packet.  If not, we bail until the next
 3375              * interrupt.
 3376              */
 3377             dip = eop;
 3378             while ((DESC_STATUS(eop) & TULIP_DSTS_RxLASTDESC) == 0) {
 3379                 if (++eop == ri->ri_last)
 3380                     eop = ri->ri_first;
 3381                 TULIP_RXDESC_POSTSYNC(ri);
 3382                 if (eop == ri->ri_nextout || DESC_STATUS(eop) & TULIP_DSTS_OWNER) {
 3383 #if defined(TULIP_DEBUG)
 3384                     sc->tulip_dbg.dbg_rxintrs++;
 3385                     sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
 3386 #endif
 3387                     TULIP_PERFEND(rxget);
 3388                     TULIP_PERFEND(rxintr);
 3389                     return;
 3390                 }
 3391                 total_len++;
 3392             }
 3393 
 3394             /*
 3395              * Dequeue the first buffer for the start of the packet.  Hopefully
 3396              * this will be the only one we need to dequeue.  However, if the
 3397              * packet consumed multiple descriptors, then we need to dequeue
 3398              * those buffers and chain to the starting mbuf.  All buffers but
 3399              * the last buffer have the same length so we can set that now.
 3400              * (we add to last_offset instead of multiplying since we normally
 3401              * won't go into the loop and thereby saving ourselves from
 3402              * doing a multiplication by 0 in the normal case).
 3403              */
 3404             ms = tulip_dequeue_mbuf(ri, dip, SYNC_RX);
 3405             CTR2(KTR_TULIP,
 3406                 "tulip_rx_intr: start packet mbuf %p from descriptor %td", ms,
 3407                 dip - ri->ri_first);
 3408             ri->ri_free++;
 3409             for (me = ms; total_len > 0; total_len--) {
 3410                 me->m_len = TULIP_RX_BUFLEN;
 3411                 last_offset += TULIP_RX_BUFLEN;
 3412                 if (++dip == ri->ri_last)
 3413                     dip = ri->ri_first;
 3414                 me->m_next = tulip_dequeue_mbuf(ri, dip, SYNC_RX);
 3415                 ri->ri_free++;
 3416                 me = me->m_next;
 3417                 CTR2(KTR_TULIP,
 3418                     "tulip_rx_intr: cont packet mbuf %p from descriptor %td",
 3419                     me, dip - ri->ri_first);
 3420             }
 3421             KASSERT(dip == eop, ("mismatched descinfo structs"));
 3422         }
 3423 
 3424         /*
 3425          *  Now get the size of received packet (minus the CRC).
 3426          */
 3427         total_len = ((DESC_STATUS(eop) >> 16) & 0x7FFF) - ETHER_CRC_LEN;
 3428         if ((sc->tulip_flags & TULIP_RXIGNORE) == 0
 3429             && ((DESC_STATUS(eop) & TULIP_DSTS_ERRSUM) == 0)) {
 3430             me->m_len = total_len - last_offset;
 3431             sc->tulip_flags |= TULIP_RXACT;
 3432             accept = 1;
 3433             CTR1(KTR_TULIP, "tulip_rx_intr: good packet; length %d",
 3434                 total_len);
 3435         } else {
 3436             CTR1(KTR_TULIP, "tulip_rx_intr: bad packet; status %08x",
 3437                 DESC_STATUS(eop));
 3438             ifp->if_ierrors++;
 3439             if (DESC_STATUS(eop) & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
 3440                 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
 3441             } else {
 3442 #if defined(TULIP_VERBOSE)
 3443                 const char *error = NULL;
 3444 #endif
 3445                 if (DESC_STATUS(eop) & TULIP_DSTS_RxTOOLONG) {
 3446                     sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
 3447 #if defined(TULIP_VERBOSE)
 3448                     error = "frame too long";
 3449 #endif
 3450                 }
 3451                 if (DESC_STATUS(eop) & TULIP_DSTS_RxBADCRC) {
 3452                     if (DESC_STATUS(eop) & TULIP_DSTS_RxDRBBLBIT) {
 3453                         sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
 3454 #if defined(TULIP_VERBOSE)
 3455                         error = "alignment error";
 3456 #endif
 3457                     } else {
 3458                         sc->tulip_dot3stats.dot3StatsFCSErrors++;
 3459 #if defined(TULIP_VERBOSE)
 3460                         error = "bad crc";
 3461 #endif
 3462                     }
 3463                 }
 3464 #if defined(TULIP_VERBOSE)
 3465                 if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
 3466                     device_printf(sc->tulip_dev, "receive: %6D: %s\n",
 3467                            mtod(ms, u_char *) + 6, ":",
 3468                            error);
 3469                     sc->tulip_flags |= TULIP_NOMESSAGES;
 3470                 }
 3471 #endif
 3472             }
 3473 
 3474         }
 3475 #if defined(TULIP_DEBUG)
 3476         cnt++;
 3477 #endif
 3478         ifp->if_ipackets++;
 3479         if (++eop == ri->ri_last)
 3480             eop = ri->ri_first;
 3481         ri->ri_nextin = eop;
 3482       queue_mbuf:
 3483         /*
 3484          * We have received a good packet that needs to be passed up the
 3485          * stack.
 3486          */
 3487         if (accept) {
 3488             struct mbuf *m0;
 3489 
 3490             KASSERT(ms != NULL, ("no packet to accept"));
 3491 #ifndef __NO_STRICT_ALIGNMENT
 3492             /*
 3493              * Copy the data into a new mbuf that is properly aligned.  If
 3494              * we fail to allocate a new mbuf, then drop the packet.  We will
 3495              * reuse the same rx buffer ('ms') below for another packet
 3496              * regardless.
 3497              */
 3498             m0 = m_devget(mtod(ms, caddr_t), total_len, ETHER_ALIGN, ifp, NULL);
 3499             if (m0 == NULL) {
 3500                 ifp->if_ierrors++;
 3501                 goto skip_input;
 3502             }
 3503 #else
 3504             /*
 3505              * Update the header for the mbuf referencing this receive
 3506              * buffer and pass it up the stack.  Allocate a new mbuf cluster
 3507              * to replace the one we just passed up the stack.
 3508              *
 3509              * Note that if this packet crossed multiple descriptors
 3510              * we don't even try to reallocate all the mbufs here.
 3511              * Instead we rely on the test at the beginning of
 3512              * the loop to refill for the extra consumed mbufs.
 3513              */
 3514             ms->m_pkthdr.len = total_len;
 3515             ms->m_pkthdr.rcvif = ifp;
 3516             m0 = ms;
 3517             ms = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
 3518 #endif
 3519             TULIP_UNLOCK(sc);
 3520             CTR1(KTR_TULIP, "tulip_rx_intr: passing %p to upper layer", m0);
 3521             (*ifp->if_input)(ifp, m0);
 3522             TULIP_LOCK(sc);
 3523         } else if (ms == NULL)
 3524             /*
 3525              * If we are priming the TULIP with mbufs, then allocate
 3526              * a new cluster for the next descriptor.
 3527              */
 3528             ms = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
 3529 
 3530 #ifndef __NO_STRICT_ALIGNMENT
 3531     skip_input:
 3532 #endif
 3533         if (ms == NULL) {
 3534             /*
 3535              * Couldn't allocate a new buffer.  Don't bother 
 3536              * trying to replenish the receive queue.
 3537              */
 3538             fillok = 0;
 3539             sc->tulip_flags |= TULIP_RXBUFSLOW;
 3540 #if defined(TULIP_DEBUG)
 3541             sc->tulip_dbg.dbg_rxlowbufs++;
 3542 #endif
 3543             TULIP_PERFEND(rxget);
 3544             continue;
 3545         }
 3546         /*
 3547          * Now give the buffer(s) to the TULIP and save in our
 3548          * receive queue.
 3549          */
 3550         do {
 3551             tulip_descinfo_t * const nextout = ri->ri_nextout;
 3552 
 3553             M_ASSERTPKTHDR(ms);
 3554             KASSERT(ms->m_data == ms->m_ext.ext_buf,
 3555                 ("rx mbuf data doesn't point to cluster"));         
 3556             ms->m_len = ms->m_pkthdr.len = TULIP_RX_BUFLEN;
 3557             error = bus_dmamap_load_mbuf(ri->ri_data_tag, *nextout->di_map, ms,
 3558                 tulip_dma_map_rxbuf, nextout->di_desc, BUS_DMA_NOWAIT);
 3559             if (error) {
 3560                 device_printf(sc->tulip_dev,
 3561                     "unable to load rx map, error = %d\n", error);
 3562                 panic("tulip_rx_intr");         /* XXX */
 3563             }
 3564             nextout->di_desc->d_status = TULIP_DSTS_OWNER;
 3565             KASSERT(nextout->di_mbuf == NULL, ("clobbering earlier rx mbuf"));
 3566             nextout->di_mbuf = ms;
 3567             CTR2(KTR_TULIP, "tulip_rx_intr: enqueued mbuf %p to descriptor %td",
 3568                 ms, nextout - ri->ri_first);
 3569             TULIP_RXDESC_POSTSYNC(ri);
 3570             if (++ri->ri_nextout == ri->ri_last)
 3571                 ri->ri_nextout = ri->ri_first;
 3572             ri->ri_free--;
 3573             me = ms->m_next;
 3574             ms->m_next = NULL;
 3575         } while ((ms = me) != NULL);
 3576 
 3577         if ((ri->ri_max - ri->ri_free) >= TULIP_RXQ_TARGET)
 3578             sc->tulip_flags &= ~TULIP_RXBUFSLOW;
 3579         TULIP_PERFEND(rxget);
 3580     }
 3581 
 3582 #if defined(TULIP_DEBUG)
 3583     sc->tulip_dbg.dbg_rxintrs++;
 3584     sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
 3585 #endif
 3586     TULIP_PERFEND(rxintr);
 3587 }
 3588 
 3589 static int
 3590 tulip_tx_intr(tulip_softc_t * const sc)
 3591 {
 3592     TULIP_PERFSTART(txintr)    
 3593     tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
 3594     struct mbuf *m;
 3595     int xmits = 0;
 3596     int descs = 0;
 3597 
 3598     CTR0(KTR_TULIP, "tulip_tx_intr: start");
 3599     TULIP_LOCK_ASSERT(sc);
 3600     while (ri->ri_free < ri->ri_max) {
 3601         u_int32_t d_flag;
 3602 
 3603         TULIP_TXDESC_POSTSYNC(ri);
 3604         if (DESC_STATUS(ri->ri_nextin) & TULIP_DSTS_OWNER)
 3605             break;
 3606 
 3607         ri->ri_free++;
 3608         descs++;
 3609         d_flag = DESC_FLAG(ri->ri_nextin);
 3610         if (d_flag & TULIP_DFLAG_TxLASTSEG) {
 3611             if (d_flag & TULIP_DFLAG_TxSETUPPKT) {
 3612                 CTR2(KTR_TULIP,
 3613                     "tulip_tx_intr: setup packet from descriptor %td: %08x",
 3614                     ri->ri_nextin - ri->ri_first, DESC_STATUS(ri->ri_nextin));
 3615                 /*
 3616                  * We've just finished processing a setup packet.
 3617                  * Mark that we finished it.  If there's not
 3618                  * another pending, startup the TULIP receiver.
 3619                  * Make sure we ack the RXSTOPPED so we won't get
 3620                  * an abormal interrupt indication.
 3621                  */
 3622                 bus_dmamap_sync(sc->tulip_setup_tag, sc->tulip_setup_map,
 3623                     BUS_DMASYNC_POSTWRITE);
 3624                 sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_HASHONLY);
 3625                 if (DESC_FLAG(ri->ri_nextin) & TULIP_DFLAG_TxINVRSFILT)
 3626                     sc->tulip_flags |= TULIP_HASHONLY;
 3627                 if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == 0) {
 3628                     tulip_rx_intr(sc);
 3629                     sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
 3630                     sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
 3631                     CTR2(KTR_TULIP,
 3632                         "tulip_tx_intr: intr mask %08x  cmdmode %08x",
 3633                         sc->tulip_intrmask, sc->tulip_cmdmode);
 3634                     TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
 3635                     TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
 3636                     TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 3637                 }
 3638             } else {
 3639                 const u_int32_t d_status = DESC_STATUS(ri->ri_nextin);
 3640 
 3641                 m = tulip_dequeue_mbuf(ri, ri->ri_nextin, SYNC_TX);
 3642                 CTR2(KTR_TULIP,
 3643                     "tulip_tx_intr: data packet %p from descriptor %td", m,
 3644                     ri->ri_nextin - ri->ri_first);
 3645                 if (m != NULL) {
 3646                     m_freem(m);
 3647 #if defined(TULIP_DEBUG)
 3648                 } else {
 3649                     device_printf(sc->tulip_dev,
 3650                         "tx_intr: failed to dequeue mbuf?!?\n");
 3651 #endif
 3652                 }
 3653                 if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
 3654                     tulip_mediapoll_event_t event = TULIP_MEDIAPOLL_TXPROBE_OK;
 3655                     if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) {
 3656 #if defined(TULIP_DEBUG)
 3657                         if (d_status & TULIP_DSTS_TxNOCARR)
 3658                             sc->tulip_dbg.dbg_txprobe_nocarr++;
 3659                         if (d_status & TULIP_DSTS_TxEXCCOLL)
 3660                             sc->tulip_dbg.dbg_txprobe_exccoll++;
 3661 #endif
 3662                         event = TULIP_MEDIAPOLL_TXPROBE_FAILED;
 3663                     }
 3664                     (*sc->tulip_boardsw->bd_media_poll)(sc, event);
 3665                     /*
 3666                      * Escape from the loop before media poll has reset the TULIP!
 3667                      */
 3668                     break;
 3669                 } else {
 3670                     xmits++;
 3671                     if (d_status & TULIP_DSTS_ERRSUM) {
 3672                         CTR1(KTR_TULIP, "tulip_tx_intr: output error: %08x",
 3673                             d_status);
 3674                         sc->tulip_ifp->if_oerrors++;
 3675                         if (d_status & TULIP_DSTS_TxEXCCOLL)
 3676                             sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
 3677                         if (d_status & TULIP_DSTS_TxLATECOLL)
 3678                             sc->tulip_dot3stats.dot3StatsLateCollisions++;
 3679                         if (d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
 3680                             sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
 3681                         if (d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
 3682                             sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
 3683                         if (d_status & TULIP_DSTS_TxUNDERFLOW)
 3684                             sc->tulip_dot3stats.dot3StatsInternalTransmitUnderflows++;
 3685                         if (d_status & TULIP_DSTS_TxBABBLE)
 3686                             sc->tulip_dot3stats.dot3StatsInternalTransmitBabbles++;
 3687                     } else {
 3688                         u_int32_t collisions = 
 3689                             (d_status & TULIP_DSTS_TxCOLLMASK)
 3690                                 >> TULIP_DSTS_V_TxCOLLCNT;
 3691 
 3692                         CTR2(KTR_TULIP,
 3693                     "tulip_tx_intr: output ok, collisions %d, status %08x",
 3694                             collisions, d_status);
 3695                         sc->tulip_ifp->if_collisions += collisions;
 3696                         if (collisions == 1)
 3697                             sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
 3698                         else if (collisions > 1)
 3699                             sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
 3700                         else if (d_status & TULIP_DSTS_TxDEFERRED)
 3701                             sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
 3702                         /*
 3703                          * SQE is only valid for 10baseT/BNC/AUI when not
 3704                          * running in full-duplex.  In order to speed up the
 3705                          * test, the corresponding bit in tulip_flags needs to
 3706                          * set as well to get us to count SQE Test Errors.
 3707                          */
 3708                         if (d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
 3709                             sc->tulip_dot3stats.dot3StatsSQETestErrors++;
 3710                     }
 3711                 }
 3712             }
 3713         }
 3714 
 3715         if (++ri->ri_nextin == ri->ri_last)
 3716             ri->ri_nextin = ri->ri_first;
 3717 
 3718         if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
 3719             sc->tulip_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
 3720     }
 3721     /*
 3722      * If nothing left to transmit, disable the timer.
 3723      * Else if progress, reset the timer back to 2 ticks.
 3724      */
 3725     if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
 3726         sc->tulip_txtimer = 0;
 3727     else if (xmits > 0)
 3728         sc->tulip_txtimer = TULIP_TXTIMER;
 3729     sc->tulip_ifp->if_opackets += xmits;
 3730     TULIP_PERFEND(txintr);
 3731     return descs;
 3732 }
 3733 
 3734 static void
 3735 tulip_print_abnormal_interrupt(tulip_softc_t * const sc, u_int32_t csr)
 3736 {
 3737     const char * const *msgp = tulip_status_bits;
 3738     const char *sep;
 3739     u_int32_t mask;
 3740     const char thrsh[] = "72|128\0\0\0" "96|256\0\0\0" "128|512\0\0" "160|1024";
 3741 
 3742     TULIP_LOCK_ASSERT(sc);
 3743     csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
 3744     device_printf(sc->tulip_dev, "abnormal interrupt:");
 3745     for (sep = " ", mask = 1; mask <= csr; mask <<= 1, msgp++) {
 3746         if ((csr & mask) && *msgp != NULL) {
 3747             printf("%s%s", sep, *msgp);
 3748             if (mask == TULIP_STS_TXUNDERFLOW && (sc->tulip_flags & TULIP_NEWTXTHRESH)) {
 3749                 sc->tulip_flags &= ~TULIP_NEWTXTHRESH;
 3750                 if (sc->tulip_cmdmode & TULIP_CMD_STOREFWD) {
 3751                     printf(" (switching to store-and-forward mode)");
 3752                 } else {
 3753                     printf(" (raising TX threshold to %s)",
 3754                            &thrsh[9 * ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) >> 14)]);
 3755                 }
 3756             }
 3757             sep = ", ";
 3758         }
 3759     }
 3760     printf("\n");
 3761 }
 3762 
 3763 static void
 3764 tulip_intr_handler(tulip_softc_t * const sc)
 3765 {
 3766     TULIP_PERFSTART(intr)
 3767     u_int32_t csr;
 3768 
 3769     CTR0(KTR_TULIP, "tulip_intr_handler invoked");
 3770     TULIP_LOCK_ASSERT(sc);
 3771     while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
 3772         TULIP_CSR_WRITE(sc, csr_status, csr);
 3773 
 3774         if (csr & TULIP_STS_SYSERROR) {
 3775             sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
 3776             if (sc->tulip_flags & TULIP_NOMESSAGES) {
 3777                 sc->tulip_flags |= TULIP_SYSTEMERROR;
 3778             } else {
 3779                 device_printf(sc->tulip_dev, "system error: %s\n",
 3780                        tulip_system_errors[sc->tulip_last_system_error]);
 3781             }
 3782             sc->tulip_flags |= TULIP_NEEDRESET;
 3783             sc->tulip_system_errors++;
 3784             break;
 3785         }
 3786         if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL) & sc->tulip_intrmask) {
 3787 #if defined(TULIP_DEBUG)
 3788             sc->tulip_dbg.dbg_link_intrs++;
 3789 #endif
 3790             if (sc->tulip_boardsw->bd_media_poll != NULL) {
 3791                 (*sc->tulip_boardsw->bd_media_poll)(sc, csr & TULIP_STS_LINKFAIL
 3792                                                     ? TULIP_MEDIAPOLL_LINKFAIL
 3793                                                     : TULIP_MEDIAPOLL_LINKPASS);
 3794                 csr &= ~TULIP_STS_ABNRMLINTR;
 3795             }
 3796             tulip_media_print(sc);
 3797         }
 3798         if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
 3799             u_int32_t misses = TULIP_CSR_READ(sc, csr_missed_frames);
 3800             if (csr & TULIP_STS_RXNOBUF)
 3801                 sc->tulip_dot3stats.dot3StatsMissedFrames += misses & 0xFFFF;
 3802             /*
 3803              * Pass 2.[012] of the 21140A-A[CDE] may hang and/or corrupt data
 3804              * on receive overflows.
 3805              */
 3806             if ((misses & 0x0FFE0000) && (sc->tulip_features & TULIP_HAVE_RXBADOVRFLW)) {
 3807                 sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
 3808                 /*
 3809                  * Stop the receiver process and spin until it's stopped.
 3810                  * Tell rx_intr to drop the packets it dequeues.
 3811                  */
 3812                 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode & ~TULIP_CMD_RXRUN);
 3813                 while ((TULIP_CSR_READ(sc, csr_status) & TULIP_STS_RXSTOPPED) == 0)
 3814                     ;
 3815                 TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
 3816                 sc->tulip_flags |= TULIP_RXIGNORE;
 3817             }
 3818             tulip_rx_intr(sc);
 3819             if (sc->tulip_flags & TULIP_RXIGNORE) {
 3820                 /*
 3821                  * Restart the receiver.
 3822                  */
 3823                 sc->tulip_flags &= ~TULIP_RXIGNORE;
 3824                 TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 3825             }
 3826         }
 3827         if (csr & TULIP_STS_ABNRMLINTR) {
 3828             u_int32_t tmp = csr & sc->tulip_intrmask
 3829                 & ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
 3830             if (csr & TULIP_STS_TXUNDERFLOW) {
 3831                 if ((sc->tulip_cmdmode & TULIP_CMD_THRESHOLDCTL) != TULIP_CMD_THRSHLD160) {
 3832                     sc->tulip_cmdmode += TULIP_CMD_THRSHLD96;
 3833                     sc->tulip_flags |= TULIP_NEWTXTHRESH;
 3834                 } else if (sc->tulip_features & TULIP_HAVE_STOREFWD) {
 3835                     sc->tulip_cmdmode |= TULIP_CMD_STOREFWD;
 3836                     sc->tulip_flags |= TULIP_NEWTXTHRESH;
 3837                 }
 3838             }
 3839             if (sc->tulip_flags & TULIP_NOMESSAGES) {
 3840                 sc->tulip_statusbits |= tmp;
 3841             } else {
 3842                 tulip_print_abnormal_interrupt(sc, tmp);
 3843                 sc->tulip_flags |= TULIP_NOMESSAGES;
 3844             }
 3845             TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
 3846         }
 3847         if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_TXPROBE_ACTIVE|TULIP_DOINGSETUP|TULIP_PROMISC)) {
 3848             tulip_tx_intr(sc);
 3849             if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
 3850                 tulip_start_locked(sc);
 3851         }
 3852     }
 3853     if (sc->tulip_flags & TULIP_NEEDRESET) {
 3854         tulip_reset(sc);
 3855         tulip_init_locked(sc);
 3856     }
 3857     TULIP_PERFEND(intr);
 3858 }
 3859 
 3860 static void
 3861 tulip_intr_shared(void *arg)
 3862 {
 3863     tulip_softc_t * sc = arg;
 3864 
 3865     for (; sc != NULL; sc = sc->tulip_slaves) {
 3866         TULIP_LOCK(sc);
 3867 #if defined(TULIP_DEBUG)
 3868         sc->tulip_dbg.dbg_intrs++;
 3869 #endif
 3870         tulip_intr_handler(sc);
 3871         TULIP_UNLOCK(sc);
 3872     }
 3873 }
 3874 
 3875 static void
 3876 tulip_intr_normal(void *arg)
 3877 {
 3878     tulip_softc_t * sc = (tulip_softc_t *) arg;
 3879 
 3880     TULIP_LOCK(sc);
 3881 #if defined(TULIP_DEBUG)
 3882     sc->tulip_dbg.dbg_intrs++;
 3883 #endif
 3884     tulip_intr_handler(sc);
 3885     TULIP_UNLOCK(sc);
 3886 }
 3887 
 3888 static struct mbuf *
 3889 tulip_txput(tulip_softc_t * const sc, struct mbuf *m)
 3890 {
 3891     TULIP_PERFSTART(txput)
 3892     tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
 3893     tulip_descinfo_t *eop, *nextout;
 3894     int segcnt, free;
 3895     u_int32_t d_status;
 3896     bus_dma_segment_t segs[TULIP_MAX_TXSEG];
 3897     bus_dmamap_t *map;
 3898     int error, nsegs;
 3899     struct mbuf *m0;
 3900 
 3901     TULIP_LOCK_ASSERT(sc);
 3902 #if defined(TULIP_DEBUG)
 3903     if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
 3904         device_printf(sc->tulip_dev, "txput%s: tx not running\n",
 3905                (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
 3906         sc->tulip_flags |= TULIP_WANTTXSTART;
 3907         sc->tulip_dbg.dbg_txput_finishes[0]++;
 3908         goto finish;
 3909     }
 3910 #endif
 3911 
 3912     /*
 3913      * Now we try to fill in our transmit descriptors.  This is
 3914      * a bit reminiscent of going on the Ark two by two
 3915      * since each descriptor for the TULIP can describe
 3916      * two buffers.  So we advance through packet filling
 3917      * each of the two entries at a time to to fill each
 3918      * descriptor.  Clear the first and last segment bits
 3919      * in each descriptor (actually just clear everything
 3920      * but the end-of-ring or chain bits) to make sure
 3921      * we don't get messed up by previously sent packets.
 3922      *
 3923      * We may fail to put the entire packet on the ring if
 3924      * there is either not enough ring entries free or if the
 3925      * packet has more than MAX_TXSEG segments.  In the former
 3926      * case we will just wait for the ring to empty.  In the
 3927      * latter case we have to recopy.
 3928      */
 3929 #if defined(KTR) && KTR_TULIP
 3930     segcnt = 1;
 3931     m0 = m;
 3932     while (m0->m_next != NULL) {
 3933             segcnt++;
 3934             m0 = m0->m_next;
 3935     }
 3936 #endif
 3937     CTR2(KTR_TULIP, "tulip_txput: sending packet %p (%d chunks)", m, segcnt);
 3938     d_status = 0;
 3939     eop = nextout = ri->ri_nextout;
 3940     segcnt = 0;
 3941     free = ri->ri_free;
 3942 
 3943     /*
 3944      * Reclaim some tx descriptors if we are out since we need at least one
 3945      * free descriptor so that we have a dma_map to load the mbuf.
 3946      */
 3947     if (free == 0) {
 3948 #if defined(TULIP_DEBUG)
 3949         sc->tulip_dbg.dbg_no_txmaps++;
 3950 #endif
 3951         free += tulip_tx_intr(sc);
 3952     }
 3953     if (free == 0) {
 3954         sc->tulip_flags |= TULIP_WANTTXSTART;
 3955 #if defined(TULIP_DEBUG)
 3956         sc->tulip_dbg.dbg_txput_finishes[1]++;
 3957 #endif
 3958         goto finish;
 3959     }
 3960     error = bus_dmamap_load_mbuf_sg(ri->ri_data_tag, *eop->di_map, m, segs,
 3961         &nsegs, BUS_DMA_NOWAIT);
 3962     if (error != 0) {
 3963         if (error == EFBIG) {
 3964             /*
 3965              * The packet exceeds the number of transmit buffer
 3966              * entries that we can use for one packet, so we have
 3967              * to recopy it into one mbuf and then try again.  If
 3968              * we can't recopy it, try again later.
 3969              */
 3970             m0 = m_defrag(m, M_DONTWAIT);
 3971             if (m0 == NULL) {
 3972                 sc->tulip_flags |= TULIP_WANTTXSTART;
 3973 #if defined(TULIP_DEBUG)
 3974                 sc->tulip_dbg.dbg_txput_finishes[2]++;
 3975 #endif
 3976                 goto finish;
 3977             }
 3978             m = m0;
 3979             error = bus_dmamap_load_mbuf_sg(ri->ri_data_tag, *eop->di_map, m,
 3980                 segs, &nsegs, BUS_DMA_NOWAIT);
 3981         }
 3982         if (error != 0) {
 3983             device_printf(sc->tulip_dev,
 3984                 "unable to load tx map, error = %d\n", error);
 3985 #if defined(TULIP_DEBUG)
 3986             sc->tulip_dbg.dbg_txput_finishes[3]++;
 3987 #endif
 3988             goto finish;
 3989         }
 3990     }
 3991     CTR1(KTR_TULIP, "tulip_txput: nsegs %d", nsegs);
 3992 
 3993     /*
 3994      * Each descriptor allows for up to 2 fragments since we don't use
 3995      * the descriptor chaining mode in this driver.
 3996      */
 3997     if ((free -= (nsegs + 1) / 2) <= 0
 3998             /*
 3999              * See if there's any unclaimed space in the transmit ring.
 4000              */
 4001             && (free += tulip_tx_intr(sc)) <= 0) {
 4002         /*
 4003          * There's no more room but since nothing
 4004          * has been committed at this point, just
 4005          * show output is active, put back the
 4006          * mbuf and return.
 4007          */
 4008         sc->tulip_flags |= TULIP_WANTTXSTART;
 4009 #if defined(TULIP_DEBUG)
 4010         sc->tulip_dbg.dbg_txput_finishes[4]++;
 4011 #endif
 4012         bus_dmamap_unload(ri->ri_data_tag, *eop->di_map);
 4013         goto finish;
 4014     }
 4015     for (; nsegs - segcnt > 1; segcnt += 2) {
 4016         eop = nextout;
 4017         eop->di_desc->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
 4018         eop->di_desc->d_status  = d_status;
 4019         eop->di_desc->d_addr1   = segs[segcnt].ds_addr & 0xffffffff;
 4020         eop->di_desc->d_length1 = segs[segcnt].ds_len;
 4021         eop->di_desc->d_addr2   = segs[segcnt+1].ds_addr & 0xffffffff;
 4022         eop->di_desc->d_length2 = segs[segcnt+1].ds_len;
 4023         d_status = TULIP_DSTS_OWNER;
 4024         if (++nextout == ri->ri_last)
 4025             nextout = ri->ri_first;
 4026     }
 4027     if (segcnt < nsegs) {
 4028         eop = nextout;
 4029         eop->di_desc->d_flag   &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
 4030         eop->di_desc->d_status  = d_status;
 4031         eop->di_desc->d_addr1   = segs[segcnt].ds_addr & 0xffffffff;
 4032         eop->di_desc->d_length1 = segs[segcnt].ds_len;
 4033         eop->di_desc->d_addr2   = 0;
 4034         eop->di_desc->d_length2 = 0;
 4035         if (++nextout == ri->ri_last)
 4036             nextout = ri->ri_first;
 4037     }
 4038 
 4039     /*
 4040      * tulip_tx_intr() harvests the mbuf from the last descriptor in the
 4041      * frame.  We just used the dmamap in the first descriptor for the
 4042      * load operation however.  Thus, to let the tulip_dequeue_mbuf() call
 4043      * in tulip_tx_intr() unload the correct dmamap, we swap the dmamap
 4044      * pointers in the two descriptors if this is a multiple-descriptor
 4045      * packet.
 4046      */
 4047     if (eop != ri->ri_nextout) {
 4048             map = eop->di_map;
 4049             eop->di_map = ri->ri_nextout->di_map;
 4050             ri->ri_nextout->di_map = map;
 4051     }
 4052 
 4053     /*
 4054      * bounce a copy to the bpf listener, if any.
 4055      */
 4056     if (!(sc->tulip_flags & TULIP_DEVICEPROBE))
 4057             BPF_MTAP(sc->tulip_ifp, m);
 4058 
 4059     /*
 4060      * The descriptors have been filled in.  Now get ready
 4061      * to transmit.
 4062      */
 4063     CTR3(KTR_TULIP, "tulip_txput: enqueued mbuf %p to descriptors %td - %td",
 4064         m, ri->ri_nextout - ri->ri_first, eop - ri->ri_first);
 4065     KASSERT(eop->di_mbuf == NULL, ("clobbering earlier tx mbuf"));
 4066     eop->di_mbuf = m;
 4067     TULIP_TXMAP_PRESYNC(ri, ri->ri_nextout);
 4068     m = NULL;
 4069 
 4070     /*
 4071      * Make sure the next descriptor after this packet is owned
 4072      * by us since it may have been set up above if we ran out
 4073      * of room in the ring.
 4074      */
 4075     nextout->di_desc->d_status = 0;
 4076     TULIP_TXDESC_PRESYNC(ri);
 4077 
 4078     /*
 4079      * Mark the last and first segments, indicate we want a transmit
 4080      * complete interrupt, and tell it to transmit!
 4081      */
 4082     eop->di_desc->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
 4083 
 4084     /*
 4085      * Note that ri->ri_nextout is still the start of the packet
 4086      * and until we set the OWNER bit, we can still back out of
 4087      * everything we have done.
 4088      */
 4089     ri->ri_nextout->di_desc->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
 4090     TULIP_TXDESC_PRESYNC(ri);
 4091     ri->ri_nextout->di_desc->d_status = TULIP_DSTS_OWNER;
 4092     TULIP_TXDESC_PRESYNC(ri);
 4093 
 4094     /*
 4095      * This advances the ring for us.
 4096      */
 4097     ri->ri_nextout = nextout;
 4098     ri->ri_free = free;
 4099 
 4100     TULIP_PERFEND(txput);
 4101 
 4102     if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
 4103         TULIP_CSR_WRITE(sc, csr_txpoll, 1);
 4104         sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 4105         TULIP_PERFEND(txput);
 4106         return NULL;
 4107     }
 4108 
 4109     /*
 4110      * switch back to the single queueing ifstart.
 4111      */
 4112     sc->tulip_flags &= ~TULIP_WANTTXSTART;
 4113     if (sc->tulip_txtimer == 0)
 4114         sc->tulip_txtimer = TULIP_TXTIMER;
 4115 #if defined(TULIP_DEBUG)
 4116     sc->tulip_dbg.dbg_txput_finishes[5]++;
 4117 #endif
 4118 
 4119     /*
 4120      * If we want a txstart, there must be not enough space in the
 4121      * transmit ring.  So we want to enable transmit done interrupts
 4122      * so we can immediately reclaim some space.  When the transmit
 4123      * interrupt is posted, the interrupt handler will call tx_intr
 4124      * to reclaim space and then txstart (since WANTTXSTART is set).
 4125      * txstart will move the packet into the transmit ring and clear
 4126      * WANTTXSTART thereby causing TXINTR to be cleared.
 4127      */
 4128   finish:
 4129 #if defined(TULIP_DEBUG)
 4130     sc->tulip_dbg.dbg_txput_finishes[6]++;
 4131 #endif
 4132     if (sc->tulip_flags & (TULIP_WANTTXSTART|TULIP_DOINGSETUP)) {
 4133         sc->tulip_ifp->if_drv_flags |= IFF_DRV_OACTIVE;
 4134         if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
 4135             sc->tulip_intrmask |= TULIP_STS_TXINTR;
 4136             TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
 4137         }
 4138     } else if ((sc->tulip_flags & TULIP_PROMISC) == 0) {
 4139         if (sc->tulip_intrmask & TULIP_STS_TXINTR) {
 4140             sc->tulip_intrmask &= ~TULIP_STS_TXINTR;
 4141             TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
 4142         }
 4143     }
 4144     TULIP_CSR_WRITE(sc, csr_txpoll, 1);
 4145     TULIP_PERFEND(txput);
 4146     return m;
 4147 }
 4148 
 4149 static void
 4150 tulip_txput_setup(tulip_softc_t * const sc)
 4151 {
 4152     tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
 4153     tulip_desc_t *nextout;
 4154 
 4155     TULIP_LOCK_ASSERT(sc);
 4156 
 4157     /*
 4158      * We will transmit, at most, one setup packet per call to ifstart.
 4159      */
 4160 
 4161 #if defined(TULIP_DEBUG)
 4162     if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
 4163         device_printf(sc->tulip_dev, "txput_setup: tx not running\n");
 4164         sc->tulip_flags |= TULIP_WANTTXSTART;
 4165         return;
 4166     }
 4167 #endif
 4168     /*
 4169      * Try to reclaim some free descriptors..
 4170      */
 4171     if (ri->ri_free < 2)
 4172         tulip_tx_intr(sc);
 4173     if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
 4174         sc->tulip_flags |= TULIP_WANTTXSTART;
 4175         return;
 4176     }
 4177     bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
 4178           sizeof(sc->tulip_setupdata));
 4179     /*
 4180      * Clear WANTSETUP and set DOINGSETUP.  Since we know that WANTSETUP is
 4181      * set and DOINGSETUP is clear doing an XOR of the two will DTRT.
 4182      */
 4183     sc->tulip_flags ^= TULIP_WANTSETUP|TULIP_DOINGSETUP;
 4184     ri->ri_free--;
 4185     nextout = ri->ri_nextout->di_desc;
 4186     nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
 4187     nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
 4188         |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
 4189     if (sc->tulip_flags & TULIP_WANTHASHPERFECT)
 4190         nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
 4191     else if (sc->tulip_flags & TULIP_WANTHASHONLY)
 4192         nextout->d_flag |= TULIP_DFLAG_TxHASHFILT|TULIP_DFLAG_TxINVRSFILT;
 4193 
 4194     nextout->d_length2 = 0;
 4195     nextout->d_addr2 = 0;
 4196     nextout->d_length1 = sizeof(sc->tulip_setupdata);
 4197     nextout->d_addr1 = sc->tulip_setup_dma_addr & 0xffffffff;
 4198     bus_dmamap_sync(sc->tulip_setup_tag, sc->tulip_setup_map,
 4199         BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 4200     TULIP_TXDESC_PRESYNC(ri);
 4201     CTR1(KTR_TULIP, "tulip_txput_setup: using descriptor %td",
 4202         ri->ri_nextout - ri->ri_first);
 4203 
 4204     /*
 4205      * Advance the ring for the next transmit packet.
 4206      */
 4207     if (++ri->ri_nextout == ri->ri_last)
 4208         ri->ri_nextout = ri->ri_first;
 4209 
 4210     /*
 4211      * Make sure the next descriptor is owned by us since it
 4212      * may have been set up above if we ran out of room in the
 4213      * ring.
 4214      */
 4215     ri->ri_nextout->di_desc->d_status = 0;
 4216     TULIP_TXDESC_PRESYNC(ri);
 4217     nextout->d_status = TULIP_DSTS_OWNER;
 4218     /*
 4219      * Flush the ownwership of the current descriptor
 4220      */
 4221     TULIP_TXDESC_PRESYNC(ri);
 4222     TULIP_CSR_WRITE(sc, csr_txpoll, 1);
 4223     if ((sc->tulip_intrmask & TULIP_STS_TXINTR) == 0) {
 4224         sc->tulip_intrmask |= TULIP_STS_TXINTR;
 4225         TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
 4226     }
 4227 }
 4228 
 4229 static int
 4230 tulip_ifioctl(struct ifnet * ifp, u_long cmd, caddr_t data)
 4231 {
 4232     TULIP_PERFSTART(ifioctl)
 4233     tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
 4234     struct ifreq *ifr = (struct ifreq *) data;
 4235     int error = 0;
 4236 
 4237     switch (cmd) {
 4238         case SIOCSIFFLAGS: {
 4239             TULIP_LOCK(sc);
 4240             tulip_init_locked(sc);
 4241             TULIP_UNLOCK(sc);
 4242             break;
 4243         }
 4244 
 4245         case SIOCSIFMEDIA:
 4246         case SIOCGIFMEDIA: {
 4247             error = ifmedia_ioctl(ifp, ifr, &sc->tulip_ifmedia, cmd);
 4248             break;
 4249         }
 4250 
 4251         case SIOCADDMULTI:
 4252         case SIOCDELMULTI: {
 4253             /*
 4254              * Update multicast listeners
 4255              */
 4256             TULIP_LOCK(sc);
 4257             tulip_init_locked(sc);
 4258             TULIP_UNLOCK(sc);
 4259             error = 0;
 4260             break;
 4261         }
 4262 
 4263 #ifdef SIOCGADDRROM
 4264         case SIOCGADDRROM: {
 4265             error = copyout(sc->tulip_rombuf, ifr->ifr_data, sizeof(sc->tulip_rombuf));
 4266             break;
 4267         }
 4268 #endif
 4269 #ifdef SIOCGCHIPID
 4270         case SIOCGCHIPID: {
 4271             ifr->ifr_metric = (int) sc->tulip_chipid;
 4272             break;
 4273         }
 4274 #endif
 4275         default: {
 4276             error = ether_ioctl(ifp, cmd, data);
 4277             break;
 4278         }
 4279     }
 4280 
 4281     TULIP_PERFEND(ifioctl);
 4282     return error;
 4283 }
 4284 
 4285 static void
 4286 tulip_start(struct ifnet * const ifp)
 4287 {
 4288     TULIP_PERFSTART(ifstart)
 4289     tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
 4290 
 4291     TULIP_LOCK(sc);
 4292     tulip_start_locked(sc);
 4293     TULIP_UNLOCK(sc);
 4294 
 4295     TULIP_PERFEND(ifstart);
 4296 }
 4297 
 4298 static void
 4299 tulip_start_locked(tulip_softc_t * const sc)
 4300 {
 4301     struct mbuf *m;
 4302 
 4303     TULIP_LOCK_ASSERT(sc);
 4304 
 4305     CTR0(KTR_TULIP, "tulip_start_locked invoked");
 4306     if ((sc->tulip_flags & (TULIP_WANTSETUP|TULIP_TXPROBE_ACTIVE)) == TULIP_WANTSETUP)
 4307         tulip_txput_setup(sc);
 4308 
 4309     CTR1(KTR_TULIP, "tulip_start_locked: %d tx packets pending",
 4310         sc->tulip_ifp->if_snd.ifq_len);
 4311     while (!IFQ_DRV_IS_EMPTY(&sc->tulip_ifp->if_snd)) {
 4312         IFQ_DRV_DEQUEUE(&sc->tulip_ifp->if_snd, m);
 4313         if(m == NULL)
 4314             break;
 4315         if ((m = tulip_txput(sc, m)) != NULL) {
 4316             IFQ_DRV_PREPEND(&sc->tulip_ifp->if_snd, m);
 4317             break;
 4318         }
 4319     }
 4320 }
 4321 
 4322 /*
 4323  * Even though this routine runs at device spl, it does not break
 4324  * our use of splnet (splsoftnet under NetBSD) for the majority
 4325  * of this driver since 
 4326  * if_watcbog is called from if_watchdog which is called from
 4327  * splsoftclock which is below spl[soft]net.
 4328  */
 4329 static void
 4330 tulip_ifwatchdog(struct ifnet *ifp)
 4331 {
 4332     TULIP_PERFSTART(ifwatchdog)
 4333     tulip_softc_t * const sc = (tulip_softc_t *)ifp->if_softc;
 4334 #if defined(TULIP_DEBUG)
 4335     u_int32_t rxintrs;
 4336 #endif
 4337 
 4338     TULIP_LOCK(sc);
 4339 #if defined(TULIP_DEBUG)
 4340     rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
 4341     if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
 4342         sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
 4343     sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
 4344 #endif /* TULIP_DEBUG */
 4345 
 4346     sc->tulip_ifp->if_timer = 1;
 4347     /*
 4348      * These should be rare so do a bulk test up front so we can just skip
 4349      * them if needed.
 4350      */
 4351     if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_NOMESSAGES)) {
 4352         /*
 4353          * If the number of receive buffer is low, try to refill
 4354          */
 4355         if (sc->tulip_flags & TULIP_RXBUFSLOW)
 4356             tulip_rx_intr(sc);
 4357 
 4358         if (sc->tulip_flags & TULIP_SYSTEMERROR) {
 4359             if_printf(sc->tulip_ifp, "%d system errors: last was %s\n",
 4360                    sc->tulip_system_errors,
 4361                    tulip_system_errors[sc->tulip_last_system_error]);
 4362         }
 4363         if (sc->tulip_statusbits) {
 4364             tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
 4365             sc->tulip_statusbits = 0;
 4366         }
 4367 
 4368         sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
 4369     }
 4370 
 4371     if (sc->tulip_txtimer)
 4372         tulip_tx_intr(sc);
 4373     if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
 4374         if_printf(sc->tulip_ifp, "transmission timeout\n");
 4375         if (TULIP_DO_AUTOSENSE(sc)) {
 4376             sc->tulip_media = TULIP_MEDIA_UNKNOWN;
 4377             sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
 4378             sc->tulip_flags &= ~(TULIP_WANTRXACT|TULIP_LINKUP);
 4379         }
 4380         tulip_reset(sc);
 4381         tulip_init_locked(sc);
 4382     }
 4383 
 4384     TULIP_PERFEND(ifwatchdog);
 4385     TULIP_PERFMERGE(sc, perf_intr_cycles);
 4386     TULIP_PERFMERGE(sc, perf_ifstart_cycles);
 4387     TULIP_PERFMERGE(sc, perf_ifioctl_cycles);
 4388     TULIP_PERFMERGE(sc, perf_ifwatchdog_cycles);
 4389     TULIP_PERFMERGE(sc, perf_timeout_cycles);
 4390     TULIP_PERFMERGE(sc, perf_ifstart_one_cycles);
 4391     TULIP_PERFMERGE(sc, perf_txput_cycles);
 4392     TULIP_PERFMERGE(sc, perf_txintr_cycles);
 4393     TULIP_PERFMERGE(sc, perf_rxintr_cycles);
 4394     TULIP_PERFMERGE(sc, perf_rxget_cycles);
 4395     TULIP_PERFMERGE(sc, perf_intr);
 4396     TULIP_PERFMERGE(sc, perf_ifstart);
 4397     TULIP_PERFMERGE(sc, perf_ifioctl);
 4398     TULIP_PERFMERGE(sc, perf_ifwatchdog);
 4399     TULIP_PERFMERGE(sc, perf_timeout);
 4400     TULIP_PERFMERGE(sc, perf_ifstart_one);
 4401     TULIP_PERFMERGE(sc, perf_txput);
 4402     TULIP_PERFMERGE(sc, perf_txintr);
 4403     TULIP_PERFMERGE(sc, perf_rxintr);
 4404     TULIP_PERFMERGE(sc, perf_rxget);
 4405     TULIP_UNLOCK(sc);
 4406 }
 4407 
 4408 static void
 4409 tulip_attach(tulip_softc_t * const sc)
 4410 {
 4411     struct ifnet *ifp;
 4412 
 4413     ifp = sc->tulip_ifp = if_alloc(IFT_ETHER);
 4414 
 4415     /* XXX: driver name/unit should be set some other way */
 4416     if_initname(ifp, "de", sc->tulip_unit);
 4417     ifp->if_softc = sc;
 4418     ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
 4419     ifp->if_ioctl = tulip_ifioctl;
 4420     ifp->if_start = tulip_start;
 4421     ifp->if_watchdog = tulip_ifwatchdog;
 4422     ifp->if_timer = 1;
 4423     ifp->if_init = tulip_init;
 4424     IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
 4425     ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
 4426     IFQ_SET_READY(&ifp->if_snd);
 4427   
 4428     device_printf(sc->tulip_dev, "%s%s pass %d.%d%s\n",
 4429            sc->tulip_boardid,
 4430            tulip_chipdescs[sc->tulip_chipid],
 4431            (sc->tulip_revinfo & 0xF0) >> 4,
 4432            sc->tulip_revinfo & 0x0F,
 4433            (sc->tulip_features & (TULIP_HAVE_ISVSROM|TULIP_HAVE_OKSROM))
 4434                  == TULIP_HAVE_ISVSROM ? " (invalid EESPROM checksum)" : "");
 4435 
 4436     TULIP_LOCK(sc);
 4437     (*sc->tulip_boardsw->bd_media_probe)(sc);
 4438     ifmedia_init(&sc->tulip_ifmedia, 0,
 4439                  tulip_ifmedia_change,
 4440                  tulip_ifmedia_status);
 4441     tulip_ifmedia_add(sc);
 4442 
 4443     tulip_reset(sc);
 4444     TULIP_UNLOCK(sc);
 4445 
 4446     ether_ifattach(sc->tulip_ifp, sc->tulip_enaddr);
 4447 
 4448     TULIP_LOCK(sc);
 4449     sc->tulip_flags &= ~TULIP_DEVICEPROBE;
 4450     TULIP_UNLOCK(sc);
 4451 }
 4452 
 4453 /* Release memory for a single descriptor ring. */
 4454 static void
 4455 tulip_busdma_freering(tulip_ringinfo_t *ri)
 4456 {
 4457     int i;
 4458 
 4459     /* Release the DMA maps and tag for data buffers. */
 4460     if (ri->ri_data_maps != NULL) {
 4461         for (i = 0; i < ri->ri_max; i++) {
 4462             if (ri->ri_data_maps[i] != NULL) {
 4463                 bus_dmamap_destroy(ri->ri_data_tag, ri->ri_data_maps[i]);
 4464                 ri->ri_data_maps[i] = NULL;
 4465             }
 4466         }
 4467         free(ri->ri_data_maps, M_DEVBUF);
 4468         ri->ri_data_maps = NULL;
 4469     }
 4470     if (ri->ri_data_tag != NULL) {
 4471         bus_dma_tag_destroy(ri->ri_data_tag);
 4472         ri->ri_data_tag = NULL;
 4473     }
 4474 
 4475     /* Release the DMA memory and tag for the ring descriptors. */
 4476     if (ri->ri_dma_addr != 0) {
 4477         bus_dmamap_unload(ri->ri_ring_tag, ri->ri_ring_map);
 4478         ri->ri_dma_addr = 0;
 4479     }
 4480     if (ri->ri_descs != NULL) {
 4481         bus_dmamem_free(ri->ri_ring_tag, ri->ri_descs, ri->ri_ring_map);
 4482         ri->ri_ring_map = NULL;
 4483         ri->ri_descs = NULL;
 4484     }
 4485     if (ri->ri_ring_tag != NULL) {
 4486         bus_dma_tag_destroy(ri->ri_ring_tag);
 4487         ri->ri_ring_tag = NULL;
 4488     }
 4489 }
 4490 
 4491 /* Allocate memory for a single descriptor ring. */
 4492 static int
 4493 tulip_busdma_allocring(device_t dev, tulip_softc_t * const sc, size_t count,
 4494     bus_size_t align, int nsegs, tulip_ringinfo_t *ri, const char *name)
 4495 {
 4496     size_t size;
 4497     int error, i;
 4498 
 4499     /* First, setup a tag. */
 4500     ri->ri_max = count;
 4501     size = count * sizeof(tulip_desc_t);
 4502     error = bus_dma_tag_create(bus_get_dma_tag(dev),
 4503         32, 0, BUS_SPACE_MAXADDR_32BIT,
 4504         BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, NULL, NULL,
 4505         &ri->ri_ring_tag);
 4506     if (error) {
 4507         device_printf(dev, "failed to allocate %s descriptor ring dma tag\n",
 4508             name);
 4509         return (error);
 4510     }
 4511 
 4512     /* Next, allocate memory for the descriptors. */
 4513     error = bus_dmamem_alloc(ri->ri_ring_tag, (void **)&ri->ri_descs,
 4514         BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ri->ri_ring_map);
 4515     if (error) {
 4516         device_printf(dev, "failed to allocate memory for %s descriptor ring\n",
 4517             name);
 4518         return (error);
 4519     }
 4520 
 4521     /* Map the descriptors. */
 4522     error = bus_dmamap_load(ri->ri_ring_tag, ri->ri_ring_map, ri->ri_descs,
 4523         size, tulip_dma_map_addr, &ri->ri_dma_addr, BUS_DMA_NOWAIT);
 4524     if (error) {
 4525         device_printf(dev, "failed to get dma address for %s descriptor ring\n",
 4526             name);
 4527         return (error);
 4528     }
 4529 
 4530     /* Allocate a tag for the data buffers. */
 4531     error = bus_dma_tag_create(bus_get_dma_tag(dev), align, 0,
 4532         BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
 4533         MCLBYTES * nsegs, nsegs, MCLBYTES, 0, NULL, NULL, &ri->ri_data_tag);
 4534     if (error) {
 4535         device_printf(dev, "failed to allocate %s buffer dma tag\n", name);
 4536         return (error);
 4537     }
 4538 
 4539     /* Allocate maps for the data buffers. */
 4540     ri->ri_data_maps = malloc(sizeof(bus_dmamap_t) * count, M_DEVBUF,
 4541         M_WAITOK | M_ZERO);
 4542     for (i = 0; i < count; i++) {
 4543         error = bus_dmamap_create(ri->ri_data_tag, 0, &ri->ri_data_maps[i]);
 4544         if (error) {
 4545             device_printf(dev, "failed to create map for %s buffer %d\n",
 4546                 name, i);
 4547             return (error);
 4548         }
 4549     }
 4550 
 4551     return (0);
 4552 }
 4553 
 4554 /* Release busdma maps, tags, and memory. */
 4555 static void
 4556 tulip_busdma_cleanup(tulip_softc_t * const sc)
 4557 {
 4558 
 4559     /* Release resources for the setup descriptor. */
 4560     if (sc->tulip_setup_dma_addr != 0) {
 4561         bus_dmamap_unload(sc->tulip_setup_tag, sc->tulip_setup_map);
 4562         sc->tulip_setup_dma_addr = 0;
 4563     }
 4564     if (sc->tulip_setupbuf != NULL) {
 4565         bus_dmamem_free(sc->tulip_setup_tag, sc->tulip_setupbuf,
 4566             sc->tulip_setup_map);
 4567         bus_dmamap_destroy(sc->tulip_setup_tag, sc->tulip_setup_map);
 4568         sc->tulip_setup_map = NULL;
 4569         sc->tulip_setupbuf = NULL;
 4570     }
 4571     if (sc->tulip_setup_tag != NULL) {
 4572         bus_dma_tag_destroy(sc->tulip_setup_tag);
 4573         sc->tulip_setup_tag = NULL;
 4574     }
 4575 
 4576     /* Release the transmit ring. */
 4577     tulip_busdma_freering(&sc->tulip_txinfo);
 4578 
 4579     /* Release the receive ring. */
 4580     tulip_busdma_freering(&sc->tulip_rxinfo);
 4581 }
 4582 
 4583 static int
 4584 tulip_busdma_init(device_t dev, tulip_softc_t * const sc)
 4585 {
 4586     int error;
 4587 
 4588     /*
 4589      * Allocate space and dmamap for transmit ring.
 4590      */
 4591     error = tulip_busdma_allocring(dev, sc, TULIP_TXDESCS, 1, TULIP_MAX_TXSEG,
 4592         &sc->tulip_txinfo, "transmit");
 4593     if (error)
 4594         return (error);
 4595 
 4596     /*
 4597      * Allocate space and dmamap for receive ring.  We tell bus_dma that
 4598      * we can map MCLBYTES so that it will accept a full MCLBYTES cluster,
 4599      * but we will only map the first TULIP_RX_BUFLEN bytes.  This is not
 4600      * a waste in practice though as an ethernet frame can easily fit
 4601      * in TULIP_RX_BUFLEN bytes.
 4602      */
 4603     error = tulip_busdma_allocring(dev, sc, TULIP_RXDESCS, 4, 1,
 4604         &sc->tulip_rxinfo, "receive");
 4605     if (error)
 4606         return (error);
 4607 
 4608     /*
 4609      * Allocate a DMA tag, memory, and map for setup descriptor
 4610      */
 4611     error = bus_dma_tag_create(bus_get_dma_tag(dev), 32, 0,
 4612         BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
 4613         sizeof(sc->tulip_setupdata), 1, sizeof(sc->tulip_setupdata), 0,
 4614         NULL, NULL, &sc->tulip_setup_tag);
 4615     if (error) {
 4616         device_printf(dev, "failed to allocate setup descriptor dma tag\n");
 4617         return (error);
 4618     }
 4619     error = bus_dmamem_alloc(sc->tulip_setup_tag, (void **)&sc->tulip_setupbuf,
 4620         BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tulip_setup_map);
 4621     if (error) {
 4622         device_printf(dev, "failed to allocate memory for setup descriptor\n");
 4623         return (error);
 4624     }
 4625     error = bus_dmamap_load(sc->tulip_setup_tag, sc->tulip_setup_map,
 4626         sc->tulip_setupbuf, sizeof(sc->tulip_setupdata), 
 4627         tulip_dma_map_addr, &sc->tulip_setup_dma_addr, BUS_DMA_NOWAIT);
 4628     if (error) {
 4629         device_printf(dev, "failed to get dma address for setup descriptor\n");
 4630         return (error);
 4631     }
 4632 
 4633     return error;
 4634 }
 4635 
 4636 static void
 4637 tulip_initcsrs(tulip_softc_t * const sc, tulip_csrptr_t csr_base,
 4638     size_t csr_size)
 4639 {
 4640     sc->tulip_csrs.csr_busmode          = csr_base +  0 * csr_size;
 4641     sc->tulip_csrs.csr_txpoll           = csr_base +  1 * csr_size;
 4642     sc->tulip_csrs.csr_rxpoll           = csr_base +  2 * csr_size;
 4643     sc->tulip_csrs.csr_rxlist           = csr_base +  3 * csr_size;
 4644     sc->tulip_csrs.csr_txlist           = csr_base +  4 * csr_size;
 4645     sc->tulip_csrs.csr_status           = csr_base +  5 * csr_size;
 4646     sc->tulip_csrs.csr_command          = csr_base +  6 * csr_size;
 4647     sc->tulip_csrs.csr_intr             = csr_base +  7 * csr_size;
 4648     sc->tulip_csrs.csr_missed_frames    = csr_base +  8 * csr_size;
 4649     sc->tulip_csrs.csr_9                = csr_base +  9 * csr_size;
 4650     sc->tulip_csrs.csr_10               = csr_base + 10 * csr_size;
 4651     sc->tulip_csrs.csr_11               = csr_base + 11 * csr_size;
 4652     sc->tulip_csrs.csr_12               = csr_base + 12 * csr_size;
 4653     sc->tulip_csrs.csr_13               = csr_base + 13 * csr_size;
 4654     sc->tulip_csrs.csr_14               = csr_base + 14 * csr_size;
 4655     sc->tulip_csrs.csr_15               = csr_base + 15 * csr_size;
 4656 }
 4657 
 4658 static int
 4659 tulip_initring(
 4660     device_t dev,
 4661     tulip_softc_t * const sc,
 4662     tulip_ringinfo_t * const ri,
 4663     int ndescs)
 4664 {
 4665     int i;
 4666 
 4667     ri->ri_descinfo = malloc(sizeof(tulip_descinfo_t) * ndescs, M_DEVBUF,
 4668         M_WAITOK | M_ZERO);
 4669     for (i = 0; i < ndescs; i++) {
 4670         ri->ri_descinfo[i].di_desc = &ri->ri_descs[i];
 4671         ri->ri_descinfo[i].di_map = &ri->ri_data_maps[i];
 4672     }
 4673     ri->ri_first = ri->ri_descinfo;
 4674     ri->ri_max = ndescs;
 4675     ri->ri_last = ri->ri_first + ri->ri_max;
 4676     bzero(ri->ri_descs, sizeof(tulip_desc_t) * ri->ri_max);
 4677     ri->ri_last[-1].di_desc->d_flag = TULIP_DFLAG_ENDRING;
 4678     return (0);
 4679 }
 4680 
 4681 /*
 4682  * This is the PCI configuration support.
 4683  */
 4684 
 4685 #define PCI_CBIO        PCIR_BAR(0)     /* Configuration Base IO Address */
 4686 #define PCI_CBMA        PCIR_BAR(1)     /* Configuration Base Memory Address */
 4687 #define PCI_CFDA        0x40    /* Configuration Driver Area */
 4688 
 4689 static int
 4690 tulip_pci_probe(device_t dev)
 4691 {
 4692     const char *name = NULL;
 4693 
 4694     if (pci_get_vendor(dev) != DEC_VENDORID)
 4695         return ENXIO;
 4696 
 4697     /*
 4698      * Some LanMedia WAN cards use the Tulip chip, but they have
 4699      * their own driver, and we should not recognize them
 4700      */
 4701     if (pci_get_subvendor(dev) == 0x1376)
 4702         return ENXIO;
 4703 
 4704     switch (pci_get_device(dev)) {
 4705     case CHIPID_21040:
 4706         name = "Digital 21040 Ethernet";
 4707         break;
 4708     case CHIPID_21041:
 4709         name = "Digital 21041 Ethernet";
 4710         break;
 4711     case CHIPID_21140:
 4712         if (pci_get_revid(dev) >= 0x20)
 4713             name = "Digital 21140A Fast Ethernet";
 4714         else
 4715             name = "Digital 21140 Fast Ethernet";
 4716         break;
 4717     case CHIPID_21142:
 4718         if (pci_get_revid(dev) >= 0x20)
 4719             name = "Digital 21143 Fast Ethernet";
 4720         else
 4721             name = "Digital 21142 Fast Ethernet";
 4722         break;
 4723     }
 4724     if (name) {
 4725         device_set_desc(dev, name);
 4726         return BUS_PROBE_LOW_PRIORITY;
 4727     }
 4728     return ENXIO;
 4729 }
 4730 
 4731 static int
 4732 tulip_shutdown(device_t dev)
 4733 {
 4734     tulip_softc_t * const sc = device_get_softc(dev);
 4735     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
 4736     DELAY(10);  /* Wait 10 microseconds (actually 50 PCI cycles but at 
 4737                    33MHz that comes to two microseconds but wait a
 4738                    bit longer anyways) */
 4739     return 0;
 4740 }
 4741 
 4742 static int
 4743 tulip_pci_attach(device_t dev)
 4744 {
 4745     tulip_softc_t *sc;
 4746     int retval, idx;
 4747     u_int32_t revinfo, cfdainfo;
 4748     unsigned csroffset = TULIP_PCI_CSROFFSET;
 4749     unsigned csrsize = TULIP_PCI_CSRSIZE;
 4750     tulip_csrptr_t csr_base;
 4751     tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
 4752     struct resource *res;
 4753     int rid, unit;
 4754 
 4755     unit = device_get_unit(dev);
 4756 
 4757     if (unit >= TULIP_MAX_DEVICES) {
 4758         device_printf(dev, "not configured; limit of %d reached or exceeded\n",
 4759                TULIP_MAX_DEVICES);
 4760         return ENXIO;
 4761     }
 4762 
 4763     revinfo  = pci_get_revid(dev);
 4764     cfdainfo = pci_read_config(dev, PCI_CFDA, 4);
 4765 
 4766     /* turn busmaster on in case BIOS doesn't set it */
 4767     pci_enable_busmaster(dev);
 4768 
 4769     if (pci_get_vendor(dev) == DEC_VENDORID) {
 4770         if (pci_get_device(dev) == CHIPID_21040)
 4771                 chipid = TULIP_21040;
 4772         else if (pci_get_device(dev) == CHIPID_21041)
 4773                 chipid = TULIP_21041;
 4774         else if (pci_get_device(dev) == CHIPID_21140)
 4775                 chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
 4776         else if (pci_get_device(dev) == CHIPID_21142)
 4777                 chipid = (revinfo >= 0x20) ? TULIP_21143 : TULIP_21142;
 4778     }
 4779     if (chipid == TULIP_CHIPID_UNKNOWN)
 4780         return ENXIO;
 4781 
 4782     if (chipid == TULIP_21040 && revinfo < 0x20) {
 4783         device_printf(dev,
 4784             "not configured; 21040 pass 2.0 required (%d.%d found)\n",
 4785             revinfo >> 4, revinfo & 0x0f);
 4786         return ENXIO;
 4787     } else if (chipid == TULIP_21140 && revinfo < 0x11) {
 4788         device_printf(dev,
 4789             "not configured; 21140 pass 1.1 required (%d.%d found)\n",
 4790             revinfo >> 4, revinfo & 0x0f);
 4791         return ENXIO;
 4792     }
 4793 
 4794     sc = device_get_softc(dev);
 4795     sc->tulip_dev = dev;
 4796     sc->tulip_pci_busno = pci_get_bus(dev);
 4797     sc->tulip_pci_devno = pci_get_slot(dev);
 4798     sc->tulip_chipid = chipid;
 4799     sc->tulip_flags |= TULIP_DEVICEPROBE;
 4800     if (chipid == TULIP_21140 || chipid == TULIP_21140A)
 4801         sc->tulip_features |= TULIP_HAVE_GPR|TULIP_HAVE_STOREFWD;
 4802     if (chipid == TULIP_21140A && revinfo <= 0x22)
 4803         sc->tulip_features |= TULIP_HAVE_RXBADOVRFLW;
 4804     if (chipid == TULIP_21140)
 4805         sc->tulip_features |= TULIP_HAVE_BROKEN_HASH;
 4806     if (chipid != TULIP_21040 && chipid != TULIP_21140)
 4807         sc->tulip_features |= TULIP_HAVE_POWERMGMT;
 4808     if (chipid == TULIP_21041 || chipid == TULIP_21142 || chipid == TULIP_21143) {
 4809         sc->tulip_features |= TULIP_HAVE_DUALSENSE;
 4810         if (chipid != TULIP_21041 || revinfo >= 0x20)
 4811             sc->tulip_features |= TULIP_HAVE_SIANWAY;
 4812         if (chipid != TULIP_21041)
 4813             sc->tulip_features |= TULIP_HAVE_SIAGP|TULIP_HAVE_RXBADOVRFLW|TULIP_HAVE_STOREFWD;
 4814         if (chipid != TULIP_21041 && revinfo >= 0x20)
 4815             sc->tulip_features |= TULIP_HAVE_SIA100;
 4816     }
 4817 
 4818     if (sc->tulip_features & TULIP_HAVE_POWERMGMT
 4819             && (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
 4820         cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
 4821         pci_write_config(dev, PCI_CFDA, cfdainfo, 4);
 4822         DELAY(11*1000);
 4823     }
 4824 
 4825     sc->tulip_unit = unit;
 4826     sc->tulip_revinfo = revinfo;
 4827 #if defined(TULIP_IOMAPPED)
 4828     rid = PCI_CBIO;
 4829     res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
 4830 #else
 4831     rid = PCI_CBMA;
 4832     res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
 4833 #endif
 4834     if (!res)
 4835         return ENXIO;
 4836     sc->tulip_csrs_bst = rman_get_bustag(res);
 4837     sc->tulip_csrs_bsh = rman_get_bushandle(res);
 4838     csr_base = 0;
 4839 
 4840     mtx_init(TULIP_MUTEX(sc), MTX_NETWORK_LOCK, device_get_nameunit(dev),
 4841         MTX_DEF);
 4842     callout_init_mtx(&sc->tulip_callout, TULIP_MUTEX(sc), 0);
 4843     tulips[unit] = sc;
 4844 
 4845     tulip_initcsrs(sc, csr_base + csroffset, csrsize);
 4846 
 4847     if ((retval = tulip_busdma_init(dev, sc)) != 0) {
 4848         device_printf(dev, "error initing bus_dma: %d\n", retval);
 4849         tulip_busdma_cleanup(sc);
 4850         mtx_destroy(TULIP_MUTEX(sc));
 4851         return ENXIO;
 4852     }
 4853 
 4854     retval = tulip_initring(dev, sc, &sc->tulip_rxinfo, TULIP_RXDESCS);
 4855     if (retval == 0)
 4856         retval = tulip_initring(dev, sc, &sc->tulip_txinfo, TULIP_TXDESCS);
 4857     if (retval) {
 4858         tulip_busdma_cleanup(sc);
 4859         mtx_destroy(TULIP_MUTEX(sc));
 4860         return retval;
 4861     }
 4862 
 4863     /*
 4864      * Make sure there won't be any interrupts or such...
 4865      */
 4866     TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
 4867     DELAY(100); /* Wait 10 microseconds (actually 50 PCI cycles but at 
 4868                    33MHz that comes to two microseconds but wait a
 4869                    bit longer anyways) */
 4870 
 4871     TULIP_LOCK(sc);
 4872     retval = tulip_read_macaddr(sc);
 4873     TULIP_UNLOCK(sc);
 4874     if (retval < 0) {
 4875         device_printf(dev, "can't read ENET ROM (why=%d) (", retval);
 4876         for (idx = 0; idx < 32; idx++)
 4877             printf("%02x", sc->tulip_rombuf[idx]);
 4878         printf("\n");
 4879         device_printf(dev, "%s%s pass %d.%d\n",
 4880                sc->tulip_boardid, tulip_chipdescs[sc->tulip_chipid],
 4881                (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
 4882         device_printf(dev, "address unknown\n");
 4883     } else {
 4884         void (*intr_rtn)(void *) = tulip_intr_normal;
 4885 
 4886         if (sc->tulip_features & TULIP_HAVE_SHAREDINTR)
 4887             intr_rtn = tulip_intr_shared;
 4888 
 4889         tulip_attach(sc);
 4890 
 4891         /* Setup interrupt last. */
 4892         if ((sc->tulip_features & TULIP_HAVE_SLAVEDINTR) == 0) {
 4893             void *ih;
 4894 
 4895             rid = 0;
 4896             res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
 4897                                          RF_SHAREABLE | RF_ACTIVE);
 4898             if (res == 0 || bus_setup_intr(dev, res, INTR_TYPE_NET |
 4899                     INTR_MPSAFE, NULL, intr_rtn, sc, &ih)) {
 4900                 device_printf(dev, "couldn't map interrupt\n");
 4901                 tulip_busdma_cleanup(sc);
 4902                 ether_ifdetach(sc->tulip_ifp);
 4903                 if_free(sc->tulip_ifp);
 4904                 mtx_destroy(TULIP_MUTEX(sc));
 4905                 return ENXIO;
 4906             }
 4907         }
 4908     }
 4909     return 0;
 4910 }
 4911 
 4912 static device_method_t tulip_pci_methods[] = {
 4913     /* Device interface */
 4914     DEVMETHOD(device_probe,     tulip_pci_probe),
 4915     DEVMETHOD(device_attach,    tulip_pci_attach),
 4916     DEVMETHOD(device_shutdown,  tulip_shutdown),
 4917     { 0, 0 }
 4918 };
 4919 
 4920 static driver_t tulip_pci_driver = {
 4921     "de",
 4922     tulip_pci_methods,
 4923     sizeof(tulip_softc_t),
 4924 };
 4925 
 4926 static devclass_t tulip_devclass;
 4927 
 4928 DRIVER_MODULE(de, pci, tulip_pci_driver, tulip_devclass, 0, 0);
 4929 
 4930 #ifdef DDB
 4931 void    tulip_dumpring(int unit, int ring);
 4932 void    tulip_dumpdesc(int unit, int ring, int desc);
 4933 void    tulip_status(int unit);
 4934 
 4935 void
 4936 tulip_dumpring(int unit, int ring)
 4937 {
 4938     tulip_softc_t *sc;
 4939     tulip_ringinfo_t *ri;
 4940     tulip_descinfo_t *di;
 4941 
 4942     if (unit < 0 || unit >= TULIP_MAX_DEVICES) {
 4943         db_printf("invalid unit %d\n", unit);
 4944         return;
 4945     }
 4946     sc = tulips[unit];
 4947     if (sc == NULL) {
 4948         db_printf("unit %d not present\n", unit);
 4949         return;
 4950     }
 4951 
 4952     switch (ring) {
 4953     case 0:
 4954         db_printf("receive ring:\n");
 4955         ri = &sc->tulip_rxinfo;
 4956         break;
 4957     case 1:
 4958         db_printf("transmit ring:\n");
 4959         ri = &sc->tulip_txinfo;
 4960         break;
 4961     default:
 4962         db_printf("invalid ring %d\n", ring);
 4963         return;
 4964     }
 4965 
 4966     db_printf(" nextin: %td, nextout: %td, max: %d, free: %d\n",
 4967         ri->ri_nextin - ri->ri_first, ri->ri_nextout - ri->ri_first,
 4968         ri->ri_max, ri->ri_free);
 4969     for (di = ri->ri_first; di != ri->ri_last; di++) {
 4970         if (di->di_mbuf != NULL)
 4971             db_printf(" descriptor %td: mbuf %p\n", di - ri->ri_first,
 4972                 di->di_mbuf);
 4973         else if (di->di_desc->d_flag & TULIP_DFLAG_TxSETUPPKT)
 4974             db_printf(" descriptor %td: setup packet\n", di - ri->ri_first);
 4975     }
 4976 }
 4977 
 4978 void
 4979 tulip_dumpdesc(int unit, int ring, int desc)
 4980 {
 4981     tulip_softc_t *sc;
 4982     tulip_ringinfo_t *ri;
 4983     tulip_descinfo_t *di;
 4984     char *s;
 4985 
 4986     if (unit < 0 || unit >= TULIP_MAX_DEVICES) {
 4987         db_printf("invalid unit %d\n", unit);
 4988         return;
 4989     }
 4990     sc = tulips[unit];
 4991     if (sc == NULL) {
 4992         db_printf("unit %d not present\n", unit);
 4993         return;
 4994     }
 4995 
 4996     switch (ring) {
 4997     case 0:
 4998         s = "receive";
 4999         ri = &sc->tulip_rxinfo;
 5000         break;
 5001     case 1:
 5002         s = "transmit";
 5003         ri = &sc->tulip_txinfo;
 5004         break;
 5005     default:
 5006         db_printf("invalid ring %d\n", ring);
 5007         return;
 5008     }
 5009 
 5010     if (desc < 0 || desc >= ri->ri_max) {
 5011         db_printf("invalid descriptor %d\n", desc);
 5012         return;
 5013     }
 5014 
 5015     db_printf("%s descriptor %d:\n", s, desc);
 5016     di = &ri->ri_first[desc];
 5017     db_printf(" mbuf: %p\n", di->di_mbuf);
 5018     db_printf(" status: %08x  flag: %03x\n", di->di_desc->d_status,
 5019         di->di_desc->d_flag);
 5020     db_printf("  addr1: %08x  len1: %03x\n", di->di_desc->d_addr1,
 5021         di->di_desc->d_length1);
 5022     db_printf("  addr2: %08x  len2: %03x\n", di->di_desc->d_addr2,
 5023         di->di_desc->d_length2);
 5024 }
 5025 #endif

Cache object: b0597ddbe59b7d90337c0db6d4acd197


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