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