Home | History | Annotate | Line # | Download | only in xscale
ixp425_if_npe.c revision 1.16
      1 /*	$NetBSD: ixp425_if_npe.c,v 1.16 2009/03/11 16:30:20 msaitoh Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Sam Leffler.  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 WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include <sys/cdefs.h>
     28 #if 0
     29 __FBSDID("$FreeBSD: src/sys/arm/xscale/ixp425/if_npe.c,v 1.1 2006/11/19 23:55:23 sam Exp $");
     30 #endif
     31 __KERNEL_RCSID(0, "$NetBSD: ixp425_if_npe.c,v 1.16 2009/03/11 16:30:20 msaitoh Exp $");
     32 
     33 /*
     34  * Intel XScale NPE Ethernet driver.
     35  *
     36  * This driver handles the two ports present on the IXP425.
     37  * Packet processing is done by the Network Processing Engines
     38  * (NPE's) that work together with a MAC and PHY. The MAC
     39  * is also mapped to the XScale cpu; the PHY is accessed via
     40  * the MAC. NPE-XScale communication happens through h/w
     41  * queues managed by the Q Manager block.
     42  *
     43  * The code here replaces the ethAcc, ethMii, and ethDB classes
     44  * in the Intel Access Library (IAL) and the OS-specific driver.
     45  *
     46  * XXX add vlan support
     47  * XXX NPE-C port doesn't work yet
     48  */
     49 
     50 #include "bpfilter.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/device.h>
     56 #include <sys/callout.h>
     57 #include <sys/mbuf.h>
     58 #include <sys/malloc.h>
     59 #include <sys/socket.h>
     60 #include <sys/endian.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/syslog.h>
     63 
     64 #include <machine/bus.h>
     65 
     66 #include <net/if.h>
     67 #include <net/if_dl.h>
     68 #include <net/if_media.h>
     69 #include <net/if_ether.h>
     70 
     71 #if NBPFILTER > 0
     72 #include <net/bpf.h>
     73 #endif
     74 
     75 #include <arm/xscale/ixp425reg.h>
     76 #include <arm/xscale/ixp425var.h>
     77 #include <arm/xscale/ixp425_qmgr.h>
     78 #include <arm/xscale/ixp425_npevar.h>
     79 #include <arm/xscale/ixp425_if_npereg.h>
     80 
     81 #include <dev/mii/miivar.h>
     82 
     83 #include "locators.h"
     84 
     85 struct npebuf {
     86 	struct npebuf	*ix_next;	/* chain to next buffer */
     87 	void		*ix_m;		/* backpointer to mbuf */
     88 	bus_dmamap_t	ix_map;		/* bus dma map for associated data */
     89 	struct npehwbuf	*ix_hw;		/* associated h/w block */
     90 	uint32_t	ix_neaddr;	/* phys address of ix_hw */
     91 };
     92 
     93 struct npedma {
     94 	const char*	name;
     95 	int		nbuf;		/* # npebuf's allocated */
     96 	bus_dmamap_t	m_map;
     97 	struct npehwbuf	*hwbuf;		/* NPE h/w buffers */
     98 	bus_dmamap_t	buf_map;
     99 	bus_addr_t	buf_phys;	/* phys addr of buffers */
    100 	struct npebuf	*buf;		/* s/w buffers (1-1 w/ h/w) */
    101 };
    102 
    103 struct npe_softc {
    104 	struct device	sc_dev;
    105 	struct ethercom	sc_ethercom;
    106 	uint8_t		sc_enaddr[ETHER_ADDR_LEN];
    107 	struct mii_data	sc_mii;
    108 	bus_space_tag_t	sc_iot;
    109 	bus_dma_tag_t	sc_dt;
    110 	bus_space_handle_t sc_ioh;	/* MAC register window */
    111 	bus_space_handle_t sc_miih;	/* MII register window */
    112 	struct ixpnpe_softc *sc_npe;	/* NPE support */
    113 	int		sc_unit;
    114 	int		sc_phy;
    115 	struct callout	sc_tick_ch;	/* Tick callout */
    116 	struct npedma	txdma;
    117 	struct npebuf	*tx_free;	/* list of free tx buffers */
    118 	struct npedma	rxdma;
    119 	int		rx_qid;		/* rx qid */
    120 	int		rx_freeqid;	/* rx free buffers qid */
    121 	int		tx_qid;		/* tx qid */
    122 	int		tx_doneqid;	/* tx completed qid */
    123 	struct npestats	*sc_stats;
    124 	bus_dmamap_t	sc_stats_map;
    125 	bus_addr_t	sc_stats_phys;	/* phys addr of sc_stats */
    126 	int		sc_if_flags;	/* keep last if_flags */
    127 };
    128 
    129 /*
    130  * Per-unit static configuration for IXP425.  The tx and
    131  * rx free Q id's are fixed by the NPE microcode.  The
    132  * rx Q id's are programmed to be separate to simplify
    133  * multi-port processing.  It may be better to handle
    134  * all traffic through one Q (as done by the Intel drivers).
    135  *
    136  * Note that the PHY's are accessible only from MAC A
    137  * on the IXP425.  This and other platform-specific
    138  * assumptions probably need to be handled through hints.
    139  */
    140 static const struct {
    141 	const char	*desc;		/* device description */
    142 	int		npeid;		/* NPE assignment */
    143 	int		macport;	/* Port number of the MAC */
    144 	uint32_t	imageid;	/* NPE firmware image id */
    145 	uint32_t	regbase;
    146 	int		regsize;
    147 	uint32_t	miibase;
    148 	int		miisize;
    149 	uint8_t		rx_qid;
    150 	uint8_t		rx_freeqid;
    151 	uint8_t		tx_qid;
    152 	uint8_t		tx_doneqid;
    153 } npeconfig[NPE_PORTS_MAX] = {
    154 	{ .desc		= "IXP NPE-B",
    155 	  .npeid	= NPE_B,
    156 	  .macport	= 0x10,
    157 	  .imageid	= IXP425_NPE_B_IMAGEID,
    158 	  .regbase	= IXP425_MAC_A_HWBASE,
    159 	  .regsize	= IXP425_MAC_A_SIZE,
    160 	  .miibase	= IXP425_MAC_A_HWBASE,
    161 	  .miisize	= IXP425_MAC_A_SIZE,
    162 	  .rx_qid	= 4,
    163 	  .rx_freeqid	= 27,
    164 	  .tx_qid	= 24,
    165 	  .tx_doneqid	= 31
    166 	},
    167 	{ .desc		= "IXP NPE-C",
    168 	  .npeid	= NPE_C,
    169 	  .macport	= 0x20,
    170 	  .imageid	= IXP425_NPE_C_IMAGEID,
    171 	  .regbase	= IXP425_MAC_B_HWBASE,
    172 	  .regsize	= IXP425_MAC_B_SIZE,
    173 	  .miibase	= IXP425_MAC_A_HWBASE,
    174 	  .miisize	= IXP425_MAC_A_SIZE,
    175 	  .rx_qid	= 12,
    176 	  .rx_freeqid	= 28,
    177 	  .tx_qid	= 25,
    178 	  .tx_doneqid	= 31
    179 	},
    180 };
    181 static struct npe_softc *npes[NPE_MAX];	/* NB: indexed by npeid */
    182 
    183 static __inline uint32_t
    184 RD4(struct npe_softc *sc, bus_size_t off)
    185 {
    186 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh, off);
    187 }
    188 
    189 static __inline void
    190 WR4(struct npe_softc *sc, bus_size_t off, uint32_t val)
    191 {
    192 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, off, val);
    193 }
    194 
    195 static int	npe_activate(struct npe_softc *);
    196 #if 0
    197 static void	npe_deactivate(struct npe_softc *);
    198 #endif
    199 static void	npe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr);
    200 static void	npe_setmac(struct npe_softc *sc, const u_char *eaddr);
    201 static void	npe_getmac(struct npe_softc *sc);
    202 static void	npe_txdone(int qid, void *arg);
    203 static int	npe_rxbuf_init(struct npe_softc *, struct npebuf *,
    204 			struct mbuf *);
    205 static void	npe_rxdone(int qid, void *arg);
    206 static void	npeinit_macreg(struct npe_softc *);
    207 static int	npeinit(struct ifnet *);
    208 static void	npeinit_resetcb(void *);
    209 static void	npeinit_locked(void *);
    210 static void	npestart(struct ifnet *);
    211 static void	npestop(struct ifnet *, int);
    212 static void	npewatchdog(struct ifnet *);
    213 static int	npeioctl(struct ifnet * ifp, u_long, void *);
    214 
    215 static int	npe_setrxqosentry(struct npe_softc *, int classix,
    216 			int trafclass, int qid);
    217 static int	npe_updatestats(struct npe_softc *);
    218 #if 0
    219 static int	npe_getstats(struct npe_softc *);
    220 static uint32_t	npe_getimageid(struct npe_softc *);
    221 static int	npe_setloopback(struct npe_softc *, int ena);
    222 #endif
    223 
    224 static int	npe_miibus_readreg(struct device *, int, int);
    225 static void	npe_miibus_writereg(struct device *, int, int, int);
    226 static void	npe_miibus_statchg(struct device *);
    227 
    228 static int	npe_debug;
    229 #define DPRINTF(sc, fmt, ...) do {			\
    230 	if (npe_debug) printf(fmt, __VA_ARGS__);	\
    231 } while (0)
    232 #define DPRINTFn(n, sc, fmt, ...) do {			\
    233 	if (npe_debug >= n) printf(fmt, __VA_ARGS__);	\
    234 } while (0)
    235 
    236 #define	NPE_TXBUF	128
    237 #define	NPE_RXBUF	64
    238 
    239 #ifndef ETHER_ALIGN
    240 #define	ETHER_ALIGN	2	/* XXX: Ditch this */
    241 #endif
    242 
    243 #define MAC2UINT64(addr)	(((uint64_t)addr[0] << 40)	\
    244 				    + ((uint64_t)addr[1] << 32)	\
    245 				    + ((uint64_t)addr[2] << 24)	\
    246 				    + ((uint64_t)addr[3] << 16)	\
    247 				    + ((uint64_t)addr[4] << 8)	\
    248 				    + (uint64_t)addr[5])
    249 
    250 /* NB: all tx done processing goes through one queue */
    251 static int tx_doneqid = -1;
    252 
    253 void (*npe_getmac_md)(int, uint8_t *);
    254 
    255 static int npe_match(struct device *, struct cfdata *, void *);
    256 static void npe_attach(struct device *, struct device *, void *);
    257 
    258 CFATTACH_DECL(npe, sizeof(struct npe_softc),
    259     npe_match, npe_attach, NULL, NULL);
    260 
    261 static int
    262 npe_match(struct device *parent, struct cfdata *cf, void *arg)
    263 {
    264 	struct ixpnpe_attach_args *na = arg;
    265 
    266 	return (na->na_unit == NPE_B || na->na_unit == NPE_C);
    267 }
    268 
    269 static void
    270 npe_attach(struct device *parent, struct device *self, void *arg)
    271 {
    272 	struct npe_softc *sc = (void *)self;
    273 	struct ixpnpe_attach_args *na = arg;
    274 	struct ixpnpe_softc *isc = (struct ixpnpe_softc *)parent;
    275 	struct ifnet *ifp;
    276 
    277 	aprint_naive("\n");
    278 	aprint_normal(": Ethernet co-processor\n");
    279 
    280 	sc->sc_iot = na->na_iot;
    281 	sc->sc_dt = na->na_dt;
    282 	sc->sc_npe = na->na_npe;
    283 	sc->sc_unit = (na->na_unit == NPE_B) ? 0 : 1;
    284 	sc->sc_phy = na->na_phy;
    285 
    286 	memset(&sc->sc_ethercom, 0, sizeof(sc->sc_ethercom));
    287 	memset(&sc->sc_mii, 0, sizeof(sc->sc_mii));
    288 
    289 	callout_init(&sc->sc_tick_ch, 0);
    290 
    291 	if (npe_activate(sc)) {
    292 		aprint_error("%s: Failed to activate NPE (missing "
    293 		    "microcode?)\n", sc->sc_dev.dv_xname);
    294 		return;
    295 	}
    296 
    297 	npe_getmac(sc);
    298 	npeinit_macreg(sc);
    299 
    300 	aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
    301 	    ether_sprintf(sc->sc_enaddr));
    302 
    303 	ifp = &sc->sc_ethercom.ec_if;
    304 	sc->sc_mii.mii_ifp = ifp;
    305 	sc->sc_mii.mii_readreg = npe_miibus_readreg;
    306 	sc->sc_mii.mii_writereg = npe_miibus_writereg;
    307 	sc->sc_mii.mii_statchg = npe_miibus_statchg;
    308 	sc->sc_ethercom.ec_mii = &sc->sc_mii;
    309 
    310 	ifmedia_init(&sc->sc_mii.mii_media, IFM_IMASK, ether_mediachange,
    311 	    npe_ifmedia_status);
    312 
    313 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, MII_PHY_ANY,
    314 		    MII_OFFSET_ANY, MIIF_DOPAUSE);
    315 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    316 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    317 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    318 	} else
    319 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    320 
    321 	ifp->if_softc = sc;
    322 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    323 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    324 	ifp->if_start = npestart;
    325 	ifp->if_ioctl = npeioctl;
    326 	ifp->if_watchdog = npewatchdog;
    327 	ifp->if_init = npeinit;
    328 	ifp->if_stop = npestop;
    329 	IFQ_SET_READY(&ifp->if_snd);
    330 
    331 	/* VLAN capable */
    332 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    333 
    334 	if_attach(ifp);
    335 	ether_ifattach(ifp, sc->sc_enaddr);
    336 
    337 	/* callback function to reset MAC */
    338 	isc->macresetcbfunc = npeinit_resetcb;
    339 	isc->macresetcbarg = sc;
    340 }
    341 
    342 /*
    343  * Compute and install the multicast filter.
    344  */
    345 static void
    346 npe_setmcast(struct npe_softc *sc)
    347 {
    348 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    349 	uint8_t mask[ETHER_ADDR_LEN], addr[ETHER_ADDR_LEN];
    350 	uint32_t reg;
    351 	uint32_t msg[2];
    352 	int i;
    353 
    354 	/* Always use filter. Is here a correct position? */
    355 	reg = RD4(sc, NPE_MAC_RX_CNTRL1);
    356 	WR4(sc, NPE_MAC_RX_CNTRL1, reg | NPE_RX_CNTRL1_ADDR_FLTR_EN);
    357 
    358 	if (ifp->if_flags & IFF_PROMISC) {
    359 		memset(mask, 0, ETHER_ADDR_LEN);
    360 		memset(addr, 0, ETHER_ADDR_LEN);
    361 	} else if (ifp->if_flags & IFF_ALLMULTI) {
    362 		static const uint8_t allmulti[ETHER_ADDR_LEN] =
    363 		    { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
    364  all_multi:
    365 		memcpy(mask, allmulti, ETHER_ADDR_LEN);
    366 		memcpy(addr, allmulti, ETHER_ADDR_LEN);
    367 	} else {
    368 		uint8_t clr[ETHER_ADDR_LEN], set[ETHER_ADDR_LEN];
    369 		struct ether_multistep step;
    370 		struct ether_multi *enm;
    371 
    372 		memset(clr, 0, ETHER_ADDR_LEN);
    373 		memset(set, 0xff, ETHER_ADDR_LEN);
    374 
    375 		ETHER_FIRST_MULTI(step, &sc->sc_ethercom, enm);
    376 		while (enm != NULL) {
    377 			if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
    378 				ifp->if_flags |= IFF_ALLMULTI;
    379 				goto all_multi;
    380 			}
    381 
    382 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
    383 				clr[i] |= enm->enm_addrlo[i];
    384 				set[i] &= enm->enm_addrlo[i];
    385 			}
    386 
    387 			ETHER_NEXT_MULTI(step, enm);
    388 		}
    389 
    390 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
    391 			mask[i] = set[i] | ~clr[i];
    392 			addr[i] = set[i];
    393 		}
    394 	}
    395 
    396 	/*
    397 	 * Write the mask and address registers.
    398 	 */
    399 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
    400 		WR4(sc, NPE_MAC_ADDR_MASK(i), mask[i]);
    401 		WR4(sc, NPE_MAC_ADDR(i), addr[i]);
    402 	}
    403 
    404 	msg[0] = NPE_ADDRESSFILTERCONFIG << NPE_MAC_MSGID_SHL
    405 	    | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
    406 	msg[1] = ((ifp->if_flags & IFF_PROMISC) ? 1 : 0) << 24
    407 	    | ((RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff) << 16)
    408 	    | (addr[5] << 8) | mask[5];
    409 	ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
    410 }
    411 
    412 static int
    413 npe_dma_setup(struct npe_softc *sc, struct npedma *dma,
    414 	const char *name, int nbuf, int maxseg)
    415 {
    416 	bus_dma_segment_t seg;
    417 	int rseg, error, i;
    418 	void *hwbuf;
    419 	size_t size;
    420 
    421 	memset(dma, 0, sizeof(*dma));
    422 
    423 	dma->name = name;
    424 	dma->nbuf = nbuf;
    425 
    426 	size = nbuf * sizeof(struct npehwbuf);
    427 
    428 	/* XXX COHERENT for now */
    429 	error = bus_dmamem_alloc(sc->sc_dt, size, sizeof(uint32_t), 0, &seg,
    430 	    1, &rseg, BUS_DMA_NOWAIT);
    431 	if (error) {
    432 		printf("%s: unable to allocate memory for %s h/w buffers, "
    433 		    "error %u\n", sc->sc_dev.dv_xname, dma->name, error);
    434 	}
    435 
    436 	error = bus_dmamem_map(sc->sc_dt, &seg, 1, size, &hwbuf,
    437 	    BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_NOCACHE);
    438 	if (error) {
    439 		printf("%s: unable to map memory for %s h/w buffers, "
    440 		    "error %u\n", sc->sc_dev.dv_xname, dma->name, error);
    441  free_dmamem:
    442 		bus_dmamem_free(sc->sc_dt, &seg, rseg);
    443 		return error;
    444 	}
    445 	dma->hwbuf = (void *)hwbuf;
    446 
    447 	error = bus_dmamap_create(sc->sc_dt, size, 1, size, 0,
    448 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &dma->buf_map);
    449 	if (error) {
    450 		printf("%s: unable to create map for %s h/w buffers, "
    451 		    "error %u\n", sc->sc_dev.dv_xname, dma->name, error);
    452  unmap_dmamem:
    453 		dma->hwbuf = NULL;
    454 		bus_dmamem_unmap(sc->sc_dt, hwbuf, size);
    455 		goto free_dmamem;
    456 	}
    457 
    458 	error = bus_dmamap_load(sc->sc_dt, dma->buf_map, hwbuf, size, NULL,
    459 	    BUS_DMA_NOWAIT);
    460 	if (error) {
    461 		printf("%s: unable to load map for %s h/w buffers, "
    462 		    "error %u\n", sc->sc_dev.dv_xname, dma->name, error);
    463  destroy_dmamap:
    464 		bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
    465 		goto unmap_dmamem;
    466 	}
    467 
    468 	/* XXX M_TEMP */
    469 	dma->buf = malloc(nbuf * sizeof(struct npebuf), M_TEMP, M_NOWAIT | M_ZERO);
    470 	if (dma->buf == NULL) {
    471 		printf("%s: unable to allocate memory for %s s/w buffers\n",
    472 		    sc->sc_dev.dv_xname, dma->name);
    473 		bus_dmamap_unload(sc->sc_dt, dma->buf_map);
    474 		error = ENOMEM;
    475 		goto destroy_dmamap;
    476 	}
    477 
    478 	dma->buf_phys = dma->buf_map->dm_segs[0].ds_addr;
    479 	for (i = 0; i < dma->nbuf; i++) {
    480 		struct npebuf *npe = &dma->buf[i];
    481 		struct npehwbuf *hw = &dma->hwbuf[i];
    482 
    483 		/* calculate offset to shared area */
    484 		npe->ix_neaddr = dma->buf_phys +
    485 			((uintptr_t)hw - (uintptr_t)dma->hwbuf);
    486 		KASSERT((npe->ix_neaddr & 0x1f) == 0);
    487 		error = bus_dmamap_create(sc->sc_dt, MCLBYTES, maxseg,
    488 		    MCLBYTES, 0, 0, &npe->ix_map);
    489 		if (error != 0) {
    490 			printf("%s: unable to create dmamap for %s buffer %u, "
    491 			    "error %u\n", sc->sc_dev.dv_xname, dma->name, i,
    492 			    error);
    493 			/* XXXSCW: Free up maps... */
    494 			return error;
    495 		}
    496 		npe->ix_hw = hw;
    497 	}
    498 	bus_dmamap_sync(sc->sc_dt, dma->buf_map, 0, dma->buf_map->dm_mapsize,
    499 	    BUS_DMASYNC_PREWRITE);
    500 	return 0;
    501 }
    502 
    503 #if 0
    504 static void
    505 npe_dma_destroy(struct npe_softc *sc, struct npedma *dma)
    506 {
    507 	int i;
    508 
    509 /* XXXSCW: Clean this up */
    510 
    511 	if (dma->hwbuf != NULL) {
    512 		for (i = 0; i < dma->nbuf; i++) {
    513 			struct npebuf *npe = &dma->buf[i];
    514 			bus_dmamap_destroy(sc->sc_dt, npe->ix_map);
    515 		}
    516 		bus_dmamap_unload(sc->sc_dt, dma->buf_map);
    517 		bus_dmamem_free(sc->sc_dt, (void *)dma->hwbuf, dma->buf_map);
    518 		bus_dmamap_destroy(sc->sc_dt, dma->buf_map);
    519 	}
    520 	if (dma->buf != NULL)
    521 		free(dma->buf, M_TEMP);
    522 	memset(dma, 0, sizeof(*dma));
    523 }
    524 #endif
    525 
    526 static int
    527 npe_activate(struct npe_softc *sc)
    528 {
    529 	bus_dma_segment_t seg;
    530 	int unit = sc->sc_unit;
    531 	int error, i, rseg;
    532 	void *statbuf;
    533 
    534 	/* load NPE firmware and start it running */
    535 	error = ixpnpe_init(sc->sc_npe, "npe_fw", npeconfig[unit].imageid);
    536 	if (error != 0)
    537 		return error;
    538 
    539 	if (bus_space_map(sc->sc_iot, npeconfig[unit].regbase,
    540 	    npeconfig[unit].regsize, 0, &sc->sc_ioh)) {
    541 		printf("%s: Cannot map registers 0x%x:0x%x\n",
    542 		    sc->sc_dev.dv_xname, npeconfig[unit].regbase,
    543 		    npeconfig[unit].regsize);
    544 		return ENOMEM;
    545 	}
    546 
    547 	if (npeconfig[unit].miibase != npeconfig[unit].regbase) {
    548 		/*
    549 		 * The PHY's are only accessible from one MAC (it appears)
    550 		 * so for other MAC's setup an additional mapping for
    551 		 * frobbing the PHY registers.
    552 		 */
    553 		if (bus_space_map(sc->sc_iot, npeconfig[unit].miibase,
    554 		    npeconfig[unit].miisize, 0, &sc->sc_miih)) {
    555 			printf("%s: Cannot map MII registers 0x%x:0x%x\n",
    556 			    sc->sc_dev.dv_xname, npeconfig[unit].miibase,
    557 			    npeconfig[unit].miisize);
    558 			return ENOMEM;
    559 		}
    560 	} else
    561 		sc->sc_miih = sc->sc_ioh;
    562 	error = npe_dma_setup(sc, &sc->txdma, "tx", NPE_TXBUF, NPE_MAXSEG);
    563 	if (error != 0)
    564 		return error;
    565 	error = npe_dma_setup(sc, &sc->rxdma, "rx", NPE_RXBUF, 1);
    566 	if (error != 0)
    567 		return error;
    568 
    569 	/* setup statistics block */
    570 	error = bus_dmamem_alloc(sc->sc_dt, sizeof(struct npestats),
    571 	    sizeof(uint32_t), 0, &seg, 1, &rseg, BUS_DMA_NOWAIT);
    572 	if (error) {
    573 		printf("%s: unable to allocate memory for stats block, "
    574 		    "error %u\n", sc->sc_dev.dv_xname, error);
    575 		return error;
    576 	}
    577 
    578 	error = bus_dmamem_map(sc->sc_dt, &seg, 1, sizeof(struct npestats),
    579 	    &statbuf, BUS_DMA_NOWAIT);
    580 	if (error) {
    581 		printf("%s: unable to map memory for stats block, "
    582 		    "error %u\n", sc->sc_dev.dv_xname, error);
    583 		return error;
    584 	}
    585 	sc->sc_stats = (void *)statbuf;
    586 
    587 	error = bus_dmamap_create(sc->sc_dt, sizeof(struct npestats), 1,
    588 	    sizeof(struct npestats), 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
    589 	    &sc->sc_stats_map);
    590 	if (error) {
    591 		printf("%s: unable to create map for stats block, "
    592 		    "error %u\n", sc->sc_dev.dv_xname, error);
    593 		return error;
    594 	}
    595 
    596 	if (bus_dmamap_load(sc->sc_dt, sc->sc_stats_map, sc->sc_stats,
    597 	    sizeof(struct npestats), NULL, BUS_DMA_NOWAIT) != 0) {
    598 		printf("%s: unable to load memory for stats block, error %u\n",
    599 		    sc->sc_dev.dv_xname, error);
    600 		return error;
    601 	}
    602 	sc->sc_stats_phys = sc->sc_stats_map->dm_segs[0].ds_addr;
    603 
    604 	/* XXX disable half-bridge LEARNING+FILTERING feature */
    605 
    606 	/*
    607 	 * Setup h/w rx/tx queues.  There are four q's:
    608 	 *   rx		inbound q of rx'd frames
    609 	 *   rx_free	pool of ixpbuf's for receiving frames
    610 	 *   tx		outbound q of frames to send
    611 	 *   tx_done	q of tx frames that have been processed
    612 	 *
    613 	 * The NPE handles the actual tx/rx process and the q manager
    614 	 * handles the queues.  The driver just writes entries to the
    615 	 * q manager mailbox's and gets callbacks when there are rx'd
    616 	 * frames to process or tx'd frames to reap.  These callbacks
    617 	 * are controlled by the q configurations; e.g. we get a
    618 	 * callback when tx_done has 2 or more frames to process and
    619 	 * when the rx q has at least one frame.  These setings can
    620 	 * changed at the time the q is configured.
    621 	 */
    622 	sc->rx_qid = npeconfig[unit].rx_qid;
    623 	ixpqmgr_qconfig(sc->rx_qid, NPE_RXBUF, 0,  1,
    624 		IX_QMGR_Q_SOURCE_ID_NOT_E, npe_rxdone, sc);
    625 	sc->rx_freeqid = npeconfig[unit].rx_freeqid;
    626 	ixpqmgr_qconfig(sc->rx_freeqid,	NPE_RXBUF, 0, NPE_RXBUF/2, 0, NULL, sc);
    627 	/* tell the NPE to direct all traffic to rx_qid */
    628 #if 0
    629 	for (i = 0; i < 8; i++)
    630 #else
    631 printf("%s: remember to fix rx q setup\n", sc->sc_dev.dv_xname);
    632 	for (i = 0; i < 4; i++)
    633 #endif
    634 		npe_setrxqosentry(sc, i, 0, sc->rx_qid);
    635 
    636 	sc->tx_qid = npeconfig[unit].tx_qid;
    637 	sc->tx_doneqid = npeconfig[unit].tx_doneqid;
    638 	ixpqmgr_qconfig(sc->tx_qid, NPE_TXBUF, 0, NPE_TXBUF, 0, NULL, sc);
    639 	if (tx_doneqid == -1) {
    640 		ixpqmgr_qconfig(sc->tx_doneqid,	NPE_TXBUF, 0,  2,
    641 			IX_QMGR_Q_SOURCE_ID_NOT_E, npe_txdone, sc);
    642 		tx_doneqid = sc->tx_doneqid;
    643 	}
    644 
    645 	KASSERT(npes[npeconfig[unit].npeid] == NULL);
    646 	npes[npeconfig[unit].npeid] = sc;
    647 
    648 	return 0;
    649 }
    650 
    651 #if 0
    652 static void
    653 npe_deactivate(struct npe_softc *sc);
    654 {
    655 	int unit = sc->sc_unit;
    656 
    657 	npes[npeconfig[unit].npeid] = NULL;
    658 
    659 	/* XXX disable q's */
    660 	if (sc->sc_npe != NULL)
    661 		ixpnpe_stop(sc->sc_npe);
    662 	if (sc->sc_stats != NULL) {
    663 		bus_dmamap_unload(sc->sc_stats_tag, sc->sc_stats_map);
    664 		bus_dmamem_free(sc->sc_stats_tag, sc->sc_stats,
    665 			sc->sc_stats_map);
    666 		bus_dmamap_destroy(sc->sc_stats_tag, sc->sc_stats_map);
    667 	}
    668 	if (sc->sc_stats_tag != NULL)
    669 		bus_dma_tag_destroy(sc->sc_stats_tag);
    670 	npe_dma_destroy(sc, &sc->txdma);
    671 	npe_dma_destroy(sc, &sc->rxdma);
    672 	bus_generic_detach(sc->sc_dev);
    673 	if (sc->sc_mii)
    674 		device_delete_child(sc->sc_dev, sc->sc_mii);
    675 #if 0
    676 	/* XXX sc_ioh and sc_miih */
    677 	if (sc->mem_res)
    678 		bus_release_resource(dev, SYS_RES_IOPORT,
    679 		    rman_get_rid(sc->mem_res), sc->mem_res);
    680 	sc->mem_res = 0;
    681 #endif
    682 }
    683 #endif
    684 
    685 /*
    686  * Notify the world which media we're using.
    687  */
    688 static void
    689 npe_ifmedia_status(struct ifnet *ifp, struct ifmediareq *ifmr)
    690 {
    691 	struct npe_softc *sc = ifp->if_softc;
    692 
    693 	mii_pollstat(&sc->sc_mii);
    694 
    695 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
    696 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
    697 }
    698 
    699 static void
    700 npe_addstats(struct npe_softc *sc)
    701 {
    702 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    703 	struct npestats *ns = sc->sc_stats;
    704 
    705 	ifp->if_oerrors +=
    706 		  be32toh(ns->dot3StatsInternalMacTransmitErrors)
    707 		+ be32toh(ns->dot3StatsCarrierSenseErrors)
    708 		+ be32toh(ns->TxVLANIdFilterDiscards)
    709 		;
    710 	ifp->if_ierrors += be32toh(ns->dot3StatsFCSErrors)
    711 		+ be32toh(ns->dot3StatsInternalMacReceiveErrors)
    712 		+ be32toh(ns->RxOverrunDiscards)
    713 		+ be32toh(ns->RxUnderflowEntryDiscards)
    714 		;
    715 	ifp->if_collisions +=
    716 		  be32toh(ns->dot3StatsSingleCollisionFrames)
    717 		+ be32toh(ns->dot3StatsMultipleCollisionFrames)
    718 		;
    719 }
    720 
    721 static void
    722 npe_tick(void *xsc)
    723 {
    724 #define	ACK	(NPE_RESETSTATS << NPE_MAC_MSGID_SHL)
    725 	struct npe_softc *sc = xsc;
    726 	uint32_t msg[2];
    727 
    728 	/*
    729 	 * NB: to avoid sleeping with the softc lock held we
    730 	 * split the NPE msg processing into two parts.  The
    731 	 * request for statistics is sent w/o waiting for a
    732 	 * reply and then on the next tick we retrieve the
    733 	 * results.  This works because npe_tick is the only
    734 	 * code that talks via the mailbox's (except at setup).
    735 	 * This likely can be handled better.
    736 	 */
    737 	if (ixpnpe_recvmsg(sc->sc_npe, msg) == 0 && msg[0] == ACK) {
    738 		bus_dmamap_sync(sc->sc_dt, sc->sc_stats_map, 0,
    739 		    sizeof(struct npestats), BUS_DMASYNC_POSTREAD);
    740 		npe_addstats(sc);
    741 	}
    742 	npe_updatestats(sc);
    743 	mii_tick(&sc->sc_mii);
    744 
    745 	/* schedule next poll */
    746 	callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
    747 #undef ACK
    748 }
    749 
    750 static void
    751 npe_setmac(struct npe_softc *sc, const u_char *eaddr)
    752 {
    753 
    754 	WR4(sc, NPE_MAC_UNI_ADDR_1, eaddr[0]);
    755 	WR4(sc, NPE_MAC_UNI_ADDR_2, eaddr[1]);
    756 	WR4(sc, NPE_MAC_UNI_ADDR_3, eaddr[2]);
    757 	WR4(sc, NPE_MAC_UNI_ADDR_4, eaddr[3]);
    758 	WR4(sc, NPE_MAC_UNI_ADDR_5, eaddr[4]);
    759 	WR4(sc, NPE_MAC_UNI_ADDR_6, eaddr[5]);
    760 }
    761 
    762 static void
    763 npe_getmac(struct npe_softc *sc)
    764 {
    765 	uint8_t *eaddr = sc->sc_enaddr;
    766 
    767 	if (npe_getmac_md != NULL) {
    768 		(*npe_getmac_md)(sc->sc_dev.dv_unit, eaddr);
    769 	} else {
    770 		/*
    771 		 * Some system's unicast address appears to be loaded from
    772 		 * EEPROM on reset
    773 		 */
    774 		eaddr[0] = RD4(sc, NPE_MAC_UNI_ADDR_1) & 0xff;
    775 		eaddr[1] = RD4(sc, NPE_MAC_UNI_ADDR_2) & 0xff;
    776 		eaddr[2] = RD4(sc, NPE_MAC_UNI_ADDR_3) & 0xff;
    777 		eaddr[3] = RD4(sc, NPE_MAC_UNI_ADDR_4) & 0xff;
    778 		eaddr[4] = RD4(sc, NPE_MAC_UNI_ADDR_5) & 0xff;
    779 		eaddr[5] = RD4(sc, NPE_MAC_UNI_ADDR_6) & 0xff;
    780 	}
    781 }
    782 
    783 struct txdone {
    784 	struct npebuf *head;
    785 	struct npebuf **tail;
    786 	int count;
    787 };
    788 
    789 static __inline void
    790 npe_txdone_finish(struct npe_softc *sc, const struct txdone *td)
    791 {
    792 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    793 
    794 	*td->tail = sc->tx_free;
    795 	sc->tx_free = td->head;
    796 	/*
    797 	 * We're no longer busy, so clear the busy flag and call the
    798 	 * start routine to xmit more packets.
    799 	 */
    800 	ifp->if_opackets += td->count;
    801 	ifp->if_flags &= ~IFF_OACTIVE;
    802 	ifp->if_timer = 0;
    803 	npestart(ifp);
    804 }
    805 
    806 /*
    807  * Q manager callback on tx done queue.  Reap mbufs
    808  * and return tx buffers to the free list.  Finally
    809  * restart output.  Note the microcode has only one
    810  * txdone q wired into it so we must use the NPE ID
    811  * returned with each npehwbuf to decide where to
    812  * send buffers.
    813  */
    814 static void
    815 npe_txdone(int qid, void *arg)
    816 {
    817 #define	P2V(a, dma) \
    818 	&(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
    819 	struct npe_softc *sc;
    820 	struct npebuf *npe;
    821 	struct txdone *td, q[NPE_MAX];
    822 	uint32_t entry;
    823 
    824 	/* XXX no NPE-A support */
    825 	q[NPE_B].tail = &q[NPE_B].head; q[NPE_B].count = 0;
    826 	q[NPE_C].tail = &q[NPE_C].head; q[NPE_C].count = 0;
    827 	/* XXX max # at a time? */
    828 	while (ixpqmgr_qread(qid, &entry) == 0) {
    829 		sc = npes[NPE_QM_Q_NPE(entry)];
    830 		DPRINTF(sc, "%s: entry 0x%x NPE %u port %u\n",
    831 		    __func__, entry, NPE_QM_Q_NPE(entry), NPE_QM_Q_PORT(entry));
    832 
    833 		npe = P2V(NPE_QM_Q_ADDR(entry), &sc->txdma);
    834 		m_freem(npe->ix_m);
    835 		npe->ix_m = NULL;
    836 
    837 		td = &q[NPE_QM_Q_NPE(entry)];
    838 		*td->tail = npe;
    839 		td->tail = &npe->ix_next;
    840 		td->count++;
    841 	}
    842 
    843 	if (q[NPE_B].count)
    844 		npe_txdone_finish(npes[NPE_B], &q[NPE_B]);
    845 	if (q[NPE_C].count)
    846 		npe_txdone_finish(npes[NPE_C], &q[NPE_C]);
    847 #undef P2V
    848 }
    849 
    850 static __inline struct mbuf *
    851 npe_getcl(void)
    852 {
    853 	struct mbuf *m;
    854 
    855 	MGETHDR(m, M_DONTWAIT, MT_DATA);
    856 	if (m != NULL) {
    857 		MCLGET(m, M_DONTWAIT);
    858 		if ((m->m_flags & M_EXT) == 0) {
    859 			m_freem(m);
    860 			m = NULL;
    861 		}
    862 	}
    863 	return (m);
    864 }
    865 
    866 static int
    867 npe_rxbuf_init(struct npe_softc *sc, struct npebuf *npe, struct mbuf *m)
    868 {
    869 	struct npehwbuf *hw;
    870 	int error;
    871 
    872 	if (m == NULL) {
    873 		m = npe_getcl();
    874 		if (m == NULL)
    875 			return ENOBUFS;
    876 	}
    877 	KASSERT(m->m_ext.ext_size >= (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
    878 	m->m_pkthdr.len = m->m_len = NPE_FRAME_SIZE_DEFAULT;
    879 	/* backload payload and align ip hdr */
    880 	m->m_data = m->m_ext.ext_buf + (m->m_ext.ext_size
    881 	    - (NPE_FRAME_SIZE_DEFAULT + ETHER_ALIGN));
    882 	error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
    883 	    BUS_DMA_READ|BUS_DMA_NOWAIT);
    884 	if (error != 0) {
    885 		m_freem(m);
    886 		return error;
    887 	}
    888 	hw = npe->ix_hw;
    889 	hw->ix_ne[0].data = htobe32(npe->ix_map->dm_segs[0].ds_addr);
    890 	/* NB: NPE requires length be a multiple of 64 */
    891 	/* NB: buffer length is shifted in word */
    892 	hw->ix_ne[0].len = htobe32(npe->ix_map->dm_segs[0].ds_len << 16);
    893 	hw->ix_ne[0].next = 0;
    894 	npe->ix_m = m;
    895 	/* Flush the memory in the mbuf */
    896 	bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0, npe->ix_map->dm_mapsize,
    897 	    BUS_DMASYNC_PREREAD);
    898 	return 0;
    899 }
    900 
    901 /*
    902  * RX q processing for a specific NPE.  Claim entries
    903  * from the hardware queue and pass the frames up the
    904  * stack. Pass the rx buffers to the free list.
    905  */
    906 static void
    907 npe_rxdone(int qid, void *arg)
    908 {
    909 #define	P2V(a, dma) \
    910 	&(dma)->buf[((a) - (dma)->buf_phys) / sizeof(struct npehwbuf)]
    911 	struct npe_softc *sc = arg;
    912 	struct npedma *dma = &sc->rxdma;
    913 	uint32_t entry;
    914 
    915 	while (ixpqmgr_qread(qid, &entry) == 0) {
    916 		struct npebuf *npe = P2V(NPE_QM_Q_ADDR(entry), dma);
    917 		struct mbuf *m;
    918 
    919 		DPRINTF(sc, "%s: entry 0x%x neaddr 0x%x ne_len 0x%x\n",
    920 		    __func__, entry, npe->ix_neaddr, npe->ix_hw->ix_ne[0].len);
    921 		/*
    922 		 * Allocate a new mbuf to replenish the rx buffer.
    923 		 * If doing so fails we drop the rx'd frame so we
    924 		 * can reuse the previous mbuf.  When we're able to
    925 		 * allocate a new mbuf dispatch the mbuf w/ rx'd
    926 		 * data up the stack and replace it with the newly
    927 		 * allocated one.
    928 		 */
    929 		m = npe_getcl();
    930 		if (m != NULL) {
    931 			struct mbuf *mrx = npe->ix_m;
    932 			struct npehwbuf *hw = npe->ix_hw;
    933 			struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    934 
    935 			/* Flush mbuf memory for rx'd data */
    936 			bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
    937 			    npe->ix_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
    938 
    939 			/* XXX flush hw buffer; works now 'cuz coherent */
    940 			/* set m_len etc. per rx frame size */
    941 			mrx->m_len = be32toh(hw->ix_ne[0].len) & 0xffff;
    942 			mrx->m_pkthdr.len = mrx->m_len;
    943 			mrx->m_pkthdr.rcvif = ifp;
    944 			/* Don't add M_HASFCS. See below */
    945 
    946 #if 1
    947 			if (mrx->m_pkthdr.len < sizeof(struct ether_header)) {
    948 				log(LOG_INFO, "%s: too short frame (len=%d)\n",
    949 				    sc->sc_dev.dv_xname, mrx->m_pkthdr.len);
    950 				/* Back out "newly allocated" mbuf. */
    951 				m_freem(m);
    952 				ifp->if_ierrors++;
    953 				goto fail;
    954 			}
    955 			if ((ifp->if_flags & IFF_PROMISC) == 0) {
    956 				struct ether_header *eh;
    957 
    958 				/*
    959 				 * Workaround for "Non-Intel XScale Technology
    960 				 * Eratta" No. 29. AA:BB:CC:DD:EE:xF's packet
    961 				 * matches the filter (both unicast and
    962 				 * multicast).
    963 				 */
    964 				eh = mtod(mrx, struct ether_header *);
    965 				if (ETHER_IS_MULTICAST(eh->ether_dhost) == 0) {
    966 					/* unicast */
    967 
    968 					if (sc->sc_enaddr[5] != eh->ether_dhost[5]) {
    969 						/* discard it */
    970 #if 0
    971 						printf("discard it\n");
    972 #endif
    973 						/*
    974 						 * Back out "newly allocated"
    975 						 * mbuf.
    976 						 */
    977 						m_freem(m);
    978 						goto fail;
    979 					}
    980 				} else if (memcmp(eh->ether_dhost,
    981 					etherbroadcastaddr, 6) == 0) {
    982 					/* Always accept broadcast packet*/
    983 				} else {
    984 					struct ethercom *ec = &sc->sc_ethercom;
    985 					struct ether_multi *enm;
    986 					struct ether_multistep step;
    987 					int match = 0;
    988 
    989 					/* multicast */
    990 
    991 					ETHER_FIRST_MULTI(step, ec, enm);
    992 					while (enm != NULL) {
    993 						uint64_t lowint, highint, dest;
    994 
    995 						lowint = MAC2UINT64(enm->enm_addrlo);
    996 						highint = MAC2UINT64(enm->enm_addrhi);
    997 						dest = MAC2UINT64(eh->ether_dhost);
    998 #if 0
    999 						printf("%llx\n", lowint);
   1000 						printf("%llx\n", dest);
   1001 						printf("%llx\n", highint);
   1002 #endif
   1003 						if ((lowint <= dest) && (dest <= highint)) {
   1004 							match = 1;
   1005 							break;
   1006 						}
   1007 						ETHER_NEXT_MULTI(step, enm);
   1008 					}
   1009 					if (match == 0) {
   1010 						/* discard it */
   1011 #if 0
   1012 						printf("discard it(M)\n");
   1013 #endif
   1014 						/*
   1015 						 * Back out "newly allocated"
   1016 						 * mbuf.
   1017 						 */
   1018 						m_freem(m);
   1019 						goto fail;
   1020 					}
   1021 				}
   1022 			}
   1023 			if (mrx->m_pkthdr.len > NPE_FRAME_SIZE_DEFAULT) {
   1024 				log(LOG_INFO, "%s: oversized frame (len=%d)\n",
   1025 				    sc->sc_dev.dv_xname, mrx->m_pkthdr.len);
   1026 				/* Back out "newly allocated" mbuf. */
   1027 				m_freem(m);
   1028 				ifp->if_ierrors++;
   1029 				goto fail;
   1030 			}
   1031 #endif
   1032 
   1033 			/*
   1034 			 * Trim FCS!
   1035 			 * NPE always adds the FCS by this driver's setting,
   1036 			 * so we always trim it here and not add M_HASFCS.
   1037 			 */
   1038 			m_adj(mrx, -ETHER_CRC_LEN);
   1039 
   1040 			ifp->if_ipackets++;
   1041 #if NBPFILTER > 0
   1042 			/*
   1043 			 * Tap off here if there is a bpf listener.
   1044 			 */
   1045 			if (__predict_false(ifp->if_bpf))
   1046 				bpf_mtap(ifp->if_bpf, mrx);
   1047 #endif
   1048 			ifp->if_input(ifp, mrx);
   1049 		} else {
   1050 fail:
   1051 			/* discard frame and re-use mbuf */
   1052 			m = npe->ix_m;
   1053 		}
   1054 		if (npe_rxbuf_init(sc, npe, m) == 0) {
   1055 			/* return npe buf to rx free list */
   1056 			ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
   1057 		} else {
   1058 			/* XXX should not happen */
   1059 		}
   1060 	}
   1061 #undef P2V
   1062 }
   1063 
   1064 static void
   1065 npe_startxmit(struct npe_softc *sc)
   1066 {
   1067 	struct npedma *dma = &sc->txdma;
   1068 	int i;
   1069 
   1070 	sc->tx_free = NULL;
   1071 	for (i = 0; i < dma->nbuf; i++) {
   1072 		struct npebuf *npe = &dma->buf[i];
   1073 		if (npe->ix_m != NULL) {
   1074 			/* NB: should not happen */
   1075 			printf("%s: %s: free mbuf at entry %u\n",
   1076 			    sc->sc_dev.dv_xname, __func__, i);
   1077 			m_freem(npe->ix_m);
   1078 		}
   1079 		npe->ix_m = NULL;
   1080 		npe->ix_next = sc->tx_free;
   1081 		sc->tx_free = npe;
   1082 	}
   1083 }
   1084 
   1085 static void
   1086 npe_startrecv(struct npe_softc *sc)
   1087 {
   1088 	struct npedma *dma = &sc->rxdma;
   1089 	struct npebuf *npe;
   1090 	int i;
   1091 
   1092 	for (i = 0; i < dma->nbuf; i++) {
   1093 		npe = &dma->buf[i];
   1094 		npe_rxbuf_init(sc, npe, npe->ix_m);
   1095 		/* set npe buf on rx free list */
   1096 		ixpqmgr_qwrite(sc->rx_freeqid, npe->ix_neaddr);
   1097 	}
   1098 }
   1099 
   1100 static void
   1101 npeinit_macreg(struct npe_softc *sc)
   1102 {
   1103 
   1104 	/*
   1105 	 * Reset MAC core.
   1106 	 */
   1107 	WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
   1108 	DELAY(NPE_MAC_RESET_DELAY);
   1109 	/* configure MAC to generate MDC clock */
   1110 	WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
   1111 
   1112 	/* disable transmitter and reciver in the MAC */
   1113  	WR4(sc, NPE_MAC_RX_CNTRL1,
   1114 	    RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
   1115  	WR4(sc, NPE_MAC_TX_CNTRL1,
   1116 	    RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
   1117 
   1118 	/*
   1119 	 * Set the MAC core registers.
   1120 	 */
   1121 	WR4(sc, NPE_MAC_INT_CLK_THRESH, 0x1);	/* clock ratio: for ipx4xx */
   1122 	WR4(sc, NPE_MAC_TX_CNTRL2,	0xf);	/* max retries */
   1123 	WR4(sc, NPE_MAC_RANDOM_SEED,	0x8);	/* LFSR back-off seed */
   1124 	/* thresholds determined by NPE firmware FS */
   1125 	WR4(sc, NPE_MAC_THRESH_P_EMPTY,	0x12);
   1126 	WR4(sc, NPE_MAC_THRESH_P_FULL,	0x30);
   1127 	WR4(sc, NPE_MAC_BUF_SIZE_TX, NPE_MAC_BUF_SIZE_TX_DEFAULT);
   1128 						/* tx fifo threshold (bytes) */
   1129 	WR4(sc, NPE_MAC_TX_DEFER,	0x15);	/* for single deferral */
   1130 	WR4(sc, NPE_MAC_RX_DEFER,	0x16);	/* deferral on inter-frame gap*/
   1131 	WR4(sc, NPE_MAC_TX_TWO_DEFER_1,	0x8);	/* for 2-part deferral */
   1132 	WR4(sc, NPE_MAC_TX_TWO_DEFER_2,	0x7);	/* for 2-part deferral */
   1133 	WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
   1134 						/* assumes MII mode */
   1135 	WR4(sc, NPE_MAC_TX_CNTRL1,
   1136 		  NPE_TX_CNTRL1_RETRY		/* retry failed xmits */
   1137 		| NPE_TX_CNTRL1_FCS_EN		/* append FCS */
   1138 		| NPE_TX_CNTRL1_2DEFER		/* 2-part deferal */
   1139 		| NPE_TX_CNTRL1_PAD_EN);	/* pad runt frames */
   1140 	/* XXX pad strip? */
   1141 	WR4(sc, NPE_MAC_RX_CNTRL1,
   1142 		  NPE_RX_CNTRL1_CRC_EN		/* include CRC/FCS */
   1143 		| NPE_RX_CNTRL1_PAUSE_EN);	/* ena pause frame handling */
   1144 	WR4(sc, NPE_MAC_RX_CNTRL2, 0);
   1145 }
   1146 
   1147 static void
   1148 npeinit_resetcb(void *xsc)
   1149 {
   1150 	struct npe_softc *sc = xsc;
   1151 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1152 	uint32_t msg[2];
   1153 
   1154 	ifp->if_oerrors++;
   1155 	npeinit_locked(sc);
   1156 
   1157 	msg[0] = NPE_NOTIFYMACRECOVERYDONE << NPE_MAC_MSGID_SHL
   1158 	    | (npeconfig[sc->sc_unit].macport << NPE_MAC_PORTID_SHL);
   1159 	msg[1] = 0;
   1160 	ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
   1161 }
   1162 
   1163 /*
   1164  * Reset and initialize the chip
   1165  */
   1166 static void
   1167 npeinit_locked(void *xsc)
   1168 {
   1169 	struct npe_softc *sc = xsc;
   1170 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1171 
   1172 	/* Cancel any pending I/O. */
   1173 	npestop(ifp, 0);
   1174 
   1175 	/* Reset the chip to a known state. */
   1176 	npeinit_macreg(sc);
   1177 	npe_setmac(sc, CLLADDR(ifp->if_sadl));
   1178 	ether_mediachange(ifp);
   1179 	npe_setmcast(sc);
   1180 
   1181 	npe_startxmit(sc);
   1182 	npe_startrecv(sc);
   1183 
   1184 	ifp->if_flags |= IFF_RUNNING;
   1185 	ifp->if_flags &= ~IFF_OACTIVE;
   1186 	ifp->if_timer = 0;		/* just in case */
   1187 
   1188 	/* enable transmitter and reciver in the MAC */
   1189  	WR4(sc, NPE_MAC_RX_CNTRL1,
   1190 	    RD4(sc, NPE_MAC_RX_CNTRL1) | NPE_RX_CNTRL1_RX_EN);
   1191  	WR4(sc, NPE_MAC_TX_CNTRL1,
   1192 	    RD4(sc, NPE_MAC_TX_CNTRL1) | NPE_TX_CNTRL1_TX_EN);
   1193 
   1194 	callout_reset(&sc->sc_tick_ch, hz, npe_tick, sc);
   1195 }
   1196 
   1197 static int
   1198 npeinit(struct ifnet *ifp)
   1199 {
   1200 	struct npe_softc *sc = ifp->if_softc;
   1201 	int s;
   1202 
   1203 	s = splnet();
   1204 	npeinit_locked(sc);
   1205 	splx(s);
   1206 
   1207 	return (0);
   1208 }
   1209 
   1210 /*
   1211  * Defragment an mbuf chain, returning at most maxfrags separate
   1212  * mbufs+clusters.  If this is not possible NULL is returned and
   1213  * the original mbuf chain is left in it's present (potentially
   1214  * modified) state.  We use two techniques: collapsing consecutive
   1215  * mbufs and replacing consecutive mbufs by a cluster.
   1216  */
   1217 static __inline struct mbuf *
   1218 npe_defrag(struct mbuf *m0)
   1219 {
   1220 	struct mbuf *m;
   1221 
   1222 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1223 	if (m == NULL)
   1224 		return (NULL);
   1225 	M_COPY_PKTHDR(m, m0);
   1226 
   1227 	if ((m->m_len = m0->m_pkthdr.len) > MHLEN) {
   1228 		MCLGET(m, M_DONTWAIT);
   1229 		if ((m->m_flags & M_EXT) == 0) {
   1230 			m_freem(m);
   1231 			return (NULL);
   1232 		}
   1233 	}
   1234 
   1235 	m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
   1236 	m_freem(m0);
   1237 
   1238 	return (m);
   1239 }
   1240 
   1241 /*
   1242  * Dequeue packets and place on the h/w transmit queue.
   1243  */
   1244 static void
   1245 npestart(struct ifnet *ifp)
   1246 {
   1247 	struct npe_softc *sc = ifp->if_softc;
   1248 	struct npebuf *npe;
   1249 	struct npehwbuf *hw;
   1250 	struct mbuf *m, *n;
   1251 	bus_dma_segment_t *segs;
   1252 	int nseg, len, error, i;
   1253 	uint32_t next;
   1254 
   1255 	if ((ifp->if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING)
   1256 		return;
   1257 
   1258 	while (sc->tx_free != NULL) {
   1259 		IFQ_DEQUEUE(&ifp->if_snd, m);
   1260 		if (m == NULL)
   1261 			break;
   1262 		npe = sc->tx_free;
   1263 		error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map, m,
   1264 		    BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1265 		if (error == EFBIG) {
   1266 			n = npe_defrag(m);
   1267 			if (n == NULL) {
   1268 				printf("%s: %s: too many fragments\n",
   1269 				    sc->sc_dev.dv_xname, __func__);
   1270 				m_freem(m);
   1271 				return;	/* XXX? */
   1272 			}
   1273 			m = n;
   1274 			error = bus_dmamap_load_mbuf(sc->sc_dt, npe->ix_map,
   1275 			    m, BUS_DMA_WRITE|BUS_DMA_NOWAIT);
   1276 		}
   1277 		if (error != 0) {
   1278 			printf("%s: %s: error %u\n",
   1279 			    sc->sc_dev.dv_xname, __func__, error);
   1280 			m_freem(m);
   1281 			return;	/* XXX? */
   1282 		}
   1283 		sc->tx_free = npe->ix_next;
   1284 
   1285 #if NBPFILTER > 0
   1286 		/*
   1287 		 * Tap off here if there is a bpf listener.
   1288 		 */
   1289 		if (__predict_false(ifp->if_bpf))
   1290 			bpf_mtap(ifp->if_bpf, m);
   1291 #endif
   1292 
   1293 		bus_dmamap_sync(sc->sc_dt, npe->ix_map, 0,
   1294 		    npe->ix_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
   1295 
   1296 		npe->ix_m = m;
   1297 		hw = npe->ix_hw;
   1298 		len = m->m_pkthdr.len;
   1299 		nseg = npe->ix_map->dm_nsegs;
   1300 		segs = npe->ix_map->dm_segs;
   1301 		next = npe->ix_neaddr + sizeof(hw->ix_ne[0]);
   1302 		for (i = 0; i < nseg; i++) {
   1303 			hw->ix_ne[i].data = htobe32(segs[i].ds_addr);
   1304 			hw->ix_ne[i].len = htobe32((segs[i].ds_len<<16) | len);
   1305 			hw->ix_ne[i].next = htobe32(next);
   1306 
   1307 			len = 0;		/* zero for segments > 1 */
   1308 			next += sizeof(hw->ix_ne[0]);
   1309 		}
   1310 		hw->ix_ne[i-1].next = 0;	/* zero last in chain */
   1311 		/* XXX flush descriptor instead of using uncached memory */
   1312 
   1313 		DPRINTF(sc, "%s: qwrite(%u, 0x%x) ne_data %x ne_len 0x%x\n",
   1314 		    __func__, sc->tx_qid, npe->ix_neaddr,
   1315 		    hw->ix_ne[0].data, hw->ix_ne[0].len);
   1316 		/* stick it on the tx q */
   1317 		/* XXX add vlan priority */
   1318 		ixpqmgr_qwrite(sc->tx_qid, npe->ix_neaddr);
   1319 
   1320 		ifp->if_timer = 5;
   1321 	}
   1322 	if (sc->tx_free == NULL)
   1323 		ifp->if_flags |= IFF_OACTIVE;
   1324 }
   1325 
   1326 static void
   1327 npe_stopxmit(struct npe_softc *sc)
   1328 {
   1329 	struct npedma *dma = &sc->txdma;
   1330 	int i;
   1331 
   1332 	/* XXX qmgr */
   1333 	for (i = 0; i < dma->nbuf; i++) {
   1334 		struct npebuf *npe = &dma->buf[i];
   1335 
   1336 		if (npe->ix_m != NULL) {
   1337 			bus_dmamap_unload(sc->sc_dt, npe->ix_map);
   1338 			m_freem(npe->ix_m);
   1339 			npe->ix_m = NULL;
   1340 		}
   1341 	}
   1342 }
   1343 
   1344 static void
   1345 npe_stoprecv(struct npe_softc *sc)
   1346 {
   1347 	struct npedma *dma = &sc->rxdma;
   1348 	int i;
   1349 
   1350 	/* XXX qmgr */
   1351 	for (i = 0; i < dma->nbuf; i++) {
   1352 		struct npebuf *npe = &dma->buf[i];
   1353 
   1354 		if (npe->ix_m != NULL) {
   1355 			bus_dmamap_unload(sc->sc_dt, npe->ix_map);
   1356 			m_freem(npe->ix_m);
   1357 			npe->ix_m = NULL;
   1358 		}
   1359 	}
   1360 }
   1361 
   1362 /*
   1363  * Turn off interrupts, and stop the nic.
   1364  */
   1365 void
   1366 npestop(struct ifnet *ifp, int disable)
   1367 {
   1368 	struct npe_softc *sc = ifp->if_softc;
   1369 
   1370 	/*  disable transmitter and reciver in the MAC  */
   1371  	WR4(sc, NPE_MAC_RX_CNTRL1,
   1372 	    RD4(sc, NPE_MAC_RX_CNTRL1) &~ NPE_RX_CNTRL1_RX_EN);
   1373  	WR4(sc, NPE_MAC_TX_CNTRL1,
   1374 	    RD4(sc, NPE_MAC_TX_CNTRL1) &~ NPE_TX_CNTRL1_TX_EN);
   1375 
   1376 	callout_stop(&sc->sc_tick_ch);
   1377 
   1378 	npe_stopxmit(sc);
   1379 	npe_stoprecv(sc);
   1380 	/* XXX go into loopback & drain q's? */
   1381 	/* XXX but beware of disabling tx above */
   1382 
   1383 	/*
   1384 	 * The MAC core rx/tx disable may leave the MAC hardware in an
   1385 	 * unpredictable state. A hw reset is executed before resetting
   1386 	 * all the MAC parameters to a known value.
   1387 	 */
   1388 	WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_RESET);
   1389 	DELAY(NPE_MAC_RESET_DELAY);
   1390 	WR4(sc, NPE_MAC_INT_CLK_THRESH, NPE_MAC_INT_CLK_THRESH_DEFAULT);
   1391 	WR4(sc, NPE_MAC_CORE_CNTRL, NPE_CORE_MDC_EN);
   1392 
   1393 	ifp->if_timer = 0;
   1394 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
   1395 }
   1396 
   1397 void
   1398 npewatchdog(struct ifnet *ifp)
   1399 {
   1400 	struct npe_softc *sc = ifp->if_softc;
   1401 	int s;
   1402 
   1403 	printf("%s: device timeout\n", sc->sc_dev.dv_xname);
   1404 	s = splnet();
   1405 	ifp->if_oerrors++;
   1406 	npeinit_locked(sc);
   1407 	splx(s);
   1408 }
   1409 
   1410 static int
   1411 npeioctl(struct ifnet *ifp, u_long cmd, void *data)
   1412 {
   1413 	struct npe_softc *sc = ifp->if_softc;
   1414 	struct ifreq *ifr = (struct ifreq *) data;
   1415 	int s, error = 0;
   1416 
   1417 	s = splnet();
   1418 
   1419 	switch (cmd) {
   1420 	case SIOCSIFMEDIA:
   1421 	case SIOCGIFMEDIA:
   1422 #if 0 /* not yet */
   1423 		/* Flow control requires full-duplex mode. */
   1424 		if (IFM_SUBTYPE(ifr->ifr_media) == IFM_AUTO ||
   1425 		    (ifr->ifr_media & IFM_FDX) == 0)
   1426 			ifr->ifr_media &= ~IFM_ETH_FMASK;
   1427 		if (IFM_SUBTYPE(ifr->ifr_media) != IFM_AUTO) {
   1428 			if ((ifr->ifr_media & IFM_ETH_FMASK) == IFM_FLOW) {
   1429 				/* We can do both TXPAUSE and RXPAUSE. */
   1430 				ifr->ifr_media |=
   1431 				    IFM_ETH_TXPAUSE | IFM_ETH_RXPAUSE;
   1432 			}
   1433 			sc->sc_flowflags = ifr->ifr_media & IFM_ETH_FMASK;
   1434 		}
   1435 #endif
   1436 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
   1437 		break;
   1438 	case SIOCSIFFLAGS:
   1439 		if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
   1440 			/*
   1441 			 * If interface is marked down and it is running,
   1442 			 * then stop and disable it.
   1443 			 */
   1444 			(*ifp->if_stop)(ifp, 1);
   1445 		} else if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
   1446 			/*
   1447 			 * If interface is marked up and it is stopped, then
   1448 			 * start it.
   1449 			 */
   1450 			error = (*ifp->if_init)(ifp);
   1451 		} else if ((ifp->if_flags & IFF_UP) != 0) {
   1452 			int diff;
   1453 
   1454 			/* Up (AND RUNNING). */
   1455 
   1456 			diff = (ifp->if_flags ^ sc->sc_if_flags)
   1457 			    & (IFF_PROMISC|IFF_ALLMULTI);
   1458 			if ((diff & (IFF_PROMISC|IFF_ALLMULTI)) != 0) {
   1459 				/*
   1460 				 * If the difference bettween last flag and
   1461 				 * new flag only IFF_PROMISC or IFF_ALLMULTI,
   1462 				 * set multicast filter only (don't reset to
   1463 				 * prevent link down).
   1464 				 */
   1465 				npe_setmcast(sc);
   1466 			} else {
   1467 				/*
   1468 				 * Reset the interface to pick up changes in
   1469 				 * any other flags that affect the hardware
   1470 				 * state.
   1471 				 */
   1472 				error = (*ifp->if_init)(ifp);
   1473 			}
   1474 		}
   1475 		sc->sc_if_flags = ifp->if_flags;
   1476 		break;
   1477 	default:
   1478 		error = ether_ioctl(ifp, cmd, data);
   1479 		if (error == ENETRESET) {
   1480 			/*
   1481 			 * Multicast list has changed; set the hardware filter
   1482 			 * accordingly.
   1483 			 */
   1484 			npe_setmcast(sc);
   1485 			error = 0;
   1486 		}
   1487 	}
   1488 
   1489 	npestart(ifp);
   1490 
   1491 	splx(s);
   1492 	return error;
   1493 }
   1494 
   1495 /*
   1496  * Setup a traffic class -> rx queue mapping.
   1497  */
   1498 static int
   1499 npe_setrxqosentry(struct npe_softc *sc, int classix, int trafclass, int qid)
   1500 {
   1501 	int npeid = npeconfig[sc->sc_unit].npeid;
   1502 	uint32_t msg[2];
   1503 
   1504 	msg[0] = (NPE_SETRXQOSENTRY << NPE_MAC_MSGID_SHL) | (npeid << 20)
   1505 	    | classix;
   1506 	msg[1] = (trafclass << 24) | (1 << 23) | (qid << 16) | (qid << 4);
   1507 	return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
   1508 }
   1509 
   1510 /*
   1511  * Update and reset the statistics in the NPE.
   1512  */
   1513 static int
   1514 npe_updatestats(struct npe_softc *sc)
   1515 {
   1516 	uint32_t msg[2];
   1517 
   1518 	msg[0] = NPE_RESETSTATS << NPE_MAC_MSGID_SHL;
   1519 	msg[1] = sc->sc_stats_phys;	/* physical address of stat block */
   1520 	return ixpnpe_sendmsg(sc->sc_npe, msg);		/* NB: no recv */
   1521 }
   1522 
   1523 #if 0
   1524 /*
   1525  * Get the current statistics block.
   1526  */
   1527 static int
   1528 npe_getstats(struct npe_softc *sc)
   1529 {
   1530 	uint32_t msg[2];
   1531 
   1532 	msg[0] = NPE_GETSTATS << NPE_MAC_MSGID_SHL;
   1533 	msg[1] = sc->sc_stats_phys;	/* physical address of stat block */
   1534 	return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
   1535 }
   1536 
   1537 /*
   1538  * Query the image id of the loaded firmware.
   1539  */
   1540 static uint32_t
   1541 npe_getimageid(struct npe_softc *sc)
   1542 {
   1543 	uint32_t msg[2];
   1544 
   1545 	msg[0] = NPE_GETSTATUS << NPE_MAC_MSGID_SHL;
   1546 	msg[1] = 0;
   1547 	return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg) == 0 ? msg[1] : 0;
   1548 }
   1549 
   1550 /*
   1551  * Enable/disable loopback.
   1552  */
   1553 static int
   1554 npe_setloopback(struct npe_softc *sc, int ena)
   1555 {
   1556 	uint32_t msg[2];
   1557 
   1558 	msg[0] = (NPE_SETLOOPBACK << NPE_MAC_MSGID_SHL) | (ena != 0);
   1559 	msg[1] = 0;
   1560 	return ixpnpe_sendandrecvmsg(sc->sc_npe, msg, msg);
   1561 }
   1562 #endif
   1563 
   1564 /*
   1565  * MII bus support routines.
   1566  *
   1567  * NB: ixp425 has one PHY per NPE
   1568  */
   1569 static uint32_t
   1570 npe_mii_mdio_read(struct npe_softc *sc, int reg)
   1571 {
   1572 #define	MII_RD4(sc, reg)	bus_space_read_4(sc->sc_iot, sc->sc_miih, reg)
   1573 	uint32_t v;
   1574 
   1575 	/* NB: registers are known to be sequential */
   1576 	v =  (MII_RD4(sc, reg+0) & 0xff) << 0;
   1577 	v |= (MII_RD4(sc, reg+4) & 0xff) << 8;
   1578 	v |= (MII_RD4(sc, reg+8) & 0xff) << 16;
   1579 	v |= (MII_RD4(sc, reg+12) & 0xff) << 24;
   1580 	return v;
   1581 #undef MII_RD4
   1582 }
   1583 
   1584 static void
   1585 npe_mii_mdio_write(struct npe_softc *sc, int reg, uint32_t cmd)
   1586 {
   1587 #define	MII_WR4(sc, reg, v) \
   1588 	bus_space_write_4(sc->sc_iot, sc->sc_miih, reg, v)
   1589 
   1590 	/* NB: registers are known to be sequential */
   1591 	MII_WR4(sc, reg+0, cmd & 0xff);
   1592 	MII_WR4(sc, reg+4, (cmd >> 8) & 0xff);
   1593 	MII_WR4(sc, reg+8, (cmd >> 16) & 0xff);
   1594 	MII_WR4(sc, reg+12, (cmd >> 24) & 0xff);
   1595 #undef MII_WR4
   1596 }
   1597 
   1598 static int
   1599 npe_mii_mdio_wait(struct npe_softc *sc)
   1600 {
   1601 #define	MAXTRIES	100	/* XXX */
   1602 	uint32_t v;
   1603 	int i;
   1604 
   1605 	for (i = 0; i < MAXTRIES; i++) {
   1606 		v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_CMD);
   1607 		if ((v & NPE_MII_GO) == 0)
   1608 			return 1;
   1609 	}
   1610 	return 0;		/* NB: timeout */
   1611 #undef MAXTRIES
   1612 }
   1613 
   1614 static int
   1615 npe_miibus_readreg(struct device *self, int phy, int reg)
   1616 {
   1617 	struct npe_softc *sc = (void *)self;
   1618 	uint32_t v;
   1619 
   1620 	if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
   1621 		return 0xffff;
   1622 	v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
   1623 	  | NPE_MII_GO;
   1624 	npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
   1625 	if (npe_mii_mdio_wait(sc))
   1626 		v = npe_mii_mdio_read(sc, NPE_MAC_MDIO_STS);
   1627 	else
   1628 		v = 0xffff | NPE_MII_READ_FAIL;
   1629 	return (v & NPE_MII_READ_FAIL) ? 0xffff : (v & 0xffff);
   1630 #undef MAXTRIES
   1631 }
   1632 
   1633 static void
   1634 npe_miibus_writereg(struct device *self, int phy, int reg, int data)
   1635 {
   1636 	struct npe_softc *sc = (void *)self;
   1637 	uint32_t v;
   1638 
   1639 	if (sc->sc_phy > IXPNPECF_PHY_DEFAULT && phy != sc->sc_phy)
   1640 		return;
   1641 	v = (phy << NPE_MII_ADDR_SHL) | (reg << NPE_MII_REG_SHL)
   1642 	  | data | NPE_MII_WRITE
   1643 	  | NPE_MII_GO;
   1644 	npe_mii_mdio_write(sc, NPE_MAC_MDIO_CMD, v);
   1645 	/* XXX complain about timeout */
   1646 	(void) npe_mii_mdio_wait(sc);
   1647 }
   1648 
   1649 static void
   1650 npe_miibus_statchg(struct device *self)
   1651 {
   1652 	struct npe_softc *sc = (void *)self;
   1653 	uint32_t tx1, rx1;
   1654 	uint32_t randoff;
   1655 
   1656 	/* sync MAC duplex state */
   1657 	tx1 = RD4(sc, NPE_MAC_TX_CNTRL1);
   1658 	rx1 = RD4(sc, NPE_MAC_RX_CNTRL1);
   1659 	if (sc->sc_mii.mii_media_active & IFM_FDX) {
   1660 		WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT);
   1661 		tx1 &= ~NPE_TX_CNTRL1_DUPLEX;
   1662 		rx1 |= NPE_RX_CNTRL1_PAUSE_EN;
   1663 	} else {
   1664 		struct timeval now;
   1665 		getmicrotime(&now);
   1666 		randoff = (RD4(sc, NPE_MAC_UNI_ADDR_6) ^ now.tv_usec)
   1667 		    & 0x7f;
   1668 		WR4(sc, NPE_MAC_SLOT_TIME, NPE_MAC_SLOT_TIME_MII_DEFAULT
   1669 		    + randoff);
   1670 		tx1 |= NPE_TX_CNTRL1_DUPLEX;
   1671 		rx1 &= ~NPE_RX_CNTRL1_PAUSE_EN;
   1672 	}
   1673 	WR4(sc, NPE_MAC_RX_CNTRL1, rx1);
   1674 	WR4(sc, NPE_MAC_TX_CNTRL1, tx1);
   1675 }
   1676