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


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

FreeBSD/Linux Kernel Cross Reference
sys/dev/ic/tulip.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: tulip.c,v 1.126.2.1 2005/01/11 06:44:30 jmc Exp $      */
    2 
    3 /*-
    4  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software contributed to The NetBSD Foundation
    8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
    9  * NASA Ames Research Center; and by Charles M. Hannum.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. All advertising materials mentioning features or use of this software
   20  *    must display the following acknowledgement:
   21  *      This product includes software developed by the NetBSD
   22  *      Foundation, Inc. and its contributors.
   23  * 4. Neither the name of The NetBSD Foundation nor the names of its
   24  *    contributors may be used to endorse or promote products derived
   25  *    from this software without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   37  * POSSIBILITY OF SUCH DAMAGE.
   38  */
   39 
   40 /*
   41  * Device driver for the Digital Semiconductor ``Tulip'' (21x4x)
   42  * Ethernet controller family, and a variety of clone chips.
   43  */
   44 
   45 #include <sys/cdefs.h>
   46 __KERNEL_RCSID(0, "$NetBSD: tulip.c,v 1.126.2.1 2005/01/11 06:44:30 jmc Exp $");
   47 
   48 #include "bpfilter.h"
   49 
   50 #include <sys/param.h>
   51 #include <sys/systm.h> 
   52 #include <sys/callout.h>
   53 #include <sys/mbuf.h>   
   54 #include <sys/malloc.h>
   55 #include <sys/kernel.h>
   56 #include <sys/socket.h>
   57 #include <sys/ioctl.h>
   58 #include <sys/errno.h>
   59 #include <sys/device.h>
   60 
   61 #include <machine/endian.h>
   62 
   63 #include <uvm/uvm_extern.h>
   64  
   65 #include <net/if.h>
   66 #include <net/if_dl.h>
   67 #include <net/if_media.h>
   68 #include <net/if_ether.h>
   69 
   70 #if NBPFILTER > 0 
   71 #include <net/bpf.h>
   72 #endif 
   73 
   74 #include <machine/bus.h>
   75 #include <machine/intr.h>
   76 
   77 #include <dev/mii/mii.h>
   78 #include <dev/mii/miivar.h>
   79 #include <dev/mii/mii_bitbang.h>
   80 
   81 #include <dev/ic/tulipreg.h>
   82 #include <dev/ic/tulipvar.h>
   83 
   84 const char * const tlp_chip_names[] = TULIP_CHIP_NAMES;
   85 
   86 const struct tulip_txthresh_tab tlp_10_txthresh_tab[] =
   87     TLP_TXTHRESH_TAB_10;
   88 
   89 const struct tulip_txthresh_tab tlp_10_100_txthresh_tab[] =
   90     TLP_TXTHRESH_TAB_10_100;
   91 
   92 const struct tulip_txthresh_tab tlp_winb_txthresh_tab[] =
   93     TLP_TXTHRESH_TAB_WINB;
   94 
   95 const struct tulip_txthresh_tab tlp_dm9102_txthresh_tab[] =
   96     TLP_TXTHRESH_TAB_DM9102;
   97 
   98 void    tlp_start __P((struct ifnet *));
   99 void    tlp_watchdog __P((struct ifnet *));
  100 int     tlp_ioctl __P((struct ifnet *, u_long, caddr_t));
  101 int     tlp_init __P((struct ifnet *));
  102 void    tlp_stop __P((struct ifnet *, int));
  103 
  104 void    tlp_shutdown __P((void *));
  105 
  106 void    tlp_rxdrain __P((struct tulip_softc *));
  107 int     tlp_add_rxbuf __P((struct tulip_softc *, int));
  108 void    tlp_idle __P((struct tulip_softc *, u_int32_t));
  109 void    tlp_srom_idle __P((struct tulip_softc *));
  110 int     tlp_srom_size __P((struct tulip_softc *));
  111 
  112 int     tlp_enable __P((struct tulip_softc *));
  113 void    tlp_disable __P((struct tulip_softc *));
  114 void    tlp_power __P((int, void *));
  115 
  116 void    tlp_filter_setup __P((struct tulip_softc *));
  117 void    tlp_winb_filter_setup __P((struct tulip_softc *));
  118 void    tlp_al981_filter_setup __P((struct tulip_softc *));
  119 
  120 void    tlp_rxintr __P((struct tulip_softc *));
  121 void    tlp_txintr __P((struct tulip_softc *));
  122 
  123 void    tlp_mii_tick __P((void *));
  124 void    tlp_mii_statchg __P((struct device *));
  125 void    tlp_winb_mii_statchg __P((struct device *));
  126 void    tlp_dm9102_mii_statchg __P((struct device *));
  127 
  128 void    tlp_mii_getmedia __P((struct tulip_softc *, struct ifmediareq *));
  129 int     tlp_mii_setmedia __P((struct tulip_softc *));
  130 
  131 int     tlp_bitbang_mii_readreg __P((struct device *, int, int));
  132 void    tlp_bitbang_mii_writereg __P((struct device *, int, int, int));
  133 
  134 int     tlp_pnic_mii_readreg __P((struct device *, int, int));
  135 void    tlp_pnic_mii_writereg __P((struct device *, int, int, int));
  136 
  137 int     tlp_al981_mii_readreg __P((struct device *, int, int));
  138 void    tlp_al981_mii_writereg __P((struct device *, int, int, int));
  139 
  140 void    tlp_2114x_preinit __P((struct tulip_softc *));
  141 void    tlp_2114x_mii_preinit __P((struct tulip_softc *));
  142 void    tlp_pnic_preinit __P((struct tulip_softc *));
  143 void    tlp_dm9102_preinit __P((struct tulip_softc *));
  144 
  145 void    tlp_21140_reset __P((struct tulip_softc *));
  146 void    tlp_21142_reset __P((struct tulip_softc *));
  147 void    tlp_pmac_reset __P((struct tulip_softc *));
  148 void    tlp_dm9102_reset __P((struct tulip_softc *));
  149 
  150 void    tlp_2114x_nway_tick __P((void *));
  151 
  152 #define tlp_mchash(addr, sz)                                            \
  153         (ether_crc32_le((addr), ETHER_ADDR_LEN) & ((sz) - 1))
  154 
  155 /*
  156  * MII bit-bang glue.
  157  */
  158 u_int32_t tlp_sio_mii_bitbang_read __P((struct device *));
  159 void    tlp_sio_mii_bitbang_write __P((struct device *, u_int32_t));
  160 
  161 const struct mii_bitbang_ops tlp_sio_mii_bitbang_ops = {
  162         tlp_sio_mii_bitbang_read,
  163         tlp_sio_mii_bitbang_write,
  164         {
  165                 MIIROM_MDO,             /* MII_BIT_MDO */
  166                 MIIROM_MDI,             /* MII_BIT_MDI */
  167                 MIIROM_MDC,             /* MII_BIT_MDC */
  168                 0,                      /* MII_BIT_DIR_HOST_PHY */
  169                 MIIROM_MIIDIR,          /* MII_BIT_DIR_PHY_HOST */
  170         }
  171 };
  172 
  173 #ifdef TLP_DEBUG
  174 #define DPRINTF(sc, x)  if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
  175                                 printf x
  176 #else
  177 #define DPRINTF(sc, x)  /* nothing */
  178 #endif
  179 
  180 #ifdef TLP_STATS
  181 void    tlp_print_stats __P((struct tulip_softc *));
  182 #endif
  183 
  184 /*
  185  * Can be used to debug the SROM-related things, including contents.
  186  * Initialized so that it's patchable.
  187  */
  188 int     tlp_srom_debug = 0;
  189 
  190 /*
  191  * tlp_attach:
  192  *
  193  *      Attach a Tulip interface to the system.
  194  */
  195 void
  196 tlp_attach(sc, enaddr)
  197         struct tulip_softc *sc;
  198         const u_int8_t *enaddr;
  199 {
  200         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
  201         int i, error;
  202 
  203         callout_init(&sc->sc_nway_callout);
  204         callout_init(&sc->sc_tick_callout);
  205 
  206         /*
  207          * NOTE: WE EXPECT THE FRONT-END TO INITIALIZE sc_regshift!
  208          */
  209 
  210         /*
  211          * Setup the transmit threshold table.
  212          */
  213         switch (sc->sc_chip) {
  214         case TULIP_CHIP_DE425:
  215         case TULIP_CHIP_21040:
  216         case TULIP_CHIP_21041:
  217                 sc->sc_txth = tlp_10_txthresh_tab;
  218                 break;
  219 
  220         case TULIP_CHIP_DM9102:
  221         case TULIP_CHIP_DM9102A:
  222                 sc->sc_txth = tlp_dm9102_txthresh_tab;
  223                 break;
  224 
  225         default:
  226                 sc->sc_txth = tlp_10_100_txthresh_tab;
  227                 break;
  228         }
  229 
  230         /*
  231          * Setup the filter setup function.
  232          */
  233         switch (sc->sc_chip) {
  234         case TULIP_CHIP_WB89C840F:
  235                 sc->sc_filter_setup = tlp_winb_filter_setup;
  236                 break;
  237 
  238         case TULIP_CHIP_AL981:
  239         case TULIP_CHIP_AN983:
  240         case TULIP_CHIP_AN985:
  241                 sc->sc_filter_setup = tlp_al981_filter_setup;
  242                 break;
  243 
  244         default:
  245                 sc->sc_filter_setup = tlp_filter_setup;
  246                 break;
  247         }
  248 
  249         /*
  250          * Set up the media status change function.
  251          */
  252         switch (sc->sc_chip) {
  253         case TULIP_CHIP_WB89C840F:
  254                 sc->sc_statchg = tlp_winb_mii_statchg;
  255                 break;
  256 
  257         case TULIP_CHIP_DM9102:
  258         case TULIP_CHIP_DM9102A:
  259                 sc->sc_statchg = tlp_dm9102_mii_statchg;
  260                 break;
  261 
  262         default:
  263                 /*
  264                  * We may override this if we have special media
  265                  * handling requirements (e.g. flipping GPIO pins).
  266                  *
  267                  * The pure-MII statchg function covers the basics.
  268                  */
  269                 sc->sc_statchg = tlp_mii_statchg;
  270                 break;
  271         }
  272 
  273         /*
  274          * Default to no FS|LS in setup packet descriptors.  They're
  275          * supposed to be zero according to the 21040 and 21143
  276          * manuals, and some chips fall over badly if they're
  277          * included.  Yet, other chips seem to require them.  Sigh.
  278          */
  279         switch (sc->sc_chip) {
  280         case TULIP_CHIP_X3201_3:
  281                 sc->sc_setup_fsls = TDCTL_Tx_FS|TDCTL_Tx_LS;
  282                 break;
  283 
  284         default:
  285                 sc->sc_setup_fsls = 0;
  286         }
  287 
  288         /*
  289          * Set up various chip-specific quirks.
  290          *
  291          * Note that wherever we can, we use the "ring" option for
  292          * transmit and receive descriptors.  This is because some
  293          * clone chips apparently have problems when using chaining,
  294          * although some *only* support chaining.
  295          *
  296          * What we do is always program the "next" pointer, and then
  297          * conditionally set the TDCTL_CH and TDCTL_ER bits in the
  298          * appropriate places.
  299          */
  300         switch (sc->sc_chip) {
  301         case TULIP_CHIP_21140:
  302         case TULIP_CHIP_21140A:
  303         case TULIP_CHIP_21142:
  304         case TULIP_CHIP_21143:
  305         case TULIP_CHIP_82C115:         /* 21143-like */
  306         case TULIP_CHIP_MX98713:        /* 21140-like */
  307         case TULIP_CHIP_MX98713A:       /* 21143-like */
  308         case TULIP_CHIP_MX98715:        /* 21143-like */
  309         case TULIP_CHIP_MX98715A:       /* 21143-like */
  310         case TULIP_CHIP_MX98715AEC_X:   /* 21143-like */
  311         case TULIP_CHIP_MX98725:        /* 21143-like */
  312                 /*
  313                  * Run these chips in ring mode.
  314                  */
  315                 sc->sc_tdctl_ch = 0;
  316                 sc->sc_tdctl_er = TDCTL_ER;
  317                 sc->sc_preinit = tlp_2114x_preinit;
  318                 break;
  319 
  320         case TULIP_CHIP_82C168:
  321         case TULIP_CHIP_82C169:
  322                 /*
  323                  * Run these chips in ring mode.
  324                  */
  325                 sc->sc_tdctl_ch = 0;
  326                 sc->sc_tdctl_er = TDCTL_ER;
  327                 sc->sc_preinit = tlp_pnic_preinit;
  328 
  329                 /*
  330                  * These chips seem to have busted DMA engines; just put them
  331                  * in Store-and-Forward mode from the get-go.
  332                  */
  333                 sc->sc_txthresh = TXTH_SF;
  334                 break;
  335 
  336         case TULIP_CHIP_WB89C840F:
  337                 /*
  338                  * Run this chip in chained mode.
  339                  */
  340                 sc->sc_tdctl_ch = TDCTL_CH;
  341                 sc->sc_tdctl_er = 0;
  342                 sc->sc_flags |= TULIPF_IC_FS;
  343                 break;
  344 
  345         case TULIP_CHIP_DM9102:
  346         case TULIP_CHIP_DM9102A:
  347                 /*
  348                  * Run these chips in chained mode.
  349                  */
  350                 sc->sc_tdctl_ch = TDCTL_CH;
  351                 sc->sc_tdctl_er = 0;
  352                 sc->sc_preinit = tlp_dm9102_preinit;
  353 
  354                 /*
  355                  * These chips have a broken bus interface, so we
  356                  * can't use any optimized bus commands.  For this
  357                  * reason, we tend to underrun pretty quickly, so
  358                  * just to Store-and-Forward mode from the get-go.
  359                  */
  360                 sc->sc_txthresh = TXTH_DM9102_SF;
  361                 break;
  362 
  363         default:
  364                 /*
  365                  * Default to running in ring mode.
  366                  */
  367                 sc->sc_tdctl_ch = 0;
  368                 sc->sc_tdctl_er = TDCTL_ER;
  369         }
  370 
  371         /*
  372          * Set up the MII bit-bang operations.
  373          */
  374         switch (sc->sc_chip) {
  375         case TULIP_CHIP_WB89C840F:      /* XXX direction bit different? */
  376                 sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
  377                 break;
  378 
  379         default:
  380                 sc->sc_bitbang_ops = &tlp_sio_mii_bitbang_ops;
  381         }
  382 
  383         SIMPLEQ_INIT(&sc->sc_txfreeq);
  384         SIMPLEQ_INIT(&sc->sc_txdirtyq);
  385 
  386         /*
  387          * Allocate the control data structures, and create and load the
  388          * DMA map for it.
  389          */
  390         if ((error = bus_dmamem_alloc(sc->sc_dmat,
  391             sizeof(struct tulip_control_data), PAGE_SIZE, 0, &sc->sc_cdseg,
  392             1, &sc->sc_cdnseg, 0)) != 0) {
  393                 printf("%s: unable to allocate control data, error = %d\n",
  394                     sc->sc_dev.dv_xname, error);
  395                 goto fail_0;
  396         }
  397 
  398         if ((error = bus_dmamem_map(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg,
  399             sizeof(struct tulip_control_data), (caddr_t *)&sc->sc_control_data,
  400             BUS_DMA_COHERENT)) != 0) {
  401                 printf("%s: unable to map control data, error = %d\n",
  402                     sc->sc_dev.dv_xname, error);
  403                 goto fail_1;
  404         }
  405 
  406         if ((error = bus_dmamap_create(sc->sc_dmat,
  407             sizeof(struct tulip_control_data), 1,
  408             sizeof(struct tulip_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
  409                 printf("%s: unable to create control data DMA map, "
  410                     "error = %d\n", sc->sc_dev.dv_xname, error);
  411                 goto fail_2;
  412         }
  413 
  414         if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cddmamap,
  415             sc->sc_control_data, sizeof(struct tulip_control_data), NULL,
  416             0)) != 0) {
  417                 printf("%s: unable to load control data DMA map, error = %d\n",
  418                     sc->sc_dev.dv_xname, error);
  419                 goto fail_3;
  420         }
  421 
  422         /*
  423          * Create the transmit buffer DMA maps.
  424          *
  425          * Note that on the Xircom clone, transmit buffers must be
  426          * 4-byte aligned.  We're almost guaranteed to have to copy
  427          * the packet in that case, so we just limit ourselves to
  428          * one segment.
  429          *
  430          * On the DM9102, the transmit logic can only handle one
  431          * DMA segment.
  432          */
  433         switch (sc->sc_chip) {
  434         case TULIP_CHIP_X3201_3:
  435         case TULIP_CHIP_DM9102:
  436         case TULIP_CHIP_DM9102A:
  437                 sc->sc_ntxsegs = 1;
  438                 break;
  439 
  440         default:
  441                 sc->sc_ntxsegs = TULIP_NTXSEGS;
  442         }
  443         for (i = 0; i < TULIP_TXQUEUELEN; i++) {
  444                 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES,
  445                     sc->sc_ntxsegs, MCLBYTES, 0, 0,
  446                     &sc->sc_txsoft[i].txs_dmamap)) != 0) {
  447                         printf("%s: unable to create tx DMA map %d, "
  448                             "error = %d\n", sc->sc_dev.dv_xname, i, error);
  449                         goto fail_4;
  450                 }
  451         }
  452 
  453         /*
  454          * Create the receive buffer DMA maps.
  455          */
  456         for (i = 0; i < TULIP_NRXDESC; i++) {
  457                 if ((error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1,
  458                     MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
  459                         printf("%s: unable to create rx DMA map %d, "
  460                             "error = %d\n", sc->sc_dev.dv_xname, i, error);
  461                         goto fail_5;
  462                 }
  463                 sc->sc_rxsoft[i].rxs_mbuf = NULL;
  464         }
  465 
  466         /*
  467          * From this point forward, the attachment cannot fail.  A failure
  468          * before this point releases all resources that may have been
  469          * allocated.
  470          */
  471         sc->sc_flags |= TULIPF_ATTACHED;
  472 
  473         /*
  474          * Reset the chip to a known state.
  475          */
  476         tlp_reset(sc);
  477 
  478         /* Announce ourselves. */
  479         printf("%s: %s%sEthernet address %s\n", sc->sc_dev.dv_xname,
  480             sc->sc_name[0] != '\0' ? sc->sc_name : "",
  481             sc->sc_name[0] != '\0' ? ", " : "",
  482             ether_sprintf(enaddr));
  483 
  484         /*
  485          * Check to see if we're the simulated Ethernet on Connectix
  486          * Virtual PC.
  487          */
  488         if (enaddr[0] == 0x00 && enaddr[1] == 0x03 && enaddr[2] == 0xff)
  489                 sc->sc_flags |= TULIPF_VPC;
  490 
  491         /*
  492          * Initialize our media structures.  This may probe the MII, if
  493          * present.
  494          */
  495         (*sc->sc_mediasw->tmsw_init)(sc);
  496 
  497         strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
  498         ifp->if_softc = sc;
  499         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
  500         ifp->if_ioctl = tlp_ioctl;
  501         ifp->if_start = tlp_start;
  502         ifp->if_watchdog = tlp_watchdog;
  503         ifp->if_init = tlp_init;
  504         ifp->if_stop = tlp_stop;
  505         IFQ_SET_READY(&ifp->if_snd);
  506 
  507         /*
  508          * We can support 802.1Q VLAN-sized frames.
  509          */
  510         sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
  511 
  512         /*
  513          * Attach the interface.
  514          */
  515         if_attach(ifp);
  516         ether_ifattach(ifp, enaddr);
  517 #if NRND > 0
  518         rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname,
  519             RND_TYPE_NET, 0);
  520 #endif
  521 
  522         /*
  523          * Make sure the interface is shutdown during reboot.
  524          */
  525         sc->sc_sdhook = shutdownhook_establish(tlp_shutdown, sc);
  526         if (sc->sc_sdhook == NULL)
  527                 printf("%s: WARNING: unable to establish shutdown hook\n",
  528                     sc->sc_dev.dv_xname);
  529 
  530         /*
  531          * Add a suspend hook to make sure we come back up after a
  532          * resume.
  533          */
  534         sc->sc_powerhook = powerhook_establish(tlp_power, sc);
  535         if (sc->sc_powerhook == NULL)
  536                 printf("%s: WARNING: unable to establish power hook\n",
  537                     sc->sc_dev.dv_xname);
  538         return;
  539 
  540         /*
  541          * Free any resources we've allocated during the failed attach
  542          * attempt.  Do this in reverse order and fall through.
  543          */
  544  fail_5:
  545         for (i = 0; i < TULIP_NRXDESC; i++) {
  546                 if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
  547                         bus_dmamap_destroy(sc->sc_dmat,
  548                             sc->sc_rxsoft[i].rxs_dmamap);
  549         }
  550  fail_4:
  551         for (i = 0; i < TULIP_TXQUEUELEN; i++) {
  552                 if (sc->sc_txsoft[i].txs_dmamap != NULL)
  553                         bus_dmamap_destroy(sc->sc_dmat,
  554                             sc->sc_txsoft[i].txs_dmamap);
  555         }
  556         bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
  557  fail_3:
  558         bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
  559  fail_2:
  560         bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
  561             sizeof(struct tulip_control_data));
  562  fail_1:
  563         bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
  564  fail_0:
  565         return;
  566 }
  567 
  568 /*
  569  * tlp_activate:
  570  *
  571  *      Handle device activation/deactivation requests.
  572  */
  573 int
  574 tlp_activate(self, act)
  575         struct device *self;
  576         enum devact act;
  577 {
  578         struct tulip_softc *sc = (void *) self;
  579         int s, error = 0;
  580 
  581         s = splnet();
  582         switch (act) {
  583         case DVACT_ACTIVATE:
  584                 error = EOPNOTSUPP;
  585                 break;
  586 
  587         case DVACT_DEACTIVATE:
  588                 if (sc->sc_flags & TULIPF_HAS_MII)
  589                         mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
  590                             MII_OFFSET_ANY);
  591                 if_deactivate(&sc->sc_ethercom.ec_if);
  592                 break;
  593         }
  594         splx(s);
  595 
  596         return (error);
  597 }
  598 
  599 /*
  600  * tlp_detach:
  601  *
  602  *      Detach a Tulip interface.
  603  */
  604 int
  605 tlp_detach(sc)
  606         struct tulip_softc *sc;
  607 {
  608         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
  609         struct tulip_rxsoft *rxs;
  610         struct tulip_txsoft *txs;
  611         int i;
  612 
  613         /*
  614          * Succeed now if there isn't any work to do.
  615          */
  616         if ((sc->sc_flags & TULIPF_ATTACHED) == 0)
  617                 return (0);
  618 
  619         /* Unhook our tick handler. */
  620         if (sc->sc_tick)
  621                 callout_stop(&sc->sc_tick_callout);
  622 
  623         if (sc->sc_flags & TULIPF_HAS_MII) {
  624                 /* Detach all PHYs */
  625                 mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
  626         }
  627 
  628         /* Delete all remaining media. */
  629         ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
  630 
  631 #if NRND > 0
  632         rnd_detach_source(&sc->sc_rnd_source);
  633 #endif
  634         ether_ifdetach(ifp);
  635         if_detach(ifp);
  636 
  637         for (i = 0; i < TULIP_NRXDESC; i++) {
  638                 rxs = &sc->sc_rxsoft[i];
  639                 if (rxs->rxs_mbuf != NULL) {
  640                         bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
  641                         m_freem(rxs->rxs_mbuf);
  642                         rxs->rxs_mbuf = NULL;
  643                 }
  644                 bus_dmamap_destroy(sc->sc_dmat, rxs->rxs_dmamap);
  645         }
  646         for (i = 0; i < TULIP_TXQUEUELEN; i++) {
  647                 txs = &sc->sc_txsoft[i];
  648                 if (txs->txs_mbuf != NULL) {
  649                         bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
  650                         m_freem(txs->txs_mbuf);
  651                         txs->txs_mbuf = NULL;
  652                 }
  653                 bus_dmamap_destroy(sc->sc_dmat, txs->txs_dmamap);
  654         }
  655         bus_dmamap_unload(sc->sc_dmat, sc->sc_cddmamap);
  656         bus_dmamap_destroy(sc->sc_dmat, sc->sc_cddmamap);
  657         bus_dmamem_unmap(sc->sc_dmat, (caddr_t)sc->sc_control_data,
  658             sizeof(struct tulip_control_data));
  659         bus_dmamem_free(sc->sc_dmat, &sc->sc_cdseg, sc->sc_cdnseg);
  660 
  661         shutdownhook_disestablish(sc->sc_sdhook);
  662         powerhook_disestablish(sc->sc_powerhook);
  663 
  664         if (sc->sc_srom)
  665                 free(sc->sc_srom, M_DEVBUF);
  666 
  667         return (0);
  668 }
  669 
  670 /*
  671  * tlp_shutdown:
  672  *
  673  *      Make sure the interface is stopped at reboot time.
  674  */
  675 void
  676 tlp_shutdown(arg)
  677         void *arg;
  678 {
  679         struct tulip_softc *sc = arg;
  680 
  681         tlp_stop(&sc->sc_ethercom.ec_if, 1);
  682 }
  683 
  684 /*
  685  * tlp_start:           [ifnet interface function]
  686  *
  687  *      Start packet transmission on the interface.
  688  */
  689 void
  690 tlp_start(ifp)
  691         struct ifnet *ifp;
  692 {
  693         struct tulip_softc *sc = ifp->if_softc;
  694         struct mbuf *m0, *m;
  695         struct tulip_txsoft *txs, *last_txs = NULL;
  696         bus_dmamap_t dmamap;
  697         int error, firsttx, nexttx, lasttx = 1, ofree, seg;
  698 
  699         DPRINTF(sc, ("%s: tlp_start: sc_flags 0x%08x, if_flags 0x%08x\n",
  700             sc->sc_dev.dv_xname, sc->sc_flags, ifp->if_flags));
  701 
  702         /*
  703          * If we want a filter setup, it means no more descriptors were
  704          * available for the setup routine.  Let it get a chance to wedge
  705          * itself into the ring.
  706          */
  707         if (sc->sc_flags & TULIPF_WANT_SETUP)
  708                 ifp->if_flags |= IFF_OACTIVE;
  709 
  710         if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
  711                 return;
  712 
  713         if (sc->sc_tick == tlp_2114x_nway_tick &&
  714             (sc->sc_flags & TULIPF_LINK_UP) == 0 && ifp->if_snd.ifq_len < 10)
  715                 return;
  716 
  717         /*
  718          * Remember the previous number of free descriptors and
  719          * the first descriptor we'll use.
  720          */
  721         ofree = sc->sc_txfree;
  722         firsttx = sc->sc_txnext;
  723 
  724         DPRINTF(sc, ("%s: tlp_start: txfree %d, txnext %d\n",
  725             sc->sc_dev.dv_xname, ofree, firsttx));
  726 
  727         /*
  728          * Loop through the send queue, setting up transmit descriptors
  729          * until we drain the queue, or use up all available transmit
  730          * descriptors.
  731          */
  732         while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
  733                sc->sc_txfree != 0) {
  734                 /*
  735                  * Grab a packet off the queue.
  736                  */
  737                 IFQ_POLL(&ifp->if_snd, m0);
  738                 if (m0 == NULL)
  739                         break;
  740                 m = NULL;
  741 
  742                 dmamap = txs->txs_dmamap;
  743 
  744                 /*
  745                  * Load the DMA map.  If this fails, the packet either
  746                  * didn't fit in the alloted number of segments, or we were
  747                  * short on resources.  In this case, we'll copy and try
  748                  * again.
  749                  *
  750                  * Note that if we're only allowed 1 Tx segment, we
  751                  * have an alignment restriction.  Do this test before
  752                  * attempting to load the DMA map, because it's more
  753                  * likely we'll trip the alignment test than the
  754                  * more-than-one-segment test.
  755                  */
  756                 if ((sc->sc_ntxsegs == 1 && (mtod(m0, uintptr_t) & 3) != 0) ||
  757                     bus_dmamap_load_mbuf(sc->sc_dmat, dmamap, m0,
  758                       BUS_DMA_WRITE|BUS_DMA_NOWAIT) != 0) {
  759                         MGETHDR(m, M_DONTWAIT, MT_DATA);
  760                         if (m == NULL) {
  761                                 printf("%s: unable to allocate Tx mbuf\n",
  762                                     sc->sc_dev.dv_xname);
  763                                 break;
  764                         }
  765                         MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
  766                         if (m0->m_pkthdr.len > MHLEN) {
  767                                 MCLGET(m, M_DONTWAIT);
  768                                 if ((m->m_flags & M_EXT) == 0) {
  769                                         printf("%s: unable to allocate Tx "
  770                                             "cluster\n", sc->sc_dev.dv_xname);
  771                                         m_freem(m);
  772                                         break;
  773                                 }
  774                         }
  775                         m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, caddr_t));
  776                         m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
  777                         error = bus_dmamap_load_mbuf(sc->sc_dmat, dmamap,
  778                             m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
  779                         if (error) {
  780                                 printf("%s: unable to load Tx buffer, "
  781                                     "error = %d\n", sc->sc_dev.dv_xname, error);
  782                                 break;
  783                         }
  784                 }
  785 
  786                 /*
  787                  * Ensure we have enough descriptors free to describe
  788                  * the packet.
  789                  */
  790                 if (dmamap->dm_nsegs > sc->sc_txfree) {
  791                         /*
  792                          * Not enough free descriptors to transmit this
  793                          * packet.  We haven't committed to anything yet,
  794                          * so just unload the DMA map, put the packet
  795                          * back on the queue, and punt.  Notify the upper
  796                          * layer that there are no more slots left.
  797                          *
  798                          * XXX We could allocate an mbuf and copy, but
  799                          * XXX it is worth it?
  800                          */
  801                         ifp->if_flags |= IFF_OACTIVE;
  802                         bus_dmamap_unload(sc->sc_dmat, dmamap);
  803                         if (m != NULL)
  804                                 m_freem(m);
  805                         break;
  806                 }
  807 
  808                 IFQ_DEQUEUE(&ifp->if_snd, m0);
  809                 if (m != NULL) {
  810                         m_freem(m0);
  811                         m0 = m;
  812                 }
  813 
  814                 /*
  815                  * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
  816                  */
  817 
  818                 /* Sync the DMA map. */
  819                 bus_dmamap_sync(sc->sc_dmat, dmamap, 0, dmamap->dm_mapsize,
  820                     BUS_DMASYNC_PREWRITE);
  821 
  822                 /*
  823                  * Initialize the transmit descriptors.
  824                  */
  825                 for (nexttx = sc->sc_txnext, seg = 0;
  826                      seg < dmamap->dm_nsegs;
  827                      seg++, nexttx = TULIP_NEXTTX(nexttx)) {
  828                         /*
  829                          * If this is the first descriptor we're
  830                          * enqueueing, don't set the OWN bit just
  831                          * yet.  That could cause a race condition.
  832                          * We'll do it below.
  833                          */
  834                         sc->sc_txdescs[nexttx].td_status =
  835                             (nexttx == firsttx) ? 0 : htole32(TDSTAT_OWN);
  836                         sc->sc_txdescs[nexttx].td_bufaddr1 =
  837                             htole32(dmamap->dm_segs[seg].ds_addr);
  838                         sc->sc_txdescs[nexttx].td_ctl =
  839                             htole32((dmamap->dm_segs[seg].ds_len <<
  840                                 TDCTL_SIZE1_SHIFT) | sc->sc_tdctl_ch |
  841                                 (nexttx == (TULIP_NTXDESC - 1) ?
  842                                  sc->sc_tdctl_er : 0));
  843                         lasttx = nexttx;
  844                 }
  845 
  846                 KASSERT(lasttx != -1);
  847 
  848                 /* Set `first segment' and `last segment' appropriately. */
  849                 sc->sc_txdescs[sc->sc_txnext].td_ctl |= htole32(TDCTL_Tx_FS);
  850                 sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_LS);
  851 
  852 #ifdef TLP_DEBUG
  853                 if (ifp->if_flags & IFF_DEBUG) {
  854                         printf("     txsoft %p transmit chain:\n", txs);
  855                         for (seg = sc->sc_txnext;; seg = TULIP_NEXTTX(seg)) {
  856                                 printf("     descriptor %d:\n", seg);
  857                                 printf("       td_status:   0x%08x\n",
  858                                     le32toh(sc->sc_txdescs[seg].td_status));
  859                                 printf("       td_ctl:      0x%08x\n",
  860                                     le32toh(sc->sc_txdescs[seg].td_ctl));
  861                                 printf("       td_bufaddr1: 0x%08x\n",
  862                                     le32toh(sc->sc_txdescs[seg].td_bufaddr1));
  863                                 printf("       td_bufaddr2: 0x%08x\n",
  864                                     le32toh(sc->sc_txdescs[seg].td_bufaddr2));
  865                                 if (seg == lasttx)
  866                                         break;
  867                         }
  868                 }
  869 #endif
  870 
  871                 /* Sync the descriptors we're using. */
  872                 TULIP_CDTXSYNC(sc, sc->sc_txnext, dmamap->dm_nsegs,
  873                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  874 
  875                 /*
  876                  * Store a pointer to the packet so we can free it later,
  877                  * and remember what txdirty will be once the packet is
  878                  * done.
  879                  */
  880                 txs->txs_mbuf = m0;
  881                 txs->txs_firstdesc = sc->sc_txnext;
  882                 txs->txs_lastdesc = lasttx;
  883                 txs->txs_ndescs = dmamap->dm_nsegs;
  884 
  885                 /* Advance the tx pointer. */
  886                 sc->sc_txfree -= dmamap->dm_nsegs;
  887                 sc->sc_txnext = nexttx;
  888 
  889                 SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
  890                 SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
  891 
  892                 last_txs = txs;
  893 
  894 #if NBPFILTER > 0
  895                 /*
  896                  * Pass the packet to any BPF listeners.
  897                  */
  898                 if (ifp->if_bpf)
  899                         bpf_mtap(ifp->if_bpf, m0);
  900 #endif /* NBPFILTER > 0 */
  901         }
  902 
  903         if (txs == NULL || sc->sc_txfree == 0) {
  904                 /* No more slots left; notify upper layer. */
  905                 ifp->if_flags |= IFF_OACTIVE;
  906         }
  907 
  908         if (sc->sc_txfree != ofree) {
  909                 DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
  910                     sc->sc_dev.dv_xname, lasttx, firsttx));
  911                 /*
  912                  * Cause a transmit interrupt to happen on the
  913                  * last packet we enqueued.
  914                  */
  915                 sc->sc_txdescs[lasttx].td_ctl |= htole32(TDCTL_Tx_IC);
  916                 TULIP_CDTXSYNC(sc, lasttx, 1,
  917                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  918 
  919                 /*
  920                  * Some clone chips want IC on the *first* segment in
  921                  * the packet.  Appease them.
  922                  */
  923                 KASSERT(last_txs != NULL);
  924                 if ((sc->sc_flags & TULIPF_IC_FS) != 0 &&
  925                     last_txs->txs_firstdesc != lasttx) {
  926                         sc->sc_txdescs[last_txs->txs_firstdesc].td_ctl |=
  927                             htole32(TDCTL_Tx_IC);
  928                         TULIP_CDTXSYNC(sc, last_txs->txs_firstdesc, 1,
  929                             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  930                 }
  931 
  932                 /*
  933                  * The entire packet chain is set up.  Give the
  934                  * first descriptor to the chip now.
  935                  */
  936                 sc->sc_txdescs[firsttx].td_status |= htole32(TDSTAT_OWN);
  937                 TULIP_CDTXSYNC(sc, firsttx, 1,
  938                     BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  939 
  940                 /* Wake up the transmitter. */
  941                 /* XXX USE AUTOPOLLING? */
  942                 TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
  943 
  944                 /* Set a watchdog timer in case the chip flakes out. */
  945                 ifp->if_timer = 5;
  946         }
  947 }
  948 
  949 /*
  950  * tlp_watchdog:        [ifnet interface function]
  951  *
  952  *      Watchdog timer handler.
  953  */
  954 void
  955 tlp_watchdog(ifp)
  956         struct ifnet *ifp;
  957 {
  958         struct tulip_softc *sc = ifp->if_softc;
  959         int doing_setup, doing_transmit;
  960 
  961         doing_setup = (sc->sc_flags & TULIPF_DOING_SETUP);
  962         doing_transmit = (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq));
  963 
  964         if (doing_setup && doing_transmit) {
  965                 printf("%s: filter setup and transmit timeout\n",
  966                     sc->sc_dev.dv_xname);
  967                 ifp->if_oerrors++;
  968         } else if (doing_transmit) {
  969                 printf("%s: transmit timeout\n", sc->sc_dev.dv_xname);
  970                 ifp->if_oerrors++;
  971         } else if (doing_setup)
  972                 printf("%s: filter setup timeout\n", sc->sc_dev.dv_xname);
  973         else
  974                 printf("%s: spurious watchdog timeout\n", sc->sc_dev.dv_xname);
  975 
  976         (void) tlp_init(ifp);
  977 
  978         /* Try to get more packets going. */
  979         tlp_start(ifp);
  980 }
  981 
  982 /*
  983  * tlp_ioctl:           [ifnet interface function]
  984  *
  985  *      Handle control requests from the operator.
  986  */
  987 int
  988 tlp_ioctl(ifp, cmd, data)
  989         struct ifnet *ifp;
  990         u_long cmd;
  991         caddr_t data;
  992 {
  993         struct tulip_softc *sc = ifp->if_softc;
  994         struct ifreq *ifr = (struct ifreq *)data;
  995         int s, error;
  996 
  997         s = splnet();
  998 
  999         switch (cmd) {
 1000         case SIOCSIFMEDIA:
 1001         case SIOCGIFMEDIA:
 1002                 error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
 1003                 break;
 1004 
 1005         default:
 1006                 error = ether_ioctl(ifp, cmd, data);
 1007                 if (error == ENETRESET) {
 1008                         if (TULIP_IS_ENABLED(sc)) {
 1009                                 /*
 1010                                  * Multicast list has changed.  Set the
 1011                                  * hardware filter accordingly.
 1012                                  */
 1013                                 (*sc->sc_filter_setup)(sc);
 1014                         }
 1015                         error = 0;
 1016                 }
 1017                 break;
 1018         }
 1019 
 1020         /* Try to get more packets going. */
 1021         if (TULIP_IS_ENABLED(sc))
 1022                 tlp_start(ifp);
 1023 
 1024         splx(s);
 1025         return (error);
 1026 }
 1027 
 1028 /*
 1029  * tlp_intr:
 1030  *
 1031  *      Interrupt service routine.
 1032  */
 1033 int
 1034 tlp_intr(arg)
 1035         void *arg;
 1036 {
 1037         struct tulip_softc *sc = arg;
 1038         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1039         u_int32_t status, rxstatus, txstatus;
 1040         int handled = 0, txthresh;
 1041 
 1042         DPRINTF(sc, ("%s: tlp_intr\n", sc->sc_dev.dv_xname));
 1043 
 1044 #ifdef DEBUG
 1045         if (TULIP_IS_ENABLED(sc) == 0)
 1046                 panic("%s: tlp_intr: not enabled", sc->sc_dev.dv_xname);
 1047 #endif
 1048 
 1049         /*
 1050          * If the interface isn't running, the interrupt couldn't
 1051          * possibly have come from us.
 1052          */
 1053         if ((ifp->if_flags & IFF_RUNNING) == 0 ||
 1054             (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 1055                 return (0);
 1056 
 1057         /* Disable interrupts on the DM9102 (interrupt edge bug). */
 1058         switch (sc->sc_chip) {
 1059         case TULIP_CHIP_DM9102:
 1060         case TULIP_CHIP_DM9102A:
 1061                 TULIP_WRITE(sc, CSR_INTEN, 0);
 1062                 break;
 1063 
 1064         default:
 1065                 /* Nothing. */
 1066                 break;
 1067         }
 1068 
 1069         for (;;) {
 1070                 status = TULIP_READ(sc, CSR_STATUS);
 1071                 if (status)
 1072                         TULIP_WRITE(sc, CSR_STATUS, status);
 1073 
 1074                 if ((status & sc->sc_inten) == 0)
 1075                         break;
 1076 
 1077                 handled = 1;
 1078 
 1079                 rxstatus = status & sc->sc_rxint_mask;
 1080                 txstatus = status & sc->sc_txint_mask;
 1081 
 1082                 if (rxstatus) {
 1083                         /* Grab new any new packets. */
 1084                         tlp_rxintr(sc);
 1085 
 1086                         if (rxstatus & STATUS_RWT)
 1087                                 printf("%s: receive watchdog timeout\n",
 1088                                     sc->sc_dev.dv_xname);
 1089 
 1090                         if (rxstatus & STATUS_RU) {
 1091                                 printf("%s: receive ring overrun\n",
 1092                                     sc->sc_dev.dv_xname);
 1093                                 /* Get the receive process going again. */
 1094                                 if (sc->sc_tdctl_er != TDCTL_ER) {
 1095                                         tlp_idle(sc, OPMODE_SR);
 1096                                         TULIP_WRITE(sc, CSR_RXLIST,
 1097                                             TULIP_CDRXADDR(sc, sc->sc_rxptr));
 1098                                         TULIP_WRITE(sc, CSR_OPMODE,
 1099                                             sc->sc_opmode);
 1100                                 }
 1101                                 TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
 1102                                 break;
 1103                         }
 1104                 }
 1105 
 1106                 if (txstatus) {
 1107                         /* Sweep up transmit descriptors. */
 1108                         tlp_txintr(sc);
 1109 
 1110                         if (txstatus & STATUS_TJT)
 1111                                 printf("%s: transmit jabber timeout\n",
 1112                                     sc->sc_dev.dv_xname);
 1113 
 1114                         if (txstatus & STATUS_UNF) {
 1115                                 /*
 1116                                  * Increase our transmit threshold if
 1117                                  * another is available.
 1118                                  */
 1119                                 txthresh = sc->sc_txthresh + 1;
 1120                                 if (sc->sc_txth[txthresh].txth_name != NULL) {
 1121                                         /* Idle the transmit process. */
 1122                                         tlp_idle(sc, OPMODE_ST);
 1123 
 1124                                         sc->sc_txthresh = txthresh;
 1125                                         sc->sc_opmode &= ~(OPMODE_TR|OPMODE_SF);
 1126                                         sc->sc_opmode |=
 1127                                             sc->sc_txth[txthresh].txth_opmode;
 1128                                         printf("%s: transmit underrun; new "
 1129                                             "threshold: %s\n",
 1130                                             sc->sc_dev.dv_xname,
 1131                                             sc->sc_txth[txthresh].txth_name);
 1132 
 1133                                         /*
 1134                                          * Set the new threshold and restart
 1135                                          * the transmit process.
 1136                                          */
 1137                                         TULIP_WRITE(sc, CSR_OPMODE,
 1138                                             sc->sc_opmode);
 1139                                 }
 1140                                         /*
 1141                                          * XXX Log every Nth underrun from
 1142                                          * XXX now on?
 1143                                          */
 1144                         }
 1145                 }
 1146 
 1147                 if (status & (STATUS_TPS|STATUS_RPS)) {
 1148                         if (status & STATUS_TPS)
 1149                                 printf("%s: transmit process stopped\n",
 1150                                     sc->sc_dev.dv_xname);
 1151                         if (status & STATUS_RPS)
 1152                                 printf("%s: receive process stopped\n",
 1153                                     sc->sc_dev.dv_xname);
 1154                         (void) tlp_init(ifp);
 1155                         break;
 1156                 }
 1157 
 1158                 if (status & STATUS_SE) {
 1159                         const char *str;
 1160                         switch (status & STATUS_EB) {
 1161                         case STATUS_EB_PARITY:
 1162                                 str = "parity error";
 1163                                 break;
 1164 
 1165                         case STATUS_EB_MABT:
 1166                                 str = "master abort";
 1167                                 break;
 1168 
 1169                         case STATUS_EB_TABT:
 1170                                 str = "target abort";
 1171                                 break;
 1172 
 1173                         default:
 1174                                 str = "unknown error";
 1175                                 break;
 1176                         }
 1177                         printf("%s: fatal system error: %s\n",
 1178                             sc->sc_dev.dv_xname, str);
 1179                         (void) tlp_init(ifp);
 1180                         break;
 1181                 }
 1182 
 1183                 /*
 1184                  * Not handled:
 1185                  *
 1186                  *      Transmit buffer unavailable -- normal
 1187                  *      condition, nothing to do, really.
 1188                  *
 1189                  *      General purpose timer experied -- we don't
 1190                  *      use the general purpose timer.
 1191                  *
 1192                  *      Early receive interrupt -- not available on
 1193                  *      all chips, we just use RI.  We also only
 1194                  *      use single-segment receive DMA, so this
 1195                  *      is mostly useless.
 1196                  */
 1197         }
 1198 
 1199         /* Bring interrupts back up on the DM9102. */
 1200         switch (sc->sc_chip) {
 1201         case TULIP_CHIP_DM9102:
 1202         case TULIP_CHIP_DM9102A:
 1203                 TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
 1204                 break;
 1205 
 1206         default:
 1207                 /* Nothing. */
 1208                 break;
 1209         }
 1210 
 1211         /* Try to get more packets going. */
 1212         tlp_start(ifp);
 1213 
 1214 #if NRND > 0
 1215         if (handled)
 1216                 rnd_add_uint32(&sc->sc_rnd_source, status);
 1217 #endif
 1218         return (handled);
 1219 }
 1220 
 1221 /*
 1222  * tlp_rxintr:
 1223  *
 1224  *      Helper; handle receive interrupts.
 1225  */
 1226 void
 1227 tlp_rxintr(sc)
 1228         struct tulip_softc *sc;
 1229 {
 1230         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1231         struct ether_header *eh;
 1232         struct tulip_rxsoft *rxs;
 1233         struct mbuf *m;
 1234         u_int32_t rxstat;
 1235         int i, len;
 1236 
 1237         for (i = sc->sc_rxptr;; i = TULIP_NEXTRX(i)) {
 1238                 rxs = &sc->sc_rxsoft[i];
 1239 
 1240                 TULIP_CDRXSYNC(sc, i,
 1241                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1242 
 1243                 rxstat = le32toh(sc->sc_rxdescs[i].td_status);
 1244 
 1245                 if (rxstat & TDSTAT_OWN) {
 1246                         /*
 1247                          * We have processed all of the receive buffers.
 1248                          */
 1249                         break;
 1250                 }
 1251 
 1252                 /*
 1253                  * Make sure the packet fit in one buffer.  This should
 1254                  * always be the case.  But the Lite-On PNIC, rev 33
 1255                  * has an awful receive engine bug, which may require
 1256                  * a very icky work-around.
 1257                  */
 1258                 if ((rxstat & (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) !=
 1259                     (TDSTAT_Rx_FS|TDSTAT_Rx_LS)) {
 1260                         printf("%s: incoming packet spilled, resetting\n",
 1261                             sc->sc_dev.dv_xname);
 1262                         (void) tlp_init(ifp);
 1263                         return;
 1264                 }
 1265 
 1266                 /*
 1267                  * If any collisions were seen on the wire, count one.
 1268                  */
 1269                 if (rxstat & TDSTAT_Rx_CS)
 1270                         ifp->if_collisions++;
 1271 
 1272                 /*
 1273                  * If an error occurred, update stats, clear the status
 1274                  * word, and leave the packet buffer in place.  It will
 1275                  * simply be reused the next time the ring comes around.
 1276                  * If 802.1Q VLAN MTU is enabled, ignore the Frame Too Long
 1277                  * error.
 1278                  */
 1279                 if (rxstat & TDSTAT_ES &&
 1280                     ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) == 0 ||
 1281                      (rxstat & (TDSTAT_Rx_DE | TDSTAT_Rx_RF |
 1282                                 TDSTAT_Rx_DB | TDSTAT_Rx_CE)) != 0)) {
 1283 #define PRINTERR(bit, str)                                              \
 1284                         if (rxstat & (bit))                             \
 1285                                 printf("%s: receive error: %s\n",       \
 1286                                     sc->sc_dev.dv_xname, str)
 1287                         ifp->if_ierrors++;
 1288                         PRINTERR(TDSTAT_Rx_DE, "descriptor error");
 1289                         PRINTERR(TDSTAT_Rx_RF, "runt frame");
 1290                         PRINTERR(TDSTAT_Rx_TL, "frame too long");
 1291                         PRINTERR(TDSTAT_Rx_RE, "MII error");
 1292                         PRINTERR(TDSTAT_Rx_DB, "dribbling bit");
 1293                         PRINTERR(TDSTAT_Rx_CE, "CRC error");
 1294 #undef PRINTERR
 1295                         TULIP_INIT_RXDESC(sc, i);
 1296                         continue;
 1297                 }
 1298 
 1299                 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
 1300                     rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
 1301 
 1302                 /*
 1303                  * No errors; receive the packet.  Note the Tulip
 1304                  * includes the CRC with every packet.
 1305                  */
 1306                 len = TDSTAT_Rx_LENGTH(rxstat);
 1307 
 1308 #ifdef __NO_STRICT_ALIGNMENT
 1309                 /*
 1310                  * Allocate a new mbuf cluster.  If that fails, we are
 1311                  * out of memory, and must drop the packet and recycle
 1312                  * the buffer that's already attached to this descriptor.
 1313                  */
 1314                 m = rxs->rxs_mbuf;
 1315                 if (tlp_add_rxbuf(sc, i) != 0) {
 1316                         ifp->if_ierrors++;
 1317                         TULIP_INIT_RXDESC(sc, i);
 1318                         bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
 1319                             rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
 1320                         continue;
 1321                 }
 1322 #else
 1323                 /*
 1324                  * The Tulip's receive buffers must be 4-byte aligned.
 1325                  * But this means that the data after the Ethernet header
 1326                  * is misaligned.  We must allocate a new buffer and
 1327                  * copy the data, shifted forward 2 bytes.
 1328                  */
 1329                 MGETHDR(m, M_DONTWAIT, MT_DATA);
 1330                 if (m == NULL) {
 1331  dropit:
 1332                         ifp->if_ierrors++;
 1333                         TULIP_INIT_RXDESC(sc, i);
 1334                         bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
 1335                             rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
 1336                         continue;
 1337                 }
 1338                 MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
 1339                 if (len > (MHLEN - 2)) {
 1340                         MCLGET(m, M_DONTWAIT);
 1341                         if ((m->m_flags & M_EXT) == 0) {
 1342                                 m_freem(m);
 1343                                 goto dropit;
 1344                         }
 1345                 }
 1346                 m->m_data += 2;
 1347 
 1348                 /*
 1349                  * Note that we use clusters for incoming frames, so the
 1350                  * buffer is virtually contiguous.
 1351                  */
 1352                 memcpy(mtod(m, caddr_t), mtod(rxs->rxs_mbuf, caddr_t), len);
 1353 
 1354                 /* Allow the receive descriptor to continue using its mbuf. */
 1355                 TULIP_INIT_RXDESC(sc, i);
 1356                 bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
 1357                     rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
 1358 #endif /* __NO_STRICT_ALIGNMENT */
 1359 
 1360                 ifp->if_ipackets++;
 1361                 eh = mtod(m, struct ether_header *);
 1362                 m->m_flags |= M_HASFCS;
 1363                 m->m_pkthdr.rcvif = ifp;
 1364                 m->m_pkthdr.len = m->m_len = len;
 1365 
 1366                 /*
 1367                  * XXX Work-around for a weird problem with the emulated
 1368                  * 21041 on Connectix Virtual PC:
 1369                  *
 1370                  * When we receive a full-size TCP segment, we seem to get
 1371                  * a packet there the Rx status says 1522 bytes, yet we do
 1372                  * not get a frame-too-long error from the chip.  The extra
 1373                  * bytes seem to always be zeros.  Perhaps Virtual PC is
 1374                  * inserting 4 bytes of zeros after every packet.  In any
 1375                  * case, let's try and detect this condition and truncate
 1376                  * the length so that it will pass up the stack.
 1377                  */
 1378                 if (__predict_false((sc->sc_flags & TULIPF_VPC) != 0)) {
 1379                         uint16_t etype = ntohs(eh->ether_type);
 1380 
 1381                         if (len > ETHER_MAX_FRAME(ifp, etype,
 1382                                                   M_HASFCS))
 1383                                 m->m_pkthdr.len = m->m_len = len =
 1384                                     ETHER_MAX_FRAME(ifp, etype, M_HASFCS);
 1385                 }
 1386 
 1387 #if NBPFILTER > 0
 1388                 /*
 1389                  * Pass this up to any BPF listeners, but only
 1390                  * pass it up the stack if its for us.
 1391                  */
 1392                 if (ifp->if_bpf)
 1393                         bpf_mtap(ifp->if_bpf, m);
 1394 #endif /* NPBFILTER > 0 */
 1395 
 1396                 /*
 1397                  * We sometimes have to run the 21140 in Hash-Only
 1398                  * mode.  If we're in that mode, and not in promiscuous
 1399                  * mode, and we have a unicast packet that isn't for
 1400                  * us, then drop it.
 1401                  */
 1402                 if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY &&
 1403                     (ifp->if_flags & IFF_PROMISC) == 0 &&
 1404                     ETHER_IS_MULTICAST(eh->ether_dhost) == 0 &&
 1405                     memcmp(LLADDR(ifp->if_sadl), eh->ether_dhost,
 1406                            ETHER_ADDR_LEN) != 0) {
 1407                         m_freem(m);
 1408                         continue;
 1409                 }
 1410 
 1411                 /* Pass it on. */
 1412                 (*ifp->if_input)(ifp, m);
 1413         }
 1414 
 1415         /* Update the receive pointer. */
 1416         sc->sc_rxptr = i;
 1417 }
 1418 
 1419 /*
 1420  * tlp_txintr:
 1421  *
 1422  *      Helper; handle transmit interrupts.
 1423  */
 1424 void
 1425 tlp_txintr(sc)
 1426         struct tulip_softc *sc;
 1427 {
 1428         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1429         struct tulip_txsoft *txs;
 1430         u_int32_t txstat;
 1431 
 1432         DPRINTF(sc, ("%s: tlp_txintr: sc_flags 0x%08x\n",
 1433             sc->sc_dev.dv_xname, sc->sc_flags));
 1434 
 1435         ifp->if_flags &= ~IFF_OACTIVE;
 1436 
 1437         /*
 1438          * Go through our Tx list and free mbufs for those
 1439          * frames that have been transmitted.
 1440          */
 1441         while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
 1442                 TULIP_CDTXSYNC(sc, txs->txs_lastdesc,
 1443                     txs->txs_ndescs,
 1444                     BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
 1445 
 1446 #ifdef TLP_DEBUG
 1447                 if (ifp->if_flags & IFF_DEBUG) {
 1448                         int i;
 1449                         printf("    txsoft %p transmit chain:\n", txs);
 1450                         for (i = txs->txs_firstdesc;; i = TULIP_NEXTTX(i)) {
 1451                                 printf("     descriptor %d:\n", i);
 1452                                 printf("       td_status:   0x%08x\n",
 1453                                     le32toh(sc->sc_txdescs[i].td_status));
 1454                                 printf("       td_ctl:      0x%08x\n",
 1455                                     le32toh(sc->sc_txdescs[i].td_ctl));
 1456                                 printf("       td_bufaddr1: 0x%08x\n",
 1457                                     le32toh(sc->sc_txdescs[i].td_bufaddr1));
 1458                                 printf("       td_bufaddr2: 0x%08x\n",
 1459                                     le32toh(sc->sc_txdescs[i].td_bufaddr2));
 1460                                 if (i == txs->txs_lastdesc)
 1461                                         break;
 1462                         }
 1463                 }
 1464 #endif
 1465 
 1466                 txstat = le32toh(sc->sc_txdescs[txs->txs_lastdesc].td_status);
 1467                 if (txstat & TDSTAT_OWN)
 1468                         break;
 1469 
 1470                 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
 1471 
 1472                 sc->sc_txfree += txs->txs_ndescs;
 1473 
 1474                 if (txs->txs_mbuf == NULL) {
 1475                         /*
 1476                          * If we didn't have an mbuf, it was the setup
 1477                          * packet.
 1478                          */
 1479 #ifdef DIAGNOSTIC
 1480                         if ((sc->sc_flags & TULIPF_DOING_SETUP) == 0)
 1481                                 panic("tlp_txintr: null mbuf, not doing setup");
 1482 #endif
 1483                         TULIP_CDSPSYNC(sc, BUS_DMASYNC_POSTWRITE);
 1484                         sc->sc_flags &= ~TULIPF_DOING_SETUP;
 1485                         SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
 1486                         continue;
 1487                 }
 1488 
 1489                 bus_dmamap_sync(sc->sc_dmat, txs->txs_dmamap,
 1490                     0, txs->txs_dmamap->dm_mapsize,
 1491                     BUS_DMASYNC_POSTWRITE);
 1492                 bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
 1493                 m_freem(txs->txs_mbuf);
 1494                 txs->txs_mbuf = NULL;
 1495 
 1496                 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
 1497 
 1498                 /*
 1499                  * Check for errors and collisions.
 1500                  */
 1501 #ifdef TLP_STATS
 1502                 if (txstat & TDSTAT_Tx_UF)
 1503                         sc->sc_stats.ts_tx_uf++;
 1504                 if (txstat & TDSTAT_Tx_TO)
 1505                         sc->sc_stats.ts_tx_to++;
 1506                 if (txstat & TDSTAT_Tx_EC)
 1507                         sc->sc_stats.ts_tx_ec++;
 1508                 if (txstat & TDSTAT_Tx_LC)
 1509                         sc->sc_stats.ts_tx_lc++;
 1510 #endif
 1511 
 1512                 if (txstat & (TDSTAT_Tx_UF|TDSTAT_Tx_TO))
 1513                         ifp->if_oerrors++;
 1514                 
 1515                 if (txstat & TDSTAT_Tx_EC)
 1516                         ifp->if_collisions += 16;
 1517                 else
 1518                         ifp->if_collisions += TDSTAT_Tx_COLLISIONS(txstat);
 1519                 if (txstat & TDSTAT_Tx_LC)
 1520                         ifp->if_collisions++;
 1521 
 1522                 ifp->if_opackets++;
 1523         }
 1524 
 1525         /*
 1526          * If there are no more pending transmissions, cancel the watchdog
 1527          * timer.
 1528          */
 1529         if (txs == NULL && (sc->sc_flags & TULIPF_DOING_SETUP) == 0)
 1530                 ifp->if_timer = 0;
 1531 
 1532         /*
 1533          * If we have a receive filter setup pending, do it now.
 1534          */
 1535         if (sc->sc_flags & TULIPF_WANT_SETUP)
 1536                 (*sc->sc_filter_setup)(sc);
 1537 }
 1538 
 1539 #ifdef TLP_STATS
 1540 void
 1541 tlp_print_stats(sc)
 1542         struct tulip_softc *sc;
 1543 {
 1544 
 1545         printf("%s: tx_uf %lu, tx_to %lu, tx_ec %lu, tx_lc %lu\n",
 1546             sc->sc_dev.dv_xname,
 1547             sc->sc_stats.ts_tx_uf, sc->sc_stats.ts_tx_to,
 1548             sc->sc_stats.ts_tx_ec, sc->sc_stats.ts_tx_lc);
 1549 }
 1550 #endif
 1551 
 1552 /*
 1553  * tlp_reset:
 1554  *
 1555  *      Perform a soft reset on the Tulip.
 1556  */
 1557 void
 1558 tlp_reset(sc)
 1559         struct tulip_softc *sc;
 1560 {
 1561         int i;
 1562 
 1563         TULIP_WRITE(sc, CSR_BUSMODE, BUSMODE_SWR);
 1564 
 1565         /*
 1566          * Xircom clone doesn't bring itself out of reset automatically.
 1567          * Instead, we have to wait at least 50 PCI cycles, and then
 1568          * clear SWR.
 1569          */
 1570         if (sc->sc_chip == TULIP_CHIP_X3201_3) {
 1571                 delay(10);
 1572                 TULIP_WRITE(sc, CSR_BUSMODE, 0);
 1573         }
 1574 
 1575         for (i = 0; i < 1000; i++) {
 1576                 /*
 1577                  * Wait at least 50 PCI cycles for the reset to
 1578                  * complete before peeking at the Tulip again.
 1579                  * 10 uSec is a bit longer than 50 PCI cycles
 1580                  * (at 33MHz), but it doesn't hurt have the extra
 1581                  * wait.
 1582                  */
 1583                 delay(10);
 1584                 if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR) == 0)
 1585                         break;
 1586         }
 1587 
 1588         if (TULIP_ISSET(sc, CSR_BUSMODE, BUSMODE_SWR))
 1589                 printf("%s: reset failed to complete\n", sc->sc_dev.dv_xname);
 1590 
 1591         delay(1000);
 1592 
 1593         /*
 1594          * If the board has any GPIO reset sequences to issue, do them now.
 1595          */
 1596         if (sc->sc_reset != NULL)
 1597                 (*sc->sc_reset)(sc);
 1598 }
 1599 
 1600 /*
 1601  * tlp_init:            [ ifnet interface function ]
 1602  *
 1603  *      Initialize the interface.  Must be called at splnet().
 1604  */
 1605 int
 1606 tlp_init(ifp)
 1607         struct ifnet *ifp;
 1608 {
 1609         struct tulip_softc *sc = ifp->if_softc;
 1610         struct tulip_txsoft *txs;
 1611         struct tulip_rxsoft *rxs;
 1612         int i, error = 0;
 1613 
 1614         if ((error = tlp_enable(sc)) != 0)
 1615                 goto out;
 1616 
 1617         /*
 1618          * Cancel any pending I/O.
 1619          */
 1620         tlp_stop(ifp, 0);
 1621 
 1622         /*
 1623          * Initialize `opmode' to 0, and call the pre-init routine, if
 1624          * any.  This is required because the 2114x and some of the
 1625          * clones require that the media-related bits in `opmode' be
 1626          * set before performing a soft-reset in order to get internal
 1627          * chip pathways are correct.  Yay!
 1628          */
 1629         sc->sc_opmode = 0;
 1630         if (sc->sc_preinit != NULL)
 1631                 (*sc->sc_preinit)(sc);
 1632 
 1633         /*
 1634          * Reset the Tulip to a known state.
 1635          */
 1636         tlp_reset(sc);
 1637 
 1638         /*
 1639          * Initialize the BUSMODE register.
 1640          */
 1641         sc->sc_busmode = BUSMODE_BAR;
 1642         switch (sc->sc_chip) {
 1643         case TULIP_CHIP_21140:
 1644         case TULIP_CHIP_21140A:
 1645         case TULIP_CHIP_21142:
 1646         case TULIP_CHIP_21143:
 1647         case TULIP_CHIP_82C115:
 1648         case TULIP_CHIP_MX98725:
 1649                 /*
 1650                  * If we're allowed to do so, use Memory Read Line
 1651                  * and Memory Read Multiple.
 1652                  *
 1653                  * XXX Should we use Memory Write and Invalidate?
 1654                  */
 1655                 if (sc->sc_flags & TULIPF_MRL)
 1656                         sc->sc_busmode |= BUSMODE_RLE;
 1657                 if (sc->sc_flags & TULIPF_MRM)
 1658                         sc->sc_busmode |= BUSMODE_RME;
 1659 #if 0
 1660                 if (sc->sc_flags & TULIPF_MWI)
 1661                         sc->sc_busmode |= BUSMODE_WLE;
 1662 #endif
 1663                 break;
 1664 
 1665         case TULIP_CHIP_82C168:
 1666         case TULIP_CHIP_82C169:
 1667                 sc->sc_busmode |= BUSMODE_PNIC_MBO;
 1668                 if (sc->sc_maxburst == 0)
 1669                         sc->sc_maxburst = 16;
 1670                 break;
 1671 
 1672         default:
 1673                 /* Nothing. */
 1674                 break;
 1675         }
 1676         switch (sc->sc_cacheline) {
 1677         default:
 1678                 /*
 1679                  * Note: We must *always* set these bits; a cache
 1680                  * alignment of 0 is RESERVED.
 1681                  */
 1682         case 8:
 1683                 sc->sc_busmode |= BUSMODE_CAL_8LW;
 1684                 break;
 1685         case 16:
 1686                 sc->sc_busmode |= BUSMODE_CAL_16LW;
 1687                 break;
 1688         case 32:
 1689                 sc->sc_busmode |= BUSMODE_CAL_32LW;
 1690                 break;
 1691         }
 1692         switch (sc->sc_maxburst) {
 1693         case 1:
 1694                 sc->sc_busmode |= BUSMODE_PBL_1LW;
 1695                 break;
 1696         case 2:
 1697                 sc->sc_busmode |= BUSMODE_PBL_2LW;
 1698                 break;
 1699         case 4:
 1700                 sc->sc_busmode |= BUSMODE_PBL_4LW;
 1701                 break;
 1702         case 8:
 1703                 sc->sc_busmode |= BUSMODE_PBL_8LW;
 1704                 break;
 1705         case 16:
 1706                 sc->sc_busmode |= BUSMODE_PBL_16LW;
 1707                 break;
 1708         case 32:
 1709                 sc->sc_busmode |= BUSMODE_PBL_32LW;
 1710                 break;
 1711         default:
 1712                 sc->sc_busmode |= BUSMODE_PBL_DEFAULT;
 1713                 break;
 1714         }
 1715 #if BYTE_ORDER == BIG_ENDIAN
 1716         /*
 1717          * Can't use BUSMODE_BLE or BUSMODE_DBO; not all chips
 1718          * support them, and even on ones that do, it doesn't
 1719          * always work.  So we always access descriptors with
 1720          * little endian via htole32/le32toh.
 1721          */
 1722 #endif
 1723         /*
 1724          * Big-endian bus requires BUSMODE_BLE anyway.
 1725          * Also, BUSMODE_DBO is needed because we assume
 1726          * descriptors are little endian.
 1727          */
 1728         if (sc->sc_flags & TULIPF_BLE)
 1729                 sc->sc_busmode |= BUSMODE_BLE;
 1730         if (sc->sc_flags & TULIPF_DBO)
 1731                 sc->sc_busmode |= BUSMODE_DBO;
 1732 
 1733         /*
 1734          * Some chips have a broken bus interface.
 1735          */
 1736         switch (sc->sc_chip) {
 1737         case TULIP_CHIP_DM9102:
 1738         case TULIP_CHIP_DM9102A:
 1739                 sc->sc_busmode = 0;
 1740                 break;
 1741 
 1742         default:
 1743                 /* Nothing. */
 1744                 break;
 1745         }
 1746 
 1747         TULIP_WRITE(sc, CSR_BUSMODE, sc->sc_busmode);
 1748 
 1749         /*
 1750          * Initialize the OPMODE register.  We don't write it until
 1751          * we're ready to begin the transmit and receive processes.
 1752          *
 1753          * Media-related OPMODE bits are set in the media callbacks
 1754          * for each specific chip/board.
 1755          */
 1756         sc->sc_opmode |= OPMODE_SR | OPMODE_ST |
 1757             sc->sc_txth[sc->sc_txthresh].txth_opmode;
 1758 
 1759         /*
 1760          * Magical mystery initialization on the Macronix chips.
 1761          * The MX98713 uses its own magic value, the rest share
 1762          * a common one.
 1763          */
 1764         switch (sc->sc_chip) {
 1765         case TULIP_CHIP_MX98713:
 1766                 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98713);
 1767                 break;
 1768 
 1769         case TULIP_CHIP_MX98713A:
 1770         case TULIP_CHIP_MX98715:
 1771         case TULIP_CHIP_MX98715A:
 1772         case TULIP_CHIP_MX98715AEC_X:
 1773         case TULIP_CHIP_MX98725:
 1774                 TULIP_WRITE(sc, CSR_PMAC_TOR, PMAC_TOR_98715);
 1775                 break;
 1776 
 1777         default:
 1778                 /* Nothing. */
 1779                 break;
 1780         }
 1781 
 1782         /*
 1783          * Initialize the transmit descriptor ring.
 1784          */
 1785         memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
 1786         for (i = 0; i < TULIP_NTXDESC; i++) {
 1787                 sc->sc_txdescs[i].td_ctl = htole32(sc->sc_tdctl_ch);
 1788                 sc->sc_txdescs[i].td_bufaddr2 =
 1789                     htole32(TULIP_CDTXADDR(sc, TULIP_NEXTTX(i)));
 1790         }
 1791         sc->sc_txdescs[TULIP_NTXDESC - 1].td_ctl |= htole32(sc->sc_tdctl_er);
 1792         TULIP_CDTXSYNC(sc, 0, TULIP_NTXDESC,
 1793             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 1794         sc->sc_txfree = TULIP_NTXDESC;
 1795         sc->sc_txnext = 0;
 1796 
 1797         /*
 1798          * Initialize the transmit job descriptors.
 1799          */
 1800         SIMPLEQ_INIT(&sc->sc_txfreeq);
 1801         SIMPLEQ_INIT(&sc->sc_txdirtyq);
 1802         for (i = 0; i < TULIP_TXQUEUELEN; i++) {
 1803                 txs = &sc->sc_txsoft[i];
 1804                 txs->txs_mbuf = NULL;
 1805                 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
 1806         }
 1807 
 1808         /*
 1809          * Initialize the receive descriptor and receive job
 1810          * descriptor rings.
 1811          */
 1812         for (i = 0; i < TULIP_NRXDESC; i++) {
 1813                 rxs = &sc->sc_rxsoft[i];
 1814                 if (rxs->rxs_mbuf == NULL) {
 1815                         if ((error = tlp_add_rxbuf(sc, i)) != 0) {
 1816                                 printf("%s: unable to allocate or map rx "
 1817                                     "buffer %d, error = %d\n",
 1818                                     sc->sc_dev.dv_xname, i, error);
 1819                                 /*
 1820                                  * XXX Should attempt to run with fewer receive
 1821                                  * XXX buffers instead of just failing.
 1822                                  */
 1823                                 tlp_rxdrain(sc);
 1824                                 goto out;
 1825                         }
 1826                 } else
 1827                         TULIP_INIT_RXDESC(sc, i);
 1828         }
 1829         sc->sc_rxptr = 0;
 1830 
 1831         /*
 1832          * Initialize the interrupt mask and enable interrupts.
 1833          */
 1834         /* normal interrupts */
 1835         sc->sc_inten =  STATUS_TI | STATUS_TU | STATUS_RI | STATUS_NIS;
 1836 
 1837         /* abnormal interrupts */
 1838         sc->sc_inten |= STATUS_TPS | STATUS_TJT | STATUS_UNF |
 1839             STATUS_RU | STATUS_RPS | STATUS_RWT | STATUS_SE | STATUS_AIS;
 1840 
 1841         sc->sc_rxint_mask = STATUS_RI|STATUS_RU|STATUS_RWT;
 1842         sc->sc_txint_mask = STATUS_TI|STATUS_UNF|STATUS_TJT;
 1843 
 1844         switch (sc->sc_chip) {
 1845         case TULIP_CHIP_WB89C840F:
 1846                 /*
 1847                  * Clear bits that we don't want that happen to
 1848                  * overlap or don't exist.
 1849                  */
 1850                 sc->sc_inten &= ~(STATUS_WINB_REI|STATUS_RWT);
 1851                 break;
 1852 
 1853         default:
 1854                 /* Nothing. */
 1855                 break;
 1856         }
 1857 
 1858         sc->sc_rxint_mask &= sc->sc_inten;
 1859         sc->sc_txint_mask &= sc->sc_inten;
 1860 
 1861         TULIP_WRITE(sc, CSR_INTEN, sc->sc_inten);
 1862         TULIP_WRITE(sc, CSR_STATUS, 0xffffffff);
 1863 
 1864         /*
 1865          * Give the transmit and receive rings to the Tulip.
 1866          */
 1867         TULIP_WRITE(sc, CSR_TXLIST, TULIP_CDTXADDR(sc, sc->sc_txnext));
 1868         TULIP_WRITE(sc, CSR_RXLIST, TULIP_CDRXADDR(sc, sc->sc_rxptr));
 1869 
 1870         /*
 1871          * On chips that do this differently, set the station address.
 1872          */
 1873         switch (sc->sc_chip) {
 1874         case TULIP_CHIP_WB89C840F:
 1875             {
 1876                 /* XXX Do this with stream writes? */
 1877                 bus_addr_t cpa = TULIP_CSR_OFFSET(sc, CSR_WINB_CPA0);
 1878 
 1879                 for (i = 0; i < ETHER_ADDR_LEN; i++) {
 1880                         bus_space_write_1(sc->sc_st, sc->sc_sh,
 1881                             cpa + i, LLADDR(ifp->if_sadl)[i]);
 1882                 }
 1883                 break;
 1884             }
 1885 
 1886         case TULIP_CHIP_AL981:
 1887         case TULIP_CHIP_AN983:
 1888         case TULIP_CHIP_AN985:
 1889             {
 1890                 u_int32_t reg;
 1891                 u_int8_t *enaddr = LLADDR(ifp->if_sadl);
 1892 
 1893                 reg = enaddr[0] |
 1894                       (enaddr[1] << 8) |
 1895                       (enaddr[2] << 16) |
 1896                       (enaddr[3] << 24);
 1897                 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR0, reg);
 1898 
 1899                 reg = enaddr[4] |
 1900                       (enaddr[5] << 8);
 1901                 bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_PAR1, reg);
 1902             }
 1903 
 1904         default:
 1905                 /* Nothing. */
 1906                 break;
 1907         }
 1908 
 1909         /*
 1910          * Set the receive filter.  This will start the transmit and
 1911          * receive processes.
 1912          */
 1913         (*sc->sc_filter_setup)(sc);
 1914 
 1915         /*
 1916          * Set the current media.
 1917          */
 1918         (void) (*sc->sc_mediasw->tmsw_set)(sc);
 1919 
 1920         /*
 1921          * Start the receive process.
 1922          */
 1923         TULIP_WRITE(sc, CSR_RXPOLL, RXPOLL_RPD);
 1924 
 1925         if (sc->sc_tick != NULL) {
 1926                 /* Start the one second clock. */
 1927                 callout_reset(&sc->sc_tick_callout, hz >> 3, sc->sc_tick, sc);
 1928         }
 1929 
 1930         /*
 1931          * Note that the interface is now running.
 1932          */
 1933         ifp->if_flags |= IFF_RUNNING;
 1934         ifp->if_flags &= ~IFF_OACTIVE;
 1935 
 1936  out:
 1937         if (error) {
 1938                 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
 1939                 ifp->if_timer = 0;
 1940                 printf("%s: interface not running\n", sc->sc_dev.dv_xname);
 1941         }
 1942         return (error);
 1943 }
 1944 
 1945 /*
 1946  * tlp_enable:
 1947  *
 1948  *      Enable the Tulip chip.
 1949  */
 1950 int
 1951 tlp_enable(sc)
 1952         struct tulip_softc *sc;
 1953 {
 1954 
 1955         if (TULIP_IS_ENABLED(sc) == 0 && sc->sc_enable != NULL) {
 1956                 if ((*sc->sc_enable)(sc) != 0) {
 1957                         printf("%s: device enable failed\n",
 1958                             sc->sc_dev.dv_xname);
 1959                         return (EIO);
 1960                 }
 1961                 sc->sc_flags |= TULIPF_ENABLED;
 1962         }
 1963         return (0);
 1964 }
 1965 
 1966 /*
 1967  * tlp_disable:
 1968  *
 1969  *      Disable the Tulip chip.
 1970  */
 1971 void
 1972 tlp_disable(sc)
 1973         struct tulip_softc *sc;
 1974 {
 1975 
 1976         if (TULIP_IS_ENABLED(sc) && sc->sc_disable != NULL) {
 1977                 (*sc->sc_disable)(sc);
 1978                 sc->sc_flags &= ~TULIPF_ENABLED;
 1979         }
 1980 }
 1981 
 1982 /*
 1983  * tlp_power:
 1984  *
 1985  *      Power management (suspend/resume) hook.
 1986  */
 1987 void
 1988 tlp_power(why, arg)
 1989         int why;
 1990         void *arg;
 1991 {
 1992         struct tulip_softc *sc = arg;
 1993         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 1994         int s;
 1995 
 1996         s = splnet();
 1997         switch (why) {
 1998         case PWR_STANDBY:
 1999                 /* do nothing! */
 2000                 break;
 2001         case PWR_SUSPEND:
 2002                 tlp_stop(ifp, 0);
 2003                 if (sc->sc_power != NULL)
 2004                         (*sc->sc_power)(sc, why);
 2005                 break;
 2006         case PWR_RESUME:
 2007                 if (ifp->if_flags & IFF_UP) {
 2008                         if (sc->sc_power != NULL)
 2009                                 (*sc->sc_power)(sc, why);
 2010                         tlp_init(ifp);
 2011                 }
 2012                 break;
 2013         case PWR_SOFTSUSPEND:
 2014         case PWR_SOFTSTANDBY:
 2015         case PWR_SOFTRESUME:
 2016                 break;
 2017         }
 2018         splx(s);
 2019 }
 2020 
 2021 /*
 2022  * tlp_rxdrain:
 2023  *
 2024  *      Drain the receive queue.
 2025  */
 2026 void
 2027 tlp_rxdrain(sc)
 2028         struct tulip_softc *sc;
 2029 {
 2030         struct tulip_rxsoft *rxs;
 2031         int i;
 2032 
 2033         for (i = 0; i < TULIP_NRXDESC; i++) {
 2034                 rxs = &sc->sc_rxsoft[i];
 2035                 if (rxs->rxs_mbuf != NULL) {
 2036                         bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
 2037                         m_freem(rxs->rxs_mbuf);
 2038                         rxs->rxs_mbuf = NULL;
 2039                 }
 2040         }
 2041 }
 2042 
 2043 /*
 2044  * tlp_stop:            [ ifnet interface function ]
 2045  *
 2046  *      Stop transmission on the interface.
 2047  */
 2048 void
 2049 tlp_stop(ifp, disable)
 2050         struct ifnet *ifp;
 2051         int disable;
 2052 {
 2053         struct tulip_softc *sc = ifp->if_softc;
 2054         struct tulip_txsoft *txs;
 2055 
 2056         if (sc->sc_tick != NULL) {
 2057                 /* Stop the one second clock. */
 2058                 callout_stop(&sc->sc_tick_callout);
 2059         }
 2060 
 2061         if (sc->sc_flags & TULIPF_HAS_MII) {
 2062                 /* Down the MII. */
 2063                 mii_down(&sc->sc_mii);
 2064         }
 2065 
 2066         /* Disable interrupts. */
 2067         TULIP_WRITE(sc, CSR_INTEN, 0);
 2068 
 2069         /* Stop the transmit and receive processes. */
 2070         sc->sc_opmode = 0;
 2071         TULIP_WRITE(sc, CSR_OPMODE, 0);
 2072         TULIP_WRITE(sc, CSR_RXLIST, 0);
 2073         TULIP_WRITE(sc, CSR_TXLIST, 0);
 2074 
 2075         /*
 2076          * Release any queued transmit buffers.
 2077          */
 2078         while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
 2079                 SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
 2080                 if (txs->txs_mbuf != NULL) {
 2081                         bus_dmamap_unload(sc->sc_dmat, txs->txs_dmamap);
 2082                         m_freem(txs->txs_mbuf);
 2083                         txs->txs_mbuf = NULL;
 2084                 }
 2085                 SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
 2086         }
 2087 
 2088         if (disable) {
 2089                 tlp_rxdrain(sc);
 2090                 tlp_disable(sc);
 2091         }
 2092 
 2093         sc->sc_flags &= ~(TULIPF_WANT_SETUP|TULIPF_DOING_SETUP);
 2094 
 2095         /*
 2096          * Mark the interface down and cancel the watchdog timer.
 2097          */
 2098         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
 2099         ifp->if_timer = 0;
 2100 
 2101         /*
 2102          * Reset the chip (needed on some flavors to actually disable it).
 2103          */
 2104         tlp_reset(sc);
 2105 }
 2106 
 2107 #define SROM_EMIT(sc, x)                                                \
 2108 do {                                                                    \
 2109         TULIP_WRITE((sc), CSR_MIIROM, (x));                             \
 2110         delay(2);                                                       \
 2111 } while (0)
 2112 
 2113 /*
 2114  * tlp_srom_idle:
 2115  *
 2116  *      Put the SROM in idle state.
 2117  */
 2118 void
 2119 tlp_srom_idle(sc)
 2120         struct tulip_softc *sc;
 2121 {
 2122         u_int32_t miirom;
 2123         int i;
 2124 
 2125         miirom = MIIROM_SR;
 2126         SROM_EMIT(sc, miirom);
 2127 
 2128         miirom |= MIIROM_RD;
 2129         SROM_EMIT(sc, miirom);
 2130 
 2131         miirom |= MIIROM_SROMCS;
 2132         SROM_EMIT(sc, miirom);
 2133 
 2134         SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2135 
 2136         /* Strobe the clock 32 times. */
 2137         for (i = 0; i < 32; i++) {
 2138                 SROM_EMIT(sc, miirom);
 2139                 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2140         }
 2141 
 2142         SROM_EMIT(sc, miirom);
 2143 
 2144         miirom &= ~MIIROM_SROMCS;
 2145         SROM_EMIT(sc, miirom);
 2146 
 2147         SROM_EMIT(sc, 0);
 2148 }
 2149 
 2150 /*
 2151  * tlp_srom_size:
 2152  *
 2153  *      Determine the number of address bits in the SROM.
 2154  */
 2155 int
 2156 tlp_srom_size(sc)
 2157         struct tulip_softc *sc;
 2158 {
 2159         u_int32_t miirom;
 2160         int x;
 2161 
 2162         /* Select the SROM. */
 2163         miirom = MIIROM_SR;
 2164         SROM_EMIT(sc, miirom);
 2165 
 2166         miirom |= MIIROM_RD;
 2167         SROM_EMIT(sc, miirom);
 2168 
 2169         /* Send CHIP SELECT for one clock tick. */
 2170         miirom |= MIIROM_SROMCS;
 2171         SROM_EMIT(sc, miirom);
 2172 
 2173         /* Shift in the READ opcode. */
 2174         for (x = 3; x > 0; x--) {
 2175                 if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
 2176                         miirom |= MIIROM_SROMDI;
 2177                 else
 2178                         miirom &= ~MIIROM_SROMDI;
 2179                 SROM_EMIT(sc, miirom);
 2180                 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2181                 SROM_EMIT(sc, miirom);
 2182         }
 2183 
 2184         /* Shift in address and look for dummy 0 bit. */
 2185         for (x = 1; x <= 12; x++) {
 2186                 miirom &= ~MIIROM_SROMDI;
 2187                 SROM_EMIT(sc, miirom);
 2188                 SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2189                 if (!TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
 2190                         break;
 2191                 SROM_EMIT(sc, miirom);
 2192         }
 2193 
 2194         /* Clear CHIP SELECT. */
 2195         miirom &= ~MIIROM_SROMCS;
 2196         SROM_EMIT(sc, miirom);
 2197 
 2198         /* Deselect the SROM. */
 2199         SROM_EMIT(sc, 0);
 2200 
 2201         if (x < 4 || x > 12) {
 2202                 printf("%s: broken MicroWire interface detected; "
 2203                     "setting SROM size to 1Kb\n", sc->sc_dev.dv_xname);
 2204                 return (6);
 2205         } else {
 2206                 if (tlp_srom_debug)
 2207                         printf("%s: SROM size is 2^%d*16 bits (%d bytes)\n",
 2208                             sc->sc_dev.dv_xname, x, (1 << (x + 4)) >> 3);
 2209                 return (x);
 2210         }
 2211 }
 2212 
 2213 /*
 2214  * tlp_read_srom:
 2215  *
 2216  *      Read the Tulip SROM.
 2217  */
 2218 int
 2219 tlp_read_srom(sc)
 2220         struct tulip_softc *sc;
 2221 {
 2222         int size;
 2223         u_int32_t miirom;
 2224         u_int16_t datain;
 2225         int i, x;
 2226 
 2227         tlp_srom_idle(sc);
 2228 
 2229         sc->sc_srom_addrbits = tlp_srom_size(sc);
 2230         if (sc->sc_srom_addrbits == 0)
 2231                 return (0);
 2232         size = TULIP_ROM_SIZE(sc->sc_srom_addrbits);
 2233         sc->sc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
 2234 
 2235         /* Select the SROM. */
 2236         miirom = MIIROM_SR;
 2237         SROM_EMIT(sc, miirom);
 2238 
 2239         miirom |= MIIROM_RD;
 2240         SROM_EMIT(sc, miirom);
 2241 
 2242         for (i = 0; i < size; i += 2) {
 2243                 /* Send CHIP SELECT for one clock tick. */
 2244                 miirom |= MIIROM_SROMCS;
 2245                 SROM_EMIT(sc, miirom);
 2246 
 2247                 /* Shift in the READ opcode. */
 2248                 for (x = 3; x > 0; x--) {
 2249                         if (TULIP_SROM_OPC_READ & (1 << (x - 1)))
 2250                                 miirom |= MIIROM_SROMDI;
 2251                         else
 2252                                 miirom &= ~MIIROM_SROMDI;
 2253                         SROM_EMIT(sc, miirom);
 2254                         SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2255                         SROM_EMIT(sc, miirom);
 2256                 }
 2257 
 2258                 /* Shift in address. */
 2259                 for (x = sc->sc_srom_addrbits; x > 0; x--) {
 2260                         if (i & (1 << x))
 2261                                 miirom |= MIIROM_SROMDI;
 2262                         else
 2263                                 miirom &= ~MIIROM_SROMDI;
 2264                         SROM_EMIT(sc, miirom);
 2265                         SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2266                         SROM_EMIT(sc, miirom);
 2267                 }
 2268 
 2269                 /* Shift out data. */
 2270                 miirom &= ~MIIROM_SROMDI;
 2271                 datain = 0;
 2272                 for (x = 16; x > 0; x--) {
 2273                         SROM_EMIT(sc, miirom|MIIROM_SROMSK);
 2274                         if (TULIP_ISSET(sc, CSR_MIIROM, MIIROM_SROMDO))
 2275                                 datain |= (1 << (x - 1));
 2276                         SROM_EMIT(sc, miirom);
 2277                 }
 2278                 sc->sc_srom[i] = datain & 0xff;
 2279                 sc->sc_srom[i + 1] = datain >> 8;
 2280 
 2281                 /* Clear CHIP SELECT. */
 2282                 miirom &= ~MIIROM_SROMCS;
 2283                 SROM_EMIT(sc, miirom);
 2284         }
 2285 
 2286         /* Deselect the SROM. */
 2287         SROM_EMIT(sc, 0);
 2288 
 2289         /* ...and idle it. */
 2290         tlp_srom_idle(sc);
 2291 
 2292         if (tlp_srom_debug) {
 2293                 printf("SROM CONTENTS:");
 2294                 for (i = 0; i < size; i++) {
 2295                         if ((i % 8) == 0)
 2296                                 printf("\n\t");
 2297                         printf("0x%02x ", sc->sc_srom[i]);
 2298                 }
 2299                 printf("\n");
 2300         }
 2301 
 2302         return (1);
 2303 }
 2304 
 2305 #undef SROM_EMIT
 2306 
 2307 /*
 2308  * tlp_add_rxbuf:
 2309  *
 2310  *      Add a receive buffer to the indicated descriptor.
 2311  */
 2312 int
 2313 tlp_add_rxbuf(sc, idx)  
 2314         struct tulip_softc *sc;
 2315         int idx;
 2316 {
 2317         struct tulip_rxsoft *rxs = &sc->sc_rxsoft[idx];
 2318         struct mbuf *m;
 2319         int error;
 2320 
 2321         MGETHDR(m, M_DONTWAIT, MT_DATA);
 2322         if (m == NULL)
 2323                 return (ENOBUFS);
 2324 
 2325         MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
 2326         MCLGET(m, M_DONTWAIT);
 2327         if ((m->m_flags & M_EXT) == 0) {
 2328                 m_freem(m);
 2329                 return (ENOBUFS);
 2330         }
 2331 
 2332         if (rxs->rxs_mbuf != NULL)
 2333                 bus_dmamap_unload(sc->sc_dmat, rxs->rxs_dmamap);
 2334 
 2335         rxs->rxs_mbuf = m;
 2336 
 2337         error = bus_dmamap_load(sc->sc_dmat, rxs->rxs_dmamap,
 2338             m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
 2339             BUS_DMA_READ|BUS_DMA_NOWAIT);
 2340         if (error) {
 2341                 printf("%s: can't load rx DMA map %d, error = %d\n",
 2342                     sc->sc_dev.dv_xname, idx, error);
 2343                 panic("tlp_add_rxbuf"); /* XXX */
 2344         }
 2345 
 2346         bus_dmamap_sync(sc->sc_dmat, rxs->rxs_dmamap, 0,
 2347             rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
 2348 
 2349         TULIP_INIT_RXDESC(sc, idx);
 2350 
 2351         return (0);
 2352 }
 2353 
 2354 /*
 2355  * tlp_srom_crcok:
 2356  *
 2357  *      Check the CRC of the Tulip SROM.
 2358  */
 2359 int
 2360 tlp_srom_crcok(romdata)
 2361         const u_int8_t *romdata;
 2362 {
 2363         u_int32_t crc;
 2364 
 2365         crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM);
 2366         crc = (crc & 0xffff) ^ 0xffff;
 2367         if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM))
 2368                 return (1);
 2369 
 2370         /*
 2371          * Try an alternate checksum.
 2372          */
 2373         crc = ether_crc32_le(romdata, TULIP_ROM_CRC32_CHECKSUM1);
 2374         crc = (crc & 0xffff) ^ 0xffff;
 2375         if (crc == TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM1))
 2376                 return (1);
 2377 
 2378         return (0);
 2379 }
 2380 
 2381 /*
 2382  * tlp_isv_srom:
 2383  *
 2384  *      Check to see if the SROM is in the new standardized format.
 2385  */
 2386 int
 2387 tlp_isv_srom(romdata)
 2388         const u_int8_t *romdata;
 2389 {
 2390         int i;
 2391         u_int16_t cksum;
 2392 
 2393         if (tlp_srom_crcok(romdata)) {
 2394                 /*
 2395                  * SROM CRC checks out; must be in the new format.
 2396                  */
 2397                 return (1);
 2398         }
 2399 
 2400         cksum = TULIP_ROM_GETW(romdata, TULIP_ROM_CRC32_CHECKSUM);
 2401         if (cksum == 0xffff || cksum == 0) {
 2402                 /*
 2403                  * No checksum present.  Check the SROM ID; 18 bytes of 0
 2404                  * followed by 1 (version) followed by the number of
 2405                  * adapters which use this SROM (should be non-zero).
 2406                  */
 2407                 for (i = 0; i < TULIP_ROM_SROM_FORMAT_VERION; i++) {
 2408                         if (romdata[i] != 0)
 2409                                 return (0);
 2410                 }
 2411                 if (romdata[TULIP_ROM_SROM_FORMAT_VERION] != 1)
 2412                         return (0);
 2413                 if (romdata[TULIP_ROM_CHIP_COUNT] == 0)
 2414                         return (0);
 2415                 return (1);
 2416         }
 2417 
 2418         return (0);
 2419 }
 2420 
 2421 /*
 2422  * tlp_isv_srom_enaddr:
 2423  *
 2424  *      Get the Ethernet address from an ISV SROM.
 2425  */
 2426 int
 2427 tlp_isv_srom_enaddr(sc, enaddr)
 2428         struct tulip_softc *sc;
 2429         u_int8_t *enaddr;
 2430 {
 2431         int i, devcnt;
 2432 
 2433         if (tlp_isv_srom(sc->sc_srom) == 0)
 2434                 return (0);
 2435 
 2436         devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
 2437         for (i = 0; i < devcnt; i++) {
 2438                 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
 2439                         break;
 2440                 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
 2441                     sc->sc_devno)
 2442                         break;
 2443         }
 2444 
 2445         if (i == devcnt)
 2446                 return (0);
 2447 
 2448         memcpy(enaddr, &sc->sc_srom[TULIP_ROM_IEEE_NETWORK_ADDRESS],
 2449             ETHER_ADDR_LEN);
 2450         enaddr[5] += i;
 2451 
 2452         return (1);
 2453 }
 2454 
 2455 /*
 2456  * tlp_parse_old_srom:
 2457  *
 2458  *      Parse old-format SROMs.
 2459  *
 2460  *      This routine is largely lifted from Matt Thomas's `de' driver.
 2461  */
 2462 int
 2463 tlp_parse_old_srom(sc, enaddr)
 2464         struct tulip_softc *sc;
 2465         u_int8_t *enaddr;
 2466 {
 2467         static const u_int8_t testpat[] =
 2468             { 0xff, 0, 0x55, 0xaa, 0xff, 0, 0x55, 0xaa };
 2469         int i;
 2470         u_int32_t cksum;
 2471 
 2472         if (memcmp(&sc->sc_srom[0], &sc->sc_srom[16], 8) != 0) {
 2473                 /*
 2474                  * Cobalt Networks interfaces simply have the address
 2475                  * in the first six bytes. The rest is zeroed out
 2476                  * on some models, but others contain unknown data.
 2477                  */
 2478                 if (sc->sc_srom[0] == 0x00 &&
 2479                     sc->sc_srom[1] == 0x10 &&
 2480                     sc->sc_srom[2] == 0xe0) {
 2481                         memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
 2482                         return (1);
 2483                 }
 2484 
 2485                 /*
 2486                  * Some vendors (e.g. ZNYX) don't use the standard
 2487                  * DEC Address ROM format, but rather just have an
 2488                  * Ethernet address in the first 6 bytes, maybe a
 2489                  * 2 byte checksum, and then all 0xff's.
 2490                  */
 2491                 for (i = 8; i < 32; i++) {
 2492                         if (sc->sc_srom[i] != 0xff &&
 2493                             sc->sc_srom[i] != 0)
 2494                                 return (0);
 2495                 }
 2496 
 2497                 /*
 2498                  * Sanity check the Ethernet address:
 2499                  *
 2500                  *      - Make sure it's not multicast or locally
 2501                  *        assigned
 2502                  *      - Make sure it has a non-0 OUI
 2503                  */
 2504                 if (sc->sc_srom[0] & 3)
 2505                         return (0);
 2506                 if (sc->sc_srom[0] == 0 && sc->sc_srom[1] == 0 &&
 2507                     sc->sc_srom[2] == 0)
 2508                         return (0);
 2509 
 2510                 memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
 2511                 return (1);
 2512         }
 2513 
 2514         /*
 2515          * Standard DEC Address ROM test.
 2516          */
 2517 
 2518         if (memcmp(&sc->sc_srom[24], testpat, 8) != 0)
 2519                 return (0);
 2520 
 2521         for (i = 0; i < 8; i++) {
 2522                 if (sc->sc_srom[i] != sc->sc_srom[15 - i])
 2523                         return (0);
 2524         }
 2525 
 2526         memcpy(enaddr, sc->sc_srom, ETHER_ADDR_LEN);
 2527 
 2528         cksum = *(u_int16_t *) &enaddr[0];
 2529 
 2530         cksum <<= 1;
 2531         if (cksum > 0xffff)
 2532                 cksum -= 0xffff;
 2533 
 2534         cksum += *(u_int16_t *) &enaddr[2];
 2535         if (cksum > 0xffff)
 2536                 cksum -= 0xffff;
 2537 
 2538         cksum <<= 1;
 2539         if (cksum > 0xffff)
 2540                 cksum -= 0xffff;
 2541 
 2542         cksum += *(u_int16_t *) &enaddr[4];
 2543         if (cksum >= 0xffff)
 2544                 cksum -= 0xffff;
 2545 
 2546         if (cksum != *(u_int16_t *) &sc->sc_srom[6])
 2547                 return (0);
 2548 
 2549         return (1);
 2550 }
 2551 
 2552 /*
 2553  * tlp_filter_setup:
 2554  *
 2555  *      Set the Tulip's receive filter.
 2556  */
 2557 void
 2558 tlp_filter_setup(sc)
 2559         struct tulip_softc *sc;
 2560 {
 2561         struct ethercom *ec = &sc->sc_ethercom;
 2562         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 2563         struct ether_multi *enm;
 2564         struct ether_multistep step;
 2565         __volatile u_int32_t *sp;
 2566         struct tulip_txsoft *txs;
 2567         u_int8_t enaddr[ETHER_ADDR_LEN];
 2568         u_int32_t hash, hashsize;
 2569         int cnt;
 2570 
 2571         DPRINTF(sc, ("%s: tlp_filter_setup: sc_flags 0x%08x\n",
 2572             sc->sc_dev.dv_xname, sc->sc_flags));
 2573 
 2574         memcpy(enaddr, LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
 2575 
 2576         /*
 2577          * If there are transmissions pending, wait until they have
 2578          * completed.
 2579          */
 2580         if (! SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ||
 2581             (sc->sc_flags & TULIPF_DOING_SETUP) != 0) {
 2582                 sc->sc_flags |= TULIPF_WANT_SETUP;
 2583                 DPRINTF(sc, ("%s: tlp_filter_setup: deferring\n",
 2584                     sc->sc_dev.dv_xname));
 2585                 return;
 2586         }
 2587         sc->sc_flags &= ~TULIPF_WANT_SETUP;
 2588 
 2589         switch (sc->sc_chip) {
 2590         case TULIP_CHIP_82C115:
 2591                 hashsize = TULIP_PNICII_HASHSIZE;
 2592                 break;
 2593 
 2594         default:
 2595                 hashsize = TULIP_MCHASHSIZE;
 2596         }
 2597 
 2598         /*
 2599          * If we're running, idle the transmit and receive engines.  If
 2600          * we're NOT running, we're being called from tlp_init(), and our
 2601          * writing OPMODE will start the transmit and receive processes
 2602          * in motion.
 2603          */
 2604         if (ifp->if_flags & IFF_RUNNING)
 2605                 tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 2606 
 2607         sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
 2608 
 2609         if (ifp->if_flags & IFF_PROMISC) {
 2610                 sc->sc_opmode |= OPMODE_PR;
 2611                 goto allmulti;
 2612         }
 2613 
 2614         /*
 2615          * Try Perfect filtering first.
 2616          */
 2617 
 2618         sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
 2619         sp = TULIP_CDSP(sc);
 2620         memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
 2621         cnt = 0;
 2622         ETHER_FIRST_MULTI(step, ec, enm);
 2623         while (enm != NULL) {
 2624                 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
 2625                         /*
 2626                          * We must listen to a range of multicast addresses.
 2627                          * For now, just accept all multicasts, rather than
 2628                          * trying to set only those filter bits needed to match
 2629                          * the range.  (At this time, the only use of address
 2630                          * ranges is for IP multicast routing, for which the
 2631                          * range is big enough to require all bits set.)
 2632                          */
 2633                         goto allmulti;
 2634                 }
 2635                 if (cnt == (TULIP_MAXADDRS - 2)) {
 2636                         /*
 2637                          * We already have our multicast limit (still need
 2638                          * our station address and broadcast).  Go to
 2639                          * Hash-Perfect mode.
 2640                          */
 2641                         goto hashperfect;
 2642                 }
 2643                 cnt++;
 2644                 *sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 0);
 2645                 *sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 1);
 2646                 *sp++ = TULIP_SP_FIELD(enm->enm_addrlo, 2);
 2647                 ETHER_NEXT_MULTI(step, enm);
 2648         }
 2649         
 2650         if (ifp->if_flags & IFF_BROADCAST) {
 2651                 /* ...and the broadcast address. */
 2652                 cnt++;
 2653                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2654                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2655                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2656         }
 2657 
 2658         /* Pad the rest with our station address. */
 2659         for (; cnt < TULIP_MAXADDRS; cnt++) {
 2660                 *sp++ = TULIP_SP_FIELD(enaddr, 0);
 2661                 *sp++ = TULIP_SP_FIELD(enaddr, 1);
 2662                 *sp++ = TULIP_SP_FIELD(enaddr, 2);
 2663         }
 2664         ifp->if_flags &= ~IFF_ALLMULTI;
 2665         goto setit;
 2666 
 2667  hashperfect:
 2668         /*
 2669          * Try Hash-Perfect mode.
 2670          */
 2671 
 2672         /*
 2673          * Some 21140 chips have broken Hash-Perfect modes.  On these
 2674          * chips, we simply use Hash-Only mode, and put our station
 2675          * address into the filter.
 2676          */
 2677         if (sc->sc_chip == TULIP_CHIP_21140)
 2678                 sc->sc_filtmode = TDCTL_Tx_FT_HASHONLY;
 2679         else
 2680                 sc->sc_filtmode = TDCTL_Tx_FT_HASH;
 2681         sp = TULIP_CDSP(sc);
 2682         memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
 2683         ETHER_FIRST_MULTI(step, ec, enm);
 2684         while (enm != NULL) {
 2685                 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
 2686                         /*
 2687                          * We must listen to a range of multicast addresses.
 2688                          * For now, just accept all multicasts, rather than
 2689                          * trying to set only those filter bits needed to match
 2690                          * the range.  (At this time, the only use of address
 2691                          * ranges is for IP multicast routing, for which the
 2692                          * range is big enough to require all bits set.)
 2693                          */
 2694                         goto allmulti;
 2695                 }
 2696                 hash = tlp_mchash(enm->enm_addrlo, hashsize);
 2697                 sp[hash >> 4] |= htole32(1 << (hash & 0xf));
 2698                 ETHER_NEXT_MULTI(step, enm);
 2699         }
 2700 
 2701         if (ifp->if_flags & IFF_BROADCAST) {
 2702                 /* ...and the broadcast address. */
 2703                 hash = tlp_mchash(etherbroadcastaddr, hashsize);
 2704                 sp[hash >> 4] |= htole32(1 << (hash & 0xf));
 2705         }
 2706 
 2707         if (sc->sc_filtmode == TDCTL_Tx_FT_HASHONLY) {
 2708                 /* ...and our station address. */
 2709                 hash = tlp_mchash(enaddr, hashsize);
 2710                 sp[hash >> 4] |= htole32(1 << (hash & 0xf));
 2711         } else {
 2712                 /*
 2713                  * Hash-Perfect mode; put our station address after
 2714                  * the hash table.
 2715                  */
 2716                 sp[39] = TULIP_SP_FIELD(enaddr, 0);
 2717                 sp[40] = TULIP_SP_FIELD(enaddr, 1);
 2718                 sp[41] = TULIP_SP_FIELD(enaddr, 2);
 2719         }
 2720         ifp->if_flags &= ~IFF_ALLMULTI;
 2721         goto setit;
 2722 
 2723  allmulti:
 2724         /*
 2725          * Use Perfect filter mode.  First address is the broadcast address,
 2726          * and pad the rest with our station address.  We'll set Pass-all-
 2727          * multicast in OPMODE below.
 2728          */
 2729         sc->sc_filtmode = TDCTL_Tx_FT_PERFECT;
 2730         sp = TULIP_CDSP(sc);
 2731         memset(TULIP_CDSP(sc), 0, TULIP_SETUP_PACKET_LEN);
 2732         cnt = 0;
 2733         if (ifp->if_flags & IFF_BROADCAST) {
 2734                 cnt++;
 2735                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2736                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2737                 *sp++ = TULIP_SP_FIELD_C(0xffff);
 2738         }
 2739         for (; cnt < TULIP_MAXADDRS; cnt++) {
 2740                 *sp++ = TULIP_SP_FIELD(enaddr, 0);
 2741                 *sp++ = TULIP_SP_FIELD(enaddr, 1);
 2742                 *sp++ = TULIP_SP_FIELD(enaddr, 2);
 2743         }
 2744         ifp->if_flags |= IFF_ALLMULTI;
 2745 
 2746  setit:
 2747         if (ifp->if_flags & IFF_ALLMULTI)
 2748                 sc->sc_opmode |= OPMODE_PM;
 2749 
 2750         /* Sync the setup packet buffer. */
 2751         TULIP_CDSPSYNC(sc, BUS_DMASYNC_PREWRITE);
 2752 
 2753         /*
 2754          * Fill in the setup packet descriptor.
 2755          */
 2756         txs = SIMPLEQ_FIRST(&sc->sc_txfreeq);
 2757 
 2758         txs->txs_firstdesc = sc->sc_txnext;
 2759         txs->txs_lastdesc = sc->sc_txnext;
 2760         txs->txs_ndescs = 1;
 2761         txs->txs_mbuf = NULL;
 2762 
 2763         sc->sc_txdescs[sc->sc_txnext].td_bufaddr1 =
 2764             htole32(TULIP_CDSPADDR(sc));
 2765         sc->sc_txdescs[sc->sc_txnext].td_ctl =
 2766             htole32((TULIP_SETUP_PACKET_LEN << TDCTL_SIZE1_SHIFT) |
 2767             sc->sc_filtmode | TDCTL_Tx_SET | sc->sc_setup_fsls |
 2768             TDCTL_Tx_IC | sc->sc_tdctl_ch |
 2769             (sc->sc_txnext == (TULIP_NTXDESC - 1) ? sc->sc_tdctl_er : 0));
 2770         sc->sc_txdescs[sc->sc_txnext].td_status = htole32(TDSTAT_OWN);
 2771         TULIP_CDTXSYNC(sc, sc->sc_txnext, txs->txs_ndescs,
 2772             BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
 2773 
 2774         /* Advance the tx pointer. */
 2775         sc->sc_txfree -= 1;
 2776         sc->sc_txnext = TULIP_NEXTTX(sc->sc_txnext);
 2777 
 2778         SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
 2779         SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
 2780 
 2781         /*
 2782          * Set the OPMODE register.  This will also resume the
 2783          * transmit transmit process we idled above.
 2784          */
 2785         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 2786 
 2787         sc->sc_flags |= TULIPF_DOING_SETUP;
 2788 
 2789         /*
 2790          * Kick the transmitter; this will cause the Tulip to
 2791          * read the setup descriptor.
 2792          */
 2793         /* XXX USE AUTOPOLLING? */
 2794         TULIP_WRITE(sc, CSR_TXPOLL, TXPOLL_TPD);
 2795 
 2796         /* Set up a watchdog timer in case the chip flakes out. */
 2797         ifp->if_timer = 5;
 2798 
 2799         DPRINTF(sc, ("%s: tlp_filter_setup: returning\n", sc->sc_dev.dv_xname));
 2800 }
 2801 
 2802 /*
 2803  * tlp_winb_filter_setup:
 2804  *
 2805  *      Set the Winbond 89C840F's receive filter.
 2806  */
 2807 void
 2808 tlp_winb_filter_setup(sc)
 2809         struct tulip_softc *sc;
 2810 {
 2811         struct ethercom *ec = &sc->sc_ethercom;
 2812         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 2813         struct ether_multi *enm;
 2814         struct ether_multistep step;
 2815         u_int32_t hash, mchash[2];
 2816 
 2817         DPRINTF(sc, ("%s: tlp_winb_filter_setup: sc_flags 0x%08x\n",
 2818             sc->sc_dev.dv_xname, sc->sc_flags));
 2819 
 2820         sc->sc_opmode &= ~(OPMODE_WINB_APP|OPMODE_WINB_AMP|OPMODE_WINB_ABP);
 2821 
 2822         if (ifp->if_flags & IFF_MULTICAST)
 2823                 sc->sc_opmode |= OPMODE_WINB_AMP;
 2824 
 2825         if (ifp->if_flags & IFF_BROADCAST)
 2826                 sc->sc_opmode |= OPMODE_WINB_ABP;
 2827 
 2828         if (ifp->if_flags & IFF_PROMISC) {
 2829                 sc->sc_opmode |= OPMODE_WINB_APP;
 2830                 goto allmulti;
 2831         }
 2832 
 2833         mchash[0] = mchash[1] = 0;
 2834 
 2835         ETHER_FIRST_MULTI(step, ec, enm);
 2836         while (enm != NULL) {
 2837                 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
 2838                         /*
 2839                          * We must listen to a range of multicast addresses.
 2840                          * For now, just accept all multicasts, rather than
 2841                          * trying to set only those filter bits needed to match
 2842                          * the range.  (At this time, the only use of address
 2843                          * ranges is for IP multicast routing, for which the
 2844                          * range is big enough to require all bits set.)
 2845                          */
 2846                         goto allmulti;
 2847                 }
 2848 
 2849                 /*
 2850                  * According to the FreeBSD `wb' driver, yes, you
 2851                  * really do invert the hash.
 2852                  */
 2853                 hash =
 2854                     (~(ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) >> 26))
 2855                     & 0x3f;
 2856                 mchash[hash >> 5] |= 1 << (hash & 0x1f);
 2857                 ETHER_NEXT_MULTI(step, enm);
 2858         }
 2859         ifp->if_flags &= ~IFF_ALLMULTI;
 2860         goto setit;
 2861 
 2862  allmulti:
 2863         ifp->if_flags |= IFF_ALLMULTI;
 2864         mchash[0] = mchash[1] = 0xffffffff;
 2865 
 2866  setit:
 2867         TULIP_WRITE(sc, CSR_WINB_CMA0, mchash[0]);
 2868         TULIP_WRITE(sc, CSR_WINB_CMA1, mchash[1]);
 2869         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 2870         DPRINTF(sc, ("%s: tlp_winb_filter_setup: returning\n",
 2871             sc->sc_dev.dv_xname));
 2872 }
 2873 
 2874 /*
 2875  * tlp_al981_filter_setup:
 2876  *
 2877  *      Set the ADMtek AL981's receive filter.
 2878  */
 2879 void
 2880 tlp_al981_filter_setup(sc)
 2881         struct tulip_softc *sc;
 2882 {
 2883         struct ethercom *ec = &sc->sc_ethercom;
 2884         struct ifnet *ifp = &sc->sc_ethercom.ec_if; 
 2885         struct ether_multi *enm;
 2886         struct ether_multistep step;
 2887         u_int32_t hash, mchash[2];
 2888 
 2889         /*
 2890          * If the chip is running, we need to reset the interface,
 2891          * and will revisit here (with IFF_RUNNING) clear.  The
 2892          * chip seems to really not like to have its multicast
 2893          * filter programmed without a reset.
 2894          */
 2895         if (ifp->if_flags & IFF_RUNNING) {
 2896                 (void) tlp_init(ifp);
 2897                 return;
 2898         }
 2899 
 2900         DPRINTF(sc, ("%s: tlp_al981_filter_setup: sc_flags 0x%08x\n",
 2901             sc->sc_dev.dv_xname, sc->sc_flags));
 2902 
 2903         sc->sc_opmode &= ~(OPMODE_PR|OPMODE_PM);
 2904 
 2905         if (ifp->if_flags & IFF_PROMISC) {
 2906                 sc->sc_opmode |= OPMODE_PR;
 2907                 goto allmulti;
 2908         }
 2909 
 2910         mchash[0] = mchash[1] = 0;
 2911 
 2912         ETHER_FIRST_MULTI(step, ec, enm);
 2913         while (enm != NULL) {
 2914                 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
 2915                         /*
 2916                          * We must listen to a range of multicast addresses.
 2917                          * For now, just accept all multicasts, rather than
 2918                          * trying to set only those filter bits needed to match
 2919                          * the range.  (At this time, the only use of address
 2920                          * ranges is for IP multicast routing, for which the
 2921                          * range is big enough to require all bits set.)
 2922                          */
 2923                         goto allmulti;
 2924                 }
 2925 
 2926                 hash = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN) & 0x3f;
 2927                 mchash[hash >> 5] |= 1 << (hash & 0x1f);
 2928                 ETHER_NEXT_MULTI(step, enm);
 2929         }
 2930         ifp->if_flags &= ~IFF_ALLMULTI;
 2931         goto setit;
 2932 
 2933  allmulti:
 2934         ifp->if_flags |= IFF_ALLMULTI;
 2935         mchash[0] = mchash[1] = 0xffffffff;
 2936 
 2937  setit:
 2938         bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR0, mchash[0]);
 2939         bus_space_write_4(sc->sc_st, sc->sc_sh, CSR_ADM_MAR1, mchash[1]);
 2940         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 2941         DPRINTF(sc, ("%s: tlp_al981_filter_setup: returning\n",
 2942             sc->sc_dev.dv_xname));
 2943 }
 2944 
 2945 /*
 2946  * tlp_idle:
 2947  *
 2948  *      Cause the transmit and/or receive processes to go idle.
 2949  */
 2950 void
 2951 tlp_idle(sc, bits)
 2952         struct tulip_softc *sc;
 2953         u_int32_t bits;
 2954 {
 2955         static const char * const tlp_tx_state_names[] = {
 2956                 "STOPPED",
 2957                 "RUNNING - FETCH",
 2958                 "RUNNING - WAIT",
 2959                 "RUNNING - READING",
 2960                 "-- RESERVED --",
 2961                 "RUNNING - SETUP",
 2962                 "SUSPENDED",
 2963                 "RUNNING - CLOSE",
 2964         };
 2965         static const char * const tlp_rx_state_names[] = {
 2966                 "STOPPED",
 2967                 "RUNNING - FETCH",
 2968                 "RUNNING - CHECK",
 2969                 "RUNNING - WAIT",
 2970                 "SUSPENDED",
 2971                 "RUNNING - CLOSE",
 2972                 "RUNNING - FLUSH",
 2973                 "RUNNING - QUEUE",
 2974         };
 2975         static const char * const dm9102_tx_state_names[] = {
 2976                 "STOPPED",
 2977                 "RUNNING - FETCH",
 2978                 "RUNNING - SETUP",
 2979                 "RUNNING - READING",
 2980                 "RUNNING - CLOSE - CLEAR OWNER",
 2981                 "RUNNING - WAIT",
 2982                 "RUNNING - CLOSE - WRITE STATUS",
 2983                 "SUSPENDED",
 2984         };
 2985         static const char * const dm9102_rx_state_names[] = {
 2986                 "STOPPED",
 2987                 "RUNNING - FETCH",
 2988                 "RUNNING - WAIT",
 2989                 "RUNNING - QUEUE",
 2990                 "RUNNING - CLOSE - CLEAR OWNER",
 2991                 "RUNNING - CLOSE - WRITE STATUS",
 2992                 "SUSPENDED",
 2993                 "RUNNING - FLUSH",
 2994         };
 2995 
 2996         const char * const *tx_state_names, * const *rx_state_names;
 2997         u_int32_t csr, ackmask = 0;
 2998         int i;
 2999 
 3000         switch (sc->sc_chip) {
 3001         case TULIP_CHIP_DM9102:
 3002         case TULIP_CHIP_DM9102A:
 3003                 tx_state_names = dm9102_tx_state_names;
 3004                 rx_state_names = dm9102_rx_state_names;
 3005                 break;
 3006 
 3007         default:
 3008                 tx_state_names = tlp_tx_state_names;
 3009                 rx_state_names = tlp_rx_state_names;
 3010                 break;
 3011         }
 3012 
 3013         if (bits & OPMODE_ST)
 3014                 ackmask |= STATUS_TPS;
 3015 
 3016         if (bits & OPMODE_SR)
 3017                 ackmask |= STATUS_RPS;
 3018 
 3019         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode & ~bits);
 3020 
 3021         for (i = 0; i < 1000; i++) {
 3022                 if (TULIP_ISSET(sc, CSR_STATUS, ackmask) == ackmask)
 3023                         break;
 3024                 delay(10);
 3025         }
 3026 
 3027         csr = TULIP_READ(sc, CSR_STATUS);
 3028         if ((csr & ackmask) != ackmask) {
 3029                 if ((bits & OPMODE_ST) != 0 && (csr & STATUS_TPS) == 0 &&
 3030                     (csr & STATUS_TS) != STATUS_TS_STOPPED) {
 3031                         printf("%s: transmit process failed to idle: "
 3032                             "state %s\n", sc->sc_dev.dv_xname,
 3033                             tx_state_names[(csr & STATUS_TS) >> 20]);
 3034                 }
 3035                 if ((bits & OPMODE_SR) != 0 && (csr & STATUS_RPS) == 0 &&
 3036                     (csr & STATUS_RS) != STATUS_RS_STOPPED) {
 3037                         switch (sc->sc_chip) {
 3038                         case TULIP_CHIP_AN983:
 3039                         case TULIP_CHIP_AN985:
 3040                         case TULIP_CHIP_DM9102A:
 3041                                 /*
 3042                                  * Filter the message out on noisy chips.
 3043                                  */
 3044                                 break;
 3045                         default:
 3046                                 printf("%s: receive process failed to idle: "
 3047                                     "state %s\n", sc->sc_dev.dv_xname,
 3048                                     rx_state_names[(csr & STATUS_RS) >> 17]);
 3049                         }
 3050                 }
 3051         }
 3052         TULIP_WRITE(sc, CSR_STATUS, ackmask);
 3053 }
 3054 
 3055 /*****************************************************************************
 3056  * Generic media support functions.
 3057  *****************************************************************************/
 3058 
 3059 /*
 3060  * tlp_mediastatus:     [ifmedia interface function]
 3061  *
 3062  *      Query the current media.
 3063  */
 3064 void
 3065 tlp_mediastatus(ifp, ifmr)
 3066         struct ifnet *ifp;
 3067         struct ifmediareq *ifmr;
 3068 {
 3069         struct tulip_softc *sc = ifp->if_softc;
 3070 
 3071         if (TULIP_IS_ENABLED(sc) == 0) {
 3072                 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
 3073                 ifmr->ifm_status = 0;
 3074                 return;
 3075         }
 3076 
 3077         (*sc->sc_mediasw->tmsw_get)(sc, ifmr);
 3078 }
 3079 
 3080 /*
 3081  * tlp_mediachange:     [ifmedia interface function]
 3082  *
 3083  *      Update the current media.
 3084  */
 3085 int
 3086 tlp_mediachange(ifp)
 3087         struct ifnet *ifp;
 3088 {
 3089         struct tulip_softc *sc = ifp->if_softc;
 3090 
 3091         if ((ifp->if_flags & IFF_UP) == 0)
 3092                 return (0);
 3093         return ((*sc->sc_mediasw->tmsw_set)(sc));
 3094 }
 3095 
 3096 /*****************************************************************************
 3097  * Support functions for MII-attached media.
 3098  *****************************************************************************/
 3099 
 3100 /*
 3101  * tlp_mii_tick:
 3102  *
 3103  *      One second timer, used to tick the MII.
 3104  */
 3105 void
 3106 tlp_mii_tick(arg)
 3107         void *arg;
 3108 {
 3109         struct tulip_softc *sc = arg;
 3110         int s;
 3111 
 3112         if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 3113                 return;
 3114 
 3115         s = splnet();
 3116         mii_tick(&sc->sc_mii);
 3117         splx(s);
 3118 
 3119         callout_reset(&sc->sc_tick_callout, hz, sc->sc_tick, sc);
 3120 }
 3121 
 3122 /*
 3123  * tlp_mii_statchg:     [mii interface function]
 3124  *
 3125  *      Callback from PHY when media changes.
 3126  */
 3127 void
 3128 tlp_mii_statchg(self)
 3129         struct device *self;
 3130 {
 3131         struct tulip_softc *sc = (struct tulip_softc *)self;
 3132 
 3133         /* Idle the transmit and receive processes. */
 3134         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 3135 
 3136         sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_HBD);
 3137 
 3138         if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T)
 3139                 sc->sc_opmode |= OPMODE_TTM;
 3140         else
 3141                 sc->sc_opmode |= OPMODE_HBD;
 3142 
 3143         if (sc->sc_mii.mii_media_active & IFM_FDX)
 3144                 sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
 3145 
 3146         /*
 3147          * Write new OPMODE bits.  This also restarts the transmit
 3148          * and receive processes.
 3149          */
 3150         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3151 }
 3152 
 3153 /*
 3154  * tlp_winb_mii_statchg: [mii interface function]
 3155  *
 3156  *      Callback from PHY when media changes.  This version is
 3157  *      for the Winbond 89C840F, which has different OPMODE bits.
 3158  */
 3159 void
 3160 tlp_winb_mii_statchg(self)
 3161         struct device *self;
 3162 {
 3163         struct tulip_softc *sc = (struct tulip_softc *)self;
 3164 
 3165         /* Idle the transmit and receive processes. */
 3166         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 3167 
 3168         sc->sc_opmode &= ~(OPMODE_WINB_FES|OPMODE_FD);
 3169 
 3170         if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_100_TX)
 3171                 sc->sc_opmode |= OPMODE_WINB_FES;
 3172 
 3173         if (sc->sc_mii.mii_media_active & IFM_FDX)
 3174                 sc->sc_opmode |= OPMODE_FD;
 3175 
 3176         /*
 3177          * Write new OPMODE bits.  This also restarts the transmit
 3178          * and receive processes.
 3179          */
 3180         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3181 }
 3182 
 3183 /*
 3184  * tlp_dm9102_mii_statchg: [mii interface function]
 3185  *
 3186  *      Callback from PHY when media changes.  This version is
 3187  *      for the DM9102.
 3188  */
 3189 void
 3190 tlp_dm9102_mii_statchg(self)
 3191         struct device *self;
 3192 {
 3193         struct tulip_softc *sc = (struct tulip_softc *)self;
 3194 
 3195         /*
 3196          * Don't idle the transmit and receive processes, here.  It
 3197          * seems to fail, and just causes excess noise.
 3198          */
 3199         sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD);
 3200 
 3201         if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_100_TX)
 3202                 sc->sc_opmode |= OPMODE_TTM;
 3203 
 3204         if (sc->sc_mii.mii_media_active & IFM_FDX)
 3205                 sc->sc_opmode |= OPMODE_FD;
 3206 
 3207         /*
 3208          * Write new OPMODE bits.
 3209          */
 3210         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3211 }
 3212 
 3213 /*
 3214  * tlp_mii_getmedia:
 3215  *
 3216  *      Callback from ifmedia to request current media status.
 3217  */
 3218 void
 3219 tlp_mii_getmedia(sc, ifmr)
 3220         struct tulip_softc *sc;
 3221         struct ifmediareq *ifmr;
 3222 {
 3223 
 3224         mii_pollstat(&sc->sc_mii);
 3225         ifmr->ifm_status = sc->sc_mii.mii_media_status;
 3226         ifmr->ifm_active = sc->sc_mii.mii_media_active;
 3227 }
 3228 
 3229 /*
 3230  * tlp_mii_setmedia:
 3231  *
 3232  *      Callback from ifmedia to request new media setting.
 3233  */
 3234 int
 3235 tlp_mii_setmedia(sc)
 3236         struct tulip_softc *sc;
 3237 {
 3238         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 3239 
 3240         if (ifp->if_flags & IFF_UP) {
 3241                 switch (sc->sc_chip) {
 3242                 case TULIP_CHIP_21142:
 3243                 case TULIP_CHIP_21143:
 3244                         /* Disable the internal Nway engine. */
 3245                         TULIP_WRITE(sc, CSR_SIATXRX, 0);
 3246                         break;
 3247 
 3248                 default:
 3249                         /* Nothing. */
 3250                         break;
 3251                 }
 3252                 mii_mediachg(&sc->sc_mii);
 3253         }
 3254         return (0);
 3255 }
 3256 
 3257 /*
 3258  * tlp_bitbang_mii_readreg:
 3259  *
 3260  *      Read a PHY register via bit-bang'ing the MII.
 3261  */
 3262 int
 3263 tlp_bitbang_mii_readreg(self, phy, reg)
 3264         struct device *self;
 3265         int phy, reg;
 3266 {
 3267         struct tulip_softc *sc = (void *) self;
 3268 
 3269         return (mii_bitbang_readreg(self, sc->sc_bitbang_ops, phy, reg));
 3270 }
 3271 
 3272 /*
 3273  * tlp_bitbang_mii_writereg:
 3274  *
 3275  *      Write a PHY register via bit-bang'ing the MII.
 3276  */
 3277 void
 3278 tlp_bitbang_mii_writereg(self, phy, reg, val)
 3279         struct device *self;
 3280         int phy, reg, val;
 3281 {
 3282         struct tulip_softc *sc = (void *) self;
 3283 
 3284         mii_bitbang_writereg(self, sc->sc_bitbang_ops, phy, reg, val);
 3285 }
 3286 
 3287 /*
 3288  * tlp_sio_mii_bitbang_read:
 3289  *
 3290  *      Read the MII serial port for the MII bit-bang module.
 3291  */
 3292 u_int32_t
 3293 tlp_sio_mii_bitbang_read(self)
 3294         struct device *self;
 3295 {
 3296         struct tulip_softc *sc = (void *) self;
 3297 
 3298         return (TULIP_READ(sc, CSR_MIIROM));
 3299 }
 3300 
 3301 /*
 3302  * tlp_sio_mii_bitbang_write:
 3303  *
 3304  *      Write the MII serial port for the MII bit-bang module.
 3305  */
 3306 void
 3307 tlp_sio_mii_bitbang_write(self, val)
 3308         struct device *self;
 3309         u_int32_t val;
 3310 {
 3311         struct tulip_softc *sc = (void *) self;
 3312 
 3313         TULIP_WRITE(sc, CSR_MIIROM, val);
 3314 }
 3315 
 3316 /*
 3317  * tlp_pnic_mii_readreg:
 3318  *
 3319  *      Read a PHY register on the Lite-On PNIC.
 3320  */
 3321 int
 3322 tlp_pnic_mii_readreg(self, phy, reg)
 3323         struct device *self;
 3324         int phy, reg;
 3325 {
 3326         struct tulip_softc *sc = (void *) self;
 3327         u_int32_t val;
 3328         int i;
 3329 
 3330         TULIP_WRITE(sc, CSR_PNIC_MII,
 3331             PNIC_MII_MBO | PNIC_MII_RESERVED |
 3332             PNIC_MII_READ | (phy << PNIC_MII_PHYSHIFT) |
 3333             (reg << PNIC_MII_REGSHIFT));
 3334 
 3335         for (i = 0; i < 1000; i++) {
 3336                 delay(10);
 3337                 val = TULIP_READ(sc, CSR_PNIC_MII);
 3338                 if ((val & PNIC_MII_BUSY) == 0) {
 3339                         if ((val & PNIC_MII_DATA) == PNIC_MII_DATA)
 3340                                 return (0);
 3341                         else
 3342                                 return (val & PNIC_MII_DATA);
 3343                 }
 3344         }
 3345         printf("%s: MII read timed out\n", sc->sc_dev.dv_xname);
 3346         return (0);
 3347 }
 3348 
 3349 /*
 3350  * tlp_pnic_mii_writereg:
 3351  *
 3352  *      Write a PHY register on the Lite-On PNIC.
 3353  */
 3354 void
 3355 tlp_pnic_mii_writereg(self, phy, reg, val)
 3356         struct device *self;
 3357         int phy, reg, val;
 3358 {
 3359         struct tulip_softc *sc = (void *) self;
 3360         int i;
 3361 
 3362         TULIP_WRITE(sc, CSR_PNIC_MII,
 3363             PNIC_MII_MBO | PNIC_MII_RESERVED |
 3364             PNIC_MII_WRITE | (phy << PNIC_MII_PHYSHIFT) |
 3365             (reg << PNIC_MII_REGSHIFT) | val);
 3366 
 3367         for (i = 0; i < 1000; i++) {
 3368                 delay(10);
 3369                 if (TULIP_ISSET(sc, CSR_PNIC_MII, PNIC_MII_BUSY) == 0)
 3370                         return;
 3371         }
 3372         printf("%s: MII write timed out\n", sc->sc_dev.dv_xname);
 3373 }
 3374 
 3375 const bus_addr_t tlp_al981_phy_regmap[] = {
 3376         CSR_ADM_BMCR,
 3377         CSR_ADM_BMSR,
 3378         CSR_ADM_PHYIDR1,
 3379         CSR_ADM_PHYIDR2,
 3380         CSR_ADM_ANAR,
 3381         CSR_ADM_ANLPAR,
 3382         CSR_ADM_ANER,
 3383 
 3384         CSR_ADM_XMC,
 3385         CSR_ADM_XCIIS,
 3386         CSR_ADM_XIE,
 3387         CSR_ADM_100CTR,
 3388 };
 3389 const int tlp_al981_phy_regmap_size = sizeof(tlp_al981_phy_regmap) /
 3390     sizeof(tlp_al981_phy_regmap[0]);
 3391 
 3392 /*
 3393  * tlp_al981_mii_readreg:
 3394  *
 3395  *      Read a PHY register on the ADMtek AL981.
 3396  */
 3397 int
 3398 tlp_al981_mii_readreg(self, phy, reg)
 3399         struct device *self;
 3400         int phy, reg;
 3401 {
 3402         struct tulip_softc *sc = (struct tulip_softc *)self;
 3403 
 3404         /* AL981 only has an internal PHY. */
 3405         if (phy != 0)
 3406                 return (0);
 3407 
 3408         if (reg >= tlp_al981_phy_regmap_size)
 3409                 return (0);
 3410 
 3411         return (bus_space_read_4(sc->sc_st, sc->sc_sh,
 3412             tlp_al981_phy_regmap[reg]) & 0xffff);
 3413 }
 3414 
 3415 /*
 3416  * tlp_al981_mii_writereg:
 3417  *
 3418  *      Write a PHY register on the ADMtek AL981.
 3419  */
 3420 void
 3421 tlp_al981_mii_writereg(self, phy, reg, val)
 3422         struct device *self;
 3423         int phy, reg, val;
 3424 {
 3425         struct tulip_softc *sc = (struct tulip_softc *)self;
 3426 
 3427         /* AL981 only has an internal PHY. */
 3428         if (phy != 0)
 3429                 return;
 3430 
 3431         if (reg >= tlp_al981_phy_regmap_size)
 3432                 return;
 3433 
 3434         bus_space_write_4(sc->sc_st, sc->sc_sh,
 3435             tlp_al981_phy_regmap[reg], val);
 3436 }
 3437 
 3438 /*****************************************************************************
 3439  * Chip-specific pre-init and reset functions.
 3440  *****************************************************************************/
 3441 
 3442 /*
 3443  * tlp_2114x_preinit:
 3444  *
 3445  *      Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
 3446  */
 3447 void
 3448 tlp_2114x_preinit(sc)
 3449         struct tulip_softc *sc;
 3450 {
 3451         struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
 3452         struct tulip_21x4x_media *tm = ife->ifm_aux;
 3453 
 3454         /*
 3455          * Whether or not we're in MII or SIA/SYM mode, the media info
 3456          * contains the appropriate OPMODE bits.
 3457          *
 3458          * Also, we always set the Must-Be-One bit.
 3459          */
 3460         sc->sc_opmode |= OPMODE_MBO | tm->tm_opmode;
 3461 
 3462         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3463 }
 3464 
 3465 /*
 3466  * tlp_2114x_mii_preinit:
 3467  *
 3468  *      Pre-init function shared by DECchip 21140, 21140A, 21142, and 21143.
 3469  *      This version is used by boards which only have MII and don't have
 3470  *      an ISV SROM.
 3471  */
 3472 void
 3473 tlp_2114x_mii_preinit(sc)
 3474         struct tulip_softc *sc;
 3475 {
 3476 
 3477         /*
 3478          * Always set the Must-Be-One bit, and Port Select (to select MII).
 3479          * We'll never be called during a media change.
 3480          */
 3481         sc->sc_opmode |= OPMODE_MBO|OPMODE_PS;
 3482         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3483 }
 3484 
 3485 /*
 3486  * tlp_pnic_preinit:
 3487  *
 3488  *      Pre-init function for the Lite-On 82c168 and 82c169.
 3489  */
 3490 void
 3491 tlp_pnic_preinit(sc)
 3492         struct tulip_softc *sc;
 3493 {
 3494 
 3495         if (sc->sc_flags & TULIPF_HAS_MII) {
 3496                 /*
 3497                  * MII case: just set the port-select bit; we will never
 3498                  * be called during a media change.
 3499                  */
 3500                 sc->sc_opmode |= OPMODE_PS;
 3501         } else {
 3502                 /*
 3503                  * ENDEC/PCS/Nway mode; enable the Tx backoff counter.
 3504                  */
 3505                 sc->sc_opmode |= OPMODE_PNIC_TBEN;
 3506         }
 3507 }
 3508 
 3509 /*
 3510  * tlp_dm9102_preinit:
 3511  *
 3512  *      Pre-init function for the Davicom DM9102.
 3513  */
 3514 void
 3515 tlp_dm9102_preinit(sc)
 3516         struct tulip_softc *sc;
 3517 {
 3518 
 3519         switch (sc->sc_chip) {
 3520         case TULIP_CHIP_DM9102:
 3521                 sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
 3522                 break;
 3523 
 3524         case TULIP_CHIP_DM9102A:
 3525                 /*
 3526                  * XXX Figure out how to actually deal with the HomePNA
 3527                  * XXX portion of the DM9102A.
 3528                  */
 3529                 sc->sc_opmode |= OPMODE_MBO|OPMODE_HBD;
 3530                 break;
 3531 
 3532         default:
 3533                 /* Nothing. */
 3534                 break;
 3535         }
 3536 
 3537         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 3538 }
 3539 
 3540 /*
 3541  * tlp_21140_reset:
 3542  *
 3543  *      Issue a reset sequence on the 21140 via the GPIO facility.
 3544  */
 3545 void
 3546 tlp_21140_reset(sc)
 3547         struct tulip_softc *sc;
 3548 {
 3549         struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
 3550         struct tulip_21x4x_media *tm = ife->ifm_aux;
 3551         int i;
 3552 
 3553         /* First, set the direction on the GPIO pins. */
 3554         TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
 3555 
 3556         /* Now, issue the reset sequence. */
 3557         for (i = 0; i < tm->tm_reset_length; i++) {
 3558                 delay(10);
 3559                 TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_reset_offset + i]);
 3560         }
 3561 
 3562         /* Now, issue the selection sequence. */
 3563         for (i = 0; i < tm->tm_gp_length; i++) {
 3564                 delay(10);
 3565                 TULIP_WRITE(sc, CSR_GPP, sc->sc_srom[tm->tm_gp_offset + i]);
 3566         }
 3567 
 3568         /* If there were no sequences, just lower the pins. */
 3569         if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
 3570                 delay(10);
 3571                 TULIP_WRITE(sc, CSR_GPP, 0);
 3572         }
 3573 }
 3574 
 3575 /*
 3576  * tlp_21142_reset:
 3577  *
 3578  *      Issue a reset sequence on the 21142 via the GPIO facility.
 3579  */
 3580 void
 3581 tlp_21142_reset(sc)
 3582         struct tulip_softc *sc;
 3583 {
 3584         struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
 3585         struct tulip_21x4x_media *tm = ife->ifm_aux;
 3586         const u_int8_t *cp;
 3587         int i;
 3588 
 3589         cp = &sc->sc_srom[tm->tm_reset_offset];
 3590         for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
 3591                 delay(10);
 3592                 TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
 3593         }
 3594 
 3595         cp = &sc->sc_srom[tm->tm_gp_offset];
 3596         for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
 3597                 delay(10);
 3598                 TULIP_WRITE(sc, CSR_SIAGEN, TULIP_ROM_GETW(cp, 0) << 16);
 3599         }
 3600 
 3601         /* If there were no sequences, just lower the pins. */
 3602         if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
 3603                 delay(10);
 3604                 TULIP_WRITE(sc, CSR_SIAGEN, 0);
 3605         }
 3606 }
 3607 
 3608 /*
 3609  * tlp_pmac_reset:
 3610  *
 3611  *      Reset routine for Macronix chips.
 3612  */
 3613 void
 3614 tlp_pmac_reset(sc)
 3615         struct tulip_softc *sc;
 3616 {
 3617 
 3618         switch (sc->sc_chip) {
 3619         case TULIP_CHIP_82C115:
 3620         case TULIP_CHIP_MX98715:
 3621         case TULIP_CHIP_MX98715A:
 3622         case TULIP_CHIP_MX98725:
 3623                 /*
 3624                  * Set the LED operating mode.  This information is located
 3625                  * in the EEPROM at byte offset 0x77, per the MX98715A and
 3626                  * MX98725 application notes.
 3627                  */
 3628                 TULIP_WRITE(sc, CSR_MIIROM, sc->sc_srom[0x77] << 24);
 3629                 break;
 3630         case TULIP_CHIP_MX98715AEC_X:
 3631                 /*
 3632                  * Set the LED operating mode.  This information is located
 3633                  * in the EEPROM at byte offset 0x76, per the MX98715AEC
 3634                  * application note.
 3635                  */
 3636                 TULIP_WRITE(sc, CSR_MIIROM, ((0xf & sc->sc_srom[0x76]) << 28)
 3637                     | ((0xf0 & sc->sc_srom[0x76]) << 20));
 3638                 break;
 3639 
 3640         default:
 3641                 /* Nothing. */
 3642                 break;
 3643         }
 3644 }
 3645 
 3646 /*
 3647  * tlp_dm9102_reset:
 3648  *
 3649  *      Reset routine for the Davicom DM9102.
 3650  */
 3651 void
 3652 tlp_dm9102_reset(sc)
 3653         struct tulip_softc *sc;
 3654 {
 3655 
 3656         TULIP_WRITE(sc, CSR_DM_PHYSTAT, DM_PHYSTAT_GEPC|DM_PHYSTAT_GPED);
 3657         delay(100);
 3658         TULIP_WRITE(sc, CSR_DM_PHYSTAT, 0);
 3659 }
 3660 
 3661 /*****************************************************************************
 3662  * Chip/board-specific media switches.  The ones here are ones that
 3663  * are potentially common to multiple front-ends.
 3664  *****************************************************************************/
 3665 
 3666 /*
 3667  * This table is a common place for all sorts of media information,
 3668  * keyed off of the SROM media code for that media.
 3669  *
 3670  * Note that we explicitly configure the 21142/21143 to always advertise
 3671  * NWay capabilities when using the UTP port.
 3672  * XXX Actually, we don't yet.
 3673  */
 3674 const struct tulip_srom_to_ifmedia tulip_srom_to_ifmedia_table[] = {
 3675         { TULIP_ROM_MB_MEDIA_TP,        IFM_10_T,       0,
 3676           "10baseT",
 3677           OPMODE_TTM,
 3678           BMSR_10THDX,
 3679           { SIACONN_21040_10BASET,
 3680             SIATXRX_21040_10BASET,
 3681             SIAGEN_21040_10BASET },
 3682 
 3683           { SIACONN_21041_10BASET,
 3684             SIATXRX_21041_10BASET,
 3685             SIAGEN_21041_10BASET },
 3686 
 3687           { SIACONN_21142_10BASET,
 3688             SIATXRX_21142_10BASET,
 3689             SIAGEN_21142_10BASET } },
 3690 
 3691         { TULIP_ROM_MB_MEDIA_BNC,       IFM_10_2,       0,
 3692           "10base2",
 3693           0,
 3694           0,
 3695           { 0,
 3696             0,
 3697             0 },
 3698 
 3699           { SIACONN_21041_BNC,
 3700             SIATXRX_21041_BNC,
 3701             SIAGEN_21041_BNC },
 3702 
 3703           { SIACONN_21142_BNC,
 3704             SIATXRX_21142_BNC,
 3705             SIAGEN_21142_BNC } },
 3706 
 3707         { TULIP_ROM_MB_MEDIA_AUI,       IFM_10_5,       0,
 3708           "10base5",
 3709           0,
 3710           0,
 3711           { SIACONN_21040_AUI,
 3712             SIATXRX_21040_AUI,
 3713             SIAGEN_21040_AUI },
 3714 
 3715           { SIACONN_21041_AUI,
 3716             SIATXRX_21041_AUI,
 3717             SIAGEN_21041_AUI },
 3718 
 3719           { SIACONN_21142_AUI,
 3720             SIATXRX_21142_AUI,
 3721             SIAGEN_21142_AUI } },
 3722 
 3723         { TULIP_ROM_MB_MEDIA_100TX,     IFM_100_TX,     0,
 3724           "100baseTX",
 3725           OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
 3726           BMSR_100TXHDX,
 3727           { 0,
 3728             0,
 3729             0 },
 3730           
 3731           { 0,
 3732             0,
 3733             0 },
 3734 
 3735           { 0,
 3736             0,
 3737             SIAGEN_ABM } },
 3738 
 3739         { TULIP_ROM_MB_MEDIA_TP_FDX,    IFM_10_T,       IFM_FDX,
 3740           "10baseT-FDX",
 3741           OPMODE_TTM|OPMODE_FD|OPMODE_HBD,
 3742           BMSR_10TFDX,
 3743           { SIACONN_21040_10BASET_FDX,
 3744             SIATXRX_21040_10BASET_FDX,
 3745             SIAGEN_21040_10BASET_FDX },
 3746 
 3747           { SIACONN_21041_10BASET_FDX,
 3748             SIATXRX_21041_10BASET_FDX,
 3749             SIAGEN_21041_10BASET_FDX },
 3750 
 3751           { SIACONN_21142_10BASET_FDX,
 3752             SIATXRX_21142_10BASET_FDX,
 3753             SIAGEN_21142_10BASET_FDX } },
 3754 
 3755         { TULIP_ROM_MB_MEDIA_100TX_FDX, IFM_100_TX,     IFM_FDX,
 3756           "100baseTX-FDX",
 3757           OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD|OPMODE_HBD,
 3758           BMSR_100TXFDX,
 3759           { 0,
 3760             0,
 3761             0 },
 3762 
 3763           { 0,
 3764             0,
 3765             0 },
 3766 
 3767           { 0,
 3768             0,
 3769             SIAGEN_ABM } },
 3770 
 3771         { TULIP_ROM_MB_MEDIA_100T4,     IFM_100_T4,     0,
 3772           "100baseT4",
 3773           OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD,
 3774           BMSR_100T4,
 3775           { 0,
 3776             0,
 3777             0 },
 3778 
 3779           { 0,
 3780             0,
 3781             0 },
 3782 
 3783           { 0,
 3784             0,
 3785             SIAGEN_ABM } },
 3786 
 3787         { TULIP_ROM_MB_MEDIA_100FX,     IFM_100_FX,     0,
 3788           "100baseFX",
 3789           OPMODE_PS|OPMODE_PCS|OPMODE_HBD,
 3790           0,
 3791           { 0,
 3792             0,
 3793             0 },
 3794 
 3795           { 0,
 3796             0,
 3797             0 },
 3798 
 3799           { 0,
 3800             0,
 3801             SIAGEN_ABM } },
 3802 
 3803         { TULIP_ROM_MB_MEDIA_100FX_FDX, IFM_100_FX,     IFM_FDX,
 3804           "100baseFX-FDX",
 3805           OPMODE_PS|OPMODE_PCS|OPMODE_FD|OPMODE_HBD,
 3806           0,
 3807           { 0,
 3808             0,
 3809             0 },
 3810 
 3811           { 0,
 3812             0,
 3813             0 },
 3814 
 3815           { 0,
 3816             0,
 3817             SIAGEN_ABM } },
 3818 
 3819         { 0,                            0,              0,
 3820           NULL,
 3821           0,
 3822           0,
 3823           { 0,
 3824             0,
 3825             0 },
 3826 
 3827           { 0,
 3828             0,
 3829             0 },
 3830 
 3831           { 0,
 3832             0,
 3833             0 } },
 3834 };
 3835 
 3836 const struct tulip_srom_to_ifmedia *tlp_srom_to_ifmedia __P((u_int8_t));
 3837 void    tlp_srom_media_info __P((struct tulip_softc *,
 3838             const struct tulip_srom_to_ifmedia *, struct tulip_21x4x_media *));
 3839 void    tlp_add_srom_media __P((struct tulip_softc *, int,
 3840             void (*)(struct tulip_softc *, struct ifmediareq *),
 3841             int (*)(struct tulip_softc *), const u_int8_t *, int));
 3842 void    tlp_print_media __P((struct tulip_softc *));
 3843 void    tlp_nway_activate __P((struct tulip_softc *, int));
 3844 void    tlp_get_minst __P((struct tulip_softc *));
 3845 
 3846 const struct tulip_srom_to_ifmedia *
 3847 tlp_srom_to_ifmedia(sm)
 3848         u_int8_t sm;
 3849 {
 3850         const struct tulip_srom_to_ifmedia *tsti;
 3851 
 3852         for (tsti = tulip_srom_to_ifmedia_table;
 3853              tsti->tsti_name != NULL; tsti++) {
 3854                 if (tsti->tsti_srom == sm)
 3855                         return (tsti);
 3856         }
 3857 
 3858         return (NULL);
 3859 }
 3860 
 3861 void
 3862 tlp_srom_media_info(sc, tsti, tm)
 3863         struct tulip_softc *sc;
 3864         const struct tulip_srom_to_ifmedia *tsti;
 3865         struct tulip_21x4x_media *tm;
 3866 {
 3867 
 3868         tm->tm_name = tsti->tsti_name;
 3869         tm->tm_opmode = tsti->tsti_opmode;
 3870 
 3871         sc->sc_sia_cap |= tsti->tsti_sia_cap;
 3872 
 3873         switch (sc->sc_chip) {
 3874         case TULIP_CHIP_DE425:
 3875         case TULIP_CHIP_21040:
 3876                 tm->tm_sia = tsti->tsti_21040;  /* struct assignment */
 3877                 break;
 3878 
 3879         case TULIP_CHIP_21041:
 3880                 tm->tm_sia = tsti->tsti_21041;  /* struct assignment */
 3881                 break;
 3882 
 3883         case TULIP_CHIP_21142:
 3884         case TULIP_CHIP_21143:
 3885         case TULIP_CHIP_82C115:
 3886         case TULIP_CHIP_MX98715:
 3887         case TULIP_CHIP_MX98715A:
 3888         case TULIP_CHIP_MX98715AEC_X:
 3889         case TULIP_CHIP_MX98725:
 3890                 tm->tm_sia = tsti->tsti_21142;  /* struct assignment */
 3891                 break;
 3892 
 3893         default:
 3894                 /* Nothing. */
 3895                 break;
 3896         }
 3897 }
 3898 
 3899 void
 3900 tlp_add_srom_media(sc, type, get, set, list, cnt)
 3901         struct tulip_softc *sc;
 3902         int type;
 3903         void (*get) __P((struct tulip_softc *, struct ifmediareq *));
 3904         int (*set) __P((struct tulip_softc *));
 3905         const u_int8_t *list;
 3906         int cnt;
 3907 {
 3908         struct tulip_21x4x_media *tm;
 3909         const struct tulip_srom_to_ifmedia *tsti;
 3910         int i;
 3911 
 3912         for (i = 0; i < cnt; i++) {
 3913                 tsti = tlp_srom_to_ifmedia(list[i]);
 3914                 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 3915                 tlp_srom_media_info(sc, tsti, tm);
 3916                 tm->tm_type = type;
 3917                 tm->tm_get = get;
 3918                 tm->tm_set = set;
 3919 
 3920                 ifmedia_add(&sc->sc_mii.mii_media,
 3921                     IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
 3922                     tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
 3923         }
 3924 }
 3925 
 3926 void
 3927 tlp_print_media(sc)
 3928         struct tulip_softc *sc;
 3929 {
 3930         struct ifmedia_entry *ife;
 3931         struct tulip_21x4x_media *tm;
 3932         const char *sep = "";
 3933 
 3934 #define PRINT(str)      printf("%s%s", sep, str); sep = ", "
 3935 
 3936         printf("%s: ", sc->sc_dev.dv_xname);
 3937         for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
 3938              ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
 3939                 tm = ife->ifm_aux;
 3940                 if (tm == NULL) {
 3941 #ifdef DIAGNOSTIC
 3942                         if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
 3943                                 panic("tlp_print_media");
 3944 #endif
 3945                         PRINT("auto");
 3946                 } else if (tm->tm_type != TULIP_ROM_MB_21140_MII &&
 3947                            tm->tm_type != TULIP_ROM_MB_21142_MII) {
 3948                         PRINT(tm->tm_name);
 3949                 }
 3950         }
 3951         printf("\n");
 3952 
 3953 #undef PRINT
 3954 }
 3955 
 3956 void
 3957 tlp_nway_activate(sc, media)
 3958         struct tulip_softc *sc;
 3959         int media;
 3960 {
 3961         struct ifmedia_entry *ife;
 3962 
 3963         ife = ifmedia_match(&sc->sc_mii.mii_media, media, 0);
 3964 #ifdef DIAGNOSTIC
 3965         if (ife == NULL)
 3966                 panic("tlp_nway_activate");
 3967 #endif
 3968         sc->sc_nway_active = ife;
 3969 }
 3970 
 3971 void
 3972 tlp_get_minst(sc)
 3973         struct tulip_softc *sc;
 3974 {
 3975 
 3976         if ((sc->sc_media_seen &
 3977             ~((1 << TULIP_ROM_MB_21140_MII) |
 3978               (1 << TULIP_ROM_MB_21142_MII))) == 0) {
 3979                 /*
 3980                  * We have not yet seen any SIA/SYM media (but are
 3981                  * about to; that's why we're called!), so assign
 3982                  * the current media instance to be the `internal media'
 3983                  * instance, and advance it so any MII media gets a
 3984                  * fresh one (used to selecting/isolating a PHY).
 3985                  */
 3986                 sc->sc_tlp_minst = sc->sc_mii.mii_instance++;
 3987         }
 3988 }
 3989 
 3990 /*
 3991  * SIA Utility functions.
 3992  */
 3993 void    tlp_sia_update_link __P((struct tulip_softc *));
 3994 void    tlp_sia_get __P((struct tulip_softc *, struct ifmediareq *));
 3995 int     tlp_sia_set __P((struct tulip_softc *));
 3996 int     tlp_sia_media __P((struct tulip_softc *, struct ifmedia_entry *));
 3997 void    tlp_sia_fixup __P((struct tulip_softc *));
 3998 
 3999 void
 4000 tlp_sia_update_link(sc)
 4001         struct tulip_softc *sc;
 4002 {
 4003         struct ifmedia_entry *ife;
 4004         struct tulip_21x4x_media *tm;
 4005         u_int32_t siastat;
 4006 
 4007         ife = TULIP_CURRENT_MEDIA(sc);
 4008         tm = ife->ifm_aux;
 4009 
 4010         sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
 4011 
 4012         siastat = TULIP_READ(sc, CSR_SIASTAT);
 4013 
 4014         /*
 4015          * Note that when we do SIA link tests, we are assuming that
 4016          * the chip is really in the mode that the current media setting
 4017          * reflects.  If we're not, then the link tests will not be
 4018          * accurate!
 4019          */
 4020         switch (IFM_SUBTYPE(ife->ifm_media)) {
 4021         case IFM_10_T:
 4022                 sc->sc_flags |= TULIPF_LINK_VALID;
 4023                 if ((siastat & SIASTAT_LS10) == 0)
 4024                         sc->sc_flags |= TULIPF_LINK_UP;
 4025                 break;
 4026 
 4027         case IFM_100_TX:
 4028         case IFM_100_T4:
 4029                 sc->sc_flags |= TULIPF_LINK_VALID;
 4030                 if ((siastat & SIASTAT_LS100) == 0)
 4031                         sc->sc_flags |= TULIPF_LINK_UP;
 4032                 break;
 4033         }
 4034 
 4035         switch (sc->sc_chip) {
 4036         case TULIP_CHIP_21142:
 4037         case TULIP_CHIP_21143:
 4038                 /*
 4039                  * On these chips, we can tell more information about
 4040                  * AUI/BNC.  Note that the AUI/BNC selection is made
 4041                  * in a different register; for our purpose, it's all
 4042                  * AUI.
 4043                  */
 4044                 switch (IFM_SUBTYPE(ife->ifm_media)) {
 4045                 case IFM_10_2:
 4046                 case IFM_10_5:
 4047                         sc->sc_flags |= TULIPF_LINK_VALID;
 4048                         if (siastat & SIASTAT_ARA) {
 4049                                 TULIP_WRITE(sc, CSR_SIASTAT, SIASTAT_ARA);
 4050                                 sc->sc_flags |= TULIPF_LINK_UP;
 4051                         }
 4052                         break;
 4053 
 4054                 default:
 4055                         /*
 4056                          * If we're SYM media and can detect the link
 4057                          * via the GPIO facility, prefer that status
 4058                          * over LS100.
 4059                          */
 4060                         if (tm->tm_type == TULIP_ROM_MB_21143_SYM &&
 4061                             tm->tm_actmask != 0) {
 4062                                 sc->sc_flags = (sc->sc_flags &
 4063                                     ~TULIPF_LINK_UP) | TULIPF_LINK_VALID;
 4064                                 if (TULIP_ISSET(sc, CSR_SIAGEN,
 4065                                     tm->tm_actmask) == tm->tm_actdata)
 4066                                         sc->sc_flags |= TULIPF_LINK_UP;
 4067                         }
 4068                 }
 4069                 break;
 4070 
 4071         default:
 4072                 /* Nothing. */
 4073                 break;
 4074         }
 4075 }
 4076 
 4077 void
 4078 tlp_sia_get(sc, ifmr)
 4079         struct tulip_softc *sc;
 4080         struct ifmediareq *ifmr;
 4081 {
 4082         struct ifmedia_entry *ife;
 4083 
 4084         ifmr->ifm_status = 0;
 4085 
 4086         tlp_sia_update_link(sc);
 4087 
 4088         ife = TULIP_CURRENT_MEDIA(sc);
 4089 
 4090         if (sc->sc_flags & TULIPF_LINK_VALID)
 4091                 ifmr->ifm_status |= IFM_AVALID;
 4092         if (sc->sc_flags & TULIPF_LINK_UP)
 4093                 ifmr->ifm_status |= IFM_ACTIVE;
 4094         ifmr->ifm_active = ife->ifm_media;
 4095 }
 4096 
 4097 void
 4098 tlp_sia_fixup(sc)
 4099         struct tulip_softc *sc;
 4100 {
 4101         struct ifmedia_entry *ife;
 4102         struct tulip_21x4x_media *tm;
 4103         u_int32_t siaconn, siatxrx, siagen;
 4104 
 4105         switch (sc->sc_chip) {
 4106         case TULIP_CHIP_82C115:
 4107         case TULIP_CHIP_MX98713A:
 4108         case TULIP_CHIP_MX98715:
 4109         case TULIP_CHIP_MX98715A:
 4110         case TULIP_CHIP_MX98715AEC_X:
 4111         case TULIP_CHIP_MX98725:
 4112                 siaconn = PMAC_SIACONN_MASK;
 4113                 siatxrx = PMAC_SIATXRX_MASK;
 4114                 siagen  = PMAC_SIAGEN_MASK;
 4115                 break;
 4116 
 4117         default:
 4118                 /* No fixups required on any other chips. */
 4119                 return;
 4120         }
 4121 
 4122         for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
 4123              ife != NULL; ife = TAILQ_NEXT(ife, ifm_list)) {
 4124                 tm = ife->ifm_aux;
 4125                 if (tm == NULL)
 4126                         continue;
 4127 
 4128                 tm->tm_siaconn &= siaconn;
 4129                 tm->tm_siatxrx &= siatxrx;
 4130                 tm->tm_siagen  &= siagen;
 4131         }
 4132 }
 4133 
 4134 int
 4135 tlp_sia_set(sc)
 4136         struct tulip_softc *sc;
 4137 {
 4138 
 4139         return (tlp_sia_media(sc, TULIP_CURRENT_MEDIA(sc)));
 4140 }
 4141 
 4142 int
 4143 tlp_sia_media(sc, ife)
 4144         struct tulip_softc *sc;
 4145         struct ifmedia_entry *ife;
 4146 {
 4147         struct tulip_21x4x_media *tm;
 4148 
 4149         tm = ife->ifm_aux;
 4150 
 4151         /*
 4152          * XXX This appears to be necessary on a bunch of the clone chips.
 4153          */
 4154         delay(20000);
 4155 
 4156         /*
 4157          * Idle the chip.
 4158          */
 4159         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 4160 
 4161         /*
 4162          * Program the SIA.  It's important to write in this order,
 4163          * resetting the SIA first.
 4164          */
 4165         TULIP_WRITE(sc, CSR_SIACONN, 0);                /* SRL bit clear */
 4166         delay(1000);
 4167 
 4168         TULIP_WRITE(sc, CSR_SIATXRX, tm->tm_siatxrx);
 4169 
 4170         switch (sc->sc_chip) {
 4171         case TULIP_CHIP_21142:
 4172         case TULIP_CHIP_21143:
 4173                 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpctl);
 4174                 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen | tm->tm_gpdata);
 4175                 break;
 4176         default:
 4177                 TULIP_WRITE(sc, CSR_SIAGEN, tm->tm_siagen);
 4178         }
 4179 
 4180         TULIP_WRITE(sc, CSR_SIACONN, tm->tm_siaconn);
 4181 
 4182         /*
 4183          * Set the OPMODE bits for this media and write OPMODE.
 4184          * This will resume the transmit and receive processes.
 4185          */
 4186         sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
 4187         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 4188 
 4189         return (0);
 4190 }
 4191 
 4192 /*
 4193  * 21140 GPIO utility functions.
 4194  */
 4195 void    tlp_21140_gpio_update_link __P((struct tulip_softc *));
 4196 
 4197 void
 4198 tlp_21140_gpio_update_link(sc)
 4199         struct tulip_softc *sc;
 4200 {
 4201         struct ifmedia_entry *ife;
 4202         struct tulip_21x4x_media *tm;
 4203 
 4204         ife = TULIP_CURRENT_MEDIA(sc);
 4205         tm = ife->ifm_aux;
 4206 
 4207         sc->sc_flags &= ~(TULIPF_LINK_UP|TULIPF_LINK_VALID);
 4208 
 4209         if (tm->tm_actmask != 0) {
 4210                 sc->sc_flags |= TULIPF_LINK_VALID;
 4211                 if (TULIP_ISSET(sc, CSR_GPP, tm->tm_actmask) ==
 4212                     tm->tm_actdata)
 4213                         sc->sc_flags |= TULIPF_LINK_UP;
 4214         }
 4215 }
 4216 
 4217 void
 4218 tlp_21140_gpio_get(sc, ifmr)
 4219         struct tulip_softc *sc;
 4220         struct ifmediareq *ifmr;
 4221 {
 4222         struct ifmedia_entry *ife;
 4223 
 4224         ifmr->ifm_status = 0;
 4225 
 4226         tlp_21140_gpio_update_link(sc);
 4227 
 4228         ife = TULIP_CURRENT_MEDIA(sc);
 4229 
 4230         if (sc->sc_flags & TULIPF_LINK_VALID)
 4231                 ifmr->ifm_status |= IFM_AVALID;
 4232         if (sc->sc_flags & TULIPF_LINK_UP)
 4233                 ifmr->ifm_status |= IFM_ACTIVE;
 4234         ifmr->ifm_active = ife->ifm_media;
 4235 }
 4236 
 4237 int
 4238 tlp_21140_gpio_set(sc)
 4239         struct tulip_softc *sc;
 4240 {
 4241         struct ifmedia_entry *ife;
 4242         struct tulip_21x4x_media *tm;
 4243 
 4244         ife = TULIP_CURRENT_MEDIA(sc);
 4245         tm = ife->ifm_aux;
 4246 
 4247         /*
 4248          * Idle the chip.
 4249          */
 4250         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 4251 
 4252         /*
 4253          * Set the GPIO pins for this media, to flip any
 4254          * relays, etc.
 4255          */
 4256         TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
 4257         delay(10);
 4258         TULIP_WRITE(sc, CSR_GPP, tm->tm_gpdata);
 4259 
 4260         /*
 4261          * Set the OPMODE bits for this media and write OPMODE.
 4262          * This will resume the transmit and receive processes.
 4263          */
 4264         sc->sc_opmode = (sc->sc_opmode & ~OPMODE_MEDIA_BITS) | tm->tm_opmode;
 4265         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 4266 
 4267         return (0);
 4268 }
 4269 
 4270 /*
 4271  * 21040 and 21041 media switches.
 4272  */
 4273 void    tlp_21040_tmsw_init __P((struct tulip_softc *));
 4274 void    tlp_21040_tp_tmsw_init __P((struct tulip_softc *));
 4275 void    tlp_21040_auibnc_tmsw_init __P((struct tulip_softc *));
 4276 void    tlp_21041_tmsw_init __P((struct tulip_softc *));
 4277 
 4278 const struct tulip_mediasw tlp_21040_mediasw = {
 4279         tlp_21040_tmsw_init, tlp_sia_get, tlp_sia_set
 4280 };
 4281 
 4282 const struct tulip_mediasw tlp_21040_tp_mediasw = {
 4283         tlp_21040_tp_tmsw_init, tlp_sia_get, tlp_sia_set
 4284 };
 4285 
 4286 const struct tulip_mediasw tlp_21040_auibnc_mediasw = {
 4287         tlp_21040_auibnc_tmsw_init, tlp_sia_get, tlp_sia_set
 4288 };
 4289 
 4290 const struct tulip_mediasw tlp_21041_mediasw = {
 4291         tlp_21041_tmsw_init, tlp_sia_get, tlp_sia_set
 4292 };
 4293 
 4294 
 4295 void
 4296 tlp_21040_tmsw_init(sc)
 4297         struct tulip_softc *sc;
 4298 {
 4299         static const u_int8_t media[] = {
 4300                 TULIP_ROM_MB_MEDIA_TP,
 4301                 TULIP_ROM_MB_MEDIA_TP_FDX,
 4302                 TULIP_ROM_MB_MEDIA_AUI,
 4303         };
 4304         struct tulip_21x4x_media *tm;
 4305 
 4306         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 4307             tlp_mediastatus);
 4308 
 4309         tlp_add_srom_media(sc, 0, NULL, NULL, media, 3);
 4310 
 4311         /*
 4312          * No SROM type for External SIA.
 4313          */
 4314         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4315         tm->tm_name = "manual";
 4316         tm->tm_opmode = 0;
 4317         tm->tm_siaconn = SIACONN_21040_EXTSIA;
 4318         tm->tm_siatxrx = SIATXRX_21040_EXTSIA;
 4319         tm->tm_siagen  = SIAGEN_21040_EXTSIA;
 4320         ifmedia_add(&sc->sc_mii.mii_media,
 4321             IFM_MAKEWORD(IFM_ETHER, IFM_MANUAL, 0, sc->sc_tlp_minst), 0, tm);
 4322 
 4323         /*
 4324          * XXX Autosense not yet supported.
 4325          */
 4326 
 4327         /* XXX This should be auto-sense. */
 4328         ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
 4329 
 4330         tlp_print_media(sc);
 4331 }
 4332 
 4333 void
 4334 tlp_21040_tp_tmsw_init(sc)
 4335         struct tulip_softc *sc;
 4336 {
 4337         static const u_int8_t media[] = {
 4338                 TULIP_ROM_MB_MEDIA_TP,
 4339                 TULIP_ROM_MB_MEDIA_TP_FDX,
 4340         };
 4341 
 4342         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 4343             tlp_mediastatus);
 4344 
 4345         tlp_add_srom_media(sc, 0, NULL, NULL, media, 2);
 4346 
 4347         ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
 4348 
 4349         tlp_print_media(sc);
 4350 }
 4351 
 4352 void
 4353 tlp_21040_auibnc_tmsw_init(sc)
 4354         struct tulip_softc *sc;
 4355 {
 4356         static const u_int8_t media[] = {
 4357                 TULIP_ROM_MB_MEDIA_AUI,
 4358         };
 4359 
 4360         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 4361             tlp_mediastatus);
 4362 
 4363         tlp_add_srom_media(sc, 0, NULL, NULL, media, 1);
 4364 
 4365         ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_5);
 4366 
 4367         tlp_print_media(sc);
 4368 }
 4369 
 4370 void
 4371 tlp_21041_tmsw_init(sc)
 4372         struct tulip_softc *sc;
 4373 {
 4374         static const u_int8_t media[] = {
 4375                 TULIP_ROM_MB_MEDIA_TP,
 4376                 TULIP_ROM_MB_MEDIA_TP_FDX,
 4377                 TULIP_ROM_MB_MEDIA_BNC,
 4378                 TULIP_ROM_MB_MEDIA_AUI,
 4379         };
 4380         int i, defmedia, devcnt, leaf_offset, mb_offset, m_cnt;
 4381         const struct tulip_srom_to_ifmedia *tsti;
 4382         struct tulip_21x4x_media *tm;
 4383         u_int16_t romdef;
 4384         u_int8_t mb;
 4385 
 4386         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 4387             tlp_mediastatus);
 4388 
 4389         if (tlp_isv_srom(sc->sc_srom) == 0) {
 4390  not_isv_srom:
 4391                 /*
 4392                  * If we have a board without the standard 21041 SROM format,
 4393                  * we just assume all media are present and try and pick a
 4394                  * reasonable default.
 4395                  */
 4396                 tlp_add_srom_media(sc, 0, NULL, NULL, media, 4);
 4397 
 4398                 /*
 4399                  * XXX Autosense not yet supported.
 4400                  */
 4401 
 4402                 /* XXX This should be auto-sense. */
 4403                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
 4404 
 4405                 tlp_print_media(sc);
 4406                 return;
 4407         }
 4408 
 4409         devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
 4410         for (i = 0; i < devcnt; i++) {
 4411                 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
 4412                         break;
 4413                 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
 4414                     sc->sc_devno)
 4415                         break;
 4416         }
 4417 
 4418         if (i == devcnt)
 4419                 goto not_isv_srom;
 4420 
 4421         leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
 4422             TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
 4423         mb_offset = leaf_offset + TULIP_ROM_IL_MEDIAn_BLOCK_BASE;
 4424         m_cnt = sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
 4425 
 4426         for (; m_cnt != 0;
 4427              m_cnt--, mb_offset += TULIP_ROM_MB_SIZE(mb)) {
 4428                 mb = sc->sc_srom[mb_offset];
 4429                 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4430                 switch (mb & TULIP_ROM_MB_MEDIA_CODE) {
 4431                 case TULIP_ROM_MB_MEDIA_TP_FDX:
 4432                 case TULIP_ROM_MB_MEDIA_TP:
 4433                 case TULIP_ROM_MB_MEDIA_BNC:
 4434                 case TULIP_ROM_MB_MEDIA_AUI:
 4435                         tsti = tlp_srom_to_ifmedia(mb &
 4436                             TULIP_ROM_MB_MEDIA_CODE);
 4437 
 4438                         tlp_srom_media_info(sc, tsti, tm);
 4439 
 4440                         /*
 4441                          * Override our default SIA settings if the
 4442                          * SROM contains its own.
 4443                          */
 4444                         if (mb & TULIP_ROM_MB_EXT) {
 4445                                 tm->tm_siaconn = TULIP_ROM_GETW(sc->sc_srom,
 4446                                     mb_offset + TULIP_ROM_MB_CSR13);
 4447                                 tm->tm_siatxrx = TULIP_ROM_GETW(sc->sc_srom,
 4448                                     mb_offset + TULIP_ROM_MB_CSR14);
 4449                                 tm->tm_siagen = TULIP_ROM_GETW(sc->sc_srom,
 4450                                     mb_offset + TULIP_ROM_MB_CSR15);
 4451                         }
 4452 
 4453                         ifmedia_add(&sc->sc_mii.mii_media,
 4454                             IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
 4455                             tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
 4456                         break;
 4457 
 4458                 default:
 4459                         printf("%s: unknown media code 0x%02x\n",
 4460                             sc->sc_dev.dv_xname,
 4461                             mb & TULIP_ROM_MB_MEDIA_CODE);
 4462                         free(tm, M_DEVBUF);
 4463                 }
 4464         }
 4465 
 4466         /*
 4467          * XXX Autosense not yet supported.
 4468          */
 4469 
 4470         romdef = TULIP_ROM_GETW(sc->sc_srom, leaf_offset +
 4471             TULIP_ROM_IL_SELECT_CONN_TYPE);
 4472         switch (romdef) {
 4473         case SELECT_CONN_TYPE_TP:
 4474         case SELECT_CONN_TYPE_TP_AUTONEG:
 4475         case SELECT_CONN_TYPE_TP_NOLINKPASS:
 4476                 defmedia = IFM_ETHER|IFM_10_T;
 4477                 break;
 4478 
 4479         case SELECT_CONN_TYPE_TP_FDX:
 4480                 defmedia = IFM_ETHER|IFM_10_T|IFM_FDX;
 4481                 break;
 4482 
 4483         case SELECT_CONN_TYPE_BNC:
 4484                 defmedia = IFM_ETHER|IFM_10_2;
 4485                 break;
 4486 
 4487         case SELECT_CONN_TYPE_AUI:
 4488                 defmedia = IFM_ETHER|IFM_10_5;
 4489                 break;
 4490 #if 0 /* XXX */
 4491         case SELECT_CONN_TYPE_ASENSE:
 4492         case SELECT_CONN_TYPE_ASENSE_AUTONEG:
 4493                 defmedia = IFM_ETHER|IFM_AUTO;
 4494                 break;
 4495 #endif
 4496         default:
 4497                 defmedia = 0;
 4498         }
 4499 
 4500         if (defmedia == 0) {
 4501                 /*
 4502                  * XXX We should default to auto-sense.
 4503                  */
 4504                 defmedia = IFM_ETHER|IFM_10_T;
 4505         }
 4506 
 4507         ifmedia_set(&sc->sc_mii.mii_media, defmedia);
 4508 
 4509         tlp_print_media(sc);
 4510 }
 4511 
 4512 /*
 4513  * DECchip 2114x ISV media switch.
 4514  */
 4515 void    tlp_2114x_isv_tmsw_init __P((struct tulip_softc *));
 4516 void    tlp_2114x_isv_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
 4517 int     tlp_2114x_isv_tmsw_set __P((struct tulip_softc *));
 4518 
 4519 const struct tulip_mediasw tlp_2114x_isv_mediasw = {
 4520         tlp_2114x_isv_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
 4521 };
 4522 
 4523 void    tlp_2114x_nway_get __P((struct tulip_softc *, struct ifmediareq *));
 4524 int     tlp_2114x_nway_set __P((struct tulip_softc *));
 4525 
 4526 void    tlp_2114x_nway_statchg __P((struct device *));
 4527 int     tlp_2114x_nway_service __P((struct tulip_softc *, int));
 4528 void    tlp_2114x_nway_auto __P((struct tulip_softc *));
 4529 void    tlp_2114x_nway_status __P((struct tulip_softc *));
 4530 
 4531 void
 4532 tlp_2114x_isv_tmsw_init(sc)
 4533         struct tulip_softc *sc;
 4534 {
 4535         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 4536         struct ifmedia_entry *ife;
 4537         struct mii_softc *phy;
 4538         struct tulip_21x4x_media *tm;
 4539         const struct tulip_srom_to_ifmedia *tsti;
 4540         int i, devcnt, leaf_offset, m_cnt, type, length;
 4541         int defmedia, miidef;
 4542         u_int16_t word;
 4543         u_int8_t *cp, *ncp;
 4544 
 4545         defmedia = miidef = 0;
 4546 
 4547         sc->sc_mii.mii_ifp = ifp;
 4548         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
 4549         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
 4550         sc->sc_mii.mii_statchg = sc->sc_statchg;
 4551 
 4552         /*
 4553          * Ignore `instance'; we may get a mixture of SIA and MII
 4554          * media, and `instance' is used to isolate or select the
 4555          * PHY on the MII as appropriate.  Note that duplicate media
 4556          * are disallowed, so ignoring `instance' is safe.
 4557          */
 4558         ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, tlp_mediachange,
 4559             tlp_mediastatus);
 4560 
 4561         devcnt = sc->sc_srom[TULIP_ROM_CHIP_COUNT];
 4562         for (i = 0; i < devcnt; i++) {
 4563                 if (sc->sc_srom[TULIP_ROM_CHIP_COUNT] == 1)
 4564                         break;
 4565                 if (sc->sc_srom[TULIP_ROM_CHIPn_DEVICE_NUMBER(i)] ==
 4566                     sc->sc_devno)
 4567                         break;
 4568         }
 4569 
 4570         if (i == devcnt) {
 4571                 printf("%s: unable to locate info leaf in SROM\n",
 4572                     sc->sc_dev.dv_xname);
 4573                 return;
 4574         }
 4575 
 4576         leaf_offset = TULIP_ROM_GETW(sc->sc_srom,
 4577             TULIP_ROM_CHIPn_INFO_LEAF_OFFSET(i));
 4578 
 4579         /* XXX SELECT CONN TYPE */
 4580 
 4581         cp = &sc->sc_srom[leaf_offset + TULIP_ROM_IL_MEDIA_COUNT];
 4582 
 4583         /*
 4584          * On some chips, the first thing in the Info Leaf is the
 4585          * GPIO pin direction data.
 4586          */
 4587         switch (sc->sc_chip) {
 4588         case TULIP_CHIP_21140:
 4589         case TULIP_CHIP_21140A:
 4590         case TULIP_CHIP_MX98713:
 4591         case TULIP_CHIP_AX88140:
 4592         case TULIP_CHIP_AX88141:
 4593                 sc->sc_gp_dir = *cp++;
 4594                 break;
 4595 
 4596         default:
 4597                 /* Nothing. */
 4598                 break;
 4599         }
 4600 
 4601         /* Get the media count. */
 4602         m_cnt = *cp++;
 4603 
 4604         for (; m_cnt != 0; cp = ncp, m_cnt--) {
 4605                 /*
 4606                  * Determine the type and length of this media block.
 4607                  * The 21143 is spec'd to always use extended format blocks,
 4608                  * but some cards don't set the bit to indicate this.
 4609                  * Hopefully there are no cards which really don't use
 4610                  * extended format blocks.
 4611                  */
 4612                 if ((*cp & 0x80) == 0 && sc->sc_chip != TULIP_CHIP_21143) {
 4613                         length = 4;
 4614                         type = TULIP_ROM_MB_21140_GPR;
 4615                 } else {
 4616                         length = (*cp++ & 0x7f) - 1;
 4617                         type = *cp++ & 0x3f;
 4618                 }
 4619 
 4620                 /* Compute the start of the next block. */
 4621                 ncp = cp + length;
 4622 
 4623                 /* Now, parse the block. */
 4624                 switch (type) {
 4625                 case TULIP_ROM_MB_21140_GPR:
 4626                         tlp_get_minst(sc);
 4627                         sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_GPR;
 4628 
 4629                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4630 
 4631                         tm->tm_type = TULIP_ROM_MB_21140_GPR;
 4632                         tm->tm_get = tlp_21140_gpio_get;
 4633                         tm->tm_set = tlp_21140_gpio_set;
 4634 
 4635                         /* First is the media type code. */
 4636                         tsti = tlp_srom_to_ifmedia(cp[0] &
 4637                             TULIP_ROM_MB_MEDIA_CODE);
 4638                         if (tsti == NULL) {
 4639                                 /* Invalid media code. */
 4640                                 free(tm, M_DEVBUF);
 4641                                 break;
 4642                         }
 4643                         
 4644                         /* Get defaults. */
 4645                         tlp_srom_media_info(sc, tsti, tm);
 4646 
 4647                         /* Next is any GPIO info for this media. */
 4648                         tm->tm_gpdata = cp[1];
 4649 
 4650                         /*
 4651                          * Next is a word containing OPMODE information
 4652                          * and info on how to detect if this media is
 4653                          * active.
 4654                          */
 4655                         word = TULIP_ROM_GETW(cp, 2);
 4656                         tm->tm_opmode &= OPMODE_FD;
 4657                         tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
 4658                         if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
 4659                                 tm->tm_actmask =
 4660                                     TULIP_ROM_MB_BITPOS(word);
 4661                                 tm->tm_actdata =
 4662                                     (word & TULIP_ROM_MB_POLARITY) ?
 4663                                     0 : tm->tm_actmask;
 4664                         }
 4665 
 4666                         ifmedia_add(&sc->sc_mii.mii_media,
 4667                             IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
 4668                             tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
 4669                         break;
 4670 
 4671                 case TULIP_ROM_MB_21140_MII:
 4672                         sc->sc_media_seen |= 1 << TULIP_ROM_MB_21140_MII;
 4673 
 4674                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4675 
 4676                         tm->tm_type = TULIP_ROM_MB_21140_MII;
 4677                         tm->tm_get = tlp_mii_getmedia;
 4678                         tm->tm_set = tlp_mii_setmedia;
 4679                         tm->tm_opmode = OPMODE_PS;
 4680 
 4681                         if (sc->sc_reset == NULL)
 4682                                 sc->sc_reset = tlp_21140_reset;
 4683 
 4684                         /* First is the PHY number. */
 4685                         tm->tm_phyno = *cp++;
 4686 
 4687                         /* Next is the MII select sequence length and offset. */
 4688                         tm->tm_gp_length = *cp++;
 4689                         tm->tm_gp_offset = cp - &sc->sc_srom[0];
 4690                         cp += tm->tm_gp_length;
 4691 
 4692                         /* Next is the MII reset sequence length and offset. */
 4693                         tm->tm_reset_length = *cp++;
 4694                         tm->tm_reset_offset = cp - &sc->sc_srom[0];
 4695                         cp += tm->tm_reset_length;
 4696 
 4697                         /*
 4698                          * The following items are left in the media block
 4699                          * that we don't particularly care about:
 4700                          *
 4701                          *      capabilities            W
 4702                          *      advertisement           W
 4703                          *      full duplex             W
 4704                          *      tx threshold            W
 4705                          *
 4706                          * These appear to be bits in the PHY registers,
 4707                          * which our MII code handles on its own.
 4708                          */
 4709 
 4710                         /*
 4711                          * Before we probe the MII bus, we need to reset
 4712                          * it and issue the selection sequence.
 4713                          */
 4714 
 4715                         /* Set the direction of the pins... */
 4716                         TULIP_WRITE(sc, CSR_GPP, GPP_GPC|sc->sc_gp_dir);
 4717 
 4718                         for (i = 0; i < tm->tm_reset_length; i++) {
 4719                                 delay(10);
 4720                                 TULIP_WRITE(sc, CSR_GPP,
 4721                                     sc->sc_srom[tm->tm_reset_offset + i]);
 4722                         }
 4723 
 4724                         for (i = 0; i < tm->tm_gp_length; i++) {
 4725                                 delay(10);
 4726                                 TULIP_WRITE(sc, CSR_GPP,
 4727                                     sc->sc_srom[tm->tm_gp_offset + i]);
 4728                         }
 4729 
 4730                         /* If there were no sequences, just lower the pins. */
 4731                         if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
 4732                                 delay(10);
 4733                                 TULIP_WRITE(sc, CSR_GPP, 0);
 4734                         }
 4735 
 4736                         /*
 4737                          * Now, probe the MII for the PHY.  Note, we know
 4738                          * the location of the PHY on the bus, but we don't
 4739                          * particularly care; the MII code just likes to
 4740                          * search the whole thing anyhow.
 4741                          */
 4742                         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
 4743                             MII_PHY_ANY, tm->tm_phyno, 0);
 4744 
 4745                         /*
 4746                          * Now, search for the PHY we hopefully just
 4747                          * configured.  If it's not configured into the
 4748                          * kernel, we lose.  The PHY's default media always
 4749                          * takes priority.
 4750                          */
 4751                         for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
 4752                              phy != NULL;
 4753                              phy = LIST_NEXT(phy, mii_list))
 4754                                 if (phy->mii_offset == tm->tm_phyno)
 4755                                         break;
 4756                         if (phy == NULL) {
 4757                                 printf("%s: unable to configure MII\n",
 4758                                     sc->sc_dev.dv_xname);
 4759                                 break;
 4760                         }
 4761 
 4762                         sc->sc_flags |= TULIPF_HAS_MII;
 4763                         sc->sc_tick = tlp_mii_tick;
 4764                         miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
 4765                             phy->mii_inst);
 4766 
 4767                         /*
 4768                          * Okay, now that we've found the PHY and the MII
 4769                          * layer has added all of the media associated
 4770                          * with that PHY, we need to traverse the media
 4771                          * list, and add our `tm' to each entry's `aux'
 4772                          * pointer.
 4773                          *
 4774                          * We do this by looking for media with our
 4775                          * PHY's `instance'.
 4776                          */
 4777                         for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
 4778                              ife != NULL;
 4779                              ife = TAILQ_NEXT(ife, ifm_list)) {
 4780                                 if (IFM_INST(ife->ifm_media) != phy->mii_inst)
 4781                                         continue;
 4782                                 ife->ifm_aux = tm;
 4783                         }
 4784                         break;
 4785 
 4786                 case TULIP_ROM_MB_21142_SIA:
 4787                         tlp_get_minst(sc);
 4788                         sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_SIA;
 4789 
 4790                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4791 
 4792                         tm->tm_type = TULIP_ROM_MB_21142_SIA;
 4793                         tm->tm_get = tlp_sia_get;
 4794                         tm->tm_set = tlp_sia_set;
 4795 
 4796                         /* First is the media type code. */
 4797                         tsti = tlp_srom_to_ifmedia(cp[0] &
 4798                             TULIP_ROM_MB_MEDIA_CODE);
 4799                         if (tsti == NULL) {
 4800                                 /* Invalid media code. */
 4801                                 free(tm, M_DEVBUF);
 4802                                 break;
 4803                         }
 4804 
 4805                         /* Get defaults. */
 4806                         tlp_srom_media_info(sc, tsti, tm);
 4807 
 4808                         /*
 4809                          * Override our default SIA settings if the
 4810                          * SROM contains its own.
 4811                          */
 4812                         if (cp[0] & 0x40) {
 4813                                 tm->tm_siaconn = TULIP_ROM_GETW(cp, 1);
 4814                                 tm->tm_siatxrx = TULIP_ROM_GETW(cp, 3);
 4815                                 tm->tm_siagen  = TULIP_ROM_GETW(cp, 5);
 4816                                 cp += 7;
 4817                         } else
 4818                                 cp++;
 4819 
 4820                         /* Next is GPIO control/data. */
 4821                         tm->tm_gpctl  = TULIP_ROM_GETW(cp, 0) << 16;
 4822                         tm->tm_gpdata = TULIP_ROM_GETW(cp, 2) << 16;
 4823 
 4824                         ifmedia_add(&sc->sc_mii.mii_media,
 4825                             IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
 4826                             tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
 4827                         break;
 4828 
 4829                 case TULIP_ROM_MB_21142_MII:
 4830                         sc->sc_media_seen |= 1 << TULIP_ROM_MB_21142_MII;
 4831 
 4832                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4833 
 4834                         tm->tm_type = TULIP_ROM_MB_21142_MII;
 4835                         tm->tm_get = tlp_mii_getmedia;
 4836                         tm->tm_set = tlp_mii_setmedia;
 4837                         tm->tm_opmode = OPMODE_PS;
 4838 
 4839                         if (sc->sc_reset == NULL)
 4840                                 sc->sc_reset = tlp_21142_reset;
 4841 
 4842                         /* First is the PHY number. */
 4843                         tm->tm_phyno = *cp++;
 4844 
 4845                         /* Next is the MII select sequence length and offset. */
 4846                         tm->tm_gp_length = *cp++;
 4847                         tm->tm_gp_offset = cp - &sc->sc_srom[0];
 4848                         cp += tm->tm_gp_length * 2;
 4849 
 4850                         /* Next is the MII reset sequence length and offset. */
 4851                         tm->tm_reset_length = *cp++;
 4852                         tm->tm_reset_offset = cp - &sc->sc_srom[0];
 4853                         cp += tm->tm_reset_length * 2;
 4854 
 4855                         /*
 4856                          * The following items are left in the media block
 4857                          * that we don't particularly care about:
 4858                          *
 4859                          *      capabilities            W
 4860                          *      advertisement           W
 4861                          *      full duplex             W
 4862                          *      tx threshold            W
 4863                          *      MII interrupt           W
 4864                          *
 4865                          * These appear to be bits in the PHY registers,
 4866                          * which our MII code handles on its own.
 4867                          */
 4868 
 4869                         /*
 4870                          * Before we probe the MII bus, we need to reset
 4871                          * it and issue the selection sequence.
 4872                          */
 4873 
 4874                         cp = &sc->sc_srom[tm->tm_reset_offset];
 4875                         for (i = 0; i < tm->tm_reset_length; i++, cp += 2) {
 4876                                 delay(10);
 4877                                 TULIP_WRITE(sc, CSR_SIAGEN,
 4878                                     TULIP_ROM_GETW(cp, 0) << 16);
 4879                         }
 4880 
 4881                         cp = &sc->sc_srom[tm->tm_gp_offset];
 4882                         for (i = 0; i < tm->tm_gp_length; i++, cp += 2) {
 4883                                 delay(10);
 4884                                 TULIP_WRITE(sc, CSR_SIAGEN,
 4885                                     TULIP_ROM_GETW(cp, 0) << 16);
 4886                         }
 4887 
 4888                         /* If there were no sequences, just lower the pins. */
 4889                         if (tm->tm_reset_length == 0 && tm->tm_gp_length == 0) {
 4890                                 delay(10);
 4891                                 TULIP_WRITE(sc, CSR_SIAGEN, 0);
 4892                         }
 4893 
 4894                         /*
 4895                          * Now, probe the MII for the PHY.  Note, we know
 4896                          * the location of the PHY on the bus, but we don't
 4897                          * particularly care; the MII code just likes to
 4898                          * search the whole thing anyhow.
 4899                          */
 4900                         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
 4901                             MII_PHY_ANY, tm->tm_phyno, 0);
 4902 
 4903                         /*
 4904                          * Now, search for the PHY we hopefully just
 4905                          * configured.  If it's not configured into the
 4906                          * kernel, we lose.  The PHY's default media always
 4907                          * takes priority.
 4908                          */
 4909                         for (phy = LIST_FIRST(&sc->sc_mii.mii_phys);
 4910                              phy != NULL;
 4911                              phy = LIST_NEXT(phy, mii_list))
 4912                                 if (phy->mii_offset == tm->tm_phyno)
 4913                                         break;
 4914                         if (phy == NULL) {
 4915                                 printf("%s: unable to configure MII\n",
 4916                                     sc->sc_dev.dv_xname);
 4917                                 break;
 4918                         }
 4919 
 4920                         sc->sc_flags |= TULIPF_HAS_MII;
 4921                         sc->sc_tick = tlp_mii_tick;
 4922                         miidef = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0,
 4923                             phy->mii_inst);
 4924 
 4925                         /*
 4926                          * Okay, now that we've found the PHY and the MII
 4927                          * layer has added all of the media associated
 4928                          * with that PHY, we need to traverse the media
 4929                          * list, and add our `tm' to each entry's `aux'
 4930                          * pointer.
 4931                          *
 4932                          * We do this by looking for media with our
 4933                          * PHY's `instance'.
 4934                          */
 4935                         for (ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
 4936                              ife != NULL;
 4937                              ife = TAILQ_NEXT(ife, ifm_list)) {
 4938                                 if (IFM_INST(ife->ifm_media) != phy->mii_inst)
 4939                                         continue;
 4940                                 ife->ifm_aux = tm;
 4941                         }
 4942                         break;
 4943 
 4944                 case TULIP_ROM_MB_21143_SYM:
 4945                         tlp_get_minst(sc);
 4946                         sc->sc_media_seen |= 1 << TULIP_ROM_MB_21143_SYM;
 4947 
 4948                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 4949 
 4950                         tm->tm_type = TULIP_ROM_MB_21143_SYM;
 4951                         tm->tm_get = tlp_sia_get;
 4952                         tm->tm_set = tlp_sia_set;
 4953 
 4954                         /* First is the media type code. */
 4955                         tsti = tlp_srom_to_ifmedia(cp[0] &
 4956                             TULIP_ROM_MB_MEDIA_CODE);
 4957                         if (tsti == NULL) {
 4958                                 /* Invalid media code. */
 4959                                 free(tm, M_DEVBUF);
 4960                                 break;
 4961                         }
 4962 
 4963                         /* Get defaults. */
 4964                         tlp_srom_media_info(sc, tsti, tm);
 4965 
 4966                         /* Next is GPIO control/data. */
 4967                         tm->tm_gpctl  = TULIP_ROM_GETW(cp, 1) << 16;
 4968                         tm->tm_gpdata = TULIP_ROM_GETW(cp, 3) << 16;
 4969 
 4970                         /*
 4971                          * Next is a word containing OPMODE information
 4972                          * and info on how to detect if this media is
 4973                          * active.
 4974                          */
 4975                         word = TULIP_ROM_GETW(cp, 5);
 4976                         tm->tm_opmode &= OPMODE_FD;
 4977                         tm->tm_opmode |= TULIP_ROM_MB_OPMODE(word);
 4978                         if ((word & TULIP_ROM_MB_NOINDICATOR) == 0) {
 4979                                 tm->tm_actmask =
 4980                                     TULIP_ROM_MB_BITPOS(word);
 4981                                 tm->tm_actdata =
 4982                                     (word & TULIP_ROM_MB_POLARITY) ?
 4983                                     0 : tm->tm_actmask;
 4984                         }
 4985 
 4986                         ifmedia_add(&sc->sc_mii.mii_media,
 4987                             IFM_MAKEWORD(IFM_ETHER, tsti->tsti_subtype,
 4988                             tsti->tsti_options, sc->sc_tlp_minst), 0, tm);
 4989                         break;
 4990 
 4991                 case TULIP_ROM_MB_21143_RESET:
 4992                         printf("%s: 21143 reset block\n", sc->sc_dev.dv_xname);
 4993                         break;
 4994 
 4995                 default:
 4996                         printf("%s: unknown ISV media block type 0x%02x\n",
 4997                             sc->sc_dev.dv_xname, type);
 4998                 }
 4999         }
 5000 
 5001         /*
 5002          * Deal with the case where no media is configured.
 5003          */
 5004         if (TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list) == NULL) {
 5005                 printf("%s: no media found!\n", sc->sc_dev.dv_xname);
 5006                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
 5007                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
 5008                 return;
 5009         }
 5010 
 5011         /*
 5012          * Pick the default media.
 5013          */
 5014         if (miidef != 0)
 5015                 defmedia = miidef;
 5016         else {
 5017                 switch (sc->sc_chip) {
 5018                 case TULIP_CHIP_21140:
 5019                 case TULIP_CHIP_21140A:
 5020                         /* XXX should come from SROM */
 5021                         defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
 5022                         if (ifmedia_match(&sc->sc_mii.mii_media, defmedia,
 5023                                 sc->sc_mii.mii_media.ifm_mask) == NULL) {
 5024                                 /*
 5025                                  * There is not a 10baseT media.
 5026                                  * Fall back to the first found one.
 5027                                  */
 5028                                 ife = TAILQ_FIRST(&sc->sc_mii.mii_media.ifm_list);
 5029                                 defmedia = ife->ifm_media;
 5030                         }
 5031                         break;
 5032 
 5033                 case TULIP_CHIP_21142:
 5034                 case TULIP_CHIP_21143:
 5035                 case TULIP_CHIP_MX98713A:
 5036                 case TULIP_CHIP_MX98715:
 5037                 case TULIP_CHIP_MX98715A:
 5038                 case TULIP_CHIP_MX98715AEC_X:
 5039                 case TULIP_CHIP_MX98725:
 5040                         tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 5041                         tm->tm_name = "auto";
 5042                         tm->tm_get = tlp_2114x_nway_get;
 5043                         tm->tm_set = tlp_2114x_nway_set;
 5044 
 5045                         defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0);
 5046                         ifmedia_add(&sc->sc_mii.mii_media, defmedia, 0, tm);
 5047 
 5048                         sc->sc_statchg = tlp_2114x_nway_statchg;
 5049                         sc->sc_tick = tlp_2114x_nway_tick;
 5050                         break;
 5051 
 5052                 default:
 5053                         defmedia = IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0);
 5054                         break;
 5055                 }
 5056         }
 5057 
 5058         ifmedia_set(&sc->sc_mii.mii_media, defmedia);
 5059 
 5060         /*
 5061          * Display any non-MII media we've located.
 5062          */
 5063         if (sc->sc_media_seen &
 5064             ~((1 << TULIP_ROM_MB_21140_MII) | (1 << TULIP_ROM_MB_21142_MII)))
 5065                 tlp_print_media(sc);
 5066 
 5067         tlp_sia_fixup(sc);
 5068 }
 5069 
 5070 void
 5071 tlp_2114x_nway_get(sc, ifmr)
 5072         struct tulip_softc *sc;
 5073         struct ifmediareq *ifmr;
 5074 {
 5075 
 5076         (void) tlp_2114x_nway_service(sc, MII_POLLSTAT);
 5077         ifmr->ifm_status = sc->sc_mii.mii_media_status;
 5078         ifmr->ifm_active = sc->sc_mii.mii_media_active; 
 5079 }
 5080 
 5081 int
 5082 tlp_2114x_nway_set(sc)
 5083         struct tulip_softc *sc;
 5084 {
 5085 
 5086         return (tlp_2114x_nway_service(sc, MII_MEDIACHG));
 5087 }
 5088 
 5089 void
 5090 tlp_2114x_nway_statchg(self)
 5091         struct device *self;
 5092 {
 5093         struct tulip_softc *sc = (struct tulip_softc *)self;
 5094         struct mii_data *mii = &sc->sc_mii;
 5095         struct ifmedia_entry *ife;
 5096 
 5097         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
 5098                 return;
 5099         
 5100         if ((ife = ifmedia_match(&mii->mii_media, mii->mii_media_active, 
 5101             mii->mii_media.ifm_mask)) == NULL) {
 5102                 printf("tlp_2114x_nway_statchg: no match for media 0x%x/0x%x\n",
 5103                     mii->mii_media_active, ~mii->mii_media.ifm_mask);
 5104                 panic("tlp_2114x_nway_statchg");
 5105         }
 5106 
 5107         tlp_sia_media(sc, ife);
 5108 }
 5109 
 5110 void
 5111 tlp_2114x_nway_tick(arg)
 5112         void *arg;
 5113 {
 5114         struct tulip_softc *sc = arg;
 5115         struct mii_data *mii = &sc->sc_mii;
 5116         int s, ticks;
 5117 
 5118         if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 5119                 return;
 5120 
 5121         s = splnet();
 5122         tlp_2114x_nway_service(sc, MII_TICK);
 5123         if ((sc->sc_flags & TULIPF_LINK_UP) == 0 &&
 5124             (mii->mii_media_status & IFM_ACTIVE) != 0 &&
 5125             IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
 5126                 sc->sc_flags |= TULIPF_LINK_UP;
 5127                 tlp_start(&sc->sc_ethercom.ec_if);
 5128         } else if ((sc->sc_flags & TULIPF_LINK_UP) != 0 &&
 5129             (mii->mii_media_status & IFM_ACTIVE) == 0) {
 5130                 sc->sc_flags &= ~TULIPF_LINK_UP;
 5131         }
 5132         splx(s);
 5133 
 5134         if ((sc->sc_flags & TULIPF_LINK_UP) == 0)
 5135                 ticks = hz >> 3;
 5136         else
 5137                 ticks = hz;
 5138         callout_reset(&sc->sc_tick_callout, ticks, tlp_2114x_nway_tick, sc);
 5139 }
 5140 
 5141 /*
 5142  * Support for the 2114X internal NWay block.  This is constructed
 5143  * somewhat like a PHY driver for simplicity.
 5144  */
 5145 
 5146 int
 5147 tlp_2114x_nway_service(sc, cmd)
 5148         struct tulip_softc *sc;
 5149         int cmd;
 5150 {
 5151         struct mii_data *mii = &sc->sc_mii;
 5152         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
 5153 
 5154         if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
 5155                 return (0);
 5156 
 5157         switch (cmd) {
 5158         case MII_POLLSTAT:
 5159                 /* Nothing special to do here. */
 5160                 break;
 5161 
 5162         case MII_MEDIACHG:
 5163                 switch (IFM_SUBTYPE(ife->ifm_media)) {
 5164                 case IFM_AUTO:
 5165                         goto restart;
 5166                 default:
 5167                         /* Manual setting doesn't go through here. */
 5168                         printf("tlp_2114x_nway_service: oops!\n");
 5169                         return (EINVAL);
 5170                 }
 5171                 break;
 5172 
 5173         case MII_TICK:
 5174                 /*
 5175                  * Only used for autonegotiation.
 5176                  */
 5177                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
 5178                         break;
 5179 
 5180                 /*
 5181                  * Check to see if we have link.  If we do, we don't
 5182                  * need to restart the autonegotiation process.
 5183                  */
 5184 #if 0
 5185                 if (mii->mii_media_status & IFM_ACTIVE)
 5186 #else
 5187                 if (sc->sc_flags & TULIPF_LINK_UP)
 5188 #endif
 5189                         break;
 5190 
 5191                 /*
 5192                  * Only retry autonegotiation every 5 seconds.
 5193                  */
 5194                 if (++sc->sc_nway_ticks != (5 << 3))
 5195                         break;
 5196 
 5197         restart:
 5198                 sc->sc_nway_ticks = 0;
 5199                 ife->ifm_data = IFM_NONE;
 5200                 tlp_2114x_nway_auto(sc);
 5201                 break;
 5202         }
 5203 
 5204         /* Update the media status. */
 5205         tlp_2114x_nway_status(sc);
 5206 
 5207         /*
 5208          * Callback if something changed.  Manually configuration goes through
 5209          * tlp_sia_set() anyway, so ignore that here.
 5210          */
 5211         if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO &&
 5212             ife->ifm_data != mii->mii_media_active) {
 5213                 (*sc->sc_statchg)(&sc->sc_dev);
 5214                 ife->ifm_data = mii->mii_media_active;
 5215         }
 5216         return (0);
 5217 }
 5218 
 5219 void
 5220 tlp_2114x_nway_auto(sc)
 5221         struct tulip_softc *sc;
 5222 {
 5223         uint32_t siastat, siatxrx;
 5224 
 5225         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 5226 
 5227         sc->sc_opmode &= ~(OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_FD);
 5228         sc->sc_opmode |= OPMODE_TTM|OPMODE_HBD;
 5229         siatxrx = 0xffbf;               /* XXX magic number */
 5230 
 5231         /* Compute the link code word to advertise. */
 5232         if (sc->sc_sia_cap & BMSR_100T4)
 5233                 siatxrx |= SIATXRX_T4;
 5234         if (sc->sc_sia_cap & BMSR_100TXFDX)
 5235                 siatxrx |= SIATXRX_TXF;
 5236         if (sc->sc_sia_cap & BMSR_100TXHDX)
 5237                 siatxrx |= SIATXRX_THX;
 5238         if (sc->sc_sia_cap & BMSR_10TFDX)
 5239                 sc->sc_opmode |= OPMODE_FD;
 5240         if (sc->sc_sia_cap & BMSR_10THDX)
 5241                 siatxrx |= SIATXRX_TH;
 5242 
 5243         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 5244 
 5245         TULIP_WRITE(sc, CSR_SIACONN, 0);
 5246         delay(1000);
 5247         TULIP_WRITE(sc, CSR_SIATXRX, siatxrx);
 5248         TULIP_WRITE(sc, CSR_SIACONN, SIACONN_SRL);
 5249 
 5250         siastat = TULIP_READ(sc, CSR_SIASTAT);
 5251         siastat &= ~(SIASTAT_ANS|SIASTAT_LPC|SIASTAT_TRA|SIASTAT_ARA|
 5252                      SIASTAT_LS100|SIASTAT_LS10|SIASTAT_MRA);
 5253         siastat |= SIASTAT_ANS_TXDIS;
 5254         TULIP_WRITE(sc, CSR_SIASTAT, siastat);
 5255 }
 5256 
 5257 void
 5258 tlp_2114x_nway_status(sc)
 5259         struct tulip_softc *sc;
 5260 {
 5261         struct mii_data *mii = &sc->sc_mii;
 5262         uint32_t siatxrx, siastat, anlpar;
 5263 
 5264         mii->mii_media_status = IFM_AVALID;
 5265         mii->mii_media_active = IFM_ETHER;
 5266 
 5267         if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
 5268                 return;
 5269 
 5270         siastat = TULIP_READ(sc, CSR_SIASTAT);
 5271         siatxrx = TULIP_READ(sc, CSR_SIATXRX);
 5272 
 5273         if (siatxrx & SIATXRX_ANE) {
 5274                 if ((siastat & SIASTAT_ANS) != SIASTAT_ANS_FLPGOOD) {
 5275                         /* Erg, still trying, I guess... */
 5276                         mii->mii_media_active |= IFM_NONE;
 5277                         return;
 5278                 }
 5279 
 5280                 if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
 5281                         mii->mii_media_status |= IFM_ACTIVE;
 5282 
 5283                 if (siastat & SIASTAT_LPN) {
 5284                         anlpar = SIASTAT_GETLPC(siastat);
 5285                         if (anlpar & ANLPAR_T4 &&
 5286                             sc->sc_sia_cap & BMSR_100T4)
 5287                                 mii->mii_media_active |= IFM_100_T4;
 5288                         else if (anlpar & ANLPAR_TX_FD &&
 5289                                  sc->sc_sia_cap & BMSR_100TXFDX)
 5290                                 mii->mii_media_active |= IFM_100_TX|IFM_FDX;
 5291                         else if (anlpar & ANLPAR_TX &&
 5292                                  sc->sc_sia_cap & BMSR_100TXHDX)
 5293                                 mii->mii_media_active |= IFM_100_TX;
 5294                         else if (anlpar & ANLPAR_10_FD &&
 5295                                  sc->sc_sia_cap & BMSR_10TFDX)
 5296                                 mii->mii_media_active |= IFM_10_T|IFM_FDX;
 5297                         else if (anlpar & ANLPAR_10 &&
 5298                                  sc->sc_sia_cap & BMSR_10THDX)
 5299                                 mii->mii_media_active |= IFM_10_T;
 5300                         else
 5301                                 mii->mii_media_active |= IFM_NONE;
 5302                 } else {
 5303                         /*
 5304                          * If the other side doesn't support NWAY, then the
 5305                          * best we can do is determine if we have a 10Mbps or
 5306                          * 100Mbps link. There's no way to know if the link 
 5307                          * is full or half duplex, so we default to half duplex
 5308                          * and hope that the user is clever enough to manually
 5309                          * change the media settings if we're wrong.
 5310                          */
 5311                         if ((siastat & SIASTAT_LS100) == 0)
 5312                                 mii->mii_media_active |= IFM_100_TX;
 5313                         else if ((siastat & SIASTAT_LS10) == 0)
 5314                                 mii->mii_media_active |= IFM_10_T;
 5315                         else
 5316                                 mii->mii_media_active |= IFM_NONE;
 5317                 }
 5318         } else {
 5319                 if (~siastat & (SIASTAT_LS10 | SIASTAT_LS100))
 5320                         mii->mii_media_status |= IFM_ACTIVE;
 5321 
 5322                 if (sc->sc_opmode & OPMODE_TTM)
 5323                         mii->mii_media_active |= IFM_10_T;
 5324                 else
 5325                         mii->mii_media_active |= IFM_100_TX;
 5326                 if (sc->sc_opmode & OPMODE_FD)
 5327                         mii->mii_media_active |= IFM_FDX;
 5328         }
 5329 }
 5330 
 5331 void
 5332 tlp_2114x_isv_tmsw_get(sc, ifmr)
 5333         struct tulip_softc *sc;
 5334         struct ifmediareq *ifmr;
 5335 {
 5336         struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
 5337         struct tulip_21x4x_media *tm = ife->ifm_aux;
 5338 
 5339         (*tm->tm_get)(sc, ifmr);
 5340 }
 5341 
 5342 int
 5343 tlp_2114x_isv_tmsw_set(sc)
 5344         struct tulip_softc *sc;
 5345 {
 5346         struct ifmedia_entry *ife = sc->sc_mii.mii_media.ifm_cur;
 5347         struct tulip_21x4x_media *tm = ife->ifm_aux;
 5348 
 5349         /*
 5350          * Check to see if we need to reset the chip, and do it.  The
 5351          * reset path will get the OPMODE register right the next
 5352          * time through.
 5353          */
 5354         if (TULIP_MEDIA_NEEDSRESET(sc, tm->tm_opmode))
 5355                 return (tlp_init(&sc->sc_ethercom.ec_if));
 5356 
 5357         return ((*tm->tm_set)(sc));
 5358 }
 5359 
 5360 /*
 5361  * MII-on-SIO media switch.  Handles only MII attached to the SIO.
 5362  */
 5363 void    tlp_sio_mii_tmsw_init __P((struct tulip_softc *));
 5364 
 5365 const struct tulip_mediasw tlp_sio_mii_mediasw = {
 5366         tlp_sio_mii_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
 5367 };
 5368 
 5369 void
 5370 tlp_sio_mii_tmsw_init(sc)
 5371         struct tulip_softc *sc;
 5372 {
 5373         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5374 
 5375         /*
 5376          * We don't attach any media info structures to the ifmedia
 5377          * entries, so if we're using a pre-init function that needs
 5378          * that info, override it to one that doesn't.
 5379          */
 5380         if (sc->sc_preinit == tlp_2114x_preinit)
 5381                 sc->sc_preinit = tlp_2114x_mii_preinit;
 5382 
 5383         sc->sc_mii.mii_ifp = ifp;
 5384         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
 5385         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
 5386         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5387         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5388             tlp_mediastatus);
 5389         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
 5390             MII_OFFSET_ANY, 0);
 5391         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
 5392                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
 5393                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
 5394         } else {
 5395                 sc->sc_flags |= TULIPF_HAS_MII;
 5396                 sc->sc_tick = tlp_mii_tick;
 5397                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5398         }
 5399 }
 5400 
 5401 /*
 5402  * Lite-On PNIC media switch.  Must handle MII or internal NWAY.
 5403  */
 5404 void    tlp_pnic_tmsw_init __P((struct tulip_softc *));
 5405 void    tlp_pnic_tmsw_get __P((struct tulip_softc *, struct ifmediareq *));
 5406 int     tlp_pnic_tmsw_set __P((struct tulip_softc *));
 5407 
 5408 const struct tulip_mediasw tlp_pnic_mediasw = {
 5409         tlp_pnic_tmsw_init, tlp_pnic_tmsw_get, tlp_pnic_tmsw_set
 5410 };
 5411 
 5412 void    tlp_pnic_nway_statchg __P((struct device *));
 5413 void    tlp_pnic_nway_tick __P((void *));
 5414 int     tlp_pnic_nway_service __P((struct tulip_softc *, int));
 5415 void    tlp_pnic_nway_reset __P((struct tulip_softc *));
 5416 int     tlp_pnic_nway_auto __P((struct tulip_softc *, int));
 5417 void    tlp_pnic_nway_auto_timeout __P((void *));
 5418 void    tlp_pnic_nway_status __P((struct tulip_softc *));
 5419 void    tlp_pnic_nway_acomp __P((struct tulip_softc *));
 5420 
 5421 void
 5422 tlp_pnic_tmsw_init(sc)
 5423         struct tulip_softc *sc;
 5424 {
 5425         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5426         const char *sep = "";
 5427 
 5428 #define ADD(m, c)       ifmedia_add(&sc->sc_mii.mii_media, (m), (c), NULL)
 5429 #define PRINT(str)      printf("%s%s", sep, str); sep = ", "
 5430 
 5431         sc->sc_mii.mii_ifp = ifp;
 5432         sc->sc_mii.mii_readreg = tlp_pnic_mii_readreg;
 5433         sc->sc_mii.mii_writereg = tlp_pnic_mii_writereg;
 5434         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5435         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5436             tlp_mediastatus);
 5437         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
 5438             MII_OFFSET_ANY, 0);
 5439         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
 5440                 /* XXX What about AUI/BNC support? */
 5441                 printf("%s: ", sc->sc_dev.dv_xname);
 5442 
 5443                 tlp_pnic_nway_reset(sc);
 5444 
 5445                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, 0, 0),
 5446                     PNIC_NWAY_TW|PNIC_NWAY_CAP10T);
 5447                 PRINT("10baseT");
 5448 
 5449                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_10_T, IFM_FDX, 0),
 5450                     PNIC_NWAY_TW|PNIC_NWAY_FD|PNIC_NWAY_CAP10TFDX);
 5451                 PRINT("10baseT-FDX");
 5452 
 5453                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, 0, 0),
 5454                     PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_CAP100TX);
 5455                 PRINT("100baseTX");
 5456 
 5457                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_100_TX, IFM_FDX, 0),
 5458                     PNIC_NWAY_TW|PNIC_NWAY_100|PNIC_NWAY_FD|
 5459                     PNIC_NWAY_CAP100TXFDX);
 5460                 PRINT("100baseTX-FDX");
 5461 
 5462                 ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0),
 5463                     PNIC_NWAY_TW|PNIC_NWAY_RN|PNIC_NWAY_NW|
 5464                     PNIC_NWAY_CAP10T|PNIC_NWAY_CAP10TFDX|
 5465                     PNIC_NWAY_CAP100TXFDX|PNIC_NWAY_CAP100TX);
 5466                 PRINT("auto");
 5467 
 5468                 printf("\n");
 5469 
 5470                 sc->sc_statchg = tlp_pnic_nway_statchg;
 5471                 sc->sc_tick = tlp_pnic_nway_tick;
 5472                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5473         } else {
 5474                 sc->sc_flags |= TULIPF_HAS_MII;
 5475                 sc->sc_tick = tlp_mii_tick;
 5476                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5477         }
 5478 
 5479 #undef ADD
 5480 #undef PRINT
 5481 }
 5482 
 5483 void
 5484 tlp_pnic_tmsw_get(sc, ifmr)
 5485         struct tulip_softc *sc;
 5486         struct ifmediareq *ifmr;
 5487 {
 5488         struct mii_data *mii = &sc->sc_mii;
 5489 
 5490         if (sc->sc_flags & TULIPF_HAS_MII)
 5491                 tlp_mii_getmedia(sc, ifmr);
 5492         else {
 5493                 mii->mii_media_status = 0;
 5494                 mii->mii_media_active = IFM_NONE;
 5495                 tlp_pnic_nway_service(sc, MII_POLLSTAT);
 5496                 ifmr->ifm_status = sc->sc_mii.mii_media_status;
 5497                 ifmr->ifm_active = sc->sc_mii.mii_media_active; 
 5498         }
 5499 }
 5500 
 5501 int
 5502 tlp_pnic_tmsw_set(sc)
 5503         struct tulip_softc *sc;
 5504 {
 5505         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5506         struct mii_data *mii = &sc->sc_mii;
 5507 
 5508         if (sc->sc_flags & TULIPF_HAS_MII) {
 5509                 /*
 5510                  * Make sure the built-in Tx jabber timer is disabled.
 5511                  */
 5512                 TULIP_WRITE(sc, CSR_PNIC_ENDEC, PNIC_ENDEC_JDIS);
 5513 
 5514                 return (tlp_mii_setmedia(sc));
 5515         }
 5516 
 5517         if (ifp->if_flags & IFF_UP) {
 5518                 mii->mii_media_status = 0;
 5519                 mii->mii_media_active = IFM_NONE;
 5520                 return (tlp_pnic_nway_service(sc, MII_MEDIACHG));
 5521         }
 5522 
 5523         return (0);
 5524 }
 5525 
 5526 void
 5527 tlp_pnic_nway_statchg(self)
 5528         struct device *self;
 5529 {
 5530         struct tulip_softc *sc = (struct tulip_softc *)self;
 5531 
 5532         /* Idle the transmit and receive processes. */
 5533         tlp_idle(sc, OPMODE_ST|OPMODE_SR);
 5534 
 5535         sc->sc_opmode &= ~(OPMODE_TTM|OPMODE_FD|OPMODE_PS|OPMODE_PCS|
 5536             OPMODE_SCR|OPMODE_HBD);
 5537 
 5538         if (IFM_SUBTYPE(sc->sc_mii.mii_media_active) == IFM_10_T) {
 5539                 sc->sc_opmode |= OPMODE_TTM;
 5540                 TULIP_WRITE(sc, CSR_GPP,
 5541                     GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 0) |
 5542                     GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
 5543         } else {
 5544                 sc->sc_opmode |= OPMODE_PS|OPMODE_PCS|OPMODE_SCR|OPMODE_HBD;
 5545                 TULIP_WRITE(sc, CSR_GPP,
 5546                     GPP_PNIC_OUT(GPP_PNIC_PIN_SPEED_RLY, 1) |
 5547                     GPP_PNIC_OUT(GPP_PNIC_PIN_100M_LPKB, 1));
 5548         }
 5549 
 5550         if (sc->sc_mii.mii_media_active & IFM_FDX)
 5551                 sc->sc_opmode |= OPMODE_FD|OPMODE_HBD;
 5552 
 5553         /*
 5554          * Write new OPMODE bits.  This also restarts the transmit
 5555          * and receive processes.
 5556          */
 5557         TULIP_WRITE(sc, CSR_OPMODE, sc->sc_opmode);
 5558 }
 5559 
 5560 void
 5561 tlp_pnic_nway_tick(arg)
 5562         void *arg;
 5563 {
 5564         struct tulip_softc *sc = arg;
 5565         int s;
 5566 
 5567         if ((sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
 5568                 return;
 5569 
 5570         s = splnet();
 5571         tlp_pnic_nway_service(sc, MII_TICK);
 5572         splx(s);
 5573 
 5574         callout_reset(&sc->sc_tick_callout, hz, tlp_pnic_nway_tick, sc);
 5575 }
 5576 
 5577 /*
 5578  * Support for the Lite-On PNIC internal NWay block.  This is constructed
 5579  * somewhat like a PHY driver for simplicity.
 5580  */
 5581 
 5582 int
 5583 tlp_pnic_nway_service(sc, cmd)
 5584         struct tulip_softc *sc;
 5585         int cmd;
 5586 {
 5587         struct mii_data *mii = &sc->sc_mii;
 5588         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
 5589 
 5590         if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
 5591                 return (0);
 5592 
 5593         switch (cmd) {
 5594         case MII_POLLSTAT:
 5595                 /* Nothing special to do here. */
 5596                 break;
 5597 
 5598         case MII_MEDIACHG:
 5599                 switch (IFM_SUBTYPE(ife->ifm_media)) {
 5600                 case IFM_AUTO:
 5601                         (void) tlp_pnic_nway_auto(sc, 1);
 5602                         break;
 5603                 case IFM_100_T4:
 5604                         /*
 5605                          * XXX Not supported as a manual setting right now.
 5606                          */
 5607                         return (EINVAL);
 5608                 default:
 5609                         /*
 5610                          * NWAY register data is stored in the ifmedia entry.
 5611                          */
 5612                         TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
 5613                 }
 5614                 break;
 5615 
 5616         case MII_TICK:
 5617                 /*
 5618                  * Only used for autonegotiation.
 5619                  */
 5620                 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
 5621                         return (0);
 5622 
 5623                 /*
 5624                  * Check to see if we have link.  If we do, we don't
 5625                  * need to restart the autonegotiation process.
 5626                  */
 5627                 if (sc->sc_flags & TULIPF_LINK_UP)
 5628                         return (0);
 5629 
 5630                 /*
 5631                  * Only retry autonegotiation every 5 seconds.
 5632                  */
 5633                 if (++sc->sc_nway_ticks != 5)
 5634                         return (0);
 5635 
 5636                 sc->sc_nway_ticks = 0;
 5637                 tlp_pnic_nway_reset(sc);
 5638                 if (tlp_pnic_nway_auto(sc, 0) == EJUSTRETURN)
 5639                         return (0);
 5640                 break;
 5641         }
 5642 
 5643         /* Update the media status. */
 5644         tlp_pnic_nway_status(sc);
 5645 
 5646         /* Callback if something changed. */
 5647         if ((sc->sc_nway_active == NULL ||
 5648              sc->sc_nway_active->ifm_media != mii->mii_media_active) ||
 5649             cmd == MII_MEDIACHG) {
 5650                 (*sc->sc_statchg)(&sc->sc_dev);
 5651                 tlp_nway_activate(sc, mii->mii_media_active);
 5652         }
 5653         return (0);
 5654 }
 5655 
 5656 void
 5657 tlp_pnic_nway_reset(sc)
 5658         struct tulip_softc *sc;
 5659 {
 5660 
 5661         TULIP_WRITE(sc, CSR_PNIC_NWAY, PNIC_NWAY_RS);
 5662         delay(100);
 5663         TULIP_WRITE(sc, CSR_PNIC_NWAY, 0);
 5664 }
 5665 
 5666 int
 5667 tlp_pnic_nway_auto(sc, waitfor)
 5668         struct tulip_softc *sc;
 5669         int waitfor;
 5670 {
 5671         struct mii_data *mii = &sc->sc_mii;
 5672         struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
 5673         u_int32_t reg;
 5674         int i;
 5675 
 5676         if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0)
 5677                 TULIP_WRITE(sc, CSR_PNIC_NWAY, ife->ifm_data);
 5678 
 5679         if (waitfor) {
 5680                 /* Wait 500ms for it to complete. */
 5681                 for (i = 0; i < 500; i++) {
 5682                         reg = TULIP_READ(sc, CSR_PNIC_NWAY);
 5683                         if (reg & PNIC_NWAY_LPAR_MASK) {
 5684                                 tlp_pnic_nway_acomp(sc);
 5685                                 return (0);
 5686                         }
 5687                         delay(1000);
 5688                 }
 5689 #if 0
 5690                 if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
 5691                         printf("%s: autonegotiation failed to complete\n",
 5692                             sc->sc_dev.dv_xname);
 5693 #endif
 5694 
 5695                 /*
 5696                  * Don't need to worry about clearing DOINGAUTO.
 5697                  * If that's set, a timeout is pending, and it will
 5698                  * clear the flag.
 5699                  */
 5700                 return (EIO);
 5701         }
 5702 
 5703         /*
 5704          * Just let it finish asynchronously.  This is for the benefit of
 5705          * the tick handler driving autonegotiation.  Don't want 500ms
 5706          * delays all the time while the system is running!
 5707          */
 5708         if ((sc->sc_flags & TULIPF_DOINGAUTO) == 0) {
 5709                 sc->sc_flags |= TULIPF_DOINGAUTO;
 5710                 callout_reset(&sc->sc_nway_callout, hz >> 1,
 5711                     tlp_pnic_nway_auto_timeout, sc);
 5712         }
 5713         return (EJUSTRETURN);
 5714 }
 5715 
 5716 void
 5717 tlp_pnic_nway_auto_timeout(arg)
 5718         void *arg;
 5719 {
 5720         struct tulip_softc *sc = arg;
 5721         u_int32_t reg;
 5722         int s;
 5723 
 5724         s = splnet();
 5725         sc->sc_flags &= ~TULIPF_DOINGAUTO;
 5726         reg = TULIP_READ(sc, CSR_PNIC_NWAY);
 5727 #if 0
 5728         if ((reg & PNIC_NWAY_LPAR_MASK) == 0)
 5729                 printf("%s: autonegotiation failed to complete\n",
 5730                     sc->sc_dev.dv_xname);
 5731 #endif
 5732 
 5733         tlp_pnic_nway_acomp(sc);
 5734 
 5735         /* Update the media status. */
 5736         (void) tlp_pnic_nway_service(sc, MII_POLLSTAT);
 5737         splx(s);
 5738 }
 5739 
 5740 void
 5741 tlp_pnic_nway_status(sc)
 5742         struct tulip_softc *sc;
 5743 {
 5744         struct mii_data *mii = &sc->sc_mii;
 5745         u_int32_t reg;
 5746 
 5747         mii->mii_media_status = IFM_AVALID;
 5748         mii->mii_media_active = IFM_ETHER;
 5749 
 5750         reg = TULIP_READ(sc, CSR_PNIC_NWAY);
 5751 
 5752         if (sc->sc_flags & TULIPF_LINK_UP)
 5753                 mii->mii_media_status |= IFM_ACTIVE;
 5754 
 5755         if (reg & PNIC_NWAY_NW) {
 5756                 if ((reg & PNIC_NWAY_LPAR_MASK) == 0) {
 5757                         /* Erg, still trying, I guess... */
 5758                         mii->mii_media_active |= IFM_NONE;
 5759                         return;
 5760                 }
 5761 
 5762 #if 0
 5763                 if (reg & PNIC_NWAY_LPAR100T4)
 5764                         mii->mii_media_active |= IFM_100_T4;
 5765                 else
 5766 #endif
 5767                 if (reg & PNIC_NWAY_LPAR100TXFDX)
 5768                         mii->mii_media_active |= IFM_100_TX|IFM_FDX;
 5769                 else if (reg & PNIC_NWAY_LPAR100TX)
 5770                         mii->mii_media_active |= IFM_100_TX;
 5771                 else if (reg & PNIC_NWAY_LPAR10TFDX)
 5772                         mii->mii_media_active |= IFM_10_T|IFM_FDX;
 5773                 else if (reg & PNIC_NWAY_LPAR10T)
 5774                         mii->mii_media_active |= IFM_10_T;
 5775                 else
 5776                         mii->mii_media_active |= IFM_NONE;
 5777         } else {
 5778                 if (reg & PNIC_NWAY_100)
 5779                         mii->mii_media_active |= IFM_100_TX;
 5780                 else
 5781                         mii->mii_media_active |= IFM_10_T;
 5782                 if (reg & PNIC_NWAY_FD)
 5783                         mii->mii_media_active |= IFM_FDX;
 5784         }
 5785 }
 5786 
 5787 void
 5788 tlp_pnic_nway_acomp(sc)
 5789         struct tulip_softc *sc;
 5790 {
 5791         u_int32_t reg;
 5792 
 5793         reg = TULIP_READ(sc, CSR_PNIC_NWAY);
 5794         reg &= ~(PNIC_NWAY_FD|PNIC_NWAY_100|PNIC_NWAY_RN);
 5795 
 5796         if (reg & (PNIC_NWAY_LPAR100TXFDX|PNIC_NWAY_LPAR100TX))
 5797                 reg |= PNIC_NWAY_100;
 5798         if (reg & (PNIC_NWAY_LPAR10TFDX|PNIC_NWAY_LPAR100TXFDX))
 5799                 reg |= PNIC_NWAY_FD;
 5800 
 5801         TULIP_WRITE(sc, CSR_PNIC_NWAY, reg);
 5802 }
 5803 
 5804 /*
 5805  * Macronix PMAC and Lite-On PNIC-II media switch:
 5806  *
 5807  *      MX98713 and MX98713A            21140-like MII or GPIO media.
 5808  *
 5809  *      MX98713A                        21143-like MII or SIA/SYM media.
 5810  *
 5811  *      MX98715, MX98715A, MX98725,     21143-like SIA/SYM media.
 5812  *      82C115, MX98715AEC-C, -E
 5813  *
 5814  * So, what we do here is fake MII-on-SIO or ISV media info, and
 5815  * use the ISV media switch get/set functions to handle the rest.
 5816  */
 5817 
 5818 void    tlp_pmac_tmsw_init __P((struct tulip_softc *));
 5819 
 5820 const struct tulip_mediasw tlp_pmac_mediasw = {
 5821         tlp_pmac_tmsw_init, tlp_2114x_isv_tmsw_get, tlp_2114x_isv_tmsw_set
 5822 };
 5823 
 5824 const struct tulip_mediasw tlp_pmac_mii_mediasw = {
 5825         tlp_pmac_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
 5826 };
 5827 
 5828 void
 5829 tlp_pmac_tmsw_init(sc)
 5830         struct tulip_softc *sc;
 5831 {
 5832         static const u_int8_t media[] = {
 5833                 TULIP_ROM_MB_MEDIA_TP,
 5834                 TULIP_ROM_MB_MEDIA_TP_FDX,
 5835                 TULIP_ROM_MB_MEDIA_100TX,
 5836                 TULIP_ROM_MB_MEDIA_100TX_FDX,
 5837         };
 5838         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5839         struct tulip_21x4x_media *tm;
 5840 
 5841         sc->sc_mii.mii_ifp = ifp;
 5842         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
 5843         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
 5844         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5845         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5846             tlp_mediastatus);
 5847         if (sc->sc_chip == TULIP_CHIP_MX98713 ||
 5848             sc->sc_chip == TULIP_CHIP_MX98713A) {
 5849                 mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
 5850                     MII_PHY_ANY, MII_OFFSET_ANY, 0);
 5851                 if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL) {
 5852                         sc->sc_flags |= TULIPF_HAS_MII;
 5853                         sc->sc_tick = tlp_mii_tick;
 5854                         sc->sc_preinit = tlp_2114x_mii_preinit;
 5855                         sc->sc_mediasw = &tlp_pmac_mii_mediasw;
 5856                         ifmedia_set(&sc->sc_mii.mii_media,
 5857                             IFM_ETHER|IFM_AUTO);
 5858                         return;
 5859                 }
 5860         }
 5861 
 5862         switch (sc->sc_chip) {
 5863         case TULIP_CHIP_MX98713:
 5864                 tlp_add_srom_media(sc, TULIP_ROM_MB_21140_GPR,
 5865                     tlp_21140_gpio_get, tlp_21140_gpio_set, media, 4);
 5866 
 5867                 /*
 5868                  * XXX Should implement auto-sense for this someday,
 5869                  * XXX when we do the same for the 21140.
 5870                  */
 5871                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_10_T);
 5872                 break;
 5873 
 5874         default:
 5875                 tlp_add_srom_media(sc, TULIP_ROM_MB_21142_SIA,
 5876                     tlp_sia_get, tlp_sia_set, media, 2);
 5877                 tlp_add_srom_media(sc, TULIP_ROM_MB_21143_SYM,
 5878                     tlp_sia_get, tlp_sia_set, media + 2, 2);
 5879 
 5880                 tm = malloc(sizeof(*tm), M_DEVBUF, M_WAITOK|M_ZERO);
 5881                 tm->tm_name = "auto";
 5882                 tm->tm_get = tlp_2114x_nway_get;
 5883                 tm->tm_set = tlp_2114x_nway_set;
 5884                 ifmedia_add(&sc->sc_mii.mii_media,
 5885                     IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0, tm);
 5886 
 5887                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5888                 sc->sc_statchg = tlp_2114x_nway_statchg;
 5889                 sc->sc_tick = tlp_2114x_nway_tick;
 5890                 break;
 5891         }
 5892 
 5893         tlp_print_media(sc);
 5894         tlp_sia_fixup(sc);
 5895 
 5896         /* Set the LED modes. */
 5897         tlp_pmac_reset(sc);
 5898 
 5899         sc->sc_reset = tlp_pmac_reset;
 5900 }
 5901 
 5902 /*
 5903  * ADMtek AL981 media switch.  Only has internal PHY.
 5904  */
 5905 void    tlp_al981_tmsw_init __P((struct tulip_softc *));
 5906 
 5907 const struct tulip_mediasw tlp_al981_mediasw = {
 5908         tlp_al981_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
 5909 };
 5910 
 5911 void
 5912 tlp_al981_tmsw_init(sc)
 5913         struct tulip_softc *sc;
 5914 {
 5915         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5916 
 5917         sc->sc_mii.mii_ifp = ifp;
 5918         sc->sc_mii.mii_readreg = tlp_al981_mii_readreg;
 5919         sc->sc_mii.mii_writereg = tlp_al981_mii_writereg;
 5920         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5921         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5922             tlp_mediastatus);
 5923         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
 5924             MII_OFFSET_ANY, 0);
 5925         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
 5926                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
 5927                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
 5928         } else {
 5929                 sc->sc_flags |= TULIPF_HAS_MII;
 5930                 sc->sc_tick = tlp_mii_tick;
 5931                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5932         }
 5933 }
 5934 
 5935 /*
 5936  * ADMtek AN983/985 media switch.  Only has internal PHY, but
 5937  * on an SIO-like interface.  Unfortunately, we can't use the
 5938  * standard SIO media switch, because the AN985 "ghosts" the
 5939  * singly PHY at every address.
 5940  */
 5941 void    tlp_an985_tmsw_init __P((struct tulip_softc *));
 5942 
 5943 const struct tulip_mediasw tlp_an985_mediasw = {
 5944         tlp_an985_tmsw_init, tlp_mii_getmedia, tlp_mii_setmedia
 5945 };
 5946 
 5947 void
 5948 tlp_an985_tmsw_init(sc)
 5949         struct tulip_softc *sc;
 5950 {
 5951         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5952 
 5953         sc->sc_mii.mii_ifp = ifp;
 5954         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
 5955         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
 5956         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5957         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5958             tlp_mediastatus);
 5959         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, 1,
 5960             MII_OFFSET_ANY, 0);
 5961         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
 5962                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
 5963                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
 5964         } else {
 5965                 sc->sc_flags |= TULIPF_HAS_MII;
 5966                 sc->sc_tick = tlp_mii_tick;
 5967                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 5968         }
 5969 }
 5970 
 5971 /*
 5972  * Davicom DM9102 media switch.  Internal PHY and possibly HomePNA.
 5973  */
 5974 void    tlp_dm9102_tmsw_init __P((struct tulip_softc *));
 5975 void    tlp_dm9102_tmsw_getmedia __P((struct tulip_softc *,
 5976             struct ifmediareq *));
 5977 int     tlp_dm9102_tmsw_setmedia __P((struct tulip_softc *));
 5978 
 5979 const struct tulip_mediasw tlp_dm9102_mediasw = {
 5980         tlp_dm9102_tmsw_init, tlp_dm9102_tmsw_getmedia,
 5981             tlp_dm9102_tmsw_setmedia
 5982 };
 5983 
 5984 void
 5985 tlp_dm9102_tmsw_init(sc)
 5986         struct tulip_softc *sc;
 5987 {
 5988         struct ifnet *ifp = &sc->sc_ethercom.ec_if;
 5989         u_int32_t opmode;
 5990 
 5991         sc->sc_mii.mii_ifp = ifp;
 5992         sc->sc_mii.mii_readreg = tlp_bitbang_mii_readreg;
 5993         sc->sc_mii.mii_writereg = tlp_bitbang_mii_writereg;
 5994         sc->sc_mii.mii_statchg = sc->sc_statchg;
 5995         ifmedia_init(&sc->sc_mii.mii_media, 0, tlp_mediachange,
 5996             tlp_mediastatus);
 5997 
 5998         /* PHY block already reset via tlp_reset(). */
 5999 
 6000         /*
 6001          * Configure OPMODE properly for the internal MII interface.
 6002          */
 6003         switch (sc->sc_chip) {
 6004         case TULIP_CHIP_DM9102:
 6005                 opmode = OPMODE_MBO|OPMODE_HBD|OPMODE_PS;
 6006                 break;
 6007 
 6008         case TULIP_CHIP_DM9102A:
 6009                 opmode = OPMODE_MBO|OPMODE_HBD;
 6010                 break;
 6011 
 6012         default:
 6013                 opmode = 0;
 6014                 break;
 6015         }
 6016 
 6017         TULIP_WRITE(sc, CSR_OPMODE, opmode);
 6018 
 6019         /* Now, probe the internal MII for the internal PHY. */
 6020         mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
 6021             MII_OFFSET_ANY, 0);
 6022 
 6023         /*
 6024          * XXX Figure out what to do about the HomePNA portion
 6025          * XXX of the DM9102A.
 6026          */
 6027 
 6028         if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
 6029                 ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
 6030                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
 6031         } else {
 6032                 sc->sc_flags |= TULIPF_HAS_MII;
 6033                 sc->sc_tick = tlp_mii_tick;
 6034                 ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
 6035         }
 6036 }
 6037 
 6038 void
 6039 tlp_dm9102_tmsw_getmedia(sc, ifmr)
 6040         struct tulip_softc *sc;
 6041         struct ifmediareq *ifmr;
 6042 {
 6043 
 6044         /* XXX HomePNA on DM9102A. */
 6045         tlp_mii_getmedia(sc, ifmr);
 6046 }
 6047 
 6048 int
 6049 tlp_dm9102_tmsw_setmedia(sc)
 6050         struct tulip_softc *sc;
 6051 {
 6052 
 6053         /* XXX HomePNA on DM9102A. */
 6054         return (tlp_mii_setmedia(sc));
 6055 }

Cache object: ec1958e2ae3e546c8d149ed1ec524055


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