Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.25
      1 /*	$NetBSD: if_xi.c,v 1.25 2002/09/30 22:27:01 thorpej 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.25 2002/09/30 22:27:01 thorpej Exp $");
     53 
     54 #include "opt_inet.h"
     55 #include "bpfilter.h"
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/device.h>
     60 #include <sys/ioctl.h>
     61 #include <sys/mbuf.h>
     62 #include <sys/malloc.h>
     63 #include <sys/socket.h>
     64 
     65 #include "rnd.h"
     66 #if NRND > 0
     67 #include <sys/rnd.h>
     68 #endif
     69 
     70 #include <net/if.h>
     71 #include <net/if_dl.h>
     72 #include <net/if_media.h>
     73 #include <net/if_types.h>
     74 #include <net/if_ether.h>
     75 
     76 #ifdef INET
     77 #include <netinet/in.h>
     78 #include <netinet/in_systm.h>
     79 #include <netinet/in_var.h>
     80 #include <netinet/ip.h>
     81 #include <netinet/if_inarp.h>
     82 #endif
     83 
     84 #ifdef IPX
     85 #include <netipx/ipx.h>
     86 #include <netipx/ipx_if.h>
     87 #endif
     88 
     89 #ifdef NS
     90 #include <netns/ns.h>
     91 #include <netns/ns_if.h>
     92 #endif
     93 
     94 #if NBPFILTER > 0
     95 #include <net/bpf.h>
     96 #include <net/bpfdesc.h>
     97 #endif
     98 
     99 #define ETHER_MIN_LEN 64
    100 #define ETHER_CRC_LEN 4
    101 
    102 /*
    103  * Maximum number of bytes to read per interrupt.  Linux recommends
    104  * somewhere between 2000-22000.
    105  * XXX This is currently a hard maximum.
    106  */
    107 #define MAX_BYTES_INTR 12000
    108 
    109 #include <dev/mii/mii.h>
    110 #include <dev/mii/miivar.h>
    111 
    112 #include <dev/pcmcia/pcmciareg.h>
    113 #include <dev/pcmcia/pcmciavar.h>
    114 #include <dev/pcmcia/pcmciadevs.h>
    115 
    116 #include <dev/pcmcia/if_xireg.h>
    117 
    118 #ifdef __GNUC__
    119 #define INLINE	__inline
    120 #else
    121 #define INLINE
    122 #endif	/* __GNUC__ */
    123 
    124 #ifdef XIDEBUG
    125 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
    126 
    127 #define XID_CONFIG	0x1
    128 #define XID_MII		0x2
    129 #define XID_INTR	0x4
    130 #define XID_FIFO	0x8
    131 
    132 #ifdef XIDEBUG_VALUE
    133 int xidebug = XIDEBUG_VALUE;
    134 #else
    135 int xidebug = 0;
    136 #endif
    137 #else
    138 #define DPRINTF(cat, x) (void)0
    139 #endif
    140 
    141 int	xi_pcmcia_match __P((struct device *, struct cfdata *, void *));
    142 void	xi_pcmcia_attach __P((struct device *, struct device *, void *));
    143 int	xi_pcmcia_detach __P((struct device *, int));
    144 int	xi_pcmcia_activate __P((struct device *, enum devact));
    145 
    146 /*
    147  * In case this chipset ever turns up out of pcmcia attachments (very
    148  * unlikely) do the driver splitup.
    149  */
    150 struct xi_softc {
    151 	struct device sc_dev;			/* Generic device info */
    152 	struct ethercom sc_ethercom;		/* Ethernet common part */
    153 
    154 	struct mii_data sc_mii;			/* MII media information */
    155 
    156 	bus_space_tag_t		sc_bst;		/* Bus cookie */
    157 	bus_space_handle_t	sc_bsh;		/* Bus I/O handle */
    158 	bus_size_t		sc_offset;	/* Offset of registers */
    159 
    160 	u_int8_t	sc_rev;			/* Chip revision */
    161 	u_int32_t	sc_flags;		/* Misc. flags */
    162 	int		sc_all_mcasts;		/* Receive all multicasts */
    163 	u_int8_t 	sc_enaddr[ETHER_ADDR_LEN];
    164 #if NRND > 0
    165 	rndsource_element_t	sc_rnd_source;
    166 #endif
    167 };
    168 
    169 struct xi_pcmcia_softc {
    170 	struct	xi_softc sc_xi;			/* Generic device info */
    171 
    172 	/* PCMCIA-specific goo */
    173 	struct	pcmcia_function *sc_pf;		/* PCMCIA function */
    174 	struct	pcmcia_io_handle sc_pcioh;	/* iospace info */
    175 	int	sc_io_window;			/* io window info */
    176 	void	*sc_ih;				/* Interrupt handler */
    177 	void	*sc_powerhook;			/* power hook descriptor */
    178 	int	sc_resource;			/* resource allocated */
    179 #define XI_RES_PCIC	1
    180 #define XI_RES_IO_ALLOC	2
    181 #define XI_RES_IO_MAP	4
    182 #define XI_RES_MI	8
    183 };
    184 
    185 CFATTACH_DECL(xi_pcmcia, sizeof(struct xi_pcmcia_softc),
    186     xi_pcmcia_match, xi_pcmcia_attach, xi_pcmcia_detach, xi_pcmcia_activate)
    187 
    188 static int xi_pcmcia_cis_quirks __P((struct pcmcia_function *));
    189 static void xi_cycle_power __P((struct xi_softc *));
    190 static int xi_ether_ioctl __P((struct ifnet *, u_long cmd, caddr_t));
    191 static void xi_full_reset __P((struct xi_softc *));
    192 static void xi_init __P((struct xi_softc *));
    193 static int xi_intr __P((void *));
    194 static int xi_ioctl __P((struct ifnet *, u_long, caddr_t));
    195 static int xi_mdi_read __P((struct device *, int, int));
    196 static void xi_mdi_write __P((struct device *, int, int, int));
    197 static int xi_mediachange __P((struct ifnet *));
    198 static void xi_mediastatus __P((struct ifnet *, struct ifmediareq *));
    199 static int xi_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    200 static int xi_pcmcia_lan_nid_ciscallback __P((struct pcmcia_tuple *, void *));
    201 static int xi_pcmcia_manfid_ciscallback __P((struct pcmcia_tuple *, void *));
    202 static u_int16_t xi_get __P((struct xi_softc *));
    203 static void xi_reset __P((struct xi_softc *));
    204 static void xi_set_address __P((struct xi_softc *));
    205 static void xi_start __P((struct ifnet *));
    206 static void xi_statchg __P((struct device *));
    207 static void xi_stop __P((struct xi_softc *));
    208 static void xi_watchdog __P((struct ifnet *));
    209 const struct xi_pcmcia_product *xi_pcmcia_identify __P((struct device *,
    210 						struct pcmcia_attach_args *));
    211 static int xi_pcmcia_enable __P((struct xi_pcmcia_softc *));
    212 static void xi_pcmcia_disable __P((struct xi_pcmcia_softc *));
    213 static void xi_pcmcia_power __P((int, void *));
    214 
    215 /* flags */
    216 #define XIFLAGS_MOHAWK	0x001		/* 100Mb capabilities (has phy) */
    217 #define XIFLAGS_DINGO	0x002		/* realport cards ??? */
    218 #define XIFLAGS_MODEM	0x004		/* modem also present */
    219 
    220 const struct xi_pcmcia_product {
    221 	u_int32_t	xpp_vendor;	/* vendor ID */
    222 	u_int32_t	xpp_product;	/* product ID */
    223 	int		xpp_expfunc;	/* expected function number */
    224 	int		xpp_flags;	/* initial softc flags */
    225 	const char	*xpp_name;	/* device name */
    226 } xi_pcmcia_products[] = {
    227 #ifdef NOT_SUPPORTED
    228 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    229 	  0,				0,
    230 	  PCMCIA_STR_XIRCOM_CE },
    231 #endif
    232 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    233 	  0,				0,
    234 	  PCMCIA_STR_XIRCOM_CE2 },
    235 	{ PCMCIA_VENDOR_XIRCOM,		0x0142,
    236 	  0,				0,
    237 	  PCMCIA_STR_XIRCOM_CE2 },
    238 	{ PCMCIA_VENDOR_XIRCOM,		0x0143,
    239 	  0,				XIFLAGS_MOHAWK,
    240 	  PCMCIA_STR_XIRCOM_CE3 },
    241 	{ PCMCIA_VENDOR_COMPAQ2,	0x0143,
    242 	  0,				XIFLAGS_MOHAWK,
    243 	  PCMCIA_STR_COMPAQ2_CPQ_10_100 },
    244 	{ PCMCIA_VENDOR_INTEL,		0x0143,
    245 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM,
    246 	  PCMCIA_STR_INTEL_EEPRO100 },
    247 	{ PCMCIA_VENDOR_XIRCOM,		PCMCIA_PRODUCT_XIRCOM_XE2000,
    248 	  0,				XIFLAGS_MOHAWK,
    249 	  PCMCIA_STR_XIRCOM_XE2000 },
    250 	{ PCMCIA_VENDOR_XIRCOM,		PCMCIA_PRODUCT_XIRCOM_REM56,
    251 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
    252 	  PCMCIA_STR_XIRCOM_REM56 },
    253 #ifdef NOT_SUPPORTED
    254 	{ PCMCIA_VENDOR_XIRCOM,		0x1141,
    255 	  0,				XIFLAGS_MODEM,
    256 	  PCMCIA_STR_XIRCOM_CEM },
    257 #endif
    258 	{ PCMCIA_VENDOR_XIRCOM,		0x1142,
    259 	  0,				XIFLAGS_MODEM,
    260 	  PCMCIA_STR_XIRCOM_CEM },
    261 	{ PCMCIA_VENDOR_XIRCOM,		0x1143,
    262 	  0,				XIFLAGS_MODEM,
    263 	  PCMCIA_STR_XIRCOM_CEM },
    264 	{ PCMCIA_VENDOR_XIRCOM,		0x1144,
    265 	  0,				XIFLAGS_MODEM,
    266 	  PCMCIA_STR_XIRCOM_CEM33 },
    267 	{ PCMCIA_VENDOR_XIRCOM,		0x1145,
    268 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM,
    269 	  PCMCIA_STR_XIRCOM_CEM56 },
    270 	{ PCMCIA_VENDOR_XIRCOM,		0x1146,
    271 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
    272 	  PCMCIA_STR_XIRCOM_REM56 },
    273 	{ PCMCIA_VENDOR_XIRCOM,		0x1147,
    274 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
    275 	  PCMCIA_STR_XIRCOM_REM56 },
    276 	{ 0,				0,
    277 	  0,				0,
    278 	  NULL },
    279 };
    280 
    281 
    282 const struct xi_pcmcia_product *
    283 xi_pcmcia_identify(dev, pa)
    284 	struct device *dev;
    285         struct pcmcia_attach_args *pa;
    286 {
    287 	const struct xi_pcmcia_product *xpp;
    288         u_int8_t id;
    289 	u_int32_t prod;
    290 
    291 	/*
    292 	 * The Xircom ethernet cards swap the revision and product fields
    293 	 * inside the CIS, which makes identification just a little
    294 	 * bit different.
    295 	 */
    296 
    297         pcmcia_scan_cis(dev, xi_pcmcia_manfid_ciscallback, &id);
    298 
    299 	prod = (pa->product & ~0xff) | id;
    300 
    301 	DPRINTF(XID_CONFIG, ("product=0x%x\n", prod));
    302 
    303 	for (xpp = xi_pcmcia_products; xpp->xpp_name != NULL; xpp++)
    304 		if (pa->manufacturer == xpp->xpp_vendor &&
    305 			prod == xpp->xpp_product &&
    306 			pa->pf->number == xpp->xpp_expfunc)
    307 			return (xpp);
    308 	return (NULL);
    309 }
    310 
    311 /*
    312  * The quirks are done here instead of the traditional framework because
    313  * of the difficulty in identifying the devices.
    314  */
    315 static int
    316 xi_pcmcia_cis_quirks(pf)
    317 	struct pcmcia_function *pf;
    318 {
    319 	struct pcmcia_config_entry *cfe;
    320 
    321 	/* Tell the pcmcia framework where the CCR is. */
    322 	pf->ccr_base = 0x800;
    323 	pf->ccr_mask = 0x67;
    324 
    325 	/* Fake a cfe. */
    326 	SIMPLEQ_FIRST(&pf->cfe_head) = cfe = (struct pcmcia_config_entry *)
    327 	    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT|M_ZERO);
    328 
    329 	if (cfe == NULL)
    330 		return -1;
    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, SIMPLEQ_FIRST(&psc->sc_pf->cfe_head));
    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 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0,
    833 	    ~rx_status & 0xff);
    834 	tx_status =
    835 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
    836 	tx_status |=
    837 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1) << 8;
    838 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0,0);
    839 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1,0);
    840 
    841 	PAGE(sc, 0);
    842 	while (esr & FULL_PKT_RCV) {
    843 		if (!(rsr & RSR_RX_OK))
    844 			break;
    845 
    846 		/* Compare bytes read this interrupt to hard maximum. */
    847 		if (recvcount > MAX_BYTES_INTR) {
    848 			DPRINTF(XID_INTR,
    849 			    ("xi: too many bytes this interrupt\n"));
    850 			ifp->if_iqdrops++;
    851 			/* Drop packet. */
    852 			bus_space_write_2(sc->sc_bst, sc->sc_bsh,
    853 			    sc->sc_offset + DO0, DO_SKIP_RX_PKT);
    854 		}
    855 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    856 		recvcount += tempint;
    857 		ifp->if_ibytes += tempint;
    858 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    859 		    sc->sc_offset + ESR);
    860 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    861 		    sc->sc_offset + RSR);
    862 	}
    863 
    864 	/* Packet too long? */
    865 	if (rsr & RSR_TOO_LONG) {
    866 		ifp->if_ierrors++;
    867 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    868 	}
    869 
    870 	/* CRC error? */
    871 	if (rsr & RSR_CRCERR) {
    872 		ifp->if_ierrors++;
    873 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    874 	}
    875 
    876 	/* Alignment error? */
    877 	if (rsr & RSR_ALIGNERR) {
    878 		ifp->if_ierrors++;
    879 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    880 	}
    881 
    882 	/* Check for rx overrun. */
    883 	if (rx_status & RX_OVERRUN) {
    884 		ifp->if_ierrors++;
    885 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    886 		    CLR_RX_OVERRUN);
    887 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    888 	}
    889 
    890 	/* Try to start more packets transmitting. */
    891 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    892 		xi_start(ifp);
    893 
    894 	/* Detected excessive collisions? */
    895 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    896 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    897 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    898 		    RESTART_TX);
    899 		ifp->if_oerrors++;
    900 	}
    901 
    902 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    903 		ifp->if_oerrors++;
    904 
    905 end:
    906 	/* Reenable interrupts. */
    907 	PAGE(sc, savedpage);
    908 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    909 	    ENABLE_INT);
    910 
    911 	/* have handled the interrupt */
    912 #if NRND > 0
    913 	rnd_add_uint32(&sc->sc_rnd_source, tx_status);
    914 #endif
    915 
    916 	return (1);
    917 }
    918 
    919 /*
    920  * Pull a packet from the card into an mbuf chain.
    921  */
    922 static u_int16_t
    923 xi_get(sc)
    924 	struct xi_softc *sc;
    925 {
    926 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    927 	struct mbuf *top, **mp, *m;
    928 	u_int16_t pktlen, len, recvcount = 0;
    929 	u_int8_t *data;
    930 	u_int8_t rsr;
    931 
    932 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    933 
    934 	PAGE(sc, 0);
    935 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    936 
    937 	pktlen =
    938 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
    939 	    RBC_COUNT_MASK;
    940 
    941 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    942 
    943 	if (pktlen == 0) {
    944 		/*
    945 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    946 		 * when MPE is set.  It is not known why.
    947 		 */
    948 		return (0);
    949 	}
    950 
    951 	/* XXX should this be incremented now ? */
    952 	recvcount += pktlen;
    953 
    954 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    955 	if (m == 0)
    956 		return (recvcount);
    957 	m->m_pkthdr.rcvif = ifp;
    958 	m->m_pkthdr.len = pktlen;
    959 	m->m_flags |= M_HASFCS;
    960 	len = MHLEN;
    961 	top = 0;
    962 	mp = &top;
    963 
    964 	while (pktlen > 0) {
    965 		if (top) {
    966 			MGET(m, M_DONTWAIT, MT_DATA);
    967 			if (m == 0) {
    968 				m_freem(top);
    969 				return (recvcount);
    970 			}
    971 			len = MLEN;
    972 		}
    973 		if (pktlen >= MINCLSIZE) {
    974 			MCLGET(m, M_DONTWAIT);
    975 			if (!(m->m_flags & M_EXT)) {
    976 				m_freem(m);
    977 				m_freem(top);
    978 				return (recvcount);
    979 			}
    980 			len = MCLBYTES;
    981 		}
    982 		if (!top) {
    983 			caddr_t newdata = (caddr_t)ALIGN(m->m_data +
    984 			    sizeof(struct ether_header)) -
    985 			    sizeof(struct ether_header);
    986 			len -= newdata - m->m_data;
    987 			m->m_data = newdata;
    988 		}
    989 		len = min(pktlen, len);
    990 		data = mtod(m, u_int8_t *);
    991 		if (len > 1) {
    992 		        len &= ~1;
    993 			bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh,
    994 			    sc->sc_offset + EDP, (u_int16_t *)data, len>>1);
    995 		} else
    996 			*data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    997 			    sc->sc_offset + EDP);
    998 		m->m_len = len;
    999 		pktlen -= len;
   1000 		*mp = m;
   1001 		mp = &m->m_next;
   1002 	}
   1003 
   1004 	/* Skip Rx packet. */
   1005 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
   1006 	    DO_SKIP_RX_PKT);
   1007 
   1008 	ifp->if_ipackets++;
   1009 
   1010 #if NBPFILTER > 0
   1011 	if (ifp->if_bpf)
   1012 		bpf_mtap(ifp->if_bpf, top);
   1013 #endif
   1014 
   1015 	(*ifp->if_input)(ifp, top);
   1016 	return (recvcount);
   1017 }
   1018 
   1019 /*
   1020  * Serial management for the MII.
   1021  * The DELAY's below stem from the fact that the maximum frequency
   1022  * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
   1023  * go much faster than that.
   1024  */
   1025 
   1026 /* Let the MII serial management be idle for one period. */
   1027 static INLINE void xi_mdi_idle __P((struct xi_softc *));
   1028 static INLINE void
   1029 xi_mdi_idle(sc)
   1030 	struct xi_softc *sc;
   1031 {
   1032 	bus_space_tag_t bst = sc->sc_bst;
   1033 	bus_space_handle_t bsh = sc->sc_bsh;
   1034 	bus_addr_t offset = sc->sc_offset;
   1035 
   1036 	/* Drive MDC low... */
   1037 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
   1038 	DELAY(1);
   1039 
   1040 	/* and high again. */
   1041 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
   1042 	DELAY(1);
   1043 }
   1044 
   1045 /* Pulse out one bit of data. */
   1046 static INLINE void xi_mdi_pulse __P((struct xi_softc *, int));
   1047 static INLINE void
   1048 xi_mdi_pulse(sc, data)
   1049 	struct xi_softc *sc;
   1050 	int data;
   1051 {
   1052 	bus_space_tag_t bst = sc->sc_bst;
   1053 	bus_space_handle_t bsh = sc->sc_bsh;
   1054 	bus_addr_t offset = sc->sc_offset;
   1055 	u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
   1056 
   1057 	/* First latch the data bit MDIO with clock bit MDC low...*/
   1058 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
   1059 	DELAY(1);
   1060 
   1061 	/* then raise the clock again, preserving the data bit. */
   1062 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
   1063 	DELAY(1);
   1064 }
   1065 
   1066 /* Probe one bit of data. */
   1067 static INLINE int xi_mdi_probe __P((struct xi_softc *sc));
   1068 static INLINE int
   1069 xi_mdi_probe(sc)
   1070 	struct xi_softc *sc;
   1071 {
   1072 	bus_space_tag_t bst = sc->sc_bst;
   1073 	bus_space_handle_t bsh = sc->sc_bsh;
   1074 	bus_size_t offset = sc->sc_offset;
   1075 	u_int8_t x;
   1076 
   1077 	/* Pull clock bit MDCK low... */
   1078 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
   1079 	DELAY(1);
   1080 
   1081 	/* Read data and drive clock high again. */
   1082 	x = bus_space_read_1(bst, bsh, offset + GP2) & MDIO;
   1083 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
   1084 	DELAY(1);
   1085 
   1086 	return (x);
   1087 }
   1088 
   1089 /* Pulse out a sequence of data bits. */
   1090 static INLINE void xi_mdi_pulse_bits __P((struct xi_softc *, u_int32_t, int));
   1091 static INLINE void
   1092 xi_mdi_pulse_bits(sc, data, len)
   1093 	struct xi_softc *sc;
   1094 	u_int32_t data;
   1095 	int len;
   1096 {
   1097 	u_int32_t mask;
   1098 
   1099 	for (mask = 1 << (len - 1); mask; mask >>= 1)
   1100 		xi_mdi_pulse(sc, data & mask);
   1101 }
   1102 
   1103 /* Read a PHY register. */
   1104 static int
   1105 xi_mdi_read(self, phy, reg)
   1106 	struct device *self;
   1107 	int phy;
   1108 	int reg;
   1109 {
   1110 	struct xi_softc *sc = (struct xi_softc *)self;
   1111 	int i;
   1112 	u_int32_t mask;
   1113 	u_int32_t data = 0;
   1114 
   1115 	PAGE(sc, 2);
   1116 	for (i = 0; i < 32; i++)	/* Synchronize. */
   1117 		xi_mdi_pulse(sc, 1);
   1118 	xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
   1119 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
   1120 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
   1121 	xi_mdi_idle(sc);		/* Turn around. */
   1122 	xi_mdi_probe(sc);		/* Drop initial zero bit. */
   1123 
   1124 	for (mask = 1 << 15; mask; mask >>= 1) {
   1125 		if (xi_mdi_probe(sc))
   1126 			data |= mask;
   1127 	}
   1128 	xi_mdi_idle(sc);
   1129 
   1130 	DPRINTF(XID_MII,
   1131 	    ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
   1132 
   1133 	return (data);
   1134 }
   1135 
   1136 /* Write a PHY register. */
   1137 static void
   1138 xi_mdi_write(self, phy, reg, value)
   1139 	struct device *self;
   1140 	int phy;
   1141 	int reg;
   1142 	int value;
   1143 {
   1144 	struct xi_softc *sc = (struct xi_softc *)self;
   1145 	int i;
   1146 
   1147 	PAGE(sc, 2);
   1148 	for (i = 0; i < 32; i++)	/* Synchronize. */
   1149 		xi_mdi_pulse(sc, 1);
   1150 	xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
   1151 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
   1152 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
   1153 	xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
   1154 	xi_mdi_pulse_bits(sc, value, 16);	/* Write the data */
   1155 	xi_mdi_idle(sc);		/* Idle away. */
   1156 
   1157 	DPRINTF(XID_MII,
   1158 	    ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
   1159 }
   1160 
   1161 static void
   1162 xi_statchg(self)
   1163 	struct device *self;
   1164 {
   1165 	/* XXX Update ifp->if_baudrate */
   1166 }
   1167 
   1168 /*
   1169  * Change media according to request.
   1170  */
   1171 static int
   1172 xi_mediachange(ifp)
   1173 	struct ifnet *ifp;
   1174 {
   1175 	DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
   1176 
   1177 	if (ifp->if_flags & IFF_UP)
   1178 		xi_init(ifp->if_softc);
   1179 	return (0);
   1180 }
   1181 
   1182 /*
   1183  * Notify the world which media we're using.
   1184  */
   1185 static void
   1186 xi_mediastatus(ifp, ifmr)
   1187 	struct ifnet *ifp;
   1188 	struct ifmediareq *ifmr;
   1189 {
   1190 	struct xi_softc *sc = ifp->if_softc;
   1191 
   1192 	DPRINTF(XID_CONFIG, ("xi_mediastatus()\n"));
   1193 
   1194 	mii_pollstat(&sc->sc_mii);
   1195 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1196 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1197 }
   1198 
   1199 static void
   1200 xi_reset(sc)
   1201 	struct xi_softc *sc;
   1202 {
   1203 	int s;
   1204 
   1205 	DPRINTF(XID_CONFIG, ("xi_reset()\n"));
   1206 
   1207 	s = splnet();
   1208 	xi_stop(sc);
   1209 	xi_full_reset(sc);
   1210 	xi_init(sc);
   1211 	splx(s);
   1212 }
   1213 
   1214 static void
   1215 xi_watchdog(ifp)
   1216 	struct ifnet *ifp;
   1217 {
   1218 	struct xi_softc *sc = ifp->if_softc;
   1219 
   1220 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1221 	++ifp->if_oerrors;
   1222 
   1223 	xi_reset(sc);
   1224 }
   1225 
   1226 static void
   1227 xi_stop(sc)
   1228 	register struct xi_softc *sc;
   1229 {
   1230 	DPRINTF(XID_CONFIG, ("xi_stop()\n"));
   1231 
   1232 	/* Disable interrupts. */
   1233 	PAGE(sc, 0);
   1234 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR, 0);
   1235 
   1236 	PAGE(sc, 1);
   1237 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + IMR0, 0);
   1238 
   1239 	/* Power down, wait. */
   1240 	PAGE(sc, 4);
   1241 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + GP1, 0);
   1242 	DELAY(40000);
   1243 
   1244 	/* Cancel watchdog timer. */
   1245 	sc->sc_ethercom.ec_if.if_timer = 0;
   1246 }
   1247 
   1248 static void
   1249 xi_init(sc)
   1250 	struct xi_softc *sc;
   1251 {
   1252 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)sc;
   1253 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1254 	int s;
   1255 
   1256 	DPRINTF(XID_CONFIG, ("xi_init()\n"));
   1257 
   1258 	if ((psc->sc_resource & XI_RES_PCIC) == 0)
   1259 		xi_pcmcia_enable(psc);
   1260 
   1261 	s = splnet();
   1262 
   1263 	xi_set_address(sc);
   1264 
   1265 	/* Set current media. */
   1266 	mii_mediachg(&sc->sc_mii);
   1267 
   1268 	ifp->if_flags |= IFF_RUNNING;
   1269 	ifp->if_flags &= ~IFF_OACTIVE;
   1270 	splx(s);
   1271 }
   1272 
   1273 /*
   1274  * Start outputting on the interface.
   1275  * Always called as splnet().
   1276  */
   1277 static void
   1278 xi_start(ifp)
   1279 	struct ifnet *ifp;
   1280 {
   1281 	struct xi_softc *sc = ifp->if_softc;
   1282 	bus_space_tag_t bst = sc->sc_bst;
   1283 	bus_space_handle_t bsh = sc->sc_bsh;
   1284 	bus_addr_t offset = sc->sc_offset;
   1285 	unsigned int s, len, pad = 0;
   1286 	struct mbuf *m0, *m;
   1287 	u_int16_t space;
   1288 
   1289 	DPRINTF(XID_CONFIG, ("xi_start()\n"));
   1290 
   1291 	/* Don't transmit if interface is busy or not running. */
   1292 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
   1293 		DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
   1294 		return;
   1295 	}
   1296 
   1297 	/* Peek at the next packet. */
   1298 	IFQ_POLL(&ifp->if_snd, m0);
   1299 	if (m0 == 0)
   1300 		return;
   1301 
   1302 	/* We need to use m->m_pkthdr.len, so require the header. */
   1303 	if (!(m0->m_flags & M_PKTHDR))
   1304 		panic("xi_start: no header mbuf");
   1305 
   1306 	len = m0->m_pkthdr.len;
   1307 
   1308 	/* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
   1309 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
   1310 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
   1311 
   1312 	PAGE(sc, 0);
   1313 	space = bus_space_read_2(bst, bsh, offset + TSO0) & 0x7fff;
   1314 	if (len + pad + 2 > space) {
   1315 		DPRINTF(XID_FIFO,
   1316 		    ("xi: not enough space in output FIFO (%d > %d)\n",
   1317 		    len + pad + 2, space));
   1318 		return;
   1319 	}
   1320 
   1321 	IFQ_DEQUEUE(&ifp->if_snd, m0);
   1322 
   1323 #if NBPFILTER > 0
   1324 	if (ifp->if_bpf)
   1325 		bpf_mtap(ifp->if_bpf, m0);
   1326 #endif
   1327 
   1328 	/*
   1329 	 * Do the output at splhigh() so that an interrupt from another device
   1330 	 * won't cause a FIFO underrun.
   1331 	 */
   1332 	s = splhigh();
   1333 
   1334 	bus_space_write_2(bst, bsh, offset + TSO2, (u_int16_t)len + pad + 2);
   1335 	bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
   1336 	for (m = m0; m; ) {
   1337 		if (m->m_len > 1)
   1338 			bus_space_write_multi_2(bst, bsh, offset + EDP,
   1339 			    mtod(m, u_int16_t *), m->m_len>>1);
   1340 		if (m->m_len & 1)
   1341 			bus_space_write_1(bst, bsh, offset + EDP,
   1342 			    *(mtod(m, u_int8_t *) + m->m_len - 1));
   1343 		MFREE(m, m0);
   1344 		m = m0;
   1345 	}
   1346 	if (sc->sc_flags & XIFLAGS_MOHAWK)
   1347 		bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
   1348 	else {
   1349 		for (; pad > 1; pad -= 2)
   1350 			bus_space_write_2(bst, bsh, offset + EDP, 0);
   1351 		if (pad == 1)
   1352 			bus_space_write_1(bst, bsh, offset + EDP, 0);
   1353 	}
   1354 
   1355 	splx(s);
   1356 
   1357 	ifp->if_timer = 5;
   1358 	++ifp->if_opackets;
   1359 }
   1360 
   1361 static int
   1362 xi_ether_ioctl(ifp, cmd, data)
   1363 	struct ifnet *ifp;
   1364 	u_long cmd;
   1365 	caddr_t data;
   1366 {
   1367 	struct ifaddr *ifa = (struct ifaddr *)data;
   1368 	struct xi_softc *sc = ifp->if_softc;
   1369 
   1370 
   1371 	DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
   1372 
   1373 	switch (cmd) {
   1374 	case SIOCSIFADDR:
   1375 		ifp->if_flags |= IFF_UP;
   1376 
   1377 		switch (ifa->ifa_addr->sa_family) {
   1378 #ifdef INET
   1379 		case AF_INET:
   1380 			xi_init(sc);
   1381 			arp_ifinit(ifp, ifa);
   1382 			break;
   1383 #endif	/* INET */
   1384 
   1385 #ifdef NS
   1386 		case AF_NS:
   1387 		{
   1388 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1389 
   1390 			if (ns_nullhost(*ina))
   1391 				ina->x_host = *(union ns_host *)
   1392 					LLADDR(ifp->if_sadl);
   1393 			else
   1394 				memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
   1395 					ifp->if_addrlen);
   1396 			/* Set new address. */
   1397 			xi_init(sc);
   1398 			break;
   1399 		}
   1400 #endif  /* NS */
   1401 
   1402 		default:
   1403 			xi_init(sc);
   1404 			break;
   1405 		}
   1406 		break;
   1407 
   1408 	default:
   1409 		return (EINVAL);
   1410 	}
   1411 
   1412 	return (0);
   1413 }
   1414 
   1415 static int
   1416 xi_ioctl(ifp, command, data)
   1417 	struct ifnet *ifp;
   1418 	u_long command;
   1419 	caddr_t data;
   1420 {
   1421 	struct xi_pcmcia_softc *psc = ifp->if_softc;
   1422 	struct xi_softc *sc = &psc->sc_xi;
   1423 	struct ifreq *ifr = (struct ifreq *)data;
   1424 	int s, error = 0;
   1425 
   1426 	DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
   1427 
   1428 	s = splnet();
   1429 
   1430 	switch (command) {
   1431 	case SIOCSIFADDR:
   1432 		error = xi_ether_ioctl(ifp, command, data);
   1433 		break;
   1434 
   1435 	case SIOCSIFFLAGS:
   1436 		sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1437 
   1438 		PAGE(sc, 0x42);
   1439 		if ((ifp->if_flags & IFF_PROMISC) ||
   1440 		    (ifp->if_flags & IFF_ALLMULTI))
   1441 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1442 			    sc->sc_offset + SWC1,
   1443 			    SWC1_PROMISC | SWC1_MCAST_PROM);
   1444 		else
   1445 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1446 			    sc->sc_offset + SWC1, 0);
   1447 
   1448 		/*
   1449 		 * If interface is marked up and not running, then start it.
   1450 		 * If it is marked down and running, stop it.
   1451 		 * XXX If it's up then re-initialize it. This is so flags
   1452 		 * such as IFF_PROMISC are handled.
   1453 		 */
   1454 		if (ifp->if_flags & IFF_UP) {
   1455 			xi_init(sc);
   1456 		} else {
   1457 			if (ifp->if_flags & IFF_RUNNING) {
   1458 				xi_pcmcia_disable(psc);
   1459 				xi_stop(sc);
   1460 				ifp->if_flags &= ~IFF_RUNNING;
   1461 			}
   1462 		}
   1463 		break;
   1464 
   1465 	case SIOCADDMULTI:
   1466 	case SIOCDELMULTI:
   1467 		sc->sc_all_mcasts = (ifp->if_flags & IFF_ALLMULTI) ? 1 : 0;
   1468 		error = (command == SIOCADDMULTI) ?
   1469 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1470 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1471 
   1472 		if (error == ENETRESET) {
   1473 			/*
   1474 			 * Multicast list has changed; set the hardware
   1475 			 * filter accordingly.
   1476 			 */
   1477 			if (!sc->sc_all_mcasts &&
   1478 			    !(ifp->if_flags & IFF_PROMISC))
   1479 				xi_set_address(sc);
   1480 
   1481 			/*
   1482 			 * xi_set_address() can turn on all_mcasts if we run
   1483 			 * out of space, so check it again rather than else {}.
   1484 			 */
   1485 			if (sc->sc_all_mcasts)
   1486 				xi_init(sc);
   1487 			error = 0;
   1488 		}
   1489 		break;
   1490 
   1491 	case SIOCSIFMEDIA:
   1492 	case SIOCGIFMEDIA:
   1493 		error =
   1494 		    ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, command);
   1495 		break;
   1496 
   1497 	default:
   1498 		error = EINVAL;
   1499 	}
   1500 	splx(s);
   1501 	return (error);
   1502 }
   1503 
   1504 static void
   1505 xi_set_address(sc)
   1506 	struct xi_softc *sc;
   1507 {
   1508 	bus_space_tag_t bst = sc->sc_bst;
   1509 	bus_space_handle_t bsh = sc->sc_bsh;
   1510 	bus_addr_t offset = sc->sc_offset;
   1511 	struct ethercom *ether = &sc->sc_ethercom;
   1512 #if 0
   1513 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1514 #endif
   1515 #if WORKING_MULTICAST
   1516 	struct ether_multistep step;
   1517 	struct ether_multi *enm;
   1518 	int page, pos, num;
   1519 #endif
   1520 	int i;
   1521 
   1522 	DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
   1523 
   1524 	PAGE(sc, 0x50);
   1525 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1526 		bus_space_write_1(bst, bsh, offset + IA + i,
   1527 		    sc->sc_enaddr[(sc->sc_flags & XIFLAGS_MOHAWK) ?  5-i : i]);
   1528 	}
   1529 
   1530 	if (ether->ec_multicnt > 0) {
   1531 #ifdef WORKING_MULTICAST
   1532 		if (ether->ec_multicnt > 9) {
   1533 #else
   1534 		{
   1535 #endif
   1536 			PAGE(sc, 0x42);
   1537 			bus_space_write_1(sc->sc_bst, sc->sc_bsh,
   1538 			    sc->sc_offset + SWC1,
   1539 			    SWC1_PROMISC | SWC1_MCAST_PROM);
   1540 			return;
   1541 		}
   1542 
   1543 #ifdef WORKING_MULTICAST
   1544 
   1545 		ETHER_FIRST_MULTI(step, ether, enm);
   1546 
   1547 		pos = IA + 6;
   1548 		for (page = 0x50, num = ether->ec_multicnt; num > 0 && enm;
   1549 		    num--) {
   1550 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1551 			    sizeof(enm->enm_addrlo)) != 0) {
   1552 				/*
   1553 				 * The multicast address is really a range;
   1554 				 * it's easier just to accept all multicasts.
   1555 				 * XXX should we be setting IFF_ALLMULTI here?
   1556 				 */
   1557 #if 0
   1558 				ifp->if_flags |= IFF_ALLMULTI;
   1559 #endif
   1560 				sc->sc_all_mcasts=1;
   1561 				break;
   1562 			}
   1563 
   1564 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
   1565 				printf("%x:", enm->enm_addrlo[i]);
   1566 				bus_space_write_1(bst, bsh, offset + pos,
   1567 				    enm->enm_addrlo[
   1568 				    (sc->sc_flags & XIFLAGS_MOHAWK) ? 5-i : i]);
   1569 
   1570 				if (++pos > 15) {
   1571 					pos = IA;
   1572 					page++;
   1573 					PAGE(sc, page);
   1574 				}
   1575 			}
   1576 			printf("\n");
   1577 			ETHER_NEXT_MULTI(step, enm);
   1578 		}
   1579 #endif
   1580 	}
   1581 }
   1582 
   1583 static void
   1584 xi_cycle_power(sc)
   1585 	struct xi_softc *sc;
   1586 {
   1587 	bus_space_tag_t bst = sc->sc_bst;
   1588 	bus_space_handle_t bsh = sc->sc_bsh;
   1589 	bus_addr_t offset = sc->sc_offset;
   1590 
   1591 	DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
   1592 
   1593 	PAGE(sc, 4);
   1594 	DELAY(1);
   1595 	bus_space_write_1(bst, bsh, offset + GP1, 0);
   1596 	DELAY(40000);
   1597 	if (sc->sc_flags & XIFLAGS_MOHAWK)
   1598 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
   1599 	else
   1600 		/* XXX What is bit 2 (aka AIC)? */
   1601 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
   1602 	DELAY(20000);
   1603 }
   1604 
   1605 static void
   1606 xi_full_reset(sc)
   1607 	struct xi_softc *sc;
   1608 {
   1609 	bus_space_tag_t bst = sc->sc_bst;
   1610 	bus_space_handle_t bsh = sc->sc_bsh;
   1611 	bus_addr_t offset = sc->sc_offset;
   1612 
   1613 	DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
   1614 
   1615 	/* Do an as extensive reset as possible on all functions. */
   1616 	xi_cycle_power(sc);
   1617 	bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
   1618 	DELAY(20000);
   1619 	bus_space_write_1(bst, bsh, offset + CR, 0);
   1620 	DELAY(20000);
   1621 	if (sc->sc_flags & XIFLAGS_MOHAWK) {
   1622 		PAGE(sc, 4);
   1623 		/*
   1624 		 * Drive GP1 low to power up ML6692 and GP2 high to power up
   1625 		 * the 10Mhz chip.  XXX What chip is that?  The phy?
   1626 		 */
   1627 		bus_space_write_1(bst, bsh, offset + GP0,
   1628 		    GP1_OUT | GP2_OUT | GP2_WR);
   1629 	}
   1630 	DELAY(500000);
   1631 
   1632 	/* Get revision information.  XXX Symbolic constants. */
   1633 	sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
   1634 	    ((sc->sc_flags & XIFLAGS_MOHAWK) ? 0x70 : 0x30) >> 4;
   1635 
   1636 	/* Media selection.  XXX Maybe manual overriding too? */
   1637 	if (!(sc->sc_flags & XIFLAGS_MOHAWK)) {
   1638 		PAGE(sc, 4);
   1639 		/*
   1640 		 * XXX I have no idea what this really does, it is from the
   1641 		 * Linux driver.
   1642 		 */
   1643 		bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
   1644 	}
   1645 	DELAY(40000);
   1646 
   1647 	/* Setup the ethernet interrupt mask. */
   1648 	PAGE(sc, 1);
   1649 #if 1
   1650 	bus_space_write_1(bst, bsh, offset + IMR0,
   1651 	    ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
   1652 	    ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
   1653 #else
   1654 	bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
   1655 #endif
   1656 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1657 		/* XXX What is this?  Not for Dingo at least. */
   1658 		/* Unmask TX underrun detection */
   1659 		bus_space_write_1(bst, bsh, offset + IMR1, 1);
   1660 	}
   1661 
   1662 	/*
   1663 	 * Disable source insertion.
   1664 	 * XXX Dingo does not have this bit, but Linux does it unconditionally.
   1665 	 */
   1666 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1667 		PAGE(sc, 0x42);
   1668 		bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
   1669 	}
   1670 
   1671 	/* Set the local memory dividing line. */
   1672 	if (sc->sc_rev != 1) {
   1673 		PAGE(sc, 2);
   1674 		/* XXX Symbolic constant preferrable. */
   1675 		bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
   1676 	}
   1677 
   1678 	xi_set_address(sc);
   1679 
   1680 	/*
   1681 	 * Apparently the receive byte pointer can be bad after a reset, so
   1682 	 * we hardwire it correctly.
   1683 	 */
   1684 	PAGE(sc, 0);
   1685 	bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
   1686 
   1687 	/* Setup ethernet MAC registers. XXX Symbolic constants. */
   1688 	PAGE(sc, 0x40);
   1689 	bus_space_write_1(bst, bsh, offset + RX0MSK,
   1690 	    PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
   1691 	bus_space_write_1(bst, bsh, offset + TX0MSK,
   1692 	    CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
   1693 	    SQE | TX_ABORT | TX_OK);
   1694 	if (!(sc->sc_flags & XIFLAGS_DINGO))
   1695 		/* XXX From Linux, dunno what 0xb0 means. */
   1696 		bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
   1697 	bus_space_write_1(bst, bsh, offset + RXST0, 0);
   1698 	bus_space_write_1(bst, bsh, offset + TXST0, 0);
   1699 	bus_space_write_1(bst, bsh, offset + TXST1, 0);
   1700 
   1701 	/* Enable MII function if available. */
   1702 	if (LIST_FIRST(&sc->sc_mii.mii_phys)) {
   1703 		PAGE(sc, 2);
   1704 		bus_space_write_1(bst, bsh, offset + MSR,
   1705 		    bus_space_read_1(bst, bsh, offset + MSR) | SELECT_MII);
   1706 		DELAY(20000);
   1707 	} else {
   1708 		PAGE(sc, 0);
   1709 
   1710 		/* XXX Do we need to do this? */
   1711 		PAGE(sc, 0x42);
   1712 		bus_space_write_1(bst, bsh, offset + SWC1, SWC1_AUTO_MEDIA);
   1713 		DELAY(50000);
   1714 
   1715 		/* XXX Linux probes the media here. */
   1716 	}
   1717 
   1718 	/* Configure the LED registers. */
   1719 	PAGE(sc, 2);
   1720 
   1721 	/* XXX This is not good for 10base2. */
   1722 	bus_space_write_1(bst, bsh, offset + LED,
   1723 	    LED_TX_ACT << LED1_SHIFT | LED_10MB_LINK << LED0_SHIFT);
   1724 	if (sc->sc_flags & XIFLAGS_DINGO)
   1725 		bus_space_write_1(bst, bsh, offset + LED3,
   1726 		    LED_100MB_LINK << LED3_SHIFT);
   1727 
   1728 	/* Enable receiver and go online. */
   1729 	PAGE(sc, 0x40);
   1730 	bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
   1731 
   1732 #if 0
   1733 	/* XXX Linux does this here - is it necessary? */
   1734 	PAGE(sc, 1);
   1735 	bus_space_write_1(bst, bsh, offset + IMR0, 0xff);
   1736 	if (!(sc->sc_flags & XIFLAGS_DINGO)) {
   1737 		/* XXX What is this?  Not for Dingo at least. */
   1738 		bus_space_write_1(bst, bsh, offset + IMR1, 1);
   1739 	}
   1740 #endif
   1741 
   1742        /* Enable interrupts. */
   1743 	PAGE(sc, 0);
   1744 	bus_space_write_1(bst, bsh, offset + CR, ENABLE_INT);
   1745 
   1746 	/* XXX This is pure magic for me, found in the Linux driver. */
   1747 	if ((sc->sc_flags & (XIFLAGS_DINGO | XIFLAGS_MODEM)) == XIFLAGS_MODEM) {
   1748 		if ((bus_space_read_1(bst, bsh, offset + 0x10) & 0x01) == 0)
   1749 			/* Unmask the master interrupt bit. */
   1750 			bus_space_write_1(bst, bsh, offset + 0x10, 0x11);
   1751 	}
   1752 
   1753 	/*
   1754 	 * The Linux driver says this:
   1755 	 * We should switch back to page 0 to avoid a bug in revision 0
   1756 	 * where regs with offset below 8 can't be read after an access
   1757 	 * to the MAC registers.
   1758 	 */
   1759 	PAGE(sc, 0);
   1760 }
   1761