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