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