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$
   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/select.h>
  117 #include <sys/socket.h>
  118 #include <sys/sockio.h>
  119 #include <sys/systm.h>
  120 #include <sys/uio.h>
  121 
  122 #include <sys/module.h>
  123 #include <sys/bus.h>
  124 
  125 #include <machine/bus.h>
  126 #include <machine/resource.h>
  127 #include <sys/rman.h>
  128  
  129 #include <net/ethernet.h>
  130 #include <net/if.h>
  131 #include <net/if_arp.h>
  132 #include <net/if_dl.h>
  133 #include <net/if_media.h>
  134 #include <net/if_mib.h>
  135 #include <net/bpf.h>
  136 
  137 #include <dev/pccard/pccardvar.h>
  138 #include "card_if.h"
  139 
  140 #include <dev/xe/if_xereg.h>
  141 #include <dev/xe/if_xevar.h>
  142 
  143 #include <machine/clock.h>
  144 
  145 /*
  146  * MII command structure
  147  */
  148 struct xe_mii_frame {
  149   u_int8_t  mii_stdelim;
  150   u_int8_t  mii_opcode;
  151   u_int8_t  mii_phyaddr;
  152   u_int8_t  mii_regaddr;
  153   u_int8_t  mii_turnaround;
  154   u_int16_t mii_data;
  155 };
  156 
  157 /*
  158  * Media autonegotiation progress constants
  159  */
  160 #define XE_AUTONEG_NONE         0       /* No autonegotiation in progress */
  161 #define XE_AUTONEG_WAITING      1       /* Waiting for transmitter to go idle */
  162 #define XE_AUTONEG_STARTED      2       /* Waiting for autonegotiation to complete */
  163 #define XE_AUTONEG_100TX        3       /* Trying to force 100baseTX link */
  164 #define XE_AUTONEG_FAIL         4       /* Autonegotiation failed */
  165 
  166 
  167 /*
  168  * Prototypes start here
  169  */
  170 static int       xe_probe               (device_t dev);
  171 static int       xe_attach              (device_t dev);
  172 static int       xe_detach              (device_t dev);
  173 static int       xe_activate            (device_t dev);
  174 static void      xe_deactivate          (device_t dev);
  175 static void      xe_init                (void *xscp);
  176 static void      xe_start               (struct ifnet *ifp);
  177 static int       xe_ioctl               (struct ifnet *ifp, u_long command, caddr_t data);
  178 static void      xe_watchdog            (struct ifnet *ifp);
  179 static int       xe_media_change        (struct ifnet *ifp);
  180 static void      xe_media_status        (struct ifnet *ifp, struct ifmediareq *mrp);
  181 static timeout_t xe_setmedia;
  182 static void      xe_hard_reset          (struct xe_softc *scp);
  183 static void      xe_soft_reset          (struct xe_softc *scp);
  184 static void      xe_stop                (struct xe_softc *scp);
  185 static void      xe_enable_intr         (struct xe_softc *scp);
  186 static void      xe_disable_intr        (struct xe_softc *scp);
  187 static void      xe_setmulti            (struct xe_softc *scp);
  188 static void      xe_setaddrs            (struct xe_softc *scp);
  189 static int       xe_pio_write_packet    (struct xe_softc *scp, struct mbuf *mbp);
  190 static u_int32_t xe_compute_crc         (u_int8_t *data, int len);
  191 static int       xe_compute_hashbit     (u_int32_t crc);
  192 
  193 /*
  194  * MII functions
  195  */
  196 static void      xe_mii_sync            (struct xe_softc *scp);
  197 static int       xe_mii_init            (struct xe_softc *scp);
  198 static void      xe_mii_send            (struct xe_softc *scp, u_int32_t bits, int cnt);
  199 static int       xe_mii_readreg         (struct xe_softc *scp, struct xe_mii_frame *frame);
  200 static int       xe_mii_writereg        (struct xe_softc *scp, struct xe_mii_frame *frame);
  201 static u_int16_t xe_phy_readreg         (struct xe_softc *scp, u_int16_t reg);
  202 static void      xe_phy_writereg        (struct xe_softc *scp, u_int16_t reg, u_int16_t data);
  203 
  204 /*
  205  * Debug functions -- uncomment for VERY verbose dignostic information.
  206  * Set to 1 for less verbose information
  207  */
  208 /* #define XE_DEBUG 2 */
  209 #ifdef XE_DEBUG
  210 #define XE_REG_DUMP(scp)                xe_reg_dump((scp))
  211 #define XE_MII_DUMP(scp)                xe_mii_dump((scp))
  212 static void      xe_reg_dump            (struct xe_softc *scp);
  213 static void      xe_mii_dump            (struct xe_softc *scp);
  214 #else
  215 #define XE_REG_DUMP(scp)
  216 #define XE_MII_DUMP(scp)
  217 #endif
  218 
  219 /*
  220  * Fixing for RealPort cards - they need a little furtling to get the
  221  * ethernet working
  222  */
  223 static int
  224 xe_cem56fix(device_t dev)
  225 {
  226   struct xe_softc *sc = (struct xe_softc *) device_get_softc(dev);
  227   bus_space_tag_t bst;
  228   bus_space_handle_t bsh;
  229   struct resource *r;
  230   int rid;
  231   int ioport;
  232 
  233 #ifdef XE_DEBUG
  234   device_printf(dev, "Hacking your Realport, master\n");
  235 #endif
  236  
  237 #if XE_DEBUG > 1
  238   device_printf(dev, "Realport port 0x%0lx, size 0x%0lx\n",
  239       bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
  240       bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid));
  241 #endif
  242 
  243   rid = 0;
  244   r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 4 << 10, RF_ACTIVE);
  245   if (!r) {
  246 #if XE_DEBUG > 0
  247     device_printf(dev, "Can't map in attribute memory\n");
  248 #endif
  249     return -1;
  250   }
  251 
  252   bsh = rman_get_bushandle(r);
  253   bst = rman_get_bustag(r);
  254 
  255   CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
  256       PCCARD_A_MEM_ATTR);
  257 
  258   bus_space_write_1(bst, bsh, DINGO_ECOR, DINGO_ECOR_IRQ_LEVEL |
  259                                           DINGO_ECOR_INT_ENABLE |
  260                                           DINGO_ECOR_IOB_ENABLE |
  261                                           DINGO_ECOR_ETH_ENABLE);
  262   ioport = bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid);
  263   bus_space_write_1(bst, bsh, DINGO_EBAR0, ioport & 0xff);
  264   bus_space_write_1(bst, bsh, DINGO_EBAR1, (ioport >> 8) & 0xff);
  265 
  266   bus_space_write_1(bst, bsh, DINGO_DCOR0, DINGO_DCOR0_SF_INT);
  267   bus_space_write_1(bst, bsh, DINGO_DCOR1, DINGO_DCOR1_INT_LEVEL |
  268                                            DINGO_DCOR1_EEDIO);
  269   bus_space_write_1(bst, bsh, DINGO_DCOR2, 0x00);
  270   bus_space_write_1(bst, bsh, DINGO_DCOR3, 0x00);
  271   bus_space_write_1(bst, bsh, DINGO_DCOR4, 0x00);
  272 
  273   bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
  274 
  275   /* success! */
  276   return 0;
  277 }
  278         
  279 /*
  280  * PCMCIA probe routine.
  281  * Probe and identify the device.  Called by the slot manager when the card is 
  282  * inserted or the machine wakes up from suspend mode.  Assmes that the slot
  283  * structure has been initialised already.
  284  */
  285 static int
  286 xe_probe(device_t dev)
  287 {
  288   struct xe_softc *scp = (struct xe_softc *) device_get_softc(dev);
  289   bus_space_tag_t bst;
  290   bus_space_handle_t bsh;
  291   int buf;
  292   u_char ver_str[CISTPL_BUFSIZE>>1];
  293   off_t offs;
  294   int success, rc, i;
  295   int rid;
  296   struct resource *r;
  297 
  298   success = 0;
  299 
  300 #ifdef XE_DEBUG
  301   device_printf(dev, "xe: Probing\n");
  302 #endif
  303 
  304   /* Map in the CIS */
  305   rid = 0;
  306   r = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 0, ~0, 4 << 10, RF_ACTIVE);
  307   if (!r) {
  308 #ifdef XE_DEBUG
  309     device_printf(dev, "Can't map in cis\n");
  310 #endif
  311     return ENOMEM;
  312   }
  313   bsh = rman_get_bushandle(r);
  314   bst = rman_get_bustag(r);
  315   buf = 0;
  316 
  317   CARD_SET_RES_FLAGS(device_get_parent(dev), dev, SYS_RES_MEMORY, rid,
  318       PCCARD_A_MEM_ATTR);
  319 
  320   /* Grep through CIS looking for relevant tuples */
  321   offs = 0;
  322   do {
  323     u_int16_t vendor;
  324     u_int8_t rev, media, prod;
  325 
  326     switch (CISTPL_TYPE(buf)) {
  327 
  328     case 0x15:  /* Grab version string (needed to ID some weird CE2's) */
  329 #if XE_DEBUG > 1
  330       device_printf(dev, "Got version string (0x15)\n");
  331 #endif
  332       for (i = 0; i < CISTPL_LEN(buf); ver_str[i] = CISTPL_DATA(buf, i++));
  333       ver_str[i] = '\0';
  334       ver_str[(CISTPL_BUFSIZE>>1) - 1] = CISTPL_LEN(buf);
  335       success++;
  336       break;
  337 
  338     case 0x20:  /* Figure out what type of card we have */
  339 #if XE_DEBUG > 1
  340       device_printf(dev, "Got card ID (0x20)\n");
  341 #endif
  342       vendor = CISTPL_DATA(buf, 0) + (CISTPL_DATA(buf, 1) << 8);
  343       rev = CISTPL_DATA(buf, 2);
  344       media = CISTPL_DATA(buf, 3);
  345       prod = CISTPL_DATA(buf, 4);
  346 
  347       switch (vendor) { /* Get vendor ID */
  348       case 0x0105:
  349         scp->vendor = "Xircom"; break;
  350       case 0x0138:
  351       case 0x0183:
  352         scp->vendor = "Compaq"; break;
  353       case 0x0089:
  354         scp->vendor = "Intel"; break;
  355       default:
  356         scp->vendor = "Unknown";
  357       }
  358 
  359       if (!((prod & 0x40) && (media & 0x01))) {
  360 #if XE_DEBUG > 1
  361         device_printf(dev, "Not a PCMCIA Ethernet card!\n");
  362 #endif
  363         rc = ENODEV;            /* Not a PCMCIA Ethernet device */
  364       } else {
  365         if (media & 0x10) {     /* Ethernet/modem cards */
  366 #if XE_DEBUG > 1
  367           device_printf(dev, "Card is Ethernet/modem combo\n");
  368 #endif
  369           scp->modem = 1;
  370           switch (prod & 0x0f) {
  371           case 1:
  372             scp->card_type = "CEM"; break;
  373           case 2:
  374             scp->ce2 = 1;
  375             scp->card_type = "CEM2"; break;
  376           case 3:
  377             scp->ce2 = 1;
  378             scp->card_type = "CEM3"; break;
  379           case 4:
  380             scp->ce2 = 1;
  381             scp->card_type = "CEM33"; break;
  382           case 5:
  383             scp->mohawk = 1;
  384             scp->card_type = "CEM56M"; break;
  385           case 6:
  386           case 7:               /* Some kind of RealPort card */
  387             scp->mohawk = 1;
  388             scp->dingo = 1;
  389             scp->card_type = "CEM56"; break;
  390           default:
  391             rc = ENODEV;
  392           }
  393         } else {                /* Ethernet-only cards */
  394 #if XE_DEBUG > 1
  395           device_printf(dev, "Card is Ethernet only\n");
  396 #endif
  397           switch (prod & 0x0f) {
  398           case 1:
  399             scp->card_type = "CE"; break;
  400           case 2:
  401             scp->ce2 = 1;
  402             scp->card_type = "CE2"; break;
  403           case 3:
  404             scp->mohawk = 1;
  405             scp->card_type = "CE3"; break;
  406           default:
  407             rc = ENODEV;
  408           }
  409         }
  410       }
  411       success++;
  412       break;
  413 
  414     case 0x22:  /* Get MAC address */
  415       if ((CISTPL_LEN(buf) == 8) &&
  416           (CISTPL_DATA(buf, 0) == 0x04) &&
  417           (CISTPL_DATA(buf, 1) == ETHER_ADDR_LEN)) {
  418 #if XE_DEBUG > 1
  419         device_printf(dev, "Got MAC address (0x22)\n");
  420 #endif
  421         for (i = 0; i < ETHER_ADDR_LEN; i++)
  422           scp->arpcom.ac_enaddr[i] = CISTPL_DATA(buf, i+2);
  423       }
  424       success++;
  425       break;
  426     default:
  427       break;
  428     }
  429 
  430     if (CISTPL_TYPE(buf) == 0xff)
  431       break;
  432     /* Skip to next tuple */
  433     buf += ((CISTPL_LEN(buf) + 2) << 1);
  434 
  435   } while (1);
  436 
  437   /* unmap the cis */
  438   bus_release_resource(dev, SYS_RES_MEMORY, rid, r);
  439 
  440   /* Die now if something went wrong above */
  441   if (success < 3)
  442     return ENXIO;
  443 
  444   /* Check for certain strange CE2's that look like CE's */
  445   if (strcmp(scp->card_type, "CE") == 0) {
  446     u_char *str = ver_str;
  447 #if XE_DEBUG > 1
  448     device_printf(dev, "Checking for weird CE2 string\n");
  449 #endif
  450     str += strlen(str) + 1;                     /* Skip forward to 3rd version string */
  451     str += strlen(str) + 1;
  452     str += strlen(str) + 1;
  453     for (i = 0; i < strlen(str) - 2; i++) {
  454       if (bcmp(&str[i], "CE2", 3) ==0) {        /* Look for "CE2" string */
  455         scp->card_type = "CE2";
  456       }
  457     }
  458   }
  459 
  460   /* Reject unsupported cards */
  461   if (strcmp(scp->card_type, "CE") == 0 || strcmp(scp->card_type, "CEM") == 0) {
  462     device_printf(dev, "Sorry, your %s card is not supported :(\n",
  463      scp->card_type);
  464     return ENODEV;
  465   }
  466 
  467   /* Success */
  468   return 0;
  469 }
  470 
  471 /*
  472  * The device entry is being removed, probably because someone ejected the
  473  * card.  The interface should have been brought down manually before calling
  474  * this function; if not you may well lose packets.  In any case, I shut down
  475  * the card and the interface, and hope for the best.
  476  */
  477 static int
  478 xe_detach(device_t dev) {
  479   struct xe_softc *sc = device_get_softc(dev);
  480 
  481   sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING; 
  482   ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
  483   xe_deactivate(dev);
  484   return 0;
  485 }
  486 
  487 /*
  488  * Attach a device.
  489  */
  490 static int
  491 xe_attach (device_t dev) {
  492   struct xe_softc *scp = device_get_softc(dev);
  493   int err;
  494 
  495 #ifdef XE_DEBUG
  496   device_printf(dev, "attach\n");
  497 #endif
  498 
  499   if ((err = xe_activate(dev)) != 0)
  500     return (err);
  501 
  502   /* Fill in some private data */
  503   scp->ifp = &scp->arpcom.ac_if;
  504   scp->ifm = &scp->ifmedia;
  505   scp->autoneg_status = 0;
  506 
  507   /* Hack RealPorts into submission */
  508   if (scp->dingo && xe_cem56fix(dev) < 0) {
  509     device_printf(dev, "Unable to fix your RealPort\n");
  510     xe_deactivate(dev);
  511     return ENODEV;
  512   }
  513 
  514   /* Hopefully safe to read this here */
  515   XE_SELECT_PAGE(4);
  516   scp->version = XE_INB(XE_BOV);
  517 
  518   scp->dev = dev;
  519   /* Initialise the ifnet structure */
  520   if (!scp->ifp->if_name) {
  521     scp->ifp->if_softc = scp;
  522     scp->ifp->if_name = "xe";
  523     scp->ifp->if_unit = device_get_unit(dev);
  524     scp->ifp->if_timer = 0;
  525     scp->ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
  526     scp->ifp->if_linkmib = &scp->mibdata;
  527     scp->ifp->if_linkmiblen = sizeof scp->mibdata;
  528     scp->ifp->if_output = ether_output;
  529     scp->ifp->if_start = xe_start;
  530     scp->ifp->if_ioctl = xe_ioctl;
  531     scp->ifp->if_watchdog = xe_watchdog;
  532     scp->ifp->if_init = xe_init;
  533     scp->ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
  534   }
  535 
  536   /* Initialise the ifmedia structure */
  537   ifmedia_init(scp->ifm, 0, xe_media_change, xe_media_status);
  538   callout_handle_init(&scp->chand);
  539 
  540   /*
  541    * Fill in supported media types.  Some cards _do_ support full duplex
  542    * operation, but this driver doesn't, yet.  Therefore we leave those modes
  543    * out of the list.  We support some form of autoselection in all cases.
  544    */
  545   if (scp->mohawk) {
  546     ifmedia_add(scp->ifm, IFM_ETHER|IFM_100_TX, 0, NULL);
  547     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
  548   }
  549   else {
  550     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_T, 0, NULL);
  551     ifmedia_add(scp->ifm, IFM_ETHER|IFM_10_2, 0, NULL);
  552   }
  553   ifmedia_add(scp->ifm, IFM_ETHER|IFM_AUTO, 0, NULL);
  554 
  555   /* Default is to autoselect best supported media type */
  556   ifmedia_set(scp->ifm, IFM_ETHER|IFM_AUTO);
  557 
  558   /* Print some useful information */
  559   device_printf(dev, "%s %s, bonding version %#x%s%s\n",
  560          scp->vendor,
  561          scp->card_type,
  562          scp->version,
  563          scp->mohawk ? ", 100Mbps capable" : "",
  564          scp->modem ?  ", with modem"      : "");
  565   if (scp->mohawk) {
  566     XE_SELECT_PAGE(0x10);
  567     device_printf(dev, "DingoID = %#x, RevisionID = %#x, VendorID = %#x\n",
  568            XE_INW(XE_DINGOID),
  569            XE_INW(XE_RevID),
  570            XE_INW(XE_VendorID));
  571   }
  572   if (scp->ce2) {
  573     XE_SELECT_PAGE(0x45);
  574     device_printf(dev, "CE2 version = %#x\n", XE_INB(XE_REV));
  575   }
  576 
  577   /* Print MAC address */
  578   device_printf(dev, "Ethernet address %6D\n", scp->arpcom.ac_enaddr, ":");
  579 
  580   /* Attach the interface */
  581   ether_ifattach(scp->ifp, ETHER_BPF_SUPPORTED);
  582 
  583   /* Done */
  584   return 0;
  585 }
  586 
  587 
  588 /*
  589  * Initialize device.  Completes the reset procedure on the card and starts
  590  * output.  If there's an autonegotiation in progress we DON'T do anything;
  591  * the media selection code will call us again when it's done.
  592  */
  593 static void
  594 xe_init(void *xscp) {
  595   struct xe_softc *scp = xscp;
  596   int s;
  597 
  598 #ifdef XE_DEBUG
  599   device_printf(scp->dev, "init\n");
  600 #endif
  601 
  602   if (TAILQ_EMPTY(&scp->ifp->if_addrhead)) return;
  603 
  604   /* Reset transmitter flags */
  605   scp->tx_queued = 0;
  606   scp->tx_tpr = 0;
  607   scp->tx_collisions = 0;
  608   scp->ifp->if_timer = 0;
  609 
  610   s = splimp();
  611 
  612   XE_SELECT_PAGE(0x42);
  613   XE_OUTB(XE_SWC0, 0x20);       /* Disable source insertion (WTF is that?) */
  614 
  615   /*
  616    * Set the 'local memory dividing line' -- splits the 32K card memory into
  617    * 8K for transmit buffers and 24K for receive.  This is done automatically
  618    * on newer revision cards.
  619    */
  620   if (scp->srev != 1) {
  621     XE_SELECT_PAGE(2);
  622     XE_OUTW(XE_RBS, 0x2000);
  623   }
  624 
  625   /* Set up multicast addresses */
  626   xe_setmulti(scp);
  627 
  628   /* Fix the data offset register -- reset leaves it off-by-one */
  629   XE_SELECT_PAGE(0);
  630   XE_OUTW(XE_DO, 0x2000);
  631 
  632   /*
  633    * Set MAC interrupt masks and clear status regs.  The bit names are direct
  634    * from the Linux code; I have no idea what most of them do.
  635    */
  636   XE_SELECT_PAGE(0x40);         /* Bit 7..0 */
  637   XE_OUTB(XE_RX0Msk, 0xff);     /* ROK, RAB, rsv, RO,  CRC, AE,  PTL, MP  */
  638   XE_OUTB(XE_TX0Msk, 0xff);     /* TOK, TAB, SQE, LL,  TU,  JAB, EXC, CRS */
  639   XE_OUTB(XE_TX0Msk+1, 0xb0);   /* rsv, rsv, PTD, EXT, rsv, rsv, rsv, rsv */
  640   XE_OUTB(XE_RST0, 0x00);       /* ROK, RAB, REN, RO,  CRC, AE,  PTL, MP  */
  641   XE_OUTB(XE_TXST0, 0x00);      /* TOK, TAB, SQE, LL,  TU,  JAB, EXC, CRS */
  642   XE_OUTB(XE_TXST1, 0x00);      /* TEN, rsv, PTD, EXT, retry_counter:4    */
  643 
  644   /*
  645    * Check for an in-progress autonegotiation.  If one is active, just set
  646    * IFF_RUNNING and return.  The media selection code will call us again when 
  647    * it's done.
  648    */
  649   if (scp->autoneg_status) {
  650     scp->ifp->if_flags |= IFF_RUNNING;
  651   }
  652   else {
  653     /* Enable receiver, put MAC online */
  654     XE_SELECT_PAGE(0x40);
  655     XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
  656 
  657     /* Set up IMR, enable interrupts */
  658     xe_enable_intr(scp);
  659 
  660     /* Attempt to start output */
  661     scp->ifp->if_flags |= IFF_RUNNING;
  662     scp->ifp->if_flags &= ~IFF_OACTIVE;
  663     xe_start(scp->ifp);
  664   }
  665 
  666   (void)splx(s);
  667 }
  668 
  669 
  670 /*
  671  * Start output on interface.  We make two assumptions here:
  672  *  1) that the current priority is set to splimp _before_ this code
  673  *     is called *and* is returned to the appropriate priority after
  674  *     return
  675  *  2) that the IFF_OACTIVE flag is checked before this code is called
  676  *     (i.e. that the output part of the interface is idle)
  677  */
  678 static void
  679 xe_start(struct ifnet *ifp) {
  680   struct xe_softc *scp = ifp->if_softc;
  681   struct mbuf *mbp;
  682 
  683   /*
  684    * Loop while there are packets to be sent, and space to send them.
  685    */
  686   while (1) {
  687     IF_DEQUEUE(&ifp->if_snd, mbp);      /* Suck a packet off the send queue */
  688 
  689     if (mbp == NULL) {
  690       /*
  691        * We are using the !OACTIVE flag to indicate to the outside world that
  692        * we can accept an additional packet rather than that the transmitter
  693        * is _actually_ active. Indeed, the transmitter may be active, but if
  694        * we haven't filled all the buffers with data then we still want to
  695        * accept more.
  696        */
  697       ifp->if_flags &= ~IFF_OACTIVE;
  698       return;
  699     }
  700 
  701     if (xe_pio_write_packet(scp, mbp) != 0) {
  702       IF_PREPEND(&ifp->if_snd, mbp);    /* Push the packet back onto the queue */
  703       ifp->if_flags |= IFF_OACTIVE;
  704       return;
  705     }
  706 
  707     /* Tap off here if there is a bpf listener */
  708     if (ifp->if_bpf) {
  709 #if XE_DEBUG > 1
  710       device_printf(scp->dev, "sending output packet to BPF\n");
  711 #endif
  712       bpf_mtap(ifp, mbp);
  713     }
  714 
  715     ifp->if_timer = 5;                  /* In case we don't hear from the card again */
  716     scp->tx_queued++;
  717 
  718     m_freem(mbp);
  719   }
  720 }
  721 
  722 
  723 /*
  724  * Process an ioctl request.  Adapted from the ed driver.
  725  */
  726 static int
  727 xe_ioctl (register struct ifnet *ifp, u_long command, caddr_t data) {
  728   struct xe_softc *scp;
  729   int s, error;
  730 
  731   scp = ifp->if_softc;
  732   error = 0;
  733 
  734   s = splimp();
  735 
  736   switch (command) {
  737 
  738    case SIOCSIFADDR:
  739    case SIOCGIFADDR:
  740    case SIOCSIFMTU:
  741     error = ether_ioctl(ifp, command, data);
  742     break;
  743 
  744    case SIOCSIFFLAGS:
  745     /*
  746      * If the interface is marked up and stopped, then start it.  If it is
  747      * marked down and running, then stop it.
  748      */
  749     if (ifp->if_flags & IFF_UP) {
  750       if (!(ifp->if_flags & IFF_RUNNING)) {
  751         xe_hard_reset(scp);
  752         xe_setmedia(scp);
  753         xe_init(scp);
  754       }
  755     }
  756     else {
  757       if (ifp->if_flags & IFF_RUNNING)
  758         xe_stop(scp);
  759     }
  760 
  761    case SIOCADDMULTI:
  762    case SIOCDELMULTI:
  763     /*
  764      * Multicast list has (maybe) changed; set the hardware filter
  765      * accordingly.  This also serves to deal with promiscuous mode if we have 
  766      * a BPF listener active.
  767      */
  768     xe_setmulti(scp);
  769     error = 0;
  770     break;
  771 
  772    case SIOCSIFMEDIA:
  773    case SIOCGIFMEDIA:
  774     /*
  775      * Someone wants to get/set media options.
  776      */
  777     error = ifmedia_ioctl(ifp, (struct ifreq *)data, &scp->ifmedia, command);
  778     break;
  779 
  780    default:
  781     error = EINVAL;
  782   }
  783 
  784   (void)splx(s);
  785 
  786   return error;
  787 }
  788 
  789 
  790 /*
  791  * Card interrupt handler.
  792  *
  793  * This function is probably more complicated than it needs to be, as it
  794  * attempts to deal with the case where multiple packets get sent between
  795  * interrupts.  This is especially annoying when working out the collision
  796  * stats.  Not sure whether this case ever really happens or not (maybe on a
  797  * slow/heavily loaded machine?) so it's probably best to leave this like it
  798  * is.
  799  *
  800  * Note that the crappy PIO used to get packets on and off the card means that 
  801  * you will spend a lot of time in this routine -- I can get my P150 to spend
  802  * 90% of its time servicing interrupts if I really hammer the network.  Could 
  803  * fix this, but then you'd start dropping/losing packets.  The moral of this
  804  * story?  If you want good network performance _and_ some cycles left over to 
  805  * get your work done, don't buy a Xircom card.  Or convince them to tell me
  806  * how to do memory-mapped I/O :)
  807  */
  808 static void
  809 xe_intr(void *xscp) 
  810 {
  811   struct xe_softc *scp = (struct xe_softc *) xscp;
  812   struct ifnet *ifp;
  813   int result;
  814   u_int16_t rx_bytes, rxs, txs;
  815   u_int8_t psr, isr, esr, rsr;
  816 
  817   ifp = &scp->arpcom.ac_if;
  818   rx_bytes = 0;                 /* Bytes received on this interrupt */
  819   result = 0;                   /* Set true if the interrupt is for us */
  820 
  821   if (scp->mohawk) {
  822     XE_OUTB(XE_CR, 0);          /* Disable interrupts */
  823   }
  824 
  825   psr = XE_INB(XE_PR);          /* Stash the current register page */
  826 
  827   /*
  828    * Read ISR to see what caused this interrupt.  Note that this clears the
  829    * ISR on CE2 type cards.
  830    */
  831   if ((isr = XE_INB(XE_ISR)) && isr != 0xff) {
  832 
  833     result = 1;                 /* This device did generate an int */
  834     esr = XE_INB(XE_ESR);       /* Read the other status registers */
  835     XE_SELECT_PAGE(0x40);
  836     rxs = XE_INB(XE_RST0);
  837     XE_OUTB(XE_RST0, ~rxs & 0xff);
  838     txs = XE_INB(XE_TXST0);
  839     txs |= XE_INB(XE_TXST1) << 8;
  840     XE_OUTB(XE_TXST0, 0);
  841     XE_OUTB(XE_TXST1, 0);
  842     XE_SELECT_PAGE(0);
  843 
  844 #if XE_DEBUG > 2
  845     printf("xe%d: ISR=%#2.2x ESR=%#2.2x RST=%#2.2x TXST=%#4.4x\n", unit, isr, esr, rxs, txs);
  846 #endif
  847 
  848     /*
  849      * Handle transmit interrupts
  850      */
  851     if (isr & XE_ISR_TX_PACKET) {
  852       u_int8_t new_tpr, sent;
  853       
  854       if ((new_tpr = XE_INB(XE_TPR)) < scp->tx_tpr)     /* Update packet count */
  855         sent = (0xff - scp->tx_tpr) + new_tpr;          /* TPR rolled over */
  856       else
  857         sent = new_tpr - scp->tx_tpr;
  858 
  859       if (sent > 0) {                           /* Packets sent since last interrupt */
  860         scp->tx_tpr = new_tpr;
  861         scp->tx_queued -= sent;
  862         ifp->if_opackets += sent;
  863         ifp->if_collisions += scp->tx_collisions;
  864 
  865         /*
  866          * Collision stats are a PITA.  If multiples frames have been sent, we 
  867          * distribute any outstanding collision count equally amongst them.
  868          * However, if we're missing interrupts we're quite likely to also
  869          * miss some collisions; thus the total count will be off anyway.
  870          * Likewise, if we miss a frame dropped due to excessive collisions
  871          * any outstanding collisions count will be held against the next
  872          * frame to be successfully sent.  Hopefully it averages out in the
  873          * end!
  874          * XXX - This will screw up if tx_collisions/sent > 14. FIX IT!
  875          */
  876         switch (scp->tx_collisions) {
  877          case 0:
  878           break;
  879          case 1:
  880           scp->mibdata.dot3StatsSingleCollisionFrames++;
  881           scp->mibdata.dot3StatsCollFrequencies[0]++;
  882           break;
  883          default:
  884           if (sent == 1) {
  885             scp->mibdata.dot3StatsMultipleCollisionFrames++;
  886             scp->mibdata.dot3StatsCollFrequencies[scp->tx_collisions-1]++;
  887           }
  888           else {                /* Distribute across multiple frames */
  889             scp->mibdata.dot3StatsMultipleCollisionFrames += sent;
  890             scp->mibdata.
  891               dot3StatsCollFrequencies[scp->tx_collisions/sent] += sent - scp->tx_collisions%sent;
  892             scp->mibdata.
  893               dot3StatsCollFrequencies[scp->tx_collisions/sent + 1] += scp->tx_collisions%sent;
  894           }
  895         }
  896         scp->tx_collisions = 0;
  897       }
  898       ifp->if_timer = 0;
  899       ifp->if_flags &= ~IFF_OACTIVE;
  900     }
  901     if (txs & 0x0002) {         /* Excessive collisions (packet dropped) */
  902       ifp->if_collisions += 16;
  903       ifp->if_oerrors++;
  904       scp->tx_collisions = 0;
  905       scp->mibdata.dot3StatsExcessiveCollisions++;
  906       scp->mibdata.dot3StatsMultipleCollisionFrames++;
  907       scp->mibdata.dot3StatsCollFrequencies[15]++;
  908       XE_OUTB(XE_CR, XE_CR_RESTART_TX);
  909     }
  910     if (txs & 0x0040)           /* Transmit aborted -- probably collisions */
  911       scp->tx_collisions++;
  912 
  913 
  914     /*
  915      * Handle receive interrupts 
  916      */
  917     while ((esr = XE_INB(XE_ESR)) & XE_ESR_FULL_PACKET_RX) {
  918 
  919       if ((rsr = XE_INB(XE_RSR)) & XE_RSR_RX_OK) {
  920         struct ether_header *ehp;
  921         struct mbuf *mbp;
  922         u_int16_t len;
  923 
  924         len = XE_INW(XE_RBC);
  925 
  926         if (len == 0)
  927           continue;
  928 
  929 #if 0
  930         /*
  931          * Limit the amount of time we spend in this loop, dropping packets if 
  932          * necessary.  The Linux code does this with considerably more
  933          * finesse, adjusting the threshold dynamically.
  934          */
  935         if ((rx_bytes += len) > 22000) {
  936           ifp->if_iqdrops++;
  937           scp->mibData.dot3StatsMissedFrames++;
  938           XE_OUTW(XE_DO, 0x8000);
  939           continue;
  940         }
  941 #endif
  942 
  943         if (len & 0x01)
  944           len++;
  945 
  946         MGETHDR(mbp, M_DONTWAIT, MT_DATA);      /* Allocate a header mbuf */
  947         if (mbp != NULL) {
  948           mbp->m_pkthdr.rcvif = ifp;
  949           mbp->m_pkthdr.len = mbp->m_len = len;
  950 
  951           /*
  952            * If the mbuf header isn't big enough for the packet, attach an
  953            * mbuf cluster to hold it.  The +2 is to allow for the nasty little 
  954            * alignment hack below.
  955            */
  956           if (len + 2 > MHLEN) {
  957             MCLGET(mbp, M_DONTWAIT);
  958             if ((mbp->m_flags & M_EXT) == 0) {
  959               m_freem(mbp);
  960               mbp = NULL;
  961             }
  962           }
  963         }
  964 
  965         if (mbp != NULL) {
  966           /*
  967            * The Ethernet header is 14 bytes long; thus the actual packet data 
  968            * won't be 32-bit aligned when it's dumped into the mbuf.  We
  969            * offset everything by 2 bytes to fix this.  Apparently the
  970            * alignment is important for NFS, damn its eyes.
  971            */
  972           mbp->m_data += 2;
  973           ehp = mtod(mbp, struct ether_header *);
  974 
  975           /*
  976            * Now get the packet, including the Ethernet header and trailer (?)
  977            * We use programmed I/O, because we don't know how to do shared
  978            * memory with these cards.  So yes, it's real slow, and heavy on
  979            * the interrupts (CPU on my P150 maxed out at ~950KBps incoming).
  980            */
  981           if (scp->srev == 0) {         /* Workaround a bug in old cards */
  982             u_short rhs;
  983 
  984             XE_SELECT_PAGE(5);
  985             rhs = XE_INW(XE_RHSA);
  986             XE_SELECT_PAGE(0);
  987 
  988             rhs += 3;                    /* Skip control info */
  989 
  990             if (rhs >= 0x8000)
  991               rhs = 0;
  992 
  993             if (rhs + len > 0x8000) {
  994               int i;
  995 
  996               /*
  997                * XXX - This i-- seems very wrong, but it's what the Linux guys 
  998                * XXX - do.  Need someone with an old CE2 to test this for me.
  999                * XXX - 99/3/28: Changed the first i-- to an i++, maybe that'll
 1000                * XXX - fix it?  It seems as though the previous version would
 1001                * XXX - have caused an infinite loop (what, another one?).
 1002                */
 1003               for (i = 0; i < len; i++, rhs++) {
 1004                 ((char *)ehp)[i] = XE_INB(XE_EDP);
 1005                 if (rhs == 0x8000) {
 1006                   rhs = 0;
 1007                   i--;
 1008                 }
 1009               }
 1010             }
 1011             else
 1012               bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
 1013                (u_int16_t *) ehp, len >> 1);
 1014           }
 1015           else
 1016             bus_space_read_multi_2(scp->bst, scp->bsh, XE_EDP, 
 1017              (u_int16_t *) ehp, len >> 1);
 1018 
 1019           /* Deliver packet to upper layers */
 1020           if (mbp != NULL) {
 1021             mbp->m_pkthdr.len = mbp->m_len = len - ETHER_HDR_LEN;
 1022             mbp->m_data += ETHER_HDR_LEN;       /* Strip off Ethernet header */
 1023             ether_input(ifp, ehp, mbp);         /* Send the packet on its way */
 1024             ifp->if_ipackets++;                 /* Success! */
 1025           }
 1026           XE_OUTW(XE_DO, 0x8000);               /* skip_rx_packet command */
 1027         }
 1028       }
 1029       else if (rsr & XE_RSR_LONG_PACKET) {      /* Packet length >1518 bytes */
 1030         scp->mibdata.dot3StatsFrameTooLongs++;
 1031         ifp->if_ierrors++;
 1032       }
 1033       else if (rsr & XE_RSR_CRC_ERROR) {        /* Bad checksum on packet */
 1034         scp->mibdata.dot3StatsFCSErrors++;
 1035         ifp->if_ierrors++;
 1036       }
 1037       else if (rsr & XE_RSR_ALIGN_ERROR) {      /* Packet alignment error */
 1038         scp->mibdata.dot3StatsAlignmentErrors++;
 1039         ifp->if_ierrors++;
 1040       }
 1041     }
 1042     if (rxs & 0x10) {                           /* Receiver overrun */
 1043       scp->mibdata.dot3StatsInternalMacReceiveErrors++;
 1044       ifp->if_ierrors++;
 1045       XE_OUTB(XE_CR, XE_CR_CLEAR_OVERRUN);
 1046     }
 1047   }
 1048 
 1049   XE_SELECT_PAGE(psr);                          /* Restore saved page */
 1050   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);            /* Re-enable interrupts */
 1051 
 1052   /* Could force an int here, instead of dropping packets? */
 1053   /* XE_OUTB(XE_CR, XE_CR_ENABLE_INTR|XE_CE_FORCE_INTR); */
 1054 
 1055   return;
 1056 }
 1057 
 1058 
 1059 /*
 1060  * Device timeout/watchdog routine.  Called automatically if we queue a packet 
 1061  * for transmission but don't get an interrupt within a specified timeout
 1062  * (usually 5 seconds).  When this happens we assume the worst and reset the
 1063  * card.
 1064  */
 1065 static void
 1066 xe_watchdog(struct ifnet *ifp) {
 1067   struct xe_softc *scp = ifp->if_softc;
 1068 
 1069   device_printf(scp->dev, "watchdog timeout; resetting card\n");
 1070   scp->tx_timeouts++;
 1071   ifp->if_oerrors += scp->tx_queued;
 1072   xe_stop(scp);
 1073   xe_hard_reset(scp);
 1074   xe_setmedia(scp);
 1075   xe_init(scp);
 1076 }
 1077 
 1078 
 1079 /*
 1080  * Change media selection.
 1081  */
 1082 static int
 1083 xe_media_change(struct ifnet *ifp) {
 1084   struct xe_softc *scp = ifp->if_softc;
 1085 
 1086 #ifdef XE_DEBUG
 1087   printf("xe%d: media_change\n", ifp->if_unit);
 1088 #endif
 1089 
 1090   if (IFM_TYPE(scp->ifm->ifm_media) != IFM_ETHER)
 1091     return(EINVAL);
 1092 
 1093   /*
 1094    * Some card/media combos aren't always possible -- filter those out here.
 1095    */
 1096   if ((IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_AUTO ||
 1097        IFM_SUBTYPE(scp->ifm->ifm_media) == IFM_100_TX) && !scp->phy_ok)
 1098     return (EINVAL);
 1099 
 1100   xe_setmedia(scp);
 1101 
 1102   return 0;
 1103 }
 1104 
 1105 
 1106 /*
 1107  * Return current media selection.
 1108  */
 1109 static void
 1110 xe_media_status(struct ifnet *ifp, struct ifmediareq *mrp) {
 1111 
 1112 #ifdef XE_DEBUG
 1113   printf("xe%d: media_status\n", ifp->if_unit);
 1114 #endif
 1115 
 1116   mrp->ifm_active = ((struct xe_softc *)ifp->if_softc)->media;
 1117 
 1118   return;
 1119 }
 1120 
 1121 
 1122 /*
 1123  * Select active media.
 1124  */
 1125 static void xe_setmedia(void *xscp) {
 1126   struct xe_softc *scp = xscp;
 1127   u_int16_t bmcr, bmsr, anar, lpar;
 1128 
 1129 #ifdef XE_DEBUG
 1130   device_printf(scp->dev, "setmedia\n");
 1131 #endif
 1132 
 1133   /* Cancel any pending timeout */
 1134   untimeout(xe_setmedia, scp, scp->chand);
 1135   xe_disable_intr(scp);
 1136 
 1137   /* Select media */
 1138   scp->media = IFM_ETHER;
 1139   switch (IFM_SUBTYPE(scp->ifm->ifm_media)) {
 1140 
 1141    case IFM_AUTO:       /* Autoselect media */
 1142     scp->media = IFM_ETHER|IFM_AUTO;
 1143 
 1144     /*
 1145      * Autoselection is really awful.  It goes something like this:
 1146      *
 1147      * Wait until the transmitter goes idle (2sec timeout).
 1148      * Reset card
 1149      *   IF a 100Mbit PHY exists
 1150      *     Start NWAY autonegotiation (3.5sec timeout)
 1151      *     IF that succeeds
 1152      *       Select 100baseTX or 10baseT, whichever was detected
 1153      *     ELSE
 1154      *       Reset card
 1155      *       IF a 100Mbit PHY exists
 1156      *         Try to force a 100baseTX link (3sec timeout)
 1157      *         IF that succeeds
 1158      *           Select 100baseTX
 1159      *         ELSE
 1160      *           Disable the PHY
 1161      *         ENDIF
 1162      *       ENDIF
 1163      *     ENDIF
 1164      *   ENDIF
 1165      * IF nothing selected so far
 1166      *   IF a 100Mbit PHY exists
 1167      *     Select 10baseT
 1168      *   ELSE
 1169      *     Select 10baseT or 10base2, whichever is connected
 1170      *   ENDIF
 1171      * ENDIF
 1172      */
 1173     switch (scp->autoneg_status) {
 1174 
 1175      case XE_AUTONEG_NONE:
 1176 #if XE_DEBUG > 1
 1177       device_printf(scp->dev, "Waiting for idle transmitter\n");
 1178 #endif
 1179       scp->arpcom.ac_if.if_flags |= IFF_OACTIVE;
 1180       scp->autoneg_status = XE_AUTONEG_WAITING;
 1181       scp->chand = timeout(xe_setmedia, scp, hz * 2);
 1182       return;
 1183 
 1184      case XE_AUTONEG_WAITING:
 1185       xe_soft_reset(scp);
 1186       if (scp->phy_ok) {
 1187 #if XE_DEBUG > 1
 1188         device_printf(scp->dev, "Starting autonegotiation\n");
 1189 #endif
 1190         bmcr = xe_phy_readreg(scp, PHY_BMCR);
 1191         bmcr &= ~(PHY_BMCR_AUTONEGENBL);
 1192         xe_phy_writereg(scp, PHY_BMCR, bmcr);
 1193         anar = xe_phy_readreg(scp, PHY_ANAR);
 1194         anar &= ~(PHY_ANAR_100BT4|PHY_ANAR_100BTXFULL|PHY_ANAR_10BTFULL);
 1195         anar |= PHY_ANAR_100BTXHALF|PHY_ANAR_10BTHALF;
 1196         xe_phy_writereg(scp, PHY_ANAR, anar);
 1197         bmcr |= PHY_BMCR_AUTONEGENBL|PHY_BMCR_AUTONEGRSTR;
 1198         xe_phy_writereg(scp, PHY_BMCR, bmcr);
 1199         scp->autoneg_status = XE_AUTONEG_STARTED;
 1200         scp->chand = timeout(xe_setmedia, scp, hz * 7/2);
 1201         return;
 1202       }
 1203       else {
 1204         scp->autoneg_status = XE_AUTONEG_FAIL;
 1205       }
 1206       break;
 1207 
 1208      case XE_AUTONEG_STARTED:
 1209       bmsr = xe_phy_readreg(scp, PHY_BMSR);
 1210       lpar = xe_phy_readreg(scp, PHY_LPAR);
 1211       if (bmsr & (PHY_BMSR_AUTONEGCOMP|PHY_BMSR_LINKSTAT)) {
 1212 #if XE_DEBUG > 1
 1213         device_printf(scp->dev, "Autonegotiation complete!\n");
 1214 #endif
 1215         /*
 1216          * XXX - Shouldn't have to do this, but (on my hub at least) the
 1217          * XXX - transmitter won't work after a successful autoneg.  So we see 
 1218          * XXX - what the negotiation result was and force that mode.  I'm
 1219          * XXX - sure there is an easy fix for this.
 1220          */
 1221         if (lpar & PHY_LPAR_100BTXHALF) {
 1222           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
 1223           XE_MII_DUMP(scp);
 1224           XE_SELECT_PAGE(2);
 1225           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1226           scp->media = IFM_ETHER|IFM_100_TX;
 1227           scp->autoneg_status = XE_AUTONEG_NONE;
 1228         }
 1229         else {
 1230           /*
 1231            * XXX - Bit of a hack going on in here.
 1232            * XXX - This is derived from Ken Hughes patch to the Linux driver
 1233            * XXX - to make it work with 10Mbit _autonegotiated_ links on CE3B
 1234            * XXX - cards.  What's a CE3B and how's it differ from a plain CE3?
 1235            * XXX - these are the things we need to find out.
 1236            */
 1237           xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1238           XE_SELECT_PAGE(2);
 1239           /* BEGIN HACK */
 1240           XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1241           XE_SELECT_PAGE(0x42);
 1242           XE_OUTB(XE_SWC1, 0x80);
 1243           scp->media = IFM_ETHER|IFM_10_T;
 1244           scp->autoneg_status = XE_AUTONEG_NONE;
 1245           /* END HACK */
 1246           /*XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);*/  /* Disable PHY? */
 1247           /*scp->autoneg_status = XE_AUTONEG_FAIL;*/
 1248         }
 1249       }
 1250       else {
 1251 #if XE_DEBUG > 1
 1252         device_printf(scp->dev, "Autonegotiation failed; trying 100baseTX\n");
 1253 #endif
 1254         XE_MII_DUMP(scp);
 1255         xe_soft_reset(scp);
 1256         if (scp->phy_ok) {
 1257           xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
 1258           scp->autoneg_status = XE_AUTONEG_100TX;
 1259           scp->chand = timeout(xe_setmedia, scp, hz * 3);
 1260           return;
 1261         }
 1262         else {
 1263           scp->autoneg_status = XE_AUTONEG_FAIL;
 1264         }
 1265       }
 1266       break;
 1267 
 1268      case XE_AUTONEG_100TX:
 1269       (void)xe_phy_readreg(scp, PHY_BMSR);
 1270       bmsr = xe_phy_readreg(scp, PHY_BMSR);
 1271       if (bmsr & PHY_BMSR_LINKSTAT) {
 1272 #if XE_DEBUG > 1
 1273         device_printf(scp->dev, "Got 100baseTX link!\n");
 1274 #endif
 1275         XE_MII_DUMP(scp);
 1276         XE_SELECT_PAGE(2);
 1277         XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1278         scp->media = IFM_ETHER|IFM_100_TX;
 1279         scp->autoneg_status = XE_AUTONEG_NONE;
 1280       }
 1281       else {
 1282 #if XE_DEBUG > 1
 1283         device_printf(scp->dev, "Autonegotiation failed; disabling PHY\n");
 1284 #endif
 1285         XE_MII_DUMP(scp);
 1286         xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1287         XE_SELECT_PAGE(2);
 1288         XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);        /* Disable PHY? */
 1289         scp->autoneg_status = XE_AUTONEG_FAIL;
 1290       }
 1291       break;
 1292     }
 1293 
 1294     /*
 1295      * If we got down here _and_ autoneg_status is XE_AUTONEG_FAIL, then
 1296      * either autonegotiation failed, or never got started to begin with.  In
 1297      * either case, select a suitable 10Mbit media and hope it works.  We
 1298      * don't need to reset the card again, since it will have been done
 1299      * already by the big switch above.
 1300      */
 1301     if (scp->autoneg_status == XE_AUTONEG_FAIL) {
 1302 #if XE_DEBUG > 1
 1303       device_printf(scp->dev, "Selecting 10baseX\n");
 1304 #endif
 1305       if (scp->mohawk) {
 1306         XE_SELECT_PAGE(0x42);
 1307         XE_OUTB(XE_SWC1, 0x80);
 1308         scp->media = IFM_ETHER|IFM_10_T;
 1309         scp->autoneg_status = XE_AUTONEG_NONE;
 1310       }
 1311       else {
 1312         XE_SELECT_PAGE(4);
 1313         XE_OUTB(XE_GPR0, 4);
 1314         DELAY(50000);
 1315         XE_SELECT_PAGE(0x42);
 1316         XE_OUTB(XE_SWC1, (XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? 0x80 : 0xc0);
 1317         scp->media = IFM_ETHER|((XE_INB(XE_ESR) & XE_ESR_MEDIA_SELECT) ? IFM_10_T : IFM_10_2);
 1318         scp->autoneg_status = XE_AUTONEG_NONE;
 1319       }
 1320     }
 1321     break;
 1322 
 1323 
 1324     /*
 1325      * If a specific media has been requested, we just reset the card and
 1326      * select it (one small exception -- if 100baseTX is requested by there is 
 1327      * no PHY, we fall back to 10baseT operation).
 1328      */
 1329    case IFM_100_TX:     /* Force 100baseTX */
 1330     xe_soft_reset(scp);
 1331     if (scp->phy_ok) {
 1332 #if XE_DEBUG > 1
 1333       device_printf(scp->dev, "Selecting 100baseTX\n");
 1334 #endif
 1335       XE_SELECT_PAGE(0x42);
 1336       XE_OUTB(XE_SWC1, 0);
 1337       xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_SPEEDSEL);
 1338       XE_SELECT_PAGE(2);
 1339       XE_OUTB(XE_MSR, XE_INB(XE_MSR) | 0x08);
 1340       scp->media |= IFM_100_TX;
 1341       break;
 1342     }
 1343     /* FALLTHROUGH */
 1344 
 1345    case IFM_10_T:       /* Force 10baseT */
 1346     xe_soft_reset(scp);
 1347 #if XE_DEBUG > 1
 1348     device_printf(scp->dev, "Selecting 10baseT\n");
 1349 #endif
 1350     if (scp->phy_ok) {
 1351       xe_phy_writereg(scp, PHY_BMCR, 0x0000);
 1352       XE_SELECT_PAGE(2);
 1353       XE_OUTB(XE_MSR, XE_INB(XE_MSR) & ~0x08);  /* Disable PHY */
 1354     }
 1355     XE_SELECT_PAGE(0x42);
 1356     XE_OUTB(XE_SWC1, 0x80);
 1357     scp->media |= IFM_10_T;
 1358     break;
 1359 
 1360    case IFM_10_2:
 1361     xe_soft_reset(scp);
 1362 #if XE_DEBUG > 1
 1363     device_printf(scp->dev, "Selecting 10base2\n");
 1364 #endif
 1365     XE_SELECT_PAGE(0x42);
 1366     XE_OUTB(XE_SWC1, 0xc0);
 1367     scp->media |= IFM_10_2;
 1368     break;
 1369   }
 1370 
 1371 
 1372   /*
 1373    * Finally, the LEDs are set to match whatever media was chosen and the
 1374    * transmitter is unblocked. 
 1375    */
 1376 #if XE_DEBUG > 1
 1377   device_printf(scp->dev, "Setting LEDs\n");
 1378 #endif
 1379   XE_SELECT_PAGE(2);
 1380   switch (IFM_SUBTYPE(scp->media)) {
 1381    case IFM_100_TX:
 1382    case IFM_10_T:
 1383     XE_OUTB(XE_LED, 0x3b);
 1384     if (scp->dingo)
 1385       XE_OUTB(0x0b, 0x04);      /* 100Mbit LED */
 1386     break;
 1387 
 1388    case IFM_10_2:
 1389     XE_OUTB(XE_LED, 0x3a);
 1390     break;
 1391   }
 1392 
 1393   /* Restart output? */
 1394   scp->ifp->if_flags &= ~IFF_OACTIVE;
 1395   xe_init(scp);
 1396 }
 1397 
 1398 
 1399 /*
 1400  * Hard reset (power cycle) the card.
 1401  */
 1402 static void
 1403 xe_hard_reset(struct xe_softc *scp) {
 1404   int s;
 1405 
 1406 #ifdef XE_DEBUG
 1407   device_printf(scp->dev, "hard_reset\n");
 1408 #endif
 1409 
 1410   s = splimp();
 1411 
 1412   /*
 1413    * Power cycle the card.
 1414    */
 1415   XE_SELECT_PAGE(4);
 1416   XE_OUTB(XE_GPR1, 0);          /* Power off */
 1417   DELAY(40000);
 1418 
 1419   if (scp->mohawk)
 1420     XE_OUTB(XE_GPR1, 1);        /* And back on again */
 1421   else
 1422     XE_OUTB(XE_GPR1, 5);        /* Also set AIC bit, whatever that is */
 1423   DELAY(40000);
 1424   XE_SELECT_PAGE(0);
 1425 
 1426   (void)splx(s);
 1427 }
 1428 
 1429 
 1430 /*
 1431  * Soft reset the card.  Also makes sure that the ML6692 and 10Mbit controller 
 1432  * are powered up, sets the silicon revision number in softc, disables
 1433  * interrupts and checks for the prescence of a 100Mbit PHY.  This should
 1434  * leave us in a position where we can access the PHY and do media
 1435  * selection. The function imposes a 0.5s delay while the hardware powers up.
 1436  */
 1437 static void
 1438 xe_soft_reset(struct xe_softc *scp) {
 1439   int s;
 1440 
 1441 #ifdef XE_DEBUG
 1442   device_printf(scp->dev, "soft_reset\n");
 1443 #endif
 1444 
 1445   s = splimp();
 1446 
 1447   /*
 1448    * Reset the card, (again).
 1449    */
 1450   XE_SELECT_PAGE(0);
 1451   XE_OUTB(XE_CR, XE_CR_SOFT_RESET);
 1452   DELAY(40000);
 1453   XE_OUTB(XE_CR, 0);
 1454   DELAY(40000);
 1455 
 1456   if (scp->mohawk) {
 1457     /*
 1458      * set GP1 and GP2 as outputs (bits 2 & 3)
 1459      * set GP1 low to power on the ML6692 (bit 0)
 1460      * set GP2 high to power on the 10Mhz chip (bit 1)
 1461      */
 1462     XE_SELECT_PAGE(4);
 1463     XE_OUTB(XE_GPR0, 0x0e);
 1464   }
 1465 
 1466   /*
 1467    * Wait for everything to wake up.
 1468    */
 1469   DELAY(500000);
 1470 
 1471   /*
 1472    * Get silicon revision number.
 1473    */
 1474   XE_SELECT_PAGE(4);
 1475   if (scp->mohawk)
 1476     scp->srev = (XE_INB(XE_BOV) & 0x70) >> 4;
 1477   else
 1478     scp->srev = (XE_INB(XE_BOV) & 0x30) >> 4;
 1479 #ifdef XE_DEBUG
 1480   device_printf(scp->dev, "silicon revision = %d\n", scp->srev);
 1481 #endif
 1482   
 1483   /*
 1484    * Shut off interrupts.
 1485    */
 1486   xe_disable_intr(scp);
 1487 
 1488   /*
 1489    * Check for PHY.
 1490    */
 1491   if (scp->mohawk) {
 1492     scp->phy_ok = xe_mii_init(scp);
 1493   }
 1494 
 1495   XE_SELECT_PAGE(0);
 1496 
 1497   (void)splx(s);
 1498 }
 1499 
 1500 
 1501 /*
 1502  * Take interface offline.  This is done by powering down the device, which I
 1503  * assume means just shutting down the transceiver and Ethernet logic.  This
 1504  * requires a _hard_ reset to recover from, as we need to power up again.
 1505  */
 1506 static void
 1507 xe_stop(struct xe_softc *scp) {
 1508   int s;
 1509 
 1510 #ifdef XE_DEBUG
 1511   device_printf(scp->dev, "stop\n");
 1512 #endif
 1513 
 1514   s = splimp();
 1515 
 1516   /*
 1517    * Shut off interrupts.
 1518    */
 1519   xe_disable_intr(scp);
 1520 
 1521   /*
 1522    * Power down.
 1523    */
 1524   XE_SELECT_PAGE(4);
 1525   XE_OUTB(XE_GPR1, 0);
 1526   XE_SELECT_PAGE(0);
 1527 
 1528   /*
 1529    * ~IFF_RUNNING == interface down.
 1530    */
 1531   scp->ifp->if_flags &= ~IFF_RUNNING;
 1532   scp->ifp->if_flags &= ~IFF_OACTIVE;
 1533   scp->ifp->if_timer = 0;
 1534 
 1535   (void)splx(s);
 1536 }
 1537 
 1538 
 1539 /*
 1540  * Enable Ethernet interrupts from the card.
 1541  */
 1542 static void
 1543 xe_enable_intr(struct xe_softc *scp) {
 1544 #ifdef XE_DEBUG
 1545   device_printf(scp->dev, "enable_intr\n");
 1546 #endif
 1547 
 1548   XE_SELECT_PAGE(1);
 1549   XE_OUTB(XE_IMR0, 0xff);               /* Unmask everything */
 1550   XE_OUTB(XE_IMR1, 0x01);               /* Unmask TX underrun detection */
 1551   DELAY(1);
 1552 
 1553   XE_SELECT_PAGE(0);
 1554   XE_OUTB(XE_CR, XE_CR_ENABLE_INTR);    /* Enable interrupts */
 1555   if (scp->modem && !scp->dingo) {      /* This bit is just magic */
 1556     if (!(XE_INB(0x10) & 0x01)) {
 1557       XE_OUTB(0x10, 0x11);              /* Unmask master int enable bit */
 1558     }
 1559   }
 1560 }
 1561 
 1562 
 1563 /*
 1564  * Disable all Ethernet interrupts from the card.
 1565  */
 1566 static void
 1567 xe_disable_intr(struct xe_softc *scp) {
 1568 #ifdef XE_DEBUG
 1569   device_printf(scp->dev, "disable_intr\n");
 1570 #endif
 1571 
 1572   XE_SELECT_PAGE(0);
 1573   XE_OUTB(XE_CR, 0);                    /* Disable interrupts */
 1574   if (scp->modem && !scp->dingo) {      /* More magic (does this work?) */
 1575     XE_OUTB(0x10, 0x10);                /* Mask the master int enable bit */
 1576   }
 1577 
 1578   XE_SELECT_PAGE(1);
 1579   XE_OUTB(XE_IMR0, 0);                  /* Forbid all interrupts */
 1580   XE_OUTB(XE_IMR1, 0);
 1581   XE_SELECT_PAGE(0);
 1582 }
 1583 
 1584 
 1585 /*
 1586  * Set up multicast filter and promiscuous mode
 1587  */
 1588 static void
 1589 xe_setmulti(struct xe_softc *scp) {
 1590   struct ifnet *ifp;
 1591   struct ifmultiaddr *maddr;
 1592   int count;
 1593 
 1594   ifp = &scp->arpcom.ac_if;
 1595   maddr = ifp->if_multiaddrs.lh_first;
 1596 
 1597   /* Get length of multicast list */
 1598   for (count = 0; maddr != NULL; maddr = maddr->ifma_link.le_next, count++);
 1599 
 1600   if ((ifp->if_flags & IFF_PROMISC) || (ifp->if_flags & IFF_ALLMULTI) || (count > 9)) {
 1601     /*
 1602      * Go into promiscuous mode if either of the PROMISC or ALLMULTI flags are
 1603      * set, or if we have been asked to deal with more than 9 multicast
 1604      * addresses.  To do this: set MPE and PME in SWC1
 1605      */
 1606     XE_SELECT_PAGE(0x42);
 1607     XE_OUTB(XE_SWC1, 0x06);
 1608   }
 1609   else if ((ifp->if_flags & IFF_MULTICAST) && (count > 0)) {
 1610     /*
 1611      * Program the filters for up to 9 addresses
 1612      */
 1613     XE_SELECT_PAGE(0x42);
 1614     XE_OUTB(XE_SWC1, 0x01);
 1615     XE_SELECT_PAGE(0x40);
 1616     XE_OUTB(XE_CMD0, XE_CMD0_OFFLINE);
 1617     /*xe_reg_dump(scp);*/
 1618     xe_setaddrs(scp);
 1619     /*xe_reg_dump(scp);*/
 1620     XE_SELECT_PAGE(0x40);
 1621     XE_OUTB(XE_CMD0, XE_CMD0_RX_ENABLE|XE_CMD0_ONLINE);
 1622   }
 1623   else {
 1624     /*
 1625      * No multicast operation (default)
 1626      */
 1627     XE_SELECT_PAGE(0x42);
 1628     XE_OUTB(XE_SWC1, 0);
 1629   }
 1630   XE_SELECT_PAGE(0);
 1631 }
 1632 
 1633 
 1634 /*
 1635  * Set up all on-chip addresses (for multicast).  AFAICS, there are 10
 1636  * of these things; the first is our MAC address, the other 9 are mcast
 1637  * addresses, padded with the MAC address if there aren't enough.
 1638  * XXX - This doesn't work right, but I'm not sure why yet.  We seem to be
 1639  * XXX - doing much the same as the Linux code, which is weird enough that
 1640  * XXX - it's probably right (despite my earlier comments to the contrary).
 1641  */
 1642 static void
 1643 xe_setaddrs(struct xe_softc *scp) {
 1644   struct ifmultiaddr *maddr;
 1645   u_int8_t *addr;
 1646   u_int8_t page, slot, byte, i;
 1647 
 1648   maddr = scp->arpcom.ac_if.if_multiaddrs.lh_first;
 1649 
 1650   XE_SELECT_PAGE(page = 0x50);
 1651 
 1652   for (slot = 0, byte = 8; slot < 10; slot++) {
 1653 
 1654     if (slot == 0)
 1655       addr = (u_int8_t *)(&scp->arpcom.ac_enaddr);
 1656     else {
 1657       while (maddr != NULL && maddr->ifma_addr->sa_family != AF_LINK)
 1658         maddr = maddr->ifma_link.le_next;
 1659       if (maddr != NULL)
 1660         addr = LLADDR((struct sockaddr_dl *)maddr->ifma_addr);
 1661       else
 1662         addr = (u_int8_t *)(&scp->arpcom.ac_enaddr);
 1663     }
 1664 
 1665     for (i = 0; i < 6; i++, byte++) {
 1666 #if XE_DEBUG > 2
 1667       if (i)
 1668         printf(":%x", addr[i]);
 1669       else
 1670         device_printf(scp->dev, "individual addresses %d: %x", slot, addr[0]);
 1671 #endif
 1672 
 1673       if (byte > 15) {
 1674         page++;
 1675         byte = 8;
 1676         XE_SELECT_PAGE(page);
 1677       }
 1678 
 1679       if (scp->mohawk)
 1680         XE_OUTB(byte, addr[5 - i]);
 1681       else
 1682         XE_OUTB(byte, addr[i]);
 1683     }
 1684 #if XE_DEBUG > 2
 1685     printf("\n");
 1686 #endif
 1687   }
 1688 
 1689   XE_SELECT_PAGE(0);
 1690 }
 1691 
 1692 
 1693 /*
 1694  * Write an outgoing packet to the card using programmed I/O.
 1695  */
 1696 static int
 1697 xe_pio_write_packet(struct xe_softc *scp, struct mbuf *mbp) {
 1698   struct mbuf *mbp2;
 1699   u_int16_t len, pad, free, ok;
 1700   u_int8_t *data;
 1701   u_int8_t savebyte[2], wantbyte;
 1702 
 1703   /* Get total packet length */
 1704   for (len = 0, mbp2 = mbp; mbp2 != NULL; len += mbp2->m_len, mbp2 = mbp2->m_next);
 1705 
 1706   /* Packets < minimum length may need to be padded out */
 1707   pad = 0;
 1708   if (len < ETHER_MIN_LEN - ETHER_CRC_LEN) {
 1709     pad = (ETHER_MIN_LEN - ETHER_CRC_LEN - len + 1) >> 1;
 1710     len = ETHER_MIN_LEN - ETHER_CRC_LEN;
 1711   }
 1712 
 1713   /* Check transmit buffer space */
 1714   XE_SELECT_PAGE(0);
 1715   XE_OUTW(XE_TRS, len+2);
 1716   free = XE_INW(XE_TSO);
 1717   ok = free & 0x8000;
 1718   free &= 0x7fff;
 1719   if (free <= len + 2)
 1720     return 1;
 1721 
 1722   /* Send packet length to card */
 1723   XE_OUTW(XE_EDP, len);
 1724 
 1725   /*
 1726    * Write packet to card using PIO (code stolen from the ed driver)
 1727    */
 1728   wantbyte = 0;
 1729   while (mbp != NULL) {
 1730     len = mbp->m_len;
 1731     if (len > 0) {
 1732       data = mtod(mbp, caddr_t);
 1733       if (wantbyte) {           /* Finish the last word */
 1734         savebyte[1] = *data;
 1735         XE_OUTW(XE_EDP, *(u_short *)savebyte);
 1736         data++;
 1737         len--;
 1738         wantbyte = 0;
 1739       }
 1740       if (len > 1) {            /* Output contiguous words */
 1741         bus_space_write_multi_2(scp->bst, scp->bsh, XE_EDP, (u_int16_t *) data,
 1742          len >> 1);
 1743         data += len & ~1;
 1744         len &= 1;
 1745       }
 1746       if (len == 1) {           /* Save last byte, if necessary */
 1747         savebyte[0] = *data;
 1748         wantbyte = 1;
 1749       }
 1750     }
 1751     mbp = mbp->m_next;
 1752   }
 1753   if (wantbyte)                 /* Last byte for odd-length packets */
 1754     XE_OUTW(XE_EDP, *(u_short *)savebyte);
 1755 
 1756   /*
 1757    * For CE3 cards, just tell 'em to send -- apparently the card will pad out
 1758    * short packets with random cruft.  Otherwise, write nonsense words to fill 
 1759    * out the packet.  I guess it is then sent automatically (?)
 1760    */
 1761   if (scp->mohawk)
 1762     XE_OUTB(XE_CR, XE_CR_TX_PACKET|XE_CR_ENABLE_INTR);
 1763   else
 1764     while (pad > 0) {
 1765       XE_OUTW(XE_EDP, 0xdead);
 1766       pad--;
 1767     }
 1768 
 1769   return 0;
 1770 }
 1771 
 1772 /*
 1773  * Compute the 32-bit Ethernet CRC for the given buffer.
 1774  */
 1775 static u_int32_t
 1776 xe_compute_crc(u_int8_t *data, int len) {
 1777   u_int32_t crc = 0xffffffff;
 1778   u_int32_t poly = 0x04c11db6;
 1779   u_int8_t current, crc31, bit;
 1780   int i, k;
 1781 
 1782   for (i = 0; i < len; i++) {
 1783     current = data[i];
 1784     for (k = 1; k <= 8; k++) {
 1785       if (crc & 0x80000000) {
 1786         crc31 = 0x01;
 1787       }
 1788       else {
 1789         crc31 = 0;
 1790       }
 1791       bit = crc31 ^ (current & 0x01);
 1792       crc <<= 1;
 1793       current >>= 1;
 1794       if (bit) {
 1795         crc = (crc ^ poly)|1;
 1796       }
 1797     }
 1798   }
 1799   return crc;
 1800 }
 1801 
 1802 
 1803 /*
 1804  * Convert a CRC into an index into the multicast hash table.  What we do is
 1805  * take the most-significant 6 bits of the CRC, reverse them, and use that as
 1806  * the bit number in the hash table.  Bits 5:3 of the result give the byte
 1807  * within the table (0-7); bits 2:0 give the bit number within that byte (also 
 1808  * 0-7), ie. the number of shifts needed to get it into the lsb position.
 1809  */
 1810 static int
 1811 xe_compute_hashbit(u_int32_t crc) {
 1812   u_int8_t hashbit = 0;
 1813   int i;
 1814 
 1815   for (i = 0; i < 6; i++) {
 1816     hashbit >>= 1;
 1817     if (crc & 0x80000000) {
 1818       hashbit &= 0x80;
 1819     }
 1820     crc <<= 1;
 1821   }
 1822   return (hashbit >> 2);
 1823 }
 1824 
 1825 
 1826 
 1827 /**************************************************************
 1828  *                                                            *
 1829  *                  M I I  F U N C T I O N S                  *
 1830  *                                                            *
 1831  **************************************************************/
 1832 
 1833 /*
 1834  * Alternative MII/PHY handling code adapted from the xl driver.  It doesn't
 1835  * seem to work any better than the xirc2_ps stuff, but it's cleaner code.
 1836  * XXX - this stuff shouldn't be here.  It should all be abstracted off to
 1837  * XXX - some kind of common MII-handling code, shared by all drivers.  But
 1838  * XXX - that's a whole other mission.
 1839  */
 1840 #define XE_MII_SET(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) | (x))
 1841 #define XE_MII_CLR(x)   XE_OUTB(XE_GPR2, (XE_INB(XE_GPR2) | 0x04) & ~(x))
 1842 
 1843 
 1844 /*
 1845  * Sync the PHYs by setting data bit and strobing the clock 32 times.
 1846  */
 1847 static void
 1848 xe_mii_sync(struct xe_softc *scp) {
 1849   register int i;
 1850 
 1851   XE_SELECT_PAGE(2);
 1852   XE_MII_SET(XE_MII_DIR|XE_MII_WRD);
 1853 
 1854   for (i = 0; i < 32; i++) {
 1855     XE_MII_SET(XE_MII_CLK);
 1856     DELAY(1);
 1857     XE_MII_CLR(XE_MII_CLK);
 1858     DELAY(1);
 1859   }
 1860 }
 1861 
 1862 
 1863 /*
 1864  * Look for a MII-compliant PHY.  If we find one, reset it.
 1865  */
 1866 static int
 1867 xe_mii_init(struct xe_softc *scp) {
 1868   u_int16_t status;
 1869 
 1870   status = xe_phy_readreg(scp, PHY_BMSR);
 1871   if ((status & 0xff00) != 0x7800) {
 1872 #if XE_DEBUG > 1
 1873     device_printf(scp->dev, "no PHY found, %0x\n", status);
 1874 #endif
 1875     return 0;
 1876   }
 1877   else {
 1878 #if XE_DEBUG > 1
 1879     device_printf(scp->dev, "PHY OK!\n");
 1880 #endif
 1881 
 1882     /* Reset the PHY */
 1883     xe_phy_writereg(scp, PHY_BMCR, PHY_BMCR_RESET);
 1884     DELAY(500);
 1885     while(xe_phy_readreg(scp, PHY_BMCR) & PHY_BMCR_RESET);
 1886     XE_MII_DUMP(scp);
 1887     return 1;
 1888   }
 1889 }
 1890 
 1891 
 1892 /*
 1893  * Clock a series of bits through the MII.
 1894  */
 1895 static void
 1896 xe_mii_send(struct xe_softc *scp, u_int32_t bits, int cnt) {
 1897   int i;
 1898 
 1899   XE_SELECT_PAGE(2);
 1900   XE_MII_CLR(XE_MII_CLK);
 1901   
 1902   for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
 1903     if (bits & i) {
 1904       XE_MII_SET(XE_MII_WRD);
 1905     } else {
 1906       XE_MII_CLR(XE_MII_WRD);
 1907     }
 1908     DELAY(1);
 1909     XE_MII_CLR(XE_MII_CLK);
 1910     DELAY(1);
 1911     XE_MII_SET(XE_MII_CLK);
 1912   }
 1913 }
 1914 
 1915 
 1916 /*
 1917  * Read an PHY register through the MII.
 1918  */
 1919 static int
 1920 xe_mii_readreg(struct xe_softc *scp, struct xe_mii_frame *frame) {
 1921   int i, ack, s;
 1922 
 1923   s = splimp();
 1924 
 1925   /*
 1926    * Set up frame for RX.
 1927    */
 1928   frame->mii_stdelim = XE_MII_STARTDELIM;
 1929   frame->mii_opcode = XE_MII_READOP;
 1930   frame->mii_turnaround = 0;
 1931   frame->mii_data = 0;
 1932         
 1933   XE_SELECT_PAGE(2);
 1934   XE_OUTB(XE_GPR2, 0);
 1935 
 1936   /*
 1937    * Turn on data xmit.
 1938    */
 1939   XE_MII_SET(XE_MII_DIR);
 1940 
 1941   xe_mii_sync(scp);
 1942 
 1943   /*    
 1944    * Send command/address info.
 1945    */
 1946   xe_mii_send(scp, frame->mii_stdelim, 2);
 1947   xe_mii_send(scp, frame->mii_opcode, 2);
 1948   xe_mii_send(scp, frame->mii_phyaddr, 5);
 1949   xe_mii_send(scp, frame->mii_regaddr, 5);
 1950 
 1951   /* Idle bit */
 1952   XE_MII_CLR((XE_MII_CLK|XE_MII_WRD));
 1953   DELAY(1);
 1954   XE_MII_SET(XE_MII_CLK);
 1955   DELAY(1);
 1956 
 1957   /* Turn off xmit. */
 1958   XE_MII_CLR(XE_MII_DIR);
 1959 
 1960   /* Check for ack */
 1961   XE_MII_CLR(XE_MII_CLK);
 1962   DELAY(1);
 1963   ack = XE_INB(XE_GPR2) & XE_MII_RDD;
 1964   XE_MII_SET(XE_MII_CLK);
 1965   DELAY(1);
 1966 
 1967   /*
 1968    * Now try reading data bits. If the ack failed, we still
 1969    * need to clock through 16 cycles to keep the PHY(s) in sync.
 1970    */
 1971   if (ack) {
 1972     for(i = 0; i < 16; i++) {
 1973       XE_MII_CLR(XE_MII_CLK);
 1974       DELAY(1);
 1975       XE_MII_SET(XE_MII_CLK);
 1976       DELAY(1);
 1977     }
 1978     goto fail;
 1979   }
 1980 
 1981   for (i = 0x8000; i; i >>= 1) {
 1982     XE_MII_CLR(XE_MII_CLK);
 1983     DELAY(1);
 1984     if (!ack) {
 1985       if (XE_INB(XE_GPR2) & XE_MII_RDD)
 1986         frame->mii_data |= i;
 1987       DELAY(1);
 1988     }
 1989     XE_MII_SET(XE_MII_CLK);
 1990     DELAY(1);
 1991   }
 1992 
 1993 fail:
 1994 
 1995   XE_MII_CLR(XE_MII_CLK);
 1996   DELAY(1);
 1997   XE_MII_SET(XE_MII_CLK);
 1998   DELAY(1);
 1999 
 2000   splx(s);
 2001 
 2002   if (ack)
 2003     return(1);
 2004   return(0);
 2005 }
 2006 
 2007 
 2008 /*
 2009  * Write to a PHY register through the MII.
 2010  */
 2011 static int
 2012 xe_mii_writereg(struct xe_softc *scp, struct xe_mii_frame *frame) {
 2013   int s;
 2014 
 2015   s = splimp();
 2016 
 2017   /*
 2018    * Set up frame for TX.
 2019    */
 2020   frame->mii_stdelim = XE_MII_STARTDELIM;
 2021   frame->mii_opcode = XE_MII_WRITEOP;
 2022   frame->mii_turnaround = XE_MII_TURNAROUND;
 2023         
 2024   XE_SELECT_PAGE(2);
 2025 
 2026   /*            
 2027    * Turn on data output.
 2028    */
 2029   XE_MII_SET(XE_MII_DIR);
 2030 
 2031   xe_mii_sync(scp);
 2032 
 2033   xe_mii_send(scp, frame->mii_stdelim, 2);
 2034   xe_mii_send(scp, frame->mii_opcode, 2);
 2035   xe_mii_send(scp, frame->mii_phyaddr, 5);
 2036   xe_mii_send(scp, frame->mii_regaddr, 5);
 2037   xe_mii_send(scp, frame->mii_turnaround, 2);
 2038   xe_mii_send(scp, frame->mii_data, 16);
 2039 
 2040   /* Idle bit. */
 2041   XE_MII_SET(XE_MII_CLK);
 2042   DELAY(1);
 2043   XE_MII_CLR(XE_MII_CLK);
 2044   DELAY(1);
 2045 
 2046   /*
 2047    * Turn off xmit.
 2048    */
 2049   XE_MII_CLR(XE_MII_DIR);
 2050 
 2051   splx(s);
 2052 
 2053   return(0);
 2054 }
 2055 
 2056 
 2057 /*
 2058  * Read a register from the PHY.
 2059  */
 2060 static u_int16_t
 2061 xe_phy_readreg(struct xe_softc *scp, u_int16_t reg) {
 2062   struct xe_mii_frame frame;
 2063 
 2064   bzero((char *)&frame, sizeof(frame));
 2065 
 2066   frame.mii_phyaddr = 0;
 2067   frame.mii_regaddr = reg;
 2068   xe_mii_readreg(scp, &frame);
 2069 
 2070   return(frame.mii_data);
 2071 }
 2072 
 2073 
 2074 /*
 2075  * Write to a PHY register.
 2076  */
 2077 static void
 2078 xe_phy_writereg(struct xe_softc *scp, u_int16_t reg, u_int16_t data) {
 2079   struct xe_mii_frame frame;
 2080 
 2081   bzero((char *)&frame, sizeof(frame));
 2082 
 2083   frame.mii_phyaddr = 0;
 2084   frame.mii_regaddr = reg;
 2085   frame.mii_data = data;
 2086   xe_mii_writereg(scp, &frame);
 2087 
 2088   return;
 2089 }
 2090 
 2091 
 2092 #ifdef XE_DEBUG
 2093 /*
 2094  * A bit of debugging code.
 2095  */
 2096 static void
 2097 xe_mii_dump(struct xe_softc *scp) {
 2098   int i, s;
 2099 
 2100   s = splimp();
 2101 
 2102   device_printf(scp->dev, "MII registers: ");
 2103   for (i = 0; i < 2; i++) {
 2104     printf(" %d:%04x", i, xe_phy_readreg(scp, i));
 2105   }
 2106   for (i = 4; i < 7; i++) {
 2107     printf(" %d:%04x", i, xe_phy_readreg(scp, i));
 2108   }
 2109   printf("\n");
 2110 
 2111   (void)splx(s);
 2112 }
 2113 
 2114 static void
 2115 xe_reg_dump(struct xe_softc *scp) {
 2116   int page, i, s;
 2117 
 2118   s = splimp();
 2119 
 2120   device_printf(scp->dev, "Common registers: ");
 2121   for (i = 0; i < 8; i++) {
 2122     printf(" %2.2x", XE_INB(i));
 2123   }
 2124   printf("\n");
 2125 
 2126   for (page = 0; page <= 8; page++) {
 2127     device_printf(scp->dev, "Register page %2.2x: ", page);
 2128     XE_SELECT_PAGE(page);
 2129     for (i = 8; i < 16; i++) {
 2130       printf(" %2.2x", XE_INB(i));
 2131     }
 2132     printf("\n");
 2133   }
 2134 
 2135   for (page = 0x10; page < 0x5f; page++) {
 2136     if ((page >= 0x11 && page <= 0x3f) ||
 2137         (page == 0x41) ||
 2138         (page >= 0x43 && page <= 0x4f) ||
 2139         (page >= 0x59))
 2140       continue;
 2141     device_printf(scp->dev, "Register page %2.2x: ", page);
 2142     XE_SELECT_PAGE(page);
 2143     for (i = 8; i < 16; i++) {
 2144       printf(" %2.2x", XE_INB(i));
 2145     }
 2146     printf("\n");
 2147   }
 2148 
 2149   (void)splx(s);
 2150 }
 2151 #endif
 2152 
 2153 int
 2154 xe_activate(device_t dev)
 2155 {
 2156         struct xe_softc *sc = device_get_softc(dev);
 2157         int start, err;
 2158 
 2159         if (!sc->dingo) {
 2160                 sc->port_rid = 0;       /* 0 is managed by pccard */
 2161                 sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
 2162                     &sc->port_rid, 0, ~0, 16, RF_ACTIVE);
 2163         } else {
 2164                 /*
 2165                  * Find a 16 byte aligned ioport for the card.
 2166                  */
 2167 #if XE_DEBUG > 0
 2168                 device_printf(dev, "Finding an aligned port for RealPort\n");
 2169 #endif /* XE_DEBUG */
 2170                 sc->port_rid = 1;       /* 0 is managed by pccard */
 2171                 start = 0x100;
 2172                 do {
 2173                         sc->port_res = bus_alloc_resource(dev,
 2174                             SYS_RES_IOPORT, &sc->port_rid, start, 0x3ff, 16,
 2175                             RF_ACTIVE);
 2176                         if (sc->port_res == 0)
 2177                                 break;          /* we failed */
 2178                         if ((rman_get_start(sc->port_res) & 0xf) == 0)
 2179                                 break;          /* good */
 2180                         bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
 2181                             sc->port_res);
 2182                         start = (rman_get_start(sc->port_res) + 15) & ~0xf;
 2183                 } while (1);
 2184 #if XE_DEBUG > 2
 2185                 device_printf(dev, "port 0x%0lx, size 0x%0lx\n",
 2186                     bus_get_resource_start(dev, SYS_RES_IOPORT, sc->port_rid),
 2187                     bus_get_resource_count(dev, SYS_RES_IOPORT, sc->port_rid));
 2188 #endif /* XE_DEBUG */
 2189         }
 2190         if (!sc->port_res) {
 2191 #if XE_DEBUG > 0
 2192                 device_printf(dev, "Cannot allocate ioport\n");
 2193 #endif          
 2194                 return ENOMEM;
 2195         }
 2196 
 2197         sc->irq_rid = 0;
 2198         sc->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->irq_rid, 
 2199             0, ~0, 1, RF_ACTIVE);
 2200         if (!sc->irq_res) {
 2201 #if XE_DEBUG > 0
 2202                 device_printf(dev, "Cannot allocate irq\n");
 2203 #endif
 2204                 xe_deactivate(dev);
 2205                 return ENOMEM;
 2206         }
 2207         if ((err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET, xe_intr, sc,
 2208             &sc->intrhand)) != 0) {
 2209                 xe_deactivate(dev);
 2210                 return err;
 2211         }
 2212 
 2213         sc->bst = rman_get_bustag(sc->port_res);
 2214         sc->bsh = rman_get_bushandle(sc->port_res);
 2215         return (0);
 2216 }
 2217 
 2218 void
 2219 xe_deactivate(device_t dev)
 2220 {
 2221         struct xe_softc *sc = device_get_softc(dev);
 2222         
 2223         if (sc->intrhand)
 2224                 bus_teardown_intr(dev, sc->irq_res, sc->intrhand);
 2225         sc->intrhand = 0;
 2226         if (sc->port_res)
 2227                 bus_release_resource(dev, SYS_RES_IOPORT, sc->port_rid, 
 2228                     sc->port_res);
 2229         sc->port_res = 0;
 2230         if (sc->irq_res)
 2231                 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, 
 2232                     sc->irq_res);
 2233         sc->irq_res = 0;
 2234         return;
 2235 }
 2236 
 2237 static device_method_t xe_pccard_methods[] = {
 2238         /* Device interface */
 2239         DEVMETHOD(device_probe,         xe_probe),
 2240         DEVMETHOD(device_attach,        xe_attach),
 2241         DEVMETHOD(device_detach,        xe_detach),
 2242 
 2243         { 0, 0 }
 2244 };
 2245 
 2246 static driver_t xe_pccard_driver = {
 2247         "xe",
 2248         xe_pccard_methods,
 2249         sizeof(struct xe_softc),
 2250 };
 2251 
 2252 devclass_t xe_devclass;
 2253 
 2254 DRIVER_MODULE(xe, pccard, xe_pccard_driver, xe_devclass, 0, 0);

Cache object: e67a5809306641f2d3d199327e833217


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