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

Cache object: 465b977c1fb73a9fdd52a61b8c6e2d55


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