Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.35
      1 /*	$NetBSD: if_xi.c,v 1.35 2004/08/06 20:38:09 mycroft Exp $ */
      2 /*	OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp 	*/
      3 
      4 /*
      5  * XXX THIS DRIVER IS BROKEN WRT. MULTICAST LISTS AND PROMISC/ALLMULTI
      6  * XXX FLAGS!
      7  */
      8 
      9 /*
     10  * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
     11  * All rights reserved.
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  * 1. Redistributions of source code must retain the above copyright
     17  *    notice, this list of conditions and the following disclaimer.
     18  * 2. Redistributions in binary form must reproduce the above copyright
     19  *    notice, this list of conditions and the following disclaimer in the
     20  *    documentation and/or other materials provided with the distribution.
     21  * 3. All advertising materials mentioning features or use of this software
     22  *    must display the following acknowledgement:
     23  *	This product includes software developed by Niklas Hallqvist,
     24  *	Brandon Creighton and Job de Haas.
     25  * 4. The name of the author may not be used to endorse or promote products
     26  *    derived from this software without specific prior written permission
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     29  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     30  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     31  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     33  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     37  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
     42  */
     43 
     44 /*
     45  * Known Bugs:
     46  *
     47  * 1) Promiscuous mode doesn't work on at least the CE2.
     48  * 2) Slow. ~450KB/s.  Memory access would be better.
     49  */
     50 
     51 #include <sys/cdefs.h>
     52 __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.35 2004/08/06 20:38:09 mycroft Exp $");
     53 
     54 #include "opt_inet.h"
     55 #include "opt_ipx.h"
     56 #include "bpfilter.h"
     57 
     58 #include <sys/param.h>
     59 #include <sys/systm.h>
     60 #include <sys/device.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/mbuf.h>
     63 #include <sys/malloc.h>
     64 #include <sys/socket.h>
     65 
     66 #include "rnd.h"
     67 #if NRND > 0
     68 #include <sys/rnd.h>
     69 #endif
     70 
     71 #include <net/if.h>
     72 #include <net/if_dl.h>
     73 #include <net/if_media.h>
     74 #include <net/if_types.h>
     75 #include <net/if_ether.h>
     76 
     77 #ifdef INET
     78 #include <netinet/in.h>
     79 #include <netinet/in_systm.h>
     80 #include <netinet/in_var.h>
     81 #include <netinet/ip.h>
     82 #include <netinet/if_inarp.h>
     83 #endif
     84 
     85 #ifdef IPX
     86 #include <netipx/ipx.h>
     87 #include <netipx/ipx_if.h>
     88 #endif
     89 
     90 #ifdef NS
     91 #include <netns/ns.h>
     92 #include <netns/ns_if.h>
     93 #endif
     94 
     95 #if NBPFILTER > 0
     96 #include <net/bpf.h>
     97 #include <net/bpfdesc.h>
     98 #endif
     99 
    100 /*
    101  * Maximum number of bytes to read per interrupt.  Linux recommends
    102  * somewhere between 2000-22000.
    103  * XXX This is currently a hard maximum.
    104  */
    105 #define MAX_BYTES_INTR 12000
    106 
    107 #include <dev/mii/mii.h>
    108 #include <dev/mii/miivar.h>
    109 
    110 #include <dev/pcmcia/pcmciareg.h>
    111 #include <dev/pcmcia/pcmciavar.h>
    112 #include <dev/pcmcia/pcmciadevs.h>
    113 
    114 #include <dev/pcmcia/if_xireg.h>
    115 
    116 #ifdef __GNUC__
    117 #define INLINE	__inline
    118 #else
    119 #define INLINE
    120 #endif	/* __GNUC__ */
    121 
    122 /*#define	XIDEBUG*/
    123 /*#define	XIDEBUG_VALUE	XID_CONFIG*/
    124 
    125 #ifdef XIDEBUG
    126 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
    127 
    128 #define XID_CONFIG	0x1
    129 #define XID_MII		0x2
    130 #define XID_INTR	0x4
    131 #define XID_FIFO	0x8
    132 
    133 #ifdef XIDEBUG_VALUE
    134 int xidebug = XIDEBUG_VALUE;
    135 #else
    136 int xidebug = 0;
    137 #endif
    138 #else
    139 #define DPRINTF(cat, x) (void)0
    140 #endif
    141 
    142 int	xi_pcmcia_match __P((struct device *, struct cfdata *, void *));
    143 void	xi_pcmcia_attach __P((struct device *, struct device *, void *));
    144 int	xi_pcmcia_detach __P((struct device *, int));
    145 int	xi_pcmcia_activate __P((struct device *, enum devact));
    146 
    147 /*
    148  * In case this chipset ever turns up out of pcmcia attachments (very
    149  * unlikely) do the driver splitup.
    150  */
    151 struct xi_softc {
    152 	struct device sc_dev;			/* Generic device info */
    153 	struct ethercom sc_ethercom;		/* Ethernet common part */
    154 
    155 	struct mii_data sc_mii;			/* MII media information */
    156 
    157 	bus_space_tag_t		sc_bst;		/* Bus cookie */
    158 	bus_space_handle_t	sc_bsh;		/* Bus I/O handle */
    159 	bus_size_t		sc_offset;	/* Offset of registers */
    160 
    161 	u_int8_t	sc_rev;			/* Chip revision */
    162 	u_int32_t	sc_flags;		/* Misc. flags */
    163 	int		sc_all_mcasts;		/* Receive all multicasts */
    164 	u_int8_t 	sc_enaddr[ETHER_ADDR_LEN];
    165 #if NRND > 0
    166 	rndsource_element_t	sc_rnd_source;
    167 #endif
    168 };
    169 
    170 struct xi_pcmcia_softc {
    171 	struct	xi_softc sc_xi;			/* Generic device info */
    172 
    173 	/* PCMCIA-specific goo */
    174 	struct	pcmcia_function *sc_pf;		/* PCMCIA function */
    175 	struct	pcmcia_io_handle sc_pcioh;	/* iospace info */
    176 	int	sc_io_window;			/* io window info */
    177 	void	*sc_ih;				/* Interrupt handler */
    178 	void	*sc_powerhook;			/* power hook descriptor */
    179 	int	sc_resource;			/* resource allocated */
    180 #define XI_RES_PCIC	1
    181 #define XI_RES_IO_ALLOC	2
    182 #define XI_RES_IO_MAP	4
    183 #define XI_RES_MI	8
    184 #define XI_RES_RND	16
    185 };
    186 
    187 CFATTACH_DECL(xi_pcmcia, sizeof(struct xi_pcmcia_softc),
    188     xi_pcmcia_match, xi_pcmcia_attach, xi_pcmcia_detach, xi_pcmcia_activate);
    189 
    190 static int xi_pcmcia_cis_quirks __P((struct pcmcia_function *));
    191 static void xi_cycle_power __P((struct xi_softc *));
    192 static int xi_ether_ioctl __P((struct ifnet *, u_long cmd, caddr_t));
    193 static void xi_full_reset __P((struct xi_softc *));
    194 static void xi_init __P((struct xi_softc *));
    195 static int xi_intr __P((void *));
    196 static int xi_ioctl __P((struct ifnet *, u_long, caddr_t));
    197 static int xi_mdi_read __P((struct device *, int, int));
    198 static void xi_mdi_write __P((struct device *, int, int, int));
    199 static int xi_mediachange __P((struct ifnet *));
    200 static void xi_mediastatus __P((struct ifnet *, struct ifmediareq *));
    201 static int xi_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    202 static int xi_pcmcia_lan_nid_ciscallback __P((struct pcmcia_tuple *, void *));
    203 static int xi_pcmcia_manfid_ciscallback __P((struct pcmcia_tuple *, void *));
    204 static u_int16_t xi_get __P((struct xi_softc *));
    205 static void xi_reset __P((struct xi_softc *));
    206 static void xi_set_address __P((struct xi_softc *));
    207 static void xi_start __P((struct ifnet *));
    208 static void xi_statchg __P((struct device *));
    209 static void xi_stop __P((struct xi_softc *));
    210 static void xi_watchdog __P((struct ifnet *));
    211 const struct xi_pcmcia_product *xi_pcmcia_identify __P((struct device *,
    212 						struct pcmcia_attach_args *));
    213 static int xi_pcmcia_enable __P((struct xi_pcmcia_softc *));
    214 static void xi_pcmcia_disable __P((struct xi_pcmcia_softc *));
    215 static void xi_pcmcia_power __P((int, void *));
    216 
    217 /* flags */
    218 #define XIFLAGS_MOHAWK	0x001		/* 100Mb capabilities (has phy) */
    219 #define XIFLAGS_DINGO	0x002		/* realport cards ??? */
    220 #define XIFLAGS_MODEM	0x004		/* modem also present */
    221 
    222 const struct xi_pcmcia_product {
    223 	u_int32_t	xpp_vendor;	/* vendor ID */
    224 	u_int32_t	xpp_product;	/* product ID */
    225 	int		xpp_expfunc;	/* expected function number */
    226 	int		xpp_flags;	/* initial softc flags */
    227 } xi_pcmcia_products[] = {
    228 #ifdef NOT_SUPPORTED
    229 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    230 	  0,				0 },
    231 #endif
    232 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    233 	  0,				0 },
    234 	{ PCMCIA_VENDOR_XIRCOM,		0x0142,
    235 	  0,				0 },
    236 	{ PCMCIA_VENDOR_XIRCOM,		0x0143,
    237 	  0,				XIFLAGS_MOHAWK },
    238 	{ PCMCIA_VENDOR_COMPAQ2,	0x0143,
    239 	  0,				XIFLAGS_MOHAWK },
    240 	{ PCMCIA_VENDOR_INTEL,		0x0143,
    241 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM },
    242 	{ PCMCIA_VENDOR_XIRCOM,		PCMCIA_PRODUCT_XIRCOM_XE2000,
    243 	  0,				XIFLAGS_MOHAWK },
    244 	{ PCMCIA_VENDOR_XIRCOM,		PCMCIA_PRODUCT_XIRCOM_REM56,
    245 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM },
    246 #ifdef NOT_SUPPORTED
    247 	{ PCMCIA_VENDOR_XIRCOM,		0x1141,
    248 	  0,				XIFLAGS_MODEM },
    249 #endif
    250 	{ PCMCIA_VENDOR_XIRCOM,		0x1142,
    251 	  0,				XIFLAGS_MODEM },
    252 	{ PCMCIA_VENDOR_XIRCOM,		0x1143,
    253 	  0,				XIFLAGS_MODEM },
    254 	{ PCMCIA_VENDOR_XIRCOM,		0x1144,
    255 	  0,				XIFLAGS_MODEM },
    256 	{ PCMCIA_VENDOR_XIRCOM,		0x1145,
    257 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM },
    258 	{ PCMCIA_VENDOR_XIRCOM,		0x1146,
    259 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM },
    260 	{ PCMCIA_VENDOR_XIRCOM,		0x1147,
    261 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM },
    262 };
    263 
    264 
    265 const struct xi_pcmcia_product *
    266 xi_pcmcia_identify(dev, pa)
    267 	struct device *dev;
    268         struct pcmcia_attach_args *pa;
    269 {
    270 	const struct xi_pcmcia_product *xpp;
    271         u_int8_t id;
    272 	u_int32_t prod;
    273 	int n;
    274 
    275 	/*
    276 	 * The Xircom ethernet cards swap the revision and product fields
    277 	 * inside the CIS, which makes identification just a little
    278 	 * bit different.
    279 	 */
    280 
    281         pcmcia_scan_cis(dev, xi_pcmcia_manfid_ciscallback, &id);
    282 
    283 	prod = (pa->product & ~0xff) | id;
    284 
    285 	DPRINTF(XID_CONFIG, ("product=0x%x\n", prod));
    286 
    287 	for (xpp = xi_pcmcia_products,
    288 	    n = sizeof(xi_pcmcia_products) / sizeof(xi_pcmcia_products[0]);
    289 	    n; xpp++, n--) {
    290 		if (pa->manufacturer == xpp->xpp_vendor &&
    291 		    prod == xpp->xpp_product &&
    292 		    pa->pf->number == xpp->xpp_expfunc)
    293 			return (xpp);
    294 	}
    295 	return (NULL);
    296 }
    297 
    298 /*
    299  * The quirks are done here instead of the traditional framework because
    300  * of the difficulty in identifying the devices.
    301  */
    302 static int
    303 xi_pcmcia_cis_quirks(pf)
    304 	struct pcmcia_function *pf;
    305 {
    306 	struct pcmcia_config_entry *cfe;
    307 
    308 	/* Tell the pcmcia framework where the CCR is. */
    309 	pf->ccr_base = 0x800;
    310 	pf->ccr_mask = 0x67;
    311 
    312 	/* Fake a cfe. */
    313 	SIMPLEQ_FIRST(&pf->cfe_head) = cfe = (struct pcmcia_config_entry *)
    314 	    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT|M_ZERO);
    315 
    316 	if (cfe == NULL)
    317 		return -1;
    318 
    319 	/*
    320 	 * XXX Use preprocessor symbols instead.
    321 	 * Enable ethernet & its interrupts, wiring them to -INT
    322 	 * No I/O base.
    323 	 */
    324 	cfe->number = 0x5;
    325 	cfe->flags = 0;		/* XXX Check! */
    326 	cfe->iftype = PCMCIA_IFTYPE_IO;
    327 	cfe->num_iospace = 0;
    328 	cfe->num_memspace = 0;
    329 	cfe->irqmask = 0x8eb0;
    330 
    331 	return 0;
    332 }
    333 
    334 int
    335 xi_pcmcia_match(parent, match, aux)
    336 	struct device *parent;
    337 	struct cfdata *match;
    338 	void *aux;
    339 {
    340 	struct pcmcia_attach_args *pa = aux;
    341 
    342 	if (pa->manufacturer == PCMCIA_VENDOR_COMPAQ2 &&
    343 	    pa->product == PCMCIA_PRODUCT_COMPAQ2_CPQ_10_100)
    344 		return (1);
    345 
    346 	if (pa->manufacturer == PCMCIA_VENDOR_INTEL &&
    347 	    pa->product == PCMCIA_PRODUCT_INTEL_EEPRO100)
    348 		return (1);
    349 
    350 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM) {
    351 		if (pa->product == 0x110a)
    352 			return (2); /* prevent attach to com_pcmcia */
    353 		if ((pa->product & (XIMEDIA_ETHER << 8)) != 0)
    354 			return (1);
    355 	}
    356 
    357 	return (0);
    358 }
    359 
    360 void
    361 xi_pcmcia_attach(parent, self, aux)
    362 	struct device *parent, *self;
    363 	void *aux;
    364 {
    365 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    366 	struct xi_softc *sc = &psc->sc_xi;
    367 	struct pcmcia_attach_args *pa = aux;
    368 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    369 	const struct xi_pcmcia_product *xpp;
    370 
    371 	aprint_normal("\n");
    372 
    373 	if (xi_pcmcia_cis_quirks(pa->pf) < 0) {
    374 		aprint_error("%s: function enable failed\n", self->dv_xname);
    375 		return;
    376 	}
    377 
    378 	/* Enable the card */
    379 	psc->sc_pf = pa->pf;
    380 	pcmcia_function_init(psc->sc_pf, SIMPLEQ_FIRST(&psc->sc_pf->cfe_head));
    381 	if (pcmcia_function_enable(psc->sc_pf)) {
    382 		aprint_error("%s: function enable failed\n", self->dv_xname);
    383 		goto fail;
    384 	}
    385 	psc->sc_resource |= XI_RES_PCIC;
    386 
    387 	/* allocate/map ISA I/O space */
    388 	if (pcmcia_io_alloc(psc->sc_pf, 0, XI_IOSIZE, XI_IOSIZE,
    389 		&psc->sc_pcioh) != 0) {
    390 		aprint_error("%s: I/O allocation failed\n", self->dv_xname);
    391 		goto fail;
    392 	}
    393 	psc->sc_resource |= XI_RES_IO_ALLOC;
    394 
    395 	sc->sc_bst = psc->sc_pcioh.iot;
    396 	sc->sc_bsh = psc->sc_pcioh.ioh;
    397 	sc->sc_offset = 0;
    398 
    399 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_AUTO, 0, XI_IOSIZE,
    400 		&psc->sc_pcioh, &psc->sc_io_window)) {
    401 		aprint_error("%s: can't map I/O space\n", self->dv_xname);
    402 		goto fail;
    403 	}
    404 	psc->sc_resource |= XI_RES_IO_MAP;
    405 
    406 	xpp = xi_pcmcia_identify(parent,pa);
    407 	if (xpp == NULL) {
    408 		aprint_error("%s: unrecognised model\n", self->dv_xname);
    409 		return;
    410 	}
    411 	sc->sc_flags = xpp->xpp_flags;
    412 
    413 	/*
    414 	 * Configuration as advised by DINGO documentation.
    415 	 * Dingo has some extra configuration registers in the CCR space.
    416 	 */
    417 	if (sc->sc_flags & XIFLAGS_DINGO) {
    418 		struct pcmcia_mem_handle pcmh;
    419 		int ccr_window;
    420 		bus_size_t ccr_offset;
    421 
    422 		/* get access to the DINGO CCR space */
    423 		if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
    424 			&pcmh)) {
    425 			DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
    426 			goto fail;
    427 		}
    428 		if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
    429 			psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
    430 			&pcmh, &ccr_offset, &ccr_window)) {
    431 			DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
    432 			pcmcia_mem_free(psc->sc_pf, &pcmh);
    433 			goto fail;
    434 		}
    435 
    436 		/* enable the second function - usually modem */
    437 		bus_space_write_1(pcmh.memt, pcmh.memh,
    438 		    ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
    439 		bus_space_write_1(pcmh.memt, pcmh.memh,
    440 		    ccr_offset + PCMCIA_CCR_DCOR1,
    441 		    PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
    442 		bus_space_write_1(pcmh.memt, pcmh.memh,
    443 		    ccr_offset + PCMCIA_CCR_DCOR2, 0);
    444 		bus_space_write_1(pcmh.memt, pcmh.memh,
    445 		    ccr_offset + PCMCIA_CCR_DCOR3, 0);
    446 		bus_space_write_1(pcmh.memt, pcmh.memh,
    447 		    ccr_offset + PCMCIA_CCR_DCOR4, 0);
    448 
    449 		/* We don't need them anymore and can free them (I think). */
    450 		pcmcia_mem_unmap(psc->sc_pf, ccr_window);
    451 		pcmcia_mem_free(psc->sc_pf, &pcmh);
    452 	}
    453 
    454 	/*
    455 	 * Get the ethernet address from FUNCE/LAN_NID tuple.
    456 	 */
    457 	xi_pcmcia_funce_enaddr(parent, sc->sc_enaddr);
    458 	if (!sc->sc_enaddr) {
    459 		aprint_error("%s: unable to get ethernet address\n",
    460 		    self->dv_xname);
    461 		goto fail;
    462 	}
    463 
    464 	printf("%s: Ethernet address %s\n", self->dv_xname,
    465 	    ether_sprintf(sc->sc_enaddr));
    466 
    467 	ifp = &sc->sc_ethercom.ec_if;
    468 	memcpy(ifp->if_xname, self->dv_xname, IFNAMSIZ);
    469 	ifp->if_softc = sc;
    470 	ifp->if_start = xi_start;
    471 	ifp->if_ioctl = xi_ioctl;
    472 	ifp->if_watchdog = xi_watchdog;
    473 	ifp->if_flags =
    474 	    IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
    475 	IFQ_SET_READY(&ifp->if_snd);
    476 
    477 	/* Reset and initialize the card. */
    478 	xi_full_reset(sc);
    479 
    480 	/*
    481 	 * Initialize our media structures and probe the MII.
    482 	 */
    483 	sc->sc_mii.mii_ifp = ifp;
    484 	sc->sc_mii.mii_readreg = xi_mdi_read;
    485 	sc->sc_mii.mii_writereg = xi_mdi_write;
    486 	sc->sc_mii.mii_statchg = xi_statchg;
    487 	ifmedia_init(&sc->sc_mii.mii_media, 0, xi_mediachange,
    488 	    xi_mediastatus);
    489 	DPRINTF(XID_MII | XID_CONFIG,
    490 	    ("xi: bmsr %x\n", xi_mdi_read(&sc->sc_dev, 0, 1)));
    491 	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    492 		MII_OFFSET_ANY, 0);
    493 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
    494 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
    495 		    NULL);
    496 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    497 
    498 	/* 802.1q capability */
    499 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    500 	/* Attach the interface. */
    501 	if_attach(ifp);
    502 	ether_ifattach(ifp, sc->sc_enaddr);
    503 	psc->sc_resource |= XI_RES_MI;
    504 
    505 #if NRND > 0
    506 	rnd_attach_source(&sc->sc_rnd_source, self->dv_xname,
    507 	    RND_TYPE_NET, 0);
    508 	psc->sc_resource |= XI_RES_RND;
    509 #endif
    510 
    511 	/*
    512 	 * Reset and initialize the card again for DINGO (as found in Linux
    513 	 * driver).  Without this Dingo will get a watchdog timeout the first
    514 	 * time.  The ugly media tickling seems to be necessary for getting
    515 	 * autonegotiation to work too.
    516 	 */
    517 	if (sc->sc_flags & XIFLAGS_DINGO) {
    518 		xi_full_reset(sc);
    519 		xi_init(sc);
    520 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    521 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE);
    522 		xi_stop(sc);
    523 	}
    524 
    525 	psc->sc_powerhook = powerhook_establish(xi_pcmcia_power, sc);
    526 
    527 	pcmcia_function_disable(psc->sc_pf);
    528 	psc->sc_resource &= ~XI_RES_PCIC;
    529 
    530 	return;
    531 
    532 fail:
    533 	if ((psc->sc_resource & XI_RES_IO_MAP) != 0) {
    534 		pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    535 		psc->sc_resource &= ~XI_RES_IO_MAP;
    536         }
    537 	if ((psc->sc_resource & XI_RES_IO_ALLOC) != 0) {
    538 		pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    539 		psc->sc_resource &= ~XI_RES_IO_ALLOC;
    540         }
    541 	if (psc->sc_resource & XI_RES_PCIC) {
    542 		pcmcia_function_disable(pa->pf);
    543 		psc->sc_resource &= ~XI_RES_PCIC;
    544 	}
    545 	free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
    546 }
    547 
    548 int
    549 xi_pcmcia_detach(self, flags)
    550      struct device *self;
    551      int flags;
    552 {
    553 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    554 	struct xi_softc *sc = &psc->sc_xi;
    555 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    556 
    557 	DPRINTF(XID_CONFIG, ("xi_pcmcia_detach()\n"));
    558 
    559 	if (psc->sc_powerhook != NULL)
    560 		powerhook_disestablish(psc->sc_powerhook);
    561 
    562 #if NRND > 0
    563 	if ((psc->sc_resource & XI_RES_RND) != 0)
    564 		rnd_detach_source(&sc->sc_rnd_source);
    565 #endif
    566 
    567 	if ((psc->sc_resource & XI_RES_MI) != 0) {
    568 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    569 		ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    570 		ether_ifdetach(ifp);
    571 		if_detach(ifp);
    572 		psc->sc_resource &= ~XI_RES_MI;
    573 	}
    574 	if (psc->sc_resource & XI_RES_IO_MAP) {
    575 		pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    576 		psc->sc_resource &= ~XI_RES_IO_MAP;
    577 	}
    578 	if ((psc->sc_resource & XI_RES_IO_ALLOC) != 0) {
    579                 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    580 	        psc->sc_resource &= ~XI_RES_IO_ALLOC;
    581         }
    582 
    583 	xi_pcmcia_disable(psc);
    584 
    585         free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
    586 
    587 	return 0;
    588 }
    589 
    590 int
    591 xi_pcmcia_activate(self, act)
    592      struct device *self;
    593      enum devact act;
    594 {
    595 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    596 	struct xi_softc *sc = &psc->sc_xi;
    597 	int s, rv=0;
    598 
    599 	DPRINTF(XID_CONFIG, ("xi_pcmcia_activate()\n"));
    600 
    601 	s = splnet();
    602 	switch (act) {
    603 	case DVACT_ACTIVATE:
    604 		rv = EOPNOTSUPP;
    605 		break;
    606 
    607 	case DVACT_DEACTIVATE:
    608 		if_deactivate(&sc->sc_ethercom.ec_if);
    609 		break;
    610 	}
    611 	splx(s);
    612 	return (rv);
    613 }
    614 
    615 static int
    616 xi_pcmcia_enable(psc)
    617         struct xi_pcmcia_softc *psc;
    618 {
    619 	struct xi_softc *sc = &psc->sc_xi;
    620 
    621 	DPRINTF(XID_CONFIG,("xi_pcmcia_enable()\n"));
    622 
    623 	if (pcmcia_function_enable(psc->sc_pf))
    624 		return (1);
    625 	psc->sc_resource |= XI_RES_PCIC;
    626 
    627 	/* establish the interrupt. */
    628 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, xi_intr, sc);
    629 	if (psc->sc_ih == NULL) {
    630 		printf("%s: couldn't establish interrupt\n",
    631 		    sc->sc_dev.dv_xname);
    632 		pcmcia_function_disable(psc->sc_pf);
    633 		psc->sc_resource &= ~XI_RES_PCIC;
    634 		return (1);
    635 	}
    636 
    637 	xi_full_reset(sc);
    638 
    639         return (0);
    640 }
    641 
    642 
    643 static void
    644 xi_pcmcia_disable(psc)
    645 	struct xi_pcmcia_softc *psc;
    646 {
    647 	DPRINTF(XID_CONFIG,("xi_pcmcia_disable()\n"));
    648 
    649 	if (psc->sc_resource & XI_RES_PCIC) {
    650 		pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    651 		pcmcia_function_disable(psc->sc_pf);
    652 		psc->sc_resource &= ~XI_RES_PCIC;
    653 	}
    654 }
    655 
    656 
    657 static void
    658 xi_pcmcia_power(why, arg)
    659 	int why;
    660 	void *arg;
    661 {
    662 	struct xi_pcmcia_softc *psc = arg;
    663 	struct xi_softc *sc = &psc->sc_xi;
    664 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    665 	int s;
    666 
    667 	DPRINTF(XID_CONFIG,("xi_pcmcia_power()\n"));
    668 
    669 	s = splnet();
    670 
    671 	switch (why) {
    672 	case PWR_SUSPEND:
    673 	case PWR_STANDBY:
    674 		if (ifp->if_flags & IFF_RUNNING) {
    675 			xi_stop(sc);
    676 		}
    677 		ifp->if_flags &= ~IFF_RUNNING;
    678 		ifp->if_timer = 0;
    679 		break;
    680 	case PWR_RESUME:
    681 		if ((ifp->if_flags & IFF_RUNNING) == 0) {
    682 			xi_init(sc);
    683 		}
    684 		ifp->if_flags |= IFF_RUNNING;
    685 		break;
    686 	case PWR_SOFTSUSPEND:
    687 	case PWR_SOFTSTANDBY:
    688 	case PWR_SOFTRESUME:
    689 		break;
    690 	}
    691 	splx(s);
    692 }
    693 
    694 /*
    695  * XXX These two functions might be OK to factor out into pcmcia.c since
    696  * if_sm_pcmcia.c uses similar ones.
    697  */
    698 static int
    699 xi_pcmcia_funce_enaddr(parent, myla)
    700 	struct device *parent;
    701 	u_int8_t *myla;
    702 {
    703 	/* XXX The Linux driver has more ways to do this in case of failure. */
    704 	return (pcmcia_scan_cis(parent, xi_pcmcia_lan_nid_ciscallback, myla));
    705 }
    706 
    707 static int
    708 xi_pcmcia_lan_nid_ciscallback(tuple, arg)
    709 	struct pcmcia_tuple *tuple;
    710 	void *arg;
    711 {
    712 	u_int8_t *myla = arg;
    713 	int i;
    714 
    715 	DPRINTF(XID_CONFIG, ("xi_pcmcia_lan_nid_ciscallback()\n"));
    716 
    717 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    718 		if (tuple->length < 2)
    719 			return (0);
    720 
    721 		switch (pcmcia_tuple_read_1(tuple, 0)) {
    722 		case PCMCIA_TPLFE_TYPE_LAN_NID:
    723 			if (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)
    724 				return (0);
    725 			break;
    726 
    727 		case 0x02:
    728 			/*
    729 			 * Not sure about this, I don't have a CE2
    730 			 * that puts the ethernet addr here.
    731 			 */
    732 		 	if (pcmcia_tuple_read_1(tuple, 1) != 13)
    733 				return (0);
    734 			break;
    735 
    736 		default:
    737 			return (0);
    738 		}
    739 
    740 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    741 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    742 		return (1);
    743 	}
    744 
    745 	/* Yet another spot where this might be. */
    746 	if (tuple->code == 0x89) {
    747 		pcmcia_tuple_read_1(tuple, 1);
    748 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    749 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    750 		return (1);
    751 	}
    752 	return (0);
    753 }
    754 
    755 int
    756 xi_pcmcia_manfid_ciscallback(tuple, arg)
    757 	struct pcmcia_tuple *tuple;
    758 	void *arg;
    759 {
    760 	u_int8_t *id = arg;
    761 
    762 	DPRINTF(XID_CONFIG, ("xi_pcmcia_manfid_callback()\n"));
    763 
    764 	if (tuple->code != PCMCIA_CISTPL_MANFID)
    765 		return (0);
    766 
    767 	if (tuple->length < 2)
    768 		return (0);
    769 
    770 	*id = pcmcia_tuple_read_1(tuple, 4);
    771 	return (1);
    772 }
    773 
    774 static int
    775 xi_intr(arg)
    776 	void *arg;
    777 {
    778 	struct xi_softc *sc = arg;
    779 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    780 	u_int8_t esr, rsr, isr, rx_status, savedpage;
    781 	u_int16_t tx_status, recvcount = 0, tempint;
    782 
    783 	DPRINTF(XID_CONFIG, ("xi_intr()\n"));
    784 
    785 #if 0
    786 	if (!(ifp->if_flags & IFF_RUNNING))
    787 		return (0);
    788 #endif
    789 
    790 	ifp->if_timer = 0;	/* turn watchdog timer off */
    791 
    792 	if (sc->sc_flags & XIFLAGS_MOHAWK) {
    793 		/* Disable interrupt (Linux does it). */
    794 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    795 		    0);
    796 	}
    797 
    798 	savedpage =
    799 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
    800 
    801 	PAGE(sc, 0);
    802 	esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
    803 	isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
    804 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    805 
    806 	/* Check to see if card has been ejected. */
    807 	if (isr == 0xff) {
    808 #ifdef DIAGNOSTIC
    809 		printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
    810 #endif
    811 		goto end;
    812 	}
    813 
    814 	PAGE(sc, 40);
    815 	rx_status =
    816 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
    817 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0,
    818 	    ~rx_status & 0xff);
    819 	tx_status =
    820 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
    821 	tx_status |=
    822 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1) << 8;
    823 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0,0);
    824 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1,0);
    825 
    826 	PAGE(sc, 0);
    827 	while (esr & FULL_PKT_RCV) {
    828 		if (!(rsr & RSR_RX_OK))
    829 			break;
    830 
    831 		/* Compare bytes read this interrupt to hard maximum. */
    832 		if (recvcount > MAX_BYTES_INTR) {
    833 			DPRINTF(XID_INTR,
    834 			    ("xi: too many bytes this interrupt\n"));
    835 			ifp->if_iqdrops++;
    836 			/* Drop packet. */
    837 			bus_space_write_2(sc->sc_bst, sc->sc_bsh,
    838 			    sc->sc_offset + DO0, DO_SKIP_RX_PKT);
    839 		}
    840 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    841 		recvcount += tempint;
    842 		ifp->if_ibytes += tempint;
    843 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    844 		    sc->sc_offset + ESR);
    845 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    846 		    sc->sc_offset + RSR);
    847 	}
    848 
    849 	/* Packet too long? */
    850 	if (rsr & RSR_TOO_LONG) {
    851 		ifp->if_ierrors++;
    852 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    853 	}
    854 
    855 	/* CRC error? */
    856 	if (rsr & RSR_CRCERR) {
    857 		ifp->if_ierrors++;
    858 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    859 	}
    860 
    861 	/* Alignment error? */
    862 	if (rsr & RSR_ALIGNERR) {
    863 		ifp->if_ierrors++;
    864 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    865 	}
    866 
    867 	/* Check for rx overrun. */
    868 	if (rx_status & RX_OVERRUN) {
    869 		ifp->if_ierrors++;
    870 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    871 		    CLR_RX_OVERRUN);
    872 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    873 	}
    874 
    875 	/* Try to start more packets transmitting. */
    876 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    877 		xi_start(ifp);
    878 
    879 	/* Detected excessive collisions? */
    880 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    881 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    882 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    883 		    RESTART_TX);
    884 		ifp->if_oerrors++;
    885 	}
    886 
    887 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    888 		ifp->if_oerrors++;
    889 
    890 	/* have handled the interrupt */
    891 #if NRND > 0
    892 	rnd_add_uint32(&sc->sc_rnd_source, tx_status);
    893 #endif
    894 
    895 end:
    896 	/* Reenable interrupts. */
    897 	PAGE(sc, savedpage);
    898 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    899 	    ENABLE_INT);
    900 
    901 	return (1);
    902 }
    903 
    904 /*
    905  * Pull a packet from the card into an mbuf chain.
    906  */
    907 static u_int16_t
    908 xi_get(sc)
    909 	struct xi_softc *sc;
    910 {
    911 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    912 	struct mbuf *top, **mp, *m;
    913 	u_int16_t pktlen, len, recvcount = 0;
    914 	u_int8_t *data;
    915 	u_int8_t rsr;
    916 
    917 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    918 
    919 	PAGE(sc, 0);
    920 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    921 
    922 	pktlen =
    923 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
    924 	    RBC_COUNT_MASK;
    925 
    926 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    927 
    928 	if (pktlen == 0) {
    929 		/*
    930 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    931 		 * when MPE is set.  It is not known why.
    932 		 */
    933 		return (0);
    934 	}
    935 
    936 	/* XXX should this be incremented now ? */
    937 	recvcount += pktlen;
    938 
    939 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    940 	if (m == 0)
    941 		return (recvcount);
    942 	m->m_pkthdr.rcvif = ifp;
    943 	m->m_pkthdr.len = pktlen;
    944 	m->m_flags |= M_HASFCS;
    945 	len = MHLEN;
    946 	top = 0;
    947 	mp = &top;
    948 
    949 	while (pktlen > 0) {
    950 		if (top) {
    951 			MGET(m, M_DONTWAIT, MT_DATA);
    952 			if (m == 0) {
    953 				m_freem(top);
    954 				return (recvcount);
    955 			}
    956 			len = MLEN;
    957 		}
    958 		if (pktlen >= MINCLSIZE) {
    959 			MCLGET(m, M_DONTWAIT);
    960 			if (!(m->m_flags & M_EXT)) {
    961 				m_freem(m);
    962 				m_freem(top);
    963 				return (recvcount);
    964 			}
    965 			len = MCLBYTES;
    966 		}
    967 		if (!top) {
    968 			caddr_t newdata = (caddr_t)ALIGN(m->m_data +
    969 			    sizeof(struct ether_header)) -
    970 			    sizeof(struct ether_header);
    971 			len -= newdata - m->m_data;
    972 			m->m_data = newdata;
    973 		}
    974 		len = min(pktlen, len);
    975 		data = mtod(m, u_int8_t *);
    976 		if (len > 1) {
    977 		        len &= ~1;
    978 			bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh,
    979 			    sc->sc_offset + EDP, (u_int16_t *)data, len>>1);
    980 		} else
    981 			*data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    982 			    sc->sc_offset + EDP);
    983 		m->m_len = len;
    984 		pktlen -= len;
    985 		*mp = m;
    986 		mp = &m->m_next;
    987 	}
    988 
    989 	/* Skip Rx packet. */
    990 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
    991 	    DO_SKIP_RX_PKT);
    992 
    993 	ifp->if_ipackets++;
    994 
    995 #if NBPFILTER > 0
    996 	if (ifp->if_bpf)
    997 		bpf_mtap(ifp->if_bpf, top);
    998 #endif
    999 
   1000 	(*ifp->if_input)(ifp, top);
   1001 	return (recvcount);
   1002 }
   1003 
   1004 /*
   1005  * Serial management for the MII.
   1006  * The DELAY's below stem from the fact that the maximum frequency
   1007  * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
   1008  * go much faster than that.
   1009  */
   1010 
   1011 /* Let the MII serial management be idle for one period. */
   1012 static INLINE void xi_mdi_idle __P((struct xi_softc *));
   1013 static INLINE void
   1014 xi_mdi_idle(sc)
   1015 	struct xi_softc *sc;
   1016 {
   1017 	bus_space_tag_t bst = sc->sc_bst;
   1018 	bus_space_handle_t bsh = sc->sc_bsh;
   1019 	bus_size_t offset = sc->sc_offset;
   1020 
   1021 	/* Drive MDC low... */
   1022 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
   1023 	DELAY(1);
   1024 
   1025 	/* and high again. */
   1026 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
   1027 	DELAY(1);
   1028 }
   1029 
   1030 /* Pulse out one bit of data. */
   1031 static INLINE void xi_mdi_pulse __P((struct xi_softc *, int));
   1032 static INLINE void
   1033 xi_mdi_pulse(sc, data)
   1034 	struct xi_softc *sc;
   1035 	int data;
   1036 {
   1037 	bus_space_tag_t bst = sc->sc_bst;
   1038 	bus_space_handle_t bsh = sc->sc_bsh;
   1039 	bus_size_t offset = sc->sc_offset;
   1040 	u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
   1041 
   1042 	/* First latch the data bit MDIO with clock bit MDC low...*/
   1043 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
   1044 	DELAY(1);
   1045 
   1046 	/* then raise the clock again, preserving the data bit. */
   1047 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
   1048 	DELAY(1);
   1049 }
   1050 
   1051 /* Probe one bit of data. */
   1052 static INLINE int xi_mdi_probe __P((struct xi_softc *sc));
   1053 static INLINE int
   1054 xi_mdi_probe(sc)
   1055 	struct xi_softc *sc;
   1056 {
   1057 	bus_space_tag_t bst = sc->sc_bst;
   1058 	bus_space_handle_t bsh = sc->sc_bsh;
   1059 	bus_size_t offset = sc->sc_offset;
   1060 	u_int8_t x;
   1061 
   1062 	/* Pull clock bit MDCK low... */
   1063 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
   1064 	DELAY(1);
   1065 
   1066 	/* Read data and drive clock high again. */
   1067 	x = bus_space_read_1(bst, bsh, offset + GP2) & MDIO;
   1068 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
   1069 	DELAY(1);
   1070 
   1071 	return (x);
   1072 }
   1073 
   1074 /* Pulse out a sequence of data bits. */
   1075 static INLINE void xi_mdi_pulse_bits __P((struct xi_softc *, u_int32_t, int));
   1076 static INLINE void
   1077 xi_mdi_pulse_bits(sc, data, len)
   1078 	struct xi_softc *sc;
   1079 	u_int32_t data;
   1080 	int len;
   1081 {
   1082 	u_int32_t mask;
   1083 
   1084 	for (mask = 1 << (len - 1); mask; mask >>= 1)
   1085 		xi_mdi_pulse(sc, data & mask);
   1086 }
   1087 
   1088 /* Read a PHY register. */
   1089 static int
   1090 xi_mdi_read(self, phy, reg)
   1091 	struct device *self;
   1092 	int phy;
   1093 	int reg;
   1094 {
   1095 	struct xi_softc *sc = (struct xi_softc *)self;
   1096 	int i;
   1097 	u_int32_t mask;
   1098 	u_int32_t data = 0;
   1099 
   1100 	PAGE(sc, 2);
   1101 	for (i = 0; i < 32; i++)	/* Synchronize. */
   1102 		xi_mdi_pulse(sc, 1);
   1103 	xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
   1104 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
   1105 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
   1106 	xi_mdi_idle(sc);		/* Turn around. */
   1107 	xi_mdi_probe(sc);		/* Drop initial zero bit. */
   1108 
   1109 	for (mask = 1 << 15; mask; mask >>= 1) {
   1110 		if (xi_mdi_probe(sc))
   1111 			data |= mask;
   1112 	}
   1113 	xi_mdi_idle(sc);
   1114 
   1115 	DPRINTF(XID_MII,
   1116 	    ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
   1117 
   1118 	return (data);
   1119 }
   1120 
   1121 /* Write a PHY register. */
   1122 static void
   1123 xi_mdi_write(self, phy, reg, value)
   1124 	struct device *self;
   1125 	int phy;
   1126 	int reg;
   1127 	int value;
   1128 {
   1129 	struct xi_softc *sc = (struct xi_softc *)self;
   1130 	int i;
   1131 
   1132 	PAGE(sc, 2);
   1133 	for (i = 0; i < 32; i++)	/* Synchronize. */
   1134 		xi_mdi_pulse(sc, 1);
   1135 	xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
   1136 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
   1137 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
   1138 	xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
   1139 	xi_mdi_pulse_bits(sc, value, 16);	/* Write the data */
   1140 	xi_mdi_idle(sc);		/* Idle away. */
   1141 
   1142 	DPRINTF(XID_MII,
   1143 	    ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
   1144 }
   1145 
   1146 static void
   1147 xi_statchg(self)
   1148 	struct device *self;
   1149 {
   1150 	/* XXX Update ifp->if_baudrate */
   1151 }
   1152 
   1153 /*
   1154  * Change media according to request.
   1155  */
   1156 static int
   1157 xi_mediachange(ifp)
   1158 	struct ifnet *ifp;
   1159 {
   1160 	DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
   1161 
   1162 	if (ifp->if_flags & IFF_UP)
   1163 		xi_init(ifp->if_softc);
   1164 	return (0);
   1165 }
   1166 
   1167 /*
   1168  * Notify the world which media we're using.
   1169  */
   1170 static void
   1171 xi_mediastatus(ifp, ifmr)
   1172 	struct ifnet *ifp;
   1173 	struct ifmediareq *ifmr;
   1174 {
   1175 	struct xi_softc *sc = ifp->if_softc;
   1176 
   1177 	DPRINTF(XID_CONFIG, ("xi_mediastatus()\n"));
   1178 
   1179 	mii_pollstat(&sc->sc_mii);
   1180 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1181 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1182 }
   1183 
   1184 static void
   1185 xi_reset(sc)
   1186 	struct xi_softc *sc;
   1187 {
   1188 	int s;
   1189 
   1190 	DPRINTF(XID_CONFIG, ("xi_reset()\n"));
   1191 
   1192 	s = splnet();
   1193 	xi_stop(sc);
   1194 	xi_full_reset(sc);
   1195 	xi_init(sc);
   1196 	splx(s);
   1197 }
   1198 
   1199 static void
   1200 xi_watchdog(ifp)
   1201 	struct ifnet *ifp;
   1202 {
   1203 	struct xi_softc *sc = ifp->if_softc;
   1204 
   1205 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1206 	++ifp->if_oerrors;
   1207 
   1208 	xi_reset(sc);
   1209 }
   1210 
   1211 static void
   1212 xi_stop(sc)
   1213 	register struct xi_softc *sc;
   1214 {
   1215 	DPRINTF(XID_CONFIG, ("xi_stop()\n"));
   1216 
   1217 	/* Disable interrupts. */
   1218 	PAGE(sc, 0);
   1219 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR, 0);
   1220 
   1221 	PAGE(sc, 1);
   1222 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + IMR0, 0);
   1223 
   1224 	/* Power down, wait. */
   1225 	PAGE(sc, 4);
   1226 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + GP1, 0);
   1227 	DELAY(40000);
   1228 
   1229 	/* Cancel watchdog timer. */
   1230 	sc->sc_ethercom.ec_if.if_timer = 0;
   1231 }
   1232 
   1233 static void
   1234 xi_init(sc)
   1235 	struct xi_softc *sc;
   1236 {
   1237 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)sc;
   1238 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1239 	int s;
   1240 
   1241 	DPRINTF(XID_CONFIG, ("xi_init()\n"));
   1242 
   1243 	if ((psc->sc_resource & XI_RES_PCIC) == 0)
   1244 		xi_pcmcia_enable(psc);
   1245 
   1246 	s = splnet();
   1247 
   1248 	xi_set_address(sc);
   1249 
   1250 	/* Set current media. */
   1251 	mii_mediachg(&sc->sc_mii);
   1252 
   1253 	ifp->if_flags |= IFF_RUNNING;
   1254 	ifp->if_flags &= ~IFF_OACTIVE;
   1255 	splx(s);
   1256 }
   1257 
   1258 /*
   1259  * Start outputting on the interface.
   1260  * Always called as splnet().
   1261  */
   1262 static void
   1263 xi_start(ifp)
   1264 	struct ifnet *ifp;
   1265 {
   1266 	struct xi_softc *sc = ifp->if_softc;
   1267 	bus_space_tag_t bst = sc->sc_bst;
   1268 	bus_space_handle_t bsh = sc->sc_bsh;
   1269 	bus_size_t offset = sc->sc_offset;
   1270 	unsigned int s, len, pad = 0;
   1271 	struct mbuf *m0, *m;
   1272 	u_int16_t space;
   1273 
   1274 	DPRINTF(XID_CONFIG, ("xi_start()\n"));
   1275 
   1276 	/* Don't transmit if interface is busy or not running. */
   1277 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
   1278 		DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
   1279 		return;
   1280 	}
   1281 
   1282 	/* Peek at the next packet. */
   1283 	IFQ_POLL(&ifp->if_snd, m0);
   1284 	if (m0 == 0)
   1285 		return;
   1286 
   1287 	/* We need to use m->m_pkthdr.len, so require the header. */
   1288 	if (!(m0->m_flags & M_PKTHDR))
   1289 		panic("xi_start: no header mbuf");
   1290 
   1291 	len = m0->m_pkthdr.len;
   1292 
   1293 	/* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
   1294 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
   1295 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
   1296 
   1297 	PAGE(sc, 0);
   1298 	space = bus_space_read_2(bst, bsh, offset + TSO0) & 0x7fff;
   1299 	if (len + pad + 2 > space) {
   1300 		DPRINTF(XID_FIFO,
   1301 		    ("xi: not enough space in output FIFO (%d > %d)\n",
   1302 		    len + pad + 2, space));
   1303 		return;
   1304 	}
   1305 
   1306 	IFQ_DEQUEUE(&ifp->if_snd, m0);
   1307 
   1308 #if NBPFILTER > 0
   1309 	if (ifp->if_bpf)
   1310 		bpf_mtap(ifp->if_bpf, m0);
   1311 #endif
   1312 
   1313 	/*
   1314 	 * Do the output at splhigh() so that an interrupt from another device
   1315 	 * won't cause a FIFO underrun.
   1316 	 */
   1317 	s = splhigh();
   1318 
   1319 	bus_space_write_2(bst, bsh, offset + TSO2, (u_int16_t)len + pad + 2);
   1320 	bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
   1321 	for (m = m0; m; ) {
   1322 		if (m->m_len > 1)
   1323 			bus_space_write_multi_2(bst, bsh, offset + EDP,
   1324 			    mtod(m, u_int16_t *), m->m_len>>1);
   1325 		if (m->m_len & 1)
   1326 			bus_space_write_1(bst, bsh, offset + EDP,
   1327 			    *(mtod(m, u_int8_t *) + m->m_len - 1));
   1328 		MFREE(m, m0);
   1329 		m = m0;
   1330 	}
   1331 	if (sc->sc_flags & XIFLAGS_MOHAWK)
   1332 		bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
   1333 	else {
   1334 		for (; pad > 1; pad -= 2)
   1335 			bus_space_write_2(bst, bsh, offset + EDP, 0);
   1336 		if (pad == 1)
   1337 			bus_space_write_1(bst, bsh, offset + EDP, 0);
   1338 	}
   1339 
   1340 	splx(s);
   1341 
   1342 	ifp->if_timer = 5;
   1343 	++ifp->if_opackets;
   1344 }
   1345 
   1346 static int
   1347 xi_ether_ioctl(ifp, cmd, data)
   1348 	struct ifnet *ifp;
   1349 	u_long cmd;
   1350 	caddr_t data;
   1351 {
   1352 	struct ifaddr *ifa = (struct ifaddr *)data;
   1353 	struct xi_softc *sc = ifp->if_softc;
   1354 
   1355 
   1356 	DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
   1357 
   1358 	switch (cmd) {
   1359 	case SIOCSIFADDR:
   1360 		ifp->if_flags |= IFF_UP;
   1361 
   1362 		switch (ifa->ifa_addr->sa_family) {
   1363 #ifdef INET
   1364 		case AF_INET:
   1365 			xi_init(sc);
   1366 			arp_ifinit(ifp, ifa);
   1367 			break;
   1368 #endif	/* INET */
   1369 
   1370 #ifdef NS
   1371 		case AF_NS:
   1372 		{
   1373 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1374 
   1375 			if (ns_nullhost(*ina))
   1376 				ina->x_host = *(union ns_host *)
   1377 					LLADDR(ifp->if_sadl);
   1378 			else
   1379 				memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
   1380 					ifp->if_addrlen);
   1381 			/* Set new address. */
   1382 			xi_init(sc);
   1383 			break;
   1384 		}
   1385 #endif  /* NS */
   1386 
   1387 		default:
   1388 			xi_init(sc);
   1389 			break;
   1390 		}
   1391 		break;
   1392 
   1393 	default:
   1394 		return (EINVAL);
   1395 	}
   1396 
   1397 	return (0);
   1398 }
   1399 
   1400 static int
   1401 xi_ioctl(ifp, command, data)
   1402 	struct ifnet *ifp;
   1403 	u_long command;
   1404 	caddr_t data;
   1405 {
   1406 	struct xi_pcmcia_softc *psc = ifp->if_softc;
   1407 	struct xi_softc *sc = &psc->sc_xi;
   1408 	struct ifreq *ifr = (struct ifreq *)data;
   1409 	int s, error = 0;
   1410 
   1411 	DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
   1412 
   1413 	s = splnet();
   1414 
   1415 	switch (command) {
   1416 	case SIOCSIFADDR:
   1417 		error = xi_ether_ioctl(ifp, command, data);
   1418 		break;
   1419 
   1420 	case SIOCSIFFLAGS:
   1421 		sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1422 
   1423 		PAGE(sc, 0x42);
   1424 		if ((ifp->if_flags & IFF_PROMISC) ||
   1425 		    (ifp->if_flags & IFF_ALLMULTI))
   1426 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1427 			    sc->sc_offset + SWC1,
   1428 			    SWC1_PROMISC | SWC1_MCAST_PROM);
   1429 		else
   1430 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1431 			    sc->sc_offset + SWC1, 0);
   1432 
   1433 		/*
   1434 		 * If interface is marked up and not running, then start it.
   1435 		 * If it is marked down and running, stop it.
   1436 		 * XXX If it's up then re-initialize it. This is so flags
   1437 		 * such as IFF_PROMISC are handled.
   1438 		 */
   1439 		if (ifp->if_flags & IFF_UP) {
   1440 			xi_init(sc);
   1441 		} else {
   1442 			if (ifp->if_flags & IFF_RUNNING) {
   1443 				xi_pcmcia_disable(psc);
   1444 				xi_stop(sc);
   1445 				ifp->if_flags &= ~IFF_RUNNING;
   1446 			}
   1447 		}
   1448 		break;
   1449 
   1450 	case SIOCADDMULTI:
   1451 	case SIOCDELMULTI:
   1452 		sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1453 		error = (command == SIOCADDMULTI) ?
   1454 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1455 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1456 
   1457 		if (error == ENETRESET) {
   1458 			/*
   1459 			 * Multicast list has changed; set the hardware
   1460 			 * filter accordingly.
   1461 			 */
   1462 			if (!sc->sc_all_mcasts &&
   1463 			    !(ifp->if_flags & IFF_PROMISC))
   1464 				xi_set_address(sc);
   1465 
   1466 			/*
   1467 			 * xi_set_address() can turn on all_mcasts if we run
   1468 			 * out of space, so check it again rather than else {}.
   1469 			 */
   1470 			if (sc->sc_all_mcasts)
   1471 				xi_init(sc);
   1472 			error = 0;
   1473 		}
   1474 		break;
   1475 
   1476 	case SIOCSIFMEDIA:
   1477 	case SIOCGIFMEDIA:
   1478 		error =
   1479 		    ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
   1480 		break;
   1481 
   1482 	default:
   1483 		error = EINVAL;
   1484 	}
   1485 	splx(s);
   1486 	return (error);
   1487 }
   1488 
   1489 static void
   1490 xi_set_address(sc)
   1491 	struct xi_softc *sc;
   1492 {
   1493 	bus_space_tag_t bst = sc->sc_bst;
   1494 	bus_space_handle_t bsh = sc->sc_bsh;
   1495 	bus_size_t offset = sc->sc_offset;
   1496 	struct ethercom *ether = &sc->sc_ethercom;
   1497 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1498 #if WORKING_MULTICAST
   1499 	struct ether_multistep step;
   1500 	struct ether_multi *enm;
   1501 	int page, pos, num;
   1502 #endif
   1503 	int i;
   1504 
   1505 	DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
   1506 
   1507 	PAGE(sc, 0x50);
   1508 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1509 		bus_space_write_1(bst, bsh, offset + IA + i,
   1510 		    sc->sc_enaddr[(sc->sc_flags & XIFLAGS_MOHAWK) ?  5-i : i]);
   1511 	}
   1512 
   1513 	if (ether->ec_multicnt > 0) {
   1514 #ifdef WORKING_MULTICAST
   1515 		if (ether->ec_multicnt > 9) {
   1516 #else
   1517 		{
   1518 #endif
   1519 			PAGE(sc, 0x42);
   1520 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1521 			    sc->sc_offset + SWC1,
   1522 			    SWC1_PROMISC | SWC1_MCAST_PROM);
   1523 			ifp->if_flags |= IFF_PROMISC;
   1524 			return;
   1525 		}
   1526 
   1527 #ifdef WORKING_MULTICAST
   1528 
   1529 		ETHER_FIRST_MULTI(step, ether, enm);
   1530 
   1531 		pos = IA + 6;
   1532 		for (page = 0x50, num = ether->ec_multicnt; num > 0 && enm;
   1533 		    num--) {
   1534 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1535 			    sizeof(enm->enm_addrlo)) != 0) {
   1536 				/*
   1537 				 * The multicast address is really a range;
   1538 				 * it's easier just to accept all multicasts.
   1539 				 * XXX should we be setting IFF_ALLMULTI here?
   1540 				 */
   1541 #if 0
   1542 				ifp->if_flags |= IFF_ALLMULTI;
   1543 #endif
   1544 				sc->sc_all_mcasts=1;
   1545 				break;
   1546 			}
   1547 
   1548 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1549 				printf("%x:", enm->enm_addrlo[i]);
   1550 				bus_space_write_1(bst, bsh, offset + pos,
   1551 				    enm->enm_addrlo[
   1552 				    (sc->sc_flags & XIFLAGS_MOHAWK) ? 5-i : i]);
   1553 
   1554 				if (++pos > 15) {
   1555 					pos = IA;
   1556 					page++;
   1557 					PAGE(sc, page);
   1558 				}
   1559 			}
   1560 			printf("\n");
   1561 			ETHER_NEXT_MULTI(step, enm);
   1562 		}
   1563 #endif
   1564 	}
   1565 }
   1566 
   1567 static void
   1568 xi_cycle_power(sc)
   1569 	struct xi_softc *sc;
   1570 {
   1571 	bus_space_tag_t bst = sc->sc_bst;
   1572 	bus_space_handle_t bsh = sc->sc_bsh;
   1573 	bus_size_t offset = sc->sc_offset;
   1574 
   1575 	DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
   1576 
   1577 	PAGE(sc, 4);
   1578 	DELAY(1);
   1579 	bus_space_write_1(bst, bsh, offset + GP1, 0);
   1580 	DELAY(40000);
   1581 	if (sc->sc_flags & XIFLAGS_MOHAWK)
   1582 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
   1583 	else
   1584 		/* XXX What is bit 2 (aka AIC)? */
   1585 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
   1586 	DELAY(20000);
   1587 }
   1588 
   1589 static void
   1590 xi_full_reset(sc)
   1591 	struct xi_softc *sc;
   1592 {
   1593 	bus_space_tag_t bst = sc->sc_bst;
   1594 	bus_space_handle_t bsh = sc->sc_bsh;
   1595 	bus_size_t offset = sc->sc_offset;
   1596 
   1597 	DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
   1598 
   1599 	/* Do an as extensive reset as possible on all functions. */
   1600 	xi_cycle_power(sc);
   1601 	bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
   1602 	DELAY(20000);
   1603 	bus_space_write_1(bst, bsh, offset + CR, 0);
   1604 	DELAY(20000);
   1605 	if (sc->sc_flags & XIFLAGS_MOHAWK) {
   1606 		PAGE(sc, 4);
   1607 		/*
   1608 		 * Drive GP1 low to power up ML6692 and GP2 high to power up
   1609 		 * the 10MHz chip.  XXX What chip is that?  The phy?
   1610 		 */
   1611 		bus_space_write_1(bst, bsh, offset + GP0,
   1612 		    GP1_OUT | GP2_OUT | GP2_WR);
   1613 	}
   1614 	DELAY(500000);
   1615 
   1616 	/* Get revision information.  XXX Symbolic constants. */
   1617 	sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
   1618 	    ((sc->sc_flags & XIFLAGS_MOHAWK) ? 0x70 : 0x30) >> 4;
   1619 
   1620 	/* Media selection.  XXX Maybe manual overriding too? */
   1621 	if (!(sc->sc_flags & XIFLAGS_MOHAWK)) {
   1622 		PAGE(sc, 4);
   1623 		/*
   1624 		 * XXX I have no idea what this really does, it is from the
   1625 		 * Linux driver.
   1626 		 */
   1627 		bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
   1628 	}
   1629 	DELAY(40000);
   1630 
   1631 	/* Setup the ethernet interrupt mask. */
   1632 	PAGE(sc, 1);
   1633 #if 1
   1634 	bus_space_write_1(bst, bsh, offset + IMR0,
   1635 	    ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
   1636 	    ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
   1637 #else
   1638 	bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
   1639 #endif
   1640 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1641 		/* XXX What is this?  Not for Dingo at least. */
   1642 		/* Unmask TX underrun detection */
   1643 		bus_space_write_1(bst, bsh, offset + IMR1, 1);
   1644 	}
   1645 
   1646 	/*
   1647 	 * Disable source insertion.
   1648 	 * XXX Dingo does not have this bit, but Linux does it unconditionally.
   1649 	 */
   1650 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1651 		PAGE(sc, 0x42);
   1652 		bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
   1653 	}
   1654 
   1655 	/* Set the local memory dividing line. */
   1656 	if (sc->sc_rev != 1) {
   1657 		PAGE(sc, 2);
   1658 		/* XXX Symbolic constant preferrable. */
   1659 		bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
   1660 	}
   1661 
   1662 	xi_set_address(sc);
   1663 
   1664 	/*
   1665 	 * Apparently the receive byte pointer can be bad after a reset, so
   1666 	 * we hardwire it correctly.
   1667 	 */
   1668 	PAGE(sc, 0);
   1669 	bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
   1670 
   1671 	/* Setup ethernet MAC registers. XXX Symbolic constants. */
   1672 	PAGE(sc, 0x40);
   1673 	bus_space_write_1(bst, bsh, offset + RX0MSK,
   1674 	    PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
   1675 	bus_space_write_1(bst, bsh, offset + TX0MSK,
   1676 	    CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
   1677 	    SQE | TX_ABORT | TX_OK);
   1678 	if (!(sc->sc_flags & XIFLAGS_DINGO))
   1679 		/* XXX From Linux, dunno what 0xb0 means. */
   1680 		bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
   1681 	bus_space_write_1(bst, bsh, offset + RXST0, 0);
   1682 	bus_space_write_1(bst, bsh, offset + TXST0, 0);
   1683 	bus_space_write_1(bst, bsh, offset + TXST1, 0);
   1684 
   1685 	/* Enable MII function if available. */
   1686 	if (LIST_FIRST(&sc->sc_mii.mii_phys)) {
   1687 		PAGE(sc, 2);
   1688 		bus_space_write_1(bst, bsh, offset + MSR,
   1689 		    bus_space_read_1(bst, bsh, offset + MSR) | SELECT_MII);
   1690 		DELAY(20000);
   1691 	} else {
   1692 		PAGE(sc, 0);
   1693 
   1694 		/* XXX Do we need to do this? */
   1695 		PAGE(sc, 0x42);
   1696 		bus_space_write_1(bst, bsh, offset + SWC1, SWC1_AUTO_MEDIA);
   1697 		DELAY(50000);
   1698 
   1699 		/* XXX Linux probes the media here. */
   1700 	}
   1701 
   1702 	/* Configure the LED registers. */
   1703 	PAGE(sc, 2);
   1704 
   1705 	/* XXX This is not good for 10base2. */
   1706 	bus_space_write_1(bst, bsh, offset + LED,
   1707 	    LED_TX_ACT << LED1_SHIFT | LED_10MB_LINK << LED0_SHIFT);
   1708 	if (sc->sc_flags & XIFLAGS_DINGO)
   1709 		bus_space_write_1(bst, bsh, offset + LED3,
   1710 		    LED_100MB_LINK << LED3_SHIFT);
   1711 
   1712 	/* Enable receiver and go online. */
   1713 	PAGE(sc, 0x40);
   1714 	bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
   1715 
   1716 #if 0
   1717 	/* XXX Linux does this here - is it necessary? */
   1718 	PAGE(sc, 1);
   1719 	bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
   1720 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1721 		/* XXX What is this?  Not for Dingo at least. */
   1722 		bus_space_write_1(bst, bsh, offset + IMR1, 1);
   1723 	}
   1724 #endif
   1725 
   1726        /* Enable interrupts. */
   1727 	PAGE(sc, 0);
   1728 	bus_space_write_1(bst, bsh, offset + CR, ENABLE_INT);
   1729 
   1730 	/* XXX This is pure magic for me, found in the Linux driver. */
   1731 	if ((sc->sc_flags & (XIFLAGS_DINGO | XIFLAGS_MODEM)) == XIFLAGS_MODEM) {
   1732 		if ((bus_space_read_1(bst, bsh, offset + 0x10) & 0x01) == 0)
   1733 			/* Unmask the master interrupt bit. */
   1734 			bus_space_write_1(bst, bsh, offset + 0x10, 0x11);
   1735 	}
   1736 
   1737 	/*
   1738 	 * The Linux driver says this:
   1739 	 * We should switch back to page 0 to avoid a bug in revision 0
   1740 	 * where regs with offset below 8 can't be read after an access
   1741 	 * to the MAC registers.
   1742 	 */
   1743 	PAGE(sc, 0);
   1744 }
   1745