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