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