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