Home | History | Annotate | Line # | Download | only in ic
lan9118.c revision 1.19
      1 /*	$NetBSD: lan9118.c,v 1.19 2015/04/13 16:33:24 riastradh 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.19 2015/04/13 16:33:24 riastradh 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);
     94 static void lan9118_miibus_writereg(device_t, int, int, int);
     95 static void lan9118_miibus_statchg(struct ifnet *);
     96 
     97 static uint16_t lan9118_mii_readreg(struct lan9118_softc *, int, int);
     98 static void 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;
    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 	/* Configure interrupt polarity */
    173 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG,
    174 	    LAN9118_IRQ_CFG_IRQ_TYPE | LAN9118_IRQ_CFG_IRQ_POL);
    175 
    176 	val = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_ID_REV);
    177 	sc->sc_id = LAN9118_ID_REV_ID(val);
    178 	sc->sc_rev = LAN9118_ID_REV_REV(val);
    179 
    180 #define LAN9xxx_ID(id)	\
    181  (IS_LAN9118(id) ? (id) : (IS_LAN9218(id) ? ((id) >> 4) + 0x100 : (id) & 0xfff))
    182 
    183 	aprint_normal(": SMSC LAN9%03x Rev %d\n",
    184 	    LAN9xxx_ID(sc->sc_id), sc->sc_rev);
    185 
    186 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    187 		aprint_normal_dev(sc->sc_dev, "byte swap mode\n");
    188 
    189 	timo = 3 * 1000 * 1000;	/* XXXX 3sec */
    190 	do {
    191 		val = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    192 		    LAN9118_MAC_CSR_CMD);
    193 		if (!(val & LAN9118_MAC_CSR_CMD_BUSY))
    194 			break;
    195 		delay(100);
    196 	} while (timo -= 100);
    197 	if (timo <= 0)
    198 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    199 	if (!(sc->sc_flags & LAN9118_FLAGS_NO_EEPROM)) {
    200 		/* Read auto-loaded MAC address */
    201 		val = lan9118_mac_readreg(sc, LAN9118_ADDRL);
    202 		sc->sc_enaddr[3] = (val >> 24) & 0xff;
    203 		sc->sc_enaddr[2] = (val >> 16) & 0xff;
    204 		sc->sc_enaddr[1] = (val >> 8) & 0xff;
    205 		sc->sc_enaddr[0] = val & 0xff;
    206 		val = lan9118_mac_readreg(sc, LAN9118_ADDRH);
    207 		sc->sc_enaddr[5] = (val >> 8) & 0xff;
    208 		sc->sc_enaddr[4] = val & 0xff;
    209 	}
    210 	aprint_normal_dev(sc->sc_dev, "MAC address %s\n",
    211 	    ether_sprintf(sc->sc_enaddr));
    212 
    213 	KASSERT(LAN9118_TX_FIF_SZ >= 2 && LAN9118_TX_FIF_SZ < 15);
    214 	sc->sc_afc_cfg = afc_cfg[LAN9118_TX_FIF_SZ];
    215 
    216 	/* Initialize the ifnet structure. */
    217 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    218 	ifp->if_softc = sc;
    219 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    220 	ifp->if_start = lan9118_start;
    221 	ifp->if_ioctl = lan9118_ioctl;
    222 	ifp->if_init = lan9118_init;
    223 	ifp->if_stop = lan9118_stop;
    224 	ifp->if_watchdog = lan9118_watchdog;
    225 	IFQ_SET_READY(&ifp->if_snd);
    226 
    227 #if 0	/* Not support 802.1Q VLAN-sized frames yet. */
    228 	sc->sc_ec.ec_capabilities |= ETHERCAP_VLAN_MTU;
    229 #endif
    230 
    231 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    232 	    lan9118_ifm_change, lan9118_ifm_status);
    233 	sc->sc_mii.mii_ifp = ifp;
    234 	sc->sc_mii.mii_readreg = lan9118_miibus_readreg;
    235 	sc->sc_mii.mii_writereg = lan9118_miibus_writereg;
    236 	sc->sc_mii.mii_statchg = lan9118_miibus_statchg;
    237 
    238 	/*
    239 	 * Number of instance of Internal PHY is always 0.  External PHY
    240 	 * number that above.
    241 	 */
    242 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
    243 
    244 	if (sc->sc_id == LAN9118_ID_9115 || sc->sc_id == LAN9118_ID_9117 ||
    245 	    sc->sc_id == LAN9218_ID_9215 || sc->sc_id == LAN9218_ID_9217) {
    246 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG) &
    247 		    LAN9118_HW_CFG_EXT_PHY_DET) {
    248 			/*
    249 			 * We always have a internal PHY at phy1.
    250 			 * In addition, external PHY is attached.
    251 			 */
    252 			DPRINTFN(1, ("%s: detect External PHY\n", __func__));
    253 
    254 			/* Switch MII and SMI */
    255 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    256 			    LAN9118_HW_CFG,
    257 			    LAN9118_HW_CFG_MBO |
    258 			    LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    259 			delay(1);	/* Wait 5 cycle */
    260 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    261 			    LAN9118_HW_CFG,
    262 			    LAN9118_HW_CFG_MBO |
    263 			    LAN9118_HW_CFG_PHY_CLK_SEL_EMII |
    264 			    LAN9118_HW_CFG_SMI_SEL |
    265 			    LAN9118_HW_CFG_EXT_PHY_EN);
    266 			delay(1);	/* Once wait more 5 cycle */
    267 
    268 			/* Call mii_attach, avoid at phy1. */
    269 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    270 			    0, MII_OFFSET_ANY, 0);
    271 			for (i = 2; i < MII_NPHY; i++)
    272 				mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    273 				    i, MII_OFFSET_ANY, 0);
    274 		}
    275 	}
    276 
    277 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    278 
    279 	/* Attach the interface. */
    280 	if_attach(ifp);
    281 	ether_ifattach(ifp, sc->sc_enaddr);
    282 
    283 	callout_init(&sc->sc_tick, 0);
    284 
    285 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    286 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    287 	return 0;
    288 }
    289 
    290 int
    291 lan9118_intr(void *arg)
    292 {
    293 	struct lan9118_softc *sc = (struct lan9118_softc *)arg;
    294 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    295 	uint32_t int_sts, int_en, datum = 0;
    296 	int handled = 0;
    297 
    298 	for (;;) {
    299 		int_sts =
    300 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS);
    301 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    302 		    int_sts);
    303 		int_en =
    304 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN);
    305 
    306 		DPRINTFN(3, ("%s: int_sts=0x%x, int_en=0x%x\n",
    307 		    __func__, int_sts, int_en));
    308 
    309 		if (!(int_sts & int_en))
    310 			break;
    311 		datum = int_sts;
    312 
    313 #if 0	/* not yet... */
    314 		if (int_sts & LAN9118_INT_PHY_INT) { /* PHY */
    315 			/* Shall we need? */
    316 		}
    317 		if (int_sts & LAN9118_INT_PME_INT) { /*Power Management Event*/
    318 			/* not yet... */
    319 		}
    320 #endif
    321 		if (int_sts & LAN9118_INT_RXE) {
    322 			ifp->if_ierrors++;
    323 			aprint_error_ifnet(ifp, "Receive Error\n");
    324 		}
    325 		if (int_sts & LAN9118_INT_TSFL) /* TX Status FIFO Level */
    326 			lan9118_txintr(sc);
    327 		if (int_sts & LAN9118_INT_RXDF_INT) {
    328 			ifp->if_ierrors++;
    329 			aprint_error_ifnet(ifp, "RX Dropped Frame Interrupt\n");
    330 		}
    331 		if (int_sts & LAN9118_INT_RSFF) {
    332 			ifp->if_ierrors++;
    333 			aprint_error_ifnet(ifp, "RX Status FIFO Full\n");
    334 		}
    335 		if (int_sts & LAN9118_INT_RSFL) /* RX Status FIFO Level */
    336 			 lan9118_rxintr(sc);
    337 	}
    338 
    339 	if (!IFQ_IS_EMPTY(&ifp->if_snd))
    340 		lan9118_start(ifp);
    341 
    342 	rnd_add_uint32(&sc->rnd_source, datum);
    343 
    344 	return handled;
    345 }
    346 
    347 
    348 static void
    349 lan9118_start(struct ifnet *ifp)
    350 {
    351 	struct lan9118_softc *sc = ifp->if_softc;
    352 	struct mbuf *m0, *m;
    353 	unsigned tdfree, totlen, dso;
    354 	uint32_t txa, txb;
    355 	uint8_t *p;
    356 	int n;
    357 
    358 	DPRINTFN(3, ("%s\n", __func__));
    359 
    360 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
    361 		return;
    362 
    363 	totlen = 0;
    364 	for (;;) {
    365 		IFQ_POLL(&ifp->if_snd, m0);
    366 		if (m0 == NULL)
    367 			break;
    368 
    369 		tdfree = LAN9118_TX_FIFO_INF_TDFREE(bus_space_read_4(sc->sc_iot,
    370 		    sc->sc_ioh, LAN9118_TX_FIFO_INF));
    371 		if (tdfree < 2036) {
    372 			/*
    373 			 * 2036 is the possible maximum FIFO consumption
    374 			 * for the most fragmented frame.
    375 			 */
    376 			ifp->if_flags |= IFF_OACTIVE;
    377 			break;
    378 		}
    379 
    380 		IFQ_DEQUEUE(&ifp->if_snd, m0);
    381 
    382 		/*
    383 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
    384 		 */
    385 
    386 		/*
    387 		 * Check mbuf chain -- "middle" buffers must be >= 4 bytes
    388 		 * and maximum # of buffers is 86.
    389 		 */
    390 		m = m0;
    391 		n = 0;
    392 		while (m) {
    393 			if (m->m_len < 4 || ++n > 86) {
    394 				/* Copy mbuf chain. */
    395 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    396 				if (m == NULL)
    397 					goto discard;   /* discard packet */
    398 				MCLGET(m, M_DONTWAIT);
    399 				if ((m->m_flags & M_EXT) == 0) {
    400 					m_freem(m);
    401 					goto discard;	/* discard packet */
    402 				}
    403 				m_copydata(m0, 0, m0->m_pkthdr.len,
    404 				    mtod(m, void *));
    405 				m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
    406 				m_freem(m0);
    407 				m0 = m;
    408 				break;
    409 			}
    410 			m = m->m_next;
    411 		}
    412 
    413 		m = m0;
    414 		totlen = m->m_pkthdr.len;
    415 		p = mtod(m, uint8_t *);
    416 		dso = (unsigned)p & 0x3;
    417 		txa =
    418 		    LAN9118_TXC_A_BEA_4B	|
    419 		    LAN9118_TXC_A_DSO(dso)	|
    420 		    LAN9118_TXC_A_FS		|
    421 		    LAN9118_TXC_A_BS(m->m_len);
    422 		txb = LAN9118_TXC_B_PL(totlen);
    423 		while (m->m_next != NULL) {
    424 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    425 			    LAN9118_TXDFIFOP, txa);
    426 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    427 			    LAN9118_TXDFIFOP, txb);
    428 			bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    429 			    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    430 			    (m->m_len + dso + 3) >> 2);
    431 
    432 			m = m->m_next;
    433 			p = mtod(m, uint8_t *);
    434 			dso = (unsigned)p & 0x3;
    435 			txa =
    436 			    LAN9118_TXC_A_BEA_4B	|
    437 			    LAN9118_TXC_A_DSO(dso)	|
    438 			    LAN9118_TXC_A_BS(m->m_len);
    439 		}
    440 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    441 		    txa | LAN9118_TXC_A_LS);
    442 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TXDFIFOP,
    443 		    txb);
    444 		bus_space_write_multi_4(sc->sc_iot, sc->sc_ioh,
    445 		    LAN9118_TXDFIFOP, (uint32_t *)(p - dso),
    446 		    (m->m_len + dso + 3) >> 2);
    447 
    448 discard:
    449 		/*
    450 		 * Pass the packet to any BPF listeners.
    451 		 */
    452 		bpf_mtap(ifp, m0);
    453 
    454 		m_freem(m0);
    455 	}
    456 	if (totlen > 0)
    457 		ifp->if_timer = 5;
    458 }
    459 
    460 static int
    461 lan9118_ioctl(struct ifnet *ifp, u_long command, void *data)
    462 {
    463 	struct lan9118_softc *sc = ifp->if_softc;
    464 	struct ifreq *ifr = data;
    465 	struct mii_data *mii = &sc->sc_mii;
    466 	int s, error = 0;
    467 
    468 	s = splnet();
    469 
    470 	switch (command) {
    471 	case SIOCSIFFLAGS:
    472 		DPRINTFN(2, ("%s: IFFLAGS\n", __func__));
    473 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    474 			break;
    475 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    476 		case IFF_RUNNING:
    477 			lan9118_stop(ifp, 0);
    478 			break;
    479 		case IFF_UP:
    480 			lan9118_init(ifp);
    481 			break;
    482 		case IFF_UP|IFF_RUNNING:
    483 			lan9118_set_filter(sc);
    484 			break;
    485 		default:
    486 			break;
    487 		}
    488 		error = 0;
    489 		break;
    490 
    491 	case SIOCGIFMEDIA:
    492 	case SIOCSIFMEDIA:
    493 		DPRINTFN(2, ("%s: MEDIA\n", __func__));
    494 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
    495 		break;
    496 
    497 	default:
    498 		DPRINTFN(2, ("%s: ETHER\n", __func__));
    499 		error = ether_ioctl(ifp, command, data);
    500 		if (error == ENETRESET) {
    501 			if (ifp->if_flags & IFF_RUNNING) {
    502 				lan9118_set_filter(sc);
    503 				DPRINTFN(2, ("%s set_filter called\n",
    504 				    __func__));
    505 			}
    506 			error = 0;
    507 		}
    508 		break;
    509 	}
    510 
    511 	splx(s);
    512 
    513 	return error;
    514 }
    515 
    516 static int
    517 lan9118_init(struct ifnet *ifp)
    518 {
    519 	struct lan9118_softc *sc = ifp->if_softc;
    520 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    521 	uint32_t reg, hw_cfg, mac_cr;
    522 	int timo, s;
    523 
    524 	DPRINTFN(2, ("%s\n", __func__));
    525 
    526 	s = splnet();
    527 
    528 	/* wait for PMT_CTRL[READY] */
    529 	timo = mstohz(5000);	/* XXXX 5sec */
    530 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    531 	    LAN9118_PMT_CTRL_READY)) {
    532 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST,
    533 		    0xbad0c0de);
    534 		tsleep(&sc, PRIBIO, "lan9118_pmt_ready", 1);
    535 		if (--timo <= 0) {
    536 			splx(s);
    537 			return EBUSY;
    538 		}
    539 	}
    540 
    541 	/* Soft Reset */
    542 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    543 	    LAN9118_HW_CFG_SRST);
    544 	do {
    545 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    546 		if (reg & LAN9118_HW_CFG_SRST_TO) {
    547 			aprint_error_dev(sc->sc_dev,
    548 			    "soft reset timeouted out\n");
    549 			splx(s);
    550 			return ETIMEDOUT;
    551 		}
    552 	} while (reg & LAN9118_HW_CFG_SRST);
    553 
    554 	/* Set MAC and PHY CSRs */
    555 
    556 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    557 		/* need byte swap */
    558 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    559 		    0xffffffff);
    560 
    561 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    562 	    LAN9118_E2P_CMD_EPCB);
    563 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    564 	    LAN9118_E2P_CMD_MACAL)) {
    565 		lan9118_mac_writereg(sc, LAN9118_ADDRL,
    566 		    sc->sc_enaddr[0] |
    567 		    sc->sc_enaddr[1] << 8 |
    568 		    sc->sc_enaddr[2] << 16 |
    569 		    sc->sc_enaddr[3] << 24);
    570 		lan9118_mac_writereg(sc, LAN9118_ADDRH,
    571 		    sc->sc_enaddr[4] | sc->sc_enaddr[5] << 8);
    572 	}
    573 
    574 	if (ifm->ifm_media & IFM_FLOW) {
    575 		lan9118_mac_writereg(sc, LAN9118_FLOW,
    576 		    LAN9118_FLOW_FCPT(1) | LAN9118_FLOW_FCEN);
    577 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_AFC_CFG,
    578 		    sc->sc_afc_cfg);
    579 	}
    580 
    581 	lan9118_ifm_change(ifp);
    582 	hw_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    583 	hw_cfg &= ~LAN9118_HW_CFG_TX_FIF_MASK;
    584 	hw_cfg |= LAN9118_HW_CFG_TX_FIF_SZ(LAN9118_TX_FIF_SZ);
    585 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG, hw_cfg);
    586 
    587 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_GPIO_CFG,
    588 	    LAN9118_GPIO_CFG_LEDX_EN(2)  |
    589 	    LAN9118_GPIO_CFG_LEDX_EN(1)  |
    590 	    LAN9118_GPIO_CFG_LEDX_EN(0)  |
    591 	    LAN9118_GPIO_CFG_GPIOBUFN(2) |
    592 	    LAN9118_GPIO_CFG_GPIOBUFN(1) |
    593 	    LAN9118_GPIO_CFG_GPIOBUFN(0));
    594 
    595 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG,
    596 	    LAN9118_IRQ_CFG_IRQ_EN);
    597 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    598 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS));
    599 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_FIFO_INT,
    600 	    LAN9118_FIFO_INT_TXSL(0) | LAN9118_FIFO_INT_RXSL(0));
    601 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN,
    602 #if 0	/* not yet... */
    603 	    LAN9118_INT_PHY_INT | /* PHY */
    604 	    LAN9118_INT_PME_INT | /* Power Management Event */
    605 #endif
    606 	    LAN9118_INT_RXE     | /* Receive Error */
    607 	    LAN9118_INT_TSFL    | /* TX Status FIFO Level */
    608 	    LAN9118_INT_RXDF_INT| /* RX Dropped Frame Interrupt */
    609 	    LAN9118_INT_RSFF    | /* RX Status FIFO Full */
    610 	    LAN9118_INT_RSFL);	  /* RX Status FIFO Level */
    611 
    612 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    613 	    LAN9118_RX_CFG_RXDOFF(2));
    614 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    615 	    LAN9118_TX_CFG_TX_ON);
    616 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    617 	lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    618 	    mac_cr | LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    619 
    620 	lan9118_set_filter(sc);
    621 
    622 	ifp->if_flags |= IFF_RUNNING;
    623 	ifp->if_flags &= ~IFF_OACTIVE;
    624 
    625 	callout_reset(&sc->sc_tick, hz, lan9118_tick, sc);
    626 
    627 	splx(s);
    628 
    629 	return 0;
    630 }
    631 
    632 static void
    633 lan9118_stop(struct ifnet *ifp, int disable)
    634 {
    635 	struct lan9118_softc *sc = ifp->if_softc;
    636 	uint32_t cr;
    637 
    638 	DPRINTFN(2, ("%s\n", __func__));
    639 
    640 	/* Disable IRQ */
    641 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN, 0);
    642 
    643 	/* Stopping transmitter */
    644 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    645 	    LAN9118_TX_CFG_STOP_TX);
    646 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG) &
    647 	    (LAN9118_TX_CFG_TX_ON | LAN9118_TX_CFG_STOP_TX));
    648 
    649 	/* Purge TX Status/Data FIFOs */
    650 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    651 	    LAN9118_TX_CFG_TXS_DUMP | LAN9118_TX_CFG_TXD_DUMP);
    652 
    653 	/* Stopping receiver, also clear TXEN */
    654 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    655 	cr &= ~(LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    656 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    657 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS) &
    658 	    LAN9118_INT_RXSTOP_INT));
    659 
    660 	/* Clear RX Status/Data FIFOs */
    661 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    662 	    LAN9118_RX_CFG_RX_DUMP);
    663 
    664 	callout_stop(&sc->sc_tick);
    665 }
    666 
    667 static void
    668 lan9118_watchdog(struct ifnet *ifp)
    669 {
    670 	struct lan9118_softc *sc = ifp->if_softc;
    671 
    672 	/*
    673 	 * Reclaim first as there is a possibility of losing Tx completion
    674 	 * interrupts.
    675 	 */
    676 	lan9118_txintr(sc);
    677 
    678 	aprint_error_ifnet(ifp, "watchdog timeout\n");
    679 	ifp->if_oerrors++;
    680 
    681 	lan9118_init(ifp);
    682 }
    683 
    684 
    685 static int
    686 lan9118_ifm_change(struct ifnet *ifp)
    687 {
    688 	struct lan9118_softc *sc = ifp->if_softc;
    689 	struct mii_data *mii = &sc->sc_mii;
    690 	struct ifmedia *ifm = &mii->mii_media;
    691 	struct ifmedia_entry *ife = ifm->ifm_cur;
    692 	uint32_t pmt_ctrl;
    693 
    694 	DPRINTFN(3, ("%s: ifm inst %d\n", __func__, IFM_INST(ife->ifm_media)));
    695 
    696 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    697 	    LAN9118_HW_CFG_MBO | LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    698 	delay(1);	/* Wait 5 cycle */
    699 
    700 	if (IFM_INST(ife->ifm_media) != 0) {
    701 		/* Use External PHY */
    702 
    703 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    704 		    LAN9118_HW_CFG_MBO			|
    705 		    LAN9118_HW_CFG_PHY_CLK_SEL_EMII	|
    706 		    LAN9118_HW_CFG_SMI_SEL		|
    707 		    LAN9118_HW_CFG_EXT_PHY_EN);
    708 		delay(1);
    709 		return mii_mediachg(&sc->sc_mii);
    710 	}
    711 
    712 	/* Setup Internal PHY */
    713 
    714 	mii->mii_media_status = IFM_AVALID;
    715 	mii->mii_media_active = IFM_ETHER;
    716 
    717 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    718 	    LAN9118_HW_CFG_MBO |
    719 	    LAN9118_HW_CFG_PHY_CLK_SEL_IPHY);
    720 	delay(1);
    721 
    722 	/* Reset PHY */
    723 	pmt_ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL);
    724 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL,
    725 	    pmt_ctrl | LAN9118_PMT_CTRL_PHY_RST);
    726 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    727 	    LAN9118_PMT_CTRL_PHY_RST);
    728 
    729 	mii_mediachg(&sc->sc_mii);
    730 	return 0;
    731 }
    732 
    733 static void
    734 lan9118_ifm_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    735 {
    736 	struct lan9118_softc *sc = ifp->if_softc;
    737 	struct mii_data *mii = &sc->sc_mii;
    738 
    739 	DPRINTFN(3, ("%s\n", __func__));
    740 
    741 	mii_pollstat(mii);
    742 	ifmr->ifm_active = mii->mii_media_active;
    743 	ifmr->ifm_status = mii->mii_media_status;
    744 }
    745 
    746 
    747 static int
    748 lan9118_miibus_readreg(device_t dev, int phy, int reg)
    749 {
    750 
    751 	return lan9118_mii_readreg(device_private(dev), phy, reg);
    752 }
    753 static void
    754 lan9118_miibus_writereg(device_t dev, int phy, int reg, int val)
    755 {
    756 
    757 	lan9118_mii_writereg(device_private(dev), phy, reg, val);
    758 }
    759 
    760 static void
    761 lan9118_miibus_statchg(struct ifnet *ifp)
    762 {
    763 	struct lan9118_softc *sc = ifp->if_softc;
    764 	u_int cr;
    765 
    766 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    767 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    768 		cr &= ~LAN9118_MAC_CR_RCVOWN;
    769 		cr |= LAN9118_MAC_CR_FDPX;
    770 	} else {
    771 		cr |= LAN9118_MAC_CR_RCVOWN;
    772 		cr &= ~LAN9118_MAC_CR_FDPX;
    773 	}
    774 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    775 }
    776 
    777 
    778 static uint16_t
    779 lan9118_mii_readreg(struct lan9118_softc *sc, int phy, int reg)
    780 {
    781 	uint32_t acc;
    782 
    783 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    784 	    LAN9118_MII_ACC_MIIBZY);
    785 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg);
    786 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    787 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    788 	    LAN9118_MII_ACC_MIIBZY);
    789 	return lan9118_mac_readreg(sc, LAN9118_MII_DATA);
    790 }
    791 
    792 static void
    793 lan9118_mii_writereg(struct lan9118_softc *sc, int phy, int reg, uint16_t val)
    794 {
    795 	uint32_t acc;
    796 
    797 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    798 	    LAN9118_MII_ACC_MIIBZY);
    799 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg) |
    800 	    LAN9118_MII_ACC_MIIWNR;
    801 	lan9118_mac_writereg(sc, LAN9118_MII_DATA, val);
    802 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    803 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    804 	    LAN9118_MII_ACC_MIIBZY);
    805 }
    806 
    807 static uint32_t
    808 lan9118_mac_readreg(struct lan9118_softc *sc, int reg)
    809 {
    810 	uint32_t cmd;
    811 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    812 
    813 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    814 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_R | reg);
    815 	do {
    816 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    817 		    LAN9118_MAC_CSR_CMD);
    818 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    819 			break;
    820 		delay(100);
    821 	} while (timo -= 100);
    822 	if (timo <= 0)
    823 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    824 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA);
    825 }
    826 
    827 static void
    828 lan9118_mac_writereg(struct lan9118_softc *sc, int reg, uint32_t val)
    829 {
    830 	uint32_t cmd;
    831 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    832 
    833 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA, val);
    834 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    835 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_W | reg);
    836 	do {
    837 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    838 		    LAN9118_MAC_CSR_CMD);
    839 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    840 			break;
    841 		delay(100);
    842 	} while (timo -= 100);
    843 	if (timo <= 0)
    844 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    845 }
    846 
    847 
    848 static void
    849 lan9118_set_filter(struct lan9118_softc *sc)
    850 {
    851 	struct ether_multistep step;
    852 	struct ether_multi *enm;
    853 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    854 	uint32_t mac_cr, h, hashes[2] = { 0, 0 };
    855 
    856 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    857 	if (ifp->if_flags & IFF_PROMISC) {
    858 		lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    859 		    mac_cr | LAN9118_MAC_CR_PRMS);
    860 		return;
    861 	}
    862 
    863 	mac_cr &= ~(LAN9118_MAC_CR_PRMS | LAN9118_MAC_CR_MCPAS |
    864 	    LAN9118_MAC_CR_BCAST | LAN9118_MAC_CR_HPFILT);
    865 	if (!(ifp->if_flags & IFF_BROADCAST))
    866 		mac_cr |= LAN9118_MAC_CR_BCAST;
    867 
    868 	if (ifp->if_flags & IFF_ALLMULTI)
    869 		mac_cr |= LAN9118_MAC_CR_MCPAS;
    870 	else {
    871 		ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    872 		while (enm != NULL) {
    873 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    874 			    ETHER_ADDR_LEN) != 0) {
    875 				/*
    876 				 * We must listen to a range of multicast
    877 				 * addresses.  For now, just accept all
    878 				 * multicasts, rather than trying to set
    879 				 * only those filter bits needed to match
    880 				 * the range.  (At this time, the only use
    881 				 * of address ranges is for IP multicast
    882 				 * routing, for which the range is big enough
    883 				 * to require all bits set.)
    884 				 */
    885 				ifp->if_flags |= IFF_ALLMULTI;
    886 				mac_cr |= LAN9118_MAC_CR_MCPAS;
    887 				break;
    888 			}
    889 			h = ether_crc32_le(enm->enm_addrlo,
    890 			    ETHER_ADDR_LEN) >> 26;
    891 			hashes[h >> 5] |= 1 << (h & 0x1f);
    892 
    893 			mac_cr |= LAN9118_MAC_CR_HPFILT;
    894 			ETHER_NEXT_MULTI(step, enm);
    895 		}
    896 		if (mac_cr & LAN9118_MAC_CR_HPFILT) {
    897 			lan9118_mac_writereg(sc, LAN9118_HASHH, hashes[1]);
    898 			lan9118_mac_writereg(sc, LAN9118_HASHL, hashes[0]);
    899 		}
    900 	}
    901 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, mac_cr);
    902 	return;
    903 }
    904 
    905 static void
    906 lan9118_rxintr(struct lan9118_softc *sc)
    907 {
    908 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    909 	struct mbuf *m;
    910 	uint32_t rx_fifo_inf, rx_status;
    911 	int pktlen;
    912 	const int pad = ETHER_HDR_LEN % sizeof(uint32_t);
    913 
    914 	DPRINTFN(3, ("%s\n", __func__));
    915 
    916 	for (;;) {
    917 		rx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    918 		    LAN9118_RX_FIFO_INF);
    919 		if (LAN9118_RX_FIFO_INF_RXSUSED(rx_fifo_inf) == 0)
    920 			break;
    921 
    922 		rx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    923 		    LAN9118_RXSFIFOP);
    924 		pktlen = LAN9118_RXS_PKTLEN(rx_status);
    925 		DPRINTFN(3, ("%s: rx_status=0x%x(pktlen %d)\n",
    926 		    __func__, rx_status, pktlen));
    927 		if (rx_status & (LAN9118_RXS_ES | LAN9118_RXS_LENERR |
    928 		    LAN9118_RXS_RWTO | LAN9118_RXS_MIIERR | LAN9118_RXS_DBIT)) {
    929 			if (rx_status & LAN9118_RXS_LENERR)
    930 				aprint_error_dev(sc->sc_dev, "Length Error\n");
    931 			if (rx_status & LAN9118_RXS_RUNTF)
    932 				aprint_error_dev(sc->sc_dev, "Runt Frame\n");
    933 			if (rx_status & LAN9118_RXS_FTL)
    934 				aprint_error_dev(sc->sc_dev,
    935 				    "Frame Too Long\n");
    936 			if (rx_status & LAN9118_RXS_RWTO)
    937 				aprint_error_dev(sc->sc_dev,
    938 				    "Receive Watchdog time-out\n");
    939 			if (rx_status & LAN9118_RXS_MIIERR)
    940 				aprint_error_dev(sc->sc_dev, "MII Error\n");
    941 			if (rx_status & LAN9118_RXS_DBIT)
    942 				aprint_error_dev(sc->sc_dev, "Drabbling Bit\n");
    943 			if (rx_status & LAN9118_RXS_COLS)
    944 				aprint_error_dev(sc->sc_dev,
    945 				    "Collision Seen\n");
    946 			if (rx_status & LAN9118_RXS_CRCERR)
    947 				aprint_error_dev(sc->sc_dev, "CRC Error\n");
    948 
    949 dropit:
    950 			ifp->if_ierrors++;
    951 			/*
    952 			 * Receive Data FIFO Fast Forward
    953 			 * When performing a fast-forward, there must be at
    954 			 * least 4 DWORDs of data in the RX data FIFO for the
    955 			 * packet being discarded.
    956 			 */
    957 			if (pktlen >= 4 * sizeof(uint32_t)) {
    958 				uint32_t rx_dp_ctl;
    959 
    960 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    961 				    LAN9118_RX_DP_CTL,
    962 				    LAN9118_RX_DP_CTL_RX_FFWD);
    963 				/* DP_FFWD bit is self clearing */
    964 				do {
    965 					rx_dp_ctl = bus_space_read_4(sc->sc_iot,
    966 					    sc->sc_ioh, LAN9118_RX_DP_CTL);
    967 				} while (rx_dp_ctl & LAN9118_RX_DP_CTL_RX_FFWD);
    968 			} else {
    969 				/* For less than 4 DWORDs do not use RX_FFWD. */
    970 				uint32_t garbage[4];
    971 
    972 				bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
    973 				    LAN9118_RXDFIFOP, garbage,
    974 				    roundup(pktlen, 4) >> 2);
    975 			}
    976 			continue;
    977 		}
    978 
    979 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    980 		if (m == NULL)
    981 			goto dropit;
    982 		if (pktlen > (MHLEN - pad)) {
    983 			MCLGET(m, M_DONTWAIT);
    984 			if ((m->m_flags & M_EXT) == 0) {
    985 				m_freem(m);
    986 				goto dropit;
    987 			}
    988 		}
    989 
    990 		/* STRICT_ALIGNMENT */
    991 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    992 		    LAN9118_RX_CFG_RXEA_4B | LAN9118_RX_CFG_RXDOFF(pad));
    993 		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, LAN9118_RXDFIFOP,
    994 		    mtod(m, uint32_t *),
    995 		    roundup(pad + pktlen, sizeof(uint32_t)) >> 2);
    996 		m->m_data += pad;
    997 
    998 		ifp->if_ipackets++;
    999 		m->m_pkthdr.rcvif = ifp;
   1000 		m->m_pkthdr.len = m->m_len = (pktlen - ETHER_CRC_LEN);
   1001 
   1002 		/*
   1003 		 * Pass this up to any BPF listeners, but only
   1004 		 * pass if up the stack if it's for us.
   1005 		 */
   1006 		bpf_mtap(ifp, m);
   1007 
   1008 		/* Pass it on. */
   1009 		(*ifp->if_input)(ifp, m);
   1010 	}
   1011 }
   1012 
   1013 static void
   1014 lan9118_txintr(struct lan9118_softc *sc)
   1015 {
   1016 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1017 	uint32_t tx_fifo_inf, tx_status;
   1018 	int fdx = IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX;
   1019 	int tdfree;
   1020 
   1021 	DPRINTFN(3, ("%s\n", __func__));
   1022 
   1023 	for (;;) {
   1024 		tx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1025 		    LAN9118_TX_FIFO_INF);
   1026 		if (LAN9118_TX_FIFO_INF_TXSUSED(tx_fifo_inf) == 0)
   1027 			break;
   1028 
   1029 		tx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1030 		    LAN9118_TXSFIFOP);
   1031 		DPRINTFN(3, ("%s: tx_status=0x%x\n", __func__, tx_status));
   1032 		if (tx_status & LAN9118_TXS_ES) {
   1033 			if (tx_status & LAN9118_TXS_LOC)
   1034 				aprint_error_dev(sc->sc_dev,
   1035 				    "Loss Of Carrier\n");
   1036 			if ((tx_status & LAN9118_TXS_NC) && !fdx)
   1037 				aprint_error_dev(sc->sc_dev, "No Carrier\n");
   1038 			if (tx_status & LAN9118_TXS_LCOL)
   1039 				aprint_error_dev(sc->sc_dev,
   1040 				    "Late Collision\n");
   1041 			if (tx_status & LAN9118_TXS_ECOL) {
   1042 				/* Rearch 16 collision */
   1043 				ifp->if_collisions += 16;
   1044 				aprint_error_dev(sc->sc_dev,
   1045 				    "Excessive Collision\n");
   1046 			}
   1047 			if (LAN9118_TXS_COLCNT(tx_status) != 0)
   1048 				aprint_error_dev(sc->sc_dev,
   1049 				    "Collision Count: %d\n",
   1050 				    LAN9118_TXS_COLCNT(tx_status));
   1051 			if (tx_status & LAN9118_TXS_ED)
   1052 				aprint_error_dev(sc->sc_dev,
   1053 				    "Excessive Deferral\n");
   1054 			if (tx_status & LAN9118_TXS_DEFERRED)
   1055 				aprint_error_dev(sc->sc_dev, "Deferred\n");
   1056 			ifp->if_oerrors++;
   1057 		} else
   1058 			ifp->if_opackets++;
   1059 	}
   1060 
   1061 	tdfree = LAN9118_TX_FIFO_INF_TDFREE(tx_fifo_inf);
   1062 	if (tdfree == LAN9118_TX_DATA_FIFO_SIZE)
   1063 		/* FIFO empty */
   1064 		ifp->if_timer = 0;
   1065 	if (tdfree >= 2036)
   1066 		/*
   1067 		 * 2036 is the possible maximum FIFO consumption
   1068 		 * for the most fragmented frame.
   1069 		 */
   1070 		ifp->if_flags &= ~IFF_OACTIVE;
   1071 }
   1072 
   1073 void
   1074 lan9118_tick(void *v)
   1075 {
   1076 	struct lan9118_softc *sc = v;
   1077 	int s;
   1078 
   1079 	s = splnet();
   1080 	mii_tick(&sc->sc_mii);
   1081 	callout_schedule(&sc->sc_tick, hz);
   1082 	splx(s);
   1083 }
   1084