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