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