Home | History | Annotate | Line # | Download | only in ic
lan9118.c revision 1.33
      1 /*	$NetBSD: lan9118.c,v 1.33 2019/05/23 13:10:51 msaitoh 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.33 2019/05/23 13:10:51 msaitoh 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 	sc->sc_ec.ec_mii = &sc->sc_mii;
    236 	ifmedia_init(&sc->sc_mii.mii_media, 0,
    237 	    lan9118_ifm_change, lan9118_ifm_status);
    238 	sc->sc_mii.mii_ifp = ifp;
    239 	sc->sc_mii.mii_readreg = lan9118_miibus_readreg;
    240 	sc->sc_mii.mii_writereg = lan9118_miibus_writereg;
    241 	sc->sc_mii.mii_statchg = lan9118_miibus_statchg;
    242 
    243 	/*
    244 	 * Number of instance of Internal PHY is always 0.  External PHY
    245 	 * number that above.
    246 	 */
    247 	mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff, 1, MII_OFFSET_ANY, 0);
    248 
    249 	if (sc->sc_id == LAN9118_ID_9115 || sc->sc_id == LAN9118_ID_9117 ||
    250 	    sc->sc_id == LAN9218_ID_9215 || sc->sc_id == LAN9218_ID_9217) {
    251 		if (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG) &
    252 		    LAN9118_HW_CFG_EXT_PHY_DET) {
    253 			/*
    254 			 * We always have a internal PHY at phy1.
    255 			 * In addition, external PHY is attached.
    256 			 */
    257 			DPRINTFN(1, ("%s: detect External PHY\n", __func__));
    258 
    259 			/* Switch MII and SMI */
    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_CD);
    264 			delay(1);	/* Wait 5 cycle */
    265 			bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    266 			    LAN9118_HW_CFG,
    267 			    LAN9118_HW_CFG_MBO |
    268 			    LAN9118_HW_CFG_PHY_CLK_SEL_EMII |
    269 			    LAN9118_HW_CFG_SMI_SEL |
    270 			    LAN9118_HW_CFG_EXT_PHY_EN);
    271 			delay(1);	/* Once wait more 5 cycle */
    272 
    273 			/* Call mii_attach, avoid at phy1. */
    274 			mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    275 			    0, MII_OFFSET_ANY, 0);
    276 			for (i = 2; i < MII_NPHY; i++)
    277 				mii_attach(sc->sc_dev, &sc->sc_mii, 0xffffffff,
    278 				    i, MII_OFFSET_ANY, 0);
    279 		}
    280 	}
    281 
    282 	ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER | IFM_AUTO);
    283 
    284 	/* Attach the interface. */
    285 	if_attach(ifp);
    286 	if_deferred_start_init(ifp, NULL);
    287 	ether_ifattach(ifp, sc->sc_enaddr);
    288 
    289 	callout_init(&sc->sc_tick, 0);
    290 
    291 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    292 	    RND_TYPE_NET, RND_FLAG_DEFAULT);
    293 	return 0;
    294 }
    295 
    296 int
    297 lan9118_intr(void *arg)
    298 {
    299 	struct lan9118_softc *sc = (struct lan9118_softc *)arg;
    300 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    301 	uint32_t int_sts, int_en, datum = 0;
    302 	int handled = 0;
    303 
    304 	for (;;) {
    305 		int_sts =
    306 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS);
    307 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    308 		    int_sts);
    309 		int_en =
    310 		    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN) |
    311 		    LAN9118_INT_SW_INT;
    312 
    313 		DPRINTFN(3, ("%s: int_sts=0x%x, int_en=0x%x\n",
    314 		    __func__, int_sts, int_en));
    315 
    316 		if (!(int_sts & int_en))
    317 			break;
    318 		datum = int_sts;
    319 		handled = 1;
    320 
    321 #if 0	/* not yet... */
    322 		if (int_sts & LAN9118_INT_PHY_INT) { /* PHY */
    323 			/* Shall we need? */
    324 		}
    325 		if (int_sts & LAN9118_INT_PME_INT) { /*Power Management Event*/
    326 			/* not yet... */
    327 		}
    328 #endif
    329 		if (int_sts & LAN9118_INT_RXE) {
    330 			ifp->if_ierrors++;
    331 			aprint_error_ifnet(ifp, "Receive Error\n");
    332 		}
    333 		if (int_sts & (LAN9118_INT_TSFL|LAN9118_INT_SW_INT)) /* TX Status FIFO Level */
    334 			lan9118_txintr(sc);
    335 		if (int_sts & LAN9118_INT_RXDF_INT) {
    336 			ifp->if_ierrors++;
    337 			aprint_error_ifnet(ifp, "RX Dropped Frame Interrupt\n");
    338 		}
    339 		if (int_sts & LAN9118_INT_RSFF) {
    340 			ifp->if_ierrors++;
    341 			aprint_error_ifnet(ifp, "RX Status FIFO Full\n");
    342 		}
    343 		if (int_sts & LAN9118_INT_RSFL) /* RX Status FIFO Level */
    344 			lan9118_rxintr(sc);
    345 	}
    346 
    347 	if (handled)
    348 		if_schedule_deferred_start(ifp);
    349 
    350 	rnd_add_uint32(&sc->rnd_source, datum);
    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, int_en;
    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 		/*
    458 		 * Pass the packet to any BPF listeners.
    459 		 */
    460 		bpf_mtap(ifp, m0, BPF_D_OUT);
    461 
    462 		m_freem(m0);
    463 	}
    464 
    465 	if (totlen > 0) {
    466 		ifp->if_timer = 5;
    467 
    468 		/*
    469 		 * Trigger a software interrupt to catch any missed completion
    470 		 * interrupts.
    471 		 */
    472 		int_en = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    473 		    LAN9118_INT_EN);
    474 		int_en |= LAN9118_INT_SW_INT;
    475 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN,
    476 		    int_en);
    477 	}
    478 }
    479 
    480 static int
    481 lan9118_ioctl(struct ifnet *ifp, u_long command, void *data)
    482 {
    483 	struct lan9118_softc *sc = ifp->if_softc;
    484 	int s, error = 0;
    485 
    486 	s = splnet();
    487 
    488 	switch (command) {
    489 	case SIOCSIFFLAGS:
    490 		DPRINTFN(2, ("%s: IFFLAGS\n", __func__));
    491 		if ((error = ifioctl_common(ifp, command, data)) != 0)
    492 			break;
    493 		switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
    494 		case IFF_RUNNING:
    495 			lan9118_stop(ifp, 0);
    496 			break;
    497 		case IFF_UP:
    498 			lan9118_init(ifp);
    499 			break;
    500 		case IFF_UP | IFF_RUNNING:
    501 			lan9118_set_filter(sc);
    502 			break;
    503 		default:
    504 			break;
    505 		}
    506 		error = 0;
    507 		break;
    508 
    509 	default:
    510 		DPRINTFN(2, ("%s: ETHER\n", __func__));
    511 		error = ether_ioctl(ifp, command, data);
    512 		if (error == ENETRESET) {
    513 			if (ifp->if_flags & IFF_RUNNING) {
    514 				lan9118_set_filter(sc);
    515 				DPRINTFN(2, ("%s set_filter called\n",
    516 				    __func__));
    517 			}
    518 			error = 0;
    519 		}
    520 		break;
    521 	}
    522 
    523 	splx(s);
    524 
    525 	return error;
    526 }
    527 
    528 static int
    529 lan9118_init(struct ifnet *ifp)
    530 {
    531 	struct lan9118_softc *sc = ifp->if_softc;
    532 	struct ifmedia *ifm = &sc->sc_mii.mii_media;
    533 	uint32_t reg, hw_cfg, mac_cr, irq_cfg;
    534 	int timo, s;
    535 
    536 	DPRINTFN(2, ("%s\n", __func__));
    537 
    538 	s = splnet();
    539 
    540 	/* wait for PMT_CTRL[READY] */
    541 	timo = mstohz(5000);	/* XXXX 5sec */
    542 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    543 	    LAN9118_PMT_CTRL_READY)) {
    544 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_BYTE_TEST,
    545 		    0xbad0c0de);
    546 		tsleep(&sc, PRIBIO, "lan9118_pmt_ready", 1);
    547 		if (--timo <= 0) {
    548 			splx(s);
    549 			return EBUSY;
    550 		}
    551 	}
    552 
    553 	/* Soft Reset */
    554 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    555 	    LAN9118_HW_CFG_SRST);
    556 	do {
    557 		reg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    558 		if (reg & LAN9118_HW_CFG_SRST_TO) {
    559 			aprint_error_dev(sc->sc_dev,
    560 			    "soft reset timeouted out\n");
    561 			splx(s);
    562 			return ETIMEDOUT;
    563 		}
    564 	} while (reg & LAN9118_HW_CFG_SRST);
    565 
    566 	/* Set MAC and PHY CSRs */
    567 
    568 	if (sc->sc_flags & LAN9118_FLAGS_SWAP)
    569 		/* need byte swap */
    570 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_WORD_SWAP,
    571 		    0xffffffff);
    572 
    573 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    574 	    LAN9118_E2P_CMD_EPCB);
    575 	if (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_E2P_CMD) &
    576 	    LAN9118_E2P_CMD_MACAL)) {
    577 		lan9118_mac_writereg(sc, LAN9118_ADDRL,
    578 		    sc->sc_enaddr[0] |
    579 		    sc->sc_enaddr[1] << 8 |
    580 		    sc->sc_enaddr[2] << 16 |
    581 		    sc->sc_enaddr[3] << 24);
    582 		lan9118_mac_writereg(sc, LAN9118_ADDRH,
    583 		    sc->sc_enaddr[4] | sc->sc_enaddr[5] << 8);
    584 	}
    585 
    586 	if (ifm->ifm_media & IFM_FLOW) {
    587 		lan9118_mac_writereg(sc, LAN9118_FLOW,
    588 		    LAN9118_FLOW_FCPT(1) | LAN9118_FLOW_FCEN);
    589 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_AFC_CFG,
    590 		    sc->sc_afc_cfg);
    591 	}
    592 
    593 	lan9118_ifm_change(ifp);
    594 	hw_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG);
    595 	hw_cfg &= ~LAN9118_HW_CFG_TX_FIF_MASK;
    596 	hw_cfg |= LAN9118_HW_CFG_TX_FIF_SZ(LAN9118_TX_FIF_SZ);
    597 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG, hw_cfg);
    598 
    599 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_GPIO_CFG,
    600 	    LAN9118_GPIO_CFG_LEDX_EN(2)	 |
    601 	    LAN9118_GPIO_CFG_LEDX_EN(1)	 |
    602 	    LAN9118_GPIO_CFG_LEDX_EN(0)	 |
    603 	    LAN9118_GPIO_CFG_GPIOBUFN(2) |
    604 	    LAN9118_GPIO_CFG_GPIOBUFN(1) |
    605 	    LAN9118_GPIO_CFG_GPIOBUFN(0));
    606 
    607 	irq_cfg = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG);
    608 	irq_cfg |= LAN9118_IRQ_CFG_IRQ_EN;
    609 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_IRQ_CFG, irq_cfg);
    610 
    611 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS,
    612 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS));
    613 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_FIFO_INT,
    614 	    LAN9118_FIFO_INT_TXSL(0) | LAN9118_FIFO_INT_RXSL(0));
    615 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN,
    616 #if 0	/* not yet... */
    617 	    LAN9118_INT_PHY_INT | /* PHY */
    618 	    LAN9118_INT_PME_INT | /* Power Management Event */
    619 #endif
    620 	    LAN9118_INT_RXE	| /* Receive Error */
    621 	    LAN9118_INT_TSFL	| /* TX Status FIFO Level */
    622 	    LAN9118_INT_RXDF_INT| /* RX Dropped Frame Interrupt */
    623 	    LAN9118_INT_RSFF	| /* RX Status FIFO Full */
    624 	    LAN9118_INT_RSFL);	  /* RX Status FIFO Level */
    625 
    626 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    627 	    LAN9118_RX_CFG_RXDOFF(2));
    628 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    629 	    LAN9118_TX_CFG_TX_ON);
    630 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    631 	lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    632 	    mac_cr | LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    633 
    634 	lan9118_set_filter(sc);
    635 
    636 	ifp->if_flags |= IFF_RUNNING;
    637 	ifp->if_flags &= ~IFF_OACTIVE;
    638 
    639 	callout_reset(&sc->sc_tick, hz, lan9118_tick, sc);
    640 
    641 	splx(s);
    642 
    643 	return 0;
    644 }
    645 
    646 static void
    647 lan9118_stop(struct ifnet *ifp, int disable)
    648 {
    649 	struct lan9118_softc *sc = ifp->if_softc;
    650 	uint32_t cr;
    651 
    652 	DPRINTFN(2, ("%s\n", __func__));
    653 
    654 	/* Disable IRQ */
    655 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_EN, 0);
    656 
    657 	/* Stopping transmitter */
    658 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    659 	    LAN9118_TX_CFG_STOP_TX);
    660 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG) &
    661 	    (LAN9118_TX_CFG_TX_ON | LAN9118_TX_CFG_STOP_TX));
    662 
    663 	/* Purge TX Status/Data FIFOs */
    664 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_TX_CFG,
    665 	    LAN9118_TX_CFG_TXS_DUMP | LAN9118_TX_CFG_TXD_DUMP);
    666 
    667 	/* Stopping receiver, also clear TXEN */
    668 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    669 	cr &= ~(LAN9118_MAC_CR_TXEN | LAN9118_MAC_CR_RXEN);
    670 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    671 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_INT_STS) &
    672 	    LAN9118_INT_RXSTOP_INT));
    673 
    674 	/* Clear RX Status/Data FIFOs */
    675 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
    676 	    LAN9118_RX_CFG_RX_DUMP);
    677 
    678 	callout_stop(&sc->sc_tick);
    679 }
    680 
    681 static void
    682 lan9118_watchdog(struct ifnet *ifp)
    683 {
    684 	struct lan9118_softc *sc = ifp->if_softc;
    685 
    686 	/*
    687 	 * Reclaim first as there is a possibility of losing Tx completion
    688 	 * interrupts.
    689 	 */
    690 	lan9118_txintr(sc);
    691 
    692 	aprint_error_ifnet(ifp, "watchdog timeout\n");
    693 	ifp->if_oerrors++;
    694 
    695 	lan9118_init(ifp);
    696 }
    697 
    698 
    699 static int
    700 lan9118_ifm_change(struct ifnet *ifp)
    701 {
    702 	struct lan9118_softc *sc = ifp->if_softc;
    703 	struct mii_data *mii = &sc->sc_mii;
    704 	struct ifmedia *ifm = &mii->mii_media;
    705 	struct ifmedia_entry *ife = ifm->ifm_cur;
    706 	uint32_t pmt_ctrl;
    707 
    708 	DPRINTFN(3, ("%s: ifm inst %d\n", __func__, IFM_INST(ife->ifm_media)));
    709 
    710 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    711 	    LAN9118_HW_CFG_MBO | LAN9118_HW_CFG_PHY_CLK_SEL_CD);
    712 	delay(1);	/* Wait 5 cycle */
    713 
    714 	if (IFM_INST(ife->ifm_media) != 0) {
    715 		/* Use External PHY */
    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_EMII	|
    720 		    LAN9118_HW_CFG_SMI_SEL		|
    721 		    LAN9118_HW_CFG_EXT_PHY_EN);
    722 		delay(1);
    723 		return mii_mediachg(&sc->sc_mii);
    724 	}
    725 
    726 	/* Setup Internal PHY */
    727 
    728 	mii->mii_media_status = IFM_AVALID;
    729 	mii->mii_media_active = IFM_ETHER;
    730 
    731 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_HW_CFG,
    732 	    LAN9118_HW_CFG_MBO |
    733 	    LAN9118_HW_CFG_PHY_CLK_SEL_IPHY);
    734 	delay(1);
    735 
    736 	/* Reset PHY */
    737 	pmt_ctrl = bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL);
    738 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL,
    739 	    pmt_ctrl | LAN9118_PMT_CTRL_PHY_RST);
    740 	while (bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_PMT_CTRL) &
    741 	    LAN9118_PMT_CTRL_PHY_RST);
    742 
    743 	mii_mediachg(&sc->sc_mii);
    744 	return 0;
    745 }
    746 
    747 static void
    748 lan9118_ifm_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    749 {
    750 	struct lan9118_softc *sc = ifp->if_softc;
    751 	struct mii_data *mii = &sc->sc_mii;
    752 
    753 	DPRINTFN(3, ("%s\n", __func__));
    754 
    755 	mii_pollstat(mii);
    756 	ifmr->ifm_active = mii->mii_media_active;
    757 	ifmr->ifm_status = mii->mii_media_status;
    758 }
    759 
    760 
    761 static int
    762 lan9118_miibus_readreg(device_t dev, int phy, int reg, uint16_t *val)
    763 {
    764 
    765 	return lan9118_mii_readreg(device_private(dev), phy, reg, val);
    766 }
    767 static int
    768 lan9118_miibus_writereg(device_t dev, int phy, int reg, uint16_t val)
    769 {
    770 
    771 	return lan9118_mii_writereg(device_private(dev), phy, reg, val);
    772 }
    773 
    774 static void
    775 lan9118_miibus_statchg(struct ifnet *ifp)
    776 {
    777 	struct lan9118_softc *sc = ifp->if_softc;
    778 	u_int cr;
    779 
    780 	cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    781 	if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) {
    782 		cr &= ~LAN9118_MAC_CR_RCVOWN;
    783 		cr |= LAN9118_MAC_CR_FDPX;
    784 	} else {
    785 		cr |= LAN9118_MAC_CR_RCVOWN;
    786 		cr &= ~LAN9118_MAC_CR_FDPX;
    787 	}
    788 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, cr);
    789 }
    790 
    791 
    792 static int
    793 lan9118_mii_readreg(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_mac_writereg(sc, LAN9118_MII_ACC, acc);
    801 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    802 	    LAN9118_MII_ACC_MIIBZY);
    803 	*val = lan9118_mac_readreg(sc, LAN9118_MII_DATA) & 0xffff;
    804 	return 0;
    805 }
    806 
    807 static int
    808 lan9118_mii_writereg(struct lan9118_softc *sc, int phy, int reg, uint16_t val)
    809 {
    810 	uint32_t acc;
    811 
    812 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    813 	    LAN9118_MII_ACC_MIIBZY);
    814 	acc = LAN9118_MII_ACC_PHYA(phy) | LAN9118_MII_ACC_MIIRINDA(reg) |
    815 	    LAN9118_MII_ACC_MIIWNR;
    816 	lan9118_mac_writereg(sc, LAN9118_MII_DATA, val);
    817 	lan9118_mac_writereg(sc, LAN9118_MII_ACC, acc);
    818 	while (lan9118_mac_readreg(sc, LAN9118_MII_ACC) &
    819 	    LAN9118_MII_ACC_MIIBZY);
    820 
    821 	return 0;
    822 }
    823 
    824 static uint32_t
    825 lan9118_mac_readreg(struct lan9118_softc *sc, int reg)
    826 {
    827 	uint32_t cmd;
    828 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    829 
    830 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    831 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_R | reg);
    832 	do {
    833 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    834 		    LAN9118_MAC_CSR_CMD);
    835 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    836 			break;
    837 		delay(100);
    838 	} while (timo -= 100);
    839 	if (timo <= 0)
    840 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    841 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA);
    842 }
    843 
    844 static void
    845 lan9118_mac_writereg(struct lan9118_softc *sc, int reg, uint32_t val)
    846 {
    847 	uint32_t cmd;
    848 	int timo = 3 * 1000 * 1000;	/* XXXX: 3sec */
    849 
    850 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_DATA, val);
    851 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_MAC_CSR_CMD,
    852 	    LAN9118_MAC_CSR_CMD_BUSY | LAN9118_MAC_CSR_CMD_W | reg);
    853 	do {
    854 		cmd = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    855 		    LAN9118_MAC_CSR_CMD);
    856 		if (!(cmd & LAN9118_MAC_CSR_CMD_BUSY))
    857 			break;
    858 		delay(100);
    859 	} while (timo -= 100);
    860 	if (timo <= 0)
    861 		aprint_error_dev(sc->sc_dev, "%s: command busy\n", __func__);
    862 }
    863 
    864 
    865 static void
    866 lan9118_set_filter(struct lan9118_softc *sc)
    867 {
    868 	struct ether_multistep step;
    869 	struct ether_multi *enm;
    870 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    871 	uint32_t mac_cr, h, hashes[2] = { 0, 0 };
    872 
    873 	mac_cr = lan9118_mac_readreg(sc, LAN9118_MAC_CR);
    874 	if (ifp->if_flags & IFF_PROMISC) {
    875 		lan9118_mac_writereg(sc, LAN9118_MAC_CR,
    876 		    mac_cr | LAN9118_MAC_CR_PRMS);
    877 		return;
    878 	}
    879 
    880 	mac_cr &= ~(LAN9118_MAC_CR_PRMS | LAN9118_MAC_CR_MCPAS |
    881 	    LAN9118_MAC_CR_BCAST | LAN9118_MAC_CR_HPFILT);
    882 	if (!(ifp->if_flags & IFF_BROADCAST))
    883 		mac_cr |= LAN9118_MAC_CR_BCAST;
    884 
    885 	if (ifp->if_flags & IFF_ALLMULTI)
    886 		mac_cr |= LAN9118_MAC_CR_MCPAS;
    887 	else {
    888 		ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
    889 		while (enm != NULL) {
    890 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
    891 			    ETHER_ADDR_LEN) != 0) {
    892 				/*
    893 				 * We must listen to a range of multicast
    894 				 * addresses.  For now, just accept all
    895 				 * multicasts, rather than trying to set
    896 				 * only those filter bits needed to match
    897 				 * the range.  (At this time, the only use
    898 				 * of address ranges is for IP multicast
    899 				 * routing, for which the range is big enough
    900 				 * to require all bits set.)
    901 				 */
    902 				ifp->if_flags |= IFF_ALLMULTI;
    903 				mac_cr |= LAN9118_MAC_CR_MCPAS;
    904 				break;
    905 			}
    906 			h = ether_crc32_le(enm->enm_addrlo,
    907 			    ETHER_ADDR_LEN) >> 26;
    908 			hashes[h >> 5] |= 1 << (h & 0x1f);
    909 
    910 			mac_cr |= LAN9118_MAC_CR_HPFILT;
    911 			ETHER_NEXT_MULTI(step, enm);
    912 		}
    913 		if (mac_cr & LAN9118_MAC_CR_HPFILT) {
    914 			lan9118_mac_writereg(sc, LAN9118_HASHH, hashes[1]);
    915 			lan9118_mac_writereg(sc, LAN9118_HASHL, hashes[0]);
    916 		}
    917 	}
    918 	lan9118_mac_writereg(sc, LAN9118_MAC_CR, mac_cr);
    919 	return;
    920 }
    921 
    922 static void
    923 lan9118_rxintr(struct lan9118_softc *sc)
    924 {
    925 	struct ifnet *ifp = &sc->sc_ec.ec_if;
    926 	struct mbuf *m;
    927 	uint32_t rx_fifo_inf, rx_status;
    928 	int pktlen;
    929 	const int pad = ETHER_HDR_LEN % sizeof(uint32_t);
    930 
    931 	DPRINTFN(3, ("%s\n", __func__));
    932 
    933 	for (;;) {
    934 		rx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    935 		    LAN9118_RX_FIFO_INF);
    936 		if (LAN9118_RX_FIFO_INF_RXSUSED(rx_fifo_inf) == 0)
    937 			break;
    938 
    939 		rx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    940 		    LAN9118_RXSFIFOP);
    941 		pktlen = LAN9118_RXS_PKTLEN(rx_status);
    942 		DPRINTFN(3, ("%s: rx_status=0x%x(pktlen %d)\n",
    943 		    __func__, rx_status, pktlen));
    944 		if (rx_status & (LAN9118_RXS_ES | LAN9118_RXS_LENERR |
    945 		    LAN9118_RXS_RWTO | LAN9118_RXS_MIIERR | LAN9118_RXS_DBIT)) {
    946 			if (rx_status & LAN9118_RXS_LENERR)
    947 				aprint_error_dev(sc->sc_dev, "Length Error\n");
    948 			if (rx_status & LAN9118_RXS_RUNTF)
    949 				aprint_error_dev(sc->sc_dev, "Runt Frame\n");
    950 			if (rx_status & LAN9118_RXS_FTL)
    951 				aprint_error_dev(sc->sc_dev,
    952 				    "Frame Too Long\n");
    953 			if (rx_status & LAN9118_RXS_RWTO)
    954 				aprint_error_dev(sc->sc_dev,
    955 				    "Receive Watchdog time-out\n");
    956 			if (rx_status & LAN9118_RXS_MIIERR)
    957 				aprint_error_dev(sc->sc_dev, "MII Error\n");
    958 			if (rx_status & LAN9118_RXS_DBIT)
    959 				aprint_error_dev(sc->sc_dev, "Drabbling Bit\n");
    960 			if (rx_status & LAN9118_RXS_COLS)
    961 				aprint_error_dev(sc->sc_dev,
    962 				    "Collision Seen\n");
    963 			if (rx_status & LAN9118_RXS_CRCERR)
    964 				aprint_error_dev(sc->sc_dev, "CRC Error\n");
    965 
    966 dropit:
    967 			ifp->if_ierrors++;
    968 			/*
    969 			 * Receive Data FIFO Fast Forward
    970 			 * When performing a fast-forward, there must be at
    971 			 * least 4 DWORDs of data in the RX data FIFO for the
    972 			 * packet being discarded.
    973 			 */
    974 			if (pktlen >= 4 * sizeof(uint32_t)) {
    975 				uint32_t rx_dp_ctl;
    976 
    977 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    978 				    LAN9118_RX_DP_CTL,
    979 				    LAN9118_RX_DP_CTL_RX_FFWD);
    980 				/* DP_FFWD bit is self clearing */
    981 				do {
    982 					rx_dp_ctl = bus_space_read_4(sc->sc_iot,
    983 					    sc->sc_ioh, LAN9118_RX_DP_CTL);
    984 				} while (rx_dp_ctl & LAN9118_RX_DP_CTL_RX_FFWD);
    985 			} else {
    986 				/* For less than 4 DWORDs do not use RX_FFWD. */
    987 				uint32_t garbage[4];
    988 
    989 				bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh,
    990 				    LAN9118_RXDFIFOP, garbage,
    991 				    roundup(pktlen, 4) >> 2);
    992 			}
    993 			continue;
    994 		}
    995 
    996 		MGETHDR(m, M_DONTWAIT, MT_DATA);
    997 		if (m == NULL)
    998 			goto dropit;
    999 		if (pktlen > (MHLEN - pad)) {
   1000 			MCLGET(m, M_DONTWAIT);
   1001 			if ((m->m_flags & M_EXT) == 0) {
   1002 				m_freem(m);
   1003 				goto dropit;
   1004 			}
   1005 		}
   1006 
   1007 		/* STRICT_ALIGNMENT */
   1008 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, LAN9118_RX_CFG,
   1009 		    LAN9118_RX_CFG_RXEA_4B | LAN9118_RX_CFG_RXDOFF(pad));
   1010 		bus_space_read_multi_4(sc->sc_iot, sc->sc_ioh, LAN9118_RXDFIFOP,
   1011 		    mtod(m, uint32_t *),
   1012 		    roundup(pad + pktlen, sizeof(uint32_t)) >> 2);
   1013 		m->m_data += pad;
   1014 
   1015 		m_set_rcvif(m, ifp);
   1016 		m->m_pkthdr.len = m->m_len = (pktlen - ETHER_CRC_LEN);
   1017 
   1018 		/* Pass it on. */
   1019 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1020 	}
   1021 }
   1022 
   1023 static void
   1024 lan9118_txintr(struct lan9118_softc *sc)
   1025 {
   1026 	struct ifnet *ifp = &sc->sc_ec.ec_if;
   1027 	uint32_t tx_fifo_inf, tx_status;
   1028 	int fdx = IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX;
   1029 	int tdfree;
   1030 
   1031 	DPRINTFN(3, ("%s\n", __func__));
   1032 
   1033 	for (;;) {
   1034 		tx_fifo_inf = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1035 		    LAN9118_TX_FIFO_INF);
   1036 		if (LAN9118_TX_FIFO_INF_TXSUSED(tx_fifo_inf) == 0)
   1037 			break;
   1038 
   1039 		tx_status = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
   1040 		    LAN9118_TXSFIFOP);
   1041 		DPRINTFN(3, ("%s: tx_status=0x%x\n", __func__, tx_status));
   1042 		if (tx_status & LAN9118_TXS_ES) {
   1043 			if (tx_status & LAN9118_TXS_LOC)
   1044 				aprint_error_dev(sc->sc_dev,
   1045 				    "Loss Of Carrier\n");
   1046 			if ((tx_status & LAN9118_TXS_NC) && !fdx)
   1047 				aprint_error_dev(sc->sc_dev, "No Carrier\n");
   1048 			if (tx_status & LAN9118_TXS_LCOL)
   1049 				aprint_error_dev(sc->sc_dev,
   1050 				    "Late Collision\n");
   1051 			if (tx_status & LAN9118_TXS_ECOL) {
   1052 				/* Rearch 16 collision */
   1053 				ifp->if_collisions += 16;
   1054 				aprint_error_dev(sc->sc_dev,
   1055 				    "Excessive Collision\n");
   1056 			}
   1057 			if (LAN9118_TXS_COLCNT(tx_status) != 0)
   1058 				aprint_error_dev(sc->sc_dev,
   1059 				    "Collision Count: %d\n",
   1060 				    LAN9118_TXS_COLCNT(tx_status));
   1061 			if (tx_status & LAN9118_TXS_ED)
   1062 				aprint_error_dev(sc->sc_dev,
   1063 				    "Excessive Deferral\n");
   1064 			if (tx_status & LAN9118_TXS_DEFERRED)
   1065 				aprint_error_dev(sc->sc_dev, "Deferred\n");
   1066 			ifp->if_oerrors++;
   1067 		} else
   1068 			ifp->if_opackets++;
   1069 	}
   1070 
   1071 	tdfree = LAN9118_TX_FIFO_INF_TDFREE(tx_fifo_inf);
   1072 	if (tdfree == LAN9118_TX_DATA_FIFO_SIZE)
   1073 		/* FIFO empty */
   1074 		ifp->if_timer = 0;
   1075 	if (tdfree >= 2036)
   1076 		/*
   1077 		 * 2036 is the possible maximum FIFO consumption
   1078 		 * for the most fragmented frame.
   1079 		 */
   1080 		ifp->if_flags &= ~IFF_OACTIVE;
   1081 }
   1082 
   1083 void
   1084 lan9118_tick(void *v)
   1085 {
   1086 	struct lan9118_softc *sc = v;
   1087 	int s;
   1088 
   1089 	s = splnet();
   1090 	mii_tick(&sc->sc_mii);
   1091 	callout_schedule(&sc->sc_tick, hz);
   1092 	splx(s);
   1093 }
   1094