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