Home | History | Annotate | Line # | Download | only in ic
lan9118.c revision 1.29
      1 /*	$NetBSD: lan9118.c,v 1.29 2019/01/22 15:34:04 jmcneill Exp $	*/
      2 /*
      3  * Copyright (c) 2008 KIYOHARA Takashi
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  * POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 #include <sys/cdefs.h>
     28 __KERNEL_RCSID(0, "$NetBSD: lan9118.c,v 1.29 2019/01/22 15:34:04 jmcneill Exp $");
     29 
     30 /*
     31  * The LAN9118 Family
     32  * * The LAN9118 is targeted for 32-bit applications requiring high
     33  *   performance, and provides the highest level of performance possible for
     34  *   a non-PCI 10/100 Ethernet controller.
     35  *
     36  * * The LAN9117 is designed to provide the highest level of performance
     37  *   possible for 16-bit applications. It also has an external MII interface,
     38  *   which can be used to attach an external PHY.
     39  *
     40  * * The LAN9116 and LAN9115 are designed for performance-sensitive
     41  *   applications with less intensive performance requirements. The LAN9116
     42  *   is for 32-bit host processors, while the LAN9115 is for 16-bit
     43  *   applications, which may also require an external PHY. Both devices
     44  *   deliver superior levels of performance.
     45  *
     46  * The LAN9218 Family
     47  *   Also support HP Auto-MDIX.
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/callout.h>
     52 #include <sys/device.h>
     53 #include <sys/errno.h>
     54 #include <sys/bus.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/proc.h>
     58 #include <sys/systm.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_ether.h>
     62 #include <net/if_media.h>
     63 
     64 #include <dev/mii/mii.h>
     65 #include <dev/mii/miivar.h>
     66 
     67 #include <net/bpf.h>
     68 #include <sys/rndsource.h>
     69 
     70 #include <dev/ic/lan9118reg.h>
     71 #include <dev/ic/lan9118var.h>
     72 
     73 
     74 #ifdef SMSH_DEBUG
     75 #define DPRINTF(x)	if (smsh_debug) printf x
     76 #define DPRINTFN(n,x)	if (smsh_debug >= (n)) printf x
     77 int smsh_debug = SMSH_DEBUG;
     78 #else
     79 #define DPRINTF(x)
     80 #define DPRINTFN(n,x)
     81 #endif
     82 
     83 
     84 static void lan9118_start(struct ifnet *);
     85 static int lan9118_ioctl(struct ifnet *, u_long, void *);
     86 static int lan9118_init(struct ifnet *);
     87 static void lan9118_stop(struct ifnet *, int);
     88 static void lan9118_watchdog(struct ifnet *);
     89 
     90 static int lan9118_ifm_change(struct ifnet *);
     91 static void lan9118_ifm_status(struct ifnet *, struct ifmediareq *);
     92 
     93 static int lan9118_miibus_readreg(device_t, int, int, uint16_t *);
     94 static int lan9118_miibus_writereg(device_t, int, int, uint16_t);
     95 static void lan9118_miibus_statchg(struct ifnet *);
     96 
     97 static int lan9118_mii_readreg(struct lan9118_softc *, int, int, uint16_t *);
     98 static int lan9118_mii_writereg(struct lan9118_softc *, int, int, uint16_t);
     99 static uint32_t lan9118_mac_readreg(struct lan9118_softc *, int);
    100 static void lan9118_mac_writereg(struct lan9118_softc *, int, uint32_t);
    101 
    102 static void lan9118_set_filter(struct lan9118_softc *);
    103 static void lan9118_rxintr(struct lan9118_softc *);
    104 static void lan9118_txintr(struct lan9118_softc *);
    105 
    106 static void lan9118_tick(void *);
    107 
    108 /* This values refer from Linux's smc911x.c */
    109 static uint32_t afc_cfg[] = {
    110 	/* 0 */ 0x00000000,
    111 	/* 1 */ 0x00000000,
    112 	/* 2 */ 0x008c4600 | LAN9118_AFC_CFG_BACK_DUR(10) |
    113 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    114 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    115 	/* 3 */ 0x00824100 | LAN9118_AFC_CFG_BACK_DUR(9) |
    116 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    117 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    118 	/* 4 */ 0x00783c00 | LAN9118_AFC_CFG_BACK_DUR(9) |
    119 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    120 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    121 	/* 5 */ 0x006e3700 | LAN9118_AFC_CFG_BACK_DUR(8) |
    122 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    123 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    124 	/* 6 */ 0x00643200 | LAN9118_AFC_CFG_BACK_DUR(8) |
    125 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    126 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    127 	/* 7 */ 0x005a2d00 | LAN9118_AFC_CFG_BACK_DUR(7) |
    128 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    129 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    130 	/* 8 */ 0x00502800 | LAN9118_AFC_CFG_BACK_DUR(7) |
    131 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    132 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    133 	/* 9 */ 0x00462300 | LAN9118_AFC_CFG_BACK_DUR(6) |
    134 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    135 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    136 	/* a */ 0x003c1e00 | LAN9118_AFC_CFG_BACK_DUR(6) |
    137 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    138 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    139 	/* b */ 0x00321900 | LAN9118_AFC_CFG_BACK_DUR(5) |
    140 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    141 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    142 	/* c */ 0x00241200 | LAN9118_AFC_CFG_BACK_DUR(4) |
    143 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    144 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    145 	/* d */ 0x00150700 | LAN9118_AFC_CFG_BACK_DUR(3) |
    146 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    147 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    148 	/* e */ 0x00060300 | LAN9118_AFC_CFG_BACK_DUR(2) |
    149 		LAN9118_AFC_CFG_FCMULT | LAN9118_AFC_CFG_FCBRD |
    150 		LAN9118_AFC_CFG_FCADD | LAN9118_AFC_CFG_FCANY,
    151 	/* f */ 0x00000000,
    152 };
    153 
    154 
    155 int
    156 lan9118_attach(struct lan9118_softc *sc)
    157 {
    158 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    159 	uint32_t val, irq_cfg;
    160 	int timo, i;
    161 
    162 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    163 		/* byte swap mode */
    164 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    165 		    0xffffffff);
    166 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST);
    167 	if (val != LAN9118_BYTE_TEST_VALUE) {
    168 		aprint_error(": failed to detect chip\n");
    169 		return EINVAL;
    170 	}
    171 
    172 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_ID_REV);
    173 	sc->sc_id = LAN9118_ID_REV_ID(val);
    174 	sc->sc_rev = LAN9118_ID_REV_REV(val);
    175 
    176 #define LAN9xxx_ID(id)	\
    177  (IS_LAN9118(id) ? (id) : (IS_LAN9218(id) ? ((id) >> 4) + 0x100 : (id) & 0xfff))
    178 
    179 	aprint_normal(": SMSC LAN9%03x Rev %d\n",
    180 	    LAN9xxx_ID(sc->sc_id), sc->sc_rev);
    181 
    182 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    183 		aprint_normal_dev(sc->sc_dev, "byte swap mode\n");
    184 
    185 	timo = 3 * 1000 * 1000;	/* XXXX 3sec */
    186 	do {
    187 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    188 		    LAN9118_MAC_CSR_CMD);
    189 		if (!(val & LAN9118_MAC_CSR_CMD_BUSY))
    190 			break;
    191 		delay(100);
    192 	} while (timo -= 100);
    193 	if (timo <= 0)
    194 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    195 	if (!(sc->sc_flags & LAN9118_FLAGS_NO_EEPROM)) {
    196 		/* Read auto-loaded MAC address */
    197 		val = lan9118_mac_readreg(sc, LAN9118_ADDRL);
    198 		sc->sc_enaddr[3] = (val >> 24) & 0xff;
    199 		sc->sc_enaddr[2] = (val >> 16) & 0xff;
    200 		sc->sc_enaddr[1] = (val >> 8) & 0xff;
    201 		sc->sc_enaddr[0] = val & 0xff;
    202 		val = lan9118_mac_readreg(sc, LAN9118_ADDRH);
    203 		sc->sc_enaddr[5] = (val >> 8) & 0xff;
    204 		sc->sc_enaddr[4] = val & 0xff;
    205 	}
    206 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
    207 	    ether_sprintf(sc->sc_enaddr));
    208 
    209 	/* Set IRQ config */
    210 	irq_cfg = 0;
    211 	if (sc->sc_flags & LAN9118_FLAGS_IRQ_ACTHI)
    212 		irq_cfg |= LAN9118_IRQ_CFG_IRQ_POL;
    213 	if (sc->sc_flags & LAN9118_FLAGS_IRQ_PP)
    214 		irq_cfg |= LAN9118_IRQ_CFG_IRQ_TYPE;
    215 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG, irq_cfg);
    216 
    217 	KASSERT(LAN9118_TX_FIF_SZ >= 2 && LAN9118_TX_FIF_SZ < 15);
    218 	sc->sc_afc_cfg = afc_cfg[LAN9118_TX_FIF_SZ];
    219 
    220 	/* Initialize the ifnet structure. */
    221 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    222 	ifp->if_softc = sc;
    223 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    224 	ifp->if_start = lan9118_start;
    225 	ifp->if_ioctl = lan9118_ioctl;
    226 	ifp->if_init = lan9118_init;
    227 	ifp->if_stop = lan9118_stop;
    228 	ifp->if_watchdog = lan9118_watchdog;
    229 	IFQ_SET_READY(&ifp->if_snd);
    230 
    231 #if 0	/* Not support 802.1Q VLAN-sized frames yet. */
    232 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    233 #endif
    234 
    235 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    236 	    lan9118_ifm_change, lan9118_ifm_status);
    237 	sc->sc_mii.mii_ifp = ifp;
    238 	sc->sc_mii.mii_readreg = lan9118_miibus_readreg;
    239 	sc->sc_mii.mii_writereg = lan9118_miibus_writereg;
    240 	sc->sc_mii.mii_statchg = lan9118_miibus_statchg;
    241 
    242 	/*
    243 	 * Number of instance of Internal PHY is always 0.  External PHY
    244 	 * number that above.
    245 	 */
    246 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
    247 
    248 	if (sc->sc_id == LAN9118_ID_9115 || sc->sc_id == LAN9118_ID_9117 ||
    249 	    sc->sc_id == LAN9218_ID_9215 || sc->sc_id == LAN9218_ID_9217) {
    250 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG) &
    251 		    LAN9118_HW_CFG_EXT_PHY_DET) {
    252 			/*
    253 			 * We always have a internal PHY at phy1.
    254 			 * In addition, external PHY is attached.
    255 			 */
    256 			DPRINTFN(1, ("%s: detect External PHY\n", __func__));
    257 
    258 			/* Switch MII and SMI */
    259 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    260 			    LAN9118_HW_CFG,
    261 			    LAN9118_HW_CFG_MBO |
    262 			    LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    263 			delay(1);	/* Wait 5 cycle */
    264 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    265 			    LAN9118_HW_CFG,
    266 			    LAN9118_HW_CFG_MBO |
    267 			    LAN9118_HW_CFG_PHY_CLK_SEL_EMII |
    268 			    LAN9118_HW_CFG_SMI_SEL |
    269 			    LAN9118_HW_CFG_EXT_PHY_EN);
    270 			delay(1);	/* Once wait more 5 cycle */
    271 
    272 			/* Call mii_attach, avoid at phy1. */
    273 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    274 			    0, MII_OFFSET_ANY, 0);
    275 			for (i = 2; i < MII_NPHY; i++)
    276 				mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    277 				    i, MII_OFFSET_ANY, 0);
    278 		}
    279 	}
    280 
    281 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    282 
    283 	/* Attach the interface. */
    284 	if_attach(ifp);
    285 	if_deferred_start_init(ifp, NULL);
    286 	ether_ifattach(ifp, sc->sc_enaddr);
    287 
    288 	callout_init(&sc->sc_tick, 0);
    289 
    290 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    291 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    292 	return 0;
    293 }
    294 
    295 int
    296 lan9118_intr(void *arg)
    297 {
    298 	struct lan9118_softc *sc = (struct lan9118_softc *)arg;
    299 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    300 	uint32_t int_sts, int_en, datum = 0;
    301 	int handled = 0;
    302 
    303 	for (;;) {
    304 		int_sts =
    305 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS);
    306 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    307 		    int_sts);
    308 		int_en =
    309 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN) |
    310 		    LAN9118_INT_SW_INT;
    311 
    312 		DPRINTFN(3, ("%s: int_sts=0x%x, int_en=0x%x\n",
    313 		    __func__, int_sts, int_en));
    314 
    315 		if (!(int_sts & int_en))
    316 			break;
    317 		datum = int_sts;
    318 		handled = 1;
    319 
    320 #if 0	/* not yet... */
    321 		if (int_sts & LAN9118_INT_PHY_INT) { /* PHY */
    322 			/* Shall we need? */
    323 		}
    324 		if (int_sts & LAN9118_INT_PME_INT) { /*Power Management Event*/
    325 			/* not yet... */
    326 		}
    327 #endif
    328 		if (int_sts & LAN9118_INT_RXE) {
    329 			ifp->if_ierrors++;
    330 			aprint_error_ifnet(ifp, "Receive Error\n");
    331 		}
    332 		if (int_sts & (LAN9118_INT_TSFL|LAN9118_INT_SW_INT)) /* TX Status FIFO Level */
    333 			lan9118_txintr(sc);
    334 		if (int_sts & LAN9118_INT_RXDF_INT) {
    335 			ifp->if_ierrors++;
    336 			aprint_error_ifnet(ifp, "RX Dropped Frame Interrupt\n");
    337 		}
    338 		if (int_sts & LAN9118_INT_RSFF) {
    339 			ifp->if_ierrors++;
    340 			aprint_error_ifnet(ifp, "RX Status FIFO Full\n");
    341 		}
    342 		if (int_sts & LAN9118_INT_RSFL) /* RX Status FIFO Level */
    343 			lan9118_rxintr(sc);
    344 	}
    345 
    346 	if (handled)
    347 		if_schedule_deferred_start(ifp);
    348 
    349 	rnd_add_uint32(&sc->rnd_source, datum);
    350 
    351 	return handled;
    352 }
    353 
    354 
    355 static void
    356 lan9118_start(struct ifnet *ifp)
    357 {
    358 	struct lan9118_softc *sc = ifp->if_softc;
    359 	struct mbuf *m0, *m;
    360 	unsigned tdfree, totlen, dso;
    361 	uint32_t txa, txb, int_en;
    362 	uint8_t *p;
    363 	int n;
    364 
    365 	DPRINTFN(3, ("%s\n", __func__));
    366 
    367 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    368 		return;
    369 
    370 	totlen = 0;
    371 	for (;;) {
    372 		IFQ_POLL(&ifp->if_snd, m0);
    373 		if (m0 == NULL)
    374 			break;
    375 
    376 		tdfree = LAN9118_TX_FIFO_INF_TDFREE(bus_space_read_4(sc->sc_iot,
    377 		    sc->sc_ioh, LAN9118_TX_FIFO_INF));
    378 		if (tdfree < 2036) {
    379 			/*
    380 			 * 2036 is the possible maximum FIFO consumption
    381 			 * for the most fragmented frame.
    382 			 */
    383 			ifp->if_flags |= IFF_OACTIVE;
    384 			break;
    385 		}
    386 
    387 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    388 
    389 		/*
    390 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    391 		 */
    392 
    393 		/*
    394 		 * Check mbuf chain -- "middle" buffers must be >= 4 bytes
    395 		 * and maximum # of buffers is 86.
    396 		 */
    397 		m = m0;
    398 		n = 0;
    399 		while (m) {
    400 			if (m->m_len < 4 || ++n > 86) {
    401 				/* Copy mbuf chain. */
    402 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    403 				if (m == NULL)
    404 					goto discard;   /* discard packet */
    405 				MCLGET(m, M_DONTWAIT);
    406 				if ((m->m_flags & M_EXT) == 0) {
    407 					m_freem(m);
    408 					goto discard;	/* discard packet */
    409 				}
    410 				m_copydata(m0, 0, m0->m_pkthdr.len,
    411 				    mtod(m, void *));
    412 				m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    413 				m_freem(m0);
    414 				m0 = m;
    415 				break;
    416 			}
    417 			m = m->m_next;
    418 		}
    419 
    420 		m = m0;
    421 		totlen = m->m_pkthdr.len;
    422 		p = mtod(m, uint8_t *);
    423 		dso = (unsigned)p & 0x3;
    424 		txa =
    425 		    LAN9118_TXC_A_BEA_4B	|
    426 		    LAN9118_TXC_A_DSO(dso)	|
    427 		    LAN9118_TXC_A_FS		|
    428 		    LAN9118_TXC_A_BS(m->m_len);
    429 		txb = LAN9118_TXC_B_PL(totlen);
    430 		while (m->m_next != NULL) {
    431 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    432 			    LAN9118_TXDFIFOP, txa);
    433 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    434 			    LAN9118_TXDFIFOP, txb);
    435 			bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    436 			    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    437 			    (m->m_len + dso + 3) >> 2);
    438 
    439 			m = m->m_next;
    440 			p = mtod(m, uint8_t *);
    441 			dso = (unsigned)p & 0x3;
    442 			txa =
    443 			    LAN9118_TXC_A_BEA_4B	|
    444 			    LAN9118_TXC_A_DSO(dso)	|
    445 			    LAN9118_TXC_A_BS(m->m_len);
    446 		}
    447 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    448 		    txa | LAN9118_TXC_A_LS);
    449 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    450 		    txb);
    451 		bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    452 		    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    453 		    (m->m_len + dso + 3) >> 2);
    454 
    455 discard:
    456 		/*
    457 		 * Pass the packet to any BPF listeners.
    458 		 */
    459 		bpf_mtap(ifp, m0, BPF_D_OUT);
    460 
    461 		m_freem(m0);
    462 	}
    463 
    464 	if (totlen > 0) {
    465 		ifp->if_timer = 5;
    466 
    467 		/*
    468 		 * Trigger a software interrupt to catch any missed completion
    469 		 * interrupts.
    470 		 */
    471 		int_en = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN);
    472 		int_en |= LAN9118_INT_SW_INT;
    473 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN, int_en);
    474 	}
    475 }
    476 
    477 static int
    478 lan9118_ioctl(struct ifnet *ifp, u_long command, void *data)
    479 {
    480 	struct lan9118_softc *sc = ifp->if_softc;
    481 	struct ifreq *ifr = data;
    482 	struct mii_data *mii = &sc->sc_mii;
    483 	int s, error = 0;
    484 
    485 	s = splnet();
    486 
    487 	switch (command) {
    488 	case SIOCSIFFLAGS:
    489 		DPRINTFN(2, ("%s: IFFLAGS\n", __func__));
    490 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    491 			break;
    492 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    493 		case IFF_RUNNING:
    494 			lan9118_stop(ifp, 0);
    495 			break;
    496 		case IFF_UP:
    497 			lan9118_init(ifp);
    498 			break;
    499 		case IFF_UP|IFF_RUNNING:
    500 			lan9118_set_filter(sc);
    501 			break;
    502 		default:
    503 			break;
    504 		}
    505 		error = 0;
    506 		break;
    507 
    508 	case SIOCGIFMEDIA:
    509 	case SIOCSIFMEDIA:
    510 		DPRINTFN(2, ("%s: MEDIA\n", __func__));
    511 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
    512 		break;
    513 
    514 	default:
    515 		DPRINTFN(2, ("%s: ETHER\n", __func__));
    516 		error = ether_ioctl(ifp, command, data);
    517 		if (error == ENETRESET) {
    518 			if (ifp->if_flags & IFF_RUNNING) {
    519 				lan9118_set_filter(sc);
    520 				DPRINTFN(2, ("%s set_filter called\n",
    521 				    __func__));
    522 			}
    523 			error = 0;
    524 		}
    525 		break;
    526 	}
    527 
    528 	splx(s);
    529 
    530 	return error;
    531 }
    532 
    533 static int
    534 lan9118_init(struct ifnet *ifp)
    535 {
    536 	struct lan9118_softc *sc = ifp->if_softc;
    537 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    538 	uint32_t reg, hw_cfg, mac_cr, irq_cfg;
    539 	int timo, s;
    540 
    541 	DPRINTFN(2, ("%s\n", __func__));
    542 
    543 	s = splnet();
    544 
    545 	/* wait for PMT_CTRL[READY] */
    546 	timo = mstohz(5000);	/* XXXX 5sec */
    547 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    548 	    LAN9118_PMT_CTRL_READY)) {
    549 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST,
    550 		    0xbad0c0de);
    551 		tsleep(&sc, PRIBIO, "lan9118_pmt_ready", 1);
    552 		if (--timo <= 0) {
    553 			splx(s);
    554 			return EBUSY;
    555 		}
    556 	}
    557 
    558 	/* Soft Reset */
    559 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    560 	    LAN9118_HW_CFG_SRST);
    561 	do {
    562 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    563 		if (reg & LAN9118_HW_CFG_SRST_TO) {
    564 			aprint_error_dev(sc->sc_dev,
    565 			    "soft reset timeouted out\n");
    566 			splx(s);
    567 			return ETIMEDOUT;
    568 		}
    569 	} while (reg & LAN9118_HW_CFG_SRST);
    570 
    571 	/* Set MAC and PHY CSRs */
    572 
    573 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    574 		/* need byte swap */
    575 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    576 		    0xffffffff);
    577 
    578 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    579 	    LAN9118_E2P_CMD_EPCB);
    580 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    581 	    LAN9118_E2P_CMD_MACAL)) {
    582 		lan9118_mac_writereg(sc, LAN9118_ADDRL,
    583 		    sc->sc_enaddr[0] |
    584 		    sc->sc_enaddr[1] << 8 |
    585 		    sc->sc_enaddr[2] << 16 |
    586 		    sc->sc_enaddr[3] << 24);
    587 		lan9118_mac_writereg(sc, LAN9118_ADDRH,
    588 		    sc->sc_enaddr[4] | sc->sc_enaddr[5] << 8);
    589 	}
    590 
    591 	if (ifm->ifm_media & IFM_FLOW) {
    592 		lan9118_mac_writereg(sc, LAN9118_FLOW,
    593 		    LAN9118_FLOW_FCPT(1) | LAN9118_FLOW_FCEN);
    594 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_AFC_CFG,
    595 		    sc->sc_afc_cfg);
    596 	}
    597 
    598 	lan9118_ifm_change(ifp);
    599 	hw_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    600 	hw_cfg &= ~LAN9118_HW_CFG_TX_FIF_MASK;
    601 	hw_cfg |= LAN9118_HW_CFG_TX_FIF_SZ(LAN9118_TX_FIF_SZ);
    602 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG, hw_cfg);
    603 
    604 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_GPIO_CFG,
    605 	    LAN9118_GPIO_CFG_LEDX_EN(2)  |
    606 	    LAN9118_GPIO_CFG_LEDX_EN(1)  |
    607 	    LAN9118_GPIO_CFG_LEDX_EN(0)  |
    608 	    LAN9118_GPIO_CFG_GPIOBUFN(2) |
    609 	    LAN9118_GPIO_CFG_GPIOBUFN(1) |
    610 	    LAN9118_GPIO_CFG_GPIOBUFN(0));
    611 
    612 	irq_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG);
    613 	irq_cfg |= LAN9118_IRQ_CFG_IRQ_EN;
    614 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG, irq_cfg);
    615 
    616 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    617 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS));
    618 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_FIFO_INT,
    619 	    LAN9118_FIFO_INT_TXSL(0) | LAN9118_FIFO_INT_RXSL(0));
    620 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN,
    621 #if 0	/* not yet... */
    622 	    LAN9118_INT_PHY_INT | /* PHY */
    623 	    LAN9118_INT_PME_INT | /* Power Management Event */
    624 #endif
    625 	    LAN9118_INT_RXE     | /* Receive Error */
    626 	    LAN9118_INT_TSFL    | /* TX Status FIFO Level */
    627 	    LAN9118_INT_RXDF_INT| /* RX Dropped Frame Interrupt */
    628 	    LAN9118_INT_RSFF    | /* RX Status FIFO Full */
    629 	    LAN9118_INT_RSFL);	  /* RX Status FIFO Level */
    630 
    631 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    632 	    LAN9118_RX_CFG_RXDOFF(2));
    633 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    634 	    LAN9118_TX_CFG_TX_ON);
    635 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    636 	lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    637 	    mac_cr | LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    638 
    639 	lan9118_set_filter(sc);
    640 
    641 	ifp->if_flags |= IFF_RUNNING;
    642 	ifp->if_flags &= ~IFF_OACTIVE;
    643 
    644 	callout_reset(&sc->sc_tick, hz, lan9118_tick, sc);
    645 
    646 	splx(s);
    647 
    648 	return 0;
    649 }
    650 
    651 static void
    652 lan9118_stop(struct ifnet *ifp, int disable)
    653 {
    654 	struct lan9118_softc *sc = ifp->if_softc;
    655 	uint32_t cr;
    656 
    657 	DPRINTFN(2, ("%s\n", __func__));
    658 
    659 	/* Disable IRQ */
    660 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN, 0);
    661 
    662 	/* Stopping transmitter */
    663 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    664 	    LAN9118_TX_CFG_STOP_TX);
    665 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG) &
    666 	    (LAN9118_TX_CFG_TX_ON | LAN9118_TX_CFG_STOP_TX));
    667 
    668 	/* Purge TX Status/Data FIFOs */
    669 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    670 	    LAN9118_TX_CFG_TXS_DUMP | LAN9118_TX_CFG_TXD_DUMP);
    671 
    672 	/* Stopping receiver, also clear TXEN */
    673 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    674 	cr &= ~(LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    675 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    676 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS) &
    677 	    LAN9118_INT_RXSTOP_INT));
    678 
    679 	/* Clear RX Status/Data FIFOs */
    680 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    681 	    LAN9118_RX_CFG_RX_DUMP);
    682 
    683 	callout_stop(&sc->sc_tick);
    684 }
    685 
    686 static void
    687 lan9118_watchdog(struct ifnet *ifp)
    688 {
    689 	struct lan9118_softc *sc = ifp->if_softc;
    690 
    691 	/*
    692 	 * Reclaim first as there is a possibility of losing Tx completion
    693 	 * interrupts.
    694 	 */
    695 	lan9118_txintr(sc);
    696 
    697 	aprint_error_ifnet(ifp, "watchdog timeout\n");
    698 	ifp->if_oerrors++;
    699 
    700 	lan9118_init(ifp);
    701 }
    702 
    703 
    704 static int
    705 lan9118_ifm_change(struct ifnet *ifp)
    706 {
    707 	struct lan9118_softc *sc = ifp->if_softc;
    708 	struct mii_data *mii = &sc->sc_mii;
    709 	struct ifmedia *ifm = &mii->mii_media;
    710 	struct ifmedia_entry *ife = ifm->ifm_cur;
    711 	uint32_t pmt_ctrl;
    712 
    713 	DPRINTFN(3, ("%s: ifm inst %d\n", __func__, IFM_INST(ife->ifm_media)));
    714 
    715 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    716 	    LAN9118_HW_CFG_MBO | LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    717 	delay(1);	/* Wait 5 cycle */
    718 
    719 	if (IFM_INST(ife->ifm_media) != 0) {
    720 		/* Use External PHY */
    721 
    722 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    723 		    LAN9118_HW_CFG_MBO			|
    724 		    LAN9118_HW_CFG_PHY_CLK_SEL_EMII	|
    725 		    LAN9118_HW_CFG_SMI_SEL		|
    726 		    LAN9118_HW_CFG_EXT_PHY_EN);
    727 		delay(1);
    728 		return mii_mediachg(&sc->sc_mii);
    729 	}
    730 
    731 	/* Setup Internal PHY */
    732 
    733 	mii->mii_media_status = IFM_AVALID;
    734 	mii->mii_media_active = IFM_ETHER;
    735 
    736 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    737 	    LAN9118_HW_CFG_MBO |
    738 	    LAN9118_HW_CFG_PHY_CLK_SEL_IPHY);
    739 	delay(1);
    740 
    741 	/* Reset PHY */
    742 	pmt_ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL);
    743 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL,
    744 	    pmt_ctrl | LAN9118_PMT_CTRL_PHY_RST);
    745 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    746 	    LAN9118_PMT_CTRL_PHY_RST);
    747 
    748 	mii_mediachg(&sc->sc_mii);
    749 	return 0;
    750 }
    751 
    752 static void
    753 lan9118_ifm_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    754 {
    755 	struct lan9118_softc *sc = ifp->if_softc;
    756 	struct mii_data *mii = &sc->sc_mii;
    757 
    758 	DPRINTFN(3, ("%s\n", __func__));
    759 
    760 	mii_pollstat(mii);
    761 	ifmr->ifm_active = mii->mii_media_active;
    762 	ifmr->ifm_status = mii->mii_media_status;
    763 }
    764 
    765 
    766 static int
    767 lan9118_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    768 {
    769 
    770 	return lan9118_mii_readreg(device_private(dev), phy, reg, val);
    771 }
    772 static int
    773 lan9118_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    774 {
    775 
    776 	return lan9118_mii_writereg(device_private(dev), phy, reg, val);
    777 }
    778 
    779 static void
    780 lan9118_miibus_statchg(struct ifnet *ifp)
    781 {
    782 	struct lan9118_softc *sc = ifp->if_softc;
    783 	u_int cr;
    784 
    785 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    786 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    787 		cr &= ~LAN9118_MAC_CR_RCVOWN;
    788 		cr |= LAN9118_MAC_CR_FDPX;
    789 	} else {
    790 		cr |= LAN9118_MAC_CR_RCVOWN;
    791 		cr &= ~LAN9118_MAC_CR_FDPX;
    792 	}
    793 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    794 }
    795 
    796 
    797 static int
    798 lan9118_mii_readreg(struct lan9118_softc *sc, int phy, int reg, uint16_t *val)
    799 {
    800 	uint32_t acc;
    801 
    802 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    803 	    LAN9118_MII_ACC_MIIBZY);
    804 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg);
    805 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    806 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    807 	    LAN9118_MII_ACC_MIIBZY);
    808 	*val = lan9118_mac_readreg(sc, LAN9118_MII_DATA) & 0xffff;
    809 	return 0;
    810 }
    811 
    812 static int
    813 lan9118_mii_writereg(struct lan9118_softc *sc, int phy, int reg, uint16_t val)
    814 {
    815 	uint32_t acc;
    816 
    817 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    818 	    LAN9118_MII_ACC_MIIBZY);
    819 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg) |
    820 	    LAN9118_MII_ACC_MIIWNR;
    821 	lan9118_mac_writereg(sc, LAN9118_MII_DATA, val);
    822 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    823 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    824 	    LAN9118_MII_ACC_MIIBZY);
    825 
    826 	return 0;
    827 }
    828 
    829 static uint32_t
    830 lan9118_mac_readreg(struct lan9118_softc *sc, int reg)
    831 {
    832 	uint32_t cmd;
    833 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    834 
    835 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    836 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_R | reg);
    837 	do {
    838 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    839 		    LAN9118_MAC_CSR_CMD);
    840 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    841 			break;
    842 		delay(100);
    843 	} while (timo -= 100);
    844 	if (timo <= 0)
    845 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    846 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA);
    847 }
    848 
    849 static void
    850 lan9118_mac_writereg(struct lan9118_softc *sc, int reg, uint32_t val)
    851 {
    852 	uint32_t cmd;
    853 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    854 
    855 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA, val);
    856 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    857 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_W | reg);
    858 	do {
    859 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    860 		    LAN9118_MAC_CSR_CMD);
    861 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    862 			break;
    863 		delay(100);
    864 	} while (timo -= 100);
    865 	if (timo <= 0)
    866 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    867 }
    868 
    869 
    870 static void
    871 lan9118_set_filter(struct lan9118_softc *sc)
    872 {
    873 	struct ether_multistep step;
    874 	struct ether_multi *enm;
    875 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    876 	uint32_t mac_cr, h, hashes[2] = { 0, 0 };
    877 
    878 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    879 	if (ifp->if_flags & IFF_PROMISC) {
    880 		lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    881 		    mac_cr | LAN9118_MAC_CR_PRMS);
    882 		return;
    883 	}
    884 
    885 	mac_cr &= ~(LAN9118_MAC_CR_PRMS | LAN9118_MAC_CR_MCPAS |
    886 	    LAN9118_MAC_CR_BCAST | LAN9118_MAC_CR_HPFILT);
    887 	if (!(ifp->if_flags & IFF_BROADCAST))
    888 		mac_cr |= LAN9118_MAC_CR_BCAST;
    889 
    890 	if (ifp->if_flags & IFF_ALLMULTI)
    891 		mac_cr |= LAN9118_MAC_CR_MCPAS;
    892 	else {
    893 		ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    894 		while (enm != NULL) {
    895 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    896 			    ETHER_ADDR_LEN) != 0) {
    897 				/*
    898 				 * We must listen to a range of multicast
    899 				 * addresses.  For now, just accept all
    900 				 * multicasts, rather than trying to set
    901 				 * only those filter bits needed to match
    902 				 * the range.  (At this time, the only use
    903 				 * of address ranges is for IP multicast
    904 				 * routing, for which the range is big enough
    905 				 * to require all bits set.)
    906 				 */
    907 				ifp->if_flags |= IFF_ALLMULTI;
    908 				mac_cr |= LAN9118_MAC_CR_MCPAS;
    909 				break;
    910 			}
    911 			h = ether_crc32_le(enm->enm_addrlo,
    912 			    ETHER_ADDR_LEN) >> 26;
    913 			hashes[h >> 5] |= 1 << (h & 0x1f);
    914 
    915 			mac_cr |= LAN9118_MAC_CR_HPFILT;
    916 			ETHER_NEXT_MULTI(step, enm);
    917 		}
    918 		if (mac_cr & LAN9118_MAC_CR_HPFILT) {
    919 			lan9118_mac_writereg(sc, LAN9118_HASHH, hashes[1]);
    920 			lan9118_mac_writereg(sc, LAN9118_HASHL, hashes[0]);
    921 		}
    922 	}
    923 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, mac_cr);
    924 	return;
    925 }
    926 
    927 static void
    928 lan9118_rxintr(struct lan9118_softc *sc)
    929 {
    930 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    931 	struct mbuf *m;
    932 	uint32_t rx_fifo_inf, rx_status;
    933 	int pktlen;
    934 	const int pad = ETHER_HDR_LEN % sizeof(uint32_t);
    935 
    936 	DPRINTFN(3, ("%s\n", __func__));
    937 
    938 	for (;;) {
    939 		rx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    940 		    LAN9118_RX_FIFO_INF);
    941 		if (LAN9118_RX_FIFO_INF_RXSUSED(rx_fifo_inf) == 0)
    942 			break;
    943 
    944 		rx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    945 		    LAN9118_RXSFIFOP);
    946 		pktlen = LAN9118_RXS_PKTLEN(rx_status);
    947 		DPRINTFN(3, ("%s: rx_status=0x%x(pktlen %d)\n",
    948 		    __func__, rx_status, pktlen));
    949 		if (rx_status & (LAN9118_RXS_ES | LAN9118_RXS_LENERR |
    950 		    LAN9118_RXS_RWTO | LAN9118_RXS_MIIERR | LAN9118_RXS_DBIT)) {
    951 			if (rx_status & LAN9118_RXS_LENERR)
    952 				aprint_error_dev(sc->sc_dev, "Length Error\n");
    953 			if (rx_status & LAN9118_RXS_RUNTF)
    954 				aprint_error_dev(sc->sc_dev, "Runt Frame\n");
    955 			if (rx_status & LAN9118_RXS_FTL)
    956 				aprint_error_dev(sc->sc_dev,
    957 				    "Frame Too Long\n");
    958 			if (rx_status & LAN9118_RXS_RWTO)
    959 				aprint_error_dev(sc->sc_dev,
    960 				    "Receive Watchdog time-out\n");
    961 			if (rx_status & LAN9118_RXS_MIIERR)
    962 				aprint_error_dev(sc->sc_dev, "MII Error\n");
    963 			if (rx_status & LAN9118_RXS_DBIT)
    964 				aprint_error_dev(sc->sc_dev, "Drabbling Bit\n");
    965 			if (rx_status & LAN9118_RXS_COLS)
    966 				aprint_error_dev(sc->sc_dev,
    967 				    "Collision Seen\n");
    968 			if (rx_status & LAN9118_RXS_CRCERR)
    969 				aprint_error_dev(sc->sc_dev, "CRC Error\n");
    970 
    971 dropit:
    972 			ifp->if_ierrors++;
    973 			/*
    974 			 * Receive Data FIFO Fast Forward
    975 			 * When performing a fast-forward, there must be at
    976 			 * least 4 DWORDs of data in the RX data FIFO for the
    977 			 * packet being discarded.
    978 			 */
    979 			if (pktlen >= 4 * sizeof(uint32_t)) {
    980 				uint32_t rx_dp_ctl;
    981 
    982 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    983 				    LAN9118_RX_DP_CTL,
    984 				    LAN9118_RX_DP_CTL_RX_FFWD);
    985 				/* DP_FFWD bit is self clearing */
    986 				do {
    987 					rx_dp_ctl = bus_space_read_4(sc->sc_iot,
    988 					    sc->sc_ioh, LAN9118_RX_DP_CTL);
    989 				} while (rx_dp_ctl & LAN9118_RX_DP_CTL_RX_FFWD);
    990 			} else {
    991 				/* For less than 4 DWORDs do not use RX_FFWD. */
    992 				uint32_t garbage[4];
    993 
    994 				bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
    995 				    LAN9118_RXDFIFOP, garbage,
    996 				    roundup(pktlen, 4) >> 2);
    997 			}
    998 			continue;
    999 		}
   1000 
   1001 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1002 		if (m == NULL)
   1003 			goto dropit;
   1004 		if (pktlen > (MHLEN - pad)) {
   1005 			MCLGET(m, M_DONTWAIT);
   1006 			if ((m->m_flags & M_EXT) == 0) {
   1007 				m_freem(m);
   1008 				goto dropit;
   1009 			}
   1010 		}
   1011 
   1012 		/* STRICT_ALIGNMENT */
   1013 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
   1014 		    LAN9118_RX_CFG_RXEA_4B | LAN9118_RX_CFG_RXDOFF(pad));
   1015 		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, LAN9118_RXDFIFOP,
   1016 		    mtod(m, uint32_t *),
   1017 		    roundup(pad + pktlen, sizeof(uint32_t)) >> 2);
   1018 		m->m_data += pad;
   1019 
   1020 		m_set_rcvif(m, ifp);
   1021 		m->m_pkthdr.len = m->m_len = (pktlen - ETHER_CRC_LEN);
   1022 
   1023 		/* Pass it on. */
   1024 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1025 	}
   1026 }
   1027 
   1028 static void
   1029 lan9118_txintr(struct lan9118_softc *sc)
   1030 {
   1031 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1032 	uint32_t tx_fifo_inf, tx_status;
   1033 	int fdx = IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX;
   1034 	int tdfree;
   1035 
   1036 	DPRINTFN(3, ("%s\n", __func__));
   1037 
   1038 	for (;;) {
   1039 		tx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1040 		    LAN9118_TX_FIFO_INF);
   1041 		if (LAN9118_TX_FIFO_INF_TXSUSED(tx_fifo_inf) == 0)
   1042 			break;
   1043 
   1044 		tx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1045 		    LAN9118_TXSFIFOP);
   1046 		DPRINTFN(3, ("%s: tx_status=0x%x\n", __func__, tx_status));
   1047 		if (tx_status & LAN9118_TXS_ES) {
   1048 			if (tx_status & LAN9118_TXS_LOC)
   1049 				aprint_error_dev(sc->sc_dev,
   1050 				    "Loss Of Carrier\n");
   1051 			if ((tx_status & LAN9118_TXS_NC) && !fdx)
   1052 				aprint_error_dev(sc->sc_dev, "No Carrier\n");
   1053 			if (tx_status & LAN9118_TXS_LCOL)
   1054 				aprint_error_dev(sc->sc_dev,
   1055 				    "Late Collision\n");
   1056 			if (tx_status & LAN9118_TXS_ECOL) {
   1057 				/* Rearch 16 collision */
   1058 				ifp->if_collisions += 16;
   1059 				aprint_error_dev(sc->sc_dev,
   1060 				    "Excessive Collision\n");
   1061 			}
   1062 			if (LAN9118_TXS_COLCNT(tx_status) != 0)
   1063 				aprint_error_dev(sc->sc_dev,
   1064 				    "Collision Count: %d\n",
   1065 				    LAN9118_TXS_COLCNT(tx_status));
   1066 			if (tx_status & LAN9118_TXS_ED)
   1067 				aprint_error_dev(sc->sc_dev,
   1068 				    "Excessive Deferral\n");
   1069 			if (tx_status & LAN9118_TXS_DEFERRED)
   1070 				aprint_error_dev(sc->sc_dev, "Deferred\n");
   1071 			ifp->if_oerrors++;
   1072 		} else
   1073 			ifp->if_opackets++;
   1074 	}
   1075 
   1076 	tdfree = LAN9118_TX_FIFO_INF_TDFREE(tx_fifo_inf);
   1077 	if (tdfree == LAN9118_TX_DATA_FIFO_SIZE)
   1078 		/* FIFO empty */
   1079 		ifp->if_timer = 0;
   1080 	if (tdfree >= 2036)
   1081 		/*
   1082 		 * 2036 is the possible maximum FIFO consumption
   1083 		 * for the most fragmented frame.
   1084 		 */
   1085 		ifp->if_flags &= ~IFF_OACTIVE;
   1086 }
   1087 
   1088 void
   1089 lan9118_tick(void *v)
   1090 {
   1091 	struct lan9118_softc *sc = v;
   1092 	int s;
   1093 
   1094 	s = splnet();
   1095 	mii_tick(&sc->sc_mii);
   1096 	callout_schedule(&sc->sc_tick, hz);
   1097 	splx(s);
   1098 }
   1099