Home | History | Annotate | Line # | Download | only in pcmcia
if_xi.c revision 1.44
      1 /*	$NetBSD: if_xi.c,v 1.44 2004/08/09 04:47:40 mycroft Exp $ */
      2 /*	OpenBSD: if_xe.c,v 1.9 1999/09/16 11:28:42 niklas Exp 	*/
      3 
      4 /*
      5  * Copyright (c) 2004 Charles M. Hannum.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Charles M. Hannum.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  */
     21 
     22 /*
     23  * Copyright (c) 1999 Niklas Hallqvist, Brandon Creighton, Job de Haas
     24  * All rights reserved.
     25  *
     26  * Redistribution and use in source and binary forms, with or without
     27  * modification, are permitted provided that the following conditions
     28  * are met:
     29  * 1. Redistributions of source code must retain the above copyright
     30  *    notice, this list of conditions and the following disclaimer.
     31  * 2. Redistributions in binary form must reproduce the above copyright
     32  *    notice, this list of conditions and the following disclaimer in the
     33  *    documentation and/or other materials provided with the distribution.
     34  * 3. All advertising materials mentioning features or use of this software
     35  *    must display the following acknowledgement:
     36  *	This product includes software developed by Niklas Hallqvist,
     37  *	Brandon Creighton and Job de Haas.
     38  * 4. The name of the author may not be used to endorse or promote products
     39  *    derived from this software without specific prior written permission
     40  *
     41  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     42  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     44  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     50  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     51  */
     52 
     53 /*
     54  * A driver for Xircom CreditCard PCMCIA Ethernet adapters.
     55  */
     56 
     57 /*
     58  * Known Bugs:
     59  *
     60  * 1) Promiscuous mode doesn't work on at least the CE2.
     61  * 2) Slow. ~450KB/s.  Memory access would be better.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 __KERNEL_RCSID(0, "$NetBSD: if_xi.c,v 1.44 2004/08/09 04:47:40 mycroft Exp $");
     66 
     67 #include "opt_inet.h"
     68 #include "opt_ipx.h"
     69 #include "bpfilter.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/device.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/mbuf.h>
     76 #include <sys/malloc.h>
     77 #include <sys/socket.h>
     78 
     79 #include <net/if.h>
     80 #include <net/if_dl.h>
     81 #include <net/if_media.h>
     82 #include <net/if_types.h>
     83 #include <net/if_ether.h>
     84 
     85 #ifdef INET
     86 #include <netinet/in.h>
     87 #include <netinet/in_systm.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet/ip.h>
     90 #include <netinet/if_inarp.h>
     91 #endif
     92 
     93 #ifdef IPX
     94 #include <netipx/ipx.h>
     95 #include <netipx/ipx_if.h>
     96 #endif
     97 
     98 #ifdef NS
     99 #include <netns/ns.h>
    100 #include <netns/ns_if.h>
    101 #endif
    102 
    103 #if NBPFILTER > 0
    104 #include <net/bpf.h>
    105 #include <net/bpfdesc.h>
    106 #endif
    107 
    108 /*
    109  * Maximum number of bytes to read per interrupt.  Linux recommends
    110  * somewhere between 2000-22000.
    111  * XXX This is currently a hard maximum.
    112  */
    113 #define MAX_BYTES_INTR 12000
    114 
    115 #include <dev/mii/mii.h>
    116 #include <dev/mii/miivar.h>
    117 
    118 #include <dev/pcmcia/pcmciareg.h>
    119 #include <dev/pcmcia/pcmciavar.h>
    120 #include <dev/pcmcia/pcmciadevs.h>
    121 
    122 #include <dev/pcmcia/if_xireg.h>
    123 #include <dev/pcmcia/if_xivar.h>
    124 
    125 #ifdef __GNUC__
    126 #define INLINE	__inline
    127 #else
    128 #define INLINE
    129 #endif	/* __GNUC__ */
    130 
    131 #define	XIDEBUG
    132 #define	XIDEBUG_VALUE	0
    133 
    134 #ifdef XIDEBUG
    135 #define DPRINTF(cat, x) if (xidebug & (cat)) printf x
    136 
    137 #define XID_CONFIG	0x01
    138 #define XID_MII		0x02
    139 #define XID_INTR	0x04
    140 #define XID_FIFO	0x08
    141 #define	XID_MCAST	0x10
    142 
    143 #ifdef XIDEBUG_VALUE
    144 int xidebug = XIDEBUG_VALUE;
    145 #else
    146 int xidebug = 0;
    147 #endif
    148 #else
    149 #define DPRINTF(cat, x) (void)0
    150 #endif
    151 
    152 #define STATIC
    153 
    154 STATIC int xi_enable __P((struct xi_softc *));
    155 STATIC void xi_disable __P((struct xi_softc *));
    156 STATIC void xi_cycle_power __P((struct xi_softc *));
    157 STATIC int xi_ether_ioctl __P((struct ifnet *, u_long cmd, caddr_t));
    158 STATIC void xi_full_reset __P((struct xi_softc *));
    159 STATIC void xi_init __P((struct xi_softc *));
    160 STATIC int xi_ioctl __P((struct ifnet *, u_long, caddr_t));
    161 STATIC int xi_mdi_read __P((struct device *, int, int));
    162 STATIC void xi_mdi_write __P((struct device *, int, int, int));
    163 STATIC int xi_mediachange __P((struct ifnet *));
    164 STATIC void xi_mediastatus __P((struct ifnet *, struct ifmediareq *));
    165 STATIC u_int16_t xi_get __P((struct xi_softc *));
    166 STATIC void xi_reset __P((struct xi_softc *));
    167 STATIC void xi_set_address __P((struct xi_softc *));
    168 STATIC void xi_start __P((struct ifnet *));
    169 STATIC void xi_statchg __P((struct device *));
    170 STATIC void xi_stop __P((struct xi_softc *));
    171 STATIC void xi_watchdog __P((struct ifnet *));
    172 
    173 void
    174 xi_attach(sc, myea)
    175 	struct xi_softc *sc;
    176 	u_int8_t *myea;
    177 {
    178 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    179 
    180 #if 0
    181 	/*
    182 	 * Configuration as advised by DINGO documentation.
    183 	 * Dingo has some extra configuration registers in the CCR space.
    184 	 */
    185 	if (sc->sc_chipset >= XI_CHIPSET_DINGO) {
    186 		struct pcmcia_mem_handle pcmh;
    187 		int ccr_window;
    188 		bus_size_t ccr_offset;
    189 
    190 		/* get access to the DINGO CCR space */
    191 		if (pcmcia_mem_alloc(psc->sc_pf, PCMCIA_CCR_SIZE_DINGO,
    192 			&pcmh)) {
    193 			DPRINTF(XID_CONFIG, ("xi: bad mem alloc\n"));
    194 			goto fail;
    195 		}
    196 		if (pcmcia_mem_map(psc->sc_pf, PCMCIA_MEM_ATTR,
    197 			psc->sc_pf->ccr_base, PCMCIA_CCR_SIZE_DINGO,
    198 			&pcmh, &ccr_offset, &ccr_window)) {
    199 			DPRINTF(XID_CONFIG, ("xi: bad mem map\n"));
    200 			pcmcia_mem_free(psc->sc_pf, &pcmh);
    201 			goto fail;
    202 		}
    203 
    204 		/* enable the second function - usually modem */
    205 		bus_space_write_1(pcmh.memt, pcmh.memh,
    206 		    ccr_offset + PCMCIA_CCR_DCOR0, PCMCIA_CCR_DCOR0_SFINT);
    207 		bus_space_write_1(pcmh.memt, pcmh.memh,
    208 		    ccr_offset + PCMCIA_CCR_DCOR1,
    209 		    PCMCIA_CCR_DCOR1_FORCE_LEVIREQ | PCMCIA_CCR_DCOR1_D6);
    210 		bus_space_write_1(pcmh.memt, pcmh.memh,
    211 		    ccr_offset + PCMCIA_CCR_DCOR2, 0);
    212 		bus_space_write_1(pcmh.memt, pcmh.memh,
    213 		    ccr_offset + PCMCIA_CCR_DCOR3, 0);
    214 		bus_space_write_1(pcmh.memt, pcmh.memh,
    215 		    ccr_offset + PCMCIA_CCR_DCOR4, 0);
    216 
    217 		/* We don't need them anymore and can free them (I think). */
    218 		pcmcia_mem_unmap(psc->sc_pf, ccr_window);
    219 		pcmcia_mem_free(psc->sc_pf, &pcmh);
    220 	}
    221 #endif
    222 
    223 	/* Reset and initialize the card. */
    224 	xi_full_reset(sc);
    225 
    226 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname, ether_sprintf(myea));
    227 
    228 	ifp = &sc->sc_ethercom.ec_if;
    229 	/* Initialize the ifnet structure. */
    230 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    231 	ifp->if_softc = sc;
    232 	ifp->if_start = xi_start;
    233 	ifp->if_ioctl = xi_ioctl;
    234 	ifp->if_watchdog = xi_watchdog;
    235 	ifp->if_flags =
    236 	    IFF_BROADCAST | IFF_NOTRAILERS | IFF_SIMPLEX | IFF_MULTICAST;
    237 	IFQ_SET_READY(&ifp->if_snd);
    238 
    239 	/* 802.1q capability */
    240 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    241 
    242 	/* Attach the interface. */
    243 	if_attach(ifp);
    244 	ether_ifattach(ifp, myea);
    245 
    246 	/*
    247 	 * Initialize our media structures and probe the MII.
    248 	 */
    249 	sc->sc_mii.mii_ifp = ifp;
    250 	sc->sc_mii.mii_readreg = xi_mdi_read;
    251 	sc->sc_mii.mii_writereg = xi_mdi_write;
    252 	sc->sc_mii.mii_statchg = xi_statchg;
    253 	ifmedia_init(&sc->sc_mii.mii_media, 0, xi_mediachange,
    254 	    xi_mediastatus);
    255 	DPRINTF(XID_MII | XID_CONFIG,
    256 	    ("xi: bmsr %x\n", xi_mdi_read(&sc->sc_dev, 0, 1)));
    257 
    258 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    259 		MII_OFFSET_ANY, 0);
    260 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL)
    261 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO, 0,
    262 		    NULL);
    263 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    264 
    265 #if NRND > 0
    266 	rnd_attach_source(&sc->sc_rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET, 0);
    267 #endif
    268 }
    269 
    270 int
    271 xi_detach(self, flags)
    272 	struct device *self;
    273 	int flags;
    274 {
    275 	struct xi_softc *sc = (void *)self;
    276 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    277 
    278 	DPRINTF(XID_CONFIG, ("xi_detach()\n"));
    279 
    280 	xi_disable(sc);
    281 
    282 #if NRND > 0
    283 	rnd_detach_source(&sc->sc_rnd_source);
    284 #endif
    285 
    286 	mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    287 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
    288 	ether_ifdetach(ifp);
    289 	if_detach(ifp);
    290 
    291 	return 0;
    292 }
    293 
    294 int
    295 xi_activate(self, act)
    296 	struct device *self;
    297 	enum devact act;
    298 {
    299 	struct xi_softc *sc = (void *)self;
    300 	int s, rv = 0;
    301 
    302 	DPRINTF(XID_CONFIG, ("xi_activate()\n"));
    303 
    304 	s = splnet();
    305 	switch (act) {
    306 	case DVACT_ACTIVATE:
    307 		rv = EOPNOTSUPP;
    308 		break;
    309 
    310 	case DVACT_DEACTIVATE:
    311 		if_deactivate(&sc->sc_ethercom.ec_if);
    312 		break;
    313 	}
    314 	splx(s);
    315 	return (rv);
    316 }
    317 
    318 int
    319 xi_intr(arg)
    320 	void *arg;
    321 {
    322 	struct xi_softc *sc = arg;
    323 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    324 	u_int8_t esr, rsr, isr, rx_status, savedpage;
    325 	u_int16_t tx_status, recvcount = 0, tempint;
    326 
    327 	DPRINTF(XID_CONFIG, ("xi_intr()\n"));
    328 
    329 	if (sc->sc_enabled == 0 ||
    330 	    (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
    331 		return (0);
    332 
    333 	ifp->if_timer = 0;	/* turn watchdog timer off */
    334 
    335 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
    336 		/* Disable interrupt (Linux does it). */
    337 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    338 		    0);
    339 	}
    340 
    341 	savedpage =
    342 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + PR);
    343 
    344 	PAGE(sc, 0);
    345 	esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ESR);
    346 	isr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + ISR0);
    347 	rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RSR);
    348 
    349 	/* Check to see if card has been ejected. */
    350 	if (isr == 0xff) {
    351 #ifdef DIAGNOSTIC
    352 		printf("%s: interrupt for dead card\n", sc->sc_dev.dv_xname);
    353 #endif
    354 		goto end;
    355 	}
    356 	DPRINTF(XID_INTR, ("xi: isr=%02x\n", isr));
    357 
    358 	PAGE(sc, 0x40);
    359 	rx_status =
    360 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0);
    361 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RXST0,
    362 	    ~rx_status & 0xff);
    363 	tx_status =
    364 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0);
    365 	tx_status |=
    366 	    bus_space_read_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1) << 8;
    367 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST0,0);
    368 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + TXST1,0);
    369 	DPRINTF(XID_INTR, ("xi: rx_status=%02x tx_status=%04x\n", rx_status,
    370 	    tx_status));
    371 
    372 	PAGE(sc, 0);
    373 	while (esr & FULL_PKT_RCV) {
    374 		if (!(rsr & RSR_RX_OK))
    375 			break;
    376 
    377 		/* Compare bytes read this interrupt to hard maximum. */
    378 		if (recvcount > MAX_BYTES_INTR) {
    379 			DPRINTF(XID_INTR,
    380 			    ("xi: too many bytes this interrupt\n"));
    381 			ifp->if_iqdrops++;
    382 			/* Drop packet. */
    383 			bus_space_write_2(sc->sc_bst, sc->sc_bsh,
    384 			    sc->sc_offset + DO0, DO_SKIP_RX_PKT);
    385 		}
    386 		tempint = xi_get(sc);	/* XXX doesn't check the error! */
    387 		recvcount += tempint;
    388 		ifp->if_ibytes += tempint;
    389 		esr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    390 		    sc->sc_offset + ESR);
    391 		rsr = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    392 		    sc->sc_offset + RSR);
    393 	}
    394 
    395 	/* Packet too long? */
    396 	if (rsr & RSR_TOO_LONG) {
    397 		ifp->if_ierrors++;
    398 		DPRINTF(XID_INTR, ("xi: packet too long\n"));
    399 	}
    400 
    401 	/* CRC error? */
    402 	if (rsr & RSR_CRCERR) {
    403 		ifp->if_ierrors++;
    404 		DPRINTF(XID_INTR, ("xi: CRC error detected\n"));
    405 	}
    406 
    407 	/* Alignment error? */
    408 	if (rsr & RSR_ALIGNERR) {
    409 		ifp->if_ierrors++;
    410 		DPRINTF(XID_INTR, ("xi: alignment error detected\n"));
    411 	}
    412 
    413 	/* Check for rx overrun. */
    414 	if (rx_status & RX_OVERRUN) {
    415 		ifp->if_ierrors++;
    416 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    417 		    CLR_RX_OVERRUN);
    418 		DPRINTF(XID_INTR, ("xi: overrun cleared\n"));
    419 	}
    420 
    421 	/* Try to start more packets transmitting. */
    422 	if (IFQ_IS_EMPTY(&ifp->if_snd) == 0)
    423 		xi_start(ifp);
    424 
    425 	/* Detected excessive collisions? */
    426 	if ((tx_status & EXCESSIVE_COLL) && ifp->if_opackets > 0) {
    427 		DPRINTF(XID_INTR, ("xi: excessive collisions\n"));
    428 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    429 		    RESTART_TX);
    430 		ifp->if_oerrors++;
    431 	}
    432 
    433 	if ((tx_status & TX_ABORT) && ifp->if_opackets > 0)
    434 		ifp->if_oerrors++;
    435 
    436 	/* have handled the interrupt */
    437 #if NRND > 0
    438 	rnd_add_uint32(&sc->sc_rnd_source, tx_status);
    439 #endif
    440 
    441 end:
    442 	/* Reenable interrupts. */
    443 	PAGE(sc, savedpage);
    444 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + CR,
    445 	    ENABLE_INT);
    446 
    447 	return (1);
    448 }
    449 
    450 /*
    451  * Pull a packet from the card into an mbuf chain.
    452  */
    453 STATIC u_int16_t
    454 xi_get(sc)
    455 	struct xi_softc *sc;
    456 {
    457 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    458 	struct mbuf *top, **mp, *m;
    459 	u_int16_t pktlen, len, recvcount = 0;
    460 	u_int8_t *data;
    461 
    462 	DPRINTF(XID_CONFIG, ("xi_get()\n"));
    463 
    464 	PAGE(sc, 0);
    465 	pktlen =
    466 	    bus_space_read_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + RBC0) &
    467 	    RBC_COUNT_MASK;
    468 
    469 	DPRINTF(XID_CONFIG, ("xi_get: pktlen=%d\n", pktlen));
    470 
    471 	if (pktlen == 0) {
    472 		/*
    473 		 * XXX At least one CE2 sets RBC0 == 0 occasionally, and only
    474 		 * when MPE is set.  It is not known why.
    475 		 */
    476 		return (0);
    477 	}
    478 
    479 	/* XXX should this be incremented now ? */
    480 	recvcount += pktlen;
    481 
    482 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    483 	if (m == 0)
    484 		return (recvcount);
    485 	m->m_pkthdr.rcvif = ifp;
    486 	m->m_pkthdr.len = pktlen;
    487 	m->m_flags |= M_HASFCS;
    488 	len = MHLEN;
    489 	top = 0;
    490 	mp = &top;
    491 
    492 	while (pktlen > 0) {
    493 		if (top) {
    494 			MGET(m, M_DONTWAIT, MT_DATA);
    495 			if (m == 0) {
    496 				m_freem(top);
    497 				return (recvcount);
    498 			}
    499 			len = MLEN;
    500 		}
    501 		if (pktlen >= MINCLSIZE) {
    502 			MCLGET(m, M_DONTWAIT);
    503 			if (!(m->m_flags & M_EXT)) {
    504 				m_freem(m);
    505 				m_freem(top);
    506 				return (recvcount);
    507 			}
    508 			len = MCLBYTES;
    509 		}
    510 		if (!top) {
    511 			caddr_t newdata = (caddr_t)ALIGN(m->m_data +
    512 			    sizeof(struct ether_header)) -
    513 			    sizeof(struct ether_header);
    514 			len -= newdata - m->m_data;
    515 			m->m_data = newdata;
    516 		}
    517 		len = min(pktlen, len);
    518 		data = mtod(m, u_int8_t *);
    519 		if (len > 1) {
    520 		        len &= ~1;
    521 			bus_space_read_multi_2(sc->sc_bst, sc->sc_bsh,
    522 			    sc->sc_offset + EDP, (u_int16_t *)data, len>>1);
    523 		} else
    524 			*data = bus_space_read_1(sc->sc_bst, sc->sc_bsh,
    525 			    sc->sc_offset + EDP);
    526 		m->m_len = len;
    527 		pktlen -= len;
    528 		*mp = m;
    529 		mp = &m->m_next;
    530 	}
    531 
    532 	/* Skip Rx packet. */
    533 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, sc->sc_offset + DO0,
    534 	    DO_SKIP_RX_PKT);
    535 
    536 	ifp->if_ipackets++;
    537 
    538 #if NBPFILTER > 0
    539 	if (ifp->if_bpf)
    540 		bpf_mtap(ifp->if_bpf, top);
    541 #endif
    542 
    543 	(*ifp->if_input)(ifp, top);
    544 	return (recvcount);
    545 }
    546 
    547 /*
    548  * Serial management for the MII.
    549  * The DELAY's below stem from the fact that the maximum frequency
    550  * acceptable on the MDC pin is 2.5 MHz and fast processors can easily
    551  * go much faster than that.
    552  */
    553 
    554 /* Let the MII serial management be idle for one period. */
    555 static INLINE void xi_mdi_idle __P((struct xi_softc *));
    556 static INLINE void
    557 xi_mdi_idle(sc)
    558 	struct xi_softc *sc;
    559 {
    560 	bus_space_tag_t bst = sc->sc_bst;
    561 	bus_space_handle_t bsh = sc->sc_bsh;
    562 	bus_size_t offset = sc->sc_offset;
    563 
    564 	/* Drive MDC low... */
    565 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
    566 	DELAY(1);
    567 
    568 	/* and high again. */
    569 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
    570 	DELAY(1);
    571 }
    572 
    573 /* Pulse out one bit of data. */
    574 static INLINE void xi_mdi_pulse __P((struct xi_softc *, int));
    575 static INLINE void
    576 xi_mdi_pulse(sc, data)
    577 	struct xi_softc *sc;
    578 	int data;
    579 {
    580 	bus_space_tag_t bst = sc->sc_bst;
    581 	bus_space_handle_t bsh = sc->sc_bsh;
    582 	bus_size_t offset = sc->sc_offset;
    583 	u_int8_t bit = data ? MDIO_HIGH : MDIO_LOW;
    584 
    585 	/* First latch the data bit MDIO with clock bit MDC low...*/
    586 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_LOW);
    587 	DELAY(1);
    588 
    589 	/* then raise the clock again, preserving the data bit. */
    590 	bus_space_write_1(bst, bsh, offset + GP2, bit | MDC_HIGH);
    591 	DELAY(1);
    592 }
    593 
    594 /* Probe one bit of data. */
    595 static INLINE int xi_mdi_probe __P((struct xi_softc *sc));
    596 static INLINE int
    597 xi_mdi_probe(sc)
    598 	struct xi_softc *sc;
    599 {
    600 	bus_space_tag_t bst = sc->sc_bst;
    601 	bus_space_handle_t bsh = sc->sc_bsh;
    602 	bus_size_t offset = sc->sc_offset;
    603 	u_int8_t x;
    604 
    605 	/* Pull clock bit MDCK low... */
    606 	bus_space_write_1(bst, bsh, offset + GP2, MDC_LOW);
    607 	DELAY(1);
    608 
    609 	/* Read data and drive clock high again. */
    610 	x = bus_space_read_1(bst, bsh, offset + GP2);
    611 	bus_space_write_1(bst, bsh, offset + GP2, MDC_HIGH);
    612 	DELAY(1);
    613 
    614 	return (x & MDIO);
    615 }
    616 
    617 /* Pulse out a sequence of data bits. */
    618 static INLINE void xi_mdi_pulse_bits __P((struct xi_softc *, u_int32_t, int));
    619 static INLINE void
    620 xi_mdi_pulse_bits(sc, data, len)
    621 	struct xi_softc *sc;
    622 	u_int32_t data;
    623 	int len;
    624 {
    625 	u_int32_t mask;
    626 
    627 	for (mask = 1 << (len - 1); mask; mask >>= 1)
    628 		xi_mdi_pulse(sc, data & mask);
    629 }
    630 
    631 /* Read a PHY register. */
    632 STATIC int
    633 xi_mdi_read(self, phy, reg)
    634 	struct device *self;
    635 	int phy;
    636 	int reg;
    637 {
    638 	struct xi_softc *sc = (struct xi_softc *)self;
    639 	int i;
    640 	u_int32_t mask;
    641 	u_int32_t data = 0;
    642 
    643 	PAGE(sc, 2);
    644 	for (i = 0; i < 32; i++)	/* Synchronize. */
    645 		xi_mdi_pulse(sc, 1);
    646 	xi_mdi_pulse_bits(sc, 0x06, 4); /* Start + Read opcode */
    647 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    648 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    649 	xi_mdi_idle(sc);		/* Turn around. */
    650 	xi_mdi_probe(sc);		/* Drop initial zero bit. */
    651 
    652 	for (mask = 1 << 15; mask; mask >>= 1) {
    653 		if (xi_mdi_probe(sc))
    654 			data |= mask;
    655 	}
    656 	xi_mdi_idle(sc);
    657 
    658 	DPRINTF(XID_MII,
    659 	    ("xi_mdi_read: phy %d reg %d -> %x\n", phy, reg, data));
    660 
    661 	return (data);
    662 }
    663 
    664 /* Write a PHY register. */
    665 STATIC void
    666 xi_mdi_write(self, phy, reg, value)
    667 	struct device *self;
    668 	int phy;
    669 	int reg;
    670 	int value;
    671 {
    672 	struct xi_softc *sc = (struct xi_softc *)self;
    673 	int i;
    674 
    675 	PAGE(sc, 2);
    676 	for (i = 0; i < 32; i++)	/* Synchronize. */
    677 		xi_mdi_pulse(sc, 1);
    678 	xi_mdi_pulse_bits(sc, 0x05, 4); /* Start + Write opcode */
    679 	xi_mdi_pulse_bits(sc, phy, 5);	/* PHY address */
    680 	xi_mdi_pulse_bits(sc, reg, 5);	/* PHY register */
    681 	xi_mdi_pulse_bits(sc, 0x02, 2); /* Turn around. */
    682 	xi_mdi_pulse_bits(sc, value, 16);	/* Write the data */
    683 	xi_mdi_idle(sc);		/* Idle away. */
    684 
    685 	DPRINTF(XID_MII,
    686 	    ("xi_mdi_write: phy %d reg %d val %x\n", phy, reg, value));
    687 }
    688 
    689 STATIC void
    690 xi_statchg(self)
    691 	struct device *self;
    692 {
    693 	/* XXX Update ifp->if_baudrate */
    694 }
    695 
    696 /*
    697  * Change media according to request.
    698  */
    699 STATIC int
    700 xi_mediachange(ifp)
    701 	struct ifnet *ifp;
    702 {
    703 	int s;
    704 
    705 	DPRINTF(XID_CONFIG, ("xi_mediachange()\n"));
    706 
    707 	if (ifp->if_flags & IFF_UP) {
    708 		s = splnet();
    709 		xi_init(ifp->if_softc);
    710 		splx(s);
    711 	}
    712 	return (0);
    713 }
    714 
    715 /*
    716  * Notify the world which media we're using.
    717  */
    718 STATIC void
    719 xi_mediastatus(ifp, ifmr)
    720 	struct ifnet *ifp;
    721 	struct ifmediareq *ifmr;
    722 {
    723 	struct xi_softc *sc = ifp->if_softc;
    724 
    725 	DPRINTF(XID_CONFIG, ("xi_mediastatus()\n"));
    726 
    727 	if (LIST_FIRST(&sc->sc_mii.mii_phys)) {
    728 		mii_pollstat(&sc->sc_mii);
    729 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
    730 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
    731 	}
    732 }
    733 
    734 STATIC void
    735 xi_reset(sc)
    736 	struct xi_softc *sc;
    737 {
    738 	int s;
    739 
    740 	DPRINTF(XID_CONFIG, ("xi_reset()\n"));
    741 
    742 	s = splnet();
    743 	xi_stop(sc);
    744 	xi_init(sc);
    745 	splx(s);
    746 }
    747 
    748 STATIC void
    749 xi_watchdog(ifp)
    750 	struct ifnet *ifp;
    751 {
    752 	struct xi_softc *sc = ifp->if_softc;
    753 
    754 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
    755 	++ifp->if_oerrors;
    756 
    757 	xi_reset(sc);
    758 }
    759 
    760 STATIC void
    761 xi_stop(sc)
    762 	register struct xi_softc *sc;
    763 {
    764 	bus_space_tag_t bst = sc->sc_bst;
    765 	bus_space_handle_t bsh = sc->sc_bsh;
    766 	bus_size_t offset = sc->sc_offset;
    767 
    768 	DPRINTF(XID_CONFIG, ("xi_stop()\n"));
    769 
    770 	PAGE(sc, 0x40);
    771 	bus_space_write_1(bst, bsh, offset + CMD0, DISABLE_RX);
    772 
    773 	/* Disable interrupts. */
    774 	PAGE(sc, 0);
    775 	bus_space_write_1(bst, bsh, offset + CR, 0);
    776 
    777 	PAGE(sc, 1);
    778 	bus_space_write_1(bst, bsh, offset + IMR0, 0);
    779 
    780 	/* Cancel watchdog timer. */
    781 	sc->sc_ethercom.ec_if.if_timer = 0;
    782 }
    783 
    784 STATIC int
    785 xi_enable(sc)
    786 	struct xi_softc *sc;
    787 {
    788 	int error;
    789 
    790 	if (!sc->sc_enabled) {
    791 		error = (*sc->sc_enable)(sc);
    792 		if (error)
    793 			return (error);
    794 		sc->sc_enabled = 1;
    795 		xi_full_reset(sc);
    796 	}
    797 	return (0);
    798 }
    799 
    800 STATIC void
    801 xi_disable(sc)
    802 	struct xi_softc *sc;
    803 {
    804 
    805 	if (sc->sc_enabled) {
    806 		sc->sc_enabled = 0;
    807 		(*sc->sc_disable)(sc);
    808 	}
    809 }
    810 
    811 STATIC void
    812 xi_init(sc)
    813 	struct xi_softc *sc;
    814 {
    815 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    816 	bus_space_tag_t bst = sc->sc_bst;
    817 	bus_space_handle_t bsh = sc->sc_bsh;
    818 	bus_size_t offset = sc->sc_offset;
    819 
    820 	DPRINTF(XID_CONFIG, ("xi_init()\n"));
    821 
    822 	/* Setup the ethernet interrupt mask. */
    823 	PAGE(sc, 1);
    824 	bus_space_write_1(bst, bsh, offset + IMR0,
    825 	    ISR_TX_OFLOW | ISR_PKT_TX | ISR_MAC_INT | /* ISR_RX_EARLY | */
    826 	    ISR_RX_FULL | ISR_RX_PKT_REJ | ISR_FORCED_INT);
    827 	if (sc->sc_chipset < XI_CHIPSET_DINGO) {
    828 		/* XXX What is this?  Not for Dingo at least. */
    829 		/* Unmask TX underrun detection */
    830 		bus_space_write_1(bst, bsh, offset + IMR1, 1);
    831 	}
    832 
    833 	/* Enable interrupts. */
    834 	PAGE(sc, 0);
    835 	bus_space_write_1(bst, bsh, offset + CR, ENABLE_INT);
    836 
    837 	xi_set_address(sc);
    838 
    839 	PAGE(sc, 0x40);
    840 	bus_space_write_1(bst, bsh, offset + CMD0, ENABLE_RX | ONLINE);
    841 
    842 	PAGE(sc, 0);
    843 
    844 	/* Set current media. */
    845 	mii_mediachg(&sc->sc_mii);
    846 
    847 	ifp->if_flags |= IFF_RUNNING;
    848 	ifp->if_flags &= ~IFF_OACTIVE;
    849 
    850 	xi_start(ifp);
    851 }
    852 
    853 /*
    854  * Start outputting on the interface.
    855  * Always called as splnet().
    856  */
    857 STATIC void
    858 xi_start(ifp)
    859 	struct ifnet *ifp;
    860 {
    861 	struct xi_softc *sc = ifp->if_softc;
    862 	bus_space_tag_t bst = sc->sc_bst;
    863 	bus_space_handle_t bsh = sc->sc_bsh;
    864 	bus_size_t offset = sc->sc_offset;
    865 	unsigned int s, len, pad = 0;
    866 	struct mbuf *m0, *m;
    867 	u_int16_t space;
    868 
    869 	DPRINTF(XID_CONFIG, ("xi_start()\n"));
    870 
    871 	/* Don't transmit if interface is busy or not running. */
    872 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) {
    873 		DPRINTF(XID_CONFIG, ("xi: interface busy or not running\n"));
    874 		return;
    875 	}
    876 
    877 	/* Peek at the next packet. */
    878 	IFQ_POLL(&ifp->if_snd, m0);
    879 	if (m0 == 0)
    880 		return;
    881 
    882 	/* We need to use m->m_pkthdr.len, so require the header. */
    883 	if (!(m0->m_flags & M_PKTHDR))
    884 		panic("xi_start: no header mbuf");
    885 
    886 	len = m0->m_pkthdr.len;
    887 
    888 #if 1
    889 	/* Pad to ETHER_MIN_LEN - ETHER_CRC_LEN. */
    890 	if (len < ETHER_MIN_LEN - ETHER_CRC_LEN)
    891 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    892 #else
    893 	pad = 0;
    894 #endif
    895 
    896 	PAGE(sc, 0);
    897 
    898 	bus_space_write_2(bst, bsh, offset + TRS, (u_int16_t)len + pad + 2);
    899 	space = bus_space_read_2(bst, bsh, offset + TSO) & 0x7fff;
    900 	if (len + pad + 2 > space) {
    901 		DPRINTF(XID_FIFO,
    902 		    ("xi: not enough space in output FIFO (%d > %d)\n",
    903 		    len + pad + 2, space));
    904 		return;
    905 	}
    906 
    907 	IFQ_DEQUEUE(&ifp->if_snd, m0);
    908 
    909 #if NBPFILTER > 0
    910 	if (ifp->if_bpf)
    911 		bpf_mtap(ifp->if_bpf, m0);
    912 #endif
    913 
    914 	/*
    915 	 * Do the output at splhigh() so that an interrupt from another device
    916 	 * won't cause a FIFO underrun.
    917 	 */
    918 	s = splhigh();
    919 
    920 	bus_space_write_2(bst, bsh, offset + EDP, (u_int16_t)len + pad);
    921 	for (m = m0; m; ) {
    922 		if (m->m_len > 1)
    923 			bus_space_write_multi_2(bst, bsh, offset + EDP,
    924 			    mtod(m, u_int16_t *), m->m_len>>1);
    925 		if (m->m_len & 1) {
    926 			DPRINTF(XID_CONFIG, ("xi: XXX odd!\n"));
    927 			bus_space_write_1(bst, bsh, offset + EDP,
    928 			    *(mtod(m, u_int8_t *) + m->m_len - 1));
    929 		}
    930 		MFREE(m, m0);
    931 		m = m0;
    932 	}
    933 	DPRINTF(XID_CONFIG, ("xi: len=%d pad=%d total=%d\n", len, pad, len+pad+4));
    934 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
    935 		bus_space_write_1(bst, bsh, offset + CR, TX_PKT | ENABLE_INT);
    936 	else {
    937 		for (; pad > 1; pad -= 2)
    938 			bus_space_write_2(bst, bsh, offset + EDP, 0);
    939 		if (pad == 1)
    940 			bus_space_write_1(bst, bsh, offset + EDP, 0);
    941 	}
    942 
    943 	splx(s);
    944 
    945 	ifp->if_timer = 5;
    946 	++ifp->if_opackets;
    947 }
    948 
    949 STATIC int
    950 xi_ether_ioctl(ifp, cmd, data)
    951 	struct ifnet *ifp;
    952 	u_long cmd;
    953 	caddr_t data;
    954 {
    955 	struct ifaddr *ifa = (struct ifaddr *)data;
    956 	struct xi_softc *sc = ifp->if_softc;
    957 	int error;
    958 
    959 	DPRINTF(XID_CONFIG, ("xi_ether_ioctl()\n"));
    960 
    961 	switch (cmd) {
    962 	case SIOCSIFADDR:
    963 		if ((error = xi_enable(sc)) != 0)
    964 			break;
    965 
    966 		ifp->if_flags |= IFF_UP;
    967 
    968 		switch (ifa->ifa_addr->sa_family) {
    969 #ifdef INET
    970 		case AF_INET:
    971 			xi_init(sc);
    972 			arp_ifinit(ifp, ifa);
    973 			break;
    974 #endif	/* INET */
    975 
    976 #ifdef NS
    977 		case AF_NS:
    978 		{
    979 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    980 
    981 			if (ns_nullhost(*ina))
    982 				ina->x_host = *(union ns_host *)
    983 					LLADDR(ifp->if_sadl);
    984 			else
    985 				memcpy(LLADDR(ifp->if_sadl), ina->x_host.c_host,
    986 					ifp->if_addrlen);
    987 			/* Set new address. */
    988 			xi_init(sc);
    989 			break;
    990 		}
    991 #endif  /* NS */
    992 
    993 		default:
    994 			xi_init(sc);
    995 			break;
    996 		}
    997 		break;
    998 
    999 	default:
   1000 		return (EINVAL);
   1001 	}
   1002 
   1003 	return (0);
   1004 }
   1005 
   1006 STATIC int
   1007 xi_ioctl(ifp, cmd, data)
   1008 	struct ifnet *ifp;
   1009 	u_long cmd;
   1010 	caddr_t data;
   1011 {
   1012 	struct xi_softc *sc = ifp->if_softc;
   1013 	struct ifreq *ifr = (struct ifreq *)data;
   1014 	int s, error = 0;
   1015 
   1016 	DPRINTF(XID_CONFIG, ("xi_ioctl()\n"));
   1017 
   1018 	s = splnet();
   1019 
   1020 	switch (cmd) {
   1021 	case SIOCSIFADDR:
   1022 		error = xi_ether_ioctl(ifp, cmd, data);
   1023 		break;
   1024 
   1025 	case SIOCSIFFLAGS:
   1026 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1027 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1028 			/*
   1029 			 * If interface is marked down and it is running,
   1030 			 * stop it.
   1031 			 */
   1032 			xi_stop(sc);
   1033 			ifp->if_flags &= ~IFF_RUNNING;
   1034 			xi_disable(sc);
   1035 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1036 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1037 			/*
   1038 			 * If interface is marked up and it is stopped,
   1039 			 * start it.
   1040 			 */
   1041 			if ((error = xi_enable(sc)) != 0)
   1042 				break;
   1043 			xi_init(sc);
   1044 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1045 			/*
   1046 			 * Reset the interface to pick up changes in any
   1047 			 * other flags that affect hardware registers.
   1048 			 */
   1049 			xi_set_address(sc);
   1050 		}
   1051 		break;
   1052 
   1053 	case SIOCADDMULTI:
   1054 	case SIOCDELMULTI:
   1055 		if (sc->sc_enabled == 0) {
   1056 			error = EIO;
   1057 			break;
   1058 		}
   1059 
   1060 		error = (cmd == SIOCADDMULTI) ?
   1061 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1062 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1063 		if (error == ENETRESET) {
   1064 			/*
   1065 			 * Multicast list has changed; set the hardware
   1066 			 * filter accordingly.
   1067 			 */
   1068 			xi_set_address(sc);
   1069 			error = 0;
   1070 		}
   1071 		break;
   1072 
   1073 	case SIOCSIFMEDIA:
   1074 	case SIOCGIFMEDIA:
   1075 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1076 		break;
   1077 
   1078 	default:
   1079 		error = EINVAL;
   1080 		break;
   1081 	}
   1082 
   1083 	splx(s);
   1084 	return (error);
   1085 }
   1086 
   1087 STATIC void
   1088 xi_set_address(sc)
   1089 	struct xi_softc *sc;
   1090 {
   1091 	bus_space_tag_t bst = sc->sc_bst;
   1092 	bus_space_handle_t bsh = sc->sc_bsh;
   1093 	bus_size_t offset = sc->sc_offset;
   1094 	struct ethercom *ether = &sc->sc_ethercom;
   1095 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1096 	struct ether_multistep step;
   1097 	struct ether_multi *enm;
   1098 	int page, num;
   1099 	int i;
   1100 	u_int8_t x;
   1101 	u_int8_t *enaddr;
   1102 	u_int8_t indaddr[64];
   1103 
   1104 	DPRINTF(XID_CONFIG, ("xi_set_address()\n"));
   1105 
   1106 	enaddr = (u_int8_t *)LLADDR(ifp->if_sadl);
   1107 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1108 		for (i = 0; i < 6; i++)
   1109 			indaddr[i] = enaddr[5 - i];
   1110 	else
   1111 		for (i = 0; i < 6; i++)
   1112 			indaddr[i] = enaddr[i];
   1113 	num = 1;
   1114 
   1115 	if (ether->ec_multicnt > 9) {
   1116 		ifp->if_flags |= IFF_ALLMULTI;
   1117 		goto done;
   1118 	}
   1119 
   1120 	ETHER_FIRST_MULTI(step, ether, enm);
   1121 	for (; enm; num++) {
   1122 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
   1123 		    sizeof(enm->enm_addrlo)) != 0) {
   1124 			/*
   1125 			 * The multicast address is really a range;
   1126 			 * it's easier just to accept all multicasts.
   1127 			 * XXX should we be setting IFF_ALLMULTI here?
   1128 			 */
   1129 			ifp->if_flags |= IFF_ALLMULTI;
   1130 			goto done;
   1131 		}
   1132 		if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1133 			for (i = 0; i < 6; i++)
   1134 				indaddr[num * 6 + i] = enm->enm_addrlo[5 - i];
   1135 		else
   1136 			for (i = 0; i < 6; i++)
   1137 				indaddr[num * 6 + i] = enm->enm_addrlo[i];
   1138 		ETHER_NEXT_MULTI(step, enm);
   1139 	}
   1140 	ifp->if_flags &= ~IFF_ALLMULTI;
   1141 
   1142 done:
   1143 	if (num < 10)
   1144 		memset(&indaddr[num * 6], 0xff, 6 * (10 - num));
   1145 
   1146 	for (page = 0; page < 8; page++) {
   1147 #ifdef XIDEBUG
   1148 		if (xidebug & XID_MCAST) {
   1149 			printf("page %d:", page);
   1150 			for (i = 0; i < 8; i++)
   1151 				printf(" %02x", indaddr[page * 8 + i]);
   1152 			printf("\n");
   1153 		}
   1154 #endif
   1155 
   1156 		PAGE(sc, 0x50 + page);
   1157 		bus_space_write_region_1(bst, bsh, offset + IA,
   1158 		    &indaddr[page * 8], page == 7 ? 4 : 8);
   1159 	}
   1160 
   1161 	PAGE(sc, 0x42);
   1162 	x = SWC1_IND_ADDR;
   1163 	if (ifp->if_flags & IFF_PROMISC)
   1164 		x |= SWC1_PROMISC;
   1165 	if (ifp->if_flags & (IFF_ALLMULTI|IFF_PROMISC))
   1166 		x |= SWC1_MCAST_PROM;
   1167 	if (!LIST_FIRST(&sc->sc_mii.mii_phys))
   1168 		x |= SWC1_AUTO_MEDIA;
   1169 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, sc->sc_offset + SWC1, x);
   1170 
   1171 	PAGE(sc, 0);
   1172 }
   1173 
   1174 STATIC void
   1175 xi_cycle_power(sc)
   1176 	struct xi_softc *sc;
   1177 {
   1178 	bus_space_tag_t bst = sc->sc_bst;
   1179 	bus_space_handle_t bsh = sc->sc_bsh;
   1180 	bus_size_t offset = sc->sc_offset;
   1181 
   1182 	DPRINTF(XID_CONFIG, ("xi_cycle_power()\n"));
   1183 
   1184 	PAGE(sc, 4);
   1185 	DELAY(1);
   1186 	bus_space_write_1(bst, bsh, offset + GP1, 0);
   1187 	DELAY(40000);
   1188 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK)
   1189 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP);
   1190 	else
   1191 		/* XXX What is bit 2 (aka AIC)? */
   1192 		bus_space_write_1(bst, bsh, offset + GP1, POWER_UP | 4);
   1193 	DELAY(20000);
   1194 }
   1195 
   1196 STATIC void
   1197 xi_full_reset(sc)
   1198 	struct xi_softc *sc;
   1199 {
   1200 	bus_space_tag_t bst = sc->sc_bst;
   1201 	bus_space_handle_t bsh = sc->sc_bsh;
   1202 	bus_size_t offset = sc->sc_offset;
   1203 	u_int8_t x;
   1204 
   1205 	DPRINTF(XID_CONFIG, ("xi_full_reset()\n"));
   1206 
   1207 	/* Do an as extensive reset as possible on all functions. */
   1208 	xi_cycle_power(sc);
   1209 	bus_space_write_1(bst, bsh, offset + CR, SOFT_RESET);
   1210 	DELAY(20000);
   1211 	bus_space_write_1(bst, bsh, offset + CR, 0);
   1212 	DELAY(20000);
   1213 	PAGE(sc, 4);
   1214 	if (sc->sc_chipset >= XI_CHIPSET_MOHAWK) {
   1215 		/*
   1216 		 * Drive GP1 low to power up ML6692 and GP2 high to power up
   1217 		 * the 10MHz chip.  XXX What chip is that?  The phy?
   1218 		 */
   1219 		bus_space_write_1(bst, bsh, offset + GP0,
   1220 		    GP1_OUT | GP2_OUT | GP2_WR);
   1221 	}
   1222 	DELAY(500000);
   1223 
   1224 	/* Get revision information.  XXX Symbolic constants. */
   1225 	sc->sc_rev = bus_space_read_1(bst, bsh, offset + BV) &
   1226 	    ((sc->sc_chipset >= XI_CHIPSET_MOHAWK) ? 0x70 : 0x30) >> 4;
   1227 	DPRINTF(XID_CONFIG, ("xi: rev=%02x\n", sc->sc_rev));
   1228 
   1229 	/* Media selection.  XXX Maybe manual overriding too? */
   1230 	if (sc->sc_chipset < XI_CHIPSET_MOHAWK) {
   1231 		/*
   1232 		 * XXX I have no idea what this really does, it is from the
   1233 		 * Linux driver.
   1234 		 */
   1235 		bus_space_write_1(bst, bsh, offset + GP0, GP1_OUT);
   1236 	}
   1237 	DELAY(40000);
   1238 
   1239 	/*
   1240 	 * Disable source insertion.
   1241 	 * XXX Dingo does not have this bit, but Linux does it unconditionally.
   1242 	 */
   1243 	if (sc->sc_chipset < XI_CHIPSET_DINGO) {
   1244 		PAGE(sc, 0x42);
   1245 		bus_space_write_1(bst, bsh, offset + SWC0, 0x20);
   1246 	}
   1247 
   1248 	/* Set the local memory dividing line. */
   1249 	if (sc->sc_rev != 1) {
   1250 		PAGE(sc, 2);
   1251 		/* XXX Symbolic constant preferrable. */
   1252 		bus_space_write_2(bst, bsh, offset + RBS0, 0x2000);
   1253 	}
   1254 
   1255 	/*
   1256 	 * Apparently the receive byte pointer can be bad after a reset, so
   1257 	 * we hardwire it correctly.
   1258 	 */
   1259 	PAGE(sc, 0);
   1260 	bus_space_write_2(bst, bsh, offset + DO0, DO_CHG_OFFSET);
   1261 
   1262 	/* Setup ethernet MAC registers. XXX Symbolic constants. */
   1263 	PAGE(sc, 0x40);
   1264 	bus_space_write_1(bst, bsh, offset + RX0MSK,
   1265 	    PKT_TOO_LONG | CRC_ERR | RX_OVERRUN | RX_ABORT | RX_OK);
   1266 	bus_space_write_1(bst, bsh, offset + TX0MSK,
   1267 	    CARRIER_LOST | EXCESSIVE_COLL | TX_UNDERRUN | LATE_COLLISION |
   1268 	    SQE | TX_ABORT | TX_OK);
   1269 	if (sc->sc_chipset < XI_CHIPSET_DINGO)
   1270 		/* XXX From Linux, dunno what 0xb0 means. */
   1271 		bus_space_write_1(bst, bsh, offset + TX1MSK, 0xb0);
   1272 	bus_space_write_1(bst, bsh, offset + RXST0, 0);
   1273 	bus_space_write_1(bst, bsh, offset + TXST0, 0);
   1274 	bus_space_write_1(bst, bsh, offset + TXST1, 0);
   1275 
   1276 	PAGE(sc, 2);
   1277 
   1278 	/* Enable MII function if available. */
   1279 	x = 0;
   1280 	if (LIST_FIRST(&sc->sc_mii.mii_phys))
   1281 		x |= SELECT_MII;
   1282 	bus_space_write_1(bst, bsh, offset + MSR, x);
   1283 	DELAY(20000);
   1284 
   1285 	/* Configure the LED registers. */
   1286 	/* XXX This is not good for 10base2. */
   1287 	bus_space_write_1(bst, bsh, offset + LED,
   1288 	    (LED_TX_ACT << LED1_SHIFT) | (LED_10MB_LINK << LED0_SHIFT));
   1289 	if (sc->sc_chipset >= XI_CHIPSET_DINGO)
   1290 		bus_space_write_1(bst, bsh, offset + LED3,
   1291 		    LED_100MB_LINK << LED3_SHIFT);
   1292 
   1293 	/*
   1294 	 * The Linux driver says this:
   1295 	 * We should switch back to page 0 to avoid a bug in revision 0
   1296 	 * where regs with offset below 8 can't be read after an access
   1297 	 * to the MAC registers.
   1298 	 */
   1299 	PAGE(sc, 0);
   1300 }
   1301