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

Cache object: 78abca9a0a531c11db77b3a81df581b9


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