Home | History | Annotate | Line # | Download | only in ic
elink3.c revision 1.116
      1  1.116  nakayama /*	$NetBSD: elink3.c,v 1.116 2006/06/07 15:34:47 nakayama Exp $	*/
      2   1.41   thorpej 
      3   1.41   thorpej /*-
      4   1.93  jdolecek  * Copyright (c) 1998, 2001 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.100     lukem 
     71  1.100     lukem #include <sys/cdefs.h>
     72  1.116  nakayama __KERNEL_RCSID(0, "$NetBSD: elink3.c,v 1.116 2006/06/07 15:34:47 nakayama Exp $");
     73    1.1   thorpej 
     74   1.39  jonathan #include "opt_inet.h"
     75   1.40  jonathan #include "opt_ns.h"
     76    1.1   thorpej #include "bpfilter.h"
     77   1.35  explorer #include "rnd.h"
     78    1.1   thorpej 
     79    1.1   thorpej #include <sys/param.h>
     80    1.3  christos #include <sys/systm.h>
     81   1.78   thorpej #include <sys/callout.h>
     82   1.41   thorpej #include <sys/kernel.h>
     83    1.1   thorpej #include <sys/mbuf.h>
     84    1.1   thorpej #include <sys/socket.h>
     85    1.1   thorpej #include <sys/ioctl.h>
     86    1.1   thorpej #include <sys/errno.h>
     87    1.1   thorpej #include <sys/syslog.h>
     88    1.1   thorpej #include <sys/select.h>
     89    1.1   thorpej #include <sys/device.h>
     90   1.35  explorer #if NRND > 0
     91   1.35  explorer #include <sys/rnd.h>
     92   1.35  explorer #endif
     93    1.1   thorpej 
     94    1.1   thorpej #include <net/if.h>
     95    1.1   thorpej #include <net/if_dl.h>
     96   1.21        is #include <net/if_ether.h>
     97   1.22  jonathan #include <net/if_media.h>
     98    1.1   thorpej 
     99    1.1   thorpej #if NBPFILTER > 0
    100    1.1   thorpej #include <net/bpf.h>
    101    1.1   thorpej #include <net/bpfdesc.h>
    102    1.1   thorpej #endif
    103    1.1   thorpej 
    104    1.1   thorpej #include <machine/cpu.h>
    105    1.2   thorpej #include <machine/bus.h>
    106    1.7   thorpej #include <machine/intr.h>
    107    1.1   thorpej 
    108   1.41   thorpej #include <dev/mii/mii.h>
    109   1.41   thorpej #include <dev/mii/miivar.h>
    110   1.67   thorpej #include <dev/mii/mii_bitbang.h>
    111   1.41   thorpej 
    112    1.1   thorpej #include <dev/ic/elink3var.h>
    113    1.1   thorpej #include <dev/ic/elink3reg.h>
    114    1.1   thorpej 
    115   1.36  jonathan #ifdef DEBUG
    116   1.36  jonathan int epdebug = 0;
    117   1.36  jonathan #endif
    118   1.36  jonathan 
    119   1.23  jonathan /*
    120   1.55  jonathan  * XXX endian workaround for big-endian CPUs  with pcmcia:
    121   1.55  jonathan  * if stream methods for bus_space_multi are not provided, define them
    122   1.55  jonathan  * using non-stream bus_space_{read,write}_multi_.
    123   1.55  jonathan  * Assumes host CPU is same endian-ness as bus.
    124   1.55  jonathan  */
    125   1.55  jonathan #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    126   1.55  jonathan #define bus_space_read_multi_stream_2	bus_space_read_multi_2
    127   1.55  jonathan #define bus_space_read_multi_stream_4	bus_space_read_multi_4
    128   1.55  jonathan #define bus_space_write_multi_stream_2	bus_space_write_multi_2
    129   1.55  jonathan #define bus_space_write_multi_stream_4	bus_space_write_multi_4
    130   1.55  jonathan #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
    131   1.55  jonathan 
    132   1.55  jonathan /*
    133   1.41   thorpej  * Structure to map media-present bits in boards to ifmedia codes and
    134   1.41   thorpej  * printable media names. Used for table-driven ifmedia initialization.
    135   1.23  jonathan  */
    136   1.23  jonathan struct ep_media {
    137   1.41   thorpej 	int	epm_mpbit;		/* media present bit */
    138   1.41   thorpej 	const char *epm_name;		/* name of medium */
    139   1.23  jonathan 	int	epm_ifmedia;		/* ifmedia word for medium */
    140   1.47      fvdl 	int	epm_epmedia;		/* ELINKMEDIA_* constant */
    141   1.23  jonathan };
    142   1.23  jonathan 
    143   1.23  jonathan /*
    144   1.41   thorpej  * Media table for the Demon/Vortex/Boomerang chipsets.
    145   1.41   thorpej  *
    146   1.41   thorpej  * Note that MII on the Demon and Vortex (3c59x) indicates an external
    147   1.41   thorpej  * MII connector (for connecting an external PHY) ... I think.  Treat
    148   1.41   thorpej  * it as `manual' on these chips.
    149   1.23  jonathan  *
    150   1.41   thorpej  * Any Boomerang (3c90x) chips with MII really do have an internal
    151   1.41   thorpej  * MII and real PHYs attached; no `native' media.
    152   1.23  jonathan  */
    153   1.87  jdolecek const struct ep_media ep_vortex_media[] = {
    154   1.47      fvdl 	{ ELINK_PCI_10BASE_T,	"10baseT",	IFM_ETHER|IFM_10_T,
    155   1.47      fvdl 	  ELINKMEDIA_10BASE_T },
    156   1.47      fvdl 	{ ELINK_PCI_10BASE_T,	"10baseT-FDX",	IFM_ETHER|IFM_10_T|IFM_FDX,
    157   1.47      fvdl 	  ELINKMEDIA_10BASE_T },
    158   1.48   thorpej 	{ ELINK_PCI_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
    159   1.47      fvdl 	  ELINKMEDIA_AUI },
    160   1.48   thorpej 	{ ELINK_PCI_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
    161   1.47      fvdl 	  ELINKMEDIA_10BASE_2 },
    162   1.47      fvdl 	{ ELINK_PCI_100BASE_TX,	"100baseTX",	IFM_ETHER|IFM_100_TX,
    163   1.47      fvdl 	  ELINKMEDIA_100BASE_TX },
    164   1.47      fvdl 	{ ELINK_PCI_100BASE_TX,	"100baseTX-FDX",IFM_ETHER|IFM_100_TX|IFM_FDX,
    165   1.47      fvdl 	  ELINKMEDIA_100BASE_TX },
    166   1.47      fvdl 	{ ELINK_PCI_100BASE_FX,	"100baseFX",	IFM_ETHER|IFM_100_FX,
    167   1.47      fvdl 	  ELINKMEDIA_100BASE_FX },
    168   1.48   thorpej 	{ ELINK_PCI_100BASE_MII,"manual",	IFM_ETHER|IFM_MANUAL,
    169   1.47      fvdl 	  ELINKMEDIA_MII },
    170   1.47      fvdl 	{ ELINK_PCI_100BASE_T4,	"100baseT4",	IFM_ETHER|IFM_100_T4,
    171   1.47      fvdl 	  ELINKMEDIA_100BASE_T4 },
    172   1.41   thorpej 	{ 0,			NULL,		0,
    173   1.41   thorpej 	  0 },
    174   1.23  jonathan };
    175   1.23  jonathan 
    176   1.23  jonathan /*
    177   1.41   thorpej  * Media table for the older 3Com Etherlink III chipset, used
    178   1.41   thorpej  * in the 3c509, 3c579, and 3c589.
    179   1.23  jonathan  */
    180   1.87  jdolecek const struct ep_media ep_509_media[] = {
    181   1.48   thorpej 	{ ELINK_W0_CC_UTP,	"10baseT",	IFM_ETHER|IFM_10_T,
    182   1.47      fvdl 	  ELINKMEDIA_10BASE_T },
    183   1.48   thorpej 	{ ELINK_W0_CC_AUI,	"10base5",	IFM_ETHER|IFM_10_5,
    184   1.47      fvdl 	  ELINKMEDIA_AUI },
    185   1.48   thorpej 	{ ELINK_W0_CC_BNC,	"10base2",	IFM_ETHER|IFM_10_2,
    186   1.47      fvdl 	  ELINKMEDIA_10BASE_2 },
    187   1.41   thorpej 	{ 0,			NULL,		0,
    188   1.41   thorpej 	  0 },
    189   1.23  jonathan };
    190   1.23  jonathan 
    191  1.110     perry void	ep_internalconfig(struct ep_softc *sc);
    192  1.110     perry void	ep_vortex_probemedia(struct ep_softc *sc);
    193  1.110     perry void	ep_509_probemedia(struct ep_softc *sc);
    194  1.110     perry 
    195  1.110     perry static void eptxstat(struct ep_softc *);
    196  1.110     perry static int epstatus(struct ep_softc *);
    197  1.110     perry int	epinit(struct ifnet *);
    198  1.110     perry void	epstop(struct ifnet *, int);
    199  1.110     perry int	epioctl(struct ifnet *, u_long, caddr_t);
    200  1.110     perry void	epstart(struct ifnet *);
    201  1.110     perry void	epwatchdog(struct ifnet *);
    202  1.110     perry void	epreset(struct ep_softc *);
    203  1.110     perry static void epshutdown(void *);
    204  1.110     perry void	epread(struct ep_softc *);
    205  1.110     perry struct mbuf *epget(struct ep_softc *, int);
    206  1.110     perry void	epmbuffill(void *);
    207  1.110     perry void	epmbufempty(struct ep_softc *);
    208  1.110     perry void	epsetfilter(struct ep_softc *);
    209  1.110     perry void	ep_roadrunner_mii_enable(struct ep_softc *);
    210  1.110     perry void	epsetmedia(struct ep_softc *);
    211   1.23  jonathan 
    212   1.23  jonathan /* ifmedia callbacks */
    213  1.110     perry int	ep_media_change(struct ifnet *ifp);
    214  1.110     perry void	ep_media_status(struct ifnet *ifp, struct ifmediareq *req);
    215    1.1   thorpej 
    216   1.41   thorpej /* MII callbacks */
    217  1.110     perry int	ep_mii_readreg(struct device *, int, int);
    218  1.110     perry void	ep_mii_writereg(struct device *, int, int, int);
    219  1.110     perry void	ep_statchg(struct device *);
    220  1.110     perry 
    221  1.110     perry void	ep_tick(void *);
    222  1.110     perry 
    223  1.110     perry static int epbusyeeprom(struct ep_softc *);
    224  1.110     perry u_int16_t ep_read_eeprom(struct ep_softc *, u_int16_t);
    225  1.110     perry static inline void ep_reset_cmd(struct ep_softc *sc, u_int cmd, u_int arg);
    226  1.110     perry static inline void ep_finish_reset(bus_space_tag_t, bus_space_handle_t);
    227  1.110     perry static inline void ep_discard_rxtop(bus_space_tag_t, bus_space_handle_t);
    228  1.114     perry static inline int ep_w1_reg(struct ep_softc *, int);
    229   1.19  jonathan 
    230   1.42   thorpej /*
    231   1.67   thorpej  * MII bit-bang glue.
    232   1.67   thorpej  */
    233  1.110     perry u_int32_t ep_mii_bitbang_read(struct device *);
    234  1.110     perry void ep_mii_bitbang_write(struct device *, u_int32_t);
    235   1.67   thorpej 
    236   1.67   thorpej const struct mii_bitbang_ops ep_mii_bitbang_ops = {
    237   1.67   thorpej 	ep_mii_bitbang_read,
    238   1.67   thorpej 	ep_mii_bitbang_write,
    239   1.67   thorpej 	{
    240   1.67   thorpej 		PHYSMGMT_DATA,		/* MII_BIT_MDO */
    241   1.67   thorpej 		PHYSMGMT_DATA,		/* MII_BIT_MDI */
    242   1.67   thorpej 		PHYSMGMT_CLK,		/* MII_BIT_MDC */
    243   1.67   thorpej 		PHYSMGMT_DIR,		/* MII_BIT_DIR_HOST_PHY */
    244   1.67   thorpej 		0,			/* MII_BIT_DIR_PHY_HOST */
    245   1.67   thorpej 	}
    246   1.67   thorpej };
    247   1.67   thorpej 
    248   1.67   thorpej /*
    249   1.42   thorpej  * Some chips (3c515 [Corkscrew] and 3c574 [RoadRunner]) have
    250   1.42   thorpej  * Window 1 registers offset!
    251   1.42   thorpej  */
    252  1.114     perry static inline int
    253   1.42   thorpej ep_w1_reg(sc, reg)
    254   1.42   thorpej 	struct ep_softc *sc;
    255   1.42   thorpej 	int reg;
    256   1.42   thorpej {
    257   1.42   thorpej 
    258   1.42   thorpej 	switch (sc->ep_chipset) {
    259   1.47      fvdl 	case ELINK_CHIPSET_CORKSCREW:
    260   1.42   thorpej 		return (reg + 0x10);
    261   1.42   thorpej 
    262   1.47      fvdl 	case ELINK_CHIPSET_ROADRUNNER:
    263   1.42   thorpej 		switch (reg) {
    264   1.47      fvdl 		case ELINK_W1_FREE_TX:
    265   1.47      fvdl 		case ELINK_W1_RUNNER_RDCTL:
    266   1.47      fvdl 		case ELINK_W1_RUNNER_WRCTL:
    267   1.42   thorpej 			return (reg);
    268   1.42   thorpej 		}
    269   1.42   thorpej 		return (reg + 0x10);
    270   1.42   thorpej 	}
    271   1.42   thorpej 
    272   1.42   thorpej 	return (reg);
    273   1.42   thorpej }
    274   1.19  jonathan 
    275   1.19  jonathan /*
    276   1.56  jonathan  * Wait for any pending reset to complete.
    277   1.19  jonathan  * On newer hardware we could poll SC_COMMAND_IN_PROGRESS,
    278   1.19  jonathan  * but older hardware doesn't implement it and we must delay.
    279   1.19  jonathan  */
    280   1.19  jonathan static inline void
    281   1.56  jonathan ep_finish_reset(iot, ioh)
    282   1.56  jonathan 	bus_space_tag_t iot;
    283   1.56  jonathan 	bus_space_handle_t ioh;
    284   1.56  jonathan {
    285   1.57  jonathan 	int i;
    286   1.56  jonathan 
    287   1.57  jonathan 	for (i = 0; i < 10000; i++) {
    288   1.60     enami 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
    289  1.102  christos 		    COMMAND_IN_PROGRESS) == 0)
    290   1.57  jonathan 			break;
    291   1.57  jonathan 		DELAY(10);
    292   1.56  jonathan 	}
    293   1.56  jonathan }
    294   1.56  jonathan 
    295   1.56  jonathan /*
    296   1.56  jonathan  * Issue a (reset) command, and be sure it has completed.
    297   1.56  jonathan  * Used for global reset, TX_RESET, RX_RESET.
    298   1.56  jonathan  */
    299   1.56  jonathan static inline void
    300   1.56  jonathan ep_reset_cmd(sc, cmd, arg)
    301   1.19  jonathan 	struct ep_softc *sc;
    302   1.19  jonathan 	u_int cmd, arg;
    303   1.19  jonathan {
    304   1.79  augustss 	bus_space_tag_t iot = sc->sc_iot;
    305   1.79  augustss 	bus_space_handle_t ioh = sc->sc_ioh;
    306   1.19  jonathan 
    307   1.19  jonathan 	bus_space_write_2(iot, ioh, cmd, arg);
    308   1.56  jonathan 	ep_finish_reset(iot, ioh);
    309   1.56  jonathan }
    310   1.56  jonathan 
    311   1.56  jonathan 
    312   1.56  jonathan static inline void
    313   1.56  jonathan ep_discard_rxtop(iot, ioh)
    314   1.79  augustss 	bus_space_tag_t iot;
    315   1.79  augustss 	bus_space_handle_t ioh;
    316   1.56  jonathan {
    317   1.57  jonathan 	int i;
    318   1.56  jonathan 
    319   1.56  jonathan 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISCARD_TOP_PACK);
    320   1.57  jonathan 
    321   1.57  jonathan         /*
    322   1.57  jonathan 	 * Spin for about 1 msec, to avoid forcing a DELAY() between
    323   1.57  jonathan 	 * every received packet (adding latency and  limiting pkt-recv rate).
    324   1.57  jonathan 	 * On PCI, at 4 30-nsec PCI bus cycles for a read, 8000 iterations
    325   1.57  jonathan 	 * is about right.
    326   1.57  jonathan 	 */
    327   1.57  jonathan 	for (i = 0; i < 8000; i++) {
    328   1.60     enami 		if ((bus_space_read_2(iot, ioh, ELINK_STATUS) &
    329  1.102  christos 		    COMMAND_IN_PROGRESS) == 0)
    330   1.57  jonathan 		    return;
    331   1.57  jonathan 	}
    332   1.57  jonathan 
    333   1.57  jonathan 	/*  Didn't complete in a hurry. Do DELAY()s. */
    334   1.56  jonathan 	ep_finish_reset(iot, ioh);
    335   1.19  jonathan }
    336   1.19  jonathan 
    337   1.20  jonathan /*
    338   1.20  jonathan  * Back-end attach and configure.
    339   1.20  jonathan  */
    340   1.75     enami int
    341   1.34   thorpej epconfig(sc, chipset, enaddr)
    342    1.1   thorpej 	struct ep_softc *sc;
    343   1.20  jonathan 	u_short chipset;
    344   1.34   thorpej 	u_int8_t *enaddr;
    345    1.1   thorpej {
    346   1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    347   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    348   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    349    1.7   thorpej 	u_int16_t i;
    350   1.83   tsutsui 	u_int8_t myla[ETHER_ADDR_LEN];
    351    1.1   thorpej 
    352   1.78   thorpej 	callout_init(&sc->sc_mii_callout);
    353   1.78   thorpej 	callout_init(&sc->sc_mbuf_callout);
    354   1.78   thorpej 
    355   1.20  jonathan 	sc->ep_chipset = chipset;
    356   1.32   thorpej 
    357   1.32   thorpej 	/*
    358   1.32   thorpej 	 * We could have been groveling around in other register
    359   1.32   thorpej 	 * windows in the front-end; make sure we're in window 0
    360   1.32   thorpej 	 * to read the EEPROM.
    361   1.32   thorpej 	 */
    362   1.32   thorpej 	GO_WINDOW(0);
    363    1.1   thorpej 
    364   1.34   thorpej 	if (enaddr == NULL) {
    365   1.34   thorpej 		/*
    366   1.59   thorpej 		 * Read the station address from the eeprom.
    367   1.34   thorpej 		 */
    368   1.83   tsutsui 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++) {
    369   1.59   thorpej 			u_int16_t x = ep_read_eeprom(sc, i);
    370   1.34   thorpej 			myla[(i << 1)] = x >> 8;
    371   1.34   thorpej 			myla[(i << 1) + 1] = x;
    372   1.34   thorpej 		}
    373   1.34   thorpej 		enaddr = myla;
    374    1.1   thorpej 	}
    375    1.1   thorpej 
    376   1.12  jonathan 	/*
    377   1.41   thorpej 	 * Vortex-based (3c59x pci,eisa) and Boomerang (3c900) cards
    378   1.23  jonathan 	 * allow FDDI-sized (4500) byte packets.  Commands only take an
    379   1.23  jonathan 	 * 11-bit parameter, and  11 bits isn't enough to hold a full-size
    380   1.23  jonathan 	 * packet length.
    381   1.12  jonathan 	 * Commands to these cards implicitly upshift a packet size
    382  1.111     perry 	 * or threshold by 2 bits.
    383   1.12  jonathan 	 * To detect  cards with large-packet support, we probe by setting
    384   1.12  jonathan 	 * the transmit threshold register, then change windows and
    385   1.12  jonathan 	 * read back the threshold register directly, and see if the
    386   1.12  jonathan 	 * threshold value was shifted or not.
    387   1.12  jonathan 	 */
    388   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    389  1.111     perry 	    SET_TX_AVAIL_THRESH | ELINK_LARGEWIN_PROBE);
    390   1.12  jonathan 	GO_WINDOW(5);
    391   1.47      fvdl 	i = bus_space_read_2(iot, ioh, ELINK_W5_TX_AVAIL_THRESH);
    392   1.12  jonathan 	GO_WINDOW(1);
    393   1.12  jonathan 	switch (i)  {
    394   1.47      fvdl 	case ELINK_LARGEWIN_PROBE:
    395   1.47      fvdl 	case (ELINK_LARGEWIN_PROBE & ELINK_LARGEWIN_MASK):
    396   1.12  jonathan 		sc->ep_pktlenshift = 0;
    397   1.12  jonathan 		break;
    398   1.12  jonathan 
    399   1.47      fvdl 	case (ELINK_LARGEWIN_PROBE << 2):
    400   1.12  jonathan 		sc->ep_pktlenshift = 2;
    401   1.12  jonathan 		break;
    402   1.12  jonathan 
    403   1.12  jonathan 	default:
    404  1.106   thorpej 		aprint_error(
    405  1.106   thorpej 		    "%s: wrote 0x%x to TX_AVAIL_THRESH, read back 0x%x. "
    406   1.14       cjs 		    "Interface disabled\n",
    407   1.47      fvdl 		    sc->sc_dev.dv_xname, ELINK_LARGEWIN_PROBE, (int) i);
    408   1.75     enami 		return (1);
    409   1.12  jonathan 	}
    410   1.20  jonathan 
    411   1.12  jonathan 	/*
    412  1.111     perry 	 * Ensure Tx-available interrupts are enabled for
    413   1.12  jonathan 	 * start the interface.
    414   1.23  jonathan 	 * XXX should be in epinit()?
    415   1.12  jonathan 	 */
    416   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    417   1.12  jonathan 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    418   1.12  jonathan 
    419   1.94   thorpej 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    420   1.23  jonathan 	ifp->if_softc = sc;
    421   1.23  jonathan 	ifp->if_start = epstart;
    422   1.23  jonathan 	ifp->if_ioctl = epioctl;
    423   1.23  jonathan 	ifp->if_watchdog = epwatchdog;
    424   1.88  jdolecek 	ifp->if_init = epinit;
    425   1.88  jdolecek 	ifp->if_stop = epstop;
    426   1.23  jonathan 	ifp->if_flags =
    427   1.23  jonathan 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    428   1.86   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    429   1.23  jonathan 
    430   1.23  jonathan 	if_attach(ifp);
    431   1.34   thorpej 	ether_ifattach(ifp, enaddr);
    432   1.23  jonathan 
    433   1.23  jonathan 	/*
    434  1.111     perry 	 * Finish configuration:
    435   1.23  jonathan 	 * determine chipset if the front-end couldn't do so,
    436   1.23  jonathan 	 * show board details, set media.
    437   1.23  jonathan 	 */
    438   1.23  jonathan 
    439   1.41   thorpej 	/*
    440   1.41   thorpej 	 * Print RAM size.  We also print the Ethernet address in here.
    441   1.41   thorpej 	 * It's extracted from the ifp, so we have to make sure it's
    442   1.41   thorpej 	 * been attached first.
    443   1.41   thorpej 	 */
    444   1.23  jonathan 	ep_internalconfig(sc);
    445   1.23  jonathan 	GO_WINDOW(0);
    446   1.23  jonathan 
    447   1.41   thorpej 	/*
    448   1.44   thorpej 	 * Display some additional information, if pertinent.
    449   1.44   thorpej 	 */
    450   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
    451  1.106   thorpej 		aprint_normal("%s: RoadRunner FIFO buffer enabled\n",
    452   1.44   thorpej 		    sc->sc_dev.dv_xname);
    453   1.44   thorpej 
    454   1.44   thorpej 	/*
    455   1.41   thorpej 	 * Initialize our media structures and MII info.  We'll
    456   1.41   thorpej 	 * probe the MII if we discover that we have one.
    457   1.20  jonathan 	 */
    458   1.41   thorpej 	sc->sc_mii.mii_ifp = ifp;
    459   1.41   thorpej 	sc->sc_mii.mii_readreg = ep_mii_readreg;
    460   1.41   thorpej 	sc->sc_mii.mii_writereg = ep_mii_writereg;
    461   1.41   thorpej 	sc->sc_mii.mii_statchg = ep_statchg;
    462  1.104      fair 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ep_media_change,
    463   1.41   thorpej 	    ep_media_status);
    464   1.20  jonathan 
    465   1.20  jonathan 	/*
    466   1.97   thorpej 	 * All CORKSCREW chips have MII.
    467   1.97   thorpej 	 */
    468   1.97   thorpej 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW)
    469   1.97   thorpej 		sc->ep_flags |= ELINK_FLAGS_MII;
    470   1.97   thorpej 
    471   1.97   thorpej 	/*
    472   1.41   thorpej 	 * Now, determine which media we have.
    473   1.20  jonathan 	 */
    474   1.20  jonathan 	switch (sc->ep_chipset) {
    475   1.59   thorpej 	case ELINK_CHIPSET_ROADRUNNER:
    476   1.59   thorpej 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    477   1.59   thorpej 			ep_roadrunner_mii_enable(sc);
    478   1.59   thorpej 			GO_WINDOW(0);
    479   1.59   thorpej 		}
    480   1.59   thorpej 		/* FALLTHROUGH */
    481   1.59   thorpej 
    482   1.97   thorpej 	case ELINK_CHIPSET_CORKSCREW:
    483   1.47      fvdl 	case ELINK_CHIPSET_BOOMERANG:
    484   1.41   thorpej 		/*
    485   1.41   thorpej 		 * If the device has MII, probe it.  We won't be using
    486   1.41   thorpej 		 * any `native' media in this case, only PHYs.  If
    487   1.41   thorpej 		 * we don't, just treat the Boomerang like the Vortex.
    488   1.41   thorpej 		 */
    489   1.47      fvdl 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    490   1.69   thorpej 			mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff,
    491   1.73   thorpej 			    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    492   1.41   thorpej 			if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    493   1.41   thorpej 				ifmedia_add(&sc->sc_mii.mii_media,
    494   1.41   thorpej 				    IFM_ETHER|IFM_NONE, 0, NULL);
    495   1.41   thorpej 				ifmedia_set(&sc->sc_mii.mii_media,
    496   1.41   thorpej 				    IFM_ETHER|IFM_NONE);
    497   1.41   thorpej 			} else {
    498   1.41   thorpej 				ifmedia_set(&sc->sc_mii.mii_media,
    499   1.41   thorpej 				    IFM_ETHER|IFM_AUTO);
    500   1.41   thorpej 			}
    501   1.41   thorpej 			break;
    502   1.41   thorpej 		}
    503   1.41   thorpej 		/* FALLTHROUGH */
    504   1.41   thorpej 
    505   1.47      fvdl 	case ELINK_CHIPSET_VORTEX:
    506   1.20  jonathan 		ep_vortex_probemedia(sc);
    507   1.20  jonathan 		break;
    508   1.20  jonathan 
    509   1.20  jonathan 	default:
    510   1.41   thorpej 		ep_509_probemedia(sc);
    511   1.20  jonathan 		break;
    512   1.20  jonathan 	}
    513   1.23  jonathan 
    514   1.20  jonathan 	GO_WINDOW(1);		/* Window 1 is operating window */
    515   1.20  jonathan 
    516   1.35  explorer #if NRND > 0
    517   1.53  explorer 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
    518   1.60     enami 	    RND_TYPE_NET, 0);
    519   1.35  explorer #endif
    520   1.35  explorer 
    521    1.1   thorpej 	sc->tx_start_thresh = 20;	/* probably a good starting point. */
    522   1.12  jonathan 
    523   1.16  jonathan 	/*  Establish callback to reset card when we reboot. */
    524   1.71  augustss 	sc->sd_hook = shutdownhook_establish(epshutdown, sc);
    525   1.16  jonathan 
    526   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    527   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    528   1.81     jhawk 
    529   1.81     jhawk 	/* The attach is successful. */
    530   1.81     jhawk 	sc->sc_flags |= ELINK_FLAGS_ATTACHED;
    531   1.75     enami 	return (0);
    532    1.1   thorpej }
    533    1.1   thorpej 
    534   1.23  jonathan 
    535    1.1   thorpej /*
    536   1.15  jonathan  * Show interface-model-independent info from window 3
    537   1.15  jonathan  * internal-configuration register.
    538   1.15  jonathan  */
    539   1.15  jonathan void
    540   1.15  jonathan ep_internalconfig(sc)
    541   1.15  jonathan 	struct ep_softc *sc;
    542   1.15  jonathan {
    543   1.15  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    544   1.15  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    545   1.15  jonathan 
    546   1.15  jonathan 	u_int config0;
    547   1.15  jonathan 	u_int config1;
    548   1.15  jonathan 
    549  1.105    simonb 	int  ram_size, ram_width, ram_split;
    550   1.15  jonathan 	/*
    551   1.15  jonathan 	 * NVRAM buffer Rx:Tx config names for busmastering cards
    552   1.15  jonathan 	 * (Demon, Vortex, and later).
    553   1.15  jonathan 	 */
    554  1.103      yamt 	const char *const onboard_ram_config[] = {
    555   1.38  augustss 		"5:3", "3:1", "1:1", "3:5" };
    556   1.15  jonathan 
    557   1.15  jonathan 	GO_WINDOW(3);
    558   1.47      fvdl 	config0 = (u_int)bus_space_read_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG);
    559   1.60     enami 	config1 = (u_int)bus_space_read_2(iot, ioh,
    560   1.60     enami 	    ELINK_W3_INTERNAL_CONFIG + 2);
    561   1.15  jonathan 	GO_WINDOW(0);
    562   1.15  jonathan 
    563   1.15  jonathan 	ram_size  = (config0 & CONFIG_RAMSIZE) >> CONFIG_RAMSIZE_SHIFT;
    564   1.15  jonathan 	ram_width = (config0 & CONFIG_RAMWIDTH) >> CONFIG_RAMWIDTH_SHIFT;
    565   1.15  jonathan 
    566   1.15  jonathan 	ram_split  = (config1 & CONFIG_RAMSPLIT) >> CONFIG_RAMSPLIT_SHIFT;
    567   1.15  jonathan 
    568  1.106   thorpej 	aprint_normal("%s: address %s, %dKB %s-wide FIFO, %s Rx:Tx split\n",
    569   1.23  jonathan 	       sc->sc_dev.dv_xname,
    570   1.41   thorpej 	       ether_sprintf(LLADDR(sc->sc_ethercom.ec_if.if_sadl)),
    571   1.23  jonathan 	       8 << ram_size,
    572   1.23  jonathan 	       (ram_width) ? "word" : "byte",
    573   1.23  jonathan 	       onboard_ram_config[ram_split]);
    574   1.15  jonathan }
    575   1.15  jonathan 
    576   1.23  jonathan 
    577   1.20  jonathan /*
    578   1.23  jonathan  * Find supported media on 3c509-generation hardware that doesn't have
    579   1.20  jonathan  * a "reset_options" register in window 3.
    580   1.23  jonathan  * Use the config_cntrl register  in window 0 instead.
    581   1.23  jonathan  * Used on original, 10Mbit ISA (3c509), 3c509B, and pre-Demon EISA cards
    582   1.23  jonathan  * that implement  CONFIG_CTRL.  We don't have a good way to set the
    583   1.89  jdolecek  * default active medium; punt to ifconfig  instead.
    584   1.20  jonathan  */
    585   1.20  jonathan void
    586   1.41   thorpej ep_509_probemedia(sc)
    587   1.20  jonathan 	struct ep_softc *sc;
    588   1.20  jonathan {
    589   1.20  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    590   1.20  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    591   1.41   thorpej 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    592   1.23  jonathan 	u_int16_t ep_w0_config, port;
    593   1.87  jdolecek 	const struct ep_media *epm;
    594   1.41   thorpej 	const char *sep = "", *defmedianame = NULL;
    595   1.41   thorpej 	int defmedia = 0;
    596   1.23  jonathan 
    597   1.20  jonathan 	GO_WINDOW(0);
    598   1.47      fvdl 	ep_w0_config = bus_space_read_2(iot, ioh, ELINK_W0_CONFIG_CTRL);
    599   1.23  jonathan 
    600  1.106   thorpej 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    601   1.23  jonathan 
    602   1.41   thorpej 	/* Sanity check that there are any media! */
    603   1.47      fvdl 	if ((ep_w0_config & ELINK_W0_CC_MEDIAMASK) == 0) {
    604  1.106   thorpej 		aprint_error("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.23  jonathan 	}
    609   1.23  jonathan 
    610   1.41   thorpej 	/*
    611   1.41   thorpej 	 * Get the default media from the EEPROM.
    612   1.41   thorpej 	 */
    613   1.59   thorpej 	port = ep_read_eeprom(sc, EEPROM_ADDR_CFG) >> 14;
    614   1.23  jonathan 
    615  1.106   thorpej #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
    616   1.23  jonathan 
    617   1.41   thorpej 	for (epm = ep_509_media; epm->epm_name != NULL; epm++) {
    618   1.41   thorpej 		if (ep_w0_config & epm->epm_mpbit) {
    619   1.46   thorpej 			/*
    620   1.46   thorpej 			 * This simple test works because 509 chipsets
    621   1.46   thorpej 			 * don't do full-duplex.
    622   1.46   thorpej 			 */
    623   1.41   thorpej 			if (epm->epm_epmedia == port || defmedia == 0) {
    624   1.41   thorpej 				defmedia = epm->epm_ifmedia;
    625   1.41   thorpej 				defmedianame = epm->epm_name;
    626   1.41   thorpej 			}
    627   1.41   thorpej 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
    628   1.41   thorpej 			    NULL);
    629   1.41   thorpej 			PRINT(epm->epm_name);
    630   1.41   thorpej 		}
    631   1.41   thorpej 	}
    632   1.41   thorpej 
    633   1.41   thorpej #undef PRINT
    634   1.41   thorpej 
    635   1.41   thorpej #ifdef DIAGNOSTIC
    636   1.41   thorpej 	if (defmedia == 0)
    637   1.41   thorpej 		panic("ep_509_probemedia: impossible");
    638   1.41   thorpej #endif
    639   1.41   thorpej 
    640  1.106   thorpej 	aprint_normal(" (default %s)\n", defmedianame);
    641   1.41   thorpej 	ifmedia_set(ifm, defmedia);
    642   1.20  jonathan }
    643   1.20  jonathan 
    644   1.15  jonathan /*
    645   1.23  jonathan  * Find media present on large-packet-capable elink3 devices.
    646   1.23  jonathan  * Show onboard configuration of large-packet-capable elink3 devices
    647   1.23  jonathan  * (Demon, Vortex, Boomerang), which do not implement CONFIG_CTRL in window 0.
    648   1.23  jonathan  * Use media and card-version info in window 3 instead.
    649   1.15  jonathan  */
    650   1.15  jonathan void
    651   1.20  jonathan ep_vortex_probemedia(sc)
    652   1.15  jonathan 	struct ep_softc *sc;
    653   1.15  jonathan {
    654   1.15  jonathan 	bus_space_tag_t iot = sc->sc_iot;
    655   1.15  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
    656   1.41   thorpej 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    657   1.87  jdolecek 	const struct ep_media *epm;
    658   1.41   thorpej 	u_int config1;
    659   1.15  jonathan 	int reset_options;
    660   1.28     veego 	int default_media;	/* 3-bit encoding of default (EEPROM) media */
    661   1.41   thorpej 	int defmedia = 0;
    662   1.41   thorpej 	const char *sep = "", *defmedianame = NULL;
    663   1.15  jonathan 
    664   1.15  jonathan 	GO_WINDOW(3);
    665   1.60     enami 	config1 = (u_int)bus_space_read_2(iot, ioh,
    666   1.60     enami 	    ELINK_W3_INTERNAL_CONFIG + 2);
    667  1.107   mycroft 	reset_options = (int)bus_space_read_2(iot, ioh, ELINK_W3_RESET_OPTIONS);
    668   1.15  jonathan 	GO_WINDOW(0);
    669   1.15  jonathan 
    670   1.23  jonathan 	default_media = (config1 & CONFIG_MEDIAMASK) >> CONFIG_MEDIAMASK_SHIFT;
    671   1.15  jonathan 
    672  1.106   thorpej 	aprint_normal("%s: ", sc->sc_dev.dv_xname);
    673   1.41   thorpej 
    674   1.41   thorpej 	/* Sanity check that there are any media! */
    675   1.47      fvdl 	if ((reset_options & ELINK_PCI_MEDIAMASK) == 0) {
    676  1.106   thorpej 		aprint_error("no media present!\n");
    677   1.41   thorpej 		ifmedia_add(ifm, IFM_ETHER|IFM_NONE, 0, NULL);
    678   1.41   thorpej 		ifmedia_set(ifm, IFM_ETHER|IFM_NONE);
    679   1.41   thorpej 		return;
    680   1.41   thorpej 	}
    681   1.41   thorpej 
    682  1.106   thorpej #define	PRINT(str)	aprint_normal("%s%s", sep, str); sep = ", "
    683   1.23  jonathan 
    684   1.41   thorpej 	for (epm = ep_vortex_media; epm->epm_name != NULL; epm++) {
    685   1.41   thorpej 		if (reset_options & epm->epm_mpbit) {
    686   1.46   thorpej 			/*
    687   1.46   thorpej 			 * Default media is a little more complicated
    688   1.46   thorpej 			 * on the Vortex.  We support full-duplex which
    689   1.46   thorpej 			 * uses the same reset options bit.
    690   1.46   thorpej 			 *
    691   1.46   thorpej 			 * XXX Check EEPROM for default to FDX?
    692   1.46   thorpej 			 */
    693   1.46   thorpej 			if (epm->epm_epmedia == default_media) {
    694   1.46   thorpej 				if ((epm->epm_ifmedia & IFM_FDX) == 0) {
    695   1.46   thorpej 					defmedia = epm->epm_ifmedia;
    696   1.46   thorpej 					defmedianame = epm->epm_name;
    697   1.46   thorpej 				}
    698   1.46   thorpej 			} else if (defmedia == 0) {
    699   1.41   thorpej 				defmedia = epm->epm_ifmedia;
    700   1.41   thorpej 				defmedianame = epm->epm_name;
    701   1.41   thorpej 			}
    702   1.41   thorpej 			ifmedia_add(ifm, epm->epm_ifmedia, epm->epm_epmedia,
    703   1.41   thorpej 			    NULL);
    704   1.41   thorpej 			PRINT(epm->epm_name);
    705   1.23  jonathan 		}
    706   1.23  jonathan 	}
    707   1.15  jonathan 
    708   1.41   thorpej #undef PRINT
    709   1.41   thorpej 
    710   1.41   thorpej #ifdef DIAGNOSTIC
    711   1.41   thorpej 	if (defmedia == 0)
    712   1.41   thorpej 		panic("ep_vortex_probemedia: impossible");
    713   1.41   thorpej #endif
    714   1.41   thorpej 
    715  1.106   thorpej 	aprint_normal(" (default %s)\n", defmedianame);
    716   1.41   thorpej 	ifmedia_set(ifm, defmedia);
    717   1.41   thorpej }
    718   1.41   thorpej 
    719   1.41   thorpej /*
    720   1.41   thorpej  * One second timer, used to tick the MII.
    721   1.41   thorpej  */
    722   1.41   thorpej void
    723   1.41   thorpej ep_tick(arg)
    724   1.41   thorpej 	void *arg;
    725   1.41   thorpej {
    726   1.41   thorpej 	struct ep_softc *sc = arg;
    727   1.41   thorpej 	int s;
    728   1.15  jonathan 
    729   1.41   thorpej #ifdef DIAGNOSTIC
    730   1.47      fvdl 	if ((sc->ep_flags & ELINK_FLAGS_MII) == 0)
    731   1.41   thorpej 		panic("ep_tick");
    732   1.41   thorpej #endif
    733   1.31  jonathan 
    734  1.115   thorpej 	if (!device_is_active(&sc->sc_dev))
    735   1.74     enami 		return;
    736   1.74     enami 
    737   1.41   thorpej 	s = splnet();
    738   1.41   thorpej 	mii_tick(&sc->sc_mii);
    739   1.41   thorpej 	splx(s);
    740   1.15  jonathan 
    741   1.78   thorpej 	callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
    742   1.15  jonathan }
    743   1.15  jonathan 
    744   1.15  jonathan /*
    745   1.20  jonathan  * Bring device up.
    746   1.20  jonathan  *
    747    1.1   thorpej  * The order in here seems important. Otherwise we may not receive
    748    1.1   thorpej  * interrupts. ?!
    749    1.1   thorpej  */
    750   1.88  jdolecek int
    751   1.88  jdolecek epinit(ifp)
    752   1.88  jdolecek 	struct ifnet *ifp;
    753    1.1   thorpej {
    754   1.88  jdolecek 	struct ep_softc *sc = ifp->if_softc;
    755   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    756   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    757   1.88  jdolecek 	int i, error;
    758  1.107   mycroft 	u_int8_t *addr;
    759   1.88  jdolecek 
    760   1.88  jdolecek 	if (!sc->enabled && (error = epenable(sc)) != 0)
    761   1.88  jdolecek 		return (error);
    762    1.1   thorpej 
    763   1.88  jdolecek 	/* Make sure any pending reset has completed before touching board */
    764   1.56  jonathan 	ep_finish_reset(iot, ioh);
    765   1.56  jonathan 
    766   1.62     enami 	/*
    767   1.88  jdolecek 	 * Cancel any pending I/O.
    768   1.62     enami 	 */
    769   1.88  jdolecek 	epstop(ifp, 0);
    770    1.1   thorpej 
    771   1.89  jdolecek 	if (sc->bustype != ELINK_BUS_PCI && sc->bustype != ELINK_BUS_EISA
    772   1.89  jdolecek 	    && sc->bustype != ELINK_BUS_MCA) {
    773    1.1   thorpej 		GO_WINDOW(0);
    774   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL, 0);
    775   1.60     enami 		bus_space_write_2(iot, ioh, ELINK_W0_CONFIG_CTRL,
    776   1.60     enami 		    ENABLE_DRQ_IRQ);
    777    1.1   thorpej 	}
    778    1.1   thorpej 
    779   1.47      fvdl 	if (sc->bustype == ELINK_BUS_PCMCIA) {
    780   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W0_RESOURCE_CFG, 0x3f00);
    781    1.1   thorpej 	}
    782    1.1   thorpej 
    783    1.1   thorpej 	GO_WINDOW(2);
    784  1.107   mycroft 	/* Reload the ether_addr. */
    785  1.107   mycroft 	addr = LLADDR(ifp->if_sadl);
    786  1.107   mycroft 	for (i = 0; i < 6; i += 2)
    787  1.107   mycroft 		bus_space_write_2(iot, ioh, ELINK_W2_ADDR_0 + i,
    788  1.107   mycroft 		    (addr[i] << 0) | (addr[i + 1] << 8));
    789    1.8  christos 
    790   1.12  jonathan 	/*
    791   1.12  jonathan 	 * Reset the station-address receive filter.
    792   1.41   thorpej 	 * A bug workaround for busmastering (Vortex, Demon) cards.
    793   1.12  jonathan 	 */
    794  1.107   mycroft 	for (i = 0; i < 6; i += 2)
    795  1.107   mycroft 		bus_space_write_2(iot, ioh, ELINK_W2_RECVMASK_0 + i, 0);
    796    1.1   thorpej 
    797   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    798   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    799    1.1   thorpej 
    800    1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    801    1.1   thorpej 	for (i = 0; i < 31; i++)
    802  1.116  nakayama 		(void)bus_space_read_2(iot, ioh,
    803  1.116  nakayama 				       ep_w1_reg(sc, ELINK_W1_TX_STATUS));
    804   1.31  jonathan 
    805   1.88  jdolecek 	/* Set threshold for Tx-space available interrupt. */
    806   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    807   1.31  jonathan 	    SET_TX_AVAIL_THRESH | (1600 >> sc->ep_pktlenshift));
    808    1.1   thorpej 
    809   1.47      fvdl 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
    810   1.44   thorpej 		/*
    811   1.44   thorpej 		 * Enable options in the PCMCIA LAN COR register, via
    812   1.44   thorpej 		 * RoadRunner Window 1.
    813   1.44   thorpej 		 *
    814   1.44   thorpej 		 * XXX MAGIC CONSTANTS!
    815   1.44   thorpej 		 */
    816   1.44   thorpej 		u_int16_t cor;
    817   1.44   thorpej 
    818   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, (1 << 11));
    819   1.44   thorpej 
    820   1.44   thorpej 		cor = bus_space_read_2(iot, ioh, 0) & ~0x30;
    821   1.47      fvdl 		if (sc->ep_flags & ELINK_FLAGS_USESHAREDMEM)
    822   1.44   thorpej 			cor |= 0x10;
    823   1.47      fvdl 		if (sc->ep_flags & ELINK_FLAGS_FORCENOWAIT)
    824   1.44   thorpej 			cor |= 0x20;
    825   1.44   thorpej 		bus_space_write_2(iot, ioh, 0, cor);
    826   1.44   thorpej 
    827   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
    828   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
    829   1.59   thorpej 
    830   1.59   thorpej 		if (sc->ep_flags & ELINK_FLAGS_MII) {
    831   1.59   thorpej 			ep_roadrunner_mii_enable(sc);
    832   1.59   thorpej 			GO_WINDOW(1);
    833   1.59   thorpej 		}
    834   1.44   thorpej 	}
    835   1.44   thorpej 
    836   1.18  jonathan 	/* Enable interrupts. */
    837   1.60     enami 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    838  1.102  christos 	    SET_RD_0_MASK | WATCHED_INTERRUPTS);
    839   1.60     enami 	bus_space_write_2(iot, ioh, ELINK_COMMAND,
    840  1.102  christos 	    SET_INTR_MASK | WATCHED_INTERRUPTS);
    841    1.1   thorpej 
    842    1.1   thorpej 	/*
    843   1.98       wiz 	 * Attempt to get rid of any stray interrupts that occurred during
    844    1.1   thorpej 	 * configuration.  On the i386 this isn't possible because one may
    845    1.1   thorpej 	 * already be queued.  However, a single stray interrupt is
    846    1.1   thorpej 	 * unimportant.
    847    1.1   thorpej 	 */
    848   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | 0xff);
    849    1.1   thorpej 
    850    1.1   thorpej 	epsetfilter(sc);
    851   1.41   thorpej 	epsetmedia(sc);
    852    1.1   thorpej 
    853   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_ENABLE);
    854   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
    855    1.1   thorpej 
    856    1.1   thorpej 	epmbuffill(sc);
    857    1.1   thorpej 
    858    1.1   thorpej 	/* Interface is now `running', with no output active. */
    859    1.1   thorpej 	ifp->if_flags |= IFF_RUNNING;
    860    1.1   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    861    1.1   thorpej 
    862   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_MII) {
    863   1.41   thorpej 		/* Start the one second clock. */
    864   1.78   thorpej 		callout_reset(&sc->sc_mii_callout, hz, ep_tick, sc);
    865   1.41   thorpej 	}
    866   1.41   thorpej 
    867    1.1   thorpej 	/* Attempt to start output, if any. */
    868    1.1   thorpej 	epstart(ifp);
    869   1.88  jdolecek 
    870   1.88  jdolecek 	return (0);
    871    1.1   thorpej }
    872    1.1   thorpej 
    873   1.20  jonathan 
    874   1.20  jonathan /*
    875  1.111     perry  * Set multicast receive filter.
    876   1.20  jonathan  * elink3 hardware has no selective multicast filter in hardware.
    877   1.20  jonathan  * Enable reception of all multicasts and filter in software.
    878   1.20  jonathan  */
    879    1.1   thorpej void
    880    1.1   thorpej epsetfilter(sc)
    881   1.79  augustss 	struct ep_softc *sc;
    882    1.1   thorpej {
    883   1.79  augustss 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    884    1.1   thorpej 
    885    1.1   thorpej 	GO_WINDOW(1);		/* Window 1 is operating window */
    886   1.60     enami 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, ELINK_COMMAND,
    887   1.60     enami 	    SET_RX_FILTER | FIL_INDIVIDUAL | FIL_BRDCST |
    888   1.60     enami 	    ((ifp->if_flags & IFF_MULTICAST) ? FIL_MULTICAST : 0) |
    889   1.60     enami 	    ((ifp->if_flags & IFF_PROMISC) ? FIL_PROMISC : 0));
    890    1.1   thorpej }
    891    1.1   thorpej 
    892   1.23  jonathan int
    893   1.23  jonathan ep_media_change(ifp)
    894   1.23  jonathan 	struct ifnet *ifp;
    895   1.23  jonathan {
    896   1.79  augustss 	struct ep_softc *sc = ifp->if_softc;
    897   1.23  jonathan 
    898   1.41   thorpej 	if (sc->enabled && (ifp->if_flags & IFF_UP) != 0)
    899   1.41   thorpej 		epreset(sc);
    900   1.34   thorpej 
    901   1.34   thorpej 	return (0);
    902   1.23  jonathan }
    903   1.23  jonathan 
    904   1.15  jonathan /*
    905   1.59   thorpej  * Reset and enable the MII on the RoadRunner.
    906   1.59   thorpej  */
    907   1.59   thorpej void
    908   1.59   thorpej ep_roadrunner_mii_enable(sc)
    909   1.59   thorpej 	struct ep_softc *sc;
    910   1.59   thorpej {
    911   1.59   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    912   1.59   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    913   1.59   thorpej 
    914   1.59   thorpej 	GO_WINDOW(3);
    915   1.59   thorpej 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    916   1.59   thorpej 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
    917   1.59   thorpej 	delay(1000);
    918   1.59   thorpej 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    919   1.59   thorpej 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_MII_RESET|
    920   1.59   thorpej 	    ELINK_RUNNER_ENABLE_MII);
    921   1.59   thorpej 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
    922   1.59   thorpej 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
    923   1.59   thorpej 	delay(1000);
    924   1.59   thorpej 	bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    925   1.59   thorpej 	    ELINK_PCI_100BASE_MII|ELINK_RUNNER_ENABLE_MII);
    926   1.59   thorpej }
    927   1.59   thorpej 
    928   1.59   thorpej /*
    929   1.41   thorpej  * Set the card to use the specified media.
    930   1.15  jonathan  */
    931   1.34   thorpej void
    932   1.41   thorpej epsetmedia(sc)
    933   1.41   thorpej 	struct ep_softc *sc;
    934    1.1   thorpej {
    935   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
    936   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
    937   1.23  jonathan 
    938   1.41   thorpej 	/* Turn everything off.  First turn off linkbeat and UTP. */
    939    1.1   thorpej 	GO_WINDOW(4);
    940   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE, 0x0);
    941   1.23  jonathan 
    942   1.23  jonathan 	/* Turn off coax */
    943   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
    944   1.23  jonathan 	delay(1000);
    945   1.23  jonathan 
    946   1.29  jonathan 	/*
    947   1.41   thorpej 	 * If the device has MII, select it, and then tell the
    948   1.41   thorpej 	 * PHY which media to use.
    949   1.41   thorpej 	 */
    950   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_MII) {
    951   1.41   thorpej 		int config0, config1;
    952   1.41   thorpej 
    953   1.41   thorpej 		GO_WINDOW(3);
    954   1.44   thorpej 
    955   1.47      fvdl 		if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
    956   1.44   thorpej 			int resopt;
    957   1.44   thorpej 
    958   1.44   thorpej 			resopt = bus_space_read_2(iot, ioh,
    959   1.47      fvdl 			    ELINK_W3_RESET_OPTIONS);
    960   1.60     enami 			bus_space_write_2(iot, ioh, ELINK_W3_RESET_OPTIONS,
    961   1.60     enami 			    resopt | ELINK_RUNNER_ENABLE_MII);
    962   1.44   thorpej 		}
    963   1.44   thorpej 
    964   1.41   thorpej 		config0 = (u_int)bus_space_read_2(iot, ioh,
    965   1.47      fvdl 		    ELINK_W3_INTERNAL_CONFIG);
    966   1.41   thorpej 		config1 = (u_int)bus_space_read_2(iot, ioh,
    967   1.47      fvdl 		    ELINK_W3_INTERNAL_CONFIG + 2);
    968   1.41   thorpej 
    969   1.41   thorpej 		config1 = config1 & ~CONFIG_MEDIAMASK;
    970   1.47      fvdl 		config1 |= (ELINKMEDIA_MII << CONFIG_MEDIAMASK_SHIFT);
    971   1.41   thorpej 
    972   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
    973   1.60     enami 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
    974   1.60     enami 		    config1);
    975   1.41   thorpej 		GO_WINDOW(1);	/* back to operating window */
    976   1.41   thorpej 
    977   1.41   thorpej 		mii_mediachg(&sc->sc_mii);
    978   1.41   thorpej 		return;
    979   1.41   thorpej 	}
    980   1.41   thorpej 
    981   1.41   thorpej 	/*
    982   1.29  jonathan 	 * Now turn on the selected media/transceiver.
    983   1.29  jonathan 	 */
    984   1.29  jonathan 	GO_WINDOW(4);
    985   1.41   thorpej 	switch (IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_cur->ifm_media)) {
    986   1.41   thorpej 	case IFM_10_T:
    987   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
    988   1.41   thorpej 		    JABBER_GUARD_ENABLE|LINKBEAT_ENABLE);
    989   1.23  jonathan 		break;
    990   1.23  jonathan 
    991   1.41   thorpej 	case IFM_10_2:
    992   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND, START_TRANSCEIVER);
    993   1.23  jonathan 		DELAY(1000);	/* 50ms not enmough? */
    994   1.23  jonathan 		break;
    995   1.23  jonathan 
    996   1.41   thorpej 	case IFM_100_TX:
    997   1.41   thorpej 	case IFM_100_FX:
    998   1.41   thorpej 	case IFM_100_T4:		/* XXX check documentation */
    999   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
   1000   1.41   thorpej 		    LINKBEAT_ENABLE);
   1001   1.23  jonathan 		DELAY(1000);	/* not strictly necessary? */
   1002   1.23  jonathan 		break;
   1003   1.23  jonathan 
   1004   1.41   thorpej 	case IFM_10_5:
   1005   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W4_MEDIA_TYPE,
   1006   1.41   thorpej 		    SQE_ENABLE);
   1007   1.41   thorpej 		DELAY(1000);	/* not strictly necessary? */
   1008   1.41   thorpej 		break;
   1009   1.41   thorpej 
   1010   1.41   thorpej 	case IFM_MANUAL:
   1011   1.41   thorpej 		/*
   1012   1.41   thorpej 		 * Nothing to do here; we are actually enabling the
   1013   1.41   thorpej 		 * external PHY on the MII port.
   1014   1.41   thorpej 		 */
   1015   1.23  jonathan 		break;
   1016   1.41   thorpej 
   1017   1.41   thorpej 	case IFM_NONE:
   1018   1.41   thorpej 		printf("%s: interface disabled\n", sc->sc_dev.dv_xname);
   1019   1.41   thorpej 		return;
   1020   1.41   thorpej 
   1021   1.23  jonathan 	default:
   1022   1.41   thorpej 		panic("epsetmedia: impossible");
   1023    1.1   thorpej 	}
   1024   1.23  jonathan 
   1025   1.23  jonathan 	/*
   1026   1.41   thorpej 	 * Tell the chip which port to use.
   1027   1.23  jonathan 	 */
   1028   1.41   thorpej 	switch (sc->ep_chipset) {
   1029   1.47      fvdl 	case ELINK_CHIPSET_VORTEX:
   1030   1.47      fvdl 	case ELINK_CHIPSET_BOOMERANG:
   1031   1.41   thorpej 	    {
   1032   1.45   thorpej 		int mctl, config0, config1;
   1033   1.23  jonathan 
   1034   1.23  jonathan 		GO_WINDOW(3);
   1035   1.23  jonathan 		config0 = (u_int)bus_space_read_2(iot, ioh,
   1036   1.47      fvdl 		    ELINK_W3_INTERNAL_CONFIG);
   1037   1.23  jonathan 		config1 = (u_int)bus_space_read_2(iot, ioh,
   1038   1.47      fvdl 		    ELINK_W3_INTERNAL_CONFIG + 2);
   1039   1.23  jonathan 
   1040   1.23  jonathan 		config1 = config1 & ~CONFIG_MEDIAMASK;
   1041   1.41   thorpej 		config1 |= (sc->sc_mii.mii_media.ifm_cur->ifm_data <<
   1042   1.41   thorpej 		    CONFIG_MEDIAMASK_SHIFT);
   1043   1.41   thorpej 
   1044   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG, config0);
   1045   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_INTERNAL_CONFIG + 2,
   1046   1.47      fvdl 		    config1);
   1047   1.45   thorpej 
   1048   1.47      fvdl 		mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   1049   1.45   thorpej 		if (sc->sc_mii.mii_media.ifm_cur->ifm_media & IFM_FDX)
   1050   1.45   thorpej 			mctl |= MAC_CONTROL_FDX;
   1051   1.45   thorpej 		else
   1052   1.45   thorpej 			mctl &= ~MAC_CONTROL_FDX;
   1053   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   1054   1.41   thorpej 		break;
   1055   1.41   thorpej 	    }
   1056   1.41   thorpej 	default:
   1057   1.41   thorpej 	    {
   1058   1.41   thorpej 		int w0_addr_cfg;
   1059   1.28     veego 
   1060   1.28     veego 		GO_WINDOW(0);
   1061   1.47      fvdl 		w0_addr_cfg = bus_space_read_2(iot, ioh, ELINK_W0_ADDRESS_CFG);
   1062   1.29  jonathan 		w0_addr_cfg &= 0x3fff;
   1063   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W0_ADDRESS_CFG, w0_addr_cfg |
   1064   1.41   thorpej 		    (sc->sc_mii.mii_media.ifm_cur->ifm_data << 14));
   1065   1.28     veego 		DELAY(1000);
   1066   1.41   thorpej 		break;
   1067   1.41   thorpej 	    }
   1068   1.23  jonathan 	}
   1069   1.23  jonathan 
   1070   1.23  jonathan 	GO_WINDOW(1);		/* Window 1 is operating window */
   1071   1.23  jonathan }
   1072   1.23  jonathan 
   1073   1.23  jonathan /*
   1074   1.23  jonathan  * Get currently-selected media from card.
   1075   1.23  jonathan  * (if_media callback, may be called before interface is brought up).
   1076   1.23  jonathan  */
   1077   1.23  jonathan void
   1078   1.23  jonathan ep_media_status(ifp, req)
   1079   1.23  jonathan 	struct ifnet *ifp;
   1080   1.23  jonathan 	struct ifmediareq *req;
   1081   1.23  jonathan {
   1082   1.79  augustss 	struct ep_softc *sc = ifp->if_softc;
   1083   1.23  jonathan 	bus_space_tag_t iot = sc->sc_iot;
   1084   1.23  jonathan 	bus_space_handle_t ioh = sc->sc_ioh;
   1085   1.23  jonathan 
   1086   1.34   thorpej 	if (sc->enabled == 0) {
   1087   1.34   thorpej 		req->ifm_active = IFM_ETHER|IFM_NONE;
   1088   1.34   thorpej 		req->ifm_status = 0;
   1089   1.34   thorpej 		return;
   1090   1.34   thorpej 	}
   1091   1.34   thorpej 
   1092   1.41   thorpej 	/*
   1093   1.41   thorpej 	 * If we have MII, go ask the PHY what's going on.
   1094   1.41   thorpej 	 */
   1095   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   1096   1.41   thorpej 		mii_pollstat(&sc->sc_mii);
   1097   1.41   thorpej 		req->ifm_active = sc->sc_mii.mii_media_active;
   1098   1.41   thorpej 		req->ifm_status = sc->sc_mii.mii_media_status;
   1099   1.41   thorpej 		return;
   1100   1.41   thorpej 	}
   1101   1.41   thorpej 
   1102   1.41   thorpej 	/*
   1103   1.41   thorpej 	 * Ok, at this point we claim that our active media is
   1104   1.41   thorpej 	 * the currently selected media.  We'll update our status
   1105   1.41   thorpej 	 * if our chipset allows us to detect link.
   1106   1.41   thorpej 	 */
   1107   1.41   thorpej 	req->ifm_active = sc->sc_mii.mii_media.ifm_cur->ifm_media;
   1108   1.41   thorpej 	req->ifm_status = 0;
   1109   1.41   thorpej 
   1110   1.23  jonathan 	switch (sc->ep_chipset) {
   1111   1.47      fvdl 	case ELINK_CHIPSET_VORTEX:
   1112   1.47      fvdl 	case ELINK_CHIPSET_BOOMERANG:
   1113   1.23  jonathan 		GO_WINDOW(4);
   1114   1.41   thorpej 		req->ifm_status = IFM_AVALID;
   1115   1.47      fvdl 		if (bus_space_read_2(iot, ioh, ELINK_W4_MEDIA_TYPE) &
   1116   1.41   thorpej 		    LINKBEAT_DETECT)
   1117   1.41   thorpej 			req->ifm_status |= IFM_ACTIVE;
   1118   1.41   thorpej 		GO_WINDOW(1);	/* back to operating window */
   1119   1.23  jonathan 		break;
   1120    1.1   thorpej 	}
   1121    1.1   thorpej }
   1122    1.1   thorpej 
   1123   1.23  jonathan 
   1124   1.23  jonathan 
   1125    1.1   thorpej /*
   1126    1.1   thorpej  * Start outputting on the interface.
   1127    1.1   thorpej  * Always called as splnet().
   1128    1.1   thorpej  */
   1129    1.1   thorpej void
   1130    1.1   thorpej epstart(ifp)
   1131    1.1   thorpej 	struct ifnet *ifp;
   1132    1.1   thorpej {
   1133   1.79  augustss 	struct ep_softc *sc = ifp->if_softc;
   1134   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1135   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1136    1.1   thorpej 	struct mbuf *m, *m0;
   1137    1.1   thorpej 	int sh, len, pad;
   1138  1.101     soren 	bus_size_t txreg;
   1139    1.1   thorpej 
   1140    1.1   thorpej 	/* Don't transmit if interface is busy or not running */
   1141   1.28     veego 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
   1142    1.1   thorpej 		return;
   1143    1.1   thorpej 
   1144    1.1   thorpej startagain:
   1145    1.1   thorpej 	/* Sneak a peek at the next packet */
   1146   1.86   thorpej 	IFQ_POLL(&ifp->if_snd, m0);
   1147    1.1   thorpej 	if (m0 == 0)
   1148    1.1   thorpej 		return;
   1149    1.1   thorpej 
   1150    1.1   thorpej 	/* We need to use m->m_pkthdr.len, so require the header */
   1151    1.1   thorpej 	if ((m0->m_flags & M_PKTHDR) == 0)
   1152    1.1   thorpej 		panic("epstart: no header mbuf");
   1153    1.1   thorpej 	len = m0->m_pkthdr.len;
   1154    1.1   thorpej 
   1155    1.1   thorpej 	pad = (4 - len) & 3;
   1156    1.1   thorpej 
   1157    1.1   thorpej 	/*
   1158    1.1   thorpej 	 * The 3c509 automatically pads short packets to minimum ethernet
   1159    1.1   thorpej 	 * length, but we drop packets that are too large. Perhaps we should
   1160    1.1   thorpej 	 * truncate them instead?
   1161    1.1   thorpej 	 */
   1162    1.1   thorpej 	if (len + pad > ETHER_MAX_LEN) {
   1163    1.1   thorpej 		/* packet is obviously too large: toss it */
   1164    1.1   thorpej 		++ifp->if_oerrors;
   1165   1.86   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1166    1.1   thorpej 		m_freem(m0);
   1167    1.1   thorpej 		goto readcheck;
   1168    1.1   thorpej 	}
   1169    1.1   thorpej 
   1170   1.47      fvdl 	if (bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_FREE_TX)) <
   1171   1.42   thorpej 	    len + pad + 4) {
   1172   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1173   1.12  jonathan 		    SET_TX_AVAIL_THRESH |
   1174   1.12  jonathan 		    ((len + pad + 4) >> sc->ep_pktlenshift));
   1175    1.1   thorpej 		/* not enough room in FIFO */
   1176    1.1   thorpej 		ifp->if_flags |= IFF_OACTIVE;
   1177    1.1   thorpej 		return;
   1178    1.1   thorpej 	} else {
   1179   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND,
   1180   1.60     enami 		    SET_TX_AVAIL_THRESH | ELINK_THRESH_DISABLE);
   1181    1.1   thorpej 	}
   1182    1.1   thorpej 
   1183   1.86   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m0);
   1184    1.1   thorpej 	if (m0 == 0)		/* not really needed */
   1185    1.1   thorpej 		return;
   1186    1.1   thorpej 
   1187   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_TX_START_THRESH |
   1188   1.60     enami 	    ((len / 4 + sc->tx_start_thresh) /* >> sc->ep_pktlenshift*/));
   1189    1.1   thorpej 
   1190    1.1   thorpej #if NBPFILTER > 0
   1191    1.1   thorpej 	if (ifp->if_bpf)
   1192    1.1   thorpej 		bpf_mtap(ifp->if_bpf, m0);
   1193    1.1   thorpej #endif
   1194    1.1   thorpej 
   1195    1.1   thorpej 	/*
   1196   1.82   thorpej 	 * Do the output at a high interrupt priority level so that an
   1197   1.82   thorpej 	 * interrupt from another device won't cause a FIFO underrun.
   1198   1.82   thorpej 	 * We choose splsched() since that blocks essentially everything
   1199   1.82   thorpej 	 * except for interrupts from serial devices (which typically
   1200   1.88  jdolecek 	 * lose data if their interrupt isn't serviced fast enough).
   1201   1.82   thorpej 	 *
   1202   1.82   thorpej 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
   1203    1.1   thorpej 	 */
   1204   1.82   thorpej 	sh = splsched();
   1205    1.1   thorpej 
   1206   1.47      fvdl 	txreg = ep_w1_reg(sc, ELINK_W1_TX_PIO_WR_1);
   1207   1.42   thorpej 
   1208   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
   1209   1.44   thorpej 		/*
   1210   1.44   thorpej 		 * Prime the FIFO buffer counter (number of 16-bit
   1211   1.44   thorpej 		 * words about to be written to the FIFO).
   1212   1.44   thorpej 		 *
   1213   1.44   thorpej 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
   1214   1.44   thorpej 		 * COUNTER IS NON-ZERO!
   1215   1.44   thorpej 		 */
   1216   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL,
   1217   1.44   thorpej 		    (len + pad) >> 1);
   1218   1.44   thorpej 	}
   1219   1.44   thorpej 
   1220   1.42   thorpej 	bus_space_write_2(iot, ioh, txreg, len);
   1221   1.42   thorpej 	bus_space_write_2(iot, ioh, txreg, 0xffff); /* Second is meaningless */
   1222   1.47      fvdl 	if (ELINK_IS_BUS_32(sc->bustype)) {
   1223   1.60     enami 		for (m = m0; m;) {
   1224   1.14       cjs 			if (m->m_len > 3)  {
   1225   1.14       cjs 				/* align our reads from core */
   1226   1.14       cjs 				if (mtod(m, u_long) & 3)  {
   1227   1.14       cjs 					u_long count =
   1228   1.14       cjs 					    4 - (mtod(m, u_long) & 3);
   1229   1.14       cjs 					bus_space_write_multi_1(iot, ioh,
   1230   1.42   thorpej 					    txreg, mtod(m, u_int8_t *), count);
   1231   1.14       cjs 					m->m_data =
   1232   1.14       cjs 					    (void *)(mtod(m, u_long) + count);
   1233   1.14       cjs 					m->m_len -= count;
   1234   1.14       cjs 				}
   1235   1.55  jonathan 				bus_space_write_multi_stream_4(iot, ioh,
   1236   1.42   thorpej 				    txreg, mtod(m, u_int32_t *), m->m_len >> 2);
   1237   1.14       cjs 				m->m_data = (void *)(mtod(m, u_long) +
   1238   1.14       cjs 					(u_long)(m->m_len & ~3));
   1239   1.14       cjs 				m->m_len -= m->m_len & ~3;
   1240   1.14       cjs 			}
   1241   1.14       cjs 			if (m->m_len)  {
   1242   1.11   thorpej 				bus_space_write_multi_1(iot, ioh,
   1243   1.42   thorpej 				    txreg, mtod(m, u_int8_t *), m->m_len);
   1244   1.14       cjs 			}
   1245    1.1   thorpej 			MFREE(m, m0);
   1246    1.1   thorpej 			m = m0;
   1247    1.1   thorpej 		}
   1248    1.1   thorpej 	} else {
   1249   1.60     enami 		for (m = m0; m;) {
   1250   1.14       cjs 			if (m->m_len > 1)  {
   1251   1.14       cjs 				if (mtod(m, u_long) & 1)  {
   1252   1.14       cjs 					bus_space_write_1(iot, ioh,
   1253   1.42   thorpej 					    txreg, *(mtod(m, u_int8_t *)));
   1254   1.14       cjs 					m->m_data =
   1255   1.14       cjs 					    (void *)(mtod(m, u_long) + 1);
   1256   1.14       cjs 					m->m_len -= 1;
   1257   1.14       cjs 				}
   1258   1.55  jonathan 				bus_space_write_multi_stream_2(iot, ioh,
   1259   1.42   thorpej 				    txreg, mtod(m, u_int16_t *),
   1260   1.14       cjs 				    m->m_len >> 1);
   1261   1.14       cjs 			}
   1262   1.14       cjs 			if (m->m_len & 1)  {
   1263   1.42   thorpej 				bus_space_write_1(iot, ioh, txreg,
   1264    1.2   thorpej 				     *(mtod(m, u_int8_t *) + m->m_len - 1));
   1265   1.14       cjs 			}
   1266    1.1   thorpej 			MFREE(m, m0);
   1267    1.1   thorpej 			m = m0;
   1268    1.1   thorpej 		}
   1269    1.1   thorpej 	}
   1270    1.1   thorpej 	while (pad--)
   1271   1.42   thorpej 		bus_space_write_1(iot, ioh, txreg, 0);
   1272    1.1   thorpej 
   1273    1.1   thorpej 	splx(sh);
   1274    1.1   thorpej 
   1275    1.1   thorpej 	++ifp->if_opackets;
   1276    1.1   thorpej 
   1277    1.1   thorpej readcheck:
   1278   1.47      fvdl 	if ((bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS)) &
   1279   1.42   thorpej 	    ERR_INCOMPLETE) == 0) {
   1280    1.1   thorpej 		/* We received a complete packet. */
   1281   1.47      fvdl 		u_int16_t status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1282    1.1   thorpej 
   1283  1.102  christos 		if ((status & INTR_LATCH) == 0) {
   1284    1.1   thorpej 			/*
   1285    1.1   thorpej 			 * No interrupt, read the packet and continue
   1286  1.111     perry 			 * Is  this supposed to happen? Is my motherboard
   1287    1.1   thorpej 			 * completely busted?
   1288    1.1   thorpej 			 */
   1289    1.1   thorpej 			epread(sc);
   1290   1.28     veego 		} else {
   1291    1.1   thorpej 			/* Got an interrupt, return so that it gets serviced. */
   1292    1.1   thorpej 			return;
   1293   1.28     veego 		}
   1294   1.28     veego 	} else {
   1295    1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
   1296    1.1   thorpej 		if (epstatus(sc)) {
   1297    1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
   1298   1.10  christos 				printf("%s: adapter reset\n",
   1299    1.9  christos 				    sc->sc_dev.dv_xname);
   1300    1.1   thorpej 			epreset(sc);
   1301    1.1   thorpej 		}
   1302    1.1   thorpej 	}
   1303    1.1   thorpej 
   1304    1.1   thorpej 	goto startagain;
   1305    1.1   thorpej }
   1306    1.1   thorpej 
   1307    1.1   thorpej 
   1308    1.1   thorpej /*
   1309    1.1   thorpej  * XXX: The 3c509 card can get in a mode where both the fifo status bit
   1310    1.1   thorpej  *	FIFOS_RX_OVERRUN and the status bit ERR_INCOMPLETE are set
   1311    1.1   thorpej  *	We detect this situation and we reset the adapter.
   1312    1.1   thorpej  *	It happens at times when there is a lot of broadcast traffic
   1313    1.1   thorpej  *	on the cable (once in a blue moon).
   1314    1.1   thorpej  */
   1315    1.1   thorpej static int
   1316    1.1   thorpej epstatus(sc)
   1317   1.79  augustss 	struct ep_softc *sc;
   1318    1.1   thorpej {
   1319   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1320   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1321    1.7   thorpej 	u_int16_t fifost;
   1322    1.1   thorpej 
   1323    1.1   thorpej 	/*
   1324    1.1   thorpej 	 * Check the FIFO status and act accordingly
   1325    1.1   thorpej 	 */
   1326    1.1   thorpej 	GO_WINDOW(4);
   1327   1.47      fvdl 	fifost = bus_space_read_2(iot, ioh, ELINK_W4_FIFO_DIAG);
   1328    1.1   thorpej 	GO_WINDOW(1);
   1329    1.1   thorpej 
   1330    1.1   thorpej 	if (fifost & FIFOS_RX_UNDERRUN) {
   1331   1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1332   1.10  christos 			printf("%s: RX underrun\n", sc->sc_dev.dv_xname);
   1333    1.1   thorpej 		epreset(sc);
   1334    1.1   thorpej 		return 0;
   1335    1.1   thorpej 	}
   1336    1.1   thorpej 
   1337    1.1   thorpej 	if (fifost & FIFOS_RX_STATUS_OVERRUN) {
   1338   1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1339   1.10  christos 			printf("%s: RX Status overrun\n", sc->sc_dev.dv_xname);
   1340    1.1   thorpej 		return 1;
   1341    1.1   thorpej 	}
   1342    1.1   thorpej 
   1343    1.1   thorpej 	if (fifost & FIFOS_RX_OVERRUN) {
   1344   1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1345   1.10  christos 			printf("%s: RX overrun\n", sc->sc_dev.dv_xname);
   1346    1.1   thorpej 		return 1;
   1347    1.1   thorpej 	}
   1348    1.1   thorpej 
   1349    1.1   thorpej 	if (fifost & FIFOS_TX_OVERRUN) {
   1350   1.21        is 		if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1351   1.10  christos 			printf("%s: TX overrun\n", sc->sc_dev.dv_xname);
   1352    1.1   thorpej 		epreset(sc);
   1353    1.1   thorpej 		return 0;
   1354    1.1   thorpej 	}
   1355    1.1   thorpej 
   1356    1.1   thorpej 	return 0;
   1357    1.1   thorpej }
   1358    1.1   thorpej 
   1359    1.1   thorpej 
   1360    1.1   thorpej static void
   1361    1.1   thorpej eptxstat(sc)
   1362   1.79  augustss 	struct ep_softc *sc;
   1363    1.1   thorpej {
   1364   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1365   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1366    1.1   thorpej 	int i;
   1367    1.1   thorpej 
   1368    1.1   thorpej 	/*
   1369    1.1   thorpej 	 * We need to read+write TX_STATUS until we get a 0 status
   1370    1.1   thorpej 	 * in order to turn off the interrupt flag.
   1371    1.1   thorpej 	 */
   1372  1.107   mycroft 	while ((i = bus_space_read_2(iot, ioh,
   1373   1.60     enami 	     ep_w1_reg(sc, ELINK_W1_TX_STATUS))) & TXS_COMPLETE) {
   1374  1.107   mycroft 		bus_space_write_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_TX_STATUS),
   1375   1.42   thorpej 		    0x0);
   1376    1.1   thorpej 
   1377    1.1   thorpej 		if (i & TXS_JABBER) {
   1378   1.21        is 			++sc->sc_ethercom.ec_if.if_oerrors;
   1379   1.21        is 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1380   1.10  christos 				printf("%s: jabber (%x)\n",
   1381    1.1   thorpej 				       sc->sc_dev.dv_xname, i);
   1382    1.1   thorpej 			epreset(sc);
   1383    1.1   thorpej 		} else if (i & TXS_UNDERRUN) {
   1384   1.21        is 			++sc->sc_ethercom.ec_if.if_oerrors;
   1385   1.21        is 			if (sc->sc_ethercom.ec_if.if_flags & IFF_DEBUG)
   1386   1.10  christos 				printf("%s: fifo underrun (%x) @%d\n",
   1387    1.1   thorpej 				       sc->sc_dev.dv_xname, i,
   1388    1.1   thorpej 				       sc->tx_start_thresh);
   1389    1.1   thorpej 			if (sc->tx_succ_ok < 100)
   1390    1.1   thorpej 				    sc->tx_start_thresh = min(ETHER_MAX_LEN,
   1391    1.1   thorpej 					    sc->tx_start_thresh + 20);
   1392    1.1   thorpej 			sc->tx_succ_ok = 0;
   1393    1.1   thorpej 			epreset(sc);
   1394    1.1   thorpej 		} else if (i & TXS_MAX_COLLISION) {
   1395   1.21        is 			++sc->sc_ethercom.ec_if.if_collisions;
   1396   1.47      fvdl 			bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_ENABLE);
   1397   1.21        is 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1398    1.1   thorpej 		} else
   1399    1.1   thorpej 			sc->tx_succ_ok = (sc->tx_succ_ok+1) & 127;
   1400    1.1   thorpej 	}
   1401    1.1   thorpej }
   1402    1.1   thorpej 
   1403    1.1   thorpej int
   1404    1.1   thorpej epintr(arg)
   1405    1.1   thorpej 	void *arg;
   1406    1.1   thorpej {
   1407   1.79  augustss 	struct ep_softc *sc = arg;
   1408   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1409   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1410   1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1411    1.7   thorpej 	u_int16_t status;
   1412    1.1   thorpej 	int ret = 0;
   1413    1.1   thorpej 
   1414  1.115   thorpej 	if (sc->enabled == 0 || !device_is_active(&sc->sc_dev))
   1415   1.34   thorpej 		return (0);
   1416   1.34   thorpej 
   1417  1.102  christos 
   1418    1.1   thorpej 	for (;;) {
   1419   1.47      fvdl 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1420    1.1   thorpej 
   1421  1.102  christos 		if ((status & WATCHED_INTERRUPTS) == 0) {
   1422  1.102  christos 			if ((status & INTR_LATCH) == 0) {
   1423   1.34   thorpej #if 0
   1424  1.102  christos 				printf("%s: intr latch cleared\n",
   1425  1.102  christos 				       sc->sc_dev.dv_xname);
   1426   1.34   thorpej #endif
   1427   1.34   thorpej 				break;
   1428   1.34   thorpej 			}
   1429   1.34   thorpej 		}
   1430    1.1   thorpej 
   1431    1.1   thorpej 		ret = 1;
   1432    1.1   thorpej 
   1433    1.1   thorpej 		/*
   1434    1.1   thorpej 		 * Acknowledge any interrupts.  It's important that we do this
   1435    1.1   thorpej 		 * first, since there would otherwise be a race condition.
   1436    1.1   thorpej 		 * Due to the i386 interrupt queueing, we may get spurious
   1437    1.1   thorpej 		 * interrupts occasionally.
   1438    1.1   thorpej 		 */
   1439   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR |
   1440  1.102  christos 		    (status & (INTR_LATCH | ALL_INTERRUPTS)));
   1441   1.34   thorpej 
   1442   1.34   thorpej #if 0
   1443   1.47      fvdl 		status = bus_space_read_2(iot, ioh, ELINK_STATUS);
   1444   1.34   thorpej 
   1445   1.34   thorpej 		printf("%s: intr%s%s%s%s\n", sc->sc_dev.dv_xname,
   1446  1.102  christos 		       (status & RX_COMPLETE)?" RX_COMPLETE":"",
   1447  1.102  christos 		       (status & TX_COMPLETE)?" TX_COMPLETE":"",
   1448  1.102  christos 		       (status & TX_AVAIL)?" TX_AVAIL":"",
   1449  1.102  christos 		       (status & CARD_FAILURE)?" CARD_FAILURE":"");
   1450   1.34   thorpej #endif
   1451    1.1   thorpej 
   1452  1.102  christos 		if (status & RX_COMPLETE) {
   1453    1.1   thorpej 			epread(sc);
   1454   1.35  explorer 		}
   1455  1.102  christos 		if (status & TX_AVAIL) {
   1456   1.21        is 			sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE;
   1457   1.21        is 			epstart(&sc->sc_ethercom.ec_if);
   1458    1.1   thorpej 		}
   1459  1.102  christos 		if (status & CARD_FAILURE) {
   1460   1.10  christos 			printf("%s: adapter failure (%x)\n",
   1461    1.9  christos 			    sc->sc_dev.dv_xname, status);
   1462   1.59   thorpej #if 1
   1463   1.88  jdolecek 			epinit(ifp);
   1464   1.59   thorpej #else
   1465    1.1   thorpej 			epreset(sc);
   1466   1.59   thorpej #endif
   1467    1.1   thorpej 			return (1);
   1468    1.1   thorpej 		}
   1469  1.102  christos 		if (status & TX_COMPLETE) {
   1470    1.1   thorpej 			eptxstat(sc);
   1471    1.1   thorpej 			epstart(ifp);
   1472    1.1   thorpej 		}
   1473   1.35  explorer 
   1474   1.35  explorer #if NRND > 0
   1475   1.35  explorer 		if (status)
   1476   1.35  explorer 			rnd_add_uint32(&sc->rnd_source, status);
   1477   1.35  explorer #endif
   1478  1.111     perry 	}
   1479    1.1   thorpej 
   1480    1.1   thorpej 	/* no more interrupts */
   1481    1.1   thorpej 	return (ret);
   1482    1.1   thorpej }
   1483    1.1   thorpej 
   1484    1.1   thorpej void
   1485    1.1   thorpej epread(sc)
   1486   1.79  augustss 	struct ep_softc *sc;
   1487    1.1   thorpej {
   1488   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1489   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1490   1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1491    1.1   thorpej 	struct mbuf *m;
   1492    1.1   thorpej 	int len;
   1493    1.1   thorpej 
   1494   1.47      fvdl 	len = bus_space_read_2(iot, ioh, ep_w1_reg(sc, ELINK_W1_RX_STATUS));
   1495    1.1   thorpej 
   1496    1.1   thorpej again:
   1497    1.1   thorpej 	if (ifp->if_flags & IFF_DEBUG) {
   1498    1.1   thorpej 		int err = len & ERR_MASK;
   1499  1.112  christos 		const char *s = NULL;
   1500    1.1   thorpej 
   1501    1.1   thorpej 		if (len & ERR_INCOMPLETE)
   1502    1.1   thorpej 			s = "incomplete packet";
   1503    1.1   thorpej 		else if (err == ERR_OVERRUN)
   1504    1.1   thorpej 			s = "packet overrun";
   1505    1.1   thorpej 		else if (err == ERR_RUNT)
   1506    1.1   thorpej 			s = "runt packet";
   1507    1.1   thorpej 		else if (err == ERR_ALIGNMENT)
   1508    1.1   thorpej 			s = "bad alignment";
   1509    1.1   thorpej 		else if (err == ERR_CRC)
   1510    1.1   thorpej 			s = "bad crc";
   1511    1.1   thorpej 		else if (err == ERR_OVERSIZE)
   1512    1.1   thorpej 			s = "oversized packet";
   1513    1.1   thorpej 		else if (err == ERR_DRIBBLE)
   1514    1.1   thorpej 			s = "dribble bits";
   1515    1.1   thorpej 
   1516    1.1   thorpej 		if (s)
   1517   1.10  christos 			printf("%s: %s\n", sc->sc_dev.dv_xname, s);
   1518    1.1   thorpej 	}
   1519    1.1   thorpej 
   1520    1.1   thorpej 	if (len & ERR_INCOMPLETE)
   1521    1.1   thorpej 		return;
   1522    1.1   thorpej 
   1523    1.1   thorpej 	if (len & ERR_RX) {
   1524    1.1   thorpej 		++ifp->if_ierrors;
   1525    1.1   thorpej 		goto abort;
   1526    1.1   thorpej 	}
   1527    1.1   thorpej 
   1528    1.1   thorpej 	len &= RX_BYTES_MASK;	/* Lower 11 bits = RX bytes. */
   1529    1.1   thorpej 
   1530    1.1   thorpej 	/* Pull packet off interface. */
   1531    1.1   thorpej 	m = epget(sc, len);
   1532    1.1   thorpej 	if (m == 0) {
   1533    1.1   thorpej 		ifp->if_ierrors++;
   1534    1.1   thorpej 		goto abort;
   1535    1.1   thorpej 	}
   1536    1.1   thorpej 
   1537    1.1   thorpej 	++ifp->if_ipackets;
   1538    1.1   thorpej 
   1539    1.1   thorpej #if NBPFILTER > 0
   1540    1.1   thorpej 	/*
   1541    1.1   thorpej 	 * Check if there's a BPF listener on this interface.
   1542    1.1   thorpej 	 * If so, hand off the raw packet to BPF.
   1543    1.1   thorpej 	 */
   1544   1.84   thorpej 	if (ifp->if_bpf)
   1545    1.1   thorpej 		bpf_mtap(ifp->if_bpf, m);
   1546   1.84   thorpej #endif
   1547    1.1   thorpej 
   1548   1.58   thorpej 	(*ifp->if_input)(ifp, m);
   1549    1.1   thorpej 
   1550    1.1   thorpej 	/*
   1551    1.1   thorpej 	 * In periods of high traffic we can actually receive enough
   1552    1.1   thorpej 	 * packets so that the fifo overrun bit will be set at this point,
   1553    1.1   thorpej 	 * even though we just read a packet. In this case we
   1554    1.1   thorpej 	 * are not going to receive any more interrupts. We check for
   1555    1.1   thorpej 	 * this condition and read again until the fifo is not full.
   1556    1.1   thorpej 	 * We could simplify this test by not using epstatus(), but
   1557    1.1   thorpej 	 * rechecking the RX_STATUS register directly. This test could
   1558    1.1   thorpej 	 * result in unnecessary looping in cases where there is a new
   1559    1.1   thorpej 	 * packet but the fifo is not full, but it will not fix the
   1560    1.1   thorpej 	 * stuck behavior.
   1561    1.1   thorpej 	 *
   1562    1.1   thorpej 	 * Even with this improvement, we still get packet overrun errors
   1563    1.1   thorpej 	 * which are hurting performance. Maybe when I get some more time
   1564    1.1   thorpej 	 * I'll modify epread() so that it can handle RX_EARLY interrupts.
   1565    1.1   thorpej 	 */
   1566    1.1   thorpej 	if (epstatus(sc)) {
   1567   1.42   thorpej 		len = bus_space_read_2(iot, ioh,
   1568   1.47      fvdl 		    ep_w1_reg(sc, ELINK_W1_RX_STATUS));
   1569    1.1   thorpej 		/* Check if we are stuck and reset [see XXX comment] */
   1570    1.1   thorpej 		if (len & ERR_INCOMPLETE) {
   1571    1.1   thorpej 			if (ifp->if_flags & IFF_DEBUG)
   1572   1.10  christos 				printf("%s: adapter reset\n",
   1573    1.9  christos 				    sc->sc_dev.dv_xname);
   1574    1.1   thorpej 			epreset(sc);
   1575    1.1   thorpej 			return;
   1576    1.1   thorpej 		}
   1577    1.1   thorpej 		goto again;
   1578    1.1   thorpej 	}
   1579    1.1   thorpej 
   1580    1.1   thorpej 	return;
   1581    1.1   thorpej 
   1582    1.1   thorpej abort:
   1583   1.56  jonathan 	ep_discard_rxtop(iot, ioh);
   1584   1.56  jonathan 
   1585    1.1   thorpej }
   1586    1.1   thorpej 
   1587    1.1   thorpej struct mbuf *
   1588    1.1   thorpej epget(sc, totlen)
   1589    1.1   thorpej 	struct ep_softc *sc;
   1590    1.1   thorpej 	int totlen;
   1591    1.1   thorpej {
   1592   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1593   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1594   1.21        is 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1595   1.93  jdolecek 	struct mbuf *m;
   1596  1.101     soren 	bus_size_t rxreg;
   1597   1.14       cjs 	int len, remaining;
   1598   1.93  jdolecek 	int s;
   1599   1.93  jdolecek 	caddr_t newdata;
   1600   1.93  jdolecek 	u_long offset;
   1601    1.1   thorpej 
   1602    1.1   thorpej 	m = sc->mb[sc->next_mb];
   1603    1.1   thorpej 	sc->mb[sc->next_mb] = 0;
   1604    1.1   thorpej 	if (m == 0) {
   1605    1.1   thorpej 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1606    1.1   thorpej 		if (m == 0)
   1607    1.1   thorpej 			return 0;
   1608    1.1   thorpej 	} else {
   1609    1.1   thorpej 		/* If the queue is no longer full, refill. */
   1610    1.1   thorpej 		if (sc->last_mb == sc->next_mb)
   1611   1.78   thorpej 			callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
   1612   1.93  jdolecek 
   1613    1.1   thorpej 		/* Convert one of our saved mbuf's. */
   1614    1.1   thorpej 		sc->next_mb = (sc->next_mb + 1) % MAX_MBS;
   1615    1.1   thorpej 		m->m_data = m->m_pktdat;
   1616    1.1   thorpej 		m->m_flags = M_PKTHDR;
   1617   1.95   thorpej 		memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
   1618    1.1   thorpej 	}
   1619    1.1   thorpej 	m->m_pkthdr.rcvif = ifp;
   1620    1.1   thorpej 	m->m_pkthdr.len = totlen;
   1621    1.1   thorpej 	len = MHLEN;
   1622   1.93  jdolecek 
   1623   1.93  jdolecek 	/*
   1624   1.93  jdolecek 	 * Allocate big enough space to hold whole packet, to avoid
   1625   1.93  jdolecek 	 * allocating new mbufs on splsched().
   1626   1.93  jdolecek 	 */
   1627   1.93  jdolecek 	if (totlen + ALIGNBYTES > len) {
   1628   1.93  jdolecek 		if (totlen + ALIGNBYTES > MCLBYTES) {
   1629   1.93  jdolecek 			len = ALIGN(totlen + ALIGNBYTES);
   1630   1.93  jdolecek 			MEXTMALLOC(m, len, M_DONTWAIT);
   1631   1.93  jdolecek 		} else {
   1632   1.93  jdolecek 			len = MCLBYTES;
   1633   1.93  jdolecek 			MCLGET(m, M_DONTWAIT);
   1634   1.93  jdolecek 		}
   1635   1.93  jdolecek 		if ((m->m_flags & M_EXT) == 0) {
   1636   1.93  jdolecek 			m_free(m);
   1637   1.93  jdolecek 			return 0;
   1638   1.93  jdolecek 		}
   1639   1.93  jdolecek 	}
   1640   1.93  jdolecek 
   1641   1.93  jdolecek 	/* align the struct ip header */
   1642   1.93  jdolecek 	newdata = (caddr_t) ALIGN(m->m_data + sizeof(struct ether_header))
   1643   1.93  jdolecek 		    - sizeof(struct ether_header);
   1644   1.93  jdolecek 	m->m_data = newdata;
   1645   1.93  jdolecek 	m->m_len = totlen;
   1646   1.93  jdolecek 
   1647   1.93  jdolecek 	rxreg = ep_w1_reg(sc, ELINK_W1_RX_PIO_RD_1);
   1648   1.93  jdolecek 	remaining = totlen;
   1649   1.93  jdolecek 	offset = mtod(m, u_long);
   1650    1.1   thorpej 
   1651    1.1   thorpej 	/*
   1652   1.82   thorpej 	 * We read the packet at a high interrupt priority level so that
   1653   1.82   thorpej 	 * an interrupt from another device won't cause the card's packet
   1654   1.82   thorpej 	 * buffer to overflow.  We choose splsched() since that blocks
   1655   1.82   thorpej 	 * essentially everything except for interrupts from serial
   1656   1.91  jdolecek 	 * devices (which typically lose data if their interrupt isn't
   1657   1.82   thorpej 	 * serviced fast enough).
   1658   1.82   thorpej 	 *
   1659   1.82   thorpej 	 * XXX THIS CAN CAUSE CLOCK DRIFT!
   1660    1.1   thorpej 	 */
   1661   1.93  jdolecek 	s = splsched();
   1662   1.42   thorpej 
   1663   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER) {
   1664   1.44   thorpej 		/*
   1665   1.44   thorpej 		 * Prime the FIFO buffer counter (number of 16-bit
   1666   1.44   thorpej 		 * words about to be read from the FIFO).
   1667   1.44   thorpej 		 *
   1668   1.44   thorpej 		 * NOTE: NO OTHER ACCESS CAN BE PERFORMED WHILE THIS
   1669   1.44   thorpej 		 * COUNTER IS NON-ZERO!
   1670   1.44   thorpej 		 */
   1671   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, totlen >> 1);
   1672   1.44   thorpej 	}
   1673   1.44   thorpej 
   1674   1.93  jdolecek 	if (ELINK_IS_BUS_32(sc->bustype)) {
   1675   1.93  jdolecek 		/*
   1676   1.93  jdolecek 		 * Read bytes up to the point where we are aligned.
   1677   1.93  jdolecek 		 * (We can align to 4 bytes, rather than ALIGNBYTES,
   1678   1.93  jdolecek 		 * here because we're later reading 4-byte chunks.)
   1679   1.93  jdolecek 		 */
   1680   1.93  jdolecek 		if ((remaining > 3) && (offset & 3))  {
   1681   1.93  jdolecek 			int count = (4 - (offset & 3));
   1682   1.93  jdolecek 			bus_space_read_multi_1(iot, ioh,
   1683   1.93  jdolecek 			    rxreg, (u_int8_t *) offset, count);
   1684   1.93  jdolecek 			offset += count;
   1685   1.93  jdolecek 			remaining -= count;
   1686   1.93  jdolecek 		}
   1687   1.93  jdolecek 		if (remaining > 3) {
   1688   1.93  jdolecek 			bus_space_read_multi_stream_4(iot, ioh,
   1689   1.93  jdolecek 			    rxreg, (u_int32_t *) offset,
   1690   1.93  jdolecek 				    remaining >> 2);
   1691   1.93  jdolecek 			offset += remaining & ~3;
   1692   1.93  jdolecek 			remaining &= 3;
   1693   1.93  jdolecek 		}
   1694   1.93  jdolecek 		if (remaining)  {
   1695   1.93  jdolecek 			bus_space_read_multi_1(iot, ioh,
   1696   1.93  jdolecek 			    rxreg, (u_int8_t *) offset, remaining);
   1697    1.1   thorpej 		}
   1698   1.93  jdolecek 	} else {
   1699   1.93  jdolecek 		if ((remaining > 1) && (offset & 1))  {
   1700   1.93  jdolecek 			bus_space_read_multi_1(iot, ioh,
   1701   1.93  jdolecek 			    rxreg, (u_int8_t *) offset, 1);
   1702   1.93  jdolecek 			remaining -= 1;
   1703   1.93  jdolecek 			offset += 1;
   1704   1.93  jdolecek 		}
   1705   1.93  jdolecek 		if (remaining > 1) {
   1706   1.93  jdolecek 			bus_space_read_multi_stream_2(iot, ioh,
   1707   1.93  jdolecek 			    rxreg, (u_int16_t *) offset,
   1708   1.93  jdolecek 			    remaining >> 1);
   1709   1.93  jdolecek 			offset += remaining & ~1;
   1710    1.1   thorpej 		}
   1711   1.93  jdolecek 		if (remaining & 1)  {
   1712   1.14       cjs 				bus_space_read_multi_1(iot, ioh,
   1713   1.93  jdolecek 			    rxreg, (u_int8_t *) offset, remaining & 1);
   1714    1.1   thorpej 		}
   1715    1.1   thorpej 	}
   1716    1.1   thorpej 
   1717   1.56  jonathan 	ep_discard_rxtop(iot, ioh);
   1718    1.1   thorpej 
   1719   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_USEFIFOBUFFER)
   1720   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
   1721   1.93  jdolecek 	splx(s);
   1722    1.1   thorpej 
   1723   1.93  jdolecek 	return (m);
   1724    1.1   thorpej }
   1725    1.1   thorpej 
   1726    1.1   thorpej int
   1727    1.1   thorpej epioctl(ifp, cmd, data)
   1728   1.79  augustss 	struct ifnet *ifp;
   1729    1.1   thorpej 	u_long cmd;
   1730    1.1   thorpej 	caddr_t data;
   1731    1.1   thorpej {
   1732    1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
   1733    1.1   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
   1734    1.1   thorpej 	int s, error = 0;
   1735    1.1   thorpej 
   1736    1.1   thorpej 	s = splnet();
   1737    1.1   thorpej 
   1738    1.1   thorpej 	switch (cmd) {
   1739    1.1   thorpej 
   1740   1.23  jonathan 	case SIOCSIFMEDIA:
   1741   1.23  jonathan 	case SIOCGIFMEDIA:
   1742   1.41   thorpej 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1743   1.23  jonathan 		break;
   1744   1.23  jonathan 
   1745    1.1   thorpej 	case SIOCADDMULTI:
   1746    1.1   thorpej 	case SIOCDELMULTI:
   1747   1.34   thorpej 		if (sc->enabled == 0) {
   1748   1.34   thorpej 			error = EIO;
   1749   1.34   thorpej 			break;
   1750   1.34   thorpej 		}
   1751   1.34   thorpej 
   1752   1.88  jdolecek 	default:
   1753   1.88  jdolecek 		error = ether_ioctl(ifp, cmd, data);
   1754    1.1   thorpej 
   1755    1.1   thorpej 		if (error == ENETRESET) {
   1756    1.1   thorpej 			/*
   1757    1.1   thorpej 			 * Multicast list has changed; set the hardware filter
   1758    1.1   thorpej 			 * accordingly.
   1759    1.1   thorpej 			 */
   1760  1.108   thorpej 			if (ifp->if_flags & IFF_RUNNING)
   1761  1.108   thorpej 				epreset(sc);
   1762    1.1   thorpej 			error = 0;
   1763    1.1   thorpej 		}
   1764    1.1   thorpej 		break;
   1765    1.1   thorpej 	}
   1766    1.1   thorpej 
   1767    1.1   thorpej 	splx(s);
   1768    1.1   thorpej 	return (error);
   1769    1.1   thorpej }
   1770    1.1   thorpej 
   1771    1.1   thorpej void
   1772    1.1   thorpej epreset(sc)
   1773    1.1   thorpej 	struct ep_softc *sc;
   1774    1.1   thorpej {
   1775    1.1   thorpej 	int s;
   1776    1.1   thorpej 
   1777    1.1   thorpej 	s = splnet();
   1778   1.88  jdolecek 	epinit(&sc->sc_ethercom.ec_if);
   1779    1.1   thorpej 	splx(s);
   1780    1.1   thorpej }
   1781    1.1   thorpej 
   1782    1.1   thorpej void
   1783    1.5   thorpej epwatchdog(ifp)
   1784    1.5   thorpej 	struct ifnet *ifp;
   1785    1.1   thorpej {
   1786    1.5   thorpej 	struct ep_softc *sc = ifp->if_softc;
   1787    1.1   thorpej 
   1788    1.1   thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1789   1.21        is 	++sc->sc_ethercom.ec_if.if_oerrors;
   1790    1.1   thorpej 
   1791    1.1   thorpej 	epreset(sc);
   1792    1.1   thorpej }
   1793    1.1   thorpej 
   1794    1.1   thorpej void
   1795   1.88  jdolecek epstop(ifp, disable)
   1796   1.88  jdolecek 	struct ifnet *ifp;
   1797   1.88  jdolecek 	int disable;
   1798    1.1   thorpej {
   1799   1.88  jdolecek 	struct ep_softc *sc = ifp->if_softc;
   1800   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1801   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1802    1.1   thorpej 
   1803   1.47      fvdl 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   1804   1.41   thorpej 		/* Stop the one second clock. */
   1805   1.78   thorpej 		callout_stop(&sc->sc_mbuf_callout);
   1806   1.66   thorpej 
   1807   1.66   thorpej 		/* Down the MII. */
   1808   1.66   thorpej 		mii_down(&sc->sc_mii);
   1809   1.44   thorpej 	}
   1810   1.44   thorpej 
   1811   1.47      fvdl 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER) {
   1812   1.44   thorpej 		/*
   1813   1.44   thorpej 		 * Clear the FIFO buffer count, thus halting
   1814   1.44   thorpej 		 * any currently-running transactions.
   1815   1.44   thorpej 		 */
   1816   1.44   thorpej 		GO_WINDOW(1);		/* sanity */
   1817   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_WRCTL, 0);
   1818   1.47      fvdl 		bus_space_write_2(iot, ioh, ELINK_W1_RUNNER_RDCTL, 0);
   1819   1.41   thorpej 	}
   1820   1.41   thorpej 
   1821   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, RX_DISABLE);
   1822   1.56  jonathan 	ep_discard_rxtop(iot, ioh);
   1823   1.56  jonathan 
   1824   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, TX_DISABLE);
   1825   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, STOP_TRANSCEIVER);
   1826   1.18  jonathan 
   1827   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, RX_RESET);
   1828   1.56  jonathan 	ep_reset_cmd(sc, ELINK_COMMAND, TX_RESET);
   1829   1.18  jonathan 
   1830  1.102  christos 	bus_space_write_2(iot, ioh, ELINK_COMMAND, ACK_INTR | INTR_LATCH);
   1831   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RD_0_MASK);
   1832   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_INTR_MASK);
   1833   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_COMMAND, SET_RX_FILTER);
   1834    1.1   thorpej 
   1835    1.1   thorpej 	epmbufempty(sc);
   1836   1.88  jdolecek 
   1837   1.88  jdolecek 	if (disable)
   1838   1.88  jdolecek 		epdisable(sc);
   1839   1.88  jdolecek 
   1840   1.88  jdolecek 	ifp->if_flags &= ~IFF_RUNNING;
   1841    1.1   thorpej }
   1842   1.16  jonathan 
   1843   1.16  jonathan 
   1844   1.16  jonathan /*
   1845   1.16  jonathan  * Before reboots, reset card completely.
   1846   1.16  jonathan  */
   1847   1.16  jonathan static void
   1848   1.16  jonathan epshutdown(arg)
   1849   1.16  jonathan 	void *arg;
   1850   1.16  jonathan {
   1851   1.79  augustss 	struct ep_softc *sc = arg;
   1852  1.111     perry 	int s = splnet();
   1853   1.16  jonathan 
   1854   1.34   thorpej 	if (sc->enabled) {
   1855  1.109    briggs 		epstop(&sc->sc_ethercom.ec_if, 0);
   1856   1.56  jonathan 		ep_reset_cmd(sc, ELINK_COMMAND, GLOBAL_RESET);
   1857  1.109    briggs 		epdisable(sc);
   1858   1.56  jonathan 		sc->enabled = 0;
   1859   1.34   thorpej 	}
   1860   1.56  jonathan 	splx(s);
   1861   1.16  jonathan }
   1862    1.1   thorpej 
   1863    1.1   thorpej /*
   1864    1.1   thorpej  * We get eeprom data from the id_port given an offset into the
   1865    1.1   thorpej  * eeprom.  Basically; after the ID_sequence is sent to all of
   1866    1.1   thorpej  * the cards; they enter the ID_CMD state where they will accept
   1867    1.1   thorpej  * command requests. 0x80-0xbf loads the eeprom data.  We then
   1868    1.1   thorpej  * read the port 16 times and with every read; the cards check
   1869    1.1   thorpej  * for contention (ie: if one card writes a 0 bit and another
   1870    1.1   thorpej  * writes a 1 bit then the host sees a 0. At the end of the cycle;
   1871    1.1   thorpej  * each card compares the data on the bus; if there is a difference
   1872    1.1   thorpej  * then that card goes into ID_WAIT state again). In the meantime;
   1873    1.1   thorpej  * one bit of data is returned in the AX register which is conveniently
   1874  1.107   mycroft  * returned to us by bus_space_read_2().  Hence; we read 16 times getting one
   1875    1.1   thorpej  * bit of data with each read.
   1876    1.2   thorpej  *
   1877    1.2   thorpej  * NOTE: the caller must provide an i/o handle for ELINK_ID_PORT!
   1878    1.1   thorpej  */
   1879    1.2   thorpej u_int16_t
   1880   1.11   thorpej epreadeeprom(iot, ioh, offset)
   1881   1.11   thorpej 	bus_space_tag_t iot;
   1882   1.11   thorpej 	bus_space_handle_t ioh;
   1883    1.2   thorpej 	int offset;
   1884    1.1   thorpej {
   1885    1.2   thorpej 	u_int16_t data = 0;
   1886    1.2   thorpej 	int i;
   1887    1.1   thorpej 
   1888  1.107   mycroft 	bus_space_write_2(iot, ioh, 0, 0x80 + offset);
   1889    1.1   thorpej 	delay(1000);
   1890    1.1   thorpej 	for (i = 0; i < 16; i++)
   1891   1.11   thorpej 		data = (data << 1) | (bus_space_read_2(iot, ioh, 0) & 1);
   1892    1.1   thorpej 	return (data);
   1893    1.1   thorpej }
   1894    1.1   thorpej 
   1895    1.1   thorpej static int
   1896    1.1   thorpej epbusyeeprom(sc)
   1897    1.1   thorpej 	struct ep_softc *sc;
   1898    1.1   thorpej {
   1899   1.11   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   1900   1.11   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   1901  1.101     soren 	bus_size_t eecmd;
   1902    1.1   thorpej 	int i = 100, j;
   1903   1.96   thorpej 	uint16_t busybit;
   1904    1.1   thorpej 
   1905   1.47      fvdl 	if (sc->bustype == ELINK_BUS_PCMCIA) {
   1906    1.1   thorpej 		delay(1000);
   1907    1.1   thorpej 		return 0;
   1908    1.1   thorpej 	}
   1909    1.1   thorpej 
   1910   1.96   thorpej 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW) {
   1911   1.96   thorpej 		eecmd = CORK_ASIC_EEPROM_COMMAND;
   1912   1.96   thorpej 		busybit = CORK_EEPROM_BUSY;
   1913   1.96   thorpej 	} else {
   1914   1.96   thorpej 		eecmd = ELINK_W0_EEPROM_COMMAND;
   1915   1.96   thorpej 		busybit = EEPROM_BUSY;
   1916   1.96   thorpej 	}
   1917   1.96   thorpej 
   1918   1.33  jonathan 	j = 0;		/* bad GCC flow analysis */
   1919    1.1   thorpej 	while (i--) {
   1920   1.96   thorpej 		j = bus_space_read_2(iot, ioh, eecmd);
   1921   1.96   thorpej 		if (j & busybit)
   1922    1.1   thorpej 			delay(100);
   1923    1.1   thorpej 		else
   1924    1.1   thorpej 			break;
   1925    1.1   thorpej 	}
   1926   1.96   thorpej 	if (i == 0) {
   1927   1.10  christos 		printf("\n%s: eeprom failed to come ready\n",
   1928    1.1   thorpej 		    sc->sc_dev.dv_xname);
   1929    1.1   thorpej 		return (1);
   1930    1.1   thorpej 	}
   1931   1.96   thorpej 	if (sc->ep_chipset != ELINK_CHIPSET_CORKSCREW &&
   1932   1.96   thorpej 	    (j & EEPROM_TST_MODE) != 0) {
   1933   1.29  jonathan 		/* XXX PnP mode? */
   1934   1.28     veego 		printf("\n%s: erase pencil mark!\n", sc->sc_dev.dv_xname);
   1935    1.1   thorpej 		return (1);
   1936    1.1   thorpej 	}
   1937    1.1   thorpej 	return (0);
   1938   1.59   thorpej }
   1939   1.59   thorpej 
   1940   1.59   thorpej u_int16_t
   1941   1.59   thorpej ep_read_eeprom(sc, offset)
   1942   1.59   thorpej 	struct ep_softc *sc;
   1943   1.59   thorpej 	u_int16_t offset;
   1944   1.59   thorpej {
   1945  1.101     soren 	bus_size_t eecmd, eedata;
   1946   1.59   thorpej 	u_int16_t readcmd;
   1947   1.59   thorpej 
   1948   1.96   thorpej 	if (sc->ep_chipset == ELINK_CHIPSET_CORKSCREW) {
   1949   1.96   thorpej 		eecmd = CORK_ASIC_EEPROM_COMMAND;
   1950   1.96   thorpej 		eedata = CORK_ASIC_EEPROM_DATA;
   1951   1.96   thorpej 	} else {
   1952   1.96   thorpej 		eecmd = ELINK_W0_EEPROM_COMMAND;
   1953   1.96   thorpej 		eedata = ELINK_W0_EEPROM_DATA;
   1954   1.96   thorpej 	}
   1955   1.96   thorpej 
   1956   1.59   thorpej 	/*
   1957   1.59   thorpej 	 * RoadRunner has a larger EEPROM, so a different read command
   1958   1.59   thorpej 	 * is required.
   1959   1.59   thorpej 	 */
   1960   1.59   thorpej 	if (sc->ep_chipset == ELINK_CHIPSET_ROADRUNNER)
   1961   1.59   thorpej 		readcmd = READ_EEPROM_RR;
   1962   1.59   thorpej 	else
   1963   1.59   thorpej 		readcmd = READ_EEPROM;
   1964   1.59   thorpej 
   1965   1.59   thorpej 	if (epbusyeeprom(sc))
   1966   1.59   thorpej 		return (0);		/* XXX why is eeprom busy? */
   1967   1.96   thorpej 
   1968   1.96   thorpej 	bus_space_write_2(sc->sc_iot, sc->sc_ioh, eecmd, readcmd | offset);
   1969   1.96   thorpej 
   1970   1.59   thorpej 	if (epbusyeeprom(sc))
   1971   1.59   thorpej 		return (0);		/* XXX why is eeprom busy? */
   1972   1.96   thorpej 
   1973   1.96   thorpej 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh, eedata));
   1974    1.1   thorpej }
   1975    1.1   thorpej 
   1976    1.1   thorpej void
   1977    1.3  christos epmbuffill(v)
   1978    1.3  christos 	void *v;
   1979    1.1   thorpej {
   1980    1.3  christos 	struct ep_softc *sc = v;
   1981   1.51   mycroft 	struct mbuf *m;
   1982    1.1   thorpej 	int s, i;
   1983    1.1   thorpej 
   1984    1.1   thorpej 	s = splnet();
   1985    1.1   thorpej 	i = sc->last_mb;
   1986    1.1   thorpej 	do {
   1987   1.51   mycroft 		if (sc->mb[i] == 0) {
   1988   1.51   mycroft 			MGET(m, M_DONTWAIT, MT_DATA);
   1989   1.51   mycroft 			if (m == 0)
   1990   1.51   mycroft 				break;
   1991   1.51   mycroft 			sc->mb[i] = m;
   1992   1.51   mycroft 		}
   1993    1.1   thorpej 		i = (i + 1) % MAX_MBS;
   1994    1.1   thorpej 	} while (i != sc->next_mb);
   1995    1.1   thorpej 	sc->last_mb = i;
   1996    1.1   thorpej 	/* If the queue was not filled, try again. */
   1997    1.1   thorpej 	if (sc->last_mb != sc->next_mb)
   1998   1.78   thorpej 		callout_reset(&sc->sc_mbuf_callout, 1, epmbuffill, sc);
   1999    1.1   thorpej 	splx(s);
   2000    1.1   thorpej }
   2001    1.1   thorpej 
   2002    1.1   thorpej void
   2003    1.1   thorpej epmbufempty(sc)
   2004    1.1   thorpej 	struct ep_softc *sc;
   2005    1.1   thorpej {
   2006    1.1   thorpej 	int s, i;
   2007    1.1   thorpej 
   2008    1.1   thorpej 	s = splnet();
   2009    1.1   thorpej 	for (i = 0; i<MAX_MBS; i++) {
   2010    1.1   thorpej 		if (sc->mb[i]) {
   2011    1.1   thorpej 			m_freem(sc->mb[i]);
   2012    1.1   thorpej 			sc->mb[i] = NULL;
   2013    1.1   thorpej 		}
   2014    1.1   thorpej 	}
   2015    1.1   thorpej 	sc->last_mb = sc->next_mb = 0;
   2016   1.78   thorpej 	callout_stop(&sc->sc_mbuf_callout);
   2017    1.1   thorpej 	splx(s);
   2018   1.34   thorpej }
   2019   1.34   thorpej 
   2020   1.34   thorpej int
   2021   1.34   thorpej epenable(sc)
   2022   1.34   thorpej 	struct ep_softc *sc;
   2023   1.34   thorpej {
   2024   1.34   thorpej 
   2025   1.34   thorpej 	if (sc->enabled == 0 && sc->enable != NULL) {
   2026   1.34   thorpej 		if ((*sc->enable)(sc) != 0) {
   2027   1.34   thorpej 			printf("%s: device enable failed\n",
   2028   1.34   thorpej 			    sc->sc_dev.dv_xname);
   2029   1.34   thorpej 			return (EIO);
   2030   1.34   thorpej 		}
   2031   1.34   thorpej 	}
   2032   1.34   thorpej 
   2033   1.34   thorpej 	sc->enabled = 1;
   2034   1.34   thorpej 	return (0);
   2035   1.34   thorpej }
   2036   1.34   thorpej 
   2037   1.34   thorpej void
   2038   1.34   thorpej epdisable(sc)
   2039   1.34   thorpej 	struct ep_softc *sc;
   2040   1.34   thorpej {
   2041   1.34   thorpej 
   2042   1.34   thorpej 	if (sc->enabled != 0 && sc->disable != NULL) {
   2043   1.34   thorpej 		(*sc->disable)(sc);
   2044   1.34   thorpej 		sc->enabled = 0;
   2045   1.34   thorpej 	}
   2046   1.50   thorpej }
   2047   1.50   thorpej 
   2048   1.74     enami /*
   2049   1.74     enami  * ep_activate:
   2050   1.74     enami  *
   2051   1.74     enami  *	Handle device activation/deactivation requests.
   2052   1.74     enami  */
   2053   1.50   thorpej int
   2054   1.50   thorpej ep_activate(self, act)
   2055   1.50   thorpej 	struct device *self;
   2056   1.50   thorpej 	enum devact act;
   2057   1.50   thorpej {
   2058   1.50   thorpej 	struct ep_softc *sc = (struct ep_softc *)self;
   2059   1.70  augustss 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2060   1.71  augustss 	int error = 0, s;
   2061   1.50   thorpej 
   2062   1.50   thorpej 	s = splnet();
   2063   1.50   thorpej 	switch (act) {
   2064   1.50   thorpej 	case DVACT_ACTIVATE:
   2065   1.71  augustss 		error = EOPNOTSUPP;
   2066   1.50   thorpej 		break;
   2067   1.50   thorpej 
   2068   1.50   thorpej 	case DVACT_DEACTIVATE:
   2069   1.74     enami 		if (sc->ep_flags & ELINK_FLAGS_MII)
   2070   1.74     enami 			mii_activate(&sc->sc_mii, act, MII_PHY_ANY,
   2071   1.74     enami 			    MII_OFFSET_ANY);
   2072   1.70  augustss 		if_deactivate(ifp);
   2073   1.50   thorpej 		break;
   2074   1.50   thorpej 	}
   2075   1.50   thorpej 	splx(s);
   2076   1.71  augustss 	return (error);
   2077   1.68  augustss }
   2078   1.68  augustss 
   2079   1.74     enami /*
   2080   1.74     enami  * ep_detach:
   2081   1.74     enami  *
   2082   1.74     enami  *	Detach a elink3 interface.
   2083   1.74     enami  */
   2084   1.68  augustss int
   2085   1.68  augustss ep_detach(self, flags)
   2086   1.68  augustss 	struct device *self;
   2087   1.68  augustss 	int flags;
   2088   1.68  augustss {
   2089   1.68  augustss 	struct ep_softc *sc = (struct ep_softc *)self;
   2090   1.70  augustss 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2091   1.81     jhawk 
   2092   1.81     jhawk 	/* Succeed now if there's no work to do. */
   2093   1.81     jhawk 	if ((sc->sc_flags & ELINK_FLAGS_ATTACHED) == 0)
   2094   1.81     jhawk 		return (0);
   2095   1.68  augustss 
   2096   1.70  augustss 	epdisable(sc);
   2097   1.68  augustss 
   2098   1.78   thorpej 	callout_stop(&sc->sc_mii_callout);
   2099   1.78   thorpej 	callout_stop(&sc->sc_mbuf_callout);
   2100   1.71  augustss 
   2101   1.74     enami 	if (sc->ep_flags & ELINK_FLAGS_MII) {
   2102   1.74     enami 		/* Detach all PHYs */
   2103   1.74     enami 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
   2104   1.74     enami 	}
   2105   1.74     enami 
   2106   1.74     enami 	/* Delete all remaining media. */
   2107   1.68  augustss 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   2108   1.72  augustss 
   2109   1.72  augustss #if NRND > 0
   2110   1.72  augustss 	rnd_detach_source(&sc->rnd_source);
   2111   1.70  augustss #endif
   2112   1.70  augustss 	ether_ifdetach(ifp);
   2113   1.70  augustss 	if_detach(ifp);
   2114   1.70  augustss 
   2115   1.71  augustss 	shutdownhook_disestablish(sc->sd_hook);
   2116   1.68  augustss 
   2117   1.68  augustss 	return (0);
   2118   1.41   thorpej }
   2119   1.41   thorpej 
   2120   1.67   thorpej u_int32_t
   2121   1.67   thorpej ep_mii_bitbang_read(self)
   2122   1.67   thorpej 	struct device *self;
   2123   1.41   thorpej {
   2124   1.67   thorpej 	struct ep_softc *sc = (void *) self;
   2125   1.41   thorpej 
   2126   1.67   thorpej 	/* We're already in Window 4. */
   2127   1.67   thorpej 	return (bus_space_read_2(sc->sc_iot, sc->sc_ioh,
   2128   1.67   thorpej 	    ELINK_W4_BOOM_PHYSMGMT));
   2129   1.41   thorpej }
   2130   1.41   thorpej 
   2131   1.41   thorpej void
   2132   1.67   thorpej ep_mii_bitbang_write(self, val)
   2133   1.67   thorpej 	struct device *self;
   2134   1.67   thorpej 	u_int32_t val;
   2135   1.41   thorpej {
   2136   1.67   thorpej 	struct ep_softc *sc = (void *) self;
   2137   1.41   thorpej 
   2138   1.67   thorpej 	/* We're already in Window 4. */
   2139   1.67   thorpej 	bus_space_write_2(sc->sc_iot, sc->sc_ioh,
   2140   1.67   thorpej 	    ELINK_W4_BOOM_PHYSMGMT, val);
   2141   1.41   thorpej }
   2142   1.41   thorpej 
   2143   1.41   thorpej int
   2144   1.41   thorpej ep_mii_readreg(self, phy, reg)
   2145   1.41   thorpej 	struct device *self;
   2146   1.41   thorpej 	int phy, reg;
   2147   1.41   thorpej {
   2148   1.67   thorpej 	struct ep_softc *sc = (void *) self;
   2149   1.67   thorpej 	int val;
   2150   1.41   thorpej 
   2151   1.41   thorpej 	GO_WINDOW(4);
   2152   1.41   thorpej 
   2153   1.67   thorpej 	val = mii_bitbang_readreg(self, &ep_mii_bitbang_ops, phy, reg);
   2154   1.41   thorpej 
   2155   1.67   thorpej 	GO_WINDOW(1);
   2156   1.41   thorpej 
   2157   1.67   thorpej 	return (val);
   2158   1.41   thorpej }
   2159   1.41   thorpej 
   2160   1.41   thorpej void
   2161   1.41   thorpej ep_mii_writereg(self, phy, reg, val)
   2162   1.41   thorpej 	struct device *self;
   2163   1.41   thorpej 	int phy, reg, val;
   2164   1.41   thorpej {
   2165   1.67   thorpej 	struct ep_softc *sc = (void *) self;
   2166   1.41   thorpej 
   2167   1.41   thorpej 	GO_WINDOW(4);
   2168   1.41   thorpej 
   2169   1.67   thorpej 	mii_bitbang_writereg(self, &ep_mii_bitbang_ops, phy, reg, val);
   2170   1.41   thorpej 
   2171   1.67   thorpej 	GO_WINDOW(1);
   2172   1.41   thorpej }
   2173   1.41   thorpej 
   2174   1.41   thorpej void
   2175   1.41   thorpej ep_statchg(self)
   2176   1.41   thorpej 	struct device *self;
   2177   1.41   thorpej {
   2178   1.45   thorpej 	struct ep_softc *sc = (struct ep_softc *)self;
   2179   1.45   thorpej 	bus_space_tag_t iot = sc->sc_iot;
   2180   1.45   thorpej 	bus_space_handle_t ioh = sc->sc_ioh;
   2181   1.45   thorpej 	int mctl;
   2182   1.45   thorpej 
   2183   1.45   thorpej 	GO_WINDOW(3);
   2184   1.47      fvdl 	mctl = bus_space_read_2(iot, ioh, ELINK_W3_MAC_CONTROL);
   2185   1.45   thorpej 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   2186   1.45   thorpej 		mctl |= MAC_CONTROL_FDX;
   2187   1.45   thorpej 	else
   2188   1.45   thorpej 		mctl &= ~MAC_CONTROL_FDX;
   2189   1.47      fvdl 	bus_space_write_2(iot, ioh, ELINK_W3_MAC_CONTROL, mctl);
   2190   1.45   thorpej 	GO_WINDOW(1);	/* back to operating window */
   2191    1.1   thorpej }
   2192