Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.4
      1 /*	$NetBSD: if_xi.c,v 1.4 2000/07/31 21:49:47 gmcgarry Exp $	*/
      2 /*	OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp 	*/
      3 
      4 /*
      5  * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Niklas Hallqvist,
     19  *	Brandon Creighton and Job de Haas.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
     37  */
     38 
     39 /*
     40  * Known Bugs:
     41  *
     42  * 1) Promiscuous mode doesn't work on at least the CE2.
     43  * 2) Slow. ~450KB/s.  Memory access would be better.
     44  */
     45 
     46 #include "opt_inet.h"
     47 #include "bpfilter.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/ioctl.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/malloc.h>
     55 #include <sys/socket.h>
     56 
     57 #include <net/if.h>
     58 #include <net/if_dl.h>
     59 #include <net/if_media.h>
     60 #include <net/if_types.h>
     61 #include <net/if_ether.h>
     62 
     63 #ifdef INET
     64 #include <netinet/in.h>
     65 #include <netinet/in_systm.h>
     66 #include <netinet/in_var.h>
     67 #include <netinet/ip.h>
     68 #include <netinet/if_inarp.h>
     69 #endif
     70 
     71 #ifdef IPX
     72 #include <netipx/ipx.h>
     73 #include <netipx/ipx_if.h>
     74 #endif
     75 
     76 #ifdef NS
     77 #include <netns/ns.h>
     78 #include <netns/ns_if.h>
     79 #endif
     80 
     81 #if NBPFILTER > 0
     82 #include <net/bpf.h>
     83 #include <net/bpfdesc.h>
     84 #endif
     85 
     86 #define ETHER_MIN_LEN 64
     87 #define ETHER_CRC_LEN 4
     88 
     89 /*
     90  * Maximum number of bytes to read per interrupt.  Linux recommends
     91  * somewhere between 2000-22000.
     92  * XXX This is currently a hard maximum.
     93  */
     94 #define MAX_BYTES_INTR 12000
     95 
     96 #include <dev/mii/mii.h>
     97 #include <dev/mii/miivar.h>
     98 
     99 #include <dev/pcmcia/pcmciareg.h>
    100 #include <dev/pcmcia/pcmciavar.h>
    101 #include <dev/pcmcia/pcmciadevs.h>
    102 
    103 #define XI_IOSIZ	16
    104 
    105 #include <dev/pcmcia/if_xireg.h>
    106 
    107 #ifdef __GNUC__
    108 #define INLINE	__inline
    109 #else
    110 #define INLINE
    111 #endif	/* __GNUC__ */
    112 
    113 #ifdef XIDEBUG
    114 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
    115 
    116 #define XID_CONFIG	0x1
    117 #define XID_MII		0x2
    118 #define XID_INTR	0x4
    119 #define XID_FIFO	0x8
    120 
    121 #ifdef XIDEBUG_VALUE
    122 int xidebug = XIDEBUG_VALUE;
    123 #else
    124 int xidebug = 0;
    125 #endif
    126 #else
    127 #define DPRINTF(cat, x) (void)0
    128 #endif
    129 
    130 int	xi_pcmcia_match __P((struct device *, struct cfdata *, void *));
    131 void	xi_pcmcia_attach __P((struct device *, struct device *, void *));
    132 int	xi_pcmcia_detach __P((struct device *, int));
    133 int	xi_pcmcia_activate __P((struct device *, enum devact));
    134 
    135 /*
    136  * In case this chipset ever turns up out of pcmcia attachments (very
    137  * unlikely) do the driver splitup.
    138  */
    139 struct xi_softc {
    140 	struct device sc_dev;			/* Generic device info */
    141 	struct ethercom sc_ethercom;		/* Ethernet common part */
    142 
    143 	struct mii_data sc_mii;			/* MII media information */
    144 
    145 	bus_space_tag_t		sc_bst;		/* Bus cookie */
    146 	bus_space_handle_t	sc_bsh;		/* Bus I/O handle */
    147 	bus_addr_t		sc_offset;	/* Offset of registers */
    148 
    149 	u_int8_t	sc_rev;			/* Chip revision */
    150 	u_int32_t	sc_flags;		/* Misc. flags */
    151 	int		sc_all_mcasts;		/* Receive all multicasts */
    152 	u_int8_t 	sc_enaddr[ETHER_ADDR_LEN];
    153 };
    154 
    155 struct xi_pcmcia_softc {
    156 	struct	xi_softc sc_xi;			/* Generic device info */
    157 
    158 	/* PCMCIA-specific goo */
    159 	struct	pcmcia_function *sc_pf;		/* PCMCIA function */
    160 	struct	pcmcia_io_handle sc_pcioh;	/* iospace info */
    161 	int	sc_io_window;			/* io window info */
    162 	void	*sc_ih;				/* Interrupt handler */
    163 
    164 	int	sc_resource;			/* resource allocated */
    165 #define XI_RES_PCIC	1
    166 #define XI_RES_IO	2
    167 #define XI_RES_MI	8
    168 };
    169 
    170 struct cfattach xi_pcmcia_ca = {
    171 	sizeof(struct xi_pcmcia_softc), xi_pcmcia_match, xi_pcmcia_attach,
    172 	xi_pcmcia_detach, xi_pcmcia_activate
    173 };
    174 
    175 static int xi_pcmcia_cis_quirks __P((struct pcmcia_function *));
    176 static void xi_cycle_power __P((struct xi_softc *));
    177 static int xi_ether_ioctl __P((struct ifnet *, u_long cmd, caddr_t));
    178 static void xi_full_reset __P((struct xi_softc *));
    179 static void xi_init __P((struct xi_softc *));
    180 static int xi_intr __P((void *));
    181 static int xi_ioctl __P((struct ifnet *, u_long, caddr_t));
    182 static int xi_mdi_read __P((struct device *, int, int));
    183 static void xi_mdi_write __P((struct device *, int, int, int));
    184 static int xi_mediachange __P((struct ifnet *));
    185 static void xi_mediastatus __P((struct ifnet *, struct ifmediareq *));
    186 static int xi_pcmcia_funce_enaddr __P((struct device *, u_int8_t *));
    187 static int xi_pcmcia_lan_nid_ciscallback __P((struct pcmcia_tuple *, void *));
    188 static int xi_pcmcia_manfid_ciscallback __P((struct pcmcia_tuple *, void *));
    189 static u_int16_t xi_get __P((struct xi_softc *));
    190 static void xi_reset __P((struct xi_softc *));
    191 static void xi_set_address __P((struct xi_softc *));
    192 static void xi_start __P((struct ifnet *));
    193 static void xi_statchg __P((struct device *));
    194 static void xi_stop __P((struct xi_softc *));
    195 static void xi_watchdog __P((struct ifnet *));
    196 struct xi_pcmcia_product *xi_pcmcia_identify __P((struct device *,
    197 						struct pcmcia_attach_args *));
    198 
    199 /* flags */
    200 #define XIFLAGS_MOHAWK	0x001		/* 100Mb capabilities (has phy) */
    201 #define XIFLAGS_DINGO	0x002		/* realport cards ??? */
    202 #define XIFLAGS_MODEM	0x004		/* modem also present */
    203 
    204 struct xi_pcmcia_product {
    205 	u_int32_t	xpp_vendor;	/* vendor ID */
    206 	u_int32_t	xpp_product;	/* product ID */
    207 	int		xpp_expfunc;	/* expected function number */
    208 	int		xpp_flags;	/* initial softc flags */
    209 	const char	*xpp_name;	/* device name */
    210 } xi_pcmcia_products[] = {
    211 #ifdef NOT_SUPPORTED
    212 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    213 	  0,				0,
    214 	  PCMCIA_STR_XIRCOM_CE },
    215 #endif
    216 	{ PCMCIA_VENDOR_XIRCOM,		0x0141,
    217 	  0,				0,
    218 	  PCMCIA_STR_XIRCOM_CE2 },
    219 	{ PCMCIA_VENDOR_XIRCOM,		0x0142,
    220 	  0,				0,
    221 	  PCMCIA_STR_XIRCOM_CE2 },
    222 	{ PCMCIA_VENDOR_XIRCOM,		0x0143,
    223 	  0,				XIFLAGS_MOHAWK,
    224 	  PCMCIA_STR_XIRCOM_CE3 },
    225 	{ PCMCIA_VENDOR_COMPAQ2,	0x0143,
    226 	  0,				XIFLAGS_MOHAWK,
    227 	  PCMCIA_STR_COMPAQ2_CPQ_10_100 },
    228 	{ PCMCIA_VENDOR_INTEL,		0x0143,
    229 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM,
    230 	  PCMCIA_STR_INTEL_EEPRO100 },
    231 #ifdef NOT_SUPPORTED
    232 	{ PCMCIA_VENDOR_XIRCOM,		0x1141,
    233 	  0,				XIFLAGS_MODEM,
    234 	  PCMCIA_STR_XIRCOM_CEM },
    235 #endif
    236 	{ PCMCIA_VENDOR_XIRCOM,		0x1142,
    237 	  0,				XIFLAGS_MODEM,
    238 	  PCMCIA_STR_XIRCOM_CEM },
    239 	{ PCMCIA_VENDOR_XIRCOM,		0x1143,
    240 	  0,				XIFLAGS_MODEM,
    241 	  PCMCIA_STR_XIRCOM_CEM },
    242 	{ PCMCIA_VENDOR_XIRCOM,		0x1144,
    243 	  0,				XIFLAGS_MODEM,
    244 	  PCMCIA_STR_XIRCOM_CEM33 },
    245 	{ PCMCIA_VENDOR_XIRCOM,		0x1145,
    246 	  0,				XIFLAGS_MOHAWK | XIFLAGS_MODEM,
    247 	  PCMCIA_STR_XIRCOM_CEM56 },
    248 	{ PCMCIA_VENDOR_XIRCOM,		0x1146,
    249 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
    250 	  PCMCIA_STR_XIRCOM_REM56 },
    251 	{ PCMCIA_VENDOR_XIRCOM,		0x1147,
    252 	  0,				XIFLAGS_MOHAWK | XIFLAGS_DINGO | XIFLAGS_MODEM,
    253 	  PCMCIA_STR_XIRCOM_REM56 },
    254 	{ 0,				0,
    255 	  0,				0,
    256 	  NULL },
    257 };
    258 
    259 
    260 struct xi_pcmcia_product *
    261 xi_pcmcia_identify(dev, pa)
    262 	struct device *dev;
    263         struct pcmcia_attach_args *pa;
    264 {
    265 	struct xi_pcmcia_product *xpp;
    266         u_int8_t id;
    267 	u_int32_t prod;
    268 
    269 	/*
    270 	 * The Xircom ethernet cards swap the revision and product fields
    271 	 * inside the CIS, which makes identification just a little
    272 	 * bit different.
    273 	 */
    274 
    275         pcmcia_scan_cis(dev, xi_pcmcia_manfid_ciscallback, &id);
    276 
    277 	prod = (pa->product & ~0xff) | id;
    278 
    279 	DPRINTF(XID_CONFIG, ("product=0x%x\n", prod));
    280 
    281 	for (xpp = xi_pcmcia_products; xpp->xpp_name != NULL; xpp++)
    282 		if (pa->manufacturer == xpp->xpp_vendor &&
    283 			prod == xpp->xpp_product &&
    284 			pa->pf->number == xpp->xpp_expfunc)
    285 			return (xpp);
    286 	return (NULL);
    287 }
    288 
    289 /*
    290  * If someone can determine which manufacturers/products require cis_quirks,
    291  * then the proper infrastucture can be used.  Until then...
    292  * This also becomes a pain with detaching.
    293  */
    294 static int
    295 xi_pcmcia_cis_quirks(pf)
    296 	struct pcmcia_function *pf;
    297 {
    298 	struct pcmcia_config_entry *cfe;
    299 
    300 	/* Tell the pcmcia framework where the CCR is. */
    301 	pf->ccr_base = 0x800;
    302 	pf->ccr_mask = 0x67;
    303 
    304 	/* Fake a cfe. */
    305 	SIMPLEQ_FIRST(&pf->cfe_head) = cfe = (struct pcmcia_config_entry *)
    306 	    malloc(sizeof(*cfe), M_DEVBUF, M_NOWAIT);
    307 
    308 	if (cfe == NULL)
    309 		return -1;
    310 	bzero(cfe, sizeof(*cfe));
    311 
    312 	/*
    313 	 * XXX Use preprocessor symbols instead.
    314 	 * Enable ethernet & its interrupts, wiring them to -INT
    315 	 * No I/O base.
    316 	 */
    317 	cfe->number = 0x5;
    318 	cfe->flags = 0;		/* XXX Check! */
    319 	cfe->iftype = PCMCIA_IFTYPE_IO;
    320 	cfe->num_iospace = 0;
    321 	cfe->num_memspace = 0;
    322 	cfe->irqmask = 0x8eb0;
    323 
    324 	return 0;
    325 }
    326 
    327 int
    328 xi_pcmcia_match(parent, match, aux)
    329 	struct device *parent;
    330 	struct cfdata *match;
    331 	void *aux;
    332 {
    333 	struct pcmcia_attach_args *pa = aux;
    334 
    335 	if (pa->pf->function != PCMCIA_FUNCTION_NETWORK)
    336 		return (0);
    337 
    338 	if (pa->manufacturer == PCMCIA_VENDOR_COMPAQ2 &&
    339 	    pa->product == PCMCIA_PRODUCT_COMPAQ2_CPQ_10_100)
    340 		return (1);
    341 
    342 	if (pa->manufacturer == PCMCIA_VENDOR_INTEL &&
    343 	   pa->product == PCMCIA_PRODUCT_INTEL_EEPRO100)
    344 		return (1);
    345 
    346 	if (pa->manufacturer == PCMCIA_VENDOR_XIRCOM &&
    347 	    ((pa->product >> 8) == XIMEDIA_ETHER ||
    348 	    (pa->product >> 8) == (XIMEDIA_ETHER | XIMEDIA_MODEM)))
    349 		return (1);
    350 
    351 	return (0);
    352 }
    353 
    354 void
    355 xi_pcmcia_attach(parent, self, aux)
    356 	struct device *parent, *self;
    357 	void *aux;
    358 {
    359 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    360 	struct xi_softc *sc = &psc->sc_xi;
    361 	struct pcmcia_attach_args *pa = aux;
    362 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    363 	struct xi_pcmcia_product *xpp;
    364 
    365 	if (xi_pcmcia_cis_quirks(pa->pf) < 0) {
    366 		printf(": function enable failed\n");
    367 		return;
    368 	}
    369 
    370 	/* Enable the card */
    371 	psc->sc_pf = pa->pf;
    372 	pcmcia_function_init(psc->sc_pf, psc->sc_pf->cfe_head.sqh_first);
    373 	if (pcmcia_function_enable(psc->sc_pf)) {
    374 		printf(": function enable failed\n");
    375 		goto fail;
    376 	}
    377 	psc->sc_resource |= XI_RES_PCIC;
    378 
    379 	/* allocate/map ISA I/O space */
    380 	if (pcmcia_io_alloc(psc->sc_pf, 0, XI_IOSIZ, XI_IOSIZ,
    381 		&psc->sc_pcioh) != 0) {
    382 		printf(": i/o allocation failed\n");
    383 		goto fail;
    384 	}
    385 	if (pcmcia_io_map(psc->sc_pf, PCMCIA_WIDTH_IO16, 0, XI_IOSIZ,
    386 		&psc->sc_pcioh, &psc->sc_io_window)) {
    387 		printf(": can't map i/o space\n");
    388 		goto fail;
    389 	}
    390 	sc->sc_bst = psc->sc_pcioh.iot;
    391 	sc->sc_bsh = psc->sc_pcioh.ioh;
    392 	sc->sc_offset = 0;
    393 	psc->sc_resource |= XI_RES_IO;
    394 
    395 	xpp = xi_pcmcia_identify(parent,pa);
    396 	if (xpp == NULL) {
    397 		printf(": unrecognised model\n");
    398 		return;
    399 	}
    400 	sc->sc_flags = xpp->xpp_flags;
    401 
    402 	printf(": %s\n", xpp->xpp_name);
    403 
    404 	/*
    405 	 * Configuration as adviced by DINGO documentation.
    406 	 * Dingo has some extra configuration registers in the CCR space.
    407 	 */
    408 	if (sc->sc_flags & XIFLAGS_DINGO) {
    409 		struct pcmcia_mem_handle pcmh;
    410 		int ccr_window;
    411 		bus_addr_t ccr_offset;
    412 
    413 		if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
    414 			&pcmh)) {
    415 			DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
    416 			goto fail;
    417 		}
    418 
    419 		if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
    420 			psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
    421 			&pcmh, &ccr_offset, &ccr_window)) {
    422 			DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
    423 			pcmcia_mem_free(psc->sc_pf, &pcmh);
    424 			goto fail;
    425 		}
    426 
    427 		bus_space_write_1(pcmh.memt, pcmh.memh,
    428 		    ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
    429 		bus_space_write_1(pcmh.memt, pcmh.memh,
    430 		    ccr_offset + PCMCIA_CCR_DCOR1,
    431 		    PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
    432 		bus_space_write_1(pcmh.memt, pcmh.memh,
    433 		    ccr_offset + PCMCIA_CCR_DCOR2, 0);
    434 		bus_space_write_1(pcmh.memt, pcmh.memh,
    435 		    ccr_offset + PCMCIA_CCR_DCOR3, 0);
    436 		bus_space_write_1(pcmh.memt, pcmh.memh,
    437 		    ccr_offset + PCMCIA_CCR_DCOR4, 0);
    438 
    439 		/* We don't need them anymore and can free them (I think). */
    440 		pcmcia_mem_unmap(psc->sc_pf, ccr_window);
    441 		pcmcia_mem_free(psc->sc_pf, &pcmh);
    442 	}
    443 
    444 	/*
    445 	 * Try to get the ethernet address from FUNCE/LAN_NID tuple.
    446 	 */
    447 	xi_pcmcia_funce_enaddr(parent, sc->sc_enaddr);
    448 	if (!sc->sc_enaddr) {
    449 		printf("%s: unable to get ethernet address\n",
    450 			sc->sc_dev.dv_xname);
    451 		goto fail;
    452 	}
    453 
    454 	printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    455 	    ether_sprintf(sc->sc_enaddr));
    456 
    457 	ifp = &sc->sc_ethercom.ec_if;
    458 	memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
    459 	ifp->if_softc = sc;
    460 	ifp->if_start = xi_start;
    461 	ifp->if_ioctl = xi_ioctl;
    462 	ifp->if_watchdog = xi_watchdog;
    463 	ifp->if_flags =
    464 	    IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
    465 	ifp->if_snd.ifq_maxlen = IFQ_MAXLEN;
    466 
    467 	/* Reset and initialize the card. */
    468 	xi_full_reset(sc);
    469 
    470 	/*
    471 	 * Initialize our media structures and probe the MII.
    472 	 */
    473 	sc->sc_mii.mii_ifp = ifp;
    474 	sc->sc_mii.mii_readreg = xi_mdi_read;
    475 	sc->sc_mii.mii_writereg = xi_mdi_write;
    476 	sc->sc_mii.mii_statchg = xi_statchg;
    477 	ifmedia_init(&sc->sc_mii.mii_media, 0, xi_mediachange,
    478 	    xi_mediastatus);
    479 	DPRINTF(XID_MII | XID_CONFIG,
    480 	    ("xi: bmsr %x\n", xi_mdi_read(&sc->sc_dev, 0, 1)));
    481 	mii_attach(self, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    482 		MII_OFFSET_ANY, 0);
    483 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
    484 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
    485 		    NULL);
    486 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    487 
    488 	/*
    489 	 * Attach the interface.
    490 	 */
    491 	if_attach(ifp);
    492 	ether_ifattach(ifp, sc->sc_enaddr);
    493 	psc->sc_resource |= XI_RES_MI;
    494 
    495 #if NBPFILTER > 0
    496 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    497 #endif	/* NBPFILTER > 0 */
    498 
    499 	/*
    500 	 * Reset and initialize the card again for DINGO (as found in Linux
    501 	 * driver).  Without this Dingo will get a watchdog timeout the first
    502 	 * time.  The ugly media tickling seems to be necessary for getting
    503 	 * autonegotiation to work too.
    504 	 */
    505 	if (sc->sc_flags & XIFLAGS_DINGO) {
    506 		xi_full_reset(sc);
    507 		xi_init(sc);
    508 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    509 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_NONE);
    510 		xi_stop(sc);
    511 	}
    512 
    513 	/* Establish the interrupt. */
    514 	psc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, xi_intr, sc);
    515 	if (psc->sc_ih == NULL) {
    516 		printf("%s: couldn't establish interrupt\n",
    517 			sc->sc_dev.dv_xname);
    518 		goto fail;
    519 	}
    520 
    521 	return;
    522 
    523 fail:
    524 	if ((psc->sc_resource & XI_RES_IO) != 0) {
    525 		/* Unmap our i/o windows. */
    526 		pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    527                 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    528         }
    529         psc->sc_resource &= ~XI_RES_IO;
    530 	if (psc->sc_resource & XI_RES_PCIC) {
    531 		pcmcia_function_disable(pa->pf);
    532 		psc->sc_resource &= ~XI_RES_PCIC;
    533 	}
    534 	free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
    535 }
    536 
    537 int
    538 xi_pcmcia_detach(self, flags)
    539      struct device *self;
    540      int flags;
    541 {
    542 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    543 	struct xi_softc *sc = &psc->sc_xi;
    544 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    545 
    546 	DPRINTF(XID_CONFIG, ("xi_pcmcia_detach()\n"));
    547 
    548 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    549 		xi_stop(sc);
    550 	}
    551 
    552 	pcmcia_function_disable(psc->sc_pf);
    553 	psc->sc_resource &= ~XI_RES_PCIC;
    554 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
    555 	ifp->if_flags &= ~IFF_RUNNING;
    556 	ifp->if_timer = 0;
    557 
    558 	if ((psc->sc_resource & XI_RES_MI) != 0) {
    559 
    560 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    561 
    562 		ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    563 #if NBPFILTER > 0
    564 		bpfdetach(ifp);
    565 #endif
    566 		ether_ifdetach(ifp);
    567 		if_detach(ifp);
    568 		psc->sc_resource &= ~XI_RES_MI;
    569 	}
    570 
    571 	if ((psc->sc_resource & XI_RES_IO) != 0) {
    572 		/* Unmap our i/o windows. */
    573 		pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
    574                 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
    575         }
    576         free(SIMPLEQ_FIRST(&psc->sc_pf->cfe_head), M_DEVBUF);
    577 	psc->sc_resource &= ~XI_RES_IO;
    578 
    579 	return 0;
    580 }
    581 
    582 int
    583 xi_pcmcia_activate(self, act)
    584      struct device *self;
    585      enum devact act;
    586 {
    587 	struct xi_pcmcia_softc *psc = (struct xi_pcmcia_softc *)self;
    588 	struct xi_softc *sc = &psc->sc_xi;
    589 	int s, rv=0;
    590 
    591 	DPRINTF(XID_CONFIG, ("xi_pcmcia_activate()\n"));
    592 
    593 	s = splnet();
    594 	switch (act) {
    595 	case DVACT_ACTIVATE:
    596 		rv = EOPNOTSUPP;
    597 		break;
    598 
    599 	case DVACT_DEACTIVATE:
    600 		if_deactivate(&sc->sc_ethercom.ec_if);
    601 		break;
    602 	}
    603 	splx(s);
    604 	return (rv);
    605 }
    606 
    607 /*
    608  * XXX These two functions might be OK to factor out into pcmcia.c since
    609  * if_sm_pcmcia.c uses similar ones.
    610  */
    611 static int
    612 xi_pcmcia_funce_enaddr(parent, myla)
    613 	struct device *parent;
    614 	u_int8_t *myla;
    615 {
    616 	/* XXX The Linux driver has more ways to do this in case of failure. */
    617 	return (pcmcia_scan_cis(parent, xi_pcmcia_lan_nid_ciscallback, myla));
    618 }
    619 
    620 static int
    621 xi_pcmcia_lan_nid_ciscallback(tuple, arg)
    622 	struct pcmcia_tuple *tuple;
    623 	void *arg;
    624 {
    625 	u_int8_t *myla = arg;
    626 	int i;
    627 
    628 	DPRINTF(XID_CONFIG, ("xi_pcmcia_lan_nid_ciscallback()\n"));
    629 
    630 	if (tuple->code == PCMCIA_CISTPL_FUNCE) {
    631 		if (tuple->length < 2)
    632 			return (0);
    633 
    634 		switch (pcmcia_tuple_read_1(tuple, 0)) {
    635 		case PCMCIA_TPLFE_TYPE_LAN_NID:
    636 			if (pcmcia_tuple_read_1(tuple, 1) != ETHER_ADDR_LEN)
    637 				return (0);
    638 			break;
    639 
    640 		case 0x02:
    641 			/*
    642 			 * Not sure about this, I don't have a CE2
    643 			 * that puts the ethernet addr here.
    644 			 */
    645 		 	if (pcmcia_tuple_read_1(tuple, 1) != 13)
    646 				return (0);
    647 			break;
    648 
    649 		default:
    650 			return (0);
    651 		}
    652 
    653 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    654 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    655 		return (1);
    656 	}
    657 
    658 	/* Yet another spot where this might be. */
    659 	if (tuple->code == 0x89) {
    660 		pcmcia_tuple_read_1(tuple, 1);
    661 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    662 			myla[i] = pcmcia_tuple_read_1(tuple, i + 2);
    663 		return (1);
    664 	}
    665 	return (0);
    666 }
    667 
    668 int
    669 xi_pcmcia_manfid_ciscallback(tuple, arg)
    670 	struct pcmcia_tuple *tuple;
    671 	void *arg;
    672 {
    673 	u_int8_t *id = arg;
    674 
    675 	DPRINTF(XID_CONFIG, ("xi_pcmcia_manfid_callback()\n"));
    676 
    677 	if (tuple->code != PCMCIA_CISTPL_MANFID)
    678 		return (0);
    679 
    680 	if (tuple->length < 2)
    681 		return (0);
    682 
    683 	*id = pcmcia_tuple_read_1(tuple, 4);
    684 	return (1);
    685 }
    686 
    687 
    688 
    689 static int
    690 xi_intr(arg)
    691 	void *arg;
    692 {
    693 	struct xi_softc *sc = arg;
    694 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    695 	u_int8_t esr, rsr, isr, rx_status, savedpage;
    696 	u_int16_t tx_status, recvcount = 0, tempint;
    697 
    698 	DPRINTF(XID_CONFIG, ("xi_intr()\n"));
    699 
    700 #if 0
    701 	if (!(ifp->if_flags & IFF_RUNNING))
    702 		return (0);
    703 #endif
    704 
    705 	ifp->if_timer = 0;	/* turn watchdog timer off */
    706 
    707 	if (sc->sc_flags & XIFLAGS_MOHAWK) {
    708 		/* Disable interrupt (Linux does it). */
    709 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    710 		    0);
    711 	}
    712 
    713 	savedpage =
    714 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
    715 
    716 	PAGE(sc, 0);
    717 	esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
    718 	isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
    719 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    720 
    721 	/* Check to see if card has been ejected. */
    722 	if (isr == 0xff) {
    723 #ifdef DIAGNOSTIC
    724 		printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
    725 #endif
    726 		goto end;
    727 	}
    728 
    729 	PAGE(sc, 40);
    730 	rx_status =
    731 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
    732 	tx_status =
    733 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
    734 
    735 	/*
    736 	 * XXX Linux writes to RXST0 and TXST* here.  My CE2 works just fine
    737 	 * without it, and I can't see an obvious reason for it.
    738 	 */
    739 
    740 	PAGE(sc, 0);
    741 	while (esr & FULL_PKT_RCV) {
    742 		if (!(rsr & RSR_RX_OK))
    743 			break;
    744 
    745 		/* Compare bytes read this interrupt to hard maximum. */
    746 		if (recvcount > MAX_BYTES_INTR) {
    747 			DPRINTF(XID_INTR,
    748 			    ("xi: too many bytes this interrupt\n"));
    749 			ifp->if_iqdrops++;
    750 			/* Drop packet. */
    751 			bus_space_write_2(sc->sc_bst, sc->sc_bsh,
    752 			    sc->sc_offset + DO0, DO_SKIP_RX_PKT);
    753 		}
    754 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    755 		recvcount += tempint;
    756 		ifp->if_ibytes += tempint;
    757 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    758 		    sc->sc_offset + ESR);
    759 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    760 		    sc->sc_offset + RSR);
    761 	}
    762 
    763 	/* Packet too long? */
    764 	if (rsr & RSR_TOO_LONG) {
    765 		ifp->if_ierrors++;
    766 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    767 	}
    768 
    769 	/* CRC error? */
    770 	if (rsr & RSR_CRCERR) {
    771 		ifp->if_ierrors++;
    772 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    773 	}
    774 
    775 	/* Alignment error? */
    776 	if (rsr & RSR_ALIGNERR) {
    777 		ifp->if_ierrors++;
    778 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    779 	}
    780 
    781 	/* Check for rx overrun. */
    782 	if (rx_status & RX_OVERRUN) {
    783 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    784 		    CLR_RX_OVERRUN);
    785 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    786 	}
    787 
    788 	/* Try to start more packets transmitting. */
    789 	if (ifp->if_snd.ifq_head)
    790 		xi_start(ifp);
    791 
    792 	/* Detected excessive collisions? */
    793 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    794 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    795 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    796 		    RESTART_TX);
    797 		ifp->if_oerrors++;
    798 	}
    799 
    800 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    801 		ifp->if_oerrors++;
    802 
    803 end:
    804 	/* Reenable interrupts. */
    805 	PAGE(sc, savedpage);
    806 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    807 	    ENABLE_INT);
    808 
    809 	return (1);
    810 }
    811 
    812 /*
    813  * Pull a packet from the card into an mbuf chain.
    814  */
    815 static u_int16_t
    816 xi_get(sc)
    817 	struct xi_softc *sc;
    818 {
    819 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    820 	struct mbuf *top, **mp, *m;
    821 	u_int16_t pktlen, len, recvcount = 0;
    822 	u_int8_t *data;
    823 	u_int8_t rsr;
    824 
    825 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    826 
    827 	PAGE(sc, 0);
    828 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    829 
    830 	pktlen =
    831 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
    832 	    RBC_COUNT_MASK;
    833 
    834 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    835 
    836 	if (pktlen == 0) {
    837 		/*
    838 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    839 		 * when MPE is set.  It is not known why.
    840 		 */
    841 		return (0);
    842 	}
    843 
    844 	/* XXX should this be incremented now ? */
    845 	recvcount += pktlen;
    846 
    847 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    848 	if (m == 0)
    849 		return (recvcount);
    850 	m->m_pkthdr.rcvif = ifp;
    851 	m->m_pkthdr.len = pktlen;
    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 = splimp();
   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 	m0 = ifp->if_snd.ifq_head;
   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 	IF_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 = splimp();
   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