Home | History | Annotate | Line # | Download | only in ic
smc91cxx.c revision 1.75
      1  1.75    cegger /*	$NetBSD: smc91cxx.c,v 1.75 2009/05/12 14:25:18 cegger Exp $	*/
      2   1.2   thorpej 
      3   1.2   thorpej /*-
      4   1.2   thorpej  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5   1.2   thorpej  * All rights reserved.
      6   1.2   thorpej  *
      7   1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.2   thorpej  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.2   thorpej  * NASA Ames Research Center.
     10   1.2   thorpej  *
     11   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     12   1.2   thorpej  * modification, are permitted provided that the following conditions
     13   1.2   thorpej  * are met:
     14   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     15   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     16   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     18   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     19   1.2   thorpej  *
     20   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21   1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22   1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23   1.3       jtc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24   1.3       jtc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25   1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26   1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27   1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28   1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29   1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30   1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     31   1.2   thorpej  */
     32   1.2   thorpej 
     33  1.51     perry /*
     34   1.2   thorpej  * Copyright (c) 1996 Gardner Buchanan <gbuchanan (at) shl.com>
     35   1.2   thorpej  * All rights reserved.
     36  1.51     perry  *
     37   1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     38   1.2   thorpej  * modification, are permitted provided that the following conditions
     39   1.2   thorpej  * are met:
     40   1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     41   1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     42   1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     43   1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     44   1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     45   1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     46   1.2   thorpej  *    must display the following acknowledgement:
     47   1.2   thorpej  *	This product includes software developed by Gardner Buchanan.
     48   1.2   thorpej  * 4. The name of Gardner Buchanan may not be used to endorse or promote
     49   1.2   thorpej  *    products derived from this software without specific prior written
     50   1.2   thorpej  *    permission.
     51  1.51     perry  *
     52   1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53   1.2   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     54   1.2   thorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     55   1.2   thorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     56   1.2   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     57   1.2   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     58   1.2   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     59   1.2   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     60   1.2   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     61   1.2   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  1.51     perry  *
     63   1.2   thorpej  *   from FreeBSD Id: if_sn.c,v 1.4 1996/03/18 15:47:16 gardner Exp
     64  1.51     perry  */
     65   1.2   thorpej 
     66   1.2   thorpej /*
     67   1.2   thorpej  * Core driver for the SMC 91Cxx family of Ethernet chips.
     68   1.2   thorpej  *
     69   1.2   thorpej  * Memory allocation interrupt logic is drived from an SMC 91C90 driver
     70   1.2   thorpej  * written for NetBSD/amiga by Michael Hitch.
     71   1.2   thorpej  */
     72  1.37     lukem 
     73  1.37     lukem #include <sys/cdefs.h>
     74  1.75    cegger __KERNEL_RCSID(0, "$NetBSD: smc91cxx.c,v 1.75 2009/05/12 14:25:18 cegger Exp $");
     75   1.2   thorpej 
     76   1.7  jonathan #include "opt_inet.h"
     77   1.2   thorpej #include "bpfilter.h"
     78   1.5  explorer #include "rnd.h"
     79   1.2   thorpej 
     80  1.51     perry #include <sys/param.h>
     81   1.2   thorpej #include <sys/systm.h>
     82   1.2   thorpej #include <sys/mbuf.h>
     83   1.2   thorpej #include <sys/syslog.h>
     84   1.2   thorpej #include <sys/socket.h>
     85   1.2   thorpej #include <sys/device.h>
     86  1.26    briggs #include <sys/kernel.h>
     87   1.2   thorpej #include <sys/malloc.h>
     88  1.51     perry #include <sys/ioctl.h>
     89   1.2   thorpej #include <sys/errno.h>
     90   1.5  explorer #if NRND > 0
     91   1.5  explorer #include <sys/rnd.h>
     92   1.5  explorer #endif
     93   1.2   thorpej 
     94  1.63        ad #include <sys/bus.h>
     95  1.63        ad #include <sys/intr.h>
     96   1.2   thorpej 
     97  1.69       chs #include <uvm/uvm_extern.h>
     98  1.69       chs 
     99   1.2   thorpej #include <net/if.h>
    100   1.2   thorpej #include <net/if_dl.h>
    101   1.2   thorpej #include <net/if_ether.h>
    102  1.51     perry #include <net/if_media.h>
    103   1.2   thorpej 
    104   1.2   thorpej #ifdef INET
    105  1.51     perry #include <netinet/in.h>
    106   1.2   thorpej #include <netinet/if_inarp.h>
    107   1.2   thorpej #include <netinet/in_systm.h>
    108   1.2   thorpej #include <netinet/in_var.h>
    109   1.2   thorpej #include <netinet/ip.h>
    110   1.2   thorpej #endif
    111   1.2   thorpej 
    112   1.2   thorpej #if NBPFILTER > 0
    113   1.2   thorpej #include <net/bpf.h>
    114   1.2   thorpej #include <net/bpfdesc.h>
    115   1.2   thorpej #endif
    116   1.2   thorpej 
    117  1.26    briggs #include <dev/mii/mii.h>
    118  1.26    briggs #include <dev/mii/miivar.h>
    119  1.26    briggs #include <dev/mii/mii_bitbang.h>
    120  1.26    briggs 
    121   1.2   thorpej #include <dev/ic/smc91cxxreg.h>
    122   1.2   thorpej #include <dev/ic/smc91cxxvar.h>
    123  1.40   thorpej 
    124  1.40   thorpej #ifndef __BUS_SPACE_HAS_STREAM_METHODS
    125  1.40   thorpej #define bus_space_write_multi_stream_2 bus_space_write_multi_2
    126  1.42       bsh #define bus_space_write_multi_stream_4 bus_space_write_multi_4
    127  1.40   thorpej #define bus_space_read_multi_stream_2  bus_space_read_multi_2
    128  1.42       bsh #define bus_space_read_multi_stream_4  bus_space_read_multi_4
    129  1.42       bsh 
    130  1.42       bsh #define bus_space_write_stream_4 bus_space_write_4
    131  1.42       bsh #define bus_space_read_stream_4  bus_space_read_4
    132  1.40   thorpej #endif /* __BUS_SPACE_HAS_STREAM_METHODS */
    133   1.2   thorpej 
    134   1.2   thorpej /* XXX Hardware padding doesn't work yet(?) */
    135   1.2   thorpej #define	SMC91CXX_SW_PAD
    136   1.2   thorpej 
    137   1.2   thorpej const char *smc91cxx_idstrs[] = {
    138   1.2   thorpej 	NULL,				/* 0 */
    139   1.2   thorpej 	NULL,				/* 1 */
    140   1.2   thorpej 	NULL,				/* 2 */
    141   1.2   thorpej 	"SMC91C90/91C92",		/* 3 */
    142  1.39       chs 	"SMC91C94/91C96",		/* 4 */
    143   1.2   thorpej 	"SMC91C95",			/* 5 */
    144   1.2   thorpej 	NULL,				/* 6 */
    145   1.2   thorpej 	"SMC91C100",			/* 7 */
    146  1.26    briggs 	"SMC91C100FD",			/* 8 */
    147  1.45       scw 	"SMC91C111",			/* 9 */
    148   1.2   thorpej 	NULL,				/* 10 */
    149   1.2   thorpej 	NULL,				/* 11 */
    150   1.2   thorpej 	NULL,				/* 12 */
    151   1.2   thorpej 	NULL,				/* 13 */
    152   1.2   thorpej 	NULL,				/* 14 */
    153   1.2   thorpej 	NULL,				/* 15 */
    154   1.2   thorpej };
    155   1.2   thorpej 
    156   1.2   thorpej /* Supported media types. */
    157   1.2   thorpej const int smc91cxx_media[] = {
    158   1.2   thorpej 	IFM_ETHER|IFM_10_T,
    159   1.2   thorpej 	IFM_ETHER|IFM_10_5,
    160   1.2   thorpej };
    161   1.2   thorpej #define	NSMC91CxxMEDIA	(sizeof(smc91cxx_media) / sizeof(smc91cxx_media[0]))
    162   1.2   thorpej 
    163  1.26    briggs /*
    164  1.26    briggs  * MII bit-bang glue.
    165  1.26    briggs  */
    166  1.75    cegger u_int32_t smc91cxx_mii_bitbang_read(device_t);
    167  1.75    cegger void smc91cxx_mii_bitbang_write(device_t, u_int32_t);
    168  1.26    briggs 
    169  1.26    briggs const struct mii_bitbang_ops smc91cxx_mii_bitbang_ops = {
    170  1.26    briggs 	smc91cxx_mii_bitbang_read,
    171  1.26    briggs 	smc91cxx_mii_bitbang_write,
    172  1.26    briggs 	{
    173  1.26    briggs 		MR_MDO,		/* MII_BIT_MDO */
    174  1.26    briggs 		MR_MDI,		/* MII_BIT_MDI */
    175  1.26    briggs 		MR_MCLK,	/* MII_BIT_MDC */
    176  1.26    briggs 		MR_MDOE,	/* MII_BIT_DIR_HOST_PHY */
    177  1.26    briggs 		0,		/* MII_BIT_DIR_PHY_HOST */
    178  1.26    briggs 	}
    179  1.26    briggs };
    180  1.26    briggs 
    181  1.26    briggs /* MII callbacks */
    182  1.75    cegger int	smc91cxx_mii_readreg(device_t, int, int);
    183  1.75    cegger void	smc91cxx_mii_writereg(device_t, int, int, int);
    184  1.75    cegger void	smc91cxx_statchg(device_t);
    185  1.50     perry void	smc91cxx_tick(void *);
    186  1.50     perry 
    187  1.50     perry int	smc91cxx_mediachange(struct ifnet *);
    188  1.50     perry void	smc91cxx_mediastatus(struct ifnet *, struct ifmediareq *);
    189  1.50     perry 
    190  1.50     perry int	smc91cxx_set_media(struct smc91cxx_softc *, int);
    191  1.50     perry 
    192  1.50     perry void	smc91cxx_init(struct smc91cxx_softc *);
    193  1.50     perry void	smc91cxx_read(struct smc91cxx_softc *);
    194  1.50     perry void	smc91cxx_reset(struct smc91cxx_softc *);
    195  1.50     perry void	smc91cxx_start(struct ifnet *);
    196  1.67      matt uint8_t	smc91cxx_copy_tx_frame(struct smc91cxx_softc *, struct mbuf *);
    197  1.50     perry void	smc91cxx_resume(struct smc91cxx_softc *);
    198  1.50     perry void	smc91cxx_stop(struct smc91cxx_softc *);
    199  1.50     perry void	smc91cxx_watchdog(struct ifnet *);
    200  1.59  christos int	smc91cxx_ioctl(struct ifnet *, u_long, void *);
    201   1.2   thorpej 
    202  1.61    dyoung static inline int ether_cmp(const void *, const void *);
    203  1.54     perry static inline int
    204  1.73       dsl ether_cmp(const void *va, const void *vb)
    205   1.2   thorpej {
    206  1.61    dyoung 	const u_int8_t *a = va;
    207  1.61    dyoung 	const u_int8_t *b = vb;
    208   1.2   thorpej 
    209   1.2   thorpej 	return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) ||
    210   1.2   thorpej 		(a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0]));
    211   1.2   thorpej }
    212   1.2   thorpej 
    213  1.67      matt static inline void
    214  1.67      matt smc91cxx_intr_mask_write(bus_space_tag_t bst, bus_space_handle_t bsh,
    215  1.67      matt 	uint8_t mask)
    216  1.67      matt {
    217  1.67      matt 	KDASSERT((mask & IM_ERCV_INT) == 0);
    218  1.67      matt #ifdef SMC91CXX_NO_BYTE_WRITE
    219  1.67      matt #if BYTE_ORDER == LITTLE_ENDIAN
    220  1.67      matt 	bus_space_write_2(bst, bsh, INTR_STAT_REG_B, mask << 8);
    221  1.67      matt #else
    222  1.68  nakayama 	bus_space_write_2(bst, bsh, INTR_STAT_REG_B, mask);
    223  1.67      matt #endif
    224  1.67      matt #else
    225  1.67      matt 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, mask);
    226  1.67      matt #endif
    227  1.67      matt 	KDASSERT(!(bus_space_read_1(bst, bsh, INTR_MASK_REG_B) & IM_ERCV_INT));
    228  1.67      matt }
    229  1.67      matt 
    230  1.67      matt static inline void
    231  1.67      matt smc91cxx_intr_ack_write(bus_space_tag_t bst, bus_space_handle_t bsh,
    232  1.67      matt 	uint8_t mask)
    233  1.67      matt {
    234  1.67      matt #ifdef SMC91CXX_NO_BYTE_WRITE
    235  1.67      matt #if BYTE_ORDER == LITTLE_ENDIAN
    236  1.67      matt 	bus_space_write_2(bst, bsh, INTR_ACK_REG_B,
    237  1.67      matt 	    mask | (bus_space_read_2(bst, bsh, INTR_ACK_REG_B) & 0xff00));
    238  1.67      matt #else
    239  1.67      matt 	bus_space_write_2(bst, bsh, INTR_ACK_REG_B,
    240  1.67      matt 	    (mask << 8) | (bus_space_read_2(bst, bsh, INTR_ACK_REG_B) & 0xff));
    241  1.67      matt #endif
    242  1.67      matt #else
    243  1.67      matt 	bus_space_write_1(bst, bsh, INTR_ACK_REG_B, mask);
    244  1.67      matt #endif
    245  1.67      matt 	KDASSERT(!(bus_space_read_1(bst, bsh, INTR_MASK_REG_B) & IM_ERCV_INT));
    246  1.67      matt }
    247  1.67      matt 
    248   1.2   thorpej void
    249  1.72       dsl smc91cxx_attach(struct smc91cxx_softc *sc, u_int8_t *myea)
    250   1.2   thorpej {
    251   1.2   thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    252   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    253   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    254  1.26    briggs 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    255   1.2   thorpej 	const char *idstr;
    256  1.26    briggs 	u_int32_t miicapabilities;
    257   1.2   thorpej 	u_int16_t tmp;
    258   1.2   thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
    259  1.45       scw 	int i, aui, mult, scale, memsize;
    260  1.26    briggs 	char pbuf[9];
    261   1.2   thorpej 
    262  1.47   mycroft 	tmp = bus_space_read_2(bst, bsh, BANK_SELECT_REG_W);
    263  1.47   mycroft 	/* check magic number */
    264  1.47   mycroft 	if ((tmp & BSR_DETECT_MASK) != BSR_DETECT_VALUE) {
    265  1.65    cegger 		aprint_error_dev(&sc->sc_dev, "failed to detect chip, bsr=%04x\n", tmp);
    266  1.47   mycroft 		return;
    267  1.47   mycroft 	}
    268  1.47   mycroft 
    269   1.2   thorpej 	/* Make sure the chip is stopped. */
    270   1.2   thorpej 	smc91cxx_stop(sc);
    271   1.2   thorpej 
    272   1.2   thorpej 	SMC_SELECT_BANK(sc, 3);
    273   1.2   thorpej 	tmp = bus_space_read_2(bst, bsh, REVISION_REG_W);
    274  1.26    briggs 	sc->sc_chipid = RR_ID(tmp);
    275  1.47   mycroft 	idstr = smc91cxx_idstrs[sc->sc_chipid];
    276  1.47   mycroft 
    277  1.65    cegger 	aprint_normal_dev(&sc->sc_dev, "");
    278   1.2   thorpej 	if (idstr != NULL)
    279  1.47   mycroft 		aprint_normal("%s, ", idstr);
    280   1.2   thorpej 	else
    281  1.47   mycroft 		aprint_normal("unknown chip id %d, ", sc->sc_chipid);
    282  1.47   mycroft 	aprint_normal("revision %d, ", RR_REV(tmp));
    283  1.26    briggs 
    284  1.26    briggs 	SMC_SELECT_BANK(sc, 0);
    285  1.45       scw 	switch (sc->sc_chipid) {
    286  1.45       scw 	default:
    287  1.45       scw 		mult = MCR_MEM_MULT(bus_space_read_2(bst, bsh, MEM_CFG_REG_W));
    288  1.45       scw 		scale = MIR_SCALE_91C9x;
    289  1.45       scw 		break;
    290  1.45       scw 
    291  1.45       scw 	case CHIP_91C111:
    292  1.45       scw 		mult = MIR_MULT_91C111;
    293  1.45       scw 		scale = MIR_SCALE_91C111;
    294  1.45       scw 	}
    295  1.26    briggs 	memsize = bus_space_read_2(bst, bsh, MEM_INFO_REG_W) & MIR_TOTAL_MASK;
    296  1.26    briggs 	if (memsize == 255) memsize++;
    297  1.45       scw 	memsize *= scale * mult;
    298  1.26    briggs 
    299  1.26    briggs 	format_bytes(pbuf, sizeof(pbuf), memsize);
    300  1.47   mycroft 	aprint_normal("buffer size: %s\n", pbuf);
    301   1.2   thorpej 
    302   1.2   thorpej 	/* Read the station address from the chip. */
    303   1.2   thorpej 	SMC_SELECT_BANK(sc, 1);
    304   1.2   thorpej 	if (myea == NULL) {
    305   1.2   thorpej 		myea = enaddr;
    306   1.2   thorpej 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    307   1.2   thorpej 			tmp = bus_space_read_2(bst, bsh, IAR_ADDR0_REG_W + i);
    308   1.2   thorpej 			myea[i + 1] = (tmp >> 8) & 0xff;
    309   1.2   thorpej 			myea[i] = tmp & 0xff;
    310   1.2   thorpej 		}
    311   1.2   thorpej 	}
    312  1.65    cegger 	aprint_normal_dev(&sc->sc_dev, "MAC address %s, ",
    313   1.2   thorpej 	    ether_sprintf(myea));
    314   1.2   thorpej 
    315   1.2   thorpej 	/* Initialize the ifnet structure. */
    316  1.65    cegger 	strlcpy(ifp->if_xname, device_xname(&sc->sc_dev), IFNAMSIZ);
    317   1.2   thorpej 	ifp->if_softc = sc;
    318   1.2   thorpej 	ifp->if_start = smc91cxx_start;
    319   1.2   thorpej 	ifp->if_ioctl = smc91cxx_ioctl;
    320   1.2   thorpej 	ifp->if_watchdog = smc91cxx_watchdog;
    321   1.2   thorpej 	ifp->if_flags =
    322   1.2   thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    323  1.32   thorpej 	IFQ_SET_READY(&ifp->if_snd);
    324   1.2   thorpej 
    325   1.2   thorpej 	/* Attach the interface. */
    326   1.2   thorpej 	if_attach(ifp);
    327   1.2   thorpej 	ether_ifattach(ifp, myea);
    328   1.2   thorpej 
    329  1.26    briggs 	/*
    330  1.26    briggs 	 * Initialize our media structures and MII info.  We will
    331  1.26    briggs 	 * probe the MII if we are on the SMC91Cxx
    332  1.26    briggs 	 */
    333  1.26    briggs 	sc->sc_mii.mii_ifp = ifp;
    334  1.26    briggs 	sc->sc_mii.mii_readreg = smc91cxx_mii_readreg;
    335  1.26    briggs 	sc->sc_mii.mii_writereg = smc91cxx_mii_writereg;
    336  1.26    briggs 	sc->sc_mii.mii_statchg = smc91cxx_statchg;
    337  1.44      fair 	ifmedia_init(ifm, IFM_IMASK, smc91cxx_mediachange, smc91cxx_mediastatus);
    338  1.26    briggs 
    339  1.26    briggs 	SMC_SELECT_BANK(sc, 1);
    340  1.26    briggs 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    341  1.26    briggs 
    342  1.35   thorpej 	miicapabilities = BMSR_MEDIAMASK|BMSR_ANEG;
    343  1.26    briggs 	switch (sc->sc_chipid) {
    344  1.26    briggs 	case CHIP_91100:
    345  1.26    briggs 		/*
    346  1.26    briggs 		 * The 91100 does not have full-duplex capabilities,
    347  1.26    briggs 		 * even if the PHY does.
    348  1.26    briggs 		 */
    349  1.26    briggs 		miicapabilities &= ~(BMSR_100TXFDX | BMSR_10TFDX);
    350  1.26    briggs 	case CHIP_91100FD:
    351  1.45       scw 	case CHIP_91C111:
    352  1.26    briggs 		if (tmp & CR_MII_SELECT) {
    353  1.47   mycroft 			aprint_normal("default media MII");
    354  1.45       scw 			if (sc->sc_chipid == CHIP_91C111) {
    355  1.47   mycroft 				aprint_normal(" (%s PHY)\n", (tmp & CR_AUI_SELECT) ?
    356  1.45       scw 				    "external" : "internal");
    357  1.45       scw 				sc->sc_internal_phy = !(tmp & CR_AUI_SELECT);
    358  1.45       scw 			} else
    359  1.47   mycroft 				aprint_normal("\n");
    360  1.26    briggs 			mii_attach(&sc->sc_dev, &sc->sc_mii, miicapabilities,
    361  1.26    briggs 			    MII_PHY_ANY, MII_OFFSET_ANY, 0);
    362  1.26    briggs 			if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    363  1.26    briggs 				ifmedia_add(&sc->sc_mii.mii_media,
    364  1.26    briggs 				    IFM_ETHER|IFM_NONE, 0, NULL);
    365  1.26    briggs 				ifmedia_set(&sc->sc_mii.mii_media,
    366  1.26    briggs 				    IFM_ETHER|IFM_NONE);
    367  1.26    briggs 			} else {
    368  1.26    briggs 				ifmedia_set(&sc->sc_mii.mii_media,
    369  1.26    briggs 				    IFM_ETHER|IFM_AUTO);
    370  1.26    briggs 			}
    371  1.26    briggs 			sc->sc_flags |= SMC_FLAGS_HAS_MII;
    372  1.26    briggs 			break;
    373  1.45       scw 		} else
    374  1.45       scw 		if (sc->sc_chipid == CHIP_91C111) {
    375  1.45       scw 			/*
    376  1.45       scw 			 * XXX: Should bring it out of low-power mode
    377  1.45       scw 			 */
    378  1.47   mycroft 			aprint_normal("EPH interface in low power mode\n");
    379  1.45       scw 			sc->sc_internal_phy = 0;
    380  1.45       scw 			return;
    381  1.26    briggs 		}
    382  1.26    briggs 		/*FALLTHROUGH*/
    383  1.26    briggs 	default:
    384  1.47   mycroft 		aprint_normal("default media %s\n", (aui = (tmp & CR_AUI_SELECT)) ?
    385  1.26    briggs 		    "AUI" : "UTP");
    386  1.26    briggs 		for (i = 0; i < NSMC91CxxMEDIA; i++)
    387  1.26    briggs 			ifmedia_add(ifm, smc91cxx_media[i], 0, NULL);
    388  1.26    briggs 		ifmedia_set(ifm, IFM_ETHER | (aui ? IFM_10_5 : IFM_10_T));
    389  1.26    briggs 		break;
    390  1.26    briggs 	}
    391   1.2   thorpej 
    392   1.5  explorer #if NRND > 0
    393  1.65    cegger 	rnd_attach_source(&sc->rnd_source, device_xname(&sc->sc_dev),
    394  1.15  explorer 			  RND_TYPE_NET, 0);
    395   1.5  explorer #endif
    396  1.25     jhawk 
    397  1.60  kiyohara 	callout_init(&sc->sc_mii_callout, 0);
    398  1.60  kiyohara 
    399  1.25     jhawk 	/* The attach is successful. */
    400  1.25     jhawk 	sc->sc_flags |= SMC_FLAGS_ATTACHED;
    401   1.2   thorpej }
    402   1.2   thorpej 
    403   1.2   thorpej /*
    404   1.2   thorpej  * Change media according to request.
    405   1.2   thorpej  */
    406   1.2   thorpej int
    407  1.72       dsl smc91cxx_mediachange(struct ifnet *ifp)
    408   1.2   thorpej {
    409   1.2   thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    410   1.2   thorpej 
    411  1.26    briggs 	return (smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_media));
    412   1.2   thorpej }
    413   1.2   thorpej 
    414   1.2   thorpej int
    415  1.72       dsl smc91cxx_set_media(struct smc91cxx_softc *sc, int media)
    416   1.2   thorpej {
    417   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    418   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    419   1.2   thorpej 	u_int16_t tmp;
    420  1.64    dyoung 	int rc;
    421   1.2   thorpej 
    422   1.4   thorpej 	/*
    423   1.4   thorpej 	 * If the interface is not currently powered on, just return.
    424   1.4   thorpej 	 * When it is enabled later, smc91cxx_init() will properly set
    425   1.4   thorpej 	 * up the media for us.
    426   1.4   thorpej 	 */
    427  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0)
    428   1.4   thorpej 		return (0);
    429   1.4   thorpej 
    430   1.2   thorpej 	if (IFM_TYPE(media) != IFM_ETHER)
    431   1.2   thorpej 		return (EINVAL);
    432   1.2   thorpej 
    433  1.64    dyoung 	if ((sc->sc_flags & SMC_FLAGS_HAS_MII) == 0 ||
    434  1.64    dyoung 	    (rc = mii_mediachg(&sc->sc_mii)) == ENXIO)
    435  1.64    dyoung 		rc = 0;
    436  1.26    briggs 
    437   1.2   thorpej 	switch (IFM_SUBTYPE(media)) {
    438   1.2   thorpej 	case IFM_10_T:
    439   1.2   thorpej 	case IFM_10_5:
    440   1.2   thorpej 		SMC_SELECT_BANK(sc, 1);
    441   1.2   thorpej 		tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    442   1.2   thorpej 		if (IFM_SUBTYPE(media) == IFM_10_5)
    443   1.2   thorpej 			tmp |= CR_AUI_SELECT;
    444   1.2   thorpej 		else
    445   1.2   thorpej 			tmp &= ~CR_AUI_SELECT;
    446   1.2   thorpej 		bus_space_write_2(bst, bsh, CONFIG_REG_W, tmp);
    447   1.2   thorpej 		delay(20000);	/* XXX is this needed? */
    448   1.2   thorpej 		break;
    449   1.2   thorpej 
    450   1.2   thorpej 	default:
    451   1.2   thorpej 		return (EINVAL);
    452   1.2   thorpej 	}
    453   1.2   thorpej 
    454  1.64    dyoung 	return rc;
    455   1.2   thorpej }
    456   1.2   thorpej 
    457   1.2   thorpej /*
    458   1.2   thorpej  * Notify the world which media we're using.
    459   1.2   thorpej  */
    460   1.2   thorpej void
    461  1.72       dsl smc91cxx_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
    462   1.2   thorpej {
    463   1.2   thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    464   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    465   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    466   1.2   thorpej 	u_int16_t tmp;
    467   1.2   thorpej 
    468  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) {
    469   1.4   thorpej 		ifmr->ifm_active = IFM_ETHER | IFM_NONE;
    470   1.4   thorpej 		ifmr->ifm_status = 0;
    471   1.4   thorpej 		return;
    472   1.4   thorpej 	}
    473   1.4   thorpej 
    474  1.26    briggs 	/*
    475  1.26    briggs 	 * If we have MII, go ask the PHY what's going on.
    476  1.26    briggs 	 */
    477  1.26    briggs 	if (sc->sc_flags & SMC_FLAGS_HAS_MII) {
    478  1.26    briggs 		mii_pollstat(&sc->sc_mii);
    479  1.26    briggs 		ifmr->ifm_active = sc->sc_mii.mii_media_active;
    480  1.26    briggs 		ifmr->ifm_status = sc->sc_mii.mii_media_status;
    481  1.26    briggs 		return;
    482  1.26    briggs 	}
    483  1.26    briggs 
    484   1.2   thorpej 	SMC_SELECT_BANK(sc, 1);
    485   1.2   thorpej 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    486   1.2   thorpej 	ifmr->ifm_active =
    487   1.2   thorpej 	    IFM_ETHER | ((tmp & CR_AUI_SELECT) ? IFM_10_5 : IFM_10_T);
    488   1.2   thorpej }
    489   1.2   thorpej 
    490   1.2   thorpej /*
    491   1.2   thorpej  * Reset and initialize the chip.
    492   1.2   thorpej  */
    493   1.2   thorpej void
    494  1.72       dsl smc91cxx_init(struct smc91cxx_softc *sc)
    495   1.2   thorpej {
    496   1.2   thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    497   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    498   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    499   1.2   thorpej 	u_int16_t tmp;
    500  1.61    dyoung 	const u_int8_t *enaddr;
    501   1.2   thorpej 	int s, i;
    502   1.2   thorpej 
    503  1.11   mycroft 	s = splnet();
    504   1.2   thorpej 
    505   1.2   thorpej 	/*
    506  1.46       wiz 	 * This resets the registers mostly to defaults, but doesn't
    507   1.2   thorpej 	 * affect the EEPROM.  After the reset cycle, we pause briefly
    508   1.2   thorpej 	 * for the chip to recover.
    509   1.2   thorpej 	 *
    510   1.2   thorpej 	 * XXX how long are we really supposed to delay?  --thorpej
    511   1.2   thorpej 	 */
    512   1.2   thorpej 	SMC_SELECT_BANK(sc, 0);
    513   1.2   thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, RCR_SOFTRESET);
    514   1.2   thorpej 	delay(100);
    515   1.2   thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
    516   1.2   thorpej 	delay(200);
    517   1.2   thorpej 
    518   1.2   thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
    519   1.2   thorpej 
    520   1.2   thorpej 	/* Set the Ethernet address. */
    521   1.2   thorpej 	SMC_SELECT_BANK(sc, 1);
    522  1.61    dyoung 	enaddr = (const u_int8_t *)CLLADDR(ifp->if_sadl);
    523   1.2   thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    524   1.2   thorpej 		tmp = enaddr[i + 1] << 8 | enaddr[i];
    525   1.2   thorpej 		bus_space_write_2(bst, bsh, IAR_ADDR0_REG_W + i, tmp);
    526   1.2   thorpej 	}
    527   1.2   thorpej 
    528   1.2   thorpej 	/*
    529   1.2   thorpej 	 * Set the control register to automatically release successfully
    530   1.2   thorpej 	 * transmitted packets (making the best use of our limited memory)
    531   1.2   thorpej 	 * and enable the EPH interrupt on certain TX errors.
    532   1.2   thorpej 	 */
    533   1.2   thorpej 	bus_space_write_2(bst, bsh, CONTROL_REG_W, (CTR_AUTO_RELEASE |
    534   1.2   thorpej 	    CTR_TE_ENABLE | CTR_CR_ENABLE | CTR_LE_ENABLE));
    535   1.2   thorpej 
    536   1.2   thorpej 	/*
    537   1.2   thorpej 	 * Reset the MMU and wait for it to be un-busy.
    538   1.2   thorpej 	 */
    539   1.2   thorpej 	SMC_SELECT_BANK(sc, 2);
    540   1.2   thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RESET);
    541  1.67      matt 	sc->sc_txpacketno = ARR_FAILED;
    542  1.48   mycroft 	for (;;) {
    543  1.48   mycroft 		tmp = bus_space_read_2(bst, bsh, MMU_CMD_REG_W);
    544  1.48   mycroft 		if (tmp == 0xffff)	/* card went away! */
    545  1.48   mycroft 			return;
    546  1.48   mycroft 		if ((tmp & MMUCR_BUSY) == 0)
    547  1.48   mycroft 			break;
    548  1.48   mycroft 	}
    549   1.2   thorpej 
    550   1.2   thorpej 	/*
    551   1.2   thorpej 	 * Disable all interrupts.
    552   1.2   thorpej 	 */
    553  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, 0);
    554   1.2   thorpej 
    555   1.2   thorpej 	/*
    556  1.45       scw 	 * On the 91c111, enable auto-negotiation, and set the LED
    557  1.45       scw 	 * status pins to something sane.
    558  1.45       scw 	 * XXX: Should be some way for MD code to decide the latter.
    559  1.45       scw 	 */
    560  1.45       scw 	SMC_SELECT_BANK(sc, 0);
    561  1.45       scw 	if (sc->sc_chipid == CHIP_91C111) {
    562  1.45       scw 		bus_space_write_2(bst, bsh, RX_PHY_CONTROL_REG_W,
    563  1.45       scw 		    RPC_ANEG |
    564  1.45       scw 		    (RPC_LS_LINK_DETECT << RPC_LSA_SHIFT) |
    565  1.45       scw 		    (RPC_LS_TXRX << RPC_LSB_SHIFT));
    566  1.45       scw 	}
    567  1.45       scw 
    568  1.45       scw 	/*
    569   1.2   thorpej 	 * Set current media.
    570   1.2   thorpej 	 */
    571  1.26    briggs 	smc91cxx_set_media(sc, sc->sc_mii.mii_media.ifm_cur->ifm_media);
    572   1.2   thorpej 
    573   1.2   thorpej 	/*
    574   1.2   thorpej 	 * Set the receive filter.  We want receive enable and auto
    575   1.2   thorpej 	 * strip of CRC from received packet.  If we are in promisc. mode,
    576   1.2   thorpej 	 * then set that bit as well.
    577   1.2   thorpej 	 *
    578   1.2   thorpej 	 * XXX Initialize multicast filter.  For now, we just accept
    579   1.2   thorpej 	 * XXX all multicast.
    580   1.2   thorpej 	 */
    581   1.2   thorpej 	SMC_SELECT_BANK(sc, 0);
    582   1.2   thorpej 
    583   1.2   thorpej 	tmp = RCR_ENABLE | RCR_STRIP_CRC | RCR_ALMUL;
    584   1.2   thorpej 	if (ifp->if_flags & IFF_PROMISC)
    585   1.2   thorpej 		tmp |= RCR_PROMISC;
    586   1.2   thorpej 
    587   1.2   thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, tmp);
    588   1.2   thorpej 
    589   1.2   thorpej 	/*
    590   1.2   thorpej 	 * Set transmitter control to "enabled".
    591   1.2   thorpej 	 */
    592   1.2   thorpej 	tmp = TCR_ENABLE;
    593   1.2   thorpej 
    594   1.2   thorpej #ifndef SMC91CXX_SW_PAD
    595   1.2   thorpej 	/*
    596   1.2   thorpej 	 * Enable hardware padding of transmitted packets.
    597   1.2   thorpej 	 * XXX doesn't work?
    598   1.2   thorpej 	 */
    599   1.2   thorpej 	tmp |= TCR_PAD_ENABLE;
    600   1.2   thorpej #endif
    601   1.2   thorpej 
    602   1.2   thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, tmp);
    603   1.2   thorpej 
    604   1.2   thorpej 	/*
    605   1.2   thorpej 	 * Now, enable interrupts.
    606   1.2   thorpej 	 */
    607   1.2   thorpej 	SMC_SELECT_BANK(sc, 2);
    608   1.2   thorpej 
    609  1.67      matt 	sc->sc_intmask = IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT;
    610  1.45       scw 	if (sc->sc_chipid == CHIP_91C111 && sc->sc_internal_phy) {
    611  1.67      matt 		sc->sc_intmask |= IM_MD_INT;
    612  1.45       scw 	}
    613  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask);
    614   1.2   thorpej 
    615   1.2   thorpej 	/* Interface is now running, with no output active. */
    616   1.2   thorpej 	ifp->if_flags |= IFF_RUNNING;
    617   1.2   thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    618   1.2   thorpej 
    619  1.26    briggs 	if (sc->sc_flags & SMC_FLAGS_HAS_MII) {
    620  1.26    briggs 		/* Start the one second clock. */
    621  1.26    briggs 		callout_reset(&sc->sc_mii_callout, hz, smc91cxx_tick, sc);
    622  1.26    briggs 	}
    623  1.26    briggs 
    624   1.2   thorpej 	/*
    625   1.2   thorpej 	 * Attempt to start any pending transmission.
    626   1.2   thorpej 	 */
    627   1.2   thorpej 	smc91cxx_start(ifp);
    628   1.2   thorpej 
    629   1.2   thorpej 	splx(s);
    630   1.2   thorpej }
    631   1.2   thorpej 
    632   1.2   thorpej /*
    633   1.2   thorpej  * Start output on an interface.
    634  1.11   mycroft  * Must be called at splnet or interrupt level.
    635   1.2   thorpej  */
    636   1.2   thorpej void
    637  1.72       dsl smc91cxx_start(struct ifnet *ifp)
    638   1.2   thorpej {
    639   1.2   thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    640   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    641   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    642   1.2   thorpej 	u_int len;
    643  1.43       scw 	struct mbuf *m;
    644   1.2   thorpej 	u_int16_t length, npages;
    645  1.67      matt 	u_int16_t oddbyte;
    646   1.2   thorpej 	u_int8_t packetno;
    647   1.2   thorpej 	int timo, pad;
    648   1.2   thorpej 
    649   1.2   thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    650   1.2   thorpej 		return;
    651   1.2   thorpej 
    652   1.2   thorpej  again:
    653   1.2   thorpej 	/*
    654   1.2   thorpej 	 * Peek at the next packet.
    655   1.2   thorpej 	 */
    656  1.32   thorpej 	IFQ_POLL(&ifp->if_snd, m);
    657  1.32   thorpej 	if (m == NULL)
    658   1.2   thorpej 		return;
    659   1.2   thorpej 
    660   1.2   thorpej 	/*
    661   1.2   thorpej 	 * Compute the frame length and set pad to give an overall even
    662   1.2   thorpej 	 * number of bytes.  Below, we assume that the packet length
    663   1.2   thorpej 	 * is even.
    664   1.2   thorpej 	 */
    665  1.43       scw 	for (len = 0; m != NULL; m = m->m_next)
    666   1.2   thorpej 		len += m->m_len;
    667   1.2   thorpej 	pad = (len & 1);
    668   1.2   thorpej 
    669   1.2   thorpej 	/*
    670   1.2   thorpej 	 * We drop packets that are too large.  Perhaps we should
    671   1.2   thorpej 	 * truncate them instead?
    672   1.2   thorpej 	 */
    673   1.2   thorpej 	if ((len + pad) > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
    674  1.65    cegger 		printf("%s: large packet discarded\n", device_xname(&sc->sc_dev));
    675   1.2   thorpej 		ifp->if_oerrors++;
    676  1.32   thorpej 		IFQ_DEQUEUE(&ifp->if_snd, m);
    677   1.2   thorpej 		m_freem(m);
    678   1.2   thorpej 		goto readcheck;
    679   1.2   thorpej 	}
    680   1.2   thorpej 
    681   1.2   thorpej #ifdef SMC91CXX_SW_PAD
    682   1.2   thorpej 	/*
    683   1.2   thorpej 	 * Not using hardware padding; pad to ETHER_MIN_LEN.
    684   1.2   thorpej 	 */
    685   1.2   thorpej 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
    686   1.2   thorpej 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    687   1.2   thorpej #endif
    688   1.2   thorpej 
    689   1.2   thorpej 	length = pad + len;
    690   1.2   thorpej 
    691   1.2   thorpej 	/*
    692   1.2   thorpej 	 * The MMU has a 256 byte page size.  The MMU expects us to
    693   1.2   thorpej 	 * ask for "npages - 1".  We include space for the status word,
    694   1.2   thorpej 	 * byte count, and control bytes in the allocation request.
    695   1.2   thorpej 	 */
    696  1.67      matt 	npages = ((length & ~1) + 6) >> 8;
    697   1.2   thorpej 
    698   1.2   thorpej 	/*
    699   1.2   thorpej 	 * Now allocate the memory.
    700   1.2   thorpej 	 */
    701   1.2   thorpej 	SMC_SELECT_BANK(sc, 2);
    702   1.2   thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ALLOC | npages);
    703   1.2   thorpej 
    704   1.2   thorpej 	timo = MEMORY_WAIT_TIME;
    705  1.67      matt 	if (__predict_false((sc->sc_txpacketno & ARR_FAILED) == 0)) {
    706  1.67      matt 		packetno = sc->sc_txpacketno;
    707  1.67      matt 		sc->sc_txpacketno = ARR_FAILED;
    708  1.67      matt 	} else {
    709  1.67      matt 		do {
    710  1.67      matt 			if (bus_space_read_1(bst, bsh,
    711  1.67      matt 			    		     INTR_STAT_REG_B) & IM_ALLOC_INT)
    712  1.67      matt 				break;
    713  1.67      matt 			delay(1);
    714  1.67      matt 		} while (--timo);
    715  1.67      matt 	}
    716   1.2   thorpej 
    717   1.2   thorpej 	packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B);
    718   1.2   thorpej 
    719   1.2   thorpej 	if (packetno & ARR_FAILED || timo == 0) {
    720   1.2   thorpej 		/*
    721   1.2   thorpej 		 * No transmit memory is available.  Record the number
    722   1.2   thorpej 		 * of requestd pages and enable the allocation completion
    723   1.2   thorpej 		 * interrupt.  Set up the watchdog timer in case we miss
    724   1.2   thorpej 		 * the interrupt.  Mark the interface as active so that
    725   1.2   thorpej 		 * no one else attempts to transmit while we're allocating
    726   1.2   thorpej 		 * memory.
    727   1.2   thorpej 		 */
    728  1.67      matt 		sc->sc_intmask |= IM_ALLOC_INT;
    729  1.67      matt 		smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask);
    730   1.2   thorpej 		ifp->if_timer = 5;
    731   1.2   thorpej 		ifp->if_flags |= IFF_OACTIVE;
    732   1.2   thorpej 
    733   1.2   thorpej 		return;
    734   1.2   thorpej 	}
    735   1.2   thorpej 
    736   1.2   thorpej 	/*
    737   1.2   thorpej 	 * We have a packet number - set the data window.
    738   1.2   thorpej 	 */
    739   1.2   thorpej 	bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
    740   1.2   thorpej 
    741   1.2   thorpej 	/*
    742   1.2   thorpej 	 * Point to the beginning of the packet.
    743   1.2   thorpej 	 */
    744   1.2   thorpej 	bus_space_write_2(bst, bsh, POINTER_REG_W, PTR_AUTOINC /* | 0x0000 */);
    745   1.2   thorpej 
    746   1.2   thorpej 	/*
    747   1.2   thorpej 	 * Send the packet length (+6 for stats, length, and control bytes)
    748   1.2   thorpej 	 * and the status word (set to zeros).
    749   1.2   thorpej 	 */
    750   1.2   thorpej 	bus_space_write_2(bst, bsh, DATA_REG_W, 0);
    751  1.52     pooka 	bus_space_write_2(bst, bsh, DATA_REG_W, (length + 6) & 0x7ff);
    752   1.2   thorpej 
    753   1.2   thorpej 	/*
    754   1.2   thorpej 	 * Get the packet from the kernel.  This will include the Ethernet
    755   1.2   thorpej 	 * frame header, MAC address, etc.
    756   1.2   thorpej 	 */
    757  1.32   thorpej 	IFQ_DEQUEUE(&ifp->if_snd, m);
    758   1.2   thorpej 
    759   1.2   thorpej 	/*
    760   1.2   thorpej 	 * Push the packet out to the card.
    761   1.2   thorpej 	 */
    762  1.67      matt 	oddbyte = smc91cxx_copy_tx_frame(sc, m);
    763   1.2   thorpej 
    764   1.2   thorpej #ifdef SMC91CXX_SW_PAD
    765  1.70       rjs #ifdef SMC91CXX_NO_BYTE_WRITE
    766  1.67      matt #if BYTE_ORDER == LITTLE_ENDIAN
    767  1.67      matt 	if (pad > 1 && (pad & 1)) {
    768  1.67      matt 		bus_space_write_2(bst, bsh, DATA_REG_W, oddbyte << 0);
    769  1.67      matt 		oddbyte = 0;
    770  1.67      matt 	}
    771  1.67      matt #else
    772  1.68  nakayama 	if (pad > 1 && (pad & 1)) {
    773  1.67      matt 		bus_space_write_2(bst, bsh, DATA_REG_W, oddbyte << 8);
    774  1.67      matt 		oddbyte = 0;
    775  1.67      matt 	}
    776  1.67      matt #endif
    777  1.70       rjs #endif
    778  1.67      matt 
    779   1.2   thorpej 	/*
    780   1.2   thorpej 	 * Push out padding.
    781   1.2   thorpej 	 */
    782   1.2   thorpej 	while (pad > 1) {
    783   1.2   thorpej 		bus_space_write_2(bst, bsh, DATA_REG_W, 0);
    784   1.2   thorpej 		pad -= 2;
    785   1.2   thorpej 	}
    786   1.2   thorpej #endif
    787   1.2   thorpej 
    788  1.70       rjs #ifdef SMC91CXX_NO_BYTE_WRITE
    789   1.2   thorpej 	/*
    790   1.2   thorpej 	 * Push out control byte and unused packet byte.  The control byte
    791   1.2   thorpej 	 * is 0, meaning the packet is even lengthed and no special
    792   1.2   thorpej 	 * CRC handling is necessary.
    793   1.2   thorpej 	 */
    794  1.67      matt #if BYTE_ORDER == LITTLE_ENDIAN
    795  1.67      matt 	bus_space_write_2(bst, bsh, DATA_REG_W,
    796  1.67      matt 	    oddbyte | (pad ? (CTLB_ODD << 8) : 0));
    797  1.67      matt #else
    798  1.67      matt 	bus_space_write_2(bst, bsh, DATA_REG_W,
    799  1.67      matt 	    (oddbyte << 8) | (pad ? CTLB_ODD : 0));
    800  1.67      matt #endif
    801  1.70       rjs #else
    802  1.70       rjs 	if (pad)
    803  1.70       rjs 		bus_space_write_1(bst, bsh, DATA_REG_B, 0);
    804  1.70       rjs #endif
    805   1.2   thorpej 
    806   1.2   thorpej 	/*
    807   1.2   thorpej 	 * Enable transmit interrupts and let the chip go.  Set a watchdog
    808   1.2   thorpej 	 * in case we miss the interrupt.
    809   1.2   thorpej 	 */
    810  1.67      matt 	sc->sc_intmask |= IM_TX_INT | IM_TX_EMPTY_INT;
    811  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, sc->sc_intmask);
    812   1.2   thorpej 
    813   1.2   thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ENQUEUE);
    814   1.2   thorpej 
    815   1.2   thorpej 	ifp->if_timer = 5;
    816   1.2   thorpej 
    817   1.2   thorpej #if NBPFILTER > 0
    818   1.2   thorpej 	/* Hand off a copy to the bpf. */
    819   1.2   thorpej 	if (ifp->if_bpf)
    820  1.43       scw 		bpf_mtap(ifp->if_bpf, m);
    821   1.2   thorpej #endif
    822   1.2   thorpej 
    823   1.2   thorpej 	ifp->if_opackets++;
    824  1.43       scw 	m_freem(m);
    825   1.2   thorpej 
    826   1.2   thorpej  readcheck:
    827   1.2   thorpej 	/*
    828   1.2   thorpej 	 * Check for incoming pcakets.  We don't want to overflow the small
    829   1.2   thorpej 	 * RX FIFO.  If nothing has arrived, attempt to queue another
    830   1.2   thorpej 	 * transmit packet.
    831   1.2   thorpej 	 */
    832   1.2   thorpej 	if (bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & FIFO_REMPTY)
    833   1.2   thorpej 		goto again;
    834   1.2   thorpej }
    835   1.2   thorpej 
    836   1.2   thorpej /*
    837  1.43       scw  * Squirt a (possibly misaligned) mbuf to the device
    838  1.43       scw  */
    839  1.67      matt uint8_t
    840  1.72       dsl smc91cxx_copy_tx_frame(struct smc91cxx_softc *sc, struct mbuf *m0)
    841  1.43       scw {
    842  1.43       scw 	bus_space_tag_t bst = sc->sc_bst;
    843  1.43       scw 	bus_space_handle_t bsh = sc->sc_bsh;
    844  1.43       scw 	struct mbuf *m;
    845  1.43       scw 	int len, leftover;
    846  1.43       scw 	u_int16_t dbuf;
    847  1.43       scw 	u_int8_t *p;
    848  1.43       scw #ifdef DIAGNOSTIC
    849  1.43       scw 	u_int8_t *lim;
    850  1.43       scw #endif
    851  1.43       scw 
    852  1.43       scw 	/* start out with no leftover data */
    853  1.43       scw 	leftover = 0;
    854  1.43       scw 	dbuf = 0;
    855  1.43       scw 
    856  1.43       scw 	/* Process the chain of mbufs */
    857  1.43       scw 	for (m = m0; m != NULL; m = m->m_next) {
    858  1.43       scw 		/*
    859  1.43       scw 		 * Process all of the data in a single mbuf.
    860  1.43       scw 		 */
    861  1.43       scw 		p = mtod(m, u_int8_t *);
    862  1.43       scw 		len = m->m_len;
    863  1.43       scw #ifdef DIAGNOSTIC
    864  1.43       scw 		lim = p + len;
    865  1.43       scw #endif
    866  1.43       scw 
    867  1.43       scw 		while (len > 0) {
    868  1.43       scw 			if (leftover) {
    869  1.43       scw 				/*
    870  1.43       scw 				 * Data left over (from mbuf or realignment).
    871  1.43       scw 				 * Buffer the next byte, and write it and
    872  1.43       scw 				 * the leftover data out.
    873  1.43       scw 				 */
    874  1.43       scw 				dbuf |= *p++ << 8;
    875  1.43       scw 				len--;
    876  1.43       scw 				bus_space_write_2(bst, bsh, DATA_REG_W, dbuf);
    877  1.43       scw 				leftover = 0;
    878  1.43       scw 			} else if ((long) p & 1) {
    879  1.43       scw 				/*
    880  1.43       scw 				 * Misaligned data.  Buffer the next byte.
    881  1.43       scw 				 */
    882  1.43       scw 				dbuf = *p++;
    883  1.43       scw 				len--;
    884  1.43       scw 				leftover = 1;
    885  1.43       scw 			} else {
    886  1.43       scw 				/*
    887  1.43       scw 				 * Aligned data.  This is the case we like.
    888  1.43       scw 				 *
    889  1.43       scw 				 * Write-region out as much as we can, then
    890  1.43       scw 				 * buffer the remaining byte (if any).
    891  1.43       scw 				 */
    892  1.43       scw 				leftover = len & 1;
    893  1.43       scw 				len &= ~1;
    894  1.43       scw 				bus_space_write_multi_stream_2(bst, bsh,
    895  1.43       scw 				    DATA_REG_W, (u_int16_t *)p, len >> 1);
    896  1.43       scw 				p += len;
    897  1.43       scw 
    898  1.43       scw 				if (leftover)
    899  1.43       scw 					dbuf = *p++;
    900  1.43       scw 				len = 0;
    901  1.43       scw 			}
    902  1.43       scw 		}
    903  1.43       scw 		if (len < 0)
    904  1.43       scw 			panic("smc91cxx_copy_tx_frame: negative len");
    905  1.43       scw #ifdef DIAGNOSTIC
    906  1.43       scw 		if (p != lim)
    907  1.43       scw 			panic("smc91cxx_copy_tx_frame: p != lim");
    908  1.43       scw #endif
    909  1.43       scw 	}
    910  1.70       rjs #ifndef SMC91CXX_NO_BYTE_WRITE
    911  1.70       rjs 	if (leftover)
    912  1.70       rjs 		bus_space_write_1(bst, bsh, DATA_REG_B, dbuf);
    913  1.70       rjs #endif
    914  1.67      matt 	return dbuf;
    915  1.43       scw }
    916  1.43       scw 
    917  1.43       scw /*
    918   1.2   thorpej  * Interrupt service routine.
    919   1.2   thorpej  */
    920   1.2   thorpej int
    921  1.72       dsl smc91cxx_intr(void *arg)
    922   1.2   thorpej {
    923   1.2   thorpej 	struct smc91cxx_softc *sc = arg;
    924   1.2   thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    925   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
    926   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    927   1.2   thorpej 	u_int8_t mask, interrupts, status;
    928  1.70       rjs 	u_int16_t packetno, tx_status, card_stats;
    929  1.70       rjs #ifdef SMC91CXX_NO_BYTE_WRITE
    930  1.70       rjs 	u_int16_t v;
    931  1.70       rjs #endif
    932   1.2   thorpej 
    933  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 ||
    934  1.55   thorpej 	    !device_is_active(&sc->sc_dev))
    935   1.4   thorpej 		return (0);
    936   1.4   thorpej 
    937   1.2   thorpej 	SMC_SELECT_BANK(sc, 2);
    938   1.2   thorpej 
    939   1.2   thorpej 	/*
    940  1.67      matt 	 * Obtain the current interrupt status and mask.
    941   1.2   thorpej 	 */
    942  1.70       rjs #ifdef SMC91CXX_NO_BYTE_WRITE
    943  1.67      matt 	v = bus_space_read_2(bst, bsh, INTR_STAT_REG_B);
    944   1.2   thorpej 
    945   1.2   thorpej 	/*
    946   1.2   thorpej 	 * Get the set of interrupt which occurred and eliminate any
    947   1.2   thorpej 	 * which are not enabled.
    948   1.2   thorpej 	 */
    949  1.67      matt #if BYTE_ORDER == LITTLE_ENDIAN
    950  1.67      matt 	mask = v >> 8;
    951  1.67      matt 	interrupts = v & 0xff;
    952  1.67      matt #else
    953  1.67      matt 	interrupts = v >> 8;
    954  1.67      matt 	mask = v & 0xff;
    955  1.67      matt #endif
    956  1.67      matt 	KDASSERT(mask == sc->sc_intmask);
    957  1.70       rjs #else
    958  1.70       rjs 	mask = bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
    959  1.70       rjs 
    960  1.70       rjs 	/*
    961  1.70       rjs 	 * Get the set of interrupt which occurred and eliminate any
    962  1.70       rjs 	 * which are not enabled.
    963  1.70       rjs 	 */
    964  1.70       rjs 	interrupts = bus_space_read_1(bst, bsh, INTR_STAT_REG_B);
    965  1.70       rjs #endif
    966   1.2   thorpej 	status = interrupts & mask;
    967   1.2   thorpej 
    968   1.2   thorpej 	/* Ours? */
    969   1.2   thorpej 	if (status == 0)
    970   1.2   thorpej 		return (0);
    971   1.2   thorpej 
    972   1.2   thorpej 	/*
    973   1.2   thorpej 	 * It's ours; disable all interrupts while we process them.
    974   1.2   thorpej 	 */
    975  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, 0);
    976   1.2   thorpej 
    977   1.2   thorpej 	/*
    978   1.2   thorpej 	 * Receive overrun interrupts.
    979   1.2   thorpej 	 */
    980   1.2   thorpej 	if (status & IM_RX_OVRN_INT) {
    981  1.67      matt 		smc91cxx_intr_ack_write(bst, bsh, IM_RX_OVRN_INT);
    982   1.2   thorpej 		ifp->if_ierrors++;
    983   1.2   thorpej 	}
    984   1.2   thorpej 
    985   1.2   thorpej 	/*
    986   1.2   thorpej 	 * Receive interrupts.
    987   1.2   thorpej 	 */
    988   1.2   thorpej 	if (status & IM_RCV_INT) {
    989   1.2   thorpej #if 1 /* DIAGNOSTIC */
    990   1.2   thorpej 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
    991  1.36     pooka 		if (packetno & FIFO_REMPTY) {
    992  1.65    cegger 			aprint_error_dev(&sc->sc_dev, "receive interrupt on empty fifo\n");
    993  1.36     pooka 			goto out;
    994  1.36     pooka 		} else
    995   1.2   thorpej #endif
    996   1.2   thorpej 		smc91cxx_read(sc);
    997   1.2   thorpej 	}
    998   1.2   thorpej 
    999   1.2   thorpej 	/*
   1000   1.2   thorpej 	 * Memory allocation interrupts.
   1001   1.2   thorpej 	 */
   1002   1.2   thorpej 	if (status & IM_ALLOC_INT) {
   1003   1.2   thorpej 		/* Disable this interrupt. */
   1004   1.2   thorpej 		mask &= ~IM_ALLOC_INT;
   1005  1.67      matt 		sc->sc_intmask &= ~IM_ALLOC_INT;
   1006   1.2   thorpej 
   1007   1.2   thorpej 		/*
   1008  1.67      matt 		 * Save allocated packet number for use in start
   1009   1.2   thorpej 		 */
   1010  1.67      matt 		packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B);
   1011  1.67      matt 		KASSERT(sc->sc_txpacketno & ARR_FAILED);
   1012  1.67      matt 		sc->sc_txpacketno = packetno;
   1013   1.2   thorpej 
   1014  1.67      matt 		/*
   1015  1.67      matt 		 * We can transmit again!
   1016  1.67      matt 		 */
   1017   1.2   thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
   1018   1.2   thorpej 		ifp->if_timer = 0;
   1019   1.2   thorpej 	}
   1020   1.2   thorpej 
   1021   1.2   thorpej 	/*
   1022   1.2   thorpej 	 * Transmit complete interrupt.  Handle transmission error messages.
   1023   1.2   thorpej 	 * This will only be called on error condition because of AUTO RELEASE
   1024   1.2   thorpej 	 * mode.
   1025   1.2   thorpej 	 */
   1026   1.2   thorpej 	if (status & IM_TX_INT) {
   1027  1.67      matt 		smc91cxx_intr_ack_write(bst, bsh, IM_TX_INT);
   1028   1.2   thorpej 
   1029   1.2   thorpej 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) &
   1030   1.2   thorpej 		    FIFO_TX_MASK;
   1031   1.2   thorpej 
   1032   1.2   thorpej 		/*
   1033   1.2   thorpej 		 * Select this as the packet to read from.
   1034   1.2   thorpej 		 */
   1035   1.2   thorpej 		bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
   1036   1.2   thorpej 
   1037   1.2   thorpej 		/*
   1038   1.2   thorpej 		 * Position the pointer to the beginning of the packet.
   1039   1.2   thorpej 		 */
   1040   1.2   thorpej 		bus_space_write_2(bst, bsh, POINTER_REG_W,
   1041   1.2   thorpej 		    PTR_AUTOINC | PTR_READ /* | 0x0000 */);
   1042   1.2   thorpej 
   1043   1.2   thorpej 		/*
   1044   1.2   thorpej 		 * Fetch the TX status word.  This will be a copy of
   1045   1.2   thorpej 		 * the EPH_STATUS_REG_W at the time of the transmission
   1046   1.2   thorpej 		 * failure.
   1047   1.2   thorpej 		 */
   1048   1.2   thorpej 		tx_status = bus_space_read_2(bst, bsh, DATA_REG_W);
   1049   1.2   thorpej 
   1050  1.67      matt 		if (tx_status & EPHSR_TX_SUC) {
   1051  1.67      matt 			static struct timeval txsuc_last;
   1052  1.67      matt 			static int txsuc_count;
   1053  1.67      matt 			if (ppsratecheck(&txsuc_last, &txsuc_count, 1))
   1054  1.67      matt 				printf("%s: successful packet caused TX"
   1055  1.74    cegger 				    " interrupt?!\n", device_xname(&sc->sc_dev));
   1056  1.67      matt 		} else
   1057   1.2   thorpej 			ifp->if_oerrors++;
   1058   1.2   thorpej 
   1059   1.2   thorpej 		if (tx_status & EPHSR_LATCOL)
   1060   1.2   thorpej 			ifp->if_collisions++;
   1061   1.2   thorpej 
   1062  1.67      matt 		/* Disable this interrupt (start will reenable if needed). */
   1063  1.67      matt 		mask &= ~IM_TX_INT;
   1064  1.67      matt 		sc->sc_intmask &= ~IM_TX_INT;
   1065  1.67      matt 
   1066   1.2   thorpej 		/*
   1067   1.2   thorpej 		 * Some of these errors disable the transmitter; reenable it.
   1068   1.2   thorpej 		 */
   1069   1.2   thorpej 		SMC_SELECT_BANK(sc, 0);
   1070   1.2   thorpej #ifdef SMC91CXX_SW_PAD
   1071   1.2   thorpej 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, TCR_ENABLE);
   1072   1.2   thorpej #else
   1073   1.2   thorpej 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W,
   1074   1.2   thorpej 		    TCR_ENABLE | TCR_PAD_ENABLE);
   1075   1.2   thorpej #endif
   1076   1.2   thorpej 
   1077   1.2   thorpej 		/*
   1078   1.2   thorpej 		 * Kill the failed packet and wait for the MMU to unbusy.
   1079   1.2   thorpej 		 */
   1080   1.2   thorpej 		SMC_SELECT_BANK(sc, 2);
   1081   1.2   thorpej 		while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
   1082   1.2   thorpej 			/* XXX bound this loop! */ ;
   1083   1.2   thorpej 		bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
   1084   1.2   thorpej 
   1085   1.2   thorpej 		ifp->if_timer = 0;
   1086   1.2   thorpej 	}
   1087   1.2   thorpej 
   1088   1.2   thorpej 	/*
   1089   1.2   thorpej 	 * Transmit underrun interrupts.  We use this opportunity to
   1090   1.2   thorpej 	 * update transmit statistics from the card.
   1091   1.2   thorpej 	 */
   1092   1.2   thorpej 	if (status & IM_TX_EMPTY_INT) {
   1093  1.67      matt 		smc91cxx_intr_ack_write(bst, bsh, IM_TX_EMPTY_INT);
   1094   1.2   thorpej 
   1095   1.2   thorpej 		/* Disable this interrupt. */
   1096   1.2   thorpej 		mask &= ~IM_TX_EMPTY_INT;
   1097  1.67      matt 		sc->sc_intmask &= ~IM_TX_EMPTY_INT;
   1098   1.2   thorpej 
   1099   1.2   thorpej 		SMC_SELECT_BANK(sc, 0);
   1100   1.2   thorpej 		card_stats = bus_space_read_2(bst, bsh, COUNTER_REG_W);
   1101   1.2   thorpej 
   1102   1.2   thorpej 		/* Single collisions. */
   1103   1.2   thorpej 		ifp->if_collisions += card_stats & ECR_COLN_MASK;
   1104   1.2   thorpej 
   1105   1.2   thorpej 		/* Multiple collisions. */
   1106   1.2   thorpej 		ifp->if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
   1107   1.2   thorpej 
   1108   1.2   thorpej 		SMC_SELECT_BANK(sc, 2);
   1109   1.2   thorpej 
   1110   1.2   thorpej 		ifp->if_timer = 0;
   1111  1.45       scw 	}
   1112  1.45       scw 
   1113  1.45       scw 	if (sc->sc_chipid == CHIP_91C111 && sc->sc_internal_phy &&
   1114  1.45       scw 	    (status & IM_MD_INT)) {
   1115  1.45       scw 		/*
   1116  1.45       scw 		 * Internal PHY status change
   1117  1.45       scw 		 */
   1118  1.45       scw 		mii_tick(&sc->sc_mii);
   1119   1.2   thorpej 	}
   1120   1.2   thorpej 
   1121   1.2   thorpej 	/*
   1122   1.2   thorpej 	 * Other errors.  Reset the interface.
   1123   1.2   thorpej 	 */
   1124   1.2   thorpej 	if (status & IM_EPH_INT) {
   1125   1.2   thorpej 		smc91cxx_stop(sc);
   1126   1.2   thorpej 		smc91cxx_init(sc);
   1127   1.2   thorpej 	}
   1128   1.2   thorpej 
   1129   1.2   thorpej 	/*
   1130   1.2   thorpej 	 * Attempt to queue more packets for transmission.
   1131   1.2   thorpej 	 */
   1132   1.2   thorpej 	smc91cxx_start(ifp);
   1133   1.2   thorpej 
   1134  1.36     pooka out:
   1135   1.2   thorpej 	/*
   1136   1.2   thorpej 	 * Reenable the interrupts we wish to receive now that processing
   1137   1.2   thorpej 	 * is complete.
   1138   1.2   thorpej 	 */
   1139  1.67      matt 	mask |= sc->sc_intmask;
   1140  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, mask);
   1141   1.5  explorer 
   1142   1.5  explorer #if NRND > 0
   1143   1.5  explorer 	if (status)
   1144   1.5  explorer 		rnd_add_uint32(&sc->rnd_source, status);
   1145   1.5  explorer #endif
   1146   1.2   thorpej 
   1147   1.2   thorpej 	return (1);
   1148   1.2   thorpej }
   1149   1.2   thorpej 
   1150   1.2   thorpej /*
   1151   1.2   thorpej  * Read a packet from the card and pass it up to the kernel.
   1152   1.2   thorpej  * NOTE!  WE EXPECT TO BE IN REGISTER WINDOW 2!
   1153   1.2   thorpej  */
   1154   1.2   thorpej void
   1155  1.72       dsl smc91cxx_read(struct smc91cxx_softc *sc)
   1156   1.2   thorpej {
   1157   1.2   thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1158   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
   1159   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
   1160   1.2   thorpej 	struct ether_header *eh;
   1161   1.2   thorpej 	struct mbuf *m;
   1162   1.2   thorpej 	u_int16_t status, packetno, packetlen;
   1163   1.2   thorpej 	u_int8_t *data;
   1164  1.41       scw 	u_int32_t dr;
   1165   1.2   thorpej 
   1166   1.2   thorpej  again:
   1167   1.2   thorpej 	/*
   1168   1.2   thorpej 	 * Set data pointer to the beginning of the packet.  Since
   1169   1.2   thorpej 	 * PTR_RCV is set, the packet number will be found automatically
   1170   1.2   thorpej 	 * in FIFO_PORTS_REG_W, FIFO_RX_MASK.
   1171   1.2   thorpej 	 */
   1172  1.67      matt 	packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
   1173  1.67      matt 	if (packetno & FIFO_REMPTY)
   1174  1.67      matt 		return;
   1175  1.67      matt 
   1176   1.2   thorpej 	bus_space_write_2(bst, bsh, POINTER_REG_W,
   1177   1.2   thorpej 	    PTR_READ | PTR_RCV | PTR_AUTOINC /* | 0x0000 */);
   1178   1.2   thorpej 
   1179   1.2   thorpej 	/*
   1180   1.2   thorpej 	 * First two words are status and packet length.
   1181   1.2   thorpej 	 */
   1182  1.41       scw 	if ((sc->sc_flags & SMC_FLAGS_32BIT_READ) == 0) {
   1183  1.41       scw 		status = bus_space_read_2(bst, bsh, DATA_REG_W);
   1184  1.41       scw 		packetlen = bus_space_read_2(bst, bsh, DATA_REG_W);
   1185  1.41       scw 	} else {
   1186  1.41       scw 		dr = bus_space_read_4(bst, bsh, DATA_REG_W);
   1187  1.41       scw #if BYTE_ORDER == LITTLE_ENDIAN
   1188  1.41       scw 		status = (u_int16_t)dr;
   1189  1.41       scw 		packetlen = (u_int16_t)(dr >> 16);
   1190  1.41       scw #else
   1191  1.41       scw 		packetlen = (u_int16_t)dr;
   1192  1.41       scw 		status = (u_int16_t)(dr >> 16);
   1193  1.41       scw #endif
   1194  1.41       scw 	}
   1195  1.41       scw 
   1196  1.41       scw 	packetlen &= RLEN_MASK;
   1197  1.67      matt 	if (packetlen < ETHER_MIN_LEN - ETHER_CRC_LEN + 6 || packetlen > 1534) {
   1198  1.67      matt 		ifp->if_ierrors++;
   1199  1.67      matt 		goto out;
   1200  1.67      matt 	}
   1201   1.2   thorpej 
   1202   1.2   thorpej 	/*
   1203   1.2   thorpej 	 * The packet length includes 3 extra words: status, length,
   1204   1.2   thorpej 	 * and an extra word that includes the control byte.
   1205   1.2   thorpej 	 */
   1206   1.2   thorpej 	packetlen -= 6;
   1207   1.2   thorpej 
   1208   1.2   thorpej 	/*
   1209   1.2   thorpej 	 * Account for receive errors and discard.
   1210   1.2   thorpej 	 */
   1211   1.2   thorpej 	if (status & RS_ERRORS) {
   1212   1.2   thorpej 		ifp->if_ierrors++;
   1213   1.2   thorpej 		goto out;
   1214   1.2   thorpej 	}
   1215   1.2   thorpej 
   1216   1.2   thorpej 	/*
   1217   1.2   thorpej 	 * Adjust for odd-length packet.
   1218   1.2   thorpej 	 */
   1219   1.2   thorpej 	if (status & RS_ODDFRAME)
   1220   1.2   thorpej 		packetlen++;
   1221   1.2   thorpej 
   1222   1.2   thorpej 	/*
   1223   1.2   thorpej 	 * Allocate a header mbuf.
   1224   1.2   thorpej 	 */
   1225   1.2   thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1226   1.2   thorpej 	if (m == NULL)
   1227   1.2   thorpej 		goto out;
   1228   1.2   thorpej 	m->m_pkthdr.rcvif = ifp;
   1229  1.33    itojun 	m->m_pkthdr.len = packetlen;
   1230   1.2   thorpej 
   1231   1.2   thorpej 	/*
   1232   1.2   thorpej 	 * Always put the packet in a cluster.
   1233   1.2   thorpej 	 * XXX should chain small mbufs if less than threshold.
   1234   1.2   thorpej 	 */
   1235   1.2   thorpej 	MCLGET(m, M_DONTWAIT);
   1236   1.2   thorpej 	if ((m->m_flags & M_EXT) == 0) {
   1237   1.2   thorpej 		m_freem(m);
   1238   1.2   thorpej 		ifp->if_ierrors++;
   1239  1.65    cegger 		aprint_error_dev(&sc->sc_dev, "can't allocate cluster for incoming packet\n");
   1240   1.2   thorpej 		goto out;
   1241   1.2   thorpej 	}
   1242   1.2   thorpej 
   1243   1.2   thorpej 	/*
   1244  1.38   thorpej 	 * Pull the packet off the interface.  Make sure the payload
   1245  1.38   thorpej 	 * is aligned.
   1246   1.2   thorpej 	 */
   1247  1.41       scw 	if ((sc->sc_flags & SMC_FLAGS_32BIT_READ) == 0) {
   1248  1.59  christos 		m->m_data = (char *) ALIGN(mtod(m, char *) +
   1249  1.41       scw 		    sizeof(struct ether_header)) - sizeof(struct ether_header);
   1250  1.41       scw 
   1251  1.41       scw 		eh = mtod(m, struct ether_header *);
   1252  1.41       scw 		data = mtod(m, u_int8_t *);
   1253  1.67      matt 		KASSERT(trunc_page((uintptr_t)data) == trunc_page((uintptr_t)data + packetlen - 1));
   1254  1.41       scw 		if (packetlen > 1)
   1255  1.41       scw 			bus_space_read_multi_stream_2(bst, bsh, DATA_REG_W,
   1256  1.41       scw 			    (u_int16_t *)data, packetlen >> 1);
   1257  1.41       scw 		if (packetlen & 1) {
   1258  1.41       scw 			data += packetlen & ~1;
   1259  1.41       scw 			*data = bus_space_read_1(bst, bsh, DATA_REG_B);
   1260  1.41       scw 		}
   1261  1.41       scw 	} else {
   1262  1.43       scw 		u_int8_t *dp;
   1263  1.43       scw 
   1264  1.59  christos 		m->m_data = (void *) ALIGN(mtod(m, void *));
   1265  1.41       scw 		eh = mtod(m, struct ether_header *);
   1266  1.43       scw 		dp = data = mtod(m, u_int8_t *);
   1267  1.67      matt 		KASSERT(trunc_page((uintptr_t)data) == trunc_page((uintptr_t)data + packetlen - 1));
   1268  1.41       scw 		if (packetlen > 3)
   1269  1.41       scw 			bus_space_read_multi_stream_4(bst, bsh, DATA_REG_W,
   1270  1.41       scw 			    (u_int32_t *)data, packetlen >> 2);
   1271  1.41       scw 		if (packetlen & 3) {
   1272  1.41       scw 			data += packetlen & ~3;
   1273  1.41       scw 			*((u_int32_t *)data) =
   1274  1.41       scw 			    bus_space_read_stream_4(bst, bsh, DATA_REG_W);
   1275  1.41       scw 		}
   1276   1.2   thorpej 	}
   1277   1.2   thorpej 
   1278   1.2   thorpej 	ifp->if_ipackets++;
   1279   1.2   thorpej 
   1280  1.21    itojun 	/*
   1281  1.21    itojun 	 * Make sure to behave as IFF_SIMPLEX in all cases.
   1282  1.21    itojun 	 * This is to cope with SMC91C92 (Megahertz XJ10BT), which
   1283  1.21    itojun 	 * loops back packets to itself on promiscuous mode.
   1284  1.21    itojun 	 * (should be ensured by chipset configuration)
   1285  1.21    itojun 	 */
   1286  1.19    itojun 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   1287  1.19    itojun 		/*
   1288  1.23    itojun 		 * Drop packet looped back from myself.
   1289  1.19    itojun 		 */
   1290  1.61    dyoung 		if (ether_cmp(eh->ether_shost, CLLADDR(ifp->if_sadl)) == 0) {
   1291   1.2   thorpej 			m_freem(m);
   1292   1.2   thorpej 			goto out;
   1293   1.2   thorpej 		}
   1294   1.2   thorpej 	}
   1295  1.21    itojun 
   1296  1.43       scw 	m->m_pkthdr.len = m->m_len = packetlen;
   1297  1.43       scw 
   1298  1.21    itojun #if NBPFILTER > 0
   1299  1.21    itojun 	/*
   1300  1.21    itojun 	 * Hand the packet off to bpf listeners.
   1301  1.21    itojun 	 */
   1302  1.21    itojun 	if (ifp->if_bpf)
   1303  1.21    itojun 		bpf_mtap(ifp->if_bpf, m);
   1304  1.21    itojun #endif
   1305   1.2   thorpej 
   1306  1.17   thorpej 	(*ifp->if_input)(ifp, m);
   1307   1.2   thorpej 
   1308   1.2   thorpej  out:
   1309   1.2   thorpej 	/*
   1310   1.2   thorpej 	 * Tell the card to free the memory occupied by this packet.
   1311   1.2   thorpej 	 */
   1312   1.2   thorpej 	while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
   1313   1.2   thorpej 		/* XXX bound this loop! */ ;
   1314   1.2   thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RELEASE);
   1315   1.2   thorpej 
   1316   1.2   thorpej 	/*
   1317   1.2   thorpej 	 * Check for another packet.
   1318   1.2   thorpej 	 */
   1319   1.2   thorpej 	packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
   1320   1.2   thorpej 	if (packetno & FIFO_REMPTY)
   1321   1.2   thorpej 		return;
   1322   1.2   thorpej 	goto again;
   1323   1.2   thorpej }
   1324   1.2   thorpej 
   1325   1.2   thorpej /*
   1326   1.2   thorpej  * Process an ioctl request.
   1327   1.2   thorpej  */
   1328   1.2   thorpej int
   1329  1.72       dsl smc91cxx_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1330   1.2   thorpej {
   1331   1.2   thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
   1332   1.2   thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
   1333   1.2   thorpej 	struct ifreq *ifr = (struct ifreq *)data;
   1334   1.2   thorpej 	int s, error = 0;
   1335   1.2   thorpej 
   1336  1.11   mycroft 	s = splnet();
   1337   1.2   thorpej 
   1338   1.2   thorpej 	switch (cmd) {
   1339  1.71    dyoung 	case SIOCINITIFADDR:
   1340   1.4   thorpej 		if ((error = smc91cxx_enable(sc)) != 0)
   1341   1.4   thorpej 			break;
   1342   1.2   thorpej 		ifp->if_flags |= IFF_UP;
   1343  1.71    dyoung 		smc91cxx_init(sc);
   1344   1.2   thorpej 		switch (ifa->ifa_addr->sa_family) {
   1345   1.2   thorpej #ifdef INET
   1346   1.2   thorpej 		case AF_INET:
   1347  1.71    dyoung 			arp_ifinit(ifp, ifa);
   1348  1.71    dyoung 			break;
   1349   1.2   thorpej #endif
   1350   1.2   thorpej 		default:
   1351   1.2   thorpej 			break;
   1352   1.2   thorpej 		}
   1353   1.2   thorpej 		break;
   1354   1.2   thorpej 
   1355   1.2   thorpej 
   1356   1.2   thorpej 	case SIOCSIFFLAGS:
   1357  1.71    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1358  1.71    dyoung 			break;
   1359  1.71    dyoung 		/* XXX re-use ether_ioctl() */
   1360  1.71    dyoung 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
   1361  1.71    dyoung 		case IFF_RUNNING:
   1362   1.2   thorpej 			/*
   1363   1.2   thorpej 			 * If interface is marked down and it is running,
   1364   1.2   thorpej 			 * stop it.
   1365   1.2   thorpej 			 */
   1366   1.2   thorpej 			smc91cxx_stop(sc);
   1367   1.2   thorpej 			ifp->if_flags &= ~IFF_RUNNING;
   1368   1.4   thorpej 			smc91cxx_disable(sc);
   1369  1.71    dyoung 			break;
   1370  1.71    dyoung 		case IFF_UP:
   1371   1.2   thorpej 			/*
   1372   1.2   thorpej 			 * If interface is marked up and it is stopped,
   1373   1.2   thorpej 			 * start it.
   1374   1.2   thorpej 			 */
   1375   1.4   thorpej 			if ((error = smc91cxx_enable(sc)) != 0)
   1376   1.4   thorpej 				break;
   1377   1.2   thorpej 			smc91cxx_init(sc);
   1378  1.71    dyoung 			break;
   1379  1.71    dyoung 		case IFF_UP|IFF_RUNNING:
   1380   1.2   thorpej 			/*
   1381   1.2   thorpej 			 * Reset the interface to pick up changes in any
   1382   1.2   thorpej 			 * other flags that affect hardware registers.
   1383   1.2   thorpej 			 */
   1384   1.2   thorpej 			smc91cxx_reset(sc);
   1385  1.71    dyoung 			break;
   1386  1.71    dyoung 		case 0:
   1387  1.71    dyoung 			break;
   1388   1.2   thorpej 		}
   1389   1.2   thorpej 		break;
   1390   1.2   thorpej 
   1391   1.2   thorpej 	case SIOCADDMULTI:
   1392   1.2   thorpej 	case SIOCDELMULTI:
   1393  1.25     jhawk 		if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0) {
   1394   1.4   thorpej 			error = EIO;
   1395   1.4   thorpej 			break;
   1396   1.4   thorpej 		}
   1397   1.4   thorpej 
   1398  1.62    dyoung 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   1399   1.2   thorpej 			/*
   1400   1.2   thorpej 			 * Multicast list has changed; set the hardware
   1401   1.2   thorpej 			 * filter accordingly.
   1402   1.2   thorpej 			 */
   1403  1.49   thorpej 			if (ifp->if_flags & IFF_RUNNING)
   1404  1.49   thorpej 				smc91cxx_reset(sc);
   1405   1.2   thorpej 			error = 0;
   1406   1.2   thorpej 		}
   1407   1.2   thorpej 		break;
   1408   1.2   thorpej 
   1409   1.2   thorpej 	case SIOCGIFMEDIA:
   1410   1.2   thorpej 	case SIOCSIFMEDIA:
   1411  1.26    briggs 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1412   1.2   thorpej 		break;
   1413   1.2   thorpej 
   1414   1.2   thorpej 	default:
   1415  1.71    dyoung 		error = ether_ioctl(ifp, cmd, data);
   1416   1.2   thorpej 		break;
   1417   1.2   thorpej 	}
   1418   1.2   thorpej 
   1419   1.2   thorpej 	splx(s);
   1420   1.2   thorpej 	return (error);
   1421   1.2   thorpej }
   1422   1.2   thorpej 
   1423   1.2   thorpej /*
   1424   1.2   thorpej  * Reset the interface.
   1425   1.2   thorpej  */
   1426   1.2   thorpej void
   1427  1.72       dsl smc91cxx_reset(struct smc91cxx_softc *sc)
   1428   1.2   thorpej {
   1429   1.2   thorpej 	int s;
   1430   1.2   thorpej 
   1431  1.11   mycroft 	s = splnet();
   1432   1.2   thorpej 	smc91cxx_stop(sc);
   1433   1.2   thorpej 	smc91cxx_init(sc);
   1434   1.2   thorpej 	splx(s);
   1435   1.2   thorpej }
   1436   1.2   thorpej 
   1437   1.2   thorpej /*
   1438   1.2   thorpej  * Watchdog timer.
   1439   1.2   thorpej  */
   1440   1.2   thorpej void
   1441  1.72       dsl smc91cxx_watchdog(struct ifnet *ifp)
   1442   1.2   thorpej {
   1443   1.2   thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
   1444   1.2   thorpej 
   1445  1.65    cegger 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
   1446   1.2   thorpej 	ifp->if_oerrors++;
   1447   1.2   thorpej 	smc91cxx_reset(sc);
   1448   1.2   thorpej }
   1449   1.2   thorpej 
   1450   1.2   thorpej /*
   1451   1.2   thorpej  * Stop output on the interface.
   1452   1.2   thorpej  */
   1453   1.2   thorpej void
   1454  1.72       dsl smc91cxx_stop(struct smc91cxx_softc *sc)
   1455   1.2   thorpej {
   1456   1.2   thorpej 	bus_space_tag_t bst = sc->sc_bst;
   1457   1.2   thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
   1458   1.2   thorpej 
   1459   1.2   thorpej 	/*
   1460   1.2   thorpej 	 * Clear interrupt mask; disable all interrupts.
   1461   1.2   thorpej 	 */
   1462   1.2   thorpej 	SMC_SELECT_BANK(sc, 2);
   1463  1.67      matt 	smc91cxx_intr_mask_write(bst, bsh, 0);
   1464   1.2   thorpej 
   1465   1.2   thorpej 	/*
   1466   1.2   thorpej 	 * Disable transmitter and receiver.
   1467   1.2   thorpej 	 */
   1468   1.2   thorpej 	SMC_SELECT_BANK(sc, 0);
   1469   1.2   thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
   1470   1.2   thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
   1471   1.2   thorpej 
   1472   1.2   thorpej 	/*
   1473   1.2   thorpej 	 * Cancel watchdog timer.
   1474   1.2   thorpej 	 */
   1475   1.2   thorpej 	sc->sc_ec.ec_if.if_timer = 0;
   1476   1.4   thorpej }
   1477   1.4   thorpej 
   1478   1.4   thorpej /*
   1479   1.4   thorpej  * Enable power on the interface.
   1480   1.4   thorpej  */
   1481   1.4   thorpej int
   1482  1.72       dsl smc91cxx_enable(struct smc91cxx_softc *sc)
   1483   1.4   thorpej {
   1484   1.4   thorpej 
   1485  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) == 0 && sc->sc_enable != NULL) {
   1486   1.4   thorpej 		if ((*sc->sc_enable)(sc) != 0) {
   1487  1.65    cegger 			aprint_error_dev(&sc->sc_dev, "device enable failed\n");
   1488   1.4   thorpej 			return (EIO);
   1489   1.4   thorpej 		}
   1490   1.4   thorpej 	}
   1491   1.4   thorpej 
   1492  1.25     jhawk 	sc->sc_flags |= SMC_FLAGS_ENABLED;
   1493   1.4   thorpej 	return (0);
   1494   1.4   thorpej }
   1495   1.4   thorpej 
   1496   1.4   thorpej /*
   1497   1.4   thorpej  * Disable power on the interface.
   1498   1.4   thorpej  */
   1499   1.4   thorpej void
   1500  1.72       dsl smc91cxx_disable(struct smc91cxx_softc *sc)
   1501   1.4   thorpej {
   1502   1.4   thorpej 
   1503  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ENABLED) != 0 && sc->sc_disable != NULL) {
   1504   1.4   thorpej 		(*sc->sc_disable)(sc);
   1505  1.25     jhawk 		sc->sc_flags &= ~SMC_FLAGS_ENABLED;
   1506   1.4   thorpej 	}
   1507  1.13   thorpej }
   1508  1.13   thorpej 
   1509  1.13   thorpej int
   1510  1.75    cegger smc91cxx_activate(device_t self, enum devact act)
   1511  1.13   thorpej {
   1512  1.13   thorpej 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
   1513  1.13   thorpej 	int rv = 0, s;
   1514  1.13   thorpej 
   1515  1.13   thorpej 	s = splnet();
   1516  1.13   thorpej 	switch (act) {
   1517  1.13   thorpej 	case DVACT_ACTIVATE:
   1518  1.13   thorpej 		rv = EOPNOTSUPP;
   1519  1.13   thorpej 		break;
   1520  1.13   thorpej 
   1521  1.13   thorpej 	case DVACT_DEACTIVATE:
   1522  1.24     enami 		if_deactivate(&sc->sc_ec.ec_if);
   1523  1.13   thorpej 		break;
   1524  1.13   thorpej 	}
   1525  1.13   thorpej 	splx(s);
   1526  1.13   thorpej 	return (rv);
   1527  1.22    itojun }
   1528  1.22    itojun 
   1529  1.22    itojun int
   1530  1.75    cegger smc91cxx_detach(device_t self, int flags)
   1531  1.22    itojun {
   1532  1.22    itojun 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
   1533  1.22    itojun 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1534  1.22    itojun 
   1535  1.25     jhawk 	/* Succeed now if there's no work to do. */
   1536  1.25     jhawk 	if ((sc->sc_flags & SMC_FLAGS_ATTACHED) == 0)
   1537  1.25     jhawk 		return (0);
   1538  1.25     jhawk 
   1539  1.25     jhawk 
   1540  1.25     jhawk 	/* smc91cxx_disable() checks SMC_FLAGS_ENABLED */
   1541  1.22    itojun 	smc91cxx_disable(sc);
   1542  1.22    itojun 
   1543  1.22    itojun 	/* smc91cxx_attach() never fails */
   1544  1.22    itojun 
   1545  1.22    itojun 	/* Delete all media. */
   1546  1.26    briggs 	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
   1547  1.22    itojun 
   1548  1.22    itojun #if NRND > 0
   1549  1.22    itojun 	rnd_detach_source(&sc->rnd_source);
   1550  1.22    itojun #endif
   1551  1.22    itojun 	ether_ifdetach(ifp);
   1552  1.22    itojun 	if_detach(ifp);
   1553  1.22    itojun 
   1554  1.22    itojun 	return (0);
   1555   1.2   thorpej }
   1556  1.26    briggs 
   1557  1.26    briggs u_int32_t
   1558  1.75    cegger smc91cxx_mii_bitbang_read(device_t self)
   1559  1.26    briggs {
   1560  1.26    briggs 	struct smc91cxx_softc *sc = (void *) self;
   1561  1.26    briggs 
   1562  1.26    briggs 	/* We're already in bank 3. */
   1563  1.26    briggs 	return (bus_space_read_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W));
   1564  1.26    briggs }
   1565  1.26    briggs 
   1566  1.26    briggs void
   1567  1.75    cegger smc91cxx_mii_bitbang_write(device_t self, u_int32_t val)
   1568  1.26    briggs {
   1569  1.26    briggs 	struct smc91cxx_softc *sc = (void *) self;
   1570  1.26    briggs 
   1571  1.26    briggs 	/* We're already in bank 3. */
   1572  1.26    briggs 	bus_space_write_2(sc->sc_bst, sc->sc_bsh, MGMT_REG_W, val);
   1573  1.26    briggs }
   1574  1.26    briggs 
   1575  1.26    briggs int
   1576  1.75    cegger smc91cxx_mii_readreg(device_t self, int phy, int reg)
   1577  1.26    briggs {
   1578  1.26    briggs 	struct smc91cxx_softc *sc = (void *) self;
   1579  1.26    briggs 	int val;
   1580  1.26    briggs 
   1581  1.26    briggs 	SMC_SELECT_BANK(sc, 3);
   1582  1.26    briggs 
   1583  1.26    briggs 	val = mii_bitbang_readreg(self, &smc91cxx_mii_bitbang_ops, phy, reg);
   1584  1.26    briggs 
   1585  1.26    briggs 	SMC_SELECT_BANK(sc, 2);
   1586  1.26    briggs 
   1587  1.26    briggs 	return (val);
   1588  1.26    briggs }
   1589  1.26    briggs 
   1590  1.26    briggs void
   1591  1.75    cegger smc91cxx_mii_writereg(device_t self, int phy, int reg, int val)
   1592  1.26    briggs {
   1593  1.26    briggs 	struct smc91cxx_softc *sc = (void *) self;
   1594  1.26    briggs 
   1595  1.26    briggs 	SMC_SELECT_BANK(sc, 3);
   1596  1.26    briggs 
   1597  1.26    briggs 	mii_bitbang_writereg(self, &smc91cxx_mii_bitbang_ops, phy, reg, val);
   1598  1.26    briggs 
   1599  1.26    briggs 	SMC_SELECT_BANK(sc, 2);
   1600  1.26    briggs }
   1601  1.26    briggs 
   1602  1.26    briggs void
   1603  1.75    cegger smc91cxx_statchg(device_t self)
   1604  1.26    briggs {
   1605  1.26    briggs 	struct smc91cxx_softc *sc = (struct smc91cxx_softc *)self;
   1606  1.26    briggs 	bus_space_tag_t bst = sc->sc_bst;
   1607  1.26    briggs 	bus_space_handle_t bsh = sc->sc_bsh;
   1608  1.26    briggs 	int mctl;
   1609  1.26    briggs 
   1610  1.26    briggs 	SMC_SELECT_BANK(sc, 0);
   1611  1.26    briggs 	mctl = bus_space_read_2(bst, bsh, TXMIT_CONTROL_REG_W);
   1612  1.26    briggs 	if (sc->sc_mii.mii_media_active & IFM_FDX)
   1613  1.26    briggs 		mctl |= TCR_SWFDUP;
   1614  1.26    briggs 	else
   1615  1.26    briggs 		mctl &= ~TCR_SWFDUP;
   1616  1.26    briggs 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, mctl);
   1617  1.26    briggs 	SMC_SELECT_BANK(sc, 2);	/* back to operating window */
   1618  1.26    briggs }
   1619  1.26    briggs 
   1620  1.26    briggs /*
   1621  1.26    briggs  * One second timer, used to tick the MII.
   1622  1.26    briggs  */
   1623  1.26    briggs void
   1624  1.72       dsl smc91cxx_tick(void *arg)
   1625  1.26    briggs {
   1626  1.26    briggs 	struct smc91cxx_softc *sc = arg;
   1627  1.26    briggs 	int s;
   1628  1.26    briggs 
   1629  1.26    briggs #ifdef DIAGNOSTIC
   1630  1.26    briggs 	if ((sc->sc_flags & SMC_FLAGS_HAS_MII) == 0)
   1631  1.26    briggs 		panic("smc91cxx_tick");
   1632  1.26    briggs #endif
   1633  1.26    briggs 
   1634  1.55   thorpej 	if (!device_is_active(&sc->sc_dev))
   1635  1.26    briggs 		return;
   1636  1.26    briggs 
   1637  1.26    briggs 	s = splnet();
   1638  1.26    briggs 	mii_tick(&sc->sc_mii);
   1639  1.26    briggs 	splx(s);
   1640  1.26    briggs 
   1641  1.26    briggs 	callout_reset(&sc->sc_mii_callout, hz, smc91cxx_tick, sc);
   1642  1.26    briggs }
   1643  1.26    briggs 
   1644