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