Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.36
      1  1.36  jonathan /*	$NetBSD: elink3.c,v 1.36 1997/10/18 00:15:02 jonathan Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*
      4  1.19  jonathan  * Copyright (c) 1996, 1997 Jonathan Stone <jonathan (at) NetBSD.org>
      5   1.6   thorpej  * Copyright (c) 1994 Herb Peyerl <hpeyerl (at) beer.org>
      6   1.1   thorpej  * All rights reserved.
      7   1.1   thorpej  *
      8   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
      9   1.1   thorpej  * modification, are permitted provided that the following conditions
     10   1.1   thorpej  * are met:
     11   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     12   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     13   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     15   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     16   1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     17   1.1   thorpej  *    must display the following acknowledgement:
     18   1.1   thorpej  *      This product includes software developed by Herb Peyerl.
     19   1.1   thorpej  * 4. The name of Herb Peyerl may not be used to endorse or promote products
     20   1.1   thorpej  *    derived from this software without specific prior written permission.
     21   1.1   thorpej  *
     22   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1   thorpej  */
     33   1.1   thorpej 
     34   1.1   thorpej #include "bpfilter.h"
     35  1.35  explorer #include "rnd.h"
     36   1.1   thorpej 
     37   1.1   thorpej #include <sys/param.h>
     38   1.3  christos #include <sys/systm.h>
     39   1.1   thorpej #include <sys/mbuf.h>
     40   1.1   thorpej #include <sys/socket.h>
     41   1.1   thorpej #include <sys/ioctl.h>
     42   1.1   thorpej #include <sys/errno.h>
     43   1.1   thorpej #include <sys/syslog.h>
     44   1.1   thorpej #include <sys/select.h>
     45   1.1   thorpej #include <sys/device.h>
     46  1.35  explorer #if NRND > 0
     47  1.35  explorer #include <sys/rnd.h>
     48  1.35  explorer #endif
     49   1.1   thorpej 
     50   1.1   thorpej #include <net/if.h>
     51   1.1   thorpej #include <net/if_dl.h>
     52  1.21        is #include <net/if_ether.h>
     53  1.22  jonathan #include <net/if_media.h>
     54   1.1   thorpej 
     55   1.1   thorpej #ifdef INET
     56   1.1   thorpej #include <netinet/in.h>
     57   1.1   thorpej #include <netinet/in_systm.h>
     58   1.1   thorpej #include <netinet/in_var.h>
     59   1.1   thorpej #include <netinet/ip.h>
     60  1.21        is #include <netinet/if_inarp.h>
     61   1.1   thorpej #endif
     62   1.1   thorpej 
     63   1.1   thorpej #ifdef NS
     64   1.1   thorpej #include <netns/ns.h>
     65   1.1   thorpej #include <netns/ns_if.h>
     66   1.1   thorpej #endif
     67   1.1   thorpej 
     68   1.1   thorpej #if NBPFILTER > 0
     69   1.1   thorpej #include <net/bpf.h>
     70   1.1   thorpej #include <net/bpfdesc.h>
     71   1.1   thorpej #endif
     72   1.1   thorpej 
     73   1.1   thorpej #include <machine/cpu.h>
     74   1.2   thorpej #include <machine/bus.h>
     75   1.7   thorpej #include <machine/intr.h>
     76   1.1   thorpej 
     77   1.1   thorpej #include <dev/ic/elink3var.h>
     78   1.1   thorpej #include <dev/ic/elink3reg.h>
     79   1.1   thorpej 
     80   1.1   thorpej #define ETHER_MIN_LEN	64
     81   1.1   thorpej #define ETHER_MAX_LEN   1518
     82   1.1   thorpej #define ETHER_ADDR_LEN  6
     83   1.1   thorpej 
     84  1.36  jonathan #ifdef DEBUG
     85  1.36  jonathan int epdebug = 0;
     86  1.36  jonathan #endif
     87  1.36  jonathan 
     88  1.23  jonathan /*
     89  1.23  jonathan  * Structure to map  media-present bits in boards to
     90  1.23  jonathan  * ifmedia codes and printable media names. Used for table-driven
     91  1.23  jonathan  * ifmedia initialization.
     92  1.23  jonathan  */
     93  1.23  jonathan struct ep_media {
     94  1.23  jonathan 	int	epm_eeprom_data;	/* bitmask for eeprom config */
     95  1.23  jonathan 	int	epm_conn;		/* sc->ep_connectors code for medium */
     96  1.23  jonathan 	char*	epm_name;		/* name of medium */
     97  1.23  jonathan 	int	epm_ifmedia;		/* ifmedia word for medium */
     98  1.23  jonathan 	int	epm_ifdata;
     99  1.23  jonathan };
    100  1.23  jonathan 
    101  1.23  jonathan /*
    102  1.23  jonathan  * ep_media table for Vortex/Demon/Boomerang:
    103  1.23  jonathan  * map from media-present bits in register RESET_OPTIONS+2
    104  1.23  jonathan  * to  ifmedia "media words" and printable names.
    105  1.23  jonathan  *
    106  1.23  jonathan  * XXX indexed directly by INTERNAL_CONFIG default_media field,
    107  1.23  jonathan  * (i.e., EPMEDIA_ constants)  forcing order of entries.
    108  1.23  jonathan  *  Note that 3 is reserved.
    109  1.23  jonathan  */
    110  1.23  jonathan struct ep_media ep_vortex_media[8] = {
    111  1.23  jonathan   { EP_PCI_UTP,        EPC_UTP, "utp",	    IFM_ETHER|IFM_10_T,
    112  1.23  jonathan        EPMEDIA_10BASE_T },
    113  1.23  jonathan   { EP_PCI_AUI,        EPC_AUI, "aui",	    IFM_ETHER|IFM_10_5,
    114  1.23  jonathan        EPMEDIA_AUI },
    115  1.23  jonathan   { 0,                 0,  	"reserved", IFM_NONE,  EPMEDIA_RESV1 },
    116  1.23  jonathan   { EP_PCI_BNC,        EPC_BNC, "bnc",	    IFM_ETHER|IFM_10_2,
    117  1.23  jonathan        EPMEDIA_10BASE_2 },
    118  1.23  jonathan   { EP_PCI_100BASE_TX, EPC_100TX, "100-TX", IFM_ETHER|IFM_100_TX,
    119  1.23  jonathan        EPMEDIA_100BASE_TX },
    120  1.23  jonathan   { EP_PCI_100BASE_FX, EPC_100FX, "100-FX", IFM_ETHER|IFM_100_FX,
    121  1.23  jonathan        EPMEDIA_100BASE_FX },
    122  1.23  jonathan   { EP_PCI_100BASE_MII,EPC_MII,   "mii",    IFM_ETHER|IFM_100_TX,
    123  1.23  jonathan        EPMEDIA_MII },
    124  1.23  jonathan   { EP_PCI_100BASE_T4, EPC_100T4, "100-T4", IFM_ETHER|IFM_100_T4,
    125  1.23  jonathan        EPMEDIA_100BASE_T4 }
    126  1.23  jonathan };
    127  1.23  jonathan 
    128  1.23  jonathan /*
    129  1.23  jonathan  * ep_media table for 3c509/3c509b/3c579/3c589:
    130  1.23  jonathan  * map from media-present bits in register CNFG_CNTRL
    131  1.23  jonathan  * (window 0, offset ?) to  ifmedia "media words" and printable names.
    132  1.23  jonathan  */
    133  1.23  jonathan struct ep_media ep_isa_media[3] = {
    134  1.23  jonathan   { EP_W0_CC_UTP,  EPC_UTP, "utp",   IFM_ETHER|IFM_10_T, EPMEDIA_10BASE_T },
    135  1.23  jonathan   { EP_W0_CC_AUI,  EPC_AUI, "aui",   IFM_ETHER|IFM_10_5, EPMEDIA_AUI },
    136  1.23  jonathan   { EP_W0_CC_BNC,  EPC_BNC, "bnc",   IFM_ETHER|IFM_10_2, EPMEDIA_10BASE_2 },
    137  1.23  jonathan };
    138  1.23  jonathan 
    139  1.23  jonathan /* Map vortex reset_options bits to if_media codes. */
    140  1.23  jonathan const u_int ep_default_to_media[8] = {
    141  1.23  jonathan 	IFM_ETHER | IFM_10_T,
    142  1.23  jonathan 	IFM_ETHER | IFM_10_5,
    143  1.23  jonathan 	0, 			/* reserved by 3Com */
    144  1.23  jonathan 	IFM_ETHER | IFM_10_2,
    145  1.23  jonathan 	IFM_ETHER | IFM_100_TX,
    146  1.23  jonathan 	IFM_ETHER | IFM_100_FX,
    147  1.23  jonathan 	IFM_ETHER | IFM_100_TX,	/* XXX really MII: need to talk to PHY */
    148  1.23  jonathan 	IFM_ETHER | IFM_100_T4,
    149  1.23  jonathan };
    150  1.23  jonathan 
    151  1.23  jonathan /* Autoconfig defintion of driver back-end */
    152   1.1   thorpej struct cfdriver ep_cd = {
    153   1.1   thorpej 	NULL, "ep", DV_IFNET
    154   1.1   thorpej };
    155   1.1   thorpej 
    156  1.23  jonathan 
    157  1.15  jonathan void	ep_internalconfig __P((struct ep_softc *sc));
    158  1.20  jonathan void	ep_vortex_probemedia __P((struct ep_softc *sc));
    159  1.23  jonathan void	ep_isa_probemedia __P((struct ep_softc *sc));
    160  1.20  jonathan 
    161   1.3  christos static void eptxstat __P((struct ep_softc *));
    162   1.1   thorpej static int epstatus __P((struct ep_softc *));
    163   1.1   thorpej void epinit __P((struct ep_softc *));
    164   1.1   thorpej int epioctl __P((struct ifnet *, u_long, caddr_t));
    165   1.1   thorpej void epstart __P((struct ifnet *));
    166   1.5   thorpej void epwatchdog __P((struct ifnet *));
    167   1.1   thorpej void epreset __P((struct ep_softc *));
    168  1.16  jonathan static void epshutdown __P((void *));
    169  1.23  jonathan void	epread __P((struct ep_softc *));
    170   1.1   thorpej struct mbuf *epget __P((struct ep_softc *, int));
    171  1.23  jonathan void	epmbuffill __P((void *));
    172  1.23  jonathan void	epmbufempty __P((struct ep_softc *));
    173  1.23  jonathan void	epsetfilter __P((struct ep_softc *));
    174  1.34   thorpej void	epsetmedia __P((struct ep_softc *, int epmedium));
    175  1.34   thorpej 
    176  1.34   thorpej int	epenable __P((struct ep_softc *));
    177  1.34   thorpej void	epdisable __P((struct ep_softc *));
    178  1.23  jonathan 
    179  1.23  jonathan /* ifmedia callbacks */
    180  1.23  jonathan int	ep_media_change __P((struct ifnet *ifp));
    181  1.23  jonathan void	ep_media_status __P((struct ifnet *ifp, struct ifmediareq *req));
    182   1.1   thorpej 
    183   1.1   thorpej static int epbusyeeprom __P((struct ep_softc *));
    184  1.19  jonathan static inline void ep_complete_cmd __P((struct ep_softc *sc,
    185  1.19  jonathan 					u_int cmd, u_int arg));
    186  1.19  jonathan 
    187  1.19  jonathan 
    188  1.19  jonathan /*
    189  1.19  jonathan  * Issue a (reset) command, and be sure it has completed.
    190  1.19  jonathan  * Used for commands that reset part or all of the  board.
    191  1.19  jonathan  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
    192  1.19  jonathan  * but older hardware doesn't implement it and we must delay.
    193  1.19  jonathan  * It's easiest to just delay always.
    194  1.19  jonathan  */
    195  1.19  jonathan static inline void
    196  1.19  jonathan ep_complete_cmd(sc, cmd, arg)
    197  1.19  jonathan 	struct ep_softc *sc;
    198  1.19  jonathan 	u_int cmd, arg;
    199  1.19  jonathan {
    200  1.19  jonathan 	register bus_space_tag_t iot = sc->sc_iot;
    201  1.19  jonathan 	register bus_space_handle_t ioh = sc->sc_ioh;
    202  1.19  jonathan 
    203  1.19  jonathan 	bus_space_write_2(iot, ioh, cmd, arg);
    204  1.19  jonathan 
    205  1.19  jonathan #ifdef notyet
    206  1.19  jonathan 	/* if this adapter family has S_COMMAND_IN_PROGRESS, use it */
    207  1.19  jonathan 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    208  1.19  jonathan 		;
    209  1.19  jonathan 	else
    210  1.19  jonathan #else
    211  1.19  jonathan 	DELAY(100000);	/* need at least 1 ms, but be generous. */
    212  1.19  jonathan #endif
    213  1.19  jonathan }
    214  1.19  jonathan 
    215  1.20  jonathan /*
    216  1.20  jonathan  * Back-end attach and configure.
    217  1.20  jonathan  */
    218   1.1   thorpej void
    219  1.34   thorpej epconfig(sc, chipset, enaddr)
    220   1.1   thorpej 	struct ep_softc *sc;
    221  1.20  jonathan 	u_short chipset;
    222  1.34   thorpej 	u_int8_t *enaddr;
    223   1.1   thorpej {
    224  1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    225  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    226  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    227   1.7   thorpej 	u_int16_t i;
    228  1.21        is 	u_int8_t myla[6];
    229   1.1   thorpej 
    230  1.20  jonathan 	sc->ep_chipset = chipset;
    231  1.32   thorpej 
    232  1.32   thorpej 	/*
    233  1.32   thorpej 	 * We could have been groveling around in other register
    234  1.32   thorpej 	 * windows in the front-end; make sure we're in window 0
    235  1.32   thorpej 	 * to read the EEPROM.
    236  1.32   thorpej 	 */
    237  1.32   thorpej 	GO_WINDOW(0);
    238   1.1   thorpej 
    239  1.34   thorpej 	if (enaddr == NULL) {
    240  1.34   thorpej 		/*
    241  1.34   thorpej 		 * Read the station address from the eeprom
    242  1.34   thorpej 		 */
    243  1.34   thorpej 		for (i = 0; i < 3; i++) {
    244  1.34   thorpej 			u_int16_t x;
    245  1.34   thorpej 			if (epbusyeeprom(sc))
    246  1.34   thorpej 				return;		/* XXX why is eeprom busy? */
    247  1.34   thorpej 			bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    248  1.34   thorpej 					  READ_EEPROM | i);
    249  1.34   thorpej 			if (epbusyeeprom(sc))
    250  1.34   thorpej 				return;		/* XXX why is eeprom busy? */
    251  1.34   thorpej 			x = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    252  1.34   thorpej 			myla[(i << 1)] = x >> 8;
    253  1.34   thorpej 			myla[(i << 1) + 1] = x;
    254  1.34   thorpej 		}
    255  1.34   thorpej 		enaddr = myla;
    256   1.1   thorpej 	}
    257   1.1   thorpej 
    258  1.23  jonathan 	printf("%s: MAC address %s\n", sc->sc_dev.dv_xname,
    259  1.34   thorpej 	    ether_sprintf(enaddr));
    260   1.1   thorpej 
    261  1.12  jonathan 	/*
    262  1.23  jonathan 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900,3c515?) cards
    263  1.23  jonathan 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
    264  1.23  jonathan 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
    265  1.23  jonathan 	 * packet length.
    266  1.12  jonathan 	 * Commands to these cards implicitly upshift a packet size
    267  1.12  jonathan 	 * or threshold by 2 bits.
    268  1.12  jonathan 	 * To detect  cards with large-packet support, we probe by setting
    269  1.12  jonathan 	 * the transmit threshold register, then change windows and
    270  1.12  jonathan 	 * read back the threshold register directly, and see if the
    271  1.12  jonathan 	 * threshold value was shifted or not.
    272  1.12  jonathan 	 */
    273  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND,
    274  1.13  jonathan 			  SET_TX_AVAIL_THRESH | EP_LARGEWIN_PROBE );
    275  1.12  jonathan 	GO_WINDOW(5);
    276  1.12  jonathan 	i = bus_space_read_2(iot, ioh, EP_W5_TX_AVAIL_THRESH);
    277  1.12  jonathan 	GO_WINDOW(1);
    278  1.12  jonathan 	switch (i)  {
    279  1.13  jonathan 	case EP_LARGEWIN_PROBE:
    280  1.13  jonathan 	case (EP_LARGEWIN_PROBE & EP_LARGEWIN_MASK):
    281  1.12  jonathan 		sc->ep_pktlenshift = 0;
    282  1.12  jonathan 		break;
    283  1.12  jonathan 
    284  1.13  jonathan 	case (EP_LARGEWIN_PROBE << 2):
    285  1.12  jonathan 		sc->ep_pktlenshift = 2;
    286  1.23  jonathan 		/* XXX does the 3c515 support Vortex-style RESET_OPTIONS? */
    287  1.12  jonathan 		break;
    288  1.12  jonathan 
    289  1.12  jonathan 	default:
    290  1.34   thorpej 		printf("%s: wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
    291  1.14       cjs 		    "Interface disabled\n",
    292  1.34   thorpej 		    sc->sc_dev.dv_xname, EP_LARGEWIN_PROBE, (int) i);
    293  1.12  jonathan 		return;
    294  1.12  jonathan 	}
    295  1.20  jonathan 
    296  1.12  jonathan 	/*
    297  1.12  jonathan 	 * Ensure Tx-available interrupts are enabled for
    298  1.12  jonathan 	 * start the interface.
    299  1.23  jonathan 	 * XXX should be in epinit()?
    300  1.12  jonathan 	 */
    301  1.12  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND,
    302  1.12  jonathan 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    303  1.12  jonathan 
    304  1.23  jonathan 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    305  1.23  jonathan 	ifp->if_softc = sc;
    306  1.23  jonathan 	ifp->if_start = epstart;
    307  1.23  jonathan 	ifp->if_ioctl = epioctl;
    308  1.23  jonathan 	ifp->if_watchdog = epwatchdog;
    309  1.23  jonathan 	ifp->if_flags =
    310  1.23  jonathan 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    311  1.23  jonathan 
    312  1.23  jonathan 	if_attach(ifp);
    313  1.34   thorpej 	ether_ifattach(ifp, enaddr);
    314  1.23  jonathan 
    315  1.23  jonathan 	/*
    316  1.23  jonathan 	 * Finish configuration:
    317  1.23  jonathan 	 * determine chipset if the front-end couldn't do so,
    318  1.23  jonathan 	 * show board details, set media.
    319  1.23  jonathan 	 */
    320  1.23  jonathan 
    321  1.23  jonathan 	/* print RAM size */
    322  1.23  jonathan 	ep_internalconfig(sc);
    323  1.23  jonathan 	GO_WINDOW(0);
    324  1.23  jonathan 
    325  1.23  jonathan 	ifmedia_init(&sc->sc_media, 0, ep_media_change, ep_media_status);
    326  1.20  jonathan 
    327  1.34   thorpej 	/*
    328  1.34   thorpej 	 * If we've got an indirect (ISA) board, the chipset is
    329  1.34   thorpej 	 * unknown.  If the board has large-packet support, it's a
    330  1.34   thorpej 	 * Vortex/Boomerang, otherwise it's a 3c509.  XXX use eeprom
    331  1.34   thorpej 	 * capability word instead?
    332  1.20  jonathan 	 */
    333  1.34   thorpej 
    334  1.23  jonathan 	if (sc->ep_chipset == EP_CHIPSET_UNKNOWN && sc->ep_pktlenshift)  {
    335  1.23  jonathan 		printf("warning: unknown chipset, possibly 3c515?\n");
    336  1.23  jonathan #ifdef notyet
    337  1.20  jonathan 		sc->sc_chipset = EP_CHIPSET_VORTEX;
    338  1.23  jonathan #endif	/* notyet */
    339  1.20  jonathan 	}
    340  1.20  jonathan 
    341  1.20  jonathan 	/*
    342  1.23  jonathan 	 * Ascertain which media types are present and inform ifmedia.
    343  1.20  jonathan 	 */
    344  1.20  jonathan 	switch (sc->ep_chipset) {
    345  1.20  jonathan 	/* on a direct bus, the attach routine can tell, but check anyway. */
    346  1.20  jonathan 	case EP_CHIPSET_VORTEX:
    347  1.20  jonathan 	case EP_CHIPSET_BOOMERANG2:
    348  1.20  jonathan 		ep_vortex_probemedia(sc);
    349  1.20  jonathan 		break;
    350  1.20  jonathan 
    351  1.20  jonathan 	/* on ISA we can't yet tell 3c509 from 3c515. Assume the former. */
    352  1.20  jonathan 	case EP_CHIPSET_3C509:
    353  1.20  jonathan 	default:
    354  1.23  jonathan 		ep_isa_probemedia(sc);
    355  1.20  jonathan 		break;
    356  1.20  jonathan 	}
    357  1.23  jonathan 
    358  1.20  jonathan 	GO_WINDOW(1);		/* Window 1 is operating window */
    359  1.20  jonathan 
    360   1.1   thorpej #if NBPFILTER > 0
    361  1.21        is 	bpfattach(&sc->sc_ethercom.ec_if.if_bpf, ifp, DLT_EN10MB,
    362   1.1   thorpej 		  sizeof(struct ether_header));
    363   1.1   thorpej #endif
    364   1.1   thorpej 
    365  1.35  explorer #if NRND > 0
    366  1.35  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
    367  1.35  explorer #endif
    368  1.35  explorer 
    369   1.1   thorpej 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    370  1.12  jonathan 
    371  1.16  jonathan 	/*  Establish callback to reset card when we reboot. */
    372  1.16  jonathan 	shutdownhook_establish(epshutdown, sc);
    373  1.16  jonathan 
    374  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    375  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    376   1.1   thorpej }
    377   1.1   thorpej 
    378  1.23  jonathan 
    379   1.1   thorpej /*
    380  1.15  jonathan  * Show interface-model-independent info from window 3
    381  1.15  jonathan  * internal-configuration register.
    382  1.15  jonathan  */
    383  1.15  jonathan void
    384  1.15  jonathan ep_internalconfig(sc)
    385  1.15  jonathan 	struct ep_softc *sc;
    386  1.15  jonathan {
    387  1.15  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    388  1.15  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    389  1.15  jonathan 
    390  1.15  jonathan 	u_int config0;
    391  1.15  jonathan 	u_int config1;
    392  1.15  jonathan 
    393  1.15  jonathan 	int  ram_size, ram_width, ram_speed, rom_size, ram_split;
    394  1.15  jonathan 	/*
    395  1.15  jonathan 	 * NVRAM buffer Rx:Tx config names for busmastering cards
    396  1.15  jonathan 	 * (Demon, Vortex, and later).
    397  1.15  jonathan 	 */
    398  1.15  jonathan 	const char *onboard_ram_config[] = {
    399  1.15  jonathan 		"5:3", "3:1", "1:1", "(undefined)" };
    400  1.15  jonathan 
    401  1.15  jonathan 	GO_WINDOW(3);
    402  1.15  jonathan 	config0 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG);
    403  1.28     veego 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
    404  1.15  jonathan 	GO_WINDOW(0);
    405  1.15  jonathan 
    406  1.15  jonathan 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
    407  1.15  jonathan 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
    408  1.15  jonathan 	ram_speed = (config0 & CONFIG_RAMSPEED) >> CONFIG_RAMSPEED_SHIFT;
    409  1.15  jonathan 	rom_size  = (config0 & CONFIG_ROMSIZE) >> CONFIG_ROMSIZE_SHIFT;
    410  1.15  jonathan 
    411  1.15  jonathan 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
    412  1.15  jonathan 
    413  1.23  jonathan 	printf("%s: %dKB %s-wide FIFO, %s Rx:Tx split, ",
    414  1.23  jonathan 	       sc->sc_dev.dv_xname,
    415  1.23  jonathan 	       8 << ram_size,
    416  1.23  jonathan 	       (ram_width) ? "word" : "byte",
    417  1.23  jonathan 	       onboard_ram_config[ram_split]);
    418  1.15  jonathan }
    419  1.15  jonathan 
    420  1.23  jonathan 
    421  1.20  jonathan /*
    422  1.23  jonathan  * Find supported media on 3c509-generation hardware that doesn't have
    423  1.20  jonathan  * a "reset_options" register in window 3.
    424  1.23  jonathan  * Use the config_cntrl register  in window 0 instead.
    425  1.23  jonathan  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
    426  1.23  jonathan  * that implement  CONFIG_CTRL.  We don't have a good way to set the
    427  1.23  jonathan  * default active mediuim; punt to ifconfig  instead.
    428  1.23  jonathan  *
    429  1.23  jonathan  * XXX what about 3c515, pcmcia 10/100?
    430  1.20  jonathan  */
    431  1.20  jonathan void
    432  1.23  jonathan ep_isa_probemedia(sc)
    433  1.20  jonathan 	struct ep_softc *sc;
    434  1.20  jonathan {
    435  1.20  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    436  1.20  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    437  1.23  jonathan 	struct ifmedia *ifm = &sc->sc_media;
    438  1.23  jonathan 	int	conn, i;
    439  1.23  jonathan 	u_int16_t ep_w0_config, port;
    440  1.23  jonathan 
    441  1.23  jonathan 	conn = 0;
    442  1.20  jonathan 	GO_WINDOW(0);
    443  1.23  jonathan 	ep_w0_config = bus_space_read_2(iot, ioh, EP_W0_CONFIG_CTRL);
    444  1.23  jonathan 	for (i = 0; i < 3; i++) {
    445  1.23  jonathan 		struct ep_media * epm = ep_isa_media + i;
    446  1.23  jonathan 
    447  1.23  jonathan 		if ((ep_w0_config & epm->epm_eeprom_data) != 0) {
    448  1.23  jonathan 
    449  1.23  jonathan 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
    450  1.23  jonathan 			if (conn)
    451  1.23  jonathan 				printf("/");
    452  1.23  jonathan 			printf(epm->epm_name);
    453  1.23  jonathan 			conn |= epm->epm_conn;
    454  1.23  jonathan 		}
    455  1.23  jonathan 	}
    456  1.23  jonathan 	sc->ep_connectors = conn;
    457  1.23  jonathan 
    458  1.23  jonathan 	/* get default medium from EEPROM */
    459  1.23  jonathan 	if (epbusyeeprom(sc))
    460  1.23  jonathan 		return;		/* XXX why is eeprom busy? */
    461  1.23  jonathan 	bus_space_write_2(iot, ioh, EP_W0_EEPROM_COMMAND,
    462  1.28     veego 	    READ_EEPROM | EEPROM_ADDR_CFG);
    463  1.23  jonathan 	if (epbusyeeprom(sc))
    464  1.23  jonathan 		return;		/* XXX why is  eeprom busy? */
    465  1.23  jonathan 	port = bus_space_read_2(iot, ioh, EP_W0_EEPROM_DATA);
    466  1.23  jonathan 	port = port >> 14;
    467  1.23  jonathan 
    468  1.23  jonathan 	printf(" (default %s)\n", ep_vortex_media[port].epm_name);
    469  1.23  jonathan 	/* tell ifconfig what currently-active media is. */
    470  1.23  jonathan 	ifmedia_set(ifm, ep_default_to_media[port]);
    471  1.23  jonathan 
    472  1.23  jonathan 	/* XXX autoselect not yet implemented */
    473  1.20  jonathan }
    474  1.20  jonathan 
    475  1.15  jonathan 
    476  1.15  jonathan /*
    477  1.23  jonathan  * Find media present on large-packet-capable elink3 devices.
    478  1.23  jonathan  * Show onboard configuration of large-packet-capable elink3 devices
    479  1.23  jonathan  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
    480  1.23  jonathan  * Use media and card-version info in window 3 instead.
    481  1.15  jonathan  *
    482  1.29  jonathan  * XXX how much of this works with 3c515, pcmcia 10/100?
    483  1.15  jonathan  */
    484  1.15  jonathan void
    485  1.20  jonathan ep_vortex_probemedia(sc)
    486  1.15  jonathan 	struct ep_softc *sc;
    487  1.15  jonathan {
    488  1.15  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    489  1.15  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    490  1.23  jonathan 	struct ifmedia *ifm = &sc->sc_media;
    491  1.28     veego 	u_int config1, conn;
    492  1.15  jonathan 	int reset_options;
    493  1.28     veego 	int default_media;	/* 3-bit encoding of default (EEPROM) media */
    494  1.28     veego 	int autoselect;		/* boolean: should default to autoselect */
    495  1.15  jonathan 	const char *medium_name;
    496  1.23  jonathan 	register int i;
    497  1.15  jonathan 
    498  1.15  jonathan 	GO_WINDOW(3);
    499  1.28     veego 	config1 = (u_int)bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
    500  1.15  jonathan 	reset_options  = (int)bus_space_read_1(iot, ioh, EP_W3_RESET_OPTIONS);
    501  1.15  jonathan 	GO_WINDOW(0);
    502  1.15  jonathan 
    503  1.23  jonathan 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    504  1.15  jonathan         autoselect = (config1 & CONFIG_AUTOSELECT) >> CONFIG_AUTOSELECT_SHIFT;
    505  1.15  jonathan 
    506  1.23  jonathan 	/* set available media options */
    507  1.29  jonathan 	conn = 0;
    508  1.23  jonathan 	for (i = 0; i < 8; i++) {
    509  1.23  jonathan 		struct ep_media * epm = ep_vortex_media + i;
    510  1.23  jonathan 
    511  1.23  jonathan 		if ((reset_options & epm->epm_eeprom_data) != 0) {
    512  1.23  jonathan 			if (conn) printf("/");
    513  1.23  jonathan 			printf(epm->epm_name);
    514  1.23  jonathan 			conn |= epm->epm_conn;
    515  1.23  jonathan 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_ifdata, 0);
    516  1.23  jonathan 		}
    517  1.23  jonathan 	}
    518  1.15  jonathan 
    519  1.23  jonathan 	sc->ep_connectors = conn;
    520  1.15  jonathan 
    521  1.23  jonathan 	/* Show  eeprom's idea of default media.  */
    522  1.23  jonathan 	medium_name = (default_media > 8)
    523  1.23  jonathan 		? "(unknown/impossible media)"
    524  1.23  jonathan 		: ep_vortex_media[default_media].epm_name;
    525  1.23  jonathan 	printf(" default %s%s\n",
    526  1.23  jonathan 	       medium_name,  (autoselect)? ", autoselect" : "" );
    527  1.31  jonathan 
    528  1.23  jonathan #ifdef notyet
    529  1.23  jonathan 	/*
    530  1.23  jonathan 	 * Set default: either the active interface the card
    531  1.23  jonathan 	 * reads  from the EEPROM, or if autoselect is true,
    532  1.23  jonathan 	 * whatever we find is actually connected.
    533  1.23  jonathan 	 *
    534  1.23  jonathan 	 * XXX autoselect not yet implemented.
    535  1.23  jonathan 	 */
    536  1.23  jonathan #endif	/* notyet */
    537  1.15  jonathan 
    538  1.23  jonathan 	/* tell ifconfig what currently-active media is. */
    539  1.23  jonathan 	ifmedia_set(ifm, ep_default_to_media[default_media]);
    540  1.15  jonathan }
    541  1.15  jonathan 
    542  1.20  jonathan 
    543  1.15  jonathan /*
    544  1.20  jonathan  * Bring device up.
    545  1.20  jonathan  *
    546   1.1   thorpej  * The order in here seems important. Otherwise we may not receive
    547   1.1   thorpej  * interrupts. ?!
    548   1.1   thorpej  */
    549   1.1   thorpej void
    550   1.1   thorpej epinit(sc)
    551   1.1   thorpej 	register struct ep_softc *sc;
    552   1.1   thorpej {
    553  1.21        is 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    554  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    555  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    556   1.1   thorpej 	int i;
    557   1.1   thorpej 
    558  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
    559   1.1   thorpej 		;
    560   1.1   thorpej 
    561   1.1   thorpej 	if (sc->bustype != EP_BUS_PCI) {
    562   1.1   thorpej 		GO_WINDOW(0);
    563  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, 0);
    564  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
    565   1.1   thorpej 	}
    566   1.1   thorpej 
    567   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
    568  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_W0_RESOURCE_CFG, 0x3f00);
    569   1.1   thorpej 	}
    570   1.1   thorpej 
    571   1.1   thorpej 	GO_WINDOW(2);
    572   1.1   thorpej 	for (i = 0; i < 6; i++)	/* Reload the ether_addr. */
    573  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W2_ADDR_0 + i,
    574  1.21        is 		    LLADDR(ifp->if_sadl)[i]);
    575   1.8  christos 
    576  1.12  jonathan 	/*
    577  1.12  jonathan 	 * Reset the station-address receive filter.
    578  1.12  jonathan 	 * A bug workaround for busmastering  (Vortex, Demon) cards.
    579  1.12  jonathan 	 */
    580  1.12  jonathan 	for (i = 0; i < 6; i++)
    581  1.12  jonathan 		bus_space_write_1(iot, ioh, EP_W2_RECVMASK_0 + i, 0);
    582   1.1   thorpej 
    583  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
    584  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
    585   1.1   thorpej 
    586   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    587   1.1   thorpej 	for (i = 0; i < 31; i++)
    588  1.11   thorpej 		bus_space_read_1(iot, ioh, EP_W1_TX_STATUS);
    589  1.31  jonathan 
    590  1.31  jonathan 	/* Set threshhold for for Tx-space avaiable interrupt. */
    591  1.31  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND,
    592  1.31  jonathan 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    593   1.1   thorpej 
    594  1.18  jonathan 	/* Enable interrupts. */
    595  1.28     veego 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK | S_CARD_FAILURE |
    596   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    597  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK | S_CARD_FAILURE |
    598   1.1   thorpej 				S_RX_COMPLETE | S_TX_COMPLETE | S_TX_AVAIL);
    599   1.1   thorpej 
    600   1.1   thorpej 	/*
    601   1.1   thorpej 	 * Attempt to get rid of any stray interrupts that occured during
    602   1.1   thorpej 	 * configuration.  On the i386 this isn't possible because one may
    603   1.1   thorpej 	 * already be queued.  However, a single stray interrupt is
    604   1.1   thorpej 	 * unimportant.
    605   1.1   thorpej 	 */
    606  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR | 0xff);
    607   1.1   thorpej 
    608   1.1   thorpej 	epsetfilter(sc);
    609  1.23  jonathan 	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
    610   1.1   thorpej 
    611  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_ENABLE);
    612  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
    613   1.1   thorpej 
    614   1.1   thorpej 	epmbuffill(sc);
    615   1.1   thorpej 
    616   1.1   thorpej 	/* Interface is now `running', with no output active. */
    617   1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
    618   1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    619   1.1   thorpej 
    620   1.1   thorpej 	/* Attempt to start output, if any. */
    621   1.1   thorpej 	epstart(ifp);
    622   1.1   thorpej }
    623   1.1   thorpej 
    624  1.20  jonathan 
    625  1.20  jonathan /*
    626  1.20  jonathan  * Set multicast receive filter.
    627  1.20  jonathan  * elink3 hardware has no selective multicast filter in hardware.
    628  1.20  jonathan  * Enable reception of all multicasts and filter in software.
    629  1.20  jonathan  */
    630   1.1   thorpej void
    631   1.1   thorpej epsetfilter(sc)
    632   1.1   thorpej 	register struct ep_softc *sc;
    633   1.1   thorpej {
    634  1.21        is 	register struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    635   1.1   thorpej 
    636   1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    637  1.11   thorpej 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, EP_COMMAND, SET_RX_FILTER |
    638   1.1   thorpej 	    FIL_INDIVIDUAL | FIL_BRDCST |
    639   1.1   thorpej 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0 ) |
    640   1.1   thorpej 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0 ));
    641   1.1   thorpej }
    642   1.1   thorpej 
    643  1.20  jonathan 
    644  1.23  jonathan int
    645  1.23  jonathan ep_media_change(ifp)
    646  1.23  jonathan 	struct ifnet *ifp;
    647  1.23  jonathan {
    648  1.23  jonathan 	register struct ep_softc *sc = ifp->if_softc;
    649  1.23  jonathan 
    650  1.34   thorpej 	/*
    651  1.34   thorpej 	 * If the interface is not currently powered on, just return.
    652  1.34   thorpej 	 * When it is enabled later, epinit() will properly set up the
    653  1.34   thorpej 	 * media for us.
    654  1.34   thorpej 	 */
    655  1.34   thorpej 	if (sc->enabled == 0)
    656  1.34   thorpej 		return (0);
    657  1.34   thorpej 
    658  1.34   thorpej 	epsetmedia(sc, sc->sc_media.ifm_cur->ifm_data);
    659  1.34   thorpej 	return (0);
    660  1.23  jonathan }
    661  1.23  jonathan 
    662  1.15  jonathan /*
    663  1.23  jonathan  * Set active media to a specific given EPMEDIA_<> value.
    664  1.23  jonathan  * For vortex/demon/boomerang cards, update media field in w3_internal_config,
    665  1.29  jonathan  *       and power on selected transceiver.
    666  1.29  jonathan  * For 3c509-generation cards (3c509/3c579/3c589/3c509B),
    667  1.29  jonathan  *	update media field in w0_address_config, and power on selected xcvr.
    668  1.15  jonathan  */
    669  1.34   thorpej void
    670  1.23  jonathan epsetmedia(sc, medium)
    671   1.1   thorpej 	register struct ep_softc *sc;
    672  1.23  jonathan 	int medium;
    673   1.1   thorpej {
    674  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    675  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    676  1.23  jonathan 	int w4_media;
    677   1.1   thorpej 
    678   1.1   thorpej 	/*
    679  1.23  jonathan 	 * First, change the media-control bits in EP_W4_MEDIA_TYPE.
    680   1.1   thorpej 	 */
    681  1.23  jonathan 
    682  1.23  jonathan 	 /* Turn everything off.  First turn off linkbeat and UTP. */
    683   1.1   thorpej 	GO_WINDOW(4);
    684  1.23  jonathan 	w4_media = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
    685  1.23  jonathan 	w4_media =  w4_media & ~(ENABLE_UTP|SQE_ENABLE);
    686  1.23  jonathan 	bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE, w4_media);
    687  1.23  jonathan 
    688  1.23  jonathan 	/* Turn off coax */
    689  1.23  jonathan 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
    690  1.23  jonathan 	delay(1000);
    691  1.23  jonathan 
    692  1.29  jonathan 	/*
    693  1.29  jonathan 	 * Now turn on the selected media/transceiver.
    694  1.29  jonathan 	 */
    695  1.29  jonathan 	GO_WINDOW(4);
    696  1.23  jonathan 	switch  (medium) {
    697  1.23  jonathan 	case EPMEDIA_10BASE_T:
    698  1.23  jonathan 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
    699  1.28     veego 		    w4_media | ENABLE_UTP);
    700  1.23  jonathan 		break;
    701  1.23  jonathan 
    702  1.23  jonathan 	case EPMEDIA_10BASE_2:
    703  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, START_TRANSCEIVER);
    704  1.23  jonathan 		DELAY(1000);	/* 50ms not enmough? */
    705  1.23  jonathan 		break;
    706  1.23  jonathan 
    707  1.23  jonathan 	/* XXX following only for new-generation cards */
    708  1.23  jonathan 	case EPMEDIA_100BASE_TX:
    709  1.23  jonathan 	case EPMEDIA_100BASE_FX:
    710  1.23  jonathan 	case EPMEDIA_100BASE_T4:	/* XXX check documentation */
    711  1.28     veego 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
    712  1.28     veego 		    w4_media | LINKBEAT_ENABLE);
    713  1.23  jonathan 		DELAY(1000);	/* not strictly necessary? */
    714  1.23  jonathan 		break;
    715  1.23  jonathan 
    716  1.23  jonathan 	case EPMEDIA_AUI:
    717  1.23  jonathan 		bus_space_write_2(iot, ioh, EP_W4_MEDIA_TYPE,
    718  1.28     veego 		    w4_media | SQE_ENABLE);
    719  1.23  jonathan 		DELAY(1000);	/*  not strictly necessary? */
    720  1.23  jonathan 		break;
    721  1.23  jonathan 	case EPMEDIA_MII:
    722  1.36  jonathan 		/* XXX talk to phy? */
    723  1.36  jonathan 	  	break;
    724  1.23  jonathan 	default:
    725  1.23  jonathan #if defined(DEBUG)
    726  1.29  jonathan 		printf("%s unknown media 0x%x\n", sc->sc_dev.dv_xname, medium);
    727  1.23  jonathan #endif
    728  1.23  jonathan 		break;
    729  1.23  jonathan 
    730   1.1   thorpej 	}
    731  1.23  jonathan 
    732  1.23  jonathan 	/*
    733  1.29  jonathan 	 * Tell the chip which PHY [sic] to use.
    734  1.23  jonathan 	 */
    735  1.23  jonathan 	if  (sc->ep_chipset==EP_CHIPSET_VORTEX	||
    736  1.23  jonathan 	     sc->ep_chipset==EP_CHIPSET_BOOMERANG2) {
    737  1.28     veego 		int config0, config1;
    738  1.23  jonathan 
    739  1.23  jonathan 		GO_WINDOW(3);
    740  1.23  jonathan 		config0 = (u_int)bus_space_read_2(iot, ioh,
    741  1.28     veego 		    EP_W3_INTERNAL_CONFIG);
    742  1.23  jonathan 		config1 = (u_int)bus_space_read_2(iot, ioh,
    743  1.28     veego 		    EP_W3_INTERNAL_CONFIG + 2);
    744  1.23  jonathan 
    745  1.23  jonathan #if defined(DEBUG)
    746  1.36  jonathan 		if (epdebug) {
    747  1.36  jonathan 			printf("%s:  read 0x%x, 0x%x from EP_W3_CONFIG register\n",
    748  1.36  jonathan 			    sc->sc_dev.dv_xname, config0, config1);
    749  1.36  jonathan 		}
    750  1.23  jonathan #endif
    751  1.23  jonathan 		config1 = config1 & ~CONFIG_MEDIAMASK;
    752  1.28     veego 		config1 |= (medium << CONFIG_MEDIAMASK_SHIFT);
    753  1.23  jonathan 
    754  1.23  jonathan #if defined(DEBUG)
    755  1.36  jonathan 		if (epdebug) {
    756  1.36  jonathan 			printf("epsetmedia: %s: medium 0x%x, 0x%x to EP_W3_CONFIG\n",
    757  1.36  jonathan 			    sc->sc_dev.dv_xname, medium, config1);
    758  1.36  jonathan 		}
    759  1.23  jonathan #endif
    760  1.28     veego 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG, config0);
    761  1.28     veego 		bus_space_write_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2, config1);
    762  1.28     veego 	}
    763  1.29  jonathan 	else if (sc->ep_chipset == EP_CHIPSET_3C509) {
    764  1.29  jonathan 		register int w0_addr_cfg;
    765  1.28     veego 
    766  1.28     veego 		GO_WINDOW(0);
    767  1.29  jonathan 		w0_addr_cfg = bus_space_read_2(iot, ioh, EP_W0_ADDRESS_CFG);
    768  1.29  jonathan 		w0_addr_cfg &= 0x3fff;
    769  1.28     veego 		bus_space_write_2(iot, ioh, EP_W0_ADDRESS_CFG,
    770  1.29  jonathan 		    w0_addr_cfg | (medium << 14));
    771  1.28     veego 		DELAY(1000);
    772  1.23  jonathan 	}
    773  1.23  jonathan 
    774  1.23  jonathan 	GO_WINDOW(1);		/* Window 1 is operating window */
    775  1.23  jonathan }
    776  1.23  jonathan 
    777  1.23  jonathan /*
    778  1.23  jonathan  * Get currently-selected media from card.
    779  1.23  jonathan  * (if_media callback, may be called before interface is brought up).
    780  1.23  jonathan  */
    781  1.23  jonathan void
    782  1.23  jonathan ep_media_status(ifp, req)
    783  1.23  jonathan 	struct ifnet *ifp;
    784  1.23  jonathan 	struct ifmediareq *req;
    785  1.23  jonathan {
    786  1.23  jonathan 	register struct ep_softc *sc = ifp->if_softc;
    787  1.23  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    788  1.23  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    789  1.28     veego 	u_int config1;
    790  1.23  jonathan 	u_int ep_mediastatus;
    791  1.23  jonathan 
    792  1.34   thorpej 	if (sc->enabled == 0) {
    793  1.34   thorpej 		req->ifm_active = IFM_ETHER|IFM_NONE;
    794  1.34   thorpej 		req->ifm_status = 0;
    795  1.34   thorpej 		return;
    796  1.34   thorpej 	}
    797  1.34   thorpej 
    798  1.23  jonathan 	/* XXX read from softc when we start autosensing media */
    799  1.23  jonathan 	req->ifm_active = sc->sc_media.ifm_cur->ifm_media;
    800  1.23  jonathan 
    801  1.23  jonathan 	switch (sc->ep_chipset) {
    802  1.23  jonathan 	case EP_CHIPSET_VORTEX:
    803  1.23  jonathan 	case EP_CHIPSET_BOOMERANG:
    804  1.23  jonathan 		GO_WINDOW(3);
    805  1.23  jonathan 		delay(5000);
    806  1.23  jonathan 
    807  1.28     veego 		config1 = bus_space_read_2(iot, ioh, EP_W3_INTERNAL_CONFIG + 2);
    808  1.23  jonathan 		GO_WINDOW(1);
    809  1.23  jonathan 
    810  1.23  jonathan 		config1 =
    811  1.23  jonathan 		    (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    812  1.23  jonathan 		req->ifm_active = ep_default_to_media[config1];
    813  1.23  jonathan 
    814  1.23  jonathan 		/* XXX check full-duplex bits? */
    815  1.23  jonathan 
    816  1.23  jonathan 		GO_WINDOW(4);
    817  1.23  jonathan 		req->ifm_status = IFM_AVALID;	/* XXX */
    818  1.23  jonathan 		ep_mediastatus = bus_space_read_2(iot, ioh, EP_W4_MEDIA_TYPE);
    819  1.23  jonathan 		if (ep_mediastatus & LINKBEAT_DETECT)
    820  1.23  jonathan 			req->ifm_status |= IFM_ACTIVE; 	/* XXX  automedia */
    821  1.23  jonathan 
    822  1.23  jonathan 		break;
    823  1.23  jonathan 
    824  1.23  jonathan 	case EP_CHIPSET_UNKNOWN:
    825  1.23  jonathan 	case EP_CHIPSET_3C509:
    826  1.23  jonathan 		req->ifm_status = 0;	/* XXX */
    827  1.23  jonathan 		break;
    828  1.23  jonathan 
    829  1.23  jonathan 	default:
    830  1.23  jonathan 		printf("%s: media_status on unknown chipset 0x%x\n",
    831  1.23  jonathan 		       ifp->if_xname, sc->ep_chipset);
    832  1.23  jonathan 		break;
    833   1.1   thorpej 	}
    834  1.23  jonathan 
    835  1.23  jonathan 	/* XXX look for softc heartbeat for other chips or media */
    836  1.23  jonathan 
    837   1.1   thorpej 	GO_WINDOW(1);
    838   1.1   thorpej }
    839   1.1   thorpej 
    840  1.23  jonathan 
    841  1.23  jonathan 
    842   1.1   thorpej /*
    843   1.1   thorpej  * Start outputting on the interface.
    844   1.1   thorpej  * Always called as splnet().
    845   1.1   thorpej  */
    846   1.1   thorpej void
    847   1.1   thorpej epstart(ifp)
    848   1.1   thorpej 	struct ifnet *ifp;
    849   1.1   thorpej {
    850   1.5   thorpej 	register struct ep_softc *sc = ifp->if_softc;
    851  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    852  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    853   1.1   thorpej 	struct mbuf *m, *m0;
    854   1.1   thorpej 	int sh, len, pad;
    855   1.1   thorpej 
    856   1.1   thorpej 	/* Don't transmit if interface is busy or not running */
    857  1.28     veego 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    858   1.1   thorpej 		return;
    859   1.1   thorpej 
    860   1.1   thorpej startagain:
    861   1.1   thorpej 	/* Sneak a peek at the next packet */
    862   1.1   thorpej 	m0 = ifp->if_snd.ifq_head;
    863   1.1   thorpej 	if (m0 == 0)
    864   1.1   thorpej 		return;
    865   1.1   thorpej 
    866   1.1   thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
    867   1.1   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
    868   1.1   thorpej 		panic("epstart: no header mbuf");
    869   1.1   thorpej 	len = m0->m_pkthdr.len;
    870   1.1   thorpej 
    871   1.1   thorpej 	pad = (4 - len) & 3;
    872   1.1   thorpej 
    873   1.1   thorpej 	/*
    874   1.1   thorpej 	 * The 3c509 automatically pads short packets to minimum ethernet
    875   1.1   thorpej 	 * length, but we drop packets that are too large. Perhaps we should
    876   1.1   thorpej 	 * truncate them instead?
    877   1.1   thorpej 	 */
    878   1.1   thorpej 	if (len + pad > ETHER_MAX_LEN) {
    879   1.1   thorpej 		/* packet is obviously too large: toss it */
    880   1.1   thorpej 		++ifp->if_oerrors;
    881   1.1   thorpej 		IF_DEQUEUE(&ifp->if_snd, m0);
    882   1.1   thorpej 		m_freem(m0);
    883   1.1   thorpej 		goto readcheck;
    884   1.1   thorpej 	}
    885   1.1   thorpej 
    886  1.11   thorpej 	if (bus_space_read_2(iot, ioh, EP_W1_FREE_TX) < len + pad + 4) {
    887  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    888  1.12  jonathan 		    SET_TX_AVAIL_THRESH |
    889  1.12  jonathan 		    ((len + pad + 4) >> sc->ep_pktlenshift));
    890   1.1   thorpej 		/* not enough room in FIFO */
    891   1.1   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    892   1.1   thorpej 		return;
    893   1.1   thorpej 	} else {
    894  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND,
    895  1.12  jonathan 		    SET_TX_AVAIL_THRESH | EP_THRESH_DISABLE );
    896   1.1   thorpej 	}
    897   1.1   thorpej 
    898   1.1   thorpej 	IF_DEQUEUE(&ifp->if_snd, m0);
    899   1.1   thorpej 	if (m0 == 0)		/* not really needed */
    900   1.1   thorpej 		return;
    901   1.1   thorpej 
    902  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_TX_START_THRESH |
    903  1.12  jonathan 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/) );
    904   1.1   thorpej 
    905   1.1   thorpej #if NBPFILTER > 0
    906   1.1   thorpej 	if (ifp->if_bpf)
    907   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m0);
    908   1.1   thorpej #endif
    909   1.1   thorpej 
    910   1.1   thorpej 	/*
    911   1.1   thorpej 	 * Do the output at splhigh() so that an interrupt from another device
    912   1.1   thorpej 	 * won't cause a FIFO underrun.
    913   1.1   thorpej 	 */
    914   1.1   thorpej 	sh = splhigh();
    915   1.1   thorpej 
    916  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1, len);
    917  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_W1_TX_PIO_WR_1,
    918   1.2   thorpej 	    0xffff);	/* Second dword meaningless */
    919   1.1   thorpej 	if (EP_IS_BUS_32(sc->bustype)) {
    920   1.1   thorpej 		for (m = m0; m; ) {
    921  1.14       cjs 			if (m->m_len > 3)  {
    922  1.14       cjs 				/* align our reads from core */
    923  1.14       cjs 				if (mtod(m, u_long) & 3)  {
    924  1.14       cjs 					u_long count =
    925  1.14       cjs 					    4 - (mtod(m, u_long) & 3);
    926  1.14       cjs 					bus_space_write_multi_1(iot, ioh,
    927  1.14       cjs 					    EP_W1_TX_PIO_WR_1,
    928  1.14       cjs 					    mtod(m, u_int8_t *), count);
    929  1.14       cjs 					m->m_data =
    930  1.14       cjs 					    (void *)(mtod(m, u_long) + count);
    931  1.14       cjs 					m->m_len -= count;
    932  1.14       cjs 				}
    933  1.11   thorpej 				bus_space_write_multi_4(iot, ioh,
    934  1.14       cjs 				    EP_W1_TX_PIO_WR_1,
    935  1.14       cjs 				    mtod(m, u_int32_t *), m->m_len >> 2);
    936  1.14       cjs 				m->m_data = (void *)(mtod(m, u_long) +
    937  1.14       cjs 					(u_long)(m->m_len & ~3));
    938  1.14       cjs 				m->m_len -= m->m_len & ~3;
    939  1.14       cjs 			}
    940  1.14       cjs 			if (m->m_len)  {
    941  1.11   thorpej 				bus_space_write_multi_1(iot, ioh,
    942   1.2   thorpej 				    EP_W1_TX_PIO_WR_1,
    943  1.14       cjs 				    mtod(m, u_int8_t *), m->m_len);
    944  1.14       cjs 			}
    945   1.1   thorpej 			MFREE(m, m0);
    946   1.1   thorpej 			m = m0;
    947   1.1   thorpej 		}
    948   1.1   thorpej 	} else {
    949   1.1   thorpej 		for (m = m0; m; ) {
    950  1.14       cjs 			if (m->m_len > 1)  {
    951  1.14       cjs 				if (mtod(m, u_long) & 1)  {
    952  1.14       cjs 					bus_space_write_1(iot, ioh,
    953  1.14       cjs 					    EP_W1_TX_PIO_WR_1,
    954  1.14       cjs 					    *(mtod(m, u_int8_t *)));
    955  1.14       cjs 					m->m_data =
    956  1.14       cjs 					    (void *)(mtod(m, u_long) + 1);
    957  1.14       cjs 					m->m_len -= 1;
    958  1.14       cjs 				}
    959  1.11   thorpej 				bus_space_write_multi_2(iot, ioh,
    960   1.2   thorpej 				    EP_W1_TX_PIO_WR_1, mtod(m, u_int16_t *),
    961  1.14       cjs 				    m->m_len >> 1);
    962  1.14       cjs 			}
    963  1.14       cjs 			if (m->m_len & 1)  {
    964  1.11   thorpej 				bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1,
    965   1.2   thorpej 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
    966  1.14       cjs 			}
    967   1.1   thorpej 			MFREE(m, m0);
    968   1.1   thorpej 			m = m0;
    969   1.1   thorpej 		}
    970   1.1   thorpej 	}
    971   1.1   thorpej 	while (pad--)
    972  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_PIO_WR_1, 0);
    973   1.1   thorpej 
    974   1.1   thorpej 	splx(sh);
    975   1.1   thorpej 
    976   1.1   thorpej 	++ifp->if_opackets;
    977   1.1   thorpej 
    978   1.1   thorpej readcheck:
    979  1.11   thorpej 	if ((bus_space_read_2(iot, ioh, EP_W1_RX_STATUS) & ERR_INCOMPLETE) == 0) {
    980   1.1   thorpej 		/* We received a complete packet. */
    981  1.11   thorpej 		u_int16_t status = bus_space_read_2(iot, ioh, EP_STATUS);
    982   1.1   thorpej 
    983   1.1   thorpej 		if ((status & S_INTR_LATCH) == 0) {
    984   1.1   thorpej 			/*
    985   1.1   thorpej 			 * No interrupt, read the packet and continue
    986   1.1   thorpej 			 * Is  this supposed to happen? Is my motherboard
    987   1.1   thorpej 			 * completely busted?
    988   1.1   thorpej 			 */
    989   1.1   thorpej 			epread(sc);
    990  1.28     veego 		} else {
    991   1.1   thorpej 			/* Got an interrupt, return so that it gets serviced. */
    992  1.34   thorpej #if 0
    993  1.34   thorpej 			printf("%s: S_INTR_LATCH %04x mask=%04x ipending=%04x (%04x)\n",
    994  1.34   thorpej 			       sc->sc_dev.dv_xname, status,
    995  1.34   thorpej 			       cpl, ipending, imask[IPL_NET]);
    996  1.34   thorpej #endif
    997  1.34   thorpej 
    998   1.1   thorpej 			return;
    999  1.28     veego 		}
   1000  1.28     veego 	} else {
   1001   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
   1002   1.1   thorpej 		if (epstatus(sc)) {
   1003   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
   1004  1.10  christos 				printf("%s: adapter reset\n",
   1005   1.9  christos 				    sc->sc_dev.dv_xname);
   1006   1.1   thorpej 			epreset(sc);
   1007   1.1   thorpej 		}
   1008   1.1   thorpej 	}
   1009   1.1   thorpej 
   1010   1.1   thorpej 	goto startagain;
   1011   1.1   thorpej }
   1012   1.1   thorpej 
   1013   1.1   thorpej 
   1014   1.1   thorpej /*
   1015   1.1   thorpej  * XXX: The 3c509 card can get in a mode where both the fifo status bit
   1016   1.1   thorpej  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
   1017   1.1   thorpej  *	We detect this situation and we reset the adapter.
   1018   1.1   thorpej  *	It happens at times when there is a lot of broadcast traffic
   1019   1.1   thorpej  *	on the cable (once in a blue moon).
   1020   1.1   thorpej  */
   1021   1.1   thorpej static int
   1022   1.1   thorpej epstatus(sc)
   1023   1.1   thorpej 	register struct ep_softc *sc;
   1024   1.1   thorpej {
   1025  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1026  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1027   1.7   thorpej 	u_int16_t fifost;
   1028   1.1   thorpej 
   1029   1.1   thorpej 	/*
   1030   1.1   thorpej 	 * Check the FIFO status and act accordingly
   1031   1.1   thorpej 	 */
   1032   1.1   thorpej 	GO_WINDOW(4);
   1033  1.11   thorpej 	fifost = bus_space_read_2(iot, ioh, EP_W4_FIFO_DIAG);
   1034   1.1   thorpej 	GO_WINDOW(1);
   1035   1.1   thorpej 
   1036   1.1   thorpej 	if (fifost & FIFOS_RX_UNDERRUN) {
   1037  1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1038  1.10  christos 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
   1039   1.1   thorpej 		epreset(sc);
   1040   1.1   thorpej 		return 0;
   1041   1.1   thorpej 	}
   1042   1.1   thorpej 
   1043   1.1   thorpej 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
   1044  1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1045  1.10  christos 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
   1046   1.1   thorpej 		return 1;
   1047   1.1   thorpej 	}
   1048   1.1   thorpej 
   1049   1.1   thorpej 	if (fifost & FIFOS_RX_OVERRUN) {
   1050  1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1051  1.10  christos 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
   1052   1.1   thorpej 		return 1;
   1053   1.1   thorpej 	}
   1054   1.1   thorpej 
   1055   1.1   thorpej 	if (fifost & FIFOS_TX_OVERRUN) {
   1056  1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1057  1.10  christos 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
   1058   1.1   thorpej 		epreset(sc);
   1059   1.1   thorpej 		return 0;
   1060   1.1   thorpej 	}
   1061   1.1   thorpej 
   1062   1.1   thorpej 	return 0;
   1063   1.1   thorpej }
   1064   1.1   thorpej 
   1065   1.1   thorpej 
   1066   1.1   thorpej static void
   1067   1.1   thorpej eptxstat(sc)
   1068   1.1   thorpej 	register struct ep_softc *sc;
   1069   1.1   thorpej {
   1070  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1071  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1072   1.1   thorpej 	int i;
   1073   1.1   thorpej 
   1074   1.1   thorpej 	/*
   1075   1.1   thorpej 	 * We need to read+write TX_STATUS until we get a 0 status
   1076   1.1   thorpej 	 * in order to turn off the interrupt flag.
   1077   1.1   thorpej 	 */
   1078  1.11   thorpej 	while ((i = bus_space_read_1(iot, ioh, EP_W1_TX_STATUS)) & TXS_COMPLETE) {
   1079  1.11   thorpej 		bus_space_write_1(iot, ioh, EP_W1_TX_STATUS, 0x0);
   1080   1.1   thorpej 
   1081   1.1   thorpej 		if (i & TXS_JABBER) {
   1082  1.21        is 			++sc->sc_ethercom.ec_if.if_oerrors;
   1083  1.21        is 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1084  1.10  christos 				printf("%s: jabber (%x)\n",
   1085   1.1   thorpej 				       sc->sc_dev.dv_xname, i);
   1086   1.1   thorpej 			epreset(sc);
   1087   1.1   thorpej 		} else if (i & TXS_UNDERRUN) {
   1088  1.21        is 			++sc->sc_ethercom.ec_if.if_oerrors;
   1089  1.21        is 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1090  1.10  christos 				printf("%s: fifo underrun (%x) @%d\n",
   1091   1.1   thorpej 				       sc->sc_dev.dv_xname, i,
   1092   1.1   thorpej 				       sc->tx_start_thresh);
   1093   1.1   thorpej 			if (sc->tx_succ_ok < 100)
   1094   1.1   thorpej 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
   1095   1.1   thorpej 					    sc->tx_start_thresh + 20);
   1096   1.1   thorpej 			sc->tx_succ_ok = 0;
   1097   1.1   thorpej 			epreset(sc);
   1098   1.1   thorpej 		} else if (i & TXS_MAX_COLLISION) {
   1099  1.21        is 			++sc->sc_ethercom.ec_if.if_collisions;
   1100  1.11   thorpej 			bus_space_write_2(iot, ioh, EP_COMMAND, TX_ENABLE);
   1101  1.21        is 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1102   1.1   thorpej 		} else
   1103   1.1   thorpej 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
   1104   1.1   thorpej 	}
   1105   1.1   thorpej }
   1106   1.1   thorpej 
   1107   1.1   thorpej int
   1108   1.1   thorpej epintr(arg)
   1109   1.1   thorpej 	void *arg;
   1110   1.1   thorpej {
   1111   1.1   thorpej 	register struct ep_softc *sc = arg;
   1112  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1113  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1114  1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1115   1.7   thorpej 	u_int16_t status;
   1116   1.1   thorpej 	int ret = 0;
   1117  1.35  explorer 	int addrandom = 0;
   1118   1.1   thorpej 
   1119  1.34   thorpej 	if (sc->enabled == 0)
   1120  1.34   thorpej 		return (0);
   1121  1.34   thorpej 
   1122   1.1   thorpej 	for (;;) {
   1123  1.11   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1124   1.1   thorpej 
   1125  1.11   thorpej 		status = bus_space_read_2(iot, ioh, EP_STATUS);
   1126   1.1   thorpej 
   1127   1.1   thorpej 		if ((status & (S_TX_COMPLETE | S_TX_AVAIL |
   1128  1.34   thorpej 			       S_RX_COMPLETE | S_CARD_FAILURE)) == 0) {
   1129  1.34   thorpej 			if ((status & S_INTR_LATCH) == 0) {
   1130  1.34   thorpej #if 0
   1131  1.34   thorpej 				printf("%s: intr latch cleared\n",
   1132  1.34   thorpej 				       sc->sc_dev.dv_xname);
   1133  1.34   thorpej #endif
   1134  1.34   thorpej 				break;
   1135  1.34   thorpej 			}
   1136  1.34   thorpej 		}
   1137   1.1   thorpej 
   1138   1.1   thorpej 		ret = 1;
   1139   1.1   thorpej 
   1140   1.1   thorpej 		/*
   1141   1.1   thorpej 		 * Acknowledge any interrupts.  It's important that we do this
   1142   1.1   thorpej 		 * first, since there would otherwise be a race condition.
   1143   1.1   thorpej 		 * Due to the i386 interrupt queueing, we may get spurious
   1144   1.1   thorpej 		 * interrupts occasionally.
   1145   1.1   thorpej 		 */
   1146  1.34   thorpej 		bus_space_write_2(iot, ioh, EP_COMMAND, ACK_INTR |
   1147  1.34   thorpej 				  (status & (C_INTR_LATCH |
   1148  1.34   thorpej 					     C_CARD_FAILURE |
   1149  1.34   thorpej 					     C_TX_COMPLETE |
   1150  1.34   thorpej 					     C_TX_AVAIL |
   1151  1.34   thorpej 					     C_RX_COMPLETE |
   1152  1.34   thorpej 					     C_RX_EARLY |
   1153  1.34   thorpej 					     C_INT_RQD |
   1154  1.34   thorpej 					     C_UPD_STATS)));
   1155  1.34   thorpej 
   1156  1.34   thorpej #if 0
   1157  1.34   thorpej 		status = bus_space_read_2(iot, ioh, EP_STATUS);
   1158  1.34   thorpej 
   1159  1.34   thorpej 		printf("%s: intr%s%s%s%s\n", sc->sc_dev.dv_xname,
   1160  1.34   thorpej 		       (status & S_RX_COMPLETE)?" RX_COMPLETE":"",
   1161  1.34   thorpej 		       (status & S_TX_COMPLETE)?" TX_COMPLETE":"",
   1162  1.34   thorpej 		       (status & S_TX_AVAIL)?" TX_AVAIL":"",
   1163  1.34   thorpej 		       (status & S_CARD_FAILURE)?" CARD_FAILURE":"");
   1164  1.34   thorpej #endif
   1165   1.1   thorpej 
   1166  1.35  explorer 		if (status & S_RX_COMPLETE) {
   1167   1.1   thorpej 			epread(sc);
   1168  1.35  explorer 			addrandom = 1;
   1169  1.35  explorer 		}
   1170   1.1   thorpej 		if (status & S_TX_AVAIL) {
   1171  1.21        is 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1172  1.21        is 			epstart(&sc->sc_ethercom.ec_if);
   1173  1.35  explorer 			addrandom = 1;
   1174   1.1   thorpej 		}
   1175   1.1   thorpej 		if (status & S_CARD_FAILURE) {
   1176  1.10  christos 			printf("%s: adapter failure (%x)\n",
   1177   1.9  christos 			    sc->sc_dev.dv_xname, status);
   1178   1.1   thorpej 			epreset(sc);
   1179   1.1   thorpej 			return (1);
   1180   1.1   thorpej 		}
   1181   1.1   thorpej 		if (status & S_TX_COMPLETE) {
   1182   1.1   thorpej 			eptxstat(sc);
   1183   1.1   thorpej 			epstart(ifp);
   1184  1.35  explorer 			addrandom = 1;
   1185   1.1   thorpej 		}
   1186  1.35  explorer 
   1187  1.35  explorer #if NRND > 0
   1188  1.35  explorer 		if (status)
   1189  1.35  explorer 			rnd_add_uint32(&sc->rnd_source, status);
   1190  1.35  explorer #endif
   1191   1.1   thorpej 	}
   1192   1.1   thorpej 
   1193   1.1   thorpej 	/* no more interrupts */
   1194   1.1   thorpej 	return (ret);
   1195   1.1   thorpej }
   1196   1.1   thorpej 
   1197   1.1   thorpej void
   1198   1.1   thorpej epread(sc)
   1199   1.1   thorpej 	register struct ep_softc *sc;
   1200   1.1   thorpej {
   1201  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1202  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1203  1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1204   1.1   thorpej 	struct mbuf *m;
   1205   1.1   thorpej 	struct ether_header *eh;
   1206   1.1   thorpej 	int len;
   1207   1.1   thorpej 
   1208  1.11   thorpej 	len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
   1209   1.1   thorpej 
   1210   1.1   thorpej again:
   1211   1.1   thorpej 	if (ifp->if_flags & IFF_DEBUG) {
   1212   1.1   thorpej 		int err = len & ERR_MASK;
   1213   1.1   thorpej 		char *s = NULL;
   1214   1.1   thorpej 
   1215   1.1   thorpej 		if (len & ERR_INCOMPLETE)
   1216   1.1   thorpej 			s = "incomplete packet";
   1217   1.1   thorpej 		else if (err == ERR_OVERRUN)
   1218   1.1   thorpej 			s = "packet overrun";
   1219   1.1   thorpej 		else if (err == ERR_RUNT)
   1220   1.1   thorpej 			s = "runt packet";
   1221   1.1   thorpej 		else if (err == ERR_ALIGNMENT)
   1222   1.1   thorpej 			s = "bad alignment";
   1223   1.1   thorpej 		else if (err == ERR_CRC)
   1224   1.1   thorpej 			s = "bad crc";
   1225   1.1   thorpej 		else if (err == ERR_OVERSIZE)
   1226   1.1   thorpej 			s = "oversized packet";
   1227   1.1   thorpej 		else if (err == ERR_DRIBBLE)
   1228   1.1   thorpej 			s = "dribble bits";
   1229   1.1   thorpej 
   1230   1.1   thorpej 		if (s)
   1231  1.10  christos 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
   1232   1.1   thorpej 	}
   1233   1.1   thorpej 
   1234   1.1   thorpej 	if (len & ERR_INCOMPLETE)
   1235   1.1   thorpej 		return;
   1236   1.1   thorpej 
   1237   1.1   thorpej 	if (len & ERR_RX) {
   1238   1.1   thorpej 		++ifp->if_ierrors;
   1239   1.1   thorpej 		goto abort;
   1240   1.1   thorpej 	}
   1241   1.1   thorpej 
   1242   1.1   thorpej 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
   1243   1.1   thorpej 
   1244   1.1   thorpej 	/* Pull packet off interface. */
   1245   1.1   thorpej 	m = epget(sc, len);
   1246   1.1   thorpej 	if (m == 0) {
   1247   1.1   thorpej 		ifp->if_ierrors++;
   1248   1.1   thorpej 		goto abort;
   1249   1.1   thorpej 	}
   1250   1.1   thorpej 
   1251   1.1   thorpej 	++ifp->if_ipackets;
   1252   1.1   thorpej 
   1253   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
   1254   1.1   thorpej 	eh = mtod(m, struct ether_header *);
   1255   1.1   thorpej 
   1256   1.1   thorpej #if NBPFILTER > 0
   1257   1.1   thorpej 	/*
   1258   1.1   thorpej 	 * Check if there's a BPF listener on this interface.
   1259   1.1   thorpej 	 * If so, hand off the raw packet to BPF.
   1260   1.1   thorpej 	 */
   1261   1.1   thorpej 	if (ifp->if_bpf) {
   1262   1.1   thorpej 		bpf_mtap(ifp->if_bpf, m);
   1263   1.1   thorpej 
   1264   1.1   thorpej 		/*
   1265   1.1   thorpej 		 * Note that the interface cannot be in promiscuous mode if
   1266   1.1   thorpej 		 * there are no BPF listeners.  And if we are in promiscuous
   1267   1.1   thorpej 		 * mode, we have to check if this packet is really ours.
   1268   1.1   thorpej 		 */
   1269   1.1   thorpej 		if ((ifp->if_flags & IFF_PROMISC) &&
   1270   1.1   thorpej 		    (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
   1271  1.21        is 		    bcmp(eh->ether_dhost, LLADDR(sc->sc_ethercom.ec_if.if_sadl),
   1272   1.1   thorpej 			    sizeof(eh->ether_dhost)) != 0) {
   1273   1.1   thorpej 			m_freem(m);
   1274   1.1   thorpej 			return;
   1275   1.1   thorpej 		}
   1276   1.1   thorpej 	}
   1277   1.1   thorpej #endif
   1278   1.1   thorpej 
   1279   1.1   thorpej 	/* We assume the header fit entirely in one mbuf. */
   1280   1.1   thorpej 	m_adj(m, sizeof(struct ether_header));
   1281   1.1   thorpej 	ether_input(ifp, eh, m);
   1282   1.1   thorpej 
   1283   1.1   thorpej 	/*
   1284   1.1   thorpej 	 * In periods of high traffic we can actually receive enough
   1285   1.1   thorpej 	 * packets so that the fifo overrun bit will be set at this point,
   1286   1.1   thorpej 	 * even though we just read a packet. In this case we
   1287   1.1   thorpej 	 * are not going to receive any more interrupts. We check for
   1288   1.1   thorpej 	 * this condition and read again until the fifo is not full.
   1289   1.1   thorpej 	 * We could simplify this test by not using epstatus(), but
   1290   1.1   thorpej 	 * rechecking the RX_STATUS register directly. This test could
   1291   1.1   thorpej 	 * result in unnecessary looping in cases where there is a new
   1292   1.1   thorpej 	 * packet but the fifo is not full, but it will not fix the
   1293   1.1   thorpej 	 * stuck behavior.
   1294   1.1   thorpej 	 *
   1295   1.1   thorpej 	 * Even with this improvement, we still get packet overrun errors
   1296   1.1   thorpej 	 * which are hurting performance. Maybe when I get some more time
   1297   1.1   thorpej 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
   1298   1.1   thorpej 	 */
   1299   1.1   thorpej 	if (epstatus(sc)) {
   1300  1.11   thorpej 		len = bus_space_read_2(iot, ioh, EP_W1_RX_STATUS);
   1301   1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
   1302   1.1   thorpej 		if (len & ERR_INCOMPLETE) {
   1303   1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
   1304  1.10  christos 				printf("%s: adapter reset\n",
   1305   1.9  christos 				    sc->sc_dev.dv_xname);
   1306   1.1   thorpej 			epreset(sc);
   1307   1.1   thorpej 			return;
   1308   1.1   thorpej 		}
   1309   1.1   thorpej 		goto again;
   1310   1.1   thorpej 	}
   1311   1.1   thorpej 
   1312   1.1   thorpej 	return;
   1313   1.1   thorpej 
   1314   1.1   thorpej abort:
   1315  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1316  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1317   1.1   thorpej 		;
   1318   1.1   thorpej }
   1319   1.1   thorpej 
   1320   1.1   thorpej struct mbuf *
   1321   1.1   thorpej epget(sc, totlen)
   1322   1.1   thorpej 	struct ep_softc *sc;
   1323   1.1   thorpej 	int totlen;
   1324   1.1   thorpej {
   1325  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1326  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1327  1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1328   1.1   thorpej 	struct mbuf *top, **mp, *m;
   1329  1.14       cjs 	int len, remaining;
   1330   1.1   thorpej 	int sh;
   1331   1.1   thorpej 
   1332   1.1   thorpej 	m = sc->mb[sc->next_mb];
   1333   1.1   thorpej 	sc->mb[sc->next_mb] = 0;
   1334   1.1   thorpej 	if (m == 0) {
   1335   1.1   thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1336   1.1   thorpej 		if (m == 0)
   1337   1.1   thorpej 			return 0;
   1338   1.1   thorpej 	} else {
   1339   1.1   thorpej 		/* If the queue is no longer full, refill. */
   1340   1.1   thorpej 		if (sc->last_mb == sc->next_mb)
   1341   1.1   thorpej 			timeout(epmbuffill, sc, 1);
   1342   1.1   thorpej 		/* Convert one of our saved mbuf's. */
   1343   1.1   thorpej 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1344   1.1   thorpej 		m->m_data = m->m_pktdat;
   1345   1.1   thorpej 		m->m_flags = M_PKTHDR;
   1346   1.1   thorpej 	}
   1347   1.1   thorpej 	m->m_pkthdr.rcvif = ifp;
   1348   1.1   thorpej 	m->m_pkthdr.len = totlen;
   1349   1.1   thorpej 	len = MHLEN;
   1350   1.1   thorpej 	top = 0;
   1351   1.1   thorpej 	mp = &top;
   1352   1.1   thorpej 
   1353   1.1   thorpej 	/*
   1354   1.1   thorpej 	 * We read the packet at splhigh() so that an interrupt from another
   1355   1.1   thorpej 	 * device doesn't cause the card's buffer to overflow while we're
   1356   1.1   thorpej 	 * reading it.  We may still lose packets at other times.
   1357   1.1   thorpej 	 */
   1358   1.1   thorpej 	sh = splhigh();
   1359   1.1   thorpej 
   1360   1.1   thorpej 	while (totlen > 0) {
   1361   1.1   thorpej 		if (top) {
   1362   1.1   thorpej 			m = sc->mb[sc->next_mb];
   1363   1.1   thorpej 			sc->mb[sc->next_mb] = 0;
   1364   1.1   thorpej 			if (m == 0) {
   1365   1.1   thorpej 				MGET(m, M_DONTWAIT, MT_DATA);
   1366   1.1   thorpej 				if (m == 0) {
   1367   1.1   thorpej 					splx(sh);
   1368   1.1   thorpej 					m_freem(top);
   1369   1.1   thorpej 					return 0;
   1370   1.1   thorpej 				}
   1371   1.1   thorpej 			} else {
   1372   1.1   thorpej 				sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1373   1.1   thorpej 			}
   1374   1.1   thorpej 			len = MLEN;
   1375   1.1   thorpej 		}
   1376   1.1   thorpej 		if (totlen >= MINCLSIZE) {
   1377   1.1   thorpej 			MCLGET(m, M_DONTWAIT);
   1378  1.27   mycroft 			if ((m->m_flags & M_EXT) == 0) {
   1379  1.26   mycroft 				splx(sh);
   1380  1.30   mycroft 				m_free(m);
   1381  1.26   mycroft 				m_freem(top);
   1382  1.26   mycroft 				return 0;
   1383  1.26   mycroft 			}
   1384  1.26   mycroft 			len = MCLBYTES;
   1385   1.1   thorpej 		}
   1386  1.24       cjs 		if (top == 0)  {
   1387  1.25       cjs 			/* align the struct ip header */
   1388  1.25       cjs 			caddr_t newdata = (caddr_t)
   1389  1.25       cjs 			    ALIGN(m->m_data + sizeof(struct ether_header))
   1390  1.25       cjs 			    - sizeof(struct ether_header);
   1391  1.25       cjs 			len -= newdata - m->m_data;
   1392  1.25       cjs 			m->m_data = newdata;
   1393  1.14       cjs 		}
   1394  1.14       cjs 		remaining = len = min(totlen, len);
   1395   1.1   thorpej 		if (EP_IS_BUS_32(sc->bustype)) {
   1396  1.14       cjs 			u_long offset = mtod(m, u_long);
   1397  1.14       cjs 			/*
   1398  1.14       cjs 			 * Read bytes up to the point where we are aligned.
   1399  1.14       cjs 			 * (We can align to 4 bytes, rather than ALIGNBYTES,
   1400  1.14       cjs 			 * here because we're later reading 4-byte chunks.)
   1401  1.14       cjs 			 */
   1402  1.14       cjs 			if ((remaining > 3) && (offset & 3))  {
   1403  1.14       cjs 				int count = (4 - (offset & 3));
   1404  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
   1405  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1406  1.14       cjs 				    (u_int8_t *) offset, count);
   1407  1.14       cjs 				offset += count;
   1408  1.14       cjs 				remaining -= count;
   1409  1.14       cjs 			}
   1410  1.14       cjs 			if (remaining > 3) {
   1411  1.11   thorpej 				bus_space_read_multi_4(iot, ioh,
   1412  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1413  1.14       cjs 				    (u_int32_t *) offset, remaining >> 2);
   1414  1.14       cjs 				offset += remaining & ~3;
   1415  1.14       cjs 				remaining &= 3;
   1416  1.14       cjs 			}
   1417  1.14       cjs 			if (remaining)  {
   1418  1.11   thorpej 				bus_space_read_multi_1(iot, ioh,
   1419  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1420  1.14       cjs 				    (u_int8_t *) offset, remaining);
   1421  1.14       cjs 			}
   1422   1.1   thorpej 		} else {
   1423  1.14       cjs 			u_long offset = mtod(m, u_long);
   1424  1.14       cjs 			if ((remaining > 1) && (offset & 1))  {
   1425  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
   1426  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1427  1.14       cjs 				    (u_int8_t *) offset, 1);
   1428  1.14       cjs 				remaining -= 1;
   1429  1.14       cjs 				offset += 1;
   1430  1.14       cjs 			}
   1431  1.14       cjs 			if (remaining > 1) {
   1432  1.11   thorpej 				bus_space_read_multi_2(iot, ioh,
   1433  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1434  1.14       cjs 				    (u_int16_t *) offset, remaining >> 1);
   1435  1.14       cjs 				offset += remaining & ~1;
   1436  1.14       cjs 			}
   1437  1.14       cjs 			if (remaining & 1)  {
   1438  1.14       cjs 				bus_space_read_multi_1(iot, ioh,
   1439  1.14       cjs 				    EP_W1_RX_PIO_RD_1,
   1440  1.14       cjs 				    (u_int8_t *) offset, remaining & 1);
   1441  1.14       cjs 			}
   1442   1.1   thorpej 		}
   1443   1.1   thorpej 		m->m_len = len;
   1444   1.1   thorpej 		totlen -= len;
   1445   1.1   thorpej 		*mp = m;
   1446   1.1   thorpej 		mp = &m->m_next;
   1447   1.1   thorpej 	}
   1448   1.1   thorpej 
   1449  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1450  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1451   1.1   thorpej 		;
   1452   1.1   thorpej 
   1453   1.1   thorpej 	splx(sh);
   1454   1.1   thorpej 
   1455   1.1   thorpej 	return top;
   1456   1.1   thorpej }
   1457   1.1   thorpej 
   1458   1.1   thorpej int
   1459   1.1   thorpej epioctl(ifp, cmd, data)
   1460   1.1   thorpej 	register struct ifnet *ifp;
   1461   1.1   thorpej 	u_long cmd;
   1462   1.1   thorpej 	caddr_t data;
   1463   1.1   thorpej {
   1464   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
   1465   1.1   thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
   1466   1.1   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
   1467   1.1   thorpej 	int s, error = 0;
   1468   1.1   thorpej 
   1469   1.1   thorpej 	s = splnet();
   1470   1.1   thorpej 
   1471   1.1   thorpej 	switch (cmd) {
   1472   1.1   thorpej 
   1473   1.1   thorpej 	case SIOCSIFADDR:
   1474  1.34   thorpej 		if ((error = epenable(sc)) != 0)
   1475  1.34   thorpej 			break;
   1476  1.34   thorpej 		/* epinit is called just below */
   1477   1.1   thorpej 		ifp->if_flags |= IFF_UP;
   1478   1.1   thorpej 		switch (ifa->ifa_addr->sa_family) {
   1479   1.1   thorpej #ifdef INET
   1480   1.1   thorpej 		case AF_INET:
   1481   1.1   thorpej 			epinit(sc);
   1482  1.21        is 			arp_ifinit(&sc->sc_ethercom.ec_if, ifa);
   1483   1.1   thorpej 			break;
   1484   1.1   thorpej #endif
   1485   1.1   thorpej #ifdef NS
   1486   1.1   thorpej 		case AF_NS:
   1487   1.1   thorpej 		    {
   1488   1.1   thorpej 			register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
   1489   1.1   thorpej 
   1490   1.1   thorpej 			if (ns_nullhost(*ina))
   1491  1.21        is 				ina->x_host = *(union ns_host *)
   1492  1.21        is 				    LLADDR(ifp->if_sadl);
   1493   1.1   thorpej 			else
   1494   1.1   thorpej 				bcopy(ina->x_host.c_host,
   1495  1.21        is 				    LLADDR(ifp->if_sadl),
   1496  1.21        is 				    ifp->if_addrlen);
   1497   1.1   thorpej 			/* Set new address. */
   1498   1.1   thorpej 			epinit(sc);
   1499   1.1   thorpej 			break;
   1500   1.1   thorpej 		    }
   1501   1.1   thorpej #endif
   1502   1.1   thorpej 		default:
   1503   1.1   thorpej 			epinit(sc);
   1504   1.1   thorpej 			break;
   1505   1.1   thorpej 		}
   1506   1.1   thorpej 		break;
   1507   1.1   thorpej 
   1508  1.23  jonathan 	case SIOCSIFMEDIA:
   1509  1.23  jonathan 	case SIOCGIFMEDIA:
   1510  1.23  jonathan 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1511  1.23  jonathan 		break;
   1512  1.23  jonathan 
   1513   1.1   thorpej 	case SIOCSIFFLAGS:
   1514   1.1   thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1515   1.1   thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1516   1.1   thorpej 			/*
   1517   1.1   thorpej 			 * If interface is marked down and it is running, then
   1518   1.1   thorpej 			 * stop it.
   1519   1.1   thorpej 			 */
   1520   1.1   thorpej 			epstop(sc);
   1521   1.1   thorpej 			ifp->if_flags &= ~IFF_RUNNING;
   1522  1.34   thorpej 			epdisable(sc);
   1523   1.1   thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1524   1.1   thorpej 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1525   1.1   thorpej 			/*
   1526   1.1   thorpej 			 * If interface is marked up and it is stopped, then
   1527   1.1   thorpej 			 * start it.
   1528   1.1   thorpej 			 */
   1529  1.34   thorpej 			if ((error = epenable(sc)) != 0)
   1530  1.34   thorpej 				break;
   1531   1.1   thorpej 			epinit(sc);
   1532  1.34   thorpej 		} else if (sc->enabled) {
   1533   1.1   thorpej 			/*
   1534   1.1   thorpej 			 * deal with flags changes:
   1535  1.23  jonathan 			 * IFF_MULTICAST, IFF_PROMISC.
   1536   1.1   thorpej 			 */
   1537   1.1   thorpej 			epsetfilter(sc);
   1538   1.1   thorpej 		}
   1539   1.1   thorpej 		break;
   1540   1.1   thorpej 
   1541   1.1   thorpej 	case SIOCADDMULTI:
   1542   1.1   thorpej 	case SIOCDELMULTI:
   1543  1.34   thorpej 		if (sc->enabled == 0) {
   1544  1.34   thorpej 			error = EIO;
   1545  1.34   thorpej 			break;
   1546  1.34   thorpej 		}
   1547  1.34   thorpej 
   1548   1.1   thorpej 		error = (cmd == SIOCADDMULTI) ?
   1549  1.21        is 		    ether_addmulti(ifr, &sc->sc_ethercom) :
   1550  1.21        is 		    ether_delmulti(ifr, &sc->sc_ethercom);
   1551   1.1   thorpej 
   1552   1.1   thorpej 		if (error == ENETRESET) {
   1553   1.1   thorpej 			/*
   1554   1.1   thorpej 			 * Multicast list has changed; set the hardware filter
   1555   1.1   thorpej 			 * accordingly.
   1556   1.1   thorpej 			 */
   1557   1.1   thorpej 			epreset(sc);
   1558   1.1   thorpej 			error = 0;
   1559   1.1   thorpej 		}
   1560   1.1   thorpej 		break;
   1561   1.1   thorpej 
   1562   1.1   thorpej 	default:
   1563   1.1   thorpej 		error = EINVAL;
   1564   1.1   thorpej 		break;
   1565   1.1   thorpej 	}
   1566   1.1   thorpej 
   1567   1.1   thorpej 	splx(s);
   1568   1.1   thorpej 	return (error);
   1569   1.1   thorpej }
   1570   1.1   thorpej 
   1571   1.1   thorpej void
   1572   1.1   thorpej epreset(sc)
   1573   1.1   thorpej 	struct ep_softc *sc;
   1574   1.1   thorpej {
   1575   1.1   thorpej 	int s;
   1576   1.1   thorpej 
   1577   1.1   thorpej 	s = splnet();
   1578   1.1   thorpej 	epstop(sc);
   1579   1.1   thorpej 	epinit(sc);
   1580   1.1   thorpej 	splx(s);
   1581   1.1   thorpej }
   1582   1.1   thorpej 
   1583   1.1   thorpej void
   1584   1.5   thorpej epwatchdog(ifp)
   1585   1.5   thorpej 	struct ifnet *ifp;
   1586   1.1   thorpej {
   1587   1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
   1588   1.1   thorpej 
   1589   1.1   thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1590  1.21        is 	++sc->sc_ethercom.ec_if.if_oerrors;
   1591   1.1   thorpej 
   1592   1.1   thorpej 	epreset(sc);
   1593   1.1   thorpej }
   1594   1.1   thorpej 
   1595   1.1   thorpej void
   1596   1.1   thorpej epstop(sc)
   1597   1.1   thorpej 	register struct ep_softc *sc;
   1598   1.1   thorpej {
   1599  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1600  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1601   1.1   thorpej 
   1602  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISABLE);
   1603  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, RX_DISCARD_TOP_PACK);
   1604  1.11   thorpej 	while (bus_space_read_2(iot, ioh, EP_STATUS) & S_COMMAND_IN_PROGRESS)
   1605   1.1   thorpej 		;
   1606  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, TX_DISABLE);
   1607  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, STOP_TRANSCEIVER);
   1608  1.18  jonathan 
   1609  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, RX_RESET);
   1610  1.19  jonathan 	ep_complete_cmd(sc, EP_COMMAND, TX_RESET);
   1611  1.18  jonathan 
   1612  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, C_INTR_LATCH);
   1613  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RD_0_MASK);
   1614  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_INTR_MASK);
   1615  1.11   thorpej 	bus_space_write_2(iot, ioh, EP_COMMAND, SET_RX_FILTER);
   1616   1.1   thorpej 
   1617   1.1   thorpej 	epmbufempty(sc);
   1618   1.1   thorpej }
   1619  1.16  jonathan 
   1620  1.16  jonathan 
   1621  1.16  jonathan /*
   1622  1.16  jonathan  * Before reboots, reset card completely.
   1623  1.16  jonathan  */
   1624  1.16  jonathan static void
   1625  1.16  jonathan epshutdown(arg)
   1626  1.16  jonathan 	void *arg;
   1627  1.16  jonathan {
   1628  1.16  jonathan 	register struct ep_softc *sc = arg;
   1629  1.16  jonathan 
   1630  1.34   thorpej 	if (sc->enabled) {
   1631  1.34   thorpej 		epstop(sc);
   1632  1.34   thorpej 		ep_complete_cmd(sc, EP_COMMAND, GLOBAL_RESET);
   1633  1.34   thorpej 	}
   1634  1.16  jonathan }
   1635   1.1   thorpej 
   1636   1.1   thorpej /*
   1637   1.1   thorpej  * We get eeprom data from the id_port given an offset into the
   1638   1.1   thorpej  * eeprom.  Basically; after the ID_sequence is sent to all of
   1639   1.1   thorpej  * the cards; they enter the ID_CMD state where they will accept
   1640   1.1   thorpej  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1641   1.1   thorpej  * read the port 16 times and with every read; the cards check
   1642   1.1   thorpej  * for contention (ie: if one card writes a 0 bit and another
   1643   1.1   thorpej  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1644   1.1   thorpej  * each card compares the data on the bus; if there is a difference
   1645   1.1   thorpej  * then that card goes into ID_WAIT state again). In the meantime;
   1646   1.1   thorpej  * one bit of data is returned in the AX register which is conveniently
   1647  1.11   thorpej  * returned to us by bus_space_read_1().  Hence; we read 16 times getting one
   1648   1.1   thorpej  * bit of data with each read.
   1649   1.2   thorpej  *
   1650   1.2   thorpej  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1651   1.1   thorpej  */
   1652   1.2   thorpej u_int16_t
   1653  1.11   thorpej epreadeeprom(iot, ioh, offset)
   1654  1.11   thorpej 	bus_space_tag_t iot;
   1655  1.11   thorpej 	bus_space_handle_t ioh;
   1656   1.2   thorpej 	int offset;
   1657   1.1   thorpej {
   1658   1.2   thorpej 	u_int16_t data = 0;
   1659   1.2   thorpej 	int i;
   1660   1.1   thorpej 
   1661  1.11   thorpej 	bus_space_write_1(iot, ioh, 0, 0x80 + offset);
   1662   1.1   thorpej 	delay(1000);
   1663   1.1   thorpej 	for (i = 0; i < 16; i++)
   1664  1.11   thorpej 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1665   1.1   thorpej 	return (data);
   1666   1.1   thorpej }
   1667   1.1   thorpej 
   1668   1.1   thorpej static int
   1669   1.1   thorpej epbusyeeprom(sc)
   1670   1.1   thorpej 	struct ep_softc *sc;
   1671   1.1   thorpej {
   1672  1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1673  1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1674   1.1   thorpej 	int i = 100, j;
   1675   1.1   thorpej 
   1676   1.1   thorpej 	if (sc->bustype == EP_BUS_PCMCIA) {
   1677   1.1   thorpej 		delay(1000);
   1678   1.1   thorpej 		return 0;
   1679   1.1   thorpej 	}
   1680   1.1   thorpej 
   1681  1.33  jonathan 	j = 0;		/* bad GCC flow analysis */
   1682   1.1   thorpej 	while (i--) {
   1683  1.11   thorpej 		j = bus_space_read_2(iot, ioh, EP_W0_EEPROM_COMMAND);
   1684   1.1   thorpej 		if (j & EEPROM_BUSY)
   1685   1.1   thorpej 			delay(100);
   1686   1.1   thorpej 		else
   1687   1.1   thorpej 			break;
   1688   1.1   thorpej 	}
   1689   1.1   thorpej 	if (!i) {
   1690  1.10  christos 		printf("\n%s: eeprom failed to come ready\n",
   1691   1.1   thorpej 		    sc->sc_dev.dv_xname);
   1692   1.1   thorpej 		return (1);
   1693   1.1   thorpej 	}
   1694   1.1   thorpej 	if (j & EEPROM_TST_MODE) {
   1695  1.29  jonathan 		/* XXX PnP mode? */
   1696  1.28     veego 		printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
   1697   1.1   thorpej 		return (1);
   1698   1.1   thorpej 	}
   1699   1.1   thorpej 	return (0);
   1700   1.1   thorpej }
   1701   1.1   thorpej 
   1702   1.1   thorpej void
   1703   1.3  christos epmbuffill(v)
   1704   1.3  christos 	void *v;
   1705   1.1   thorpej {
   1706   1.3  christos 	struct ep_softc *sc = v;
   1707   1.1   thorpej 	int s, i;
   1708   1.1   thorpej 
   1709   1.1   thorpej 	s = splnet();
   1710   1.1   thorpej 	i = sc->last_mb;
   1711   1.1   thorpej 	do {
   1712   1.1   thorpej 		if (sc->mb[i] == NULL)
   1713   1.1   thorpej 			MGET(sc->mb[i], M_DONTWAIT, MT_DATA);
   1714   1.1   thorpej 		if (sc->mb[i] == NULL)
   1715   1.1   thorpej 			break;
   1716   1.1   thorpej 		i = (i + 1) % MAX_MBS;
   1717   1.1   thorpej 	} while (i != sc->next_mb);
   1718   1.1   thorpej 	sc->last_mb = i;
   1719   1.1   thorpej 	/* If the queue was not filled, try again. */
   1720   1.1   thorpej 	if (sc->last_mb != sc->next_mb)
   1721   1.1   thorpej 		timeout(epmbuffill, sc, 1);
   1722   1.1   thorpej 	splx(s);
   1723   1.1   thorpej }
   1724   1.1   thorpej 
   1725   1.1   thorpej void
   1726   1.1   thorpej epmbufempty(sc)
   1727   1.1   thorpej 	struct ep_softc *sc;
   1728   1.1   thorpej {
   1729   1.1   thorpej 	int s, i;
   1730   1.1   thorpej 
   1731   1.1   thorpej 	s = splnet();
   1732   1.1   thorpej 	for (i = 0; i<MAX_MBS; i++) {
   1733   1.1   thorpej 		if (sc->mb[i]) {
   1734   1.1   thorpej 			m_freem(sc->mb[i]);
   1735   1.1   thorpej 			sc->mb[i] = NULL;
   1736   1.1   thorpej 		}
   1737   1.1   thorpej 	}
   1738   1.1   thorpej 	sc->last_mb = sc->next_mb = 0;
   1739   1.1   thorpej 	untimeout(epmbuffill, sc);
   1740   1.1   thorpej 	splx(s);
   1741  1.34   thorpej }
   1742  1.34   thorpej 
   1743  1.34   thorpej int
   1744  1.34   thorpej epenable(sc)
   1745  1.34   thorpej 	struct ep_softc *sc;
   1746  1.34   thorpej {
   1747  1.34   thorpej 
   1748  1.34   thorpej 	if (sc->enabled == 0 && sc->enable != NULL) {
   1749  1.34   thorpej 		if ((*sc->enable)(sc) != 0) {
   1750  1.34   thorpej 			printf("%s: device enable failed\n",
   1751  1.34   thorpej 			    sc->sc_dev.dv_xname);
   1752  1.34   thorpej 			return (EIO);
   1753  1.34   thorpej 		}
   1754  1.34   thorpej 	}
   1755  1.34   thorpej 
   1756  1.34   thorpej 	sc->enabled = 1;
   1757  1.34   thorpej 	return (0);
   1758  1.34   thorpej }
   1759  1.34   thorpej 
   1760  1.34   thorpej void
   1761  1.34   thorpej epdisable(sc)
   1762  1.34   thorpej 	struct ep_softc *sc;
   1763  1.34   thorpej {
   1764  1.34   thorpej 
   1765  1.34   thorpej 	if (sc->enabled != 0 && sc->disable != NULL) {
   1766  1.34   thorpej 		(*sc->disable)(sc);
   1767  1.34   thorpej 		sc->enabled = 0;
   1768  1.34   thorpej 	}
   1769   1.1   thorpej }
   1770