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

Cache object: 5ce864eabd6facaa06a8847589ac6a35


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