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

Cache object: 8958c35c697341cc98f4619bd5ae0c15


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