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/xe/if_xe.c

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

    1 /*-
    2  * Copyright (c) 1998, 1999, 2003  Scott Mitchell
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  */
   26 /*
   27  * Portions of this software were derived from Werner Koch's xirc2ps driver
   28  * for Linux under the terms of the following license (from v1.30 of the
   29  * xirc2ps driver):
   30  *
   31  * Copyright (c) 1997 by Werner Koch (dd9jn)
   32  *
   33  * Redistribution and use in source and binary forms, with or without
   34  * modification, are permitted provided that the following conditions
   35  * are met:
   36  * 1. Redistributions of source code must retain the above copyright
   37  *    notice, and the entire permission notice in its entirety,
   38  *    including the disclaimer of warranties.
   39  * 2. Redistributions in binary form must reproduce the above copyright
   40  *    notice, this list of conditions and the following disclaimer in the
   41  *    documentation and/or other materials provided with the distribution.
   42  * 3. The name of the author may not be used to endorse or promote
   43  *    products derived from this software without specific prior
   44  *    written permission.
   45  *
   46  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
   47  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   49  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
   50  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   51  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   52  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   53  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   54  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   55  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   56  * OF THE POSSIBILITY OF SUCH DAMAGE.
   57  */
   58 
   59 #include <sys/cdefs.h>
   60 __FBSDID("$FreeBSD: releng/5.2/sys/dev/xe/if_xe.c 122625 2003-11-13 20:55:53Z obrien $");
   61 
   62 /*              
   63  * FreeBSD device driver for Xircom CreditCard PCMCIA Ethernet adapters.  The
   64  * following cards are currently known to work with the driver:
   65  *   Xircom CreditCard 10/100 (CE3)
   66  *   Xircom CreditCard Ethernet + Modem 28 (CEM28)
   67  *   Xircom CreditCard Ethernet 10/100 + Modem 56 (CEM56)
   68  *   Xircom RealPort Ethernet 10
   69  *   Xircom RealPort Ethernet 10/100
   70  *   Xircom RealPort Ethernet 10/100 + Modem 56 (REM56, REM56G)
   71  *   Intel EtherExpress Pro/100 PC Card Mobile Adapter 16 (Pro/100 M16A)
   72  *   Compaq Netelligent 10/100 PC Card (CPQ-10/100)
   73  *
   74  * Some other cards *should* work, but support for them is either broken or in 
   75  * an unknown state at the moment.  I'm always interested in hearing from
   76  * people who own any of these cards:
   77  *   Xircom CreditCard 10Base-T (PS-CE2-10)
   78  *   Xircom CreditCard Ethernet + ModemII (CEM2)
   79  *   Xircom CEM28 and CEM33 Ethernet/Modem cards (may be variants of CEM2?)
   80  *
   81  * Thanks to all who assisted with the development and testing of the driver,
   82  * especially: Werner Koch, Duke Kamstra, Duncan Barclay, Jason George, Dru
   83  * Nelson, Mike Kephart, Bill Rainey and Douglas Rand.  Apologies if I've left
   84  * out anyone who deserves a mention here.
   85  *
   86  * Special thanks to Ade Lovett for both hosting the mailing list and doing
   87  * the CEM56/REM56 support code; and the FreeBSD UK Users' Group for hosting
   88  * the web pages.
   89  *
   90  * Author email: <scott@uk.freebsd.org>
   91  * Driver web page: http://ukug.uk.freebsd.org/~scott/xe_drv/
   92  */
   93 
   94 
   95 #include <sys/param.h>
   96 #include <sys/cdefs.h>
   97 #include <sys/errno.h>
   98 #include <sys/kernel.h>
   99 #include <sys/mbuf.h>
  100 #include <sys/socket.h>
  101 #include <sys/sockio.h>
  102 #include <sys/systm.h>
  103 #include <sys/uio.h>
  104 #include <sys/sysctl.h>
  105 
  106 #include <sys/module.h>
  107 #include <sys/bus.h>
  108 
  109 #include <machine/bus.h>
  110 #include <machine/resource.h>
  111 #include <sys/rman.h>
  112  
  113 #include <net/ethernet.h>
  114 #include <net/if.h>
  115 #include <net/if_arp.h>
  116 #include <net/if_dl.h>
  117 #include <net/if_media.h>
  118 #include <net/if_mib.h>
  119 #include <net/bpf.h>
  120 
  121 #include <dev/xe/if_xereg.h>
  122 #include <dev/xe/if_xevar.h>
  123 
  124 /*
  125  * MII command structure
  126  */
  127 struct xe_mii_frame {
  128   u_int8_t  mii_stdelim;
  129   u_int8_t  mii_opcode;
  130   u_int8_t  mii_phyaddr;
  131   u_int8_t  mii_regaddr;
  132   u_int8_t  mii_turnaround;
  133   u_int16_t mii_data;
  134 };
  135 
  136 /*
  137  * Media autonegotiation progress constants
  138  */
  139 #define XE_AUTONEG_NONE         0       /* No autonegotiation in progress */
  140 #define XE_AUTONEG_WAITING      1       /* Waiting for transmitter to go idle */
  141 #define XE_AUTONEG_STARTED      2       /* Waiting for autonegotiation to complete */
  142 #define XE_AUTONEG_100TX        3       /* Trying to force 100baseTX link */
  143 #define XE_AUTONEG_FAIL         4       /* Autonegotiation failed */
  144 
  145 /*
  146  * Multicast hashing CRC constants
  147  */
  148 #define XE_CRC_POLY  0x04c11db6
  149 
  150 /*
  151  * Prototypes start here
  152  */
  153 static void      xe_init                (void *xscp);
  154 static void      xe_start               (struct ifnet *ifp);
  155 static int       xe_ioctl               (struct ifnet *ifp, u_long command, caddr_t data);
  156 static void      xe_watchdog            (struct ifnet *ifp);
  157 static int       xe_media_change        (struct ifnet *ifp);
  158 static void      xe_media_status        (struct ifnet *ifp, struct ifmediareq *mrp);
  159 static timeout_t xe_setmedia;
  160 static void      xe_reset               (struct xe_softc *scp);
  161 static void      xe_stop                (struct xe_softc *scp);
  162 static void      xe_enable_intr         (struct xe_softc *scp);
  163 static void      xe_disable_intr        (struct xe_softc *scp);
  164 static void      xe_set_multicast       (struct xe_softc *scp);
  165 static void      xe_set_addr            (struct xe_softc *scp, u_int8_t* addr, unsigned idx);
  166 static void      xe_mchash              (struct xe_softc *scp, caddr_t addr);
  167 static int       xe_pio_write_packet    (struct xe_softc *scp, struct mbuf *mbp);
  168 
  169 /*
  170  * MII functions
  171  */
  172 static void      xe_mii_sync            (struct xe_softc *scp);
  173 static int       xe_mii_init            (struct xe_softc *scp);
  174 static void      xe_mii_send            (struct xe_softc *scp, u_int32_t bits, int cnt);
  175 static int       xe_mii_readreg         (struct xe_softc *scp, struct xe_mii_frame *frame);
  176 static int       xe_mii_writereg        (struct xe_softc *scp, struct xe_mii_frame *frame);
  177 static u_int16_t xe_phy_readreg         (struct xe_softc *scp, u_int16_t reg);
  178 static void      xe_phy_writereg        (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
  179 
  180 /*
  181  * Debugging functions
  182  */
  183 static void      xe_mii_dump            (struct xe_softc *scp);
  184 #if 0
  185 static void      xe_reg_dump            (struct xe_softc *scp);
  186 #endif
  187 
  188 /*
  189  * Debug logging levels - set with hw.xe.debug sysctl
  190  * 0 = None
  191  * 1 = More hardware details, probe/attach progress
  192  * 2 = Most function calls, ioctls and media selection progress
  193  * 3 = Everything - interrupts, packets in/out and multicast address setup
  194  */
  195 #define XE_DEBUG
  196 #ifdef XE_DEBUG
  197 
  198 /* sysctl vars */
  199 SYSCTL_NODE(_hw, OID_AUTO, xe, CTLFLAG_RD, 0, "if_xe parameters");
  200 
  201 int xe_debug = 0;
  202 SYSCTL_INT(_hw_xe, OID_AUTO, debug, CTLFLAG_RW,
  203                    &xe_debug, 0, "if_xe debug level");
  204 
  205 #define DEVPRINTF(level, arg) if (xe_debug >= (level)) device_printf arg
  206 #define DPRINTF(level, arg) if (xe_debug >= (level)) printf arg
  207 #define XE_MII_DUMP(scp) if (xe_debug >= 3) xe_mii_dump(scp)
  208 #if 0
  209 #define XE_REG_DUMP(scp) if (xe_debug >= 3) xe_reg_dump(scp)
  210 #endif
  211 #else
  212 #define DEVPRINTF(level, arg)
  213 #define DPRINTF(level, arg)
  214 #define XE_MII_DUMP(scp)
  215 #if 0
  216 #define XE_REG_DUMP(scp)
  217 #endif
  218 #endif
  219 
  220 
  221 /*
  222  * Attach a device.
  223  */
  224 int
  225 xe_attach (device_t dev)
  226 {
  227   struct xe_softc *scp = device_get_softc(dev);
  228 
  229   DEVPRINTF(2, (dev, "attach\n"));
  230 
  231   /* Initialise stuff... */
  232   scp->dev = dev;
  233   scp->ifp = &scp->arpcom.ac_if;
  234   scp->ifm = &scp->ifmedia;
  235   scp->autoneg_status = XE_AUTONEG_NONE;
  236 
  237   /* Initialise the ifnet structure */
  238   scp->ifp->if_softc = scp;
  239   if_initname(scp->ifp, device_get_name(dev), device_get_unit(dev));
  240   scp->ifp->if_timer = 0;
  241   scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
  242   scp->ifp->if_linkmib = &scp->mibdata;
  243   scp->ifp->if_linkmiblen = sizeof scp->mibdata;
  244   scp->ifp->if_output = ether_output;
  245   scp->ifp->if_start = xe_start;
  246   scp->ifp->if_ioctl = xe_ioctl;
  247   scp->ifp->if_watchdog = xe_watchdog;
  248   scp->ifp->if_init = xe_init;
  249   scp->ifp->if_baudrate = 100000000;
  250   scp->ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  251 
  252   /* Initialise the ifmedia structure */
  253   ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
  254   callout_handle_init(&scp->chand);
  255 
  256   /* Add supported media types */
  257   if (scp->mohawk) {
  258     ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
  259     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
  260     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
  261   }
  262   ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
  263   if (scp->ce2)
  264     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
  265   ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
  266 
  267   /* Default is to autoselect best supported media type */
  268   ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
  269 
  270   /* Get the hardware into a known state */
  271   xe_reset(scp);
  272 
  273   /* Get hardware version numbers */
  274   XE_SELECT_PAGE(4);
  275   scp->version = XE_INB(XE_BOV);
  276   if (scp->mohawk)
  277     scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
  278   else
  279     scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
  280 
  281   /* Print some useful information */
  282   device_printf(dev, "%s %s, version 0x%02x/0x%02x%s%s\n",
  283          scp->vendor,
  284          scp->card_type,
  285          scp->version,
  286          scp->srev,
  287          scp->mohawk ? ", 100Mbps capable" : "",
  288          scp->modem ?  ", with modem"      : "");
  289   if (scp->mohawk) {
  290     XE_SELECT_PAGE(0x10);
  291     DEVPRINTF(1, (dev, "DingoID=0x%04x, RevisionID=0x%04x, VendorID=0x%04x\n",
  292            XE_INW(XE_DINGOID),
  293            XE_INW(XE_RevID),
  294            XE_INW(XE_VendorID)));
  295   }
  296   if (scp->ce2) {
  297     XE_SELECT_PAGE(0x45);
  298     DEVPRINTF(1, (dev, "CE2 version = 0x%#02x\n", XE_INB(XE_REV)));
  299   }
  300   device_printf(dev, "Ethernet address %6D\n", scp->arpcom.ac_enaddr, ":");
  301 
  302   /* Attach the interface */
  303   ether_ifattach(scp->ifp, scp->arpcom.ac_enaddr);
  304 
  305   /* Done */
  306   return 0;
  307 }
  308 
  309 
  310 /*
  311  * Complete hardware intitialisation and enable output.  Exits without doing
  312  * anything if there's no address assigned to the card, or if media selection
  313  * is in progress (the latter implies we've already run this function).
  314  */
  315 static void
  316 xe_init(void *xscp) {
  317   struct xe_softc *scp = xscp;
  318   unsigned i;
  319   int s;
  320 
  321   if (TAILQ_EMPTY(&scp->ifp->if_addrhead)) return;
  322 
  323   if (scp->autoneg_status != XE_AUTONEG_NONE) return;
  324 
  325   DEVPRINTF(2, (scp->dev, "init\n"));
  326 
  327   s = splimp();
  328 
  329   /* Reset transmitter flags */
  330   scp->tx_queued = 0;
  331   scp->tx_tpr = 0;
  332   scp->tx_timeouts = 0;
  333   scp->tx_thres = 64;
  334   scp->tx_min = ETHER_MIN_LEN - ETHER_CRC_LEN;
  335   scp->ifp->if_timer = 0;
  336 
  337   /* Soft reset the card */
  338   XE_SELECT_PAGE(0);
  339   XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
  340   DELAY(40000);
  341   XE_OUTB(XE_CR, 0);
  342   DELAY(40000);
  343 
  344   if (scp->mohawk) {
  345     /*
  346      * set GP1 and GP2 as outputs (bits 2 & 3)
  347      * set GP1 low to power on the ML6692 (bit 0)
  348      * set GP2 high to power on the 10Mhz chip (bit 1)
  349      */
  350     XE_SELECT_PAGE(4);
  351     XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP2_OUT);
  352   }
  353 
  354   /* Shut off interrupts */
  355   xe_disable_intr(scp);
  356 
  357   /* Wait for everything to wake up */
  358   DELAY(500000);
  359 
  360   /* Check for PHY */
  361   if (scp->mohawk)
  362     scp->phy_ok = xe_mii_init(scp);
  363 
  364   /* Disable 'source insertion' (not sure what that means) */
  365   XE_SELECT_PAGE(0x42);
  366   XE_OUTB(XE_SWC0, XE_SWC0_NO_SRC_INSERT);
  367 
  368   /* Set 8K/24K Tx/Rx buffer split */
  369   if (scp->srev != 1) {
  370     XE_SELECT_PAGE(2);
  371     XE_OUTW(XE_RBS, 0x2000);
  372   }
  373 
  374   /* Enable early transmit mode on Mohawk/Dingo */
  375   if (scp->mohawk) {
  376     XE_SELECT_PAGE(0x03);
  377     XE_OUTW(XE_TPT, scp->tx_thres);
  378     XE_SELECT_PAGE(0x01);
  379     XE_OUTB(XE_ECR, XE_INB(XE_ECR) | XE_ECR_EARLY_TX);
  380   }
  381 
  382   /* Put MAC address in first 'individual address' register */
  383   XE_SELECT_PAGE(0x50);
  384   for (i = 0; i < 6; i++)
  385     XE_OUTB(0x08 + i, scp->arpcom.ac_enaddr[scp->mohawk ? 5 - i : i]);
  386 
  387   /* Set up multicast addresses */
  388   xe_set_multicast(scp);
  389 
  390   /* Fix the receive data offset -- reset can leave it off-by-one */
  391   XE_SELECT_PAGE(0);
  392   XE_OUTW(XE_DO, 0x2000);
  393 
  394   /* Set interrupt masks */
  395   XE_SELECT_PAGE(1);
  396   XE_OUTB(XE_IMR0, XE_IMR0_TX_PACKET | XE_IMR0_MAC_INTR | XE_IMR0_RX_PACKET);
  397 
  398   /* Set MAC interrupt masks */
  399   XE_SELECT_PAGE(0x40);
  400   XE_OUTB(XE_RX0Msk,
  401           ~(XE_RX0M_RX_OVERRUN | XE_RX0M_CRC_ERROR
  402             | XE_RX0M_ALIGN_ERROR | XE_RX0M_LONG_PACKET));
  403   XE_OUTB(XE_TX0Msk,
  404           ~(XE_TX0M_SQE_FAIL | XE_TX0M_LATE_COLLISION | XE_TX0M_TX_UNDERRUN
  405             | XE_TX0M_16_COLLISIONS | XE_TX0M_NO_CARRIER));
  406 
  407   /* Clear MAC status registers */
  408   XE_SELECT_PAGE(0x40);
  409   XE_OUTB(XE_RST0, 0x00);
  410   XE_OUTB(XE_TXST0, 0x00);
  411 
  412   /* Enable receiver and put MAC online */
  413   XE_SELECT_PAGE(0x40);
  414   XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
  415 
  416   /* Set up IMR, enable interrupts */
  417   xe_enable_intr(scp);
  418 
  419   /* Start media selection */
  420   xe_setmedia(scp);
  421 
  422   /* Enable output */
  423   scp->ifp->if_flags |= IFF_RUNNING;
  424   scp->ifp->if_flags &= ~IFF_OACTIVE;
  425 
  426   (void)splx(s);
  427 }
  428 
  429 
  430 /*
  431  * Start output on interface.  Should be called at splimp() priority.  Check
  432  * that the output is idle (ie, IFF_OACTIVE is not set) before calling this
  433  * function.  If media selection is in progress we set IFF_OACTIVE ourselves
  434  * and return immediately.
  435  */
  436 static void
  437 xe_start(struct ifnet *ifp) {
  438   struct xe_softc *scp = ifp->if_softc;
  439   struct mbuf *mbp;
  440 
  441   if (scp->autoneg_status != XE_AUTONEG_NONE) {
  442     ifp->if_flags |= IFF_OACTIVE;
  443     return;
  444   }
  445 
  446   DEVPRINTF(3, (scp->dev, "start\n"));
  447 
  448   /*
  449    * Loop while there are packets to be sent, and space to send them.
  450    */
  451   while (1) {
  452     /* Suck a packet off the send queue */
  453     IF_DEQUEUE(&ifp->if_snd, mbp);
  454 
  455     if (mbp == NULL) {
  456       /*
  457        * We are using the !OACTIVE flag to indicate to the outside world that
  458        * we can accept an additional packet rather than that the transmitter
  459        * is _actually_ active. Indeed, the transmitter may be active, but if
  460        * we haven't filled all the buffers with data then we still want to
  461        * accept more.
  462        */
  463       ifp->if_flags &= ~IFF_OACTIVE;
  464       return;
  465     }
  466 
  467     if (xe_pio_write_packet(scp, mbp) != 0) {
  468       /* Push the packet back onto the queue */
  469       IF_PREPEND(&ifp->if_snd, mbp);
  470       ifp->if_flags |= IFF_OACTIVE;
  471       return;
  472     }
  473 
  474     /* Tap off here if there is a bpf listener */
  475     BPF_MTAP(ifp, mbp);
  476 
  477     /* In case we don't hear from the card again... */
  478     ifp->if_timer = 5;
  479     scp->tx_queued++;
  480 
  481     m_freem(mbp);
  482   }
  483 }
  484 
  485 
  486 /*
  487  * Process an ioctl request.  Adapted from the ed driver.
  488  */
  489 static int
  490 xe_ioctl (register struct ifnet *ifp, u_long command, caddr_t data) {
  491   struct xe_softc *scp;
  492   int s, error;
  493 
  494   scp = ifp->if_softc;
  495   error = 0;
  496 
  497   s = splimp();
  498 
  499   switch (command) {
  500 
  501   case SIOCSIFFLAGS:
  502     DEVPRINTF(2, (scp->dev, "ioctl: SIOCSIFFLAGS: 0x%04x\n", ifp->if_flags));
  503     /*
  504      * If the interface is marked up and stopped, then start it.  If it is
  505      * marked down and running, then stop it.
  506      */
  507     if (ifp->if_flags & IFF_UP) {
  508       if (!(ifp->if_flags & IFF_RUNNING)) {
  509         xe_reset(scp);
  510         xe_init(scp);
  511       }
  512     }
  513     else {
  514       if (ifp->if_flags & IFF_RUNNING)
  515         xe_stop(scp);
  516     }
  517     /* FALL THROUGH  (handle changes to PROMISC/ALLMULTI flags) */
  518 
  519   case SIOCADDMULTI:
  520   case SIOCDELMULTI:
  521     DEVPRINTF(2, (scp->dev, "ioctl: SIOC{ADD,DEL}MULTI\n"));
  522     /*
  523      * Multicast list has (maybe) changed; set the hardware filters
  524      * accordingly.
  525      */
  526     xe_set_multicast(scp);
  527     error = 0;
  528     break;
  529 
  530   case SIOCSIFMEDIA:
  531   case SIOCGIFMEDIA:
  532     DEVPRINTF(3, (scp->dev, "ioctl: bounce to ifmedia_ioctl\n"));
  533     /*
  534      * Someone wants to get/set media options.
  535      */
  536     error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
  537     break;
  538 
  539   default:
  540     DEVPRINTF(3, (scp->dev, "ioctl: bounce to ether_ioctl\n"));
  541     error = ether_ioctl(ifp, command, data);
  542   }
  543 
  544   (void)splx(s);
  545 
  546   return error;
  547 }
  548 
  549 
  550 /*
  551  * Card interrupt handler.
  552  *
  553  * This function is probably more complicated than it needs to be, as it
  554  * attempts to deal with the case where multiple packets get sent between
  555  * interrupts.  This is especially annoying when working out the collision
  556  * stats.  Not sure whether this case ever really happens or not (maybe on a
  557  * slow/heavily loaded machine?) so it's probably best to leave this like it
  558  * is.
  559  *
  560  * Note that the crappy PIO used to get packets on and off the card means that 
  561  * you will spend a lot of time in this routine -- I can get my P150 to spend
  562  * 90% of its time servicing interrupts if I really hammer the network.  Could 
  563  * fix this, but then you'd start dropping/losing packets.  The moral of this
  564  * story?  If you want good network performance _and_ some cycles left over to 
  565  * get your work done, don't buy a Xircom card.  Or convince them to tell me
  566  * how to do memory-mapped I/O :)
  567  */
  568 static void
  569 xe_intr(void *xscp) 
  570 {
  571   struct xe_softc *scp = (struct xe_softc *) xscp;
  572   struct ifnet *ifp;
  573   u_int8_t psr, isr, esr, rsr, rst0, txst0, txst1, coll;
  574 
  575   ifp = &scp->arpcom.ac_if;
  576 
  577   /* Disable interrupts */
  578   if (scp->mohawk)
  579     XE_OUTB(XE_CR, 0);
  580 
  581   /* Cache current register page */
  582   psr = XE_INB(XE_PR);
  583 
  584   /* Read ISR to see what caused this interrupt */
  585   while ((isr = XE_INB(XE_ISR)) != 0) {
  586 
  587     /* 0xff might mean the card is no longer around */
  588     if (isr == 0xff) {
  589       DEVPRINTF(3, (scp->dev, "intr: interrupt received for missing card?\n"));
  590       break;
  591     }
  592 
  593     /* Read other status registers */
  594     XE_SELECT_PAGE(0x40);
  595     rst0 = XE_INB(XE_RST0);
  596     XE_OUTB(XE_RST0, 0);
  597     txst0 = XE_INB(XE_TXST0);
  598     txst1 = XE_INB(XE_TXST1);
  599     coll = txst1 & XE_TXST1_RETRY_COUNT;
  600     XE_OUTB(XE_TXST0, 0);
  601     XE_OUTB(XE_TXST1, 0);
  602     XE_SELECT_PAGE(0);
  603 
  604     DEVPRINTF(3, (scp->dev, "intr: ISR=0x%02x, RST=0x%02x, TXT=0x%02x%02x, COLL=0x%01x\n", isr, rst0, txst1, txst0, coll));
  605 
  606     if (isr & XE_ISR_TX_PACKET) {
  607       u_int8_t tpr, sent;
  608 
  609       /* Update packet count, accounting for rollover */
  610       tpr = XE_INB(XE_TPR);
  611       sent = -scp->tx_tpr + tpr;
  612 
  613       /* Update statistics if we actually sent anything */
  614       if (sent > 0) {
  615         scp->tx_tpr = tpr;
  616         scp->tx_queued -= sent;
  617         ifp->if_opackets += sent;
  618         ifp->if_collisions += coll;
  619 
  620         /*
  621          * According to the Xircom manual, Dingo will sometimes manage to
  622          * transmit a packet with triggering an interrupt.  If this happens,
  623          * we have sent > 1 and the collision count only reflects collisions
  624          * on the last packet sent (the one that triggered the interrupt).
  625          * Collision stats might therefore be a bit low, but there doesn't
  626          * seem to be anything we can do about that.
  627          */
  628 
  629         switch (coll) {
  630         case 0:
  631           break;
  632         case 1:
  633           scp->mibdata.dot3StatsSingleCollisionFrames++;
  634           scp->mibdata.dot3StatsCollFrequencies[0]++;
  635           break;
  636         default:
  637           scp->mibdata.dot3StatsMultipleCollisionFrames++;
  638           scp->mibdata.dot3StatsCollFrequencies[coll-1]++;
  639         }
  640       }
  641       ifp->if_timer = 0;
  642       ifp->if_flags &= ~IFF_OACTIVE;
  643     }
  644 
  645     /* Handle most MAC interrupts */
  646     if (isr & XE_ISR_MAC_INTR) {
  647 
  648 #if 0
  649       /* Carrier sense lost -- only in 10Mbit HDX mode */
  650       if (txst0 & XE_TXST0_NO_CARRIER || !(txst1 & XE_TXST1_LINK_STATUS)) {
  651         /* XXX - Need to update media status here */
  652         device_printf(scp->dev, "no carrier\n");
  653         ifp->if_oerrors++;
  654         scp->mibdata.dot3StatsCarrierSenseErrors++;
  655       }
  656 #endif
  657       /* Excessive collisions -- try sending again */
  658       if (txst0 & XE_TXST0_16_COLLISIONS) {
  659         ifp->if_collisions += 16;
  660         ifp->if_oerrors++;
  661         scp->mibdata.dot3StatsExcessiveCollisions++;
  662         scp->mibdata.dot3StatsMultipleCollisionFrames++;
  663         scp->mibdata.dot3StatsCollFrequencies[15]++;
  664         XE_OUTB(XE_CR, XE_CR_RESTART_TX);
  665       }
  666       /* Transmit underrun -- increase early transmit threshold */
  667       if (txst0 & XE_TXST0_TX_UNDERRUN && scp->mohawk) {
  668         DEVPRINTF(1, (scp->dev, "transmit underrun"));
  669         if (scp->tx_thres < ETHER_MAX_LEN) {
  670           if ((scp->tx_thres += 64) > ETHER_MAX_LEN)
  671             scp->tx_thres = ETHER_MAX_LEN;
  672           DPRINTF(1, (": increasing transmit threshold to %u", scp->tx_thres));
  673           XE_SELECT_PAGE(0x3);
  674           XE_OUTW(XE_TPT, scp->tx_thres);
  675           XE_SELECT_PAGE(0x0);
  676         }
  677         DPRINTF(1, ("\n"));
  678         ifp->if_oerrors++;
  679         scp->mibdata.dot3StatsInternalMacTransmitErrors++;
  680       }
  681       /* Late collision -- just complain about it */
  682       if (txst0 & XE_TXST0_LATE_COLLISION) {
  683         device_printf(scp->dev, "late collision\n");
  684         ifp->if_oerrors++;
  685         scp->mibdata.dot3StatsLateCollisions++;
  686       }
  687       /* SQE test failure -- just complain about it */
  688       if (txst0 & XE_TXST0_SQE_FAIL) {
  689         device_printf(scp->dev, "SQE test failure\n");
  690         ifp->if_oerrors++;
  691         scp->mibdata.dot3StatsSQETestErrors++;
  692       }
  693       /* Packet too long -- what happens to these */
  694       if (rst0 & XE_RST0_LONG_PACKET) {
  695         device_printf(scp->dev, "received giant packet\n");
  696         ifp->if_ierrors++;
  697         scp->mibdata.dot3StatsFrameTooLongs++;
  698       }
  699       /* CRC error -- packet dropped */
  700       if (rst0 & XE_RST0_CRC_ERROR) {
  701         device_printf(scp->dev, "CRC error\n");
  702         ifp->if_ierrors++;
  703         scp->mibdata.dot3StatsFCSErrors++;
  704       }
  705     }
  706 
  707     /* Handle received packet(s) */
  708     while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
  709       rsr = XE_INB(XE_RSR);
  710 
  711     DEVPRINTF(3, (scp->dev, "intr: ESR=0x%02x, RSR=0x%02x\n", esr, rsr));
  712 
  713       /* Make sure packet is a good one */
  714       if (rsr & XE_RSR_RX_OK) {
  715         struct ether_header *ehp;
  716         struct mbuf *mbp;
  717         u_int16_t len;
  718 
  719         len = XE_INW(XE_RBC) - ETHER_CRC_LEN;
  720 
  721     DEVPRINTF(3, (scp->dev, "intr: receive length = %d\n", len));
  722 
  723         if (len == 0) {
  724           ifp->if_iqdrops++;
  725           continue;
  726         }
  727 
  728         /*
  729          * Allocate mbuf to hold received packet.  If the mbuf header isn't
  730          * big enough, we attach an mbuf cluster to hold the packet.  Note the
  731          * +=2 to align the packet data on a 32-bit boundary, and the +3 to
  732          * allow for the possibility of reading one more byte than the actual
  733          * packet length (we always read 16-bit words).
  734          * XXX - Surely there's a better way to do this alignment?
  735          */
  736         MGETHDR(mbp, M_DONTWAIT, MT_DATA);
  737         if (mbp == NULL) {
  738           ifp->if_iqdrops++;
  739           continue;
  740         }
  741 
  742         if (len + 3 > MHLEN) {
  743           MCLGET(mbp, M_DONTWAIT);
  744           if ((mbp->m_flags & M_EXT) == 0) {
  745             m_freem(mbp);
  746             ifp->if_iqdrops++;
  747             continue;
  748           }
  749         }
  750 
  751         mbp->m_data += 2;
  752         ehp = mtod(mbp, struct ether_header *);
  753 
  754         /*
  755          * Now get the packet in PIO mode, including the Ethernet header but
  756          * omitting the trailing CRC.
  757          */
  758 
  759         /*
  760          * Work around a bug in CE2 cards.  There seems to be a problem with
  761          * duplicated and extraneous bytes in the receive buffer, but without
  762          * any real documentation for the CE2 it's hard to tell for sure.
  763          * XXX - Needs testing on CE2 hardware
  764          */
  765         if (scp->srev == 0) {
  766           u_short rhs;
  767 
  768           XE_SELECT_PAGE(5);
  769           rhs = XE_INW(XE_RHSA);
  770           XE_SELECT_PAGE(0);
  771 
  772           rhs += 3;                      /* Skip control info */
  773 
  774           if (rhs >= 0x8000)
  775             rhs = 0;
  776 
  777           if (rhs + len > 0x8000) {
  778             int i;
  779 
  780             for (i = 0; i < len; i++, rhs++) {
  781               ((char *)ehp)[i] = XE_INB(XE_EDP);
  782               if (rhs == 0x8000) {
  783                 rhs = 0;
  784                 i--;
  785               }
  786             }
  787           }
  788           else
  789             bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
  790                                    (u_int16_t *) ehp, (len + 1) >> 1);
  791         }
  792         else
  793           bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
  794                                  (u_int16_t *) ehp, (len + 1) >> 1);
  795 
  796         /* Deliver packet to upper layers */
  797         mbp->m_pkthdr.rcvif = ifp;
  798         mbp->m_pkthdr.len = mbp->m_len = len;
  799         (*ifp->if_input)(ifp, mbp);
  800         ifp->if_ipackets++;
  801       }
  802 
  803       /* Packet alignment error -- drop packet */
  804       else if (rsr & XE_RSR_ALIGN_ERROR) {
  805         device_printf(scp->dev, "alignment error\n");
  806         scp->mibdata.dot3StatsAlignmentErrors++;
  807         ifp->if_ierrors++;
  808       }
  809 
  810       /* Skip to next packet, if there is one */
  811       XE_OUTW(XE_DO, 0x8000);
  812     }
  813 
  814     /* Clear receiver overruns now we have some free buffer space */
  815     if (rst0 & XE_RST0_RX_OVERRUN) {
  816       DEVPRINTF(1, (scp->dev, "receive overrun\n"));
  817       ifp->if_ierrors++;
  818       scp->mibdata.dot3StatsInternalMacReceiveErrors++;
  819       XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
  820     }
  821   }
  822 
  823   /* Restore saved page */
  824   XE_SELECT_PAGE(psr);
  825 
  826   /* Re-enable interrupts */
  827   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);
  828 
  829   return;
  830 }
  831 
  832 
  833 /*
  834  * Device timeout/watchdog routine.  Called automatically if we queue a packet 
  835  * for transmission but don't get an interrupt within a specified timeout
  836  * (usually 5 seconds).  When this happens we assume the worst and reset the
  837  * card.
  838  */
  839 static void
  840 xe_watchdog(struct ifnet *ifp) {
  841   struct xe_softc *scp = ifp->if_softc;
  842 
  843   device_printf(scp->dev, "watchdog timeout: resetting card\n");
  844   scp->tx_timeouts++;
  845   ifp->if_oerrors += scp->tx_queued;
  846   xe_stop(scp);
  847   xe_reset(scp);
  848   xe_init(scp);
  849 }
  850 
  851 
  852 /*
  853  * Change media selection.
  854  */
  855 static int
  856 xe_media_change(struct ifnet *ifp) {
  857   struct xe_softc *scp = ifp->if_softc;
  858 
  859   DEVPRINTF(2, (scp->dev, "media_change\n"));
  860 
  861   if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
  862     return(EINVAL);
  863 
  864   /*
  865    * Some card/media combos aren't always possible -- filter those out here.
  866    */
  867   if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
  868        IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
  869     return (EINVAL);
  870 
  871   xe_setmedia(scp);
  872 
  873   return 0;
  874 }
  875 
  876 
  877 /*
  878  * Return current media selection.
  879  */
  880 static void
  881 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
  882   struct xe_softc *scp = ifp->if_softc;
  883 
  884   DEVPRINTF(3, (scp->dev, "media_status\n"));
  885 
  886   /* XXX - This is clearly wrong.  Will fix once I have CE2 working */
  887   mrp->ifm_status = IFM_AVALID | IFM_ACTIVE;
  888   mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
  889 
  890   return;
  891 }
  892 
  893 
  894 /*
  895  * Select active media.
  896  */
  897 static void xe_setmedia(void *xscp) {
  898   struct xe_softc *scp = xscp;
  899   u_int16_t bmcr, bmsr, anar, lpar;
  900 
  901   DEVPRINTF(2, (scp->dev, "setmedia\n"));
  902 
  903   /* Cancel any pending timeout */
  904   untimeout(xe_setmedia, scp, scp->chand);
  905   xe_disable_intr(scp);
  906 
  907   /* Select media */
  908   scp->media = IFM_ETHER;
  909   switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
  910 
  911    case IFM_AUTO:       /* Autoselect media */
  912     scp->media = IFM_ETHER|IFM_AUTO;
  913 
  914     /*
  915      * Autoselection is really awful.  It goes something like this:
  916      *
  917      * Wait until the transmitter goes idle (2sec timeout).
  918      * Reset card
  919      *   IF a 100Mbit PHY exists
  920      *     Start NWAY autonegotiation (3.5sec timeout)
  921      *     IF that succeeds
  922      *       Select 100baseTX or 10baseT, whichever was detected
  923      *     ELSE
  924      *       Reset card
  925      *       IF a 100Mbit PHY exists
  926      *         Try to force a 100baseTX link (3sec timeout)
  927      *         IF that succeeds
  928      *           Select 100baseTX
  929      *         ELSE
  930      *           Disable the PHY
  931      *         ENDIF
  932      *       ENDIF
  933      *     ENDIF
  934      *   ENDIF
  935      * IF nothing selected so far
  936      *   IF a 100Mbit PHY exists
  937      *     Select 10baseT
  938      *   ELSE
  939      *     Select 10baseT or 10base2, whichever is connected
  940      *   ENDIF
  941      * ENDIF
  942      */
  943     switch (scp->autoneg_status) {
  944 
  945     case XE_AUTONEG_NONE:
  946       DEVPRINTF(2, (scp->dev, "Waiting for idle transmitter\n"));
  947       scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
  948       scp->autoneg_status = XE_AUTONEG_WAITING;
  949       /* FALL THROUGH */
  950 
  951     case XE_AUTONEG_WAITING:
  952       if (scp->tx_queued != 0) {
  953         scp->chand = timeout(xe_setmedia, scp, hz/2);
  954         return;
  955       }
  956       if (scp->phy_ok) {
  957         DEVPRINTF(2, (scp->dev, "Starting autonegotiation\n"));
  958         bmcr = xe_phy_readreg(scp, PHY_BMCR);
  959         bmcr &= ~(PHY_BMCR_AUTONEGENBL);
  960         xe_phy_writereg(scp, PHY_BMCR, bmcr);
  961         anar = xe_phy_readreg(scp, PHY_ANAR);
  962         anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
  963         anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
  964         xe_phy_writereg(scp, PHY_ANAR, anar);
  965         bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
  966         xe_phy_writereg(scp, PHY_BMCR, bmcr);
  967         scp->autoneg_status = XE_AUTONEG_STARTED;
  968         scp->chand = timeout(xe_setmedia, scp, hz * 7/2);
  969         return;
  970       }
  971       else {
  972         scp->autoneg_status = XE_AUTONEG_FAIL;
  973       }
  974       break;
  975 
  976      case XE_AUTONEG_STARTED:
  977       bmsr = xe_phy_readreg(scp, PHY_BMSR);
  978       lpar = xe_phy_readreg(scp, PHY_LPAR);
  979       if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
  980         DEVPRINTF(2, (scp->dev, "Autonegotiation complete!\n"));
  981         /*
  982          * XXX - Shouldn't have to do this, but (on my hub at least) the
  983          * XXX - transmitter won't work after a successful autoneg.  So we see 
  984          * XXX - what the negotiation result was and force that mode.  I'm
  985          * XXX - sure there is an easy fix for this.
  986          */
  987         if (lpar & PHY_LPAR_100BTXHALF) {
  988           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
  989           XE_MII_DUMP(scp);
  990           XE_SELECT_PAGE(2);
  991           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
  992           scp->media = IFM_ETHER|IFM_100_TX;
  993           scp->autoneg_status = XE_AUTONEG_NONE;
  994         }
  995         else {
  996           /*
  997            * XXX - Bit of a hack going on in here.
  998            * XXX - This is derived from Ken Hughes patch to the Linux driver
  999            * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
 1000            * XXX - cards.  What's a CE3B and how's it differ from a plain CE3?
 1001            * XXX - these are the things we need to find out.
 1002            */
 1003           xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1004           XE_SELECT_PAGE(2);
 1005           /* BEGIN HACK */
 1006           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1007           XE_SELECT_PAGE(0x42);
 1008           XE_OUTB(XE_SWC1, 0x80);
 1009           scp->media = IFM_ETHER|IFM_10_T;
 1010           scp->autoneg_status = XE_AUTONEG_NONE;
 1011           /* END HACK */
 1012           /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/  /* Disable PHY? */
 1013           /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
 1014         }
 1015       }
 1016       else {
 1017         DEVPRINTF(2, (scp->dev, "Autonegotiation failed; trying 100baseTX\n"));
 1018         XE_MII_DUMP(scp);
 1019         if (scp->phy_ok) {
 1020           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
 1021           scp->autoneg_status = XE_AUTONEG_100TX;
 1022           scp->chand = timeout(xe_setmedia, scp, hz * 3);
 1023           return;
 1024         }
 1025         else {
 1026           scp->autoneg_status = XE_AUTONEG_FAIL;
 1027         }
 1028       }
 1029       break;
 1030 
 1031      case XE_AUTONEG_100TX:
 1032       (void)xe_phy_readreg(scp, PHY_BMSR);
 1033       bmsr = xe_phy_readreg(scp, PHY_BMSR);
 1034       if (bmsr & PHY_BMSR_LINKSTAT) {
 1035         DEVPRINTF(2, (scp->dev, "Got 100baseTX link!\n"));
 1036         XE_MII_DUMP(scp);
 1037         XE_SELECT_PAGE(2);
 1038         XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1039         scp->media = IFM_ETHER|IFM_100_TX;
 1040         scp->autoneg_status = XE_AUTONEG_NONE;
 1041       }
 1042       else {
 1043         DEVPRINTF(2, (scp->dev, "Autonegotiation failed; disabling PHY\n"));
 1044         XE_MII_DUMP(scp);
 1045         xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1046         XE_SELECT_PAGE(2);
 1047         XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);        /* Disable PHY? */
 1048         scp->autoneg_status = XE_AUTONEG_FAIL;
 1049       }
 1050       break;
 1051     }
 1052 
 1053     /*
 1054      * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
 1055      * either autonegotiation failed, or never got started to begin with.  In
 1056      * either case, select a suitable 10Mbit media and hope it works.  We
 1057      * don't need to reset the card again, since it will have been done
 1058      * already by the big switch above.
 1059      */
 1060     if (scp->autoneg_status == XE_AUTONEG_FAIL) {
 1061       DEVPRINTF(2, (scp->dev, "Selecting 10baseX\n"));
 1062       if (scp->mohawk) {
 1063         XE_SELECT_PAGE(0x42);
 1064         XE_OUTB(XE_SWC1, 0x80);
 1065         scp->media = IFM_ETHER|IFM_10_T;
 1066         scp->autoneg_status = XE_AUTONEG_NONE;
 1067       }
 1068       else {
 1069         XE_SELECT_PAGE(4);
 1070         XE_OUTB(XE_GPR0, 4);
 1071         DELAY(50000);
 1072         XE_SELECT_PAGE(0x42);
 1073         XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
 1074         scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
 1075         scp->autoneg_status = XE_AUTONEG_NONE;
 1076       }
 1077     }
 1078     break;
 1079 
 1080 
 1081     /*
 1082      * If a specific media has been requested, we just reset the card and
 1083      * select it (one small exception -- if 100baseTX is requested by there is 
 1084      * no PHY, we fall back to 10baseT operation).
 1085      */
 1086    case IFM_100_TX:     /* Force 100baseTX */
 1087     if (scp->phy_ok) {
 1088       DEVPRINTF(2, (scp->dev, "Selecting 100baseTX\n"));
 1089       XE_SELECT_PAGE(0x42);
 1090       XE_OUTB(XE_SWC1, 0);
 1091       xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
 1092       XE_SELECT_PAGE(2);
 1093       XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1094       scp->media |= IFM_100_TX;
 1095       break;
 1096     }
 1097     /* FALLTHROUGH */
 1098 
 1099    case IFM_10_T:       /* Force 10baseT */
 1100     DEVPRINTF(2, (scp->dev, "Selecting 10baseT\n"));
 1101     if (scp->phy_ok) {
 1102       xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1103       XE_SELECT_PAGE(2);
 1104       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);  /* Disable PHY */
 1105     }
 1106     XE_SELECT_PAGE(0x42);
 1107     XE_OUTB(XE_SWC1, 0x80);
 1108     scp->media |= IFM_10_T;
 1109     break;
 1110 
 1111    case IFM_10_2:
 1112     DEVPRINTF(2, (scp->dev, "Selecting 10base2\n"));
 1113     XE_SELECT_PAGE(0x42);
 1114     XE_OUTB(XE_SWC1, 0xc0);
 1115     scp->media |= IFM_10_2;
 1116     break;
 1117   }
 1118 
 1119 
 1120   /*
 1121    * Finally, the LEDs are set to match whatever media was chosen and the
 1122    * transmitter is unblocked. 
 1123    */
 1124   DEVPRINTF(2, (scp->dev, "Setting LEDs\n"));
 1125   XE_SELECT_PAGE(2);
 1126   switch (IFM_SUBTYPE(scp->media)) {
 1127    case IFM_100_TX:
 1128    case IFM_10_T:
 1129     XE_OUTB(XE_LED, 0x3b);
 1130     if (scp->dingo)
 1131       XE_OUTB(0x0b, 0x04);      /* 100Mbit LED */
 1132     break;
 1133 
 1134    case IFM_10_2:
 1135     XE_OUTB(XE_LED, 0x3a);
 1136     break;
 1137   }
 1138 
 1139   /* Restart output? */
 1140   xe_enable_intr(scp);
 1141   scp->ifp->if_flags &= ~IFF_OACTIVE;
 1142   xe_start(scp->ifp);
 1143 }
 1144 
 1145 
 1146 /*
 1147  * Hard reset (power cycle) the card.
 1148  */
 1149 static void
 1150 xe_reset(struct xe_softc *scp) {
 1151   int s;
 1152 
 1153   DEVPRINTF(2, (scp->dev, "reset\n"));
 1154 
 1155   s = splimp();
 1156 
 1157   /* Power down */
 1158   XE_SELECT_PAGE(4);
 1159   XE_OUTB(XE_GPR1, 0);
 1160   DELAY(40000);
 1161 
 1162   /* Power up again */
 1163   if (scp->mohawk)
 1164     XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN);
 1165   else
 1166     XE_OUTB(XE_GPR1, XE_GPR1_POWER_DOWN|XE_GPR1_AIC);
 1167 
 1168   DELAY(40000);
 1169   XE_SELECT_PAGE(0);
 1170 
 1171   (void)splx(s);
 1172 }
 1173 
 1174 
 1175 /*
 1176  * Take interface offline.  This is done by powering down the device, which I
 1177  * assume means just shutting down the transceiver and Ethernet logic.  This
 1178  * requires a _hard_ reset to recover from, as we need to power up again.
 1179  */
 1180 static void
 1181 xe_stop(struct xe_softc *scp) {
 1182   int s;
 1183 
 1184   DEVPRINTF(2, (scp->dev, "stop\n"));
 1185 
 1186   s = splimp();
 1187 
 1188   /*
 1189    * Shut off interrupts.
 1190    */
 1191   xe_disable_intr(scp);
 1192 
 1193   /*
 1194    * Power down.
 1195    */
 1196   XE_SELECT_PAGE(4);
 1197   XE_OUTB(XE_GPR1, 0);
 1198   XE_SELECT_PAGE(0);
 1199   if (scp->mohawk) {
 1200     /*
 1201      * set GP1 and GP2 as outputs (bits 2 & 3)
 1202      * set GP1 high to power on the ML6692 (bit 0)
 1203      * set GP2 low to power on the 10Mhz chip (bit 1)
 1204      */
 1205     XE_SELECT_PAGE(4);
 1206     XE_OUTB(XE_GPR0, XE_GPR0_GP2_SELECT|XE_GPR0_GP1_SELECT|XE_GPR0_GP1_OUT);
 1207   }
 1208 
 1209   /*
 1210    * ~IFF_RUNNING == interface down.
 1211    */
 1212   scp->ifp->if_flags &= ~IFF_RUNNING;
 1213   scp->ifp->if_flags &= ~IFF_OACTIVE;
 1214   scp->ifp->if_timer = 0;
 1215 
 1216   (void)splx(s);
 1217 }
 1218 
 1219 
 1220 /*
 1221  * Enable interrupts from the card.
 1222  */
 1223 static void
 1224 xe_enable_intr(struct xe_softc *scp) {
 1225 
 1226   DEVPRINTF(2, (scp->dev, "enable_intr\n"));
 1227 
 1228   XE_SELECT_PAGE(0);
 1229   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);    /* Enable interrupts */
 1230   if (scp->modem && !scp->dingo) {      /* This bit is just magic */
 1231     if (!(XE_INB(0x10) & 0x01)) {
 1232       XE_OUTB(0x10, 0x11);              /* Unmask master int enable bit */
 1233     }
 1234   }
 1235 }
 1236 
 1237 
 1238 /*
 1239  * Disable interrupts from the card.
 1240  */
 1241 static void
 1242 xe_disable_intr(struct xe_softc *scp) {
 1243 
 1244   DEVPRINTF(2, (scp->dev, "disable_intr\n"));
 1245 
 1246   XE_SELECT_PAGE(0);
 1247   XE_OUTB(XE_CR, 0);                    /* Disable interrupts */
 1248   if (scp->modem && !scp->dingo) {      /* More magic */
 1249     XE_OUTB(0x10, 0x10);                /* Mask the master int enable bit */
 1250   }
 1251 }
 1252 
 1253 
 1254 /*
 1255  * Set up multicast filter and promiscuous modes.
 1256  */
 1257 static void
 1258 xe_set_multicast(struct xe_softc *scp) {
 1259   struct ifnet *ifp;
 1260   struct ifmultiaddr *maddr;
 1261   unsigned count, i;
 1262 
 1263   DEVPRINTF(2, (scp->dev, "set_multicast\n"));
 1264 
 1265   ifp = &scp->arpcom.ac_if;
 1266   XE_SELECT_PAGE(0x42);
 1267 
 1268   /* Handle PROMISC flag */
 1269   if (ifp->if_flags & IFF_PROMISC) {
 1270     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_PROMISCUOUS);
 1271     return;
 1272   }
 1273   else
 1274     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_PROMISCUOUS);
 1275 
 1276   /* Handle ALLMULTI flag */
 1277   if (ifp->if_flags & IFF_ALLMULTI) {
 1278     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) | XE_SWC1_ALLMULTI);
 1279     return;
 1280   }
 1281   else
 1282     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI);
 1283 
 1284   /* Iterate over multicast address list */
 1285   count = 0;
 1286 #if __FreeBSD_version < 500000
 1287   LIST_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
 1288 #else
 1289   TAILQ_FOREACH(maddr, &ifp->if_multiaddrs, ifma_link) {
 1290 #endif
 1291     if (maddr->ifma_addr->sa_family != AF_LINK)
 1292       continue;
 1293 
 1294     count++;
 1295 
 1296     if (count < 10)
 1297       /* First 9 use Individual Addresses for exact matching */
 1298       xe_set_addr(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr), count);
 1299     else
 1300       if (scp->mohawk)
 1301         /* Use hash filter on Mohawk and Dingo */
 1302         xe_mchash(scp, LLADDR((struct sockaddr_dl *)maddr->ifma_addr));
 1303       else
 1304         /* Nowhere else to put them on CE2 */
 1305         break;
 1306   }
 1307 
 1308   DEVPRINTF(2, (scp->dev, "set_multicast: count = %u\n", count));
 1309 
 1310   /* Now do some cleanup and enable multicast handling as needed */
 1311   if (count == 0) {
 1312     /* Disable all multicast handling */
 1313     XE_SELECT_PAGE(0x42);
 1314     XE_OUTB(XE_SWC1, XE_INB(XE_SWC1) & ~(XE_SWC1_IA_ENABLE|XE_SWC1_ALLMULTI));
 1315     if (scp->mohawk) {
 1316       XE_SELECT_PAGE(0x02);
 1317       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
 1318     }
 1319   }
 1320   else if (count < 10) {
 1321     /* Full in any unused Individual Addresses with our MAC address */
 1322     for (i = count + 1; i < 10; i++)
 1323       xe_set_addr(scp, (u_int8_t *)(&scp->arpcom.ac_enaddr), i);
 1324     /* Enable Individual Address matching only */
 1325     XE_SELECT_PAGE(0x42);
 1326     XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
 1327     if (scp->mohawk) {
 1328       XE_SELECT_PAGE(0x02);
 1329       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
 1330     }
 1331   }    
 1332   else {
 1333     if (scp->mohawk) {
 1334       /* Check whether hash table is full */
 1335       XE_SELECT_PAGE(0x58);
 1336       for (i = 0x08; i < 0x10; i++)
 1337         if (XE_INB(i) != 0xff)
 1338           break;
 1339       if (i == 0x10) {
 1340         /* Hash table full - enable promiscuous multicast matching */
 1341         XE_SELECT_PAGE(0x42);
 1342         XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
 1343         XE_SELECT_PAGE(0x02);
 1344         XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~XE_MSR_HASH_TABLE);
 1345       }
 1346       else {
 1347         /* Enable hash table and Individual Address matching */
 1348         XE_SELECT_PAGE(0x42);
 1349         XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_ALLMULTI) | XE_SWC1_IA_ENABLE);
 1350         XE_SELECT_PAGE(0x02);
 1351         XE_OUTB(XE_MSR, XE_INB(XE_MSR) | XE_MSR_HASH_TABLE);
 1352       }
 1353     }
 1354     else {
 1355       /* Enable promiscuous multicast matching */
 1356       XE_SELECT_PAGE(0x42);
 1357       XE_OUTB(XE_SWC1, (XE_INB(XE_SWC1) & ~XE_SWC1_IA_ENABLE) | XE_SWC1_ALLMULTI);
 1358     }
 1359   }
 1360 
 1361   XE_SELECT_PAGE(0);
 1362 }
 1363 
 1364 
 1365 /*
 1366  * Copy the Ethernet multicast address in addr to the on-chip registers for
 1367  * Individual Address idx.  Assumes that addr is really a multicast address
 1368  * and that idx > 0 (slot 0 is always used for the card MAC address).
 1369  */
 1370 static void
 1371 xe_set_addr(struct xe_softc *scp, u_int8_t* addr, unsigned idx) {
 1372   u_int8_t page, reg;
 1373   unsigned i;
 1374 
 1375   /*
 1376    * Individual Addresses are stored in registers 8-F of pages 0x50-0x57.  IA1
 1377    * therefore starts at register 0xE on page 0x50.  The expressions below
 1378    * compute the starting page and register for any IA index > 0.
 1379    */
 1380   --idx;
 1381   page = 0x50 + idx%4 + idx/4*3;
 1382   reg = 0x0e - 2 * (idx%4);
 1383 
 1384   DEVPRINTF(3, (scp->dev, "set_addr: idx = %u, page = 0x%02x, reg = 0x%02x\n",
 1385                 idx+1, page, reg));
 1386 
 1387   /*
 1388    * Copy the IA bytes.  Note that the byte order is reversed for Mohawk and
 1389    * Dingo wrt. CE2 hardware.
 1390    */
 1391   XE_SELECT_PAGE(page);
 1392   for (i = 0; i < 6; i++) {
 1393     if (i > 0) {
 1394       DPRINTF(3, (":%02x", addr[i]));
 1395     }
 1396     else {
 1397       DEVPRINTF(3, (scp->dev, "set_addr: %02x", addr[0]));
 1398     }
 1399     XE_OUTB(reg, addr[scp->mohawk ? 5 - i : i]);
 1400     if (++reg == 0x10) {
 1401       reg = 0x08;
 1402       XE_SELECT_PAGE(++page);
 1403     }
 1404   }
 1405   DPRINTF(3, ("\n"));
 1406 }
 1407 
 1408 
 1409 /*
 1410  * Set the appropriate bit in the multicast hash table for the supplied
 1411  * Ethernet multicast address addr.  Assumes that addr is really a multicast
 1412  * address.
 1413  */
 1414 static void
 1415 xe_mchash(struct xe_softc* scp, caddr_t addr) {
 1416   u_int32_t crc = 0xffffffff;
 1417   int idx, bit;
 1418   u_int8_t carry, byte, data, crc31, hash;
 1419 
 1420   /* Compute CRC of the address -- standard Ethernet CRC function */
 1421   for (data = *addr++, idx = 0; idx < 6; idx++, data >>= 1) {
 1422     for (bit = 1; bit <= 8; bit++) {
 1423       if (crc & 0x80000000)
 1424         crc31 = 0x01;
 1425       else
 1426         crc31 = 0;
 1427       carry = crc31 ^ (data & 0x01);
 1428       crc <<= 1;
 1429       data >>= 1;
 1430         crc = (crc ^ XE_CRC_POLY) | (carry|0x1);
 1431     }
 1432   }
 1433 
 1434   DEVPRINTF(3, (scp->dev, "set_hash: CRC = 0x%08x\n", crc));
 1435 
 1436   /*
 1437    * Convert a CRC into an index into the multicast hash table.  What we do is
 1438    * take the most-significant 6 bits of the CRC, reverse them, and use that as
 1439    * the bit number in the hash table.  Bits 5:3 of the result give the byte
 1440    * within the table (0-7); bits 2:0 give the bit number within that byte (also
 1441    * 0-7), ie. the number of shifts needed to get it into the lsb position.
 1442    */
 1443   for (idx = 0, hash = 0; idx < 6; idx++) {
 1444     hash >>= 1;
 1445     if (crc & 0x80000000) {
 1446       hash |= 0x20;
 1447     }
 1448     crc <<= 1;
 1449   }
 1450 
 1451   /* Top 3 bits of hash give register - 8, bottom 3 give bit within register */
 1452   byte = hash >> 3 | 0x08;
 1453   carry = 0x01 << (hash & 0x07);
 1454 
 1455   DEVPRINTF(3, (scp->dev, "set_hash: hash = 0x%02x, byte = 0x%02x, carry = 0x%02x\n", hash, byte, carry));
 1456 
 1457   XE_SELECT_PAGE(0x58);
 1458   XE_OUTB(byte, XE_INB(byte) | carry);
 1459 }
 1460 
 1461 
 1462 /*
 1463  * Write an outgoing packet to the card using programmed I/O.
 1464  */
 1465 static int
 1466 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
 1467   unsigned len, pad;
 1468   unsigned char wantbyte;
 1469   u_int8_t *data;
 1470   u_int8_t savebyte[2];
 1471 
 1472   /* Get total packet length */
 1473   if (mbp->m_flags & M_PKTHDR)
 1474     len = mbp->m_pkthdr.len;
 1475   else {
 1476     struct mbuf* mbp2 = mbp;
 1477     for (len = 0; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
 1478   }
 1479 
 1480   DEVPRINTF(3, (scp->dev, "pio_write_packet: len = %u\n", len));
 1481 
 1482   /* Packets < minimum length may need to be padded out */
 1483   pad = 0;
 1484   if (len < scp->tx_min) {
 1485     pad = scp->tx_min - len;
 1486     len = scp->tx_min;
 1487   }
 1488 
 1489   /* Check transmit buffer space */
 1490   XE_SELECT_PAGE(0);
 1491   XE_OUTW(XE_TRS, len+2);       /* Only effective on rev. 1 CE2 cards */
 1492   if ((XE_INW(XE_TSO) & 0x7fff) <= len + 2)
 1493     return 1;
 1494 
 1495   /* Send packet length to card */
 1496   XE_OUTW(XE_EDP, len);
 1497 
 1498   /*
 1499    * Write packet to card using PIO (code stolen from the ed driver)
 1500    */
 1501   wantbyte = 0;
 1502   while (mbp != NULL) {
 1503     len = mbp->m_len;
 1504     if (len > 0) {
 1505       data = mtod(mbp, caddr_t);
 1506       if (wantbyte) {           /* Finish the last word */
 1507         savebyte[1] = *data;
 1508         XE_OUTW(XE_EDP, *(u_short *)savebyte);
 1509         data++;
 1510         len--;
 1511         wantbyte = 0;
 1512       }
 1513       if (len > 1) {            /* Output contiguous words */
 1514         bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
 1515                                 len >> 1);
 1516         data += len & ~1;
 1517         len &= 1;
 1518       }
 1519       if (len == 1) {           /* Save last byte, if necessary */
 1520         savebyte[0] = *data;
 1521         wantbyte = 1;
 1522       }
 1523     }
 1524     mbp = mbp->m_next;
 1525   }
 1526 
 1527   /*
 1528    * Send last byte of odd-length packets
 1529    */
 1530   if (wantbyte)
 1531     XE_OUTB(XE_EDP, savebyte[0]);
 1532 
 1533   /*
 1534    * Can just tell CE3 cards to send; short packets will be padded out with
 1535    * random cruft automatically.  For CE2, manually pad the packet with
 1536    * garbage; it will be sent when the required number or bytes have been
 1537    * delivered to the card.
 1538    */
 1539   if (scp->mohawk)
 1540     XE_OUTB(XE_CR, XE_CR_TX_PACKET | XE_CR_RESTART_TX | XE_CR_ENABLE_INTR);
 1541   else if (pad > 0) {
 1542     if (pad & 0x01)
 1543       XE_OUTB(XE_EDP, 0xaa);
 1544     pad >>= 1;
 1545     while (pad > 0) {
 1546       XE_OUTW(XE_EDP, 0xdead);
 1547       pad--;
 1548     }
 1549   }
 1550 
 1551   return 0;
 1552 }
 1553 
 1554 
 1555 
 1556 /**************************************************************
 1557  *                                                            *
 1558  *                  M I I  F U N C T I O N S                  *
 1559  *                                                            *
 1560  **************************************************************/
 1561 
 1562 /*
 1563  * Alternative MII/PHY handling code adapted from the xl driver.  It doesn't
 1564  * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
 1565  * XXX - this stuff shouldn't be here.  It should all be abstracted off to
 1566  * XXX - some kind of common MII-handling code, shared by all drivers.  But
 1567  * XXX - that's a whole other mission.
 1568  */
 1569 #define XE_MII_SET(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
 1570 #define XE_MII_CLR(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
 1571 
 1572 
 1573 /*
 1574  * Sync the PHYs by setting data bit and strobing the clock 32 times.
 1575  */
 1576 static void
 1577 xe_mii_sync(struct xe_softc *scp) {
 1578   register int i;
 1579 
 1580   XE_SELECT_PAGE(2);
 1581   XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
 1582 
 1583   for (i = 0; i < 32; i++) {
 1584     XE_MII_SET(XE_MII_CLK);
 1585     DELAY(1);
 1586     XE_MII_CLR(XE_MII_CLK);
 1587     DELAY(1);
 1588   }
 1589 }
 1590 
 1591 
 1592 /*
 1593  * Look for a MII-compliant PHY.  If we find one, reset it.
 1594  */
 1595 static int
 1596 xe_mii_init(struct xe_softc *scp) {
 1597   u_int16_t status;
 1598 
 1599   status = xe_phy_readreg(scp, PHY_BMSR);
 1600   if ((status & 0xff00) != 0x7800) {
 1601     DEVPRINTF(2, (scp->dev, "no PHY found, %0x\n", status));
 1602     return 0;
 1603   }
 1604   else {
 1605     DEVPRINTF(2, (scp->dev, "PHY OK!\n"));
 1606 
 1607     /* Reset the PHY */
 1608     xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
 1609     DELAY(500);
 1610     while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
 1611     XE_MII_DUMP(scp);
 1612     return 1;
 1613   }
 1614 }
 1615 
 1616 
 1617 /*
 1618  * Clock a series of bits through the MII.
 1619  */
 1620 static void
 1621 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
 1622   int i;
 1623 
 1624   XE_SELECT_PAGE(2);
 1625   XE_MII_CLR(XE_MII_CLK);
 1626   
 1627   for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
 1628     if (bits & i) {
 1629       XE_MII_SET(XE_MII_WRD);
 1630     } else {
 1631       XE_MII_CLR(XE_MII_WRD);
 1632     }
 1633     DELAY(1);
 1634     XE_MII_CLR(XE_MII_CLK);
 1635     DELAY(1);
 1636     XE_MII_SET(XE_MII_CLK);
 1637   }
 1638 }
 1639 
 1640 
 1641 /*
 1642  * Read an PHY register through the MII.
 1643  */
 1644 static int
 1645 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
 1646   int i, ack, s;
 1647 
 1648   s = splimp();
 1649 
 1650   /*
 1651    * Set up frame for RX.
 1652    */
 1653   frame->mii_stdelim = XE_MII_STARTDELIM;
 1654   frame->mii_opcode = XE_MII_READOP;
 1655   frame->mii_turnaround = 0;
 1656   frame->mii_data = 0;
 1657         
 1658   XE_SELECT_PAGE(2);
 1659   XE_OUTB(XE_GPR2, 0);
 1660 
 1661   /*
 1662    * Turn on data xmit.
 1663    */
 1664   XE_MII_SET(XE_MII_DIR);
 1665 
 1666   xe_mii_sync(scp);
 1667 
 1668   /*    
 1669    * Send command/address info.
 1670    */
 1671   xe_mii_send(scp, frame->mii_stdelim, 2);
 1672   xe_mii_send(scp, frame->mii_opcode, 2);
 1673   xe_mii_send(scp, frame->mii_phyaddr, 5);
 1674   xe_mii_send(scp, frame->mii_regaddr, 5);
 1675 
 1676   /* Idle bit */
 1677   XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
 1678   DELAY(1);
 1679   XE_MII_SET(XE_MII_CLK);
 1680   DELAY(1);
 1681 
 1682   /* Turn off xmit. */
 1683   XE_MII_CLR(XE_MII_DIR);
 1684 
 1685   /* Check for ack */
 1686   XE_MII_CLR(XE_MII_CLK);
 1687   DELAY(1);
 1688   ack = XE_INB(XE_GPR2) & XE_MII_RDD;
 1689   XE_MII_SET(XE_MII_CLK);
 1690   DELAY(1);
 1691 
 1692   /*
 1693    * Now try reading data bits. If the ack failed, we still
 1694    * need to clock through 16 cycles to keep the PHY(s) in sync.
 1695    */
 1696   if (ack) {
 1697     for(i = 0; i < 16; i++) {
 1698       XE_MII_CLR(XE_MII_CLK);
 1699       DELAY(1);
 1700       XE_MII_SET(XE_MII_CLK);
 1701       DELAY(1);
 1702     }
 1703     goto fail;
 1704   }
 1705 
 1706   for (i = 0x8000; i; i >>= 1) {
 1707     XE_MII_CLR(XE_MII_CLK);
 1708     DELAY(1);
 1709     if (!ack) {
 1710       if (XE_INB(XE_GPR2) & XE_MII_RDD)
 1711         frame->mii_data |= i;
 1712       DELAY(1);
 1713     }
 1714     XE_MII_SET(XE_MII_CLK);
 1715     DELAY(1);
 1716   }
 1717 
 1718 fail:
 1719 
 1720   XE_MII_CLR(XE_MII_CLK);
 1721   DELAY(1);
 1722   XE_MII_SET(XE_MII_CLK);
 1723   DELAY(1);
 1724 
 1725   splx(s);
 1726 
 1727   if (ack)
 1728     return(1);
 1729   return(0);
 1730 }
 1731 
 1732 
 1733 /*
 1734  * Write to a PHY register through the MII.
 1735  */
 1736 static int
 1737 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
 1738   int s;
 1739 
 1740   s = splimp();
 1741 
 1742   /*
 1743    * Set up frame for TX.
 1744    */
 1745   frame->mii_stdelim = XE_MII_STARTDELIM;
 1746   frame->mii_opcode = XE_MII_WRITEOP;
 1747   frame->mii_turnaround = XE_MII_TURNAROUND;
 1748         
 1749   XE_SELECT_PAGE(2);
 1750 
 1751   /*            
 1752    * Turn on data output.
 1753    */
 1754   XE_MII_SET(XE_MII_DIR);
 1755 
 1756   xe_mii_sync(scp);
 1757 
 1758   xe_mii_send(scp, frame->mii_stdelim, 2);
 1759   xe_mii_send(scp, frame->mii_opcode, 2);
 1760   xe_mii_send(scp, frame->mii_phyaddr, 5);
 1761   xe_mii_send(scp, frame->mii_regaddr, 5);
 1762   xe_mii_send(scp, frame->mii_turnaround, 2);
 1763   xe_mii_send(scp, frame->mii_data, 16);
 1764 
 1765   /* Idle bit. */
 1766   XE_MII_SET(XE_MII_CLK);
 1767   DELAY(1);
 1768   XE_MII_CLR(XE_MII_CLK);
 1769   DELAY(1);
 1770 
 1771   /*
 1772    * Turn off xmit.
 1773    */
 1774   XE_MII_CLR(XE_MII_DIR);
 1775 
 1776   splx(s);
 1777 
 1778   return(0);
 1779 }
 1780 
 1781 
 1782 /*
 1783  * Read a register from the PHY.
 1784  */
 1785 static u_int16_t
 1786 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
 1787   struct xe_mii_frame frame;
 1788 
 1789   bzero((char *)&frame, sizeof(frame));
 1790 
 1791   frame.mii_phyaddr = 0;
 1792   frame.mii_regaddr = reg;
 1793   xe_mii_readreg(scp, &frame);
 1794 
 1795   return(frame.mii_data);
 1796 }
 1797 
 1798 
 1799 /*
 1800  * Write to a PHY register.
 1801  */
 1802 static void
 1803 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
 1804   struct xe_mii_frame frame;
 1805 
 1806   bzero((char *)&frame, sizeof(frame));
 1807 
 1808   frame.mii_phyaddr = 0;
 1809   frame.mii_regaddr = reg;
 1810   frame.mii_data = data;
 1811   xe_mii_writereg(scp, &frame);
 1812 
 1813   return;
 1814 }
 1815 
 1816 
 1817 /*
 1818  * A bit of debugging code.
 1819  */
 1820 static void
 1821 xe_mii_dump(struct xe_softc *scp) {
 1822   int i, s;
 1823 
 1824   s = splimp();
 1825 
 1826   device_printf(scp->dev, "MII registers: ");
 1827   for (i = 0; i < 2; i++) {
 1828     printf(" %d:%04x", i, xe_phy_readreg(scp, i));
 1829   }
 1830   for (i = 4; i < 7; i++) {
 1831     printf(" %d:%04x", i, xe_phy_readreg(scp, i));
 1832   }
 1833   printf("\n");
 1834 
 1835   (void)splx(s);
 1836 }
 1837 
 1838 #if 0
 1839 void
 1840 xe_reg_dump(struct xe_softc *scp) {
 1841   int page, i, s;
 1842 
 1843   s = splimp();
 1844 
 1845   device_printf(scp->dev, "Common registers: ");
 1846   for (i = 0; i < 8; i++) {
 1847     printf(" %2.2x", XE_INB(i));
 1848   }
 1849   printf("\n");
 1850 
 1851   for (page = 0; page <= 8; page++) {
 1852     device_printf(scp->dev, "Register page %2.2x: ", page);
 1853     XE_SELECT_PAGE(page);
 1854     for (i = 8; i < 16; i++) {
 1855       printf(" %2.2x", XE_INB(i));
 1856     }
 1857     printf("\n");
 1858   }
 1859 
 1860   for (page = 0x10; page < 0x5f; page++) {
 1861     if ((page >= 0x11 && page <= 0x3f) ||
 1862         (page == 0x41) ||
 1863         (page >= 0x43 && page <= 0x4f) ||
 1864         (page >= 0x59))
 1865       continue;
 1866     device_printf(scp->dev, "Register page %2.2x: ", page);
 1867     XE_SELECT_PAGE(page);
 1868     for (i = 8; i < 16; i++) {
 1869       printf(" %2.2x", XE_INB(i));
 1870     }
 1871     printf("\n");
 1872   }
 1873 
 1874   (void)splx(s);
 1875 }
 1876 #endif
 1877 
 1878 int
 1879 xe_activate(device_t dev)
 1880 {
 1881         struct xe_softc *sc = device_get_softc(dev);
 1882         int start, err, i;
 1883 
 1884         DEVPRINTF(2, (dev, "activate\n"));
 1885 
 1886         if (!sc->modem) {
 1887                 sc->port_rid = 0;       /* 0 is managed by pccard */
 1888                 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
 1889                     &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
 1890         }
 1891         else if (sc->dingo) {
 1892                 /*
 1893                  * Find a 16 byte aligned ioport for the card.
 1894                  */
 1895                 DEVPRINTF(1, (dev, "Finding an aligned port for RealPort\n"));
 1896                 sc->port_rid = 1;       /* 0 is managed by pccard */
 1897                 start = 0x100;
 1898                 do {
 1899                         sc->port_res = bus_alloc_resource(dev,
 1900                             SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
 1901                             RF_ACTIVE);
 1902                         if (sc->port_res == 0)
 1903                                 break;          /* we failed */
 1904                         if ((rman_get_start(sc->port_res) & 0xf) == 0)
 1905                                 break;          /* good */
 1906                         bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
 1907                             sc->port_res);
 1908                         start = (rman_get_start(sc->port_res) + 15) & ~0xf;
 1909                 } while (1);
 1910                 DEVPRINTF(1, (dev, "RealPort port 0x%0lx, size 0x%0lx\n",
 1911                     bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
 1912                     bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
 1913         }
 1914         else if (sc->ce2) {
 1915             /*
 1916              * Find contiguous I/O port for the Ethernet function on CEM2 and
 1917              * CEM3 cards.  We allocate window 0 wherever pccard has decided
 1918              * it should be, then find an available window adjacent to it for
 1919              * the second function.  Not sure that both windows are actually
 1920              * needed.
 1921              */
 1922             DEVPRINTF(1, (dev, "Finding I/O port for CEM2/CEM3\n"));
 1923             sc->ce2_port_rid = 0;       /* 0 is managed by pccard */
 1924             sc->ce2_port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
 1925                                                   &sc->ce2_port_rid, 0, ~0,
 1926                                                   8, RF_ACTIVE);
 1927             if (!sc->ce2_port_res) {
 1928                 DEVPRINTF(1, (dev, "Cannot allocate I/O port for modem\n"));
 1929                 return ENOMEM;
 1930             }
 1931 
 1932             sc->port_rid = 1;
 1933             start = bus_get_resource_start(dev, SYS_RES_IOPORT,
 1934                                            sc->ce2_port_rid);
 1935             for (i = 0; i < 2; i++) {
 1936                 start += (i == 0 ? 8 : -24);
 1937                 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
 1938                                                   &sc->port_rid, start,
 1939                                                   start + 18, 18, RF_ACTIVE);
 1940                 if (sc->port_res == 0)
 1941                     continue;   /* Failed, try again if possible */
 1942                 if (bus_get_resource_start(dev, SYS_RES_IOPORT,
 1943                                            sc->port_rid) == start)
 1944                     break;      /* Success! */
 1945 
 1946                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid,
 1947                                      sc->port_res);
 1948                 sc->port_res = 0;
 1949             }
 1950                 DEVPRINTF(1, (dev, "CEM2/CEM3 port 0x%0lx, size 0x%0lx\n",
 1951                     bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
 1952                     bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid)));
 1953         }
 1954 
 1955         if (!sc->port_res) {
 1956                 DEVPRINTF(1, (dev, "Cannot allocate ioport\n"));
 1957                 return ENOMEM;
 1958         }
 1959 
 1960         sc->irq_rid = 0;
 1961         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 
 1962             0, ~0, 1, RF_ACTIVE);
 1963         if (!sc->irq_res) {
 1964                 DEVPRINTF(1, (dev, "Cannot allocate irq\n"));
 1965                 xe_deactivate(dev);
 1966                 return ENOMEM;
 1967         }
 1968         if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, xe_intr, sc,
 1969             &sc->intrhand)) != 0) {
 1970                 xe_deactivate(dev);
 1971                 return err;
 1972         }
 1973 
 1974         sc->bst = rman_get_bustag(sc->port_res);
 1975         sc->bsh = rman_get_bushandle(sc->port_res);
 1976         return (0);
 1977 }
 1978 
 1979 void
 1980 xe_deactivate(device_t dev)
 1981 {
 1982         struct xe_softc *sc = device_get_softc(dev);
 1983         
 1984         DEVPRINTF(2, (dev, "deactivate\n"));
 1985         xe_disable_intr(sc);
 1986 
 1987         if (sc->intrhand)
 1988                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
 1989         sc->intrhand = 0;
 1990         if (sc->port_res)
 1991                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
 1992                     sc->port_res);
 1993         sc->port_res = 0;
 1994         if (sc->ce2_port_res)
 1995             bus_release_resource(dev, SYS_RES_IOPORT, sc->ce2_port_rid,
 1996                                  sc->ce2_port_res);
 1997         sc->ce2_port_res = 0;
 1998         if (sc->irq_res)
 1999                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 
 2000                     sc->irq_res);
 2001         sc->irq_res = 0;
 2002         return;
 2003 }

Cache object: 525958b32d62fa7f8501c0f8187bbcd4


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