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