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