Home | History | Annotate | Line # | Download | only in ic
smc91cxx.c revision 1.3
      1  1.3      jtc /*	$NetBSD: smc91cxx.c,v 1.3 1997/10/09 08:03:52 jtc 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.2  thorpej 
     80  1.2  thorpej #include "bpfilter.h"
     81  1.2  thorpej 
     82  1.2  thorpej #include <sys/param.h>
     83  1.2  thorpej #include <sys/systm.h>
     84  1.2  thorpej #include <sys/mbuf.h>
     85  1.2  thorpej #include <sys/syslog.h>
     86  1.2  thorpej #include <sys/socket.h>
     87  1.2  thorpej #include <sys/device.h>
     88  1.2  thorpej #include <sys/malloc.h>
     89  1.2  thorpej #include <sys/ioctl.h>
     90  1.2  thorpej #include <sys/errno.h>
     91  1.2  thorpej 
     92  1.2  thorpej #include <machine/bus.h>
     93  1.2  thorpej #include <machine/intr.h>
     94  1.2  thorpej 
     95  1.2  thorpej #include <net/if.h>
     96  1.2  thorpej #include <net/if_dl.h>
     97  1.2  thorpej #include <net/if_ether.h>
     98  1.2  thorpej #include <net/if_media.h>
     99  1.2  thorpej 
    100  1.2  thorpej #ifdef INET
    101  1.2  thorpej #include <netinet/in.h>
    102  1.2  thorpej #include <netinet/if_inarp.h>
    103  1.2  thorpej #include <netinet/in_systm.h>
    104  1.2  thorpej #include <netinet/in_var.h>
    105  1.2  thorpej #include <netinet/ip.h>
    106  1.2  thorpej #endif
    107  1.2  thorpej 
    108  1.2  thorpej #ifdef NS
    109  1.2  thorpej #include <netns/ns.h>
    110  1.2  thorpej #include <netns/ns_if.h>
    111  1.2  thorpej #endif
    112  1.2  thorpej 
    113  1.2  thorpej #if defined(CCITT) && defined(LLC)
    114  1.2  thorpej #include <sys/socketvar.h>
    115  1.2  thorpej #include <netccitt/x25.h>
    116  1.2  thorpej #include <netccitt/pk.h>
    117  1.2  thorpej #include <netccitt/pk_var.h>
    118  1.2  thorpej #include <netccitt/pk_extern.h>
    119  1.2  thorpej #endif
    120  1.2  thorpej 
    121  1.2  thorpej #if NBPFILTER > 0
    122  1.2  thorpej #include <net/bpf.h>
    123  1.2  thorpej #include <net/bpfdesc.h>
    124  1.2  thorpej #endif
    125  1.2  thorpej 
    126  1.2  thorpej #include <dev/ic/smc91cxxreg.h>
    127  1.2  thorpej #include <dev/ic/smc91cxxvar.h>
    128  1.2  thorpej 
    129  1.2  thorpej /* XXX Hardware padding doesn't work yet(?) */
    130  1.2  thorpej #define	SMC91CXX_SW_PAD
    131  1.2  thorpej 
    132  1.2  thorpej struct cfdriver sm_cd = {
    133  1.2  thorpej 	NULL, "sm", DV_IFNET
    134  1.2  thorpej };
    135  1.2  thorpej 
    136  1.2  thorpej const char *smc91cxx_idstrs[] = {
    137  1.2  thorpej 	NULL,				/* 0 */
    138  1.2  thorpej 	NULL,				/* 1 */
    139  1.2  thorpej 	NULL,				/* 2 */
    140  1.2  thorpej 	"SMC91C90/91C92",		/* 3 */
    141  1.2  thorpej 	"SMC91C94",			/* 4 */
    142  1.2  thorpej 	"SMC91C95",			/* 5 */
    143  1.2  thorpej 	NULL,				/* 6 */
    144  1.2  thorpej 	"SMC91C100",			/* 7 */
    145  1.2  thorpej 	NULL,				/* 8 */
    146  1.2  thorpej 	NULL,				/* 9 */
    147  1.2  thorpej 	NULL,				/* 10 */
    148  1.2  thorpej 	NULL,				/* 11 */
    149  1.2  thorpej 	NULL,				/* 12 */
    150  1.2  thorpej 	NULL,				/* 13 */
    151  1.2  thorpej 	NULL,				/* 14 */
    152  1.2  thorpej 	NULL,				/* 15 */
    153  1.2  thorpej };
    154  1.2  thorpej 
    155  1.2  thorpej /* Supported media types. */
    156  1.2  thorpej const int smc91cxx_media[] = {
    157  1.2  thorpej 	IFM_ETHER|IFM_10_T,
    158  1.2  thorpej 	IFM_ETHER|IFM_10_5,
    159  1.2  thorpej };
    160  1.2  thorpej #define	NSMC91CxxMEDIA	(sizeof(smc91cxx_media) / sizeof(smc91cxx_media[0]))
    161  1.2  thorpej 
    162  1.2  thorpej int	smc91cxx_mediachange __P((struct ifnet *));
    163  1.2  thorpej void	smc91cxx_mediastatus __P((struct ifnet *, struct ifmediareq *));
    164  1.2  thorpej 
    165  1.2  thorpej int	smc91cxx_set_media __P((struct smc91cxx_softc *, int));
    166  1.2  thorpej 
    167  1.2  thorpej void	smc91cxx_init __P((struct smc91cxx_softc *));
    168  1.2  thorpej void	smc91cxx_read __P((struct smc91cxx_softc *));
    169  1.2  thorpej void	smc91cxx_reset __P((struct smc91cxx_softc *));
    170  1.2  thorpej void	smc91cxx_start __P((struct ifnet *));
    171  1.2  thorpej void	smc91cxx_resume __P((struct smc91cxx_softc *));
    172  1.2  thorpej void	smc91cxx_stop __P((struct smc91cxx_softc *));
    173  1.2  thorpej void	smc91cxx_watchdog __P((struct ifnet *));
    174  1.2  thorpej int	smc91cxx_ioctl __P((struct ifnet *, u_long, caddr_t));
    175  1.2  thorpej 
    176  1.2  thorpej /* XXX Should be in a common header file. */
    177  1.2  thorpej #define	ETHER_MAX_LEN	1518
    178  1.2  thorpej #define	ETHER_MIN_LEN	64
    179  1.2  thorpej #define	ETHER_CRC_LEN	4
    180  1.2  thorpej 
    181  1.2  thorpej static __inline int ether_cmp __P((void *, void *));
    182  1.2  thorpej static __inline int
    183  1.2  thorpej ether_cmp(va, vb)
    184  1.2  thorpej 	void *va, *vb;
    185  1.2  thorpej {
    186  1.2  thorpej 	u_int8_t *a = va;
    187  1.2  thorpej 	u_int8_t *b = vb;
    188  1.2  thorpej 
    189  1.2  thorpej 	return ((a[5] != b[5]) || (a[4] != b[4]) || (a[3] != b[3]) ||
    190  1.2  thorpej 		(a[2] != b[2]) || (a[1] != b[1]) || (a[0] != b[0]));
    191  1.2  thorpej }
    192  1.2  thorpej 
    193  1.2  thorpej void
    194  1.2  thorpej smc91cxx_attach(sc, myea)
    195  1.2  thorpej 	struct smc91cxx_softc *sc;
    196  1.2  thorpej 	u_int8_t *myea;
    197  1.2  thorpej {
    198  1.2  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    199  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    200  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    201  1.2  thorpej 	const char *idstr;
    202  1.2  thorpej 	u_int16_t tmp;
    203  1.2  thorpej 	u_int8_t enaddr[ETHER_ADDR_LEN];
    204  1.2  thorpej 	int i, aui;
    205  1.2  thorpej 
    206  1.2  thorpej 	/* Make sure the chip is stopped. */
    207  1.2  thorpej 	smc91cxx_stop(sc);
    208  1.2  thorpej 
    209  1.2  thorpej 	SMC_SELECT_BANK(sc, 3);
    210  1.2  thorpej 	tmp = bus_space_read_2(bst, bsh, REVISION_REG_W);
    211  1.2  thorpej 	idstr = smc91cxx_idstrs[RR_ID(tmp)];
    212  1.2  thorpej 	printf("%s: ", sc->sc_dev.dv_xname);
    213  1.2  thorpej 	if (idstr != NULL)
    214  1.2  thorpej 		printf("%s, ", idstr);
    215  1.2  thorpej 	else
    216  1.2  thorpej 		printf("unknown chip id %d, ", RR_ID(tmp));
    217  1.2  thorpej 	printf("revision %d\n", RR_REV(tmp));
    218  1.2  thorpej 
    219  1.2  thorpej 	/* Read the station address from the chip. */
    220  1.2  thorpej 	SMC_SELECT_BANK(sc, 1);
    221  1.2  thorpej 	if (myea == NULL) {
    222  1.2  thorpej 		myea = enaddr;
    223  1.2  thorpej 		for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    224  1.2  thorpej 			tmp = bus_space_read_2(bst, bsh, IAR_ADDR0_REG_W + i);
    225  1.2  thorpej 			myea[i + 1] = (tmp >> 8) & 0xff;
    226  1.2  thorpej 			myea[i] = tmp & 0xff;
    227  1.2  thorpej 		}
    228  1.2  thorpej 	}
    229  1.2  thorpej 	printf("%s: MAC address %s, ", sc->sc_dev.dv_xname,
    230  1.2  thorpej 	    ether_sprintf(myea));
    231  1.2  thorpej 
    232  1.2  thorpej 	/* ..and default media. */
    233  1.2  thorpej 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    234  1.2  thorpej 	printf("default media %s\n", (aui = (tmp & CR_AUI_SELECT)) ?
    235  1.2  thorpej 	    "AUI" : "UTP");
    236  1.2  thorpej 
    237  1.2  thorpej 	/* Initialize the ifnet structure. */
    238  1.2  thorpej 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
    239  1.2  thorpej 	ifp->if_softc = sc;
    240  1.2  thorpej 	ifp->if_start = smc91cxx_start;
    241  1.2  thorpej 	ifp->if_ioctl = smc91cxx_ioctl;
    242  1.2  thorpej 	ifp->if_watchdog = smc91cxx_watchdog;
    243  1.2  thorpej 	ifp->if_flags =
    244  1.2  thorpej 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
    245  1.2  thorpej 
    246  1.2  thorpej 	/* Attach the interface. */
    247  1.2  thorpej 	if_attach(ifp);
    248  1.2  thorpej 	ether_ifattach(ifp, myea);
    249  1.2  thorpej 
    250  1.2  thorpej 	/* Initialize the media structures. */
    251  1.2  thorpej 	ifmedia_init(&sc->sc_media, 0, smc91cxx_mediachange,
    252  1.2  thorpej 	    smc91cxx_mediastatus);
    253  1.2  thorpej 	for (i = 0; i < NSMC91CxxMEDIA; i++)
    254  1.2  thorpej 		ifmedia_add(&sc->sc_media, smc91cxx_media[i], 0, NULL);
    255  1.2  thorpej 	ifmedia_set(&sc->sc_media, IFM_ETHER | (aui ? IFM_10_5 : IFM_10_T));
    256  1.2  thorpej 
    257  1.2  thorpej #if NBPFILTER > 0
    258  1.2  thorpej 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    259  1.2  thorpej #endif
    260  1.2  thorpej }
    261  1.2  thorpej 
    262  1.2  thorpej /*
    263  1.2  thorpej  * Change media according to request.
    264  1.2  thorpej  */
    265  1.2  thorpej int
    266  1.2  thorpej smc91cxx_mediachange(ifp)
    267  1.2  thorpej 	struct ifnet *ifp;
    268  1.2  thorpej {
    269  1.2  thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    270  1.2  thorpej 
    271  1.2  thorpej 	return (smc91cxx_set_media(sc, sc->sc_media.ifm_media));
    272  1.2  thorpej }
    273  1.2  thorpej 
    274  1.2  thorpej int
    275  1.2  thorpej smc91cxx_set_media(sc, media)
    276  1.2  thorpej 	struct smc91cxx_softc *sc;
    277  1.2  thorpej 	int media;
    278  1.2  thorpej {
    279  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    280  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    281  1.2  thorpej 	u_int16_t tmp;
    282  1.2  thorpej 
    283  1.2  thorpej 	if (IFM_TYPE(media) != IFM_ETHER)
    284  1.2  thorpej 		return (EINVAL);
    285  1.2  thorpej 
    286  1.2  thorpej 	switch (IFM_SUBTYPE(media)) {
    287  1.2  thorpej 	case IFM_10_T:
    288  1.2  thorpej 	case IFM_10_5:
    289  1.2  thorpej 		SMC_SELECT_BANK(sc, 1);
    290  1.2  thorpej 		tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    291  1.2  thorpej 		if (IFM_SUBTYPE(media) == IFM_10_5)
    292  1.2  thorpej 			tmp |= CR_AUI_SELECT;
    293  1.2  thorpej 		else
    294  1.2  thorpej 			tmp &= ~CR_AUI_SELECT;
    295  1.2  thorpej 		bus_space_write_2(bst, bsh, CONFIG_REG_W, tmp);
    296  1.2  thorpej 		delay(20000);	/* XXX is this needed? */
    297  1.2  thorpej 		break;
    298  1.2  thorpej 
    299  1.2  thorpej 	default:
    300  1.2  thorpej 		return (EINVAL);
    301  1.2  thorpej 	}
    302  1.2  thorpej 
    303  1.2  thorpej 	return (0);
    304  1.2  thorpej }
    305  1.2  thorpej 
    306  1.2  thorpej /*
    307  1.2  thorpej  * Notify the world which media we're using.
    308  1.2  thorpej  */
    309  1.2  thorpej void
    310  1.2  thorpej smc91cxx_mediastatus(ifp, ifmr)
    311  1.2  thorpej 	struct ifnet *ifp;
    312  1.2  thorpej 	struct ifmediareq *ifmr;
    313  1.2  thorpej {
    314  1.2  thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    315  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    316  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    317  1.2  thorpej 	u_int16_t tmp;
    318  1.2  thorpej 
    319  1.2  thorpej 	SMC_SELECT_BANK(sc, 1);
    320  1.2  thorpej 	tmp = bus_space_read_2(bst, bsh, CONFIG_REG_W);
    321  1.2  thorpej 	ifmr->ifm_active =
    322  1.2  thorpej 	    IFM_ETHER | ((tmp & CR_AUI_SELECT) ? IFM_10_5 : IFM_10_T);
    323  1.2  thorpej }
    324  1.2  thorpej 
    325  1.2  thorpej /*
    326  1.2  thorpej  * Reset and initialize the chip.
    327  1.2  thorpej  */
    328  1.2  thorpej void
    329  1.2  thorpej smc91cxx_init(sc)
    330  1.2  thorpej 	struct smc91cxx_softc *sc;
    331  1.2  thorpej {
    332  1.2  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    333  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    334  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    335  1.2  thorpej 	u_int16_t tmp;
    336  1.2  thorpej 	u_int8_t *enaddr;
    337  1.2  thorpej 	int s, i;
    338  1.2  thorpej 
    339  1.2  thorpej 	s = splimp();
    340  1.2  thorpej 
    341  1.2  thorpej 	/*
    342  1.2  thorpej 	 * This resets the registersmostly to defaults, but doesn't
    343  1.2  thorpej 	 * affect the EEPROM.  After the reset cycle, we pause briefly
    344  1.2  thorpej 	 * for the chip to recover.
    345  1.2  thorpej 	 *
    346  1.2  thorpej 	 * XXX how long are we really supposed to delay?  --thorpej
    347  1.2  thorpej 	 */
    348  1.2  thorpej 	SMC_SELECT_BANK(sc, 0);
    349  1.2  thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, RCR_SOFTRESET);
    350  1.2  thorpej 	delay(100);
    351  1.2  thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
    352  1.2  thorpej 	delay(200);
    353  1.2  thorpej 
    354  1.2  thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
    355  1.2  thorpej 
    356  1.2  thorpej 	/* Set the Ethernet address. */
    357  1.2  thorpej 	SMC_SELECT_BANK(sc, 1);
    358  1.2  thorpej 	enaddr = (u_int8_t *)LLADDR(ifp->if_sadl);
    359  1.2  thorpej 	for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
    360  1.2  thorpej 		tmp = enaddr[i + 1] << 8 | enaddr[i];
    361  1.2  thorpej 		bus_space_write_2(bst, bsh, IAR_ADDR0_REG_W + i, tmp);
    362  1.2  thorpej 	}
    363  1.2  thorpej 
    364  1.2  thorpej 	/*
    365  1.2  thorpej 	 * Set the control register to automatically release successfully
    366  1.2  thorpej 	 * transmitted packets (making the best use of our limited memory)
    367  1.2  thorpej 	 * and enable the EPH interrupt on certain TX errors.
    368  1.2  thorpej 	 */
    369  1.2  thorpej 	bus_space_write_2(bst, bsh, CONTROL_REG_W, (CTR_AUTO_RELEASE |
    370  1.2  thorpej 	    CTR_TE_ENABLE | CTR_CR_ENABLE | CTR_LE_ENABLE));
    371  1.2  thorpej 
    372  1.2  thorpej 	/*
    373  1.2  thorpej 	 * Reset the MMU and wait for it to be un-busy.
    374  1.2  thorpej 	 */
    375  1.2  thorpej 	SMC_SELECT_BANK(sc, 2);
    376  1.2  thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RESET);
    377  1.2  thorpej 	while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
    378  1.2  thorpej 		/* XXX bound this loop! */ ;
    379  1.2  thorpej 
    380  1.2  thorpej 	/*
    381  1.2  thorpej 	 * Disable all interrupts.
    382  1.2  thorpej 	 */
    383  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
    384  1.2  thorpej 
    385  1.2  thorpej 	/*
    386  1.2  thorpej 	 * Set current media.
    387  1.2  thorpej 	 */
    388  1.2  thorpej 	smc91cxx_set_media(sc, sc->sc_media.ifm_cur->ifm_media);
    389  1.2  thorpej 
    390  1.2  thorpej 	/*
    391  1.2  thorpej 	 * Set the receive filter.  We want receive enable and auto
    392  1.2  thorpej 	 * strip of CRC from received packet.  If we are in promisc. mode,
    393  1.2  thorpej 	 * then set that bit as well.
    394  1.2  thorpej 	 *
    395  1.2  thorpej 	 * XXX Initialize multicast filter.  For now, we just accept
    396  1.2  thorpej 	 * XXX all multicast.
    397  1.2  thorpej 	 */
    398  1.2  thorpej 	SMC_SELECT_BANK(sc, 0);
    399  1.2  thorpej 
    400  1.2  thorpej 	tmp = RCR_ENABLE | RCR_STRIP_CRC | RCR_ALMUL;
    401  1.2  thorpej 	if (ifp->if_flags & IFF_PROMISC)
    402  1.2  thorpej 		tmp |= RCR_PROMISC;
    403  1.2  thorpej 
    404  1.2  thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, tmp);
    405  1.2  thorpej 
    406  1.2  thorpej 	/*
    407  1.2  thorpej 	 * Set transmitter control to "enabled".
    408  1.2  thorpej 	 */
    409  1.2  thorpej 	tmp = TCR_ENABLE;
    410  1.2  thorpej 
    411  1.2  thorpej #ifndef SMC91CXX_SW_PAD
    412  1.2  thorpej 	/*
    413  1.2  thorpej 	 * Enable hardware padding of transmitted packets.
    414  1.2  thorpej 	 * XXX doesn't work?
    415  1.2  thorpej 	 */
    416  1.2  thorpej 	tmp |= TCR_PAD_ENABLE;
    417  1.2  thorpej #endif
    418  1.2  thorpej 
    419  1.2  thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, tmp);
    420  1.2  thorpej 
    421  1.2  thorpej 	/*
    422  1.2  thorpej 	 * Now, enable interrupts.
    423  1.2  thorpej 	 */
    424  1.2  thorpej 	SMC_SELECT_BANK(sc, 2);
    425  1.2  thorpej 
    426  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
    427  1.2  thorpej 	    IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT | IM_TX_INT);
    428  1.2  thorpej 
    429  1.2  thorpej 	/* Interface is now running, with no output active. */
    430  1.2  thorpej 	ifp->if_flags |= IFF_RUNNING;
    431  1.2  thorpej 	ifp->if_flags &= ~IFF_OACTIVE;
    432  1.2  thorpej 
    433  1.2  thorpej 	/*
    434  1.2  thorpej 	 * Attempt to start any pending transmission.
    435  1.2  thorpej 	 */
    436  1.2  thorpej 	smc91cxx_start(ifp);
    437  1.2  thorpej 
    438  1.2  thorpej 	splx(s);
    439  1.2  thorpej }
    440  1.2  thorpej 
    441  1.2  thorpej /*
    442  1.2  thorpej  * Start output on an interface.
    443  1.2  thorpej  * Must be called at splimp or interrupt level.
    444  1.2  thorpej  */
    445  1.2  thorpej void
    446  1.2  thorpej smc91cxx_start(ifp)
    447  1.2  thorpej 	struct ifnet *ifp;
    448  1.2  thorpej {
    449  1.2  thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    450  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    451  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    452  1.2  thorpej 	u_int len;
    453  1.2  thorpej 	struct mbuf *m, *top;
    454  1.2  thorpej 	u_int16_t length, npages;
    455  1.2  thorpej 	u_int8_t packetno;
    456  1.2  thorpej 	int timo, pad;
    457  1.2  thorpej 
    458  1.2  thorpej 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
    459  1.2  thorpej 		return;
    460  1.2  thorpej 
    461  1.2  thorpej  again:
    462  1.2  thorpej 	/*
    463  1.2  thorpej 	 * Peek at the next packet.
    464  1.2  thorpej 	 */
    465  1.2  thorpej 	if ((m = ifp->if_snd.ifq_head) == NULL)
    466  1.2  thorpej 		return;
    467  1.2  thorpej 
    468  1.2  thorpej 	/*
    469  1.2  thorpej 	 * Compute the frame length and set pad to give an overall even
    470  1.2  thorpej 	 * number of bytes.  Below, we assume that the packet length
    471  1.2  thorpej 	 * is even.
    472  1.2  thorpej 	 */
    473  1.2  thorpej 	for (len = 0, top = m; m != NULL; m = m->m_next)
    474  1.2  thorpej 		len += m->m_len;
    475  1.2  thorpej 	pad = (len & 1);
    476  1.2  thorpej 
    477  1.2  thorpej 	/*
    478  1.2  thorpej 	 * We drop packets that are too large.  Perhaps we should
    479  1.2  thorpej 	 * truncate them instead?
    480  1.2  thorpej 	 */
    481  1.2  thorpej 	if ((len + pad) > (ETHER_MAX_LEN - ETHER_CRC_LEN)) {
    482  1.2  thorpej 		printf("%s: large packet discarded\n", sc->sc_dev.dv_xname);
    483  1.2  thorpej 		ifp->if_oerrors++;
    484  1.2  thorpej 		IF_DEQUEUE(&ifp->if_snd, m);
    485  1.2  thorpej 		m_freem(m);
    486  1.2  thorpej 		goto readcheck;
    487  1.2  thorpej 	}
    488  1.2  thorpej 
    489  1.2  thorpej #ifdef SMC91CXX_SW_PAD
    490  1.2  thorpej 	/*
    491  1.2  thorpej 	 * Not using hardware padding; pad to ETHER_MIN_LEN.
    492  1.2  thorpej 	 */
    493  1.2  thorpej 	if (len < (ETHER_MIN_LEN - ETHER_CRC_LEN))
    494  1.2  thorpej 		pad = ETHER_MIN_LEN - ETHER_CRC_LEN - len;
    495  1.2  thorpej #endif
    496  1.2  thorpej 
    497  1.2  thorpej 	length = pad + len;
    498  1.2  thorpej 
    499  1.2  thorpej 	/*
    500  1.2  thorpej 	 * The MMU has a 256 byte page size.  The MMU expects us to
    501  1.2  thorpej 	 * ask for "npages - 1".  We include space for the status word,
    502  1.2  thorpej 	 * byte count, and control bytes in the allocation request.
    503  1.2  thorpej 	 */
    504  1.2  thorpej 	npages = (length + 6) >> 8;
    505  1.2  thorpej 
    506  1.2  thorpej 	/*
    507  1.2  thorpej 	 * Now allocate the memory.
    508  1.2  thorpej 	 */
    509  1.2  thorpej 	SMC_SELECT_BANK(sc, 2);
    510  1.2  thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ALLOC | npages);
    511  1.2  thorpej 
    512  1.2  thorpej 	timo = MEMORY_WAIT_TIME;
    513  1.2  thorpej 	do {
    514  1.2  thorpej 		if (bus_space_read_1(bst, bsh, INTR_STAT_REG_B) & IM_ALLOC_INT)
    515  1.2  thorpej 			break;
    516  1.2  thorpej 		delay(1);
    517  1.2  thorpej 	} while (--timo);
    518  1.2  thorpej 
    519  1.2  thorpej 	packetno = bus_space_read_1(bst, bsh, ALLOC_RESULT_REG_B);
    520  1.2  thorpej 
    521  1.2  thorpej 	if (packetno & ARR_FAILED || timo == 0) {
    522  1.2  thorpej 		/*
    523  1.2  thorpej 		 * No transmit memory is available.  Record the number
    524  1.2  thorpej 		 * of requestd pages and enable the allocation completion
    525  1.2  thorpej 		 * interrupt.  Set up the watchdog timer in case we miss
    526  1.2  thorpej 		 * the interrupt.  Mark the interface as active so that
    527  1.2  thorpej 		 * no one else attempts to transmit while we're allocating
    528  1.2  thorpej 		 * memory.
    529  1.2  thorpej 		 */
    530  1.2  thorpej 		bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
    531  1.2  thorpej 		    bus_space_read_1(bst, bsh, INTR_MASK_REG_B) | IM_ALLOC_INT);
    532  1.2  thorpej 
    533  1.2  thorpej 		ifp->if_timer = 5;
    534  1.2  thorpej 		ifp->if_flags |= IFF_OACTIVE;
    535  1.2  thorpej 
    536  1.2  thorpej 		return;
    537  1.2  thorpej 	}
    538  1.2  thorpej 
    539  1.2  thorpej 	/*
    540  1.2  thorpej 	 * We have a packet number - set the data window.
    541  1.2  thorpej 	 */
    542  1.2  thorpej 	bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
    543  1.2  thorpej 
    544  1.2  thorpej 	/*
    545  1.2  thorpej 	 * Point to the beginning of the packet.
    546  1.2  thorpej 	 */
    547  1.2  thorpej 	bus_space_write_2(bst, bsh, POINTER_REG_W, PTR_AUTOINC /* | 0x0000 */);
    548  1.2  thorpej 
    549  1.2  thorpej 	/*
    550  1.2  thorpej 	 * Send the packet length (+6 for stats, length, and control bytes)
    551  1.2  thorpej 	 * and the status word (set to zeros).
    552  1.2  thorpej 	 */
    553  1.2  thorpej 	bus_space_write_2(bst, bsh, DATA_REG_W, 0);
    554  1.2  thorpej 	bus_space_write_1(bst, bsh, DATA_REG_B, (length + 6) & 0xff);
    555  1.2  thorpej 	bus_space_write_1(bst, bsh, DATA_REG_B, ((length + 6) >> 8) & 0xff);
    556  1.2  thorpej 
    557  1.2  thorpej 	/*
    558  1.2  thorpej 	 * Get the packet from the kernel.  This will include the Ethernet
    559  1.2  thorpej 	 * frame header, MAC address, etc.
    560  1.2  thorpej 	 */
    561  1.2  thorpej 	IF_DEQUEUE(&ifp->if_snd, m);
    562  1.2  thorpej 
    563  1.2  thorpej 	/*
    564  1.2  thorpej 	 * Push the packet out to the card.
    565  1.2  thorpej 	 */
    566  1.2  thorpej 	for (top = m; m != NULL; m = m->m_next) {
    567  1.2  thorpej 		/* Words... */
    568  1.2  thorpej 		bus_space_write_multi_2(bst, bsh, DATA_REG_W,
    569  1.2  thorpej 		    mtod(m, u_int16_t *), m->m_len >> 1);
    570  1.2  thorpej 
    571  1.2  thorpej 		/* ...and the remaining byte, if any. */
    572  1.2  thorpej 		if (m->m_len & 1)
    573  1.2  thorpej 			bus_space_write_1(bst, bsh, DATA_REG_B,
    574  1.2  thorpej 			  *(u_int8_t *)(mtod(m, u_int8_t *) + (m->m_len - 1)));
    575  1.2  thorpej 	}
    576  1.2  thorpej 
    577  1.2  thorpej #ifdef SMC91CXX_SW_PAD
    578  1.2  thorpej 	/*
    579  1.2  thorpej 	 * Push out padding.
    580  1.2  thorpej 	 */
    581  1.2  thorpej 	while (pad > 1) {
    582  1.2  thorpej 		bus_space_write_2(bst, bsh, DATA_REG_W, 0);
    583  1.2  thorpej 		pad -= 2;
    584  1.2  thorpej 	}
    585  1.2  thorpej 	if (pad)
    586  1.2  thorpej 		bus_space_write_1(bst, bsh, DATA_REG_B, 0);
    587  1.2  thorpej #endif
    588  1.2  thorpej 
    589  1.2  thorpej 	/*
    590  1.2  thorpej 	 * Push out control byte and unused packet byte.  The control byte
    591  1.2  thorpej 	 * is 0, meaning the packet is even lengthed and no special
    592  1.2  thorpej 	 * CRC handling is necessary.
    593  1.2  thorpej 	 */
    594  1.2  thorpej 	bus_space_write_2(bst, bsh, DATA_REG_W, 0);
    595  1.2  thorpej 
    596  1.2  thorpej 	/*
    597  1.2  thorpej 	 * Enable transmit interrupts and let the chip go.  Set a watchdog
    598  1.2  thorpej 	 * in case we miss the interrupt.
    599  1.2  thorpej 	 */
    600  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B,
    601  1.2  thorpej 	    bus_space_read_1(bst, bsh, INTR_MASK_REG_B) |
    602  1.2  thorpej 	    IM_TX_INT | IM_TX_EMPTY_INT);
    603  1.2  thorpej 
    604  1.2  thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_ENQUEUE);
    605  1.2  thorpej 
    606  1.2  thorpej 	ifp->if_timer = 5;
    607  1.2  thorpej 
    608  1.2  thorpej #if NBPFILTER > 0
    609  1.2  thorpej 	/* Hand off a copy to the bpf. */
    610  1.2  thorpej 	if (ifp->if_bpf)
    611  1.2  thorpej 		bpf_mtap(ifp->if_bpf, top);
    612  1.2  thorpej #endif
    613  1.2  thorpej 
    614  1.2  thorpej 	ifp->if_opackets++;
    615  1.2  thorpej 	m_freem(top);
    616  1.2  thorpej 
    617  1.2  thorpej  readcheck:
    618  1.2  thorpej 	/*
    619  1.2  thorpej 	 * Check for incoming pcakets.  We don't want to overflow the small
    620  1.2  thorpej 	 * RX FIFO.  If nothing has arrived, attempt to queue another
    621  1.2  thorpej 	 * transmit packet.
    622  1.2  thorpej 	 */
    623  1.2  thorpej 	if (bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) & FIFO_REMPTY)
    624  1.2  thorpej 		goto again;
    625  1.2  thorpej }
    626  1.2  thorpej 
    627  1.2  thorpej /*
    628  1.2  thorpej  * Interrupt service routine.
    629  1.2  thorpej  */
    630  1.2  thorpej int
    631  1.2  thorpej smc91cxx_intr(arg)
    632  1.2  thorpej 	void *arg;
    633  1.2  thorpej {
    634  1.2  thorpej 	struct smc91cxx_softc *sc = arg;
    635  1.2  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    636  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    637  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    638  1.2  thorpej 	u_int8_t mask, interrupts, status;
    639  1.2  thorpej 	u_int16_t packetno, tx_status, card_stats;
    640  1.2  thorpej 
    641  1.2  thorpej 	SMC_SELECT_BANK(sc, 2);
    642  1.2  thorpej 
    643  1.2  thorpej 	/*
    644  1.2  thorpej 	 * Obtain the current interrupt mask.
    645  1.2  thorpej 	 */
    646  1.2  thorpej 	mask = bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
    647  1.2  thorpej 
    648  1.2  thorpej 	/*
    649  1.2  thorpej 	 * Get the set of interrupt which occurred and eliminate any
    650  1.2  thorpej 	 * which are not enabled.
    651  1.2  thorpej 	 */
    652  1.2  thorpej 	interrupts = bus_space_read_1(bst, bsh, INTR_STAT_REG_B);
    653  1.2  thorpej 	status = interrupts & mask;
    654  1.2  thorpej 
    655  1.2  thorpej 	/* Ours? */
    656  1.2  thorpej 	if (status == 0)
    657  1.2  thorpej 		return (0);
    658  1.2  thorpej 
    659  1.2  thorpej 	/*
    660  1.2  thorpej 	 * It's ours; disable all interrupts while we process them.
    661  1.2  thorpej 	 */
    662  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
    663  1.2  thorpej 
    664  1.2  thorpej 	/*
    665  1.2  thorpej 	 * Receive overrun interrupts.
    666  1.2  thorpej 	 */
    667  1.2  thorpej 	if (status & IM_RX_OVRN_INT) {
    668  1.2  thorpej 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_RX_OVRN_INT);
    669  1.2  thorpej 		ifp->if_ierrors++;
    670  1.2  thorpej 	}
    671  1.2  thorpej 
    672  1.2  thorpej 	/*
    673  1.2  thorpej 	 * Receive interrupts.
    674  1.2  thorpej 	 */
    675  1.2  thorpej 	if (status & IM_RCV_INT) {
    676  1.2  thorpej #if 1 /* DIAGNOSTIC */
    677  1.2  thorpej 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
    678  1.2  thorpej 		if (packetno & FIFO_REMPTY)
    679  1.2  thorpej 			printf("%s: receive interrupt on empty fifo\n",
    680  1.2  thorpej 			    sc->sc_dev.dv_xname);
    681  1.2  thorpej 		else
    682  1.2  thorpej #endif
    683  1.2  thorpej 		smc91cxx_read(sc);
    684  1.2  thorpej 	}
    685  1.2  thorpej 
    686  1.2  thorpej 	/*
    687  1.2  thorpej 	 * Memory allocation interrupts.
    688  1.2  thorpej 	 */
    689  1.2  thorpej 	if (status & IM_ALLOC_INT) {
    690  1.2  thorpej 		/* Disable this interrupt. */
    691  1.2  thorpej 		mask &= ~IM_ALLOC_INT;
    692  1.2  thorpej 
    693  1.2  thorpej 		/*
    694  1.2  thorpej 		 * Release the just-allocated memory.  We will reallocate
    695  1.2  thorpej 		 * it through the normal start logic.
    696  1.2  thorpej 		 */
    697  1.2  thorpej 		while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
    698  1.2  thorpej 			/* XXX bound this loop! */ ;
    699  1.2  thorpej 		bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
    700  1.2  thorpej 
    701  1.2  thorpej 		ifp->if_flags &= ~IFF_OACTIVE;
    702  1.2  thorpej 		ifp->if_timer = 0;
    703  1.2  thorpej 	}
    704  1.2  thorpej 
    705  1.2  thorpej 	/*
    706  1.2  thorpej 	 * Transmit complete interrupt.  Handle transmission error messages.
    707  1.2  thorpej 	 * This will only be called on error condition because of AUTO RELEASE
    708  1.2  thorpej 	 * mode.
    709  1.2  thorpej 	 */
    710  1.2  thorpej 	if (status & IM_TX_INT) {
    711  1.2  thorpej 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_INT);
    712  1.2  thorpej 
    713  1.2  thorpej 		packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W) &
    714  1.2  thorpej 		    FIFO_TX_MASK;
    715  1.2  thorpej 
    716  1.2  thorpej 		/*
    717  1.2  thorpej 		 * Select this as the packet to read from.
    718  1.2  thorpej 		 */
    719  1.2  thorpej 		bus_space_write_1(bst, bsh, PACKET_NUM_REG_B, packetno);
    720  1.2  thorpej 
    721  1.2  thorpej 		/*
    722  1.2  thorpej 		 * Position the pointer to the beginning of the packet.
    723  1.2  thorpej 		 */
    724  1.2  thorpej 		bus_space_write_2(bst, bsh, POINTER_REG_W,
    725  1.2  thorpej 		    PTR_AUTOINC | PTR_READ /* | 0x0000 */);
    726  1.2  thorpej 
    727  1.2  thorpej 		/*
    728  1.2  thorpej 		 * Fetch the TX status word.  This will be a copy of
    729  1.2  thorpej 		 * the EPH_STATUS_REG_W at the time of the transmission
    730  1.2  thorpej 		 * failure.
    731  1.2  thorpej 		 */
    732  1.2  thorpej 		tx_status = bus_space_read_2(bst, bsh, DATA_REG_W);
    733  1.2  thorpej 
    734  1.2  thorpej 		if (tx_status & EPHSR_TX_SUC)
    735  1.2  thorpej 			printf("%s: successful packet caused TX interrupt?!\n",
    736  1.2  thorpej 			    sc->sc_dev.dv_xname);
    737  1.2  thorpej 		else
    738  1.2  thorpej 			ifp->if_oerrors++;
    739  1.2  thorpej 
    740  1.2  thorpej 		if (tx_status & EPHSR_LATCOL)
    741  1.2  thorpej 			ifp->if_collisions++;
    742  1.2  thorpej 
    743  1.2  thorpej 		/*
    744  1.2  thorpej 		 * Some of these errors disable the transmitter; reenable it.
    745  1.2  thorpej 		 */
    746  1.2  thorpej 		SMC_SELECT_BANK(sc, 0);
    747  1.2  thorpej #ifdef SMC91CXX_SW_PAD
    748  1.2  thorpej 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, TCR_ENABLE);
    749  1.2  thorpej #else
    750  1.2  thorpej 		bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W,
    751  1.2  thorpej 		    TCR_ENABLE | TCR_PAD_ENABLE);
    752  1.2  thorpej #endif
    753  1.2  thorpej 
    754  1.2  thorpej 		/*
    755  1.2  thorpej 		 * Kill the failed packet and wait for the MMU to unbusy.
    756  1.2  thorpej 		 */
    757  1.2  thorpej 		SMC_SELECT_BANK(sc, 2);
    758  1.2  thorpej 		while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
    759  1.2  thorpej 			/* XXX bound this loop! */ ;
    760  1.2  thorpej 		bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_FREEPKT);
    761  1.2  thorpej 
    762  1.2  thorpej 		ifp->if_timer = 0;
    763  1.2  thorpej 	}
    764  1.2  thorpej 
    765  1.2  thorpej 	/*
    766  1.2  thorpej 	 * Transmit underrun interrupts.  We use this opportunity to
    767  1.2  thorpej 	 * update transmit statistics from the card.
    768  1.2  thorpej 	 */
    769  1.2  thorpej 	if (status & IM_TX_EMPTY_INT) {
    770  1.2  thorpej 		bus_space_write_1(bst, bsh, INTR_ACK_REG_B, IM_TX_EMPTY_INT);
    771  1.2  thorpej 
    772  1.2  thorpej 		/* Disable this interrupt. */
    773  1.2  thorpej 		mask &= ~IM_TX_EMPTY_INT;
    774  1.2  thorpej 
    775  1.2  thorpej 		SMC_SELECT_BANK(sc, 0);
    776  1.2  thorpej 		card_stats = bus_space_read_2(bst, bsh, COUNTER_REG_W);
    777  1.2  thorpej 
    778  1.2  thorpej 		/* Single collisions. */
    779  1.2  thorpej 		ifp->if_collisions += card_stats & ECR_COLN_MASK;
    780  1.2  thorpej 
    781  1.2  thorpej 		/* Multiple collisions. */
    782  1.2  thorpej 		ifp->if_collisions += (card_stats & ECR_MCOLN_MASK) >> 4;
    783  1.2  thorpej 
    784  1.2  thorpej 		SMC_SELECT_BANK(sc, 2);
    785  1.2  thorpej 
    786  1.2  thorpej 		ifp->if_timer = 0;
    787  1.2  thorpej 	}
    788  1.2  thorpej 
    789  1.2  thorpej 	/*
    790  1.2  thorpej 	 * Other errors.  Reset the interface.
    791  1.2  thorpej 	 */
    792  1.2  thorpej 	if (status & IM_EPH_INT) {
    793  1.2  thorpej 		smc91cxx_stop(sc);
    794  1.2  thorpej 		smc91cxx_init(sc);
    795  1.2  thorpej 	}
    796  1.2  thorpej 
    797  1.2  thorpej 	/*
    798  1.2  thorpej 	 * Attempt to queue more packets for transmission.
    799  1.2  thorpej 	 */
    800  1.2  thorpej 	smc91cxx_start(ifp);
    801  1.2  thorpej 
    802  1.2  thorpej 	/*
    803  1.2  thorpej 	 * Reenable the interrupts we wish to receive now that processing
    804  1.2  thorpej 	 * is complete.
    805  1.2  thorpej 	 */
    806  1.2  thorpej 	mask |= bus_space_read_1(bst, bsh, INTR_MASK_REG_B);
    807  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, mask);
    808  1.2  thorpej 
    809  1.2  thorpej 	return (1);
    810  1.2  thorpej }
    811  1.2  thorpej 
    812  1.2  thorpej /*
    813  1.2  thorpej  * Read a packet from the card and pass it up to the kernel.
    814  1.2  thorpej  * NOTE!  WE EXPECT TO BE IN REGISTER WINDOW 2!
    815  1.2  thorpej  */
    816  1.2  thorpej void
    817  1.2  thorpej smc91cxx_read(sc)
    818  1.2  thorpej 	struct smc91cxx_softc *sc;
    819  1.2  thorpej {
    820  1.2  thorpej 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    821  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
    822  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
    823  1.2  thorpej 	struct ether_header *eh;
    824  1.2  thorpej 	struct mbuf *m;
    825  1.2  thorpej 	u_int16_t status, packetno, packetlen;
    826  1.2  thorpej 	u_int8_t *data;
    827  1.2  thorpej 
    828  1.2  thorpej  again:
    829  1.2  thorpej 	/*
    830  1.2  thorpej 	 * Set data pointer to the beginning of the packet.  Since
    831  1.2  thorpej 	 * PTR_RCV is set, the packet number will be found automatically
    832  1.2  thorpej 	 * in FIFO_PORTS_REG_W, FIFO_RX_MASK.
    833  1.2  thorpej 	 */
    834  1.2  thorpej 	bus_space_write_2(bst, bsh, POINTER_REG_W,
    835  1.2  thorpej 	    PTR_READ | PTR_RCV | PTR_AUTOINC /* | 0x0000 */);
    836  1.2  thorpej 
    837  1.2  thorpej 	/*
    838  1.2  thorpej 	 * First two words are status and packet length.
    839  1.2  thorpej 	 */
    840  1.2  thorpej 	status = bus_space_read_2(bst, bsh, DATA_REG_W);
    841  1.2  thorpej 	packetlen = bus_space_read_2(bst, bsh, DATA_REG_W);
    842  1.2  thorpej 
    843  1.2  thorpej 	/*
    844  1.2  thorpej 	 * The packet length includes 3 extra words: status, length,
    845  1.2  thorpej 	 * and an extra word that includes the control byte.
    846  1.2  thorpej 	 */
    847  1.2  thorpej 	packetlen -= 6;
    848  1.2  thorpej 
    849  1.2  thorpej 	/*
    850  1.2  thorpej 	 * Account for receive errors and discard.
    851  1.2  thorpej 	 */
    852  1.2  thorpej 	if (status & RS_ERRORS) {
    853  1.2  thorpej 		ifp->if_ierrors++;
    854  1.2  thorpej 		goto out;
    855  1.2  thorpej 	}
    856  1.2  thorpej 
    857  1.2  thorpej 	/*
    858  1.2  thorpej 	 * Adjust for odd-length packet.
    859  1.2  thorpej 	 */
    860  1.2  thorpej 	if (status & RS_ODDFRAME)
    861  1.2  thorpej 		packetlen++;
    862  1.2  thorpej 
    863  1.2  thorpej 	/*
    864  1.2  thorpej 	 * Allocate a header mbuf.
    865  1.2  thorpej 	 */
    866  1.2  thorpej 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    867  1.2  thorpej 	if (m == NULL)
    868  1.2  thorpej 		goto out;
    869  1.2  thorpej 
    870  1.2  thorpej 	m->m_pkthdr.rcvif = ifp;
    871  1.2  thorpej 	m->m_pkthdr.len = m->m_len = packetlen;
    872  1.2  thorpej 
    873  1.2  thorpej 	/*
    874  1.2  thorpej 	 * Always put the packet in a cluster.
    875  1.2  thorpej 	 * XXX should chain small mbufs if less than threshold.
    876  1.2  thorpej 	 */
    877  1.2  thorpej 	MCLGET(m, M_DONTWAIT);
    878  1.2  thorpej 	if ((m->m_flags & M_EXT) == 0) {
    879  1.2  thorpej 		m_freem(m);
    880  1.2  thorpej 		ifp->if_ierrors++;
    881  1.2  thorpej 		printf("%s: can't allocate cluster for incoming packet\n",
    882  1.2  thorpej 		    sc->sc_dev.dv_xname);
    883  1.2  thorpej 		goto out;
    884  1.2  thorpej 	}
    885  1.2  thorpej 
    886  1.2  thorpej 	/*
    887  1.2  thorpej 	 * Pull the packet off the interface.
    888  1.2  thorpej 	 */
    889  1.2  thorpej 	eh = mtod(m, struct ether_header *);
    890  1.2  thorpej 	data = mtod(m, u_int8_t *);
    891  1.2  thorpej 	bus_space_read_multi_2(bst, bsh, DATA_REG_W, (u_int16_t *)data,
    892  1.2  thorpej 	    packetlen >> 1);
    893  1.2  thorpej 	if (packetlen & 1) {
    894  1.2  thorpej 		data += packetlen & ~1;
    895  1.2  thorpej 		*data = bus_space_read_1(bst, bsh, DATA_REG_B);
    896  1.2  thorpej 	}
    897  1.2  thorpej 
    898  1.2  thorpej 	ifp->if_ipackets++;
    899  1.2  thorpej 
    900  1.2  thorpej #if NBPFILTER > 0
    901  1.2  thorpej 	/*
    902  1.2  thorpej 	 * Hand the packet off to bpf listeners.  If there's a bpf listener,
    903  1.2  thorpej 	 * we need to check if the packet is ours.
    904  1.2  thorpej 	 */
    905  1.2  thorpej 	if (ifp->if_bpf) {
    906  1.2  thorpej 		bpf_mtap(ifp->if_bpf, m);
    907  1.2  thorpej 
    908  1.2  thorpej 		if ((ifp->if_flags & IFF_PROMISC) &&
    909  1.2  thorpej 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
    910  1.2  thorpej 		    ether_cmp(eh->ether_dhost, LLADDR(ifp->if_sadl))) {
    911  1.2  thorpej 			m_freem(m);
    912  1.2  thorpej 			goto out;
    913  1.2  thorpej 		}
    914  1.2  thorpej 	}
    915  1.2  thorpej #endif
    916  1.2  thorpej 
    917  1.2  thorpej 	/*
    918  1.2  thorpej 	 * Strip the ethernet header.
    919  1.2  thorpej 	 */
    920  1.2  thorpej 	m->m_pkthdr.len = m->m_len = packetlen - sizeof(struct ether_header);
    921  1.2  thorpej 	m->m_data += sizeof(struct ether_header);
    922  1.2  thorpej 
    923  1.2  thorpej 	ether_input(ifp, eh, m);
    924  1.2  thorpej 
    925  1.2  thorpej  out:
    926  1.2  thorpej 	/*
    927  1.2  thorpej 	 * Tell the card to free the memory occupied by this packet.
    928  1.2  thorpej 	 */
    929  1.2  thorpej 	while (bus_space_read_2(bst, bsh, MMU_CMD_REG_W) & MMUCR_BUSY)
    930  1.2  thorpej 		/* XXX bound this loop! */ ;
    931  1.2  thorpej 	bus_space_write_2(bst, bsh, MMU_CMD_REG_W, MMUCR_RELEASE);
    932  1.2  thorpej 
    933  1.2  thorpej 	/*
    934  1.2  thorpej 	 * Check for another packet.
    935  1.2  thorpej 	 */
    936  1.2  thorpej 	packetno = bus_space_read_2(bst, bsh, FIFO_PORTS_REG_W);
    937  1.2  thorpej 	if (packetno & FIFO_REMPTY)
    938  1.2  thorpej 		return;
    939  1.2  thorpej 	goto again;
    940  1.2  thorpej }
    941  1.2  thorpej 
    942  1.2  thorpej /*
    943  1.2  thorpej  * Process an ioctl request.
    944  1.2  thorpej  */
    945  1.2  thorpej int
    946  1.2  thorpej smc91cxx_ioctl(ifp, cmd, data)
    947  1.2  thorpej 	struct ifnet *ifp;
    948  1.2  thorpej 	u_long cmd;
    949  1.2  thorpej 	caddr_t data;
    950  1.2  thorpej {
    951  1.2  thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
    952  1.2  thorpej 	struct ifaddr *ifa = (struct ifaddr *)data;
    953  1.2  thorpej 	struct ifreq *ifr = (struct ifreq *)data;
    954  1.2  thorpej 	int s, error = 0;
    955  1.2  thorpej 
    956  1.2  thorpej 	s = splimp();
    957  1.2  thorpej 
    958  1.2  thorpej 	switch (cmd) {
    959  1.2  thorpej 	case SIOCSIFADDR:
    960  1.2  thorpej 		ifp->if_flags |= IFF_UP;
    961  1.2  thorpej 		switch (ifa->ifa_addr->sa_family) {
    962  1.2  thorpej #ifdef INET
    963  1.2  thorpej 		case AF_INET:
    964  1.2  thorpej 		smc91cxx_init(sc);
    965  1.2  thorpej 		arp_ifinit(ifp, ifa);
    966  1.2  thorpej 		break;
    967  1.2  thorpej #endif
    968  1.2  thorpej #ifdef NS
    969  1.2  thorpej 		case AF_NS:
    970  1.2  thorpej 		    {
    971  1.2  thorpej 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
    972  1.2  thorpej 
    973  1.2  thorpej 			if (ns_nullhost(*ina))
    974  1.2  thorpej 				ina->x_host =
    975  1.2  thorpej 				    *(union ns_host *)LLADDR(ifp->if_sadl);
    976  1.2  thorpej 			else {
    977  1.2  thorpej 				bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
    978  1.2  thorpej 				    ETHER_ADDR_LEN);
    979  1.2  thorpej 			}
    980  1.2  thorpej 
    981  1.2  thorpej 			/*
    982  1.2  thorpej 			 * Set new address.  Reset, because the receiver
    983  1.2  thorpej 			 * has to be stopped before we can set the new
    984  1.2  thorpej 			 * MAC address.
    985  1.2  thorpej 			 */
    986  1.2  thorpej 			smc91cxx_reset(sc);
    987  1.2  thorpej 			break;
    988  1.2  thorpej 		    }
    989  1.2  thorpej #endif
    990  1.2  thorpej 		default:
    991  1.2  thorpej 			smc91cxx_init(sc);
    992  1.2  thorpej 			break;
    993  1.2  thorpej 		}
    994  1.2  thorpej 		break;
    995  1.2  thorpej 
    996  1.2  thorpej #if defined(CCITT) && defined(LLC)
    997  1.2  thorpej 	case SIOCSIFCONF_X25:
    998  1.2  thorpej 		ifp->if_flags |= IFF_UP;
    999  1.2  thorpej 		ifa->ifa_rtrequest = cons_rtrequest;	/* XXX */
   1000  1.2  thorpej 		error = x25_llcglue(PRC_IFUP, ifa->ifa_addr);
   1001  1.2  thorpej 		if (error == 0)
   1002  1.2  thorpej 			smc91cxx_init(sc);
   1003  1.2  thorpej 		break;
   1004  1.2  thorpej #endif
   1005  1.2  thorpej 
   1006  1.2  thorpej 	case SIOCSIFFLAGS:
   1007  1.2  thorpej 		if ((ifp->if_flags & IFF_UP) == 0 &&
   1008  1.2  thorpej 		    (ifp->if_flags & IFF_RUNNING) != 0) {
   1009  1.2  thorpej 			/*
   1010  1.2  thorpej 			 * If interface is marked down and it is running,
   1011  1.2  thorpej 			 * stop it.
   1012  1.2  thorpej 			 */
   1013  1.2  thorpej 			smc91cxx_stop(sc);
   1014  1.2  thorpej 			ifp->if_flags &= ~IFF_RUNNING;
   1015  1.2  thorpej 		} else if ((ifp->if_flags & IFF_UP) != 0 &&
   1016  1.2  thorpej 			   (ifp->if_flags & IFF_RUNNING) == 0) {
   1017  1.2  thorpej 			/*
   1018  1.2  thorpej 			 * If interface is marked up and it is stopped,
   1019  1.2  thorpej 			 * start it.
   1020  1.2  thorpej 			 */
   1021  1.2  thorpej 			smc91cxx_init(sc);
   1022  1.2  thorpej 		} else {
   1023  1.2  thorpej 			/*
   1024  1.2  thorpej 			 * Reset the interface to pick up changes in any
   1025  1.2  thorpej 			 * other flags that affect hardware registers.
   1026  1.2  thorpej 			 */
   1027  1.2  thorpej 			smc91cxx_reset(sc);
   1028  1.2  thorpej 		}
   1029  1.2  thorpej 		break;
   1030  1.2  thorpej 
   1031  1.2  thorpej 	case SIOCADDMULTI:
   1032  1.2  thorpej 	case SIOCDELMULTI:
   1033  1.2  thorpej 		error = (cmd == SIOCADDMULTI) ?
   1034  1.2  thorpej 		    ether_addmulti(ifr, &sc->sc_ec) :
   1035  1.2  thorpej 		    ether_delmulti(ifr, &sc->sc_ec);
   1036  1.2  thorpej 		if (error == ENETRESET) {
   1037  1.2  thorpej 			/*
   1038  1.2  thorpej 			 * Multicast list has changed; set the hardware
   1039  1.2  thorpej 			 * filter accordingly.
   1040  1.2  thorpej 			 */
   1041  1.2  thorpej 			smc91cxx_reset(sc);
   1042  1.2  thorpej 			error = 0;
   1043  1.2  thorpej 		}
   1044  1.2  thorpej 		break;
   1045  1.2  thorpej 
   1046  1.2  thorpej 	case SIOCGIFMEDIA:
   1047  1.2  thorpej 	case SIOCSIFMEDIA:
   1048  1.2  thorpej 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
   1049  1.2  thorpej 		break;
   1050  1.2  thorpej 
   1051  1.2  thorpej 	default:
   1052  1.2  thorpej 		error = EINVAL;
   1053  1.2  thorpej 		break;
   1054  1.2  thorpej 	}
   1055  1.2  thorpej 
   1056  1.2  thorpej 	splx(s);
   1057  1.2  thorpej 	return (error);
   1058  1.2  thorpej }
   1059  1.2  thorpej 
   1060  1.2  thorpej /*
   1061  1.2  thorpej  * Reset the interface.
   1062  1.2  thorpej  */
   1063  1.2  thorpej void
   1064  1.2  thorpej smc91cxx_reset(sc)
   1065  1.2  thorpej 	struct smc91cxx_softc *sc;
   1066  1.2  thorpej {
   1067  1.2  thorpej 	int s;
   1068  1.2  thorpej 
   1069  1.2  thorpej 	s = splimp();
   1070  1.2  thorpej 	smc91cxx_stop(sc);
   1071  1.2  thorpej 	smc91cxx_init(sc);
   1072  1.2  thorpej 	splx(s);
   1073  1.2  thorpej }
   1074  1.2  thorpej 
   1075  1.2  thorpej /*
   1076  1.2  thorpej  * Watchdog timer.
   1077  1.2  thorpej  */
   1078  1.2  thorpej void
   1079  1.2  thorpej smc91cxx_watchdog(ifp)
   1080  1.2  thorpej 	struct ifnet *ifp;
   1081  1.2  thorpej {
   1082  1.2  thorpej 	struct smc91cxx_softc *sc = ifp->if_softc;
   1083  1.2  thorpej 
   1084  1.2  thorpej 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
   1085  1.2  thorpej 	ifp->if_oerrors++;
   1086  1.2  thorpej 	smc91cxx_reset(sc);
   1087  1.2  thorpej }
   1088  1.2  thorpej 
   1089  1.2  thorpej /*
   1090  1.2  thorpej  * Stop output on the interface.
   1091  1.2  thorpej  */
   1092  1.2  thorpej void
   1093  1.2  thorpej smc91cxx_stop(sc)
   1094  1.2  thorpej 	struct smc91cxx_softc *sc;
   1095  1.2  thorpej {
   1096  1.2  thorpej 	bus_space_tag_t bst = sc->sc_bst;
   1097  1.2  thorpej 	bus_space_handle_t bsh = sc->sc_bsh;
   1098  1.2  thorpej 
   1099  1.2  thorpej 	/*
   1100  1.2  thorpej 	 * Clear interrupt mask; disable all interrupts.
   1101  1.2  thorpej 	 */
   1102  1.2  thorpej 	SMC_SELECT_BANK(sc, 2);
   1103  1.2  thorpej 	bus_space_write_1(bst, bsh, INTR_MASK_REG_B, 0);
   1104  1.2  thorpej 
   1105  1.2  thorpej 	/*
   1106  1.2  thorpej 	 * Disable transmitter and receiver.
   1107  1.2  thorpej 	 */
   1108  1.2  thorpej 	SMC_SELECT_BANK(sc, 0);
   1109  1.2  thorpej 	bus_space_write_2(bst, bsh, RECV_CONTROL_REG_W, 0);
   1110  1.2  thorpej 	bus_space_write_2(bst, bsh, TXMIT_CONTROL_REG_W, 0);
   1111  1.2  thorpej 
   1112  1.2  thorpej 	/*
   1113  1.2  thorpej 	 * Cancel watchdog timer.
   1114  1.2  thorpej 	 */
   1115  1.2  thorpej 	sc->sc_ec.ec_if.if_timer = 0;
   1116  1.2  thorpej }
   1117