Home | History | Annotate | Line # | Download | only in marvell
if_gfe.c revision 1.4
      1 /*	$NetBSD: if_gfe.c,v 1.4 2003/03/18 15:09:28 matt Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2002 Allegro Networks, Inc., Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed for the NetBSD Project by
     18  *      Allegro Networks, Inc., and Wasabi Systems, Inc.
     19  * 4. The name of Allegro Networks, Inc. may not be used to endorse
     20  *    or promote products derived from this software without specific prior
     21  *    written permission.
     22  * 5. The name of Wasabi Systems, Inc. may not be used to endorse
     23  *    or promote products derived from this software without specific prior
     24  *    written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY ALLEGRO NETWORKS, INC. AND
     27  * WASABI SYSTEMS, INC. ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     29  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL EITHER ALLEGRO NETWORKS, INC. OR WASABI SYSTEMS, INC.
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * if_gfe.c -- GT ethernet MAC driver
     42  */
     43 
     44 #include "opt_inet.h"
     45 #include "bpfilter.h"
     46 
     47 #include <sys/param.h>
     48 #include <sys/types.h>
     49 #include <sys/inttypes.h>
     50 #include <sys/queue.h>
     51 
     52 #include <sys/callout.h>
     53 #include <sys/device.h>
     54 #include <sys/errno.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/mbuf.h>
     57 #include <sys/socket.h>
     58 
     59 #include <machine/bus.h>
     60 
     61 #include <net/if.h>
     62 #include <net/if_dl.h>
     63 #include <net/if_ether.h>
     64 #include <net/if_media.h>
     65 
     66 #ifdef INET
     67 #include <netinet/in.h>
     68 #include <netinet/if_inarp.h>
     69 #endif
     70 #if NBPFILTER > 0
     71 #include <net/bpf.h>
     72 #endif
     73 
     74 #include <dev/mii/miivar.h>
     75 
     76 #include <dev/marvell/gtintrreg.h>
     77 #include <dev/marvell/gtethreg.h>
     78 
     79 #include <dev/marvell/gtvar.h>
     80 #include <dev/marvell/if_gfevar.h>
     81 
     82 #define	GE_READ(sc, reg) \
     83 	bus_space_read_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg)
     84 #define	GE_WRITE(sc, reg, v) \
     85 	bus_space_write_4((sc)->sc_gt_memt, (sc)->sc_memh, ETH__ ## reg, (v))
     86 
     87 #define	GE_DEBUG
     88 #if 0
     89 #define	GE_NOHASH
     90 #define	GE_NORX
     91 #endif
     92 
     93 #ifdef GE_DEBUG
     94 #define	GE_DPRINTF(sc, a)	do \
     95 				  if ((sc)->sc_ec.ec_if.if_flags & IFF_DEBUG) \
     96 				    printf a; \
     97 				while (0)
     98 #define	GE_FUNC_ENTER(sc, func)	GE_DPRINTF(sc, ("[" func))
     99 #define	GE_FUNC_EXIT(sc, str)	GE_DPRINTF(sc, (str "]"))
    100 #else
    101 #define	GE_DPRINTF(sc, a)	do { } while (0)
    102 #define	GE_FUNC_ENTER(sc, func)	do { } while (0)
    103 #define	GE_FUNC_EXIT(sc, str)	do { } while (0)
    104 #endif
    105 enum gfe_whack_op {
    106 	GE_WHACK_START,		GE_WHACK_RESTART,
    107 	GE_WHACK_CHANGE,	GE_WHACK_STOP
    108 };
    109 
    110 enum gfe_hash_op {
    111 	GE_HASH_ADD,		GE_HASH_REMOVE,
    112 };
    113 
    114 #if 1
    115 #define	htogt32(a)		htobe32(a)
    116 #define	gt32toh(a)		be32toh(a)
    117 #else
    118 #define	htogt32(a)		htole32(a)
    119 #define	gt32toh(a)		le32toh(a)
    120 #endif
    121 
    122 #define	STATIC
    123 
    124 STATIC int gfe_match (struct device *, struct cfdata *, void *);
    125 STATIC void gfe_attach (struct device *, struct device *, void *);
    126 
    127 STATIC int gfe_dmamem_alloc(struct gfe_softc *, struct gfe_dmamem *, int,
    128 	size_t, int);
    129 STATIC void gfe_dmamem_free(struct gfe_softc *, struct gfe_dmamem *);
    130 
    131 STATIC int gfe_ifioctl (struct ifnet *, u_long, caddr_t);
    132 STATIC void gfe_ifstart (struct ifnet *);
    133 STATIC void gfe_ifwatchdog (struct ifnet *);
    134 
    135 STATIC int gfe_mii_mediachange (struct ifnet *);
    136 STATIC void gfe_mii_mediastatus (struct ifnet *, struct ifmediareq *);
    137 STATIC int gfe_mii_read (struct device *, int, int);
    138 STATIC void gfe_mii_write (struct device *, int, int, int);
    139 STATIC void gfe_mii_statchg (struct device *);
    140 
    141 STATIC void gfe_tick(void *arg);
    142 
    143 STATIC void gfe_tx_restart(void *);
    144 STATIC int gfe_tx_enqueue(struct gfe_softc *, enum gfe_txprio);
    145 STATIC uint32_t gfe_tx_done(struct gfe_softc *, enum gfe_txprio, uint32_t);
    146 STATIC void gfe_tx_cleanup(struct gfe_softc *, enum gfe_txprio, int);
    147 STATIC int gfe_tx_start(struct gfe_softc *, enum gfe_txprio);
    148 STATIC void gfe_tx_stop(struct gfe_softc *, enum gfe_whack_op);
    149 
    150 STATIC void gfe_rx_cleanup(struct gfe_softc *, enum gfe_rxprio);
    151 STATIC void gfe_rx_get(struct gfe_softc *, enum gfe_rxprio);
    152 STATIC int gfe_rx_prime(struct gfe_softc *);
    153 STATIC uint32_t gfe_rx_process(struct gfe_softc *, uint32_t, uint32_t);
    154 STATIC int gfe_rx_rxqalloc(struct gfe_softc *, enum gfe_rxprio);
    155 STATIC void gfe_rx_stop(struct gfe_softc *, enum gfe_whack_op);
    156 
    157 STATIC int gfe_intr(void *);
    158 
    159 STATIC int gfe_whack(struct gfe_softc *, enum gfe_whack_op);
    160 
    161 STATIC int gfe_hash_compute(struct gfe_softc *, const u_int8_t [ETHER_ADDR_LEN]);
    162 STATIC int gfe_hash_entry_op(struct gfe_softc *, enum gfe_hash_op,
    163 	enum gfe_rxprio, const u_int8_t [ETHER_ADDR_LEN]);
    164 STATIC int gfe_hash_multichg(struct ethercom *, const struct ether_multi *,
    165 	u_long);
    166 STATIC int gfe_hash_fill(struct gfe_softc *);
    167 STATIC int gfe_hash_alloc(struct gfe_softc *);
    168 
    169 /* Linkup to the rest of the kernel */
    170 CFATTACH_DECL(gfe, sizeof(struct gfe_softc),
    171     gfe_match, gfe_attach, NULL, NULL);
    172 
    173 extern struct cfdriver gfe_cd;
    174 
    175 int
    176 gfe_match(struct device *parent, struct cfdata *cf, void *aux)
    177 {
    178 	struct gt_softc *gt = (struct gt_softc *) parent;
    179 	struct gt_attach_args *ga = aux;
    180 	uint8_t enaddr[6];
    181 
    182 	if (!GT_ETHEROK(gt, ga, &gfe_cd))
    183 		return 0;
    184 
    185 	if (gtget_macaddr(gt, ga->ga_unit, enaddr) < 0)
    186 		return 0;
    187 
    188 	if (enaddr[0] == 0 && enaddr[1] == 0 && enaddr[2] == 0 &&
    189 	    enaddr[3] == 0 && enaddr[4] == 0 && enaddr[5] == 0)
    190 		return 0;
    191 
    192 	return 1;
    193 }
    194 
    195 /*
    196  * Attach this instance, and then all the sub-devices
    197  */
    198 void
    199 gfe_attach(struct device *parent, struct device *self, void *aux)
    200 {
    201 	struct gt_attach_args *ga = aux;
    202 	struct gt_softc *gt = (struct gt_softc *) parent;
    203 	struct gfe_softc *sc = (struct gfe_softc *) self;
    204 	struct ifnet *ifp;
    205 	uint32_t data;
    206 	uint8_t enaddr[6];
    207 	int phyaddr;
    208 	uint32_t sdcr;
    209 
    210 	GT_ETHERFOUND(gt, ga);
    211 
    212 	sc->sc_gt_memt = ga->ga_memt;
    213 	sc->sc_gt_memh = ga->ga_memh;
    214 	sc->sc_dmat = ga->ga_dmat;
    215 	sc->sc_macno = ga->ga_unit;
    216 
    217 	if (bus_space_subregion(sc->sc_gt_memt, sc->sc_gt_memh,
    218 		    ETH_BASE(sc->sc_macno), ETH_SIZE, &sc->sc_memh)) {
    219 		aprint_error(": failed to map registers\n");
    220 	}
    221 
    222 	callout_init(&sc->sc_co);
    223 
    224 	data = bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, ETH_EPAR);
    225 	phyaddr = ETH_EPAR_PhyAD_GET(data, sc->sc_macno);
    226 
    227 	gtget_macaddr(gt, sc->sc_macno, enaddr);
    228 
    229 	sc->sc_pcr = GE_READ(sc, EPCR);
    230 	sc->sc_pcxr = GE_READ(sc, EPCXR);
    231 	sc->sc_intrmask = GE_READ(sc, EIMR) | ETH_IR_MIIPhySTC;
    232 
    233 	aprint_normal(": address %s", ether_sprintf(enaddr));
    234 
    235 #if defined(DEBUG)
    236 	aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
    237 #endif
    238 
    239 	sc->sc_pcxr &= ~ETH_EPCXR_PRIOrx_Override;
    240 	if (sc->sc_dev.dv_cfdata->cf_flags & 1) {
    241 		aprint_normal(", phy %d (rmii)", phyaddr);
    242 		sc->sc_pcxr |= ETH_EPCXR_RMIIEn;
    243 	} else {
    244 		aprint_normal(", phy %d (mii)", phyaddr);
    245 		sc->sc_pcxr &= ~ETH_EPCXR_RMIIEn;
    246 	}
    247 	sc->sc_pcxr &= ~(3 << 14);
    248 	sc->sc_pcxr |= (ETH_EPCXR_MFL_1536 << 14);
    249 
    250 	if (sc->sc_pcr & ETH_EPCR_EN) {
    251 		int tries = 1000;
    252 		/*
    253 		 * Abort transmitter and receiver and wait for them to quiese
    254 		 */
    255 		GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR|ETH_ESDCMR_AT);
    256 		do {
    257 			delay(100);
    258 		} while (tries-- > 0 && (GE_READ(sc, ESDCMR) & (ETH_ESDCMR_AR|ETH_ESDCMR_AT)));
    259 	}
    260 
    261 	sc->sc_pcr &= ~ETH_EPCR_EN;
    262 
    263 #if defined(DEBUG)
    264 	aprint_normal(", pcr %#x, pcxr %#x", sc->sc_pcr, sc->sc_pcxr);
    265 #endif
    266 
    267 	/*
    268 	 * Now turn off the GT.  If it didn't quiese, too ***ing bad.
    269 	 */
    270 	GE_WRITE(sc, EPCR, sc->sc_pcr);
    271 	GE_WRITE(sc, EIMR, sc->sc_intrmask);
    272 	sdcr = GE_READ(sc, ESDCR);
    273 	ETH_ESDCR_BSZ_SET(sdcr, ETH_ESDCR_BSZ_4);
    274 	sdcr |= ETH_ESDCR_RIFB;
    275 	GE_WRITE(sc, ESDCR, sdcr);
    276 	sc->sc_max_frame_length = 1536;
    277 
    278 	aprint_normal("\n");
    279 	sc->sc_mii.mii_ifp = &sc->sc_ec.ec_if;
    280 	sc->sc_mii.mii_readreg = gfe_mii_read;
    281 	sc->sc_mii.mii_writereg = gfe_mii_write;
    282 	sc->sc_mii.mii_statchg = gfe_mii_statchg;
    283 
    284 	ifmedia_init(&sc->sc_mii.mii_media, 0, gfe_mii_mediachange,
    285 		gfe_mii_mediastatus);
    286 
    287 	mii_attach(&sc->sc_dev, &sc->sc_mii, 0xffffffff, phyaddr,
    288 		MII_OFFSET_ANY, MIIF_NOISOLATE);
    289 	if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
    290 		ifmedia_add(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL);
    291 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_NONE);
    292 	} else {
    293 		ifmedia_set(&sc->sc_mii.mii_media, IFM_ETHER|IFM_AUTO);
    294 	}
    295 
    296 	ifp = &sc->sc_ec.ec_if;
    297 	strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
    298 	ifp->if_softc = sc;
    299 	/* ifp->if_mowner = &sc->sc_mowner; */
    300 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    301 #if 0
    302 	ifp->if_flags |= IFF_DEBUG;
    303 #endif
    304 	ifp->if_ioctl = gfe_ifioctl;
    305 	ifp->if_start = gfe_ifstart;
    306 	ifp->if_watchdog = gfe_ifwatchdog;
    307 
    308 	if_attach(ifp);
    309 	ether_ifattach(ifp, enaddr);
    310 #if NBPFILTER > 0
    311 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
    312 #endif
    313 #if NRND > 0
    314 	rnd_attach_source(&sc->sc_rnd_source, self->dv_xname, RND_TYPE_NET, 0);
    315 #endif
    316 	intr_establish(IRQ_ETH0 + sc->sc_macno, IST_LEVEL, IPL_NET,
    317 	    gfe_intr, sc);
    318 }
    319 
    320 int
    321 gfe_dmamem_alloc(struct gfe_softc *sc, struct gfe_dmamem *gdm, int maxsegs,
    322 	size_t size, int flags)
    323 {
    324 	int error = 0;
    325 	GE_FUNC_ENTER(sc, "gfe_dmamem_alloc");
    326 	gdm->gdm_size = size;
    327 	gdm->gdm_maxsegs = maxsegs;
    328 
    329 #if 1
    330 	flags |= BUS_DMA_NOCACHE;
    331 #endif
    332 
    333 	error = bus_dmamem_alloc(sc->sc_dmat, gdm->gdm_size, NBPG,
    334 	    gdm->gdm_size, gdm->gdm_segs, gdm->gdm_maxsegs, &gdm->gdm_nsegs,
    335 	    BUS_DMA_NOWAIT);
    336 	if (error)
    337 		goto fail;
    338 
    339 	error = bus_dmamem_map(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs,
    340 	    gdm->gdm_size, &gdm->gdm_kva, flags | BUS_DMA_NOWAIT);
    341 	if (error)
    342 		goto fail;
    343 
    344 	error = bus_dmamap_create(sc->sc_dmat, gdm->gdm_size, gdm->gdm_nsegs,
    345 	    gdm->gdm_size, 0, BUS_DMA_ALLOCNOW|BUS_DMA_NOWAIT, &gdm->gdm_map);
    346 	if (error)
    347 		goto fail;
    348 
    349 	error = bus_dmamap_load(sc->sc_dmat, gdm->gdm_map, gdm->gdm_kva,
    350 	    gdm->gdm_size, NULL, BUS_DMA_NOWAIT);
    351 	if (error)
    352 		goto fail;
    353 
    354 	/* invalidate from cache */
    355 	bus_dmamap_sync(sc->sc_dmat, gdm->gdm_map, 0, gdm->gdm_size,
    356 	    BUS_DMASYNC_PREREAD);
    357 fail:
    358 	if (error) {
    359 		gfe_dmamem_free(sc, gdm);
    360 		GE_DPRINTF(sc, (":err=%d", error));
    361 	}
    362 	GE_DPRINTF(sc, (":kva=%p/%#x,map=%p,nsegs=%d,pa=%x/%x",
    363 	    gdm->gdm_kva, gdm->gdm_size, gdm->gdm_map, gdm->gdm_map->dm_nsegs,
    364 	    gdm->gdm_map->dm_segs->ds_addr, gdm->gdm_map->dm_segs->ds_len));
    365 	GE_FUNC_EXIT(sc, "");
    366 	return error;
    367 }
    368 
    369 void
    370 gfe_dmamem_free(struct gfe_softc *sc, struct gfe_dmamem *gdm)
    371 {
    372 	GE_FUNC_ENTER(sc, "gfe_dmamem_free");
    373 	if (gdm->gdm_map)
    374 		bus_dmamap_destroy(sc->sc_dmat, gdm->gdm_map);
    375 	if (gdm->gdm_kva)
    376 		bus_dmamem_unmap(sc->sc_dmat, gdm->gdm_kva, gdm->gdm_size);
    377 	if (gdm->gdm_nsegs > 0)
    378 		bus_dmamem_free(sc->sc_dmat, gdm->gdm_segs, gdm->gdm_nsegs);
    379 	gdm->gdm_map = NULL;
    380 	gdm->gdm_kva = NULL;
    381 	gdm->gdm_nsegs = 0;
    382 	GE_FUNC_EXIT(sc, "");
    383 }
    384 
    385 int
    386 gfe_ifioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
    387 {
    388 	struct gfe_softc * const sc = ifp->if_softc;
    389 	struct ifreq *ifr = (struct ifreq *) data;
    390 	struct ifaddr *ifa = (struct ifaddr *) data;
    391 	int s, error = 0;
    392 
    393 	GE_FUNC_ENTER(sc, "gfe_ifioctl");
    394 	s = splnet();
    395 
    396 	switch (cmd) {
    397 	case SIOCSIFADDR:
    398 		ifp->if_flags |= IFF_UP;
    399 		switch (ifa->ifa_addr->sa_family) {
    400 #ifdef INET
    401 		case AF_INET:
    402 			error = gfe_whack(sc, GE_WHACK_START);
    403 			if (error == 0)
    404 				arp_ifinit(ifp, ifa);
    405 			break;
    406 #endif
    407 		default:
    408 			error = gfe_whack(sc, GE_WHACK_START);
    409 			break;
    410 		}
    411 		break;
    412 
    413 	case SIOCSIFFLAGS:
    414 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
    415 		case IFF_UP|IFF_RUNNING:/* active->active, update */
    416 			error = gfe_whack(sc, GE_WHACK_CHANGE);
    417 			break;
    418 		case IFF_RUNNING:	/* not up, so we stop */
    419 			error = gfe_whack(sc, GE_WHACK_STOP);
    420 			break;
    421 		case IFF_UP:		/* not running, so we start */
    422 			error = gfe_whack(sc, GE_WHACK_START);
    423 			break;
    424 		case 0:			/* idle->idle: do nothing */
    425 			break;
    426 		}
    427 		break;
    428 
    429 	case SIOCADDMULTI:
    430 	case SIOCDELMULTI:
    431 		error = (cmd == SIOCADDMULTI)
    432 		    ? ether_addmulti(ifr, &sc->sc_ec)
    433 		    : ether_delmulti(ifr, &sc->sc_ec);
    434 		if (error == ENETRESET) {
    435 			if (ifp->if_flags & IFF_RUNNING)
    436 				error = gfe_whack(sc, GE_WHACK_CHANGE);
    437 			else
    438 				error = 0;
    439 		}
    440 		break;
    441 
    442 	case SIOCSIFMTU:
    443 		if (ifr->ifr_mtu > ETHERMTU || ifr->ifr_mtu < ETHERMIN) {
    444 			error = EINVAL;
    445 			break;
    446 		}
    447 		ifp->if_mtu = ifr->ifr_mtu;
    448 		break;
    449 
    450 	case SIOCSIFMEDIA:
    451 	case SIOCGIFMEDIA:
    452 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii.mii_media, cmd);
    453 		break;
    454 
    455 	default:
    456 		error = EINVAL;
    457 		break;
    458 	}
    459 	splx(s);
    460 	GE_FUNC_EXIT(sc, "");
    461 	return error;
    462 }
    463 
    464 void
    465 gfe_ifstart(struct ifnet *ifp)
    466 {
    467 	struct gfe_softc * const sc = ifp->if_softc;
    468 	struct mbuf *m;
    469 
    470 	GE_FUNC_ENTER(sc, "gfe_ifstart");
    471 
    472 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
    473 		GE_FUNC_EXIT(sc, "$");
    474 		return;
    475 	}
    476 
    477 	if (sc->sc_txq[GE_TXPRIO_HI] == NULL) {
    478 		ifp->if_flags |= IFF_OACTIVE;
    479 #if defined(DEBUG) || defined(DIAGNOSTIC)
    480 		printf("%s: ifstart: txq not yet created\n", ifp->if_xname);
    481 #endif
    482 		GE_FUNC_EXIT(sc, "");
    483 		return;
    484 	}
    485 
    486 	for (;;) {
    487 		IF_DEQUEUE(&ifp->if_snd, m);
    488 		if (m == NULL) {
    489 			ifp->if_flags &= ~IFF_OACTIVE;
    490 			GE_FUNC_EXIT(sc, "");
    491 			return;
    492 		}
    493 
    494 		/*
    495 		 * No space in the pending queue?  try later.
    496 		 */
    497 		if (IF_QFULL(&sc->sc_txq[GE_TXPRIO_HI]->txq_pendq))
    498 			break;
    499 
    500 		/*
    501 		 * Try to enqueue a mbuf to the device. If that fails, we
    502 		 * can always try to map the next mbuf.
    503 		 */
    504 		IF_ENQUEUE(&sc->sc_txq[GE_TXPRIO_HI]->txq_pendq, m);
    505 		GE_DPRINTF(sc, (">"));
    506 #ifndef GE_NOTX
    507 		(void) gfe_tx_enqueue(sc, GE_TXPRIO_HI);
    508 #endif
    509 	}
    510 
    511 	/*
    512 	 * Attempt to queue the mbuf for send failed.
    513 	 */
    514 	IF_PREPEND(&ifp->if_snd, m);
    515 	ifp->if_flags |= IFF_OACTIVE;
    516 	GE_FUNC_EXIT(sc, "%%");
    517 }
    518 
    519 void
    520 gfe_ifwatchdog(struct ifnet *ifp)
    521 {
    522 	struct gfe_softc * const sc = ifp->if_softc;
    523 	struct gfe_txqueue *txq;
    524 
    525 	GE_FUNC_ENTER(sc, "gfe_ifwatchdog");
    526 	printf("%s: device timeout",
    527 		sc->sc_dev.dv_xname);
    528 	if ((txq = sc->sc_txq[GE_TXPRIO_HI]) != NULL) {
    529 		unsigned int curtxdnum = (bus_space_read_4(sc->sc_gt_memt, sc->sc_gt_memh, txq->txq_ectdp) - txq->txq_desc_busaddr) / 16;
    530 		printf(" (fi=%d,lo=%d,cur=%d(%#x),icm=%#x) ",
    531 		    txq->txq_fi, txq->txq_lo, curtxdnum,
    532 		    txq->txq_descs[curtxdnum].ed_cmdsts,
    533 		    GE_READ(sc, EICR));
    534 	}
    535 	printf("\n");
    536 	ifp->if_oerrors++;
    537 	(void) gfe_whack(sc, GE_WHACK_RESTART);
    538 	GE_FUNC_EXIT(sc, "");
    539 }
    540 
    541 int
    543 gfe_rx_rxqalloc(struct gfe_softc *sc, enum gfe_rxprio rxprio)
    544 {
    545 	struct gfe_rxqueue *rxq;
    546 	volatile struct gt_eth_desc *rxd;
    547 	const bus_dma_segment_t *ds;
    548 	int error;
    549 	int idx;
    550 	bus_addr_t nxtaddr;
    551 	bus_size_t boff;
    552 
    553 	GE_FUNC_ENTER(sc, "gfe_rx_rxqalloc");
    554 	GE_DPRINTF(sc, ("(%d)", rxprio));
    555 	if (sc->sc_rxq[rxprio] != NULL) {
    556 		GE_FUNC_EXIT(sc, "");
    557 		return 0;
    558 	}
    559 
    560 	rxq = (struct gfe_rxqueue *) malloc(sizeof(*rxq), M_DEVBUF, M_NOWAIT);
    561 	if (rxq == NULL) {
    562 		GE_FUNC_EXIT(sc, "!");
    563 		return ENOMEM;
    564 	}
    565 
    566 	memset(rxq, 0, sizeof(*rxq));
    567 
    568 	error = gfe_dmamem_alloc(sc, &rxq->rxq_desc_mem, 1,
    569 	    GE_RXDESC_MEMSIZE, 0);
    570 	if (error) {
    571 		free(rxq, M_DEVBUF);
    572 		GE_FUNC_EXIT(sc, "!!");
    573 		return error;
    574 	}
    575 	error = gfe_dmamem_alloc(sc, &rxq->rxq_buf_mem, GE_RXBUF_NSEGS,
    576 	    GE_RXBUF_MEMSIZE, 0);
    577 	if (error) {
    578 		gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
    579 		free(rxq, M_DEVBUF);
    580 		GE_FUNC_EXIT(sc, "!!!");
    581 		return error;
    582 	}
    583 
    584 	memset(rxq->rxq_desc_mem.gdm_kva, 0, GE_TXMEM_SIZE);
    585 
    586 	sc->sc_rxq[rxprio] = rxq;
    587 	rxq->rxq_descs =
    588 	    (volatile struct gt_eth_desc *) rxq->rxq_desc_mem.gdm_kva;
    589 	rxq->rxq_desc_busaddr = rxq->rxq_desc_mem.gdm_map->dm_segs[0].ds_addr;
    590 	rxq->rxq_bufs = (struct gfe_rxbuf *) rxq->rxq_buf_mem.gdm_kva;
    591 	rxq->rxq_fi = 0;
    592 	rxq->rxq_active = GE_RXDESC_MAX;
    593 	for (idx = 0, rxd = rxq->rxq_descs,
    594 		boff = 0, ds = rxq->rxq_buf_mem.gdm_map->dm_segs,
    595 		nxtaddr = rxq->rxq_desc_busaddr + sizeof(*rxd);
    596 	     idx < GE_RXDESC_MAX;
    597 	     idx++, rxd++, nxtaddr += sizeof(*rxd)) {
    598 		rxd->ed_lencnt = htogt32(GE_RXBUF_SIZE << 16);
    599 		rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
    600 		rxd->ed_bufptr = htogt32(ds->ds_addr + boff);
    601 		/*
    602 		 * update the nxtptr to point to the next txd.
    603 		 */
    604 		if (idx == GE_RXDESC_MAX - 1)
    605 			nxtaddr = rxq->rxq_desc_busaddr;
    606 		rxd->ed_nxtptr = htogt32(nxtaddr);
    607 		boff += GE_RXBUF_SIZE;
    608 		if (boff == ds->ds_len) {
    609 			ds++;
    610 			boff = 0;
    611 		}
    612 	}
    613 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map, 0,
    614 			rxq->rxq_desc_mem.gdm_map->dm_mapsize,
    615 			BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    616 	bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map, 0,
    617 			rxq->rxq_buf_mem.gdm_map->dm_mapsize,
    618 			BUS_DMASYNC_PREREAD);
    619 
    620 	rxq->rxq_intrbits = ETH_IR_RxBuffer|ETH_IR_RxError;
    621 	switch (rxprio) {
    622 	case GE_RXPRIO_HI:
    623 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_3|ETH_IR_RxError_3;
    624 		rxq->rxq_efrdp = ETH_EFRDP3(sc->sc_macno);
    625 		rxq->rxq_ecrdp = ETH_ECRDP3(sc->sc_macno);
    626 		break;
    627 	case GE_RXPRIO_MEDHI:
    628 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_2|ETH_IR_RxError_2;
    629 		rxq->rxq_efrdp = ETH_EFRDP2(sc->sc_macno);
    630 		rxq->rxq_ecrdp = ETH_ECRDP2(sc->sc_macno);
    631 		break;
    632 	case GE_RXPRIO_MEDLO:
    633 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_1|ETH_IR_RxError_1;
    634 		rxq->rxq_efrdp = ETH_EFRDP1(sc->sc_macno);
    635 		rxq->rxq_ecrdp = ETH_ECRDP1(sc->sc_macno);
    636 		break;
    637 	case GE_RXPRIO_LO:
    638 		rxq->rxq_intrbits |= ETH_IR_RxBuffer_0|ETH_IR_RxError_0;
    639 		rxq->rxq_efrdp = ETH_EFRDP0(sc->sc_macno);
    640 		rxq->rxq_ecrdp = ETH_ECRDP0(sc->sc_macno);
    641 		break;
    642 	}
    643 	GE_FUNC_EXIT(sc, "");
    644 	return error;
    645 }
    646 
    647 void
    648 gfe_rx_get(struct gfe_softc *sc, enum gfe_rxprio rxprio)
    649 {
    650 	struct ifnet * const ifp = &sc->sc_ec.ec_if;
    651 	struct gfe_rxqueue * const rxq = sc->sc_rxq[rxprio];
    652 	struct mbuf *m = rxq->rxq_curpkt;
    653 
    654 	GE_FUNC_ENTER(sc, "gfe_rx_get");
    655 	GE_DPRINTF(sc, ("(%d)", rxprio));
    656 
    657 	while (rxq->rxq_active > 0) {
    658 		volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[rxq->rxq_fi];
    659 		struct gfe_rxbuf *rxb = &rxq->rxq_bufs[rxq->rxq_fi];
    660 		const struct ether_header *eh;
    661 		unsigned int cmdsts;
    662 		size_t buflen;
    663 
    664 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
    665 				rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
    666 				BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    667 		cmdsts = gt32toh(rxd->ed_cmdsts);
    668 		GE_DPRINTF(sc, (":%d=%#x", rxq->rxq_fi, cmdsts));
    669 		rxq->rxq_cmdsts = cmdsts;
    670 		/*
    671 		 * Sometimes the GE "forgets" to reset the ownership bit.
    672 		 * But if the length has been rewritten, the packet is ours
    673 		 * so pretend the O bit is set.
    674 		 */
    675 		buflen = gt32toh(rxd->ed_lencnt) & 0xffff;
    676 		if ((cmdsts & RX_CMD_O) && buflen == 0) {
    677 			bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
    678 				rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
    679 				BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    680 			break;
    681 		}
    682 
    683 		/*
    684 		 * If this is not a single buffer packet with no errors
    685 		 * or for some reason it's bigger than our frame size,
    686 		 * ignore it and go to the next packet.
    687 		 */
    688 		if ((cmdsts & (RX_CMD_F|RX_CMD_L|RX_STS_ES)) !=
    689 			    (RX_CMD_F|RX_CMD_L) ||
    690 		    buflen > sc->sc_max_frame_length) {
    691 			GE_DPRINTF(sc, ("!"));
    692 			--rxq->rxq_active;
    693 			ifp->if_ipackets++;
    694 			ifp->if_ierrors++;
    695 			goto give_it_back;
    696 		}
    697 
    698 		if (m == NULL) {
    699 			MGETHDR(m, M_DONTWAIT, MT_DATA);
    700 			if (m == NULL) {
    701 				GE_DPRINTF(sc, ("?"));
    702 				break;
    703 			}
    704 			m->m_data += 2;
    705 		}
    706 		if ((m->m_flags & M_EXT) == 0 && buflen > MHLEN - 2) {
    707 			MCLGET(m, M_DONTWAIT);
    708 			if ((m->m_flags & M_EXT) == 0) {
    709 				GE_DPRINTF(sc, ("?"));
    710 				break;
    711 			}
    712 			m->m_data += 2;
    713 		}
    714 		m->m_len = 0;
    715 		m->m_pkthdr.len = 0;
    716 		m->m_pkthdr.rcvif = &sc->sc_ec.ec_if;
    717 		rxq->rxq_cmdsts = cmdsts;
    718 		--rxq->rxq_active;
    719 
    720 		ifp->if_ibytes += buflen;
    721 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_buf_mem.gdm_map,
    722 		    rxq->rxq_fi * sizeof(*rxb), buflen, BUS_DMASYNC_POSTREAD);
    723 
    724 		KASSERT(m->m_len == 0 && m->m_pkthdr.len == 0);
    725 		memcpy(m->m_data + m->m_len, rxb->rb_data, buflen);
    726 		m->m_len = buflen;
    727 		m->m_pkthdr.len = buflen;
    728 		m->m_flags |= M_HASFCS;
    729 
    730 		ifp->if_ipackets++;
    731 #if NBPFILTER > 0
    732 		if (ifp->if_bpf != NULL)
    733 			bpf_mtap(ifp->if_bpf, m);
    734 #endif
    735 
    736 		eh = (const struct ether_header *) m->m_data;
    737 		if ((ifp->if_flags & IFF_PROMISC) ||
    738 		    (rxq->rxq_cmdsts & RX_STS_M) == 0 ||
    739 		    (rxq->rxq_cmdsts & RX_STS_HE) ||
    740 		    (eh->ether_dhost[0] & 1) != 0 ||
    741 		    memcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
    742 			ETHER_ADDR_LEN) == 0) {
    743 			(*ifp->if_input)(ifp, m);
    744 			m = NULL;
    745 			GE_DPRINTF(sc, (">"));
    746 		} else {
    747 			m->m_len = 0;
    748 			m->m_pkthdr.len = 0;
    749 			GE_DPRINTF(sc, ("+"));
    750 		}
    751 		rxq->rxq_cmdsts = 0;
    752 
    753 	   give_it_back:
    754 		rxd->ed_lencnt &= ~0xffff;	/* zero out length */
    755 		rxd->ed_cmdsts = htogt32(RX_CMD_F|RX_CMD_L|RX_CMD_O|RX_CMD_EI);
    756 #if 0
    757 		GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)",
    758 		    rxq->rxq_fi,
    759 		    ((unsigned long *)rxd)[0], ((unsigned long *)rxd)[1],
    760 		    ((unsigned long *)rxd)[2], ((unsigned long *)rxd)[3]));
    761 #endif
    762 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
    763 				rxq->rxq_fi * sizeof(*rxd), sizeof(*rxd),
    764 				BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    765 #if 0
    766 		/*
    767 		 * Can't touch descriptor after doing a dmamap_sync.
    768 		 */
    769 		rxq->rxq_fi = (rxd->ed_nxtptr - rxq->rxq_desc_busaddr) /
    770 			sizeof(*rxd);
    771 #else
    772 		if (++rxq->rxq_fi == GE_RXDESC_MAX)
    773 			rxq->rxq_fi = 0;
    774 #endif
    775 		rxq->rxq_active++;
    776 	}
    777 	rxq->rxq_curpkt = m;
    778 	GE_FUNC_EXIT(sc, "");
    779 }
    780 
    781 uint32_t
    782 gfe_rx_process(struct gfe_softc *sc, uint32_t cause, uint32_t intrmask)
    783 {
    784 	struct gfe_rxqueue *rxq;
    785 	uint32_t rxbits;
    786 #define	RXPRIO_DECODER	0xffffaa50
    787 	GE_FUNC_ENTER(sc, "gfe_rx_process");
    788 
    789 	rxbits = ETH_IR_RxBuffer_GET(cause);
    790 	while (rxbits) {
    791 		enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
    792 		GE_DPRINTF(sc, ("%1x", rxbits));
    793 		rxbits &= ~(1 << rxprio);
    794 		gfe_rx_get(sc, rxprio);
    795 	}
    796 
    797 	rxbits = ETH_IR_RxError_GET(cause);
    798 	while (rxbits) {
    799 		enum gfe_rxprio rxprio = (RXPRIO_DECODER >> (rxbits * 2)) & 3;
    800 		uint32_t masks[(GE_RXDESC_MAX + 31) / 32];
    801 		int idx;
    802 		rxbits &= ~(1 << rxprio);
    803 		rxq = sc->sc_rxq[rxprio];
    804 		sc->sc_idlemask |= (rxq->rxq_intrbits & ETH_IR_RxBits);
    805 		intrmask &= ~(rxq->rxq_intrbits & ETH_IR_RxBits);
    806 		if ((sc->sc_tickflags & GE_TICK_RX_RESTART) == 0) {
    807 			sc->sc_tickflags |= GE_TICK_RX_RESTART;
    808 			callout_reset(&sc->sc_co, 1, gfe_tick, sc);
    809 		}
    810 		sc->sc_ec.ec_if.if_ierrors++;
    811 		GE_DPRINTF(sc, ("%s: rx queue %d filled at %u\n",
    812 		    sc->sc_dev.dv_xname, rxprio, rxq->rxq_fi));
    813 		memset(masks, 0, sizeof(masks));
    814 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
    815 		    0, rxq->rxq_desc_mem.gdm_size,
    816 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
    817 		for (idx = 0; idx < GE_RXDESC_MAX; idx++) {
    818 			volatile struct gt_eth_desc *rxd = &rxq->rxq_descs[idx];
    819 
    820 			if (RX_CMD_O & gt32toh(rxd->ed_cmdsts))
    821 				masks[idx/32] |= 1 << (idx & 31);
    822 		}
    823 		bus_dmamap_sync(sc->sc_dmat, rxq->rxq_desc_mem.gdm_map,
    824 		    0, rxq->rxq_desc_mem.gdm_size,
    825 		    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
    826 #if defined(DEBUG)
    827 		printf("%s: rx queue %d filled at %u=%#x(%#x/%#x)\n",
    828 		    sc->sc_dev.dv_xname, rxprio, rxq->rxq_fi,
    829 		    rxq->rxq_cmdsts, masks[0], masks[1]);
    830 #endif
    831 	}
    832 	if ((intrmask & ETH_IR_RxBits) == 0)
    833 		intrmask &= ~(ETH_IR_RxBuffer|ETH_IR_RxError);
    834 
    835 	GE_FUNC_EXIT(sc, "");
    836 	return intrmask;
    837 }
    838 
    839 int
    840 gfe_rx_prime(struct gfe_softc *sc)
    841 {
    842 	struct gfe_rxqueue *rxq;
    843 	int error;
    844 
    845 	GE_FUNC_ENTER(sc, "gfe_rx_prime");
    846 
    847 	error = gfe_rx_rxqalloc(sc, GE_RXPRIO_HI);
    848 	if (error)
    849 		goto bail;
    850 	rxq = sc->sc_rxq[GE_RXPRIO_HI];
    851 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
    852 		GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
    853 		GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
    854 	}
    855 	sc->sc_intrmask |= rxq->rxq_intrbits;
    856 
    857 	error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDHI);
    858 	if (error)
    859 		goto bail;
    860 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
    861 		rxq = sc->sc_rxq[GE_RXPRIO_MEDHI];
    862 		GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
    863 		GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
    864 		sc->sc_intrmask |= rxq->rxq_intrbits;
    865 	}
    866 
    867 	error = gfe_rx_rxqalloc(sc, GE_RXPRIO_MEDLO);
    868 	if (error)
    869 		goto bail;
    870 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
    871 		rxq = sc->sc_rxq[GE_RXPRIO_MEDLO];
    872 		GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
    873 		GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
    874 		sc->sc_intrmask |= rxq->rxq_intrbits;
    875 	}
    876 
    877 	error = gfe_rx_rxqalloc(sc, GE_RXPRIO_LO);
    878 	if (error)
    879 		goto bail;
    880 	if ((sc->sc_flags & GE_RXACTIVE) == 0) {
    881 		rxq = sc->sc_rxq[GE_RXPRIO_LO];
    882 		GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
    883 		GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
    884 		sc->sc_intrmask |= rxq->rxq_intrbits;
    885 	}
    886 
    887   bail:
    888 	GE_FUNC_EXIT(sc, "");
    889 	return error;
    890 }
    891 
    892 void
    893 gfe_rx_cleanup(struct gfe_softc *sc, enum gfe_rxprio rxprio)
    894 {
    895 	struct gfe_rxqueue *rxq = sc->sc_rxq[rxprio];
    896 	GE_FUNC_ENTER(sc, "gfe_rx_cleanup");
    897 	if (rxq == NULL) {
    898 		GE_FUNC_EXIT(sc, "");
    899 		return;
    900 	}
    901 
    902 	if (rxq->rxq_curpkt)
    903 		m_freem(rxq->rxq_curpkt);
    904 	gfe_dmamem_free(sc, &rxq->rxq_desc_mem);
    905 	gfe_dmamem_free(sc, &rxq->rxq_buf_mem);
    906 	free(rxq, M_DEVBUF);
    907 	sc->sc_rxq[rxprio] = NULL;
    908 	GE_FUNC_EXIT(sc, "");
    909 }
    910 
    911 void
    912 gfe_rx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
    913 {
    914 	GE_FUNC_ENTER(sc, "gfe_rx_stop");
    915 	sc->sc_flags &= ~GE_RXACTIVE;
    916 	sc->sc_idlemask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
    917 	sc->sc_intrmask &= ~(ETH_IR_RxBits|ETH_IR_RxBuffer|ETH_IR_RxError);
    918 	GE_WRITE(sc, EIMR, sc->sc_intrmask);
    919 	GE_WRITE(sc, ESDCMR, ETH_ESDCMR_AR);
    920 	do {
    921 		delay(10);
    922 	} while (GE_READ(sc, ESDCMR) & ETH_ESDCMR_AR);
    923 	gfe_rx_cleanup(sc, GE_RXPRIO_HI);
    924 	gfe_rx_cleanup(sc, GE_RXPRIO_MEDHI);
    925 	gfe_rx_cleanup(sc, GE_RXPRIO_MEDLO);
    926 	gfe_rx_cleanup(sc, GE_RXPRIO_LO);
    927 	GE_FUNC_EXIT(sc, "");
    928 }
    929 
    930 void
    932 gfe_tick(void *arg)
    933 {
    934 	struct gfe_softc * const sc = arg;
    935 	uint32_t intrmask;
    936 	unsigned int tickflags;
    937 	int s;
    938 
    939 	GE_FUNC_ENTER(sc, "gfe_tick");
    940 
    941 	s = splnet();
    942 
    943 	tickflags = sc->sc_tickflags;
    944 	sc->sc_tickflags = 0;
    945 	intrmask = sc->sc_intrmask;
    946 	if (tickflags & GE_TICK_TX_IFSTART)
    947 		gfe_ifstart(&sc->sc_ec.ec_if);
    948 	if (tickflags & GE_TICK_RX_RESTART) {
    949 		intrmask |= sc->sc_idlemask;
    950 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_3|ETH_IR_RxError_3)) {
    951 			struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_HI];
    952 			rxq->rxq_fi = 0;
    953 			GE_WRITE(sc, EFRDP3, rxq->rxq_desc_busaddr);
    954 			GE_WRITE(sc, ECRDP3, rxq->rxq_desc_busaddr);
    955 		}
    956 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_2|ETH_IR_RxError_2)) {
    957 			struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_MEDHI];
    958 			rxq->rxq_fi = 0;
    959 			GE_WRITE(sc, EFRDP2, rxq->rxq_desc_busaddr);
    960 			GE_WRITE(sc, ECRDP2, rxq->rxq_desc_busaddr);
    961 		}
    962 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_1|ETH_IR_RxError_1)) {
    963 			struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_MEDLO];
    964 			rxq->rxq_fi = 0;
    965 			GE_WRITE(sc, EFRDP1, rxq->rxq_desc_busaddr);
    966 			GE_WRITE(sc, ECRDP1, rxq->rxq_desc_busaddr);
    967 		}
    968 		if (sc->sc_idlemask & (ETH_IR_RxBuffer_0|ETH_IR_RxError_0)) {
    969 			struct gfe_rxqueue *rxq = sc->sc_rxq[GE_RXPRIO_LO];
    970 			rxq->rxq_fi = 0;
    971 			GE_WRITE(sc, EFRDP0, rxq->rxq_desc_busaddr);
    972 			GE_WRITE(sc, ECRDP0, rxq->rxq_desc_busaddr);
    973 		}
    974 		sc->sc_idlemask = 0;
    975 	}
    976 	if (intrmask != sc->sc_intrmask) {
    977 		sc->sc_intrmask = intrmask;
    978 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
    979 	}
    980 	gfe_intr(sc);
    981 	splx(s);
    982 
    983 	GE_FUNC_EXIT(sc, "");
    984 }
    985 
    986 int
    987 gfe_tx_enqueue(struct gfe_softc *sc, enum gfe_txprio txprio)
    988 {
    989 	struct gfe_txqueue * const txq = sc->sc_txq[txprio];
    990 	volatile struct gt_eth_desc * const txd = &txq->txq_descs[txq->txq_lo];
    991 	uint32_t intrmask = sc->sc_intrmask;
    992 	struct mbuf *m;
    993 
    994 	GE_FUNC_ENTER(sc, "gfe_tx_enqueue");
    995 
    996 	/*
    997 	 * Anything in the pending queue to enqueue?  if not, punt.
    998 	 * otherwise grab its dmamap.
    999 	 */
   1000 	if ((m = txq->txq_pendq.ifq_head) == NULL) {
   1001 		GE_FUNC_EXIT(sc, "-");
   1002 		return 0;
   1003 	}
   1004 
   1005 	/*
   1006 	 * Have we [over]consumed our limit of descriptors?
   1007 	 * Do we have enough free descriptors?
   1008 	 */
   1009 	if (GE_TXDESC_MAX == txq->txq_nactive + 1) {
   1010 		volatile struct gt_eth_desc * const txd2 = &txq->txq_descs[txq->txq_fi];
   1011 		uint32_t cmdsts;
   1012 		size_t pktlen;
   1013 		bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1014 				txq->txq_fi * sizeof(*txd), sizeof(*txd),
   1015 				BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1016 		cmdsts = gt32toh(txd2->ed_cmdsts);
   1017 		if (cmdsts & TX_CMD_O) {
   1018 			bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1019 				txq->txq_fi * sizeof(*txd), sizeof(*txd),
   1020 				BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1021 			GE_FUNC_EXIT(sc, "@");
   1022 			return 0;
   1023 		}
   1024 		if (++txq->txq_fi == GE_TXDESC_MAX)
   1025 			txq->txq_fi = 0;
   1026 		txq->txq_inptr = gt32toh(txd2->ed_bufptr) - txq->txq_buf_busaddr;
   1027 		pktlen = (gt32toh(txd2->ed_lencnt) >> 16) & 0xffff;
   1028 		txq->txq_inptr += (pktlen + 7) & ~7;
   1029 		txq->txq_nactive--;
   1030 
   1031 		/* statistics */
   1032 		sc->sc_ec.ec_if.if_opackets++;
   1033 		sc->sc_ec.ec_if.if_obytes += pktlen;
   1034 		if (cmdsts & TX_STS_ES)
   1035 			sc->sc_ec.ec_if.if_oerrors++;
   1036 		GE_DPRINTF(sc, ("%%"));
   1037 	}
   1038 
   1039 	/*
   1040 	 * If this packet would wrap around the end of the buffer, reset back
   1041 	 * to the beginning.
   1042 	 */
   1043 	if (txq->txq_outptr + m->m_pkthdr.len > GE_TXBUF_SIZE) {
   1044 		txq->txq_ei_gapcount += GE_TXBUF_SIZE - txq->txq_outptr;
   1045 		txq->txq_outptr = 0;
   1046 	}
   1047 
   1048 	/*
   1049 	 * Make sure the output packet doesn't run over the beginning of
   1050 	 * what we've already given the GT.
   1051 	 */
   1052 	if (txq->txq_outptr <= txq->txq_inptr &&
   1053 	    txq->txq_outptr + m->m_pkthdr.len > txq->txq_inptr) {
   1054 		intrmask |= txq->txq_intrbits &
   1055 		    (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow);
   1056 		if (sc->sc_intrmask != intrmask) {
   1057 			sc->sc_intrmask = intrmask;
   1058 			GE_WRITE(sc, EIMR, sc->sc_intrmask);
   1059 		}
   1060 		GE_FUNC_EXIT(sc, "#");
   1061 		return 0;
   1062 	}
   1063 
   1064 	/*
   1065 	 * The end-of-list descriptor we put on last time is the starting point
   1066 	 * for this packet.  The GT is supposed to terminate list processing on
   1067 	 * a NULL nxtptr but that currently is broken so a CPU-owned descriptor
   1068 	 * must terminate the list.
   1069 	 */
   1070 	intrmask = sc->sc_intrmask;
   1071 
   1072 	m_copydata(m, 0, m->m_pkthdr.len,
   1073 	    txq->txq_buf_mem.gdm_kva + txq->txq_outptr);
   1074 	bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
   1075 	    txq->txq_outptr, m->m_pkthdr.len, BUS_DMASYNC_PREWRITE);
   1076 	txd->ed_bufptr = htogt32(txq->txq_buf_busaddr + txq->txq_outptr);
   1077 	txd->ed_lencnt = htogt32(m->m_pkthdr.len << 16);
   1078 #if 0
   1079 	bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1080 	    txq->txq_lo * sizeof(*txd), sizeof(*txd),
   1081 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1082 #endif
   1083 
   1084 	/*
   1085 	 * Request a buffer interrupt every 2/3 of the way thru the transmit
   1086 	 * buffer.
   1087 	 */
   1088 	txq->txq_ei_gapcount += m->m_pkthdr.len + 7;
   1089 	if (txq->txq_ei_gapcount > 2 * GE_TXBUF_SIZE / 3) {
   1090 		txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST|TX_CMD_EI);
   1091 		txq->txq_ei_gapcount = 0;
   1092 	} else {
   1093 		txd->ed_cmdsts = htogt32(TX_CMD_FIRST|TX_CMD_LAST);
   1094 	}
   1095 #if 0
   1096 	GE_DPRINTF(sc, ("([%d]->%08lx.%08lx.%08lx.%08lx)", txq->txq_lo,
   1097 	    ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
   1098 	    ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
   1099 #endif
   1100 	bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1101 	    txq->txq_lo * sizeof(*txd), sizeof(*txd),
   1102 	    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1103 
   1104 	txq->txq_outptr += (m->m_pkthdr.len + 31) & ~31;
   1105 	/*
   1106 	 * Tell the SDMA engine to "Fetch!"
   1107 	 */
   1108 	GE_WRITE(sc, ESDCMR,
   1109 		 txq->txq_esdcmrbits & (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
   1110 
   1111 	GE_DPRINTF(sc, ("(%d)", txq->txq_lo));
   1112 
   1113 	/*
   1114 	 * Update the last out appropriately.
   1115 	 */
   1116 	if (++txq->txq_lo == GE_TXDESC_MAX)
   1117 		txq->txq_lo = 0;
   1118 
   1119 	/*
   1120 	 * Move mbuf from the pending queue to the snd queue.
   1121 	 */
   1122 	IF_DEQUEUE(&txq->txq_pendq, m);
   1123 #if NBPFILTER > 0
   1124 	if (sc->sc_ec.ec_if.if_bpf != NULL)
   1125 		bpf_mtap(sc->sc_ec.ec_if.if_bpf, m);
   1126 #endif
   1127 	m_freem(m);
   1128 	sc->sc_ec.ec_if.if_flags &= ~IFF_OACTIVE;
   1129 
   1130 	/*
   1131 	 * Since we have put an item into the packet queue, we now want
   1132 	 * an interrupt when the transmit queue finishes processing the
   1133 	 * list.  But only update the mask if needs changing.
   1134 	 */
   1135 	intrmask |= txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow);
   1136 	if (sc->sc_intrmask != intrmask) {
   1137 		sc->sc_intrmask = intrmask;
   1138 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
   1139 	}
   1140 	if (sc->sc_ec.ec_if.if_timer == 0)
   1141 		sc->sc_ec.ec_if.if_timer = 5;
   1142 	GE_FUNC_EXIT(sc, "*");
   1143 	return 1;
   1144 }
   1145 
   1146 uint32_t
   1147 gfe_tx_done(struct gfe_softc *sc, enum gfe_txprio txprio, uint32_t intrmask)
   1148 {
   1149 	struct gfe_txqueue * const txq = sc->sc_txq[txprio];
   1150 
   1151 	GE_FUNC_ENTER(sc, "gfe_tx_done");
   1152 
   1153 	if (txq == NULL) {
   1154 		GE_FUNC_EXIT(sc, "");
   1155 		return intrmask;
   1156 	}
   1157 
   1158 	while (txq->txq_nactive > 0) {
   1159 		volatile struct gt_eth_desc *txd = &txq->txq_descs[txq->txq_fi];
   1160 		uint32_t cmdsts;
   1161 		size_t pktlen;
   1162 
   1163 		bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1164 		    txq->txq_fi * sizeof(*txd), sizeof(*txd),
   1165 		    BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE);
   1166 		if ((cmdsts = gt32toh(txd->ed_cmdsts)) & TX_CMD_O) {
   1167 			/*
   1168 			 * If the GT owns this descriptor and according
   1169 			 * to the status register, the transmit engine
   1170 			 * is not running, restart it.
   1171 			 */
   1172 #if 0
   1173 			if ((GE_READ(sc, EPSR) & txq->txq_epsrbits &
   1174 			    (ETH_EPSR_TxHigh|ETH_EPSR_TxLow)) == 0) {
   1175 				/*
   1176 				 * If the current transmit descriptor isn't
   1177 				 * pointing at this descriptor, then we've
   1178 				 * lost synch, reset it to this one before
   1179 				 * restarting.
   1180 				 */
   1181 				unsigned int curtxdnum = (
   1182 				    gt_read(sc->sc_dev.dv_parent,
   1183 					txq->txq_ectdp) -
   1184 				    txq->txq_desc_busaddr) / 16;
   1185 				if (curtxdnum != txq->txq_fi) {
   1186 					gt_write(sc->sc_dev.dv_parent,
   1187 					    txq->txq_ectdp,
   1188 					    txq->txq_desc_busaddr +
   1189 					      sizeof(*ed) * txq->txq_fi);
   1190 					GE_DPRINTF(sc,
   1191 					    ("(oldcur=%d,newcur=fi(%d))",
   1192 					     curtxdnum, txq->txq_fi));
   1193 					printf("%s: transmitter synchronization"
   1194 					    " lost at %d; repositioning"
   1195 					    " to %d\n",
   1196 					    sc->sc_dev.dv_xname,
   1197 					    curtxdnum, txq->txq_fi);
   1198 				}
   1199 				/*
   1200 				 * [Re-] Kick the transmit engine.
   1201 				 */
   1202 				GE_WRITE(sc, ESDCMR,
   1203 				     txq->txq_esdcmrbits &
   1204 					    (ETH_ESDCMR_TXDH|ETH_ESDCMR_TXDL));
   1205 				GE_DPRINTF(sc, ("*"));
   1206 			}
   1207 #endif
   1208 			bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map,
   1209 			    txq->txq_fi * sizeof(*txd), sizeof(*txd),
   1210 			    BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1211 			GE_FUNC_EXIT(sc, "");
   1212 			return intrmask;
   1213 		}
   1214 #if 0
   1215 		GE_DPRINTF(sc, ("([%d]<-%08lx.%08lx.%08lx.%08lx)",
   1216 		    txq->txq_lo,
   1217 		    ((unsigned long *)txd)[0], ((unsigned long *)txd)[1],
   1218 		    ((unsigned long *)txd)[2], ((unsigned long *)txd)[3]));
   1219 #endif
   1220 		GE_DPRINTF(sc, ("(%d)", txq->txq_fi));
   1221 		if (++txq->txq_fi == GE_TXDESC_MAX)
   1222 			txq->txq_fi = 0;
   1223 		txq->txq_inptr = gt32toh(txd->ed_bufptr) - txq->txq_buf_busaddr;
   1224 		pktlen = (gt32toh(txd->ed_lencnt) >> 16) & 0xffff;
   1225 		txq->txq_inptr += (pktlen + 31) & ~31;
   1226 		bus_dmamap_sync(sc->sc_dmat, txq->txq_buf_mem.gdm_map,
   1227 		    txq->txq_inptr, pktlen, BUS_DMASYNC_POSTWRITE);
   1228 
   1229 		/* statistics */
   1230 		sc->sc_ec.ec_if.if_opackets++;
   1231 		sc->sc_ec.ec_if.if_obytes += pktlen;
   1232 		if (cmdsts & TX_STS_ES)
   1233 			sc->sc_ec.ec_if.if_oerrors++;
   1234 
   1235 		txd->ed_bufptr = 0;
   1236 
   1237 		sc->sc_ec.ec_if.if_timer = 5;
   1238 		--txq->txq_nactive;
   1239 	}
   1240 	if (txq->txq_nactive != 0)
   1241 		panic("%s: transmit fifo%d empty but active count (%d) > 0!",
   1242 		    sc->sc_dev.dv_xname, txprio, txq->txq_nactive);
   1243 	sc->sc_ec.ec_if.if_timer = 0;
   1244 	intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxEndHigh|ETH_IR_TxEndLow));
   1245 	intrmask &= ~(txq->txq_intrbits & (ETH_IR_TxBufferHigh|ETH_IR_TxBufferLow));
   1246 	GE_FUNC_EXIT(sc, "");
   1247 	return intrmask;
   1248 }
   1249 
   1250 int
   1251 gfe_tx_start(struct gfe_softc *sc, enum gfe_txprio txprio)
   1252 {
   1253 	struct gfe_txqueue *txq;
   1254 	volatile struct gt_eth_desc *txd;
   1255 	unsigned int i;
   1256 	bus_addr_t addr;
   1257 
   1258 	GE_FUNC_ENTER(sc, "gfe_tx_start");
   1259 
   1260 	sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
   1261 			     ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
   1262 
   1263 	if ((txq = sc->sc_txq[txprio]) == NULL) {
   1264 		int error;
   1265 		txq = (struct gfe_txqueue *) malloc(sizeof(*txq),
   1266 		    M_DEVBUF, M_NOWAIT);
   1267 		if (txq == NULL) {
   1268 			GE_FUNC_EXIT(sc, "");
   1269 			return ENOMEM;
   1270 		}
   1271 		memset(txq, 0, sizeof(*txq));
   1272 		error = gfe_dmamem_alloc(sc, &txq->txq_desc_mem, 1,
   1273 		    GE_TXMEM_SIZE, 0);
   1274 		if (error) {
   1275 			free(txq, M_DEVBUF);
   1276 			GE_FUNC_EXIT(sc, "");
   1277 			return error;
   1278 		}
   1279 		error = gfe_dmamem_alloc(sc, &txq->txq_buf_mem, 1,
   1280 		    GE_TXBUF_SIZE, 0);
   1281 		if (error) {
   1282 			gfe_dmamem_free(sc, &txq->txq_desc_mem);
   1283 			free(txq, M_DEVBUF);
   1284 			GE_FUNC_EXIT(sc, "");
   1285 			return error;
   1286 		}
   1287 		sc->sc_txq[txprio] = txq;
   1288 	}
   1289 
   1290 	txq->txq_descs =
   1291 	    (volatile struct gt_eth_desc *) txq->txq_desc_mem.gdm_kva;
   1292 	txq->txq_desc_busaddr = txq->txq_desc_mem.gdm_map->dm_segs[0].ds_addr;
   1293 	txq->txq_buf_busaddr = txq->txq_buf_mem.gdm_map->dm_segs[0].ds_addr;
   1294 
   1295 	txq->txq_pendq.ifq_maxlen = 10;
   1296 	txq->txq_ei_gapcount = 0;
   1297 	txq->txq_nactive = 0;
   1298 	txq->txq_fi = 0;
   1299 	txq->txq_lo = 0;
   1300 	txq->txq_inptr = GE_TXBUF_SIZE;
   1301 	txq->txq_outptr = 0;
   1302 	for (i = 0, txd = txq->txq_descs,
   1303 	     addr = txq->txq_desc_busaddr + sizeof(*txd);
   1304 			i < GE_TXDESC_MAX - 1;
   1305 			i++, txd++, addr += sizeof(*txd)) {
   1306 		/*
   1307 		 * update the nxtptr to point to the next txd.
   1308 		 */
   1309 		txd->ed_cmdsts = 0;
   1310 		txd->ed_nxtptr = htogt32(addr);
   1311 	}
   1312 	txq->txq_descs[GE_TXDESC_MAX-1].ed_nxtptr =
   1313 	    htogt32(txq->txq_desc_busaddr);
   1314 	bus_dmamap_sync(sc->sc_dmat, txq->txq_desc_mem.gdm_map, 0,
   1315 	    GE_TXMEM_SIZE, BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
   1316 
   1317 	switch (txprio) {
   1318 	case GE_TXPRIO_HI:
   1319 		txq->txq_intrbits = ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh;
   1320 		txq->txq_esdcmrbits = ETH_ESDCMR_TXDH;
   1321 		txq->txq_epsrbits = ETH_EPSR_TxHigh;
   1322 		txq->txq_ectdp = ETH_ECTDP1(sc->sc_macno);
   1323 		GE_WRITE(sc, ECTDP1, txq->txq_desc_busaddr);
   1324 		break;
   1325 
   1326 	case GE_TXPRIO_LO:
   1327 		txq->txq_intrbits = ETH_IR_TxEndLow|ETH_IR_TxBufferLow;
   1328 		txq->txq_esdcmrbits = ETH_ESDCMR_TXDL;
   1329 		txq->txq_epsrbits = ETH_EPSR_TxLow;
   1330 		txq->txq_ectdp = ETH_ECTDP0(sc->sc_macno);
   1331 		GE_WRITE(sc, ECTDP0, txq->txq_desc_busaddr);
   1332 		break;
   1333 
   1334 	case GE_TXPRIO_NONE:
   1335 		break;
   1336 	}
   1337 #if 0
   1338 	GE_DPRINTF(sc, ("(ectdp=%#x", txq->txq_ectdp));
   1339 	gt_write(sc->sc_dev.dv_parent, txq->txq_ectdp, txq->txq_desc_busaddr);
   1340 	GE_DPRINTF(sc, (")"));
   1341 #endif
   1342 
   1343 	/*
   1344 	 * If we are restarting, there may be packets in the pending queue
   1345 	 * waiting to be enqueued.  Try enqueuing packets from both priority
   1346 	 * queues until the pending queue is empty or there no room for them
   1347 	 * on the device.
   1348 	 */
   1349 	while (gfe_tx_enqueue(sc, txprio))
   1350 		continue;
   1351 
   1352 	GE_FUNC_EXIT(sc, "");
   1353 	return 0;
   1354 }
   1355 
   1356 void
   1357 gfe_tx_cleanup(struct gfe_softc *sc, enum gfe_txprio txprio, int flush)
   1358 {
   1359 	struct gfe_txqueue * const txq = sc->sc_txq[txprio];
   1360 
   1361 	GE_FUNC_ENTER(sc, "gfe_tx_cleanup");
   1362 	if (txq == NULL) {
   1363 		GE_FUNC_EXIT(sc, "");
   1364 		return;
   1365 	}
   1366 
   1367 	if (!flush) {
   1368 		GE_FUNC_EXIT(sc, "");
   1369 		return;
   1370 	}
   1371 
   1372 	gfe_dmamem_free(sc, &txq->txq_desc_mem);
   1373 	gfe_dmamem_free(sc, &txq->txq_buf_mem);
   1374 	free(txq, M_DEVBUF);
   1375 	sc->sc_txq[txprio] = NULL;
   1376 	GE_FUNC_EXIT(sc, "-F");
   1377 }
   1378 
   1379 void
   1380 gfe_tx_stop(struct gfe_softc *sc, enum gfe_whack_op op)
   1381 {
   1382 	GE_FUNC_ENTER(sc, "gfe_tx_stop");
   1383 
   1384 	GE_WRITE(sc, ESDCMR, ETH_ESDCMR_STDH|ETH_ESDCMR_STDL);
   1385 
   1386 	sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, sc->sc_intrmask);
   1387 	sc->sc_intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, sc->sc_intrmask);
   1388 	sc->sc_intrmask &= ~(ETH_IR_TxEndHigh|ETH_IR_TxBufferHigh|
   1389 			     ETH_IR_TxEndLow |ETH_IR_TxBufferLow);
   1390 
   1391 	gfe_tx_cleanup(sc, GE_TXPRIO_HI, op == GE_WHACK_STOP);
   1392 	gfe_tx_cleanup(sc, GE_TXPRIO_LO, op == GE_WHACK_STOP);
   1393 
   1394 	sc->sc_ec.ec_if.if_timer = 0;
   1395 	GE_FUNC_EXIT(sc, "");
   1396 }
   1397 
   1398 int
   1400 gfe_intr(void *arg)
   1401 {
   1402 	struct gfe_softc * const sc = arg;
   1403 	uint32_t cause;
   1404 	uint32_t intrmask = sc->sc_intrmask;
   1405 	int claim = 0;
   1406 	int cnt;
   1407 
   1408 	GE_FUNC_ENTER(sc, "gfe_intr");
   1409 
   1410 	for (cnt = 0; cnt < 4; cnt++) {
   1411 		if (sc->sc_intrmask != intrmask) {
   1412 			sc->sc_intrmask = intrmask;
   1413 			GE_WRITE(sc, EIMR, sc->sc_intrmask);
   1414 		}
   1415 		cause = GE_READ(sc, EICR);
   1416 		cause &= sc->sc_intrmask;
   1417 		GE_DPRINTF(sc, (".%#x", cause));
   1418 		if (cause == 0)
   1419 			break;
   1420 
   1421 		claim = 1;
   1422 
   1423 		GE_WRITE(sc, EICR, ~cause);
   1424 #ifndef GE_NORX
   1425 		if (cause & (ETH_IR_RxBuffer|ETH_IR_RxError))
   1426 			intrmask = gfe_rx_process(sc, cause, intrmask);
   1427 #endif
   1428 
   1429 #ifndef GE_NOTX
   1430 		if (cause & (ETH_IR_TxBufferHigh|ETH_IR_TxEndHigh))
   1431 			intrmask = gfe_tx_done(sc, GE_TXPRIO_HI, intrmask);
   1432 		if (cause & (ETH_IR_TxBufferLow|ETH_IR_TxEndLow))
   1433 			intrmask = gfe_tx_done(sc, GE_TXPRIO_LO, intrmask);
   1434 #endif
   1435 		if (cause & ETH_IR_MIIPhySTC) {
   1436 			sc->sc_flags |= GE_PHYSTSCHG;
   1437 			/* intrmask &= ~ETH_IR_MIIPhySTC; */
   1438 		}
   1439 	}
   1440 
   1441 	GE_FUNC_EXIT(sc, "");
   1442 	return claim;
   1443 }
   1444 
   1445 int
   1447 gfe_mii_mediachange (struct ifnet *ifp)
   1448 {
   1449 	struct gfe_softc *sc = ifp->if_softc;
   1450 
   1451 	if (ifp->if_flags & IFF_UP)
   1452 		mii_mediachg(&sc->sc_mii);
   1453 
   1454 	return (0);
   1455 }
   1456 void
   1457 gfe_mii_mediastatus (struct ifnet *ifp, struct ifmediareq *ifmr)
   1458 {
   1459 	struct gfe_softc *sc = ifp->if_softc;
   1460 
   1461 	if (sc->sc_flags & GE_PHYSTSCHG) {
   1462 		sc->sc_flags &= ~GE_PHYSTSCHG;
   1463 		mii_pollstat(&sc->sc_mii);
   1464 	}
   1465 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   1466 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   1467 }
   1468 
   1469 int
   1470 gfe_mii_read (struct device *self, int phy, int reg)
   1471 {
   1472 	return gt_mii_read(self, self->dv_parent, phy, reg);
   1473 }
   1474 
   1475 void
   1476 gfe_mii_write (struct device *self, int phy, int reg, int value)
   1477 {
   1478 	gt_mii_write(self, self->dv_parent, phy, reg, value);
   1479 }
   1480 
   1481 void
   1482 gfe_mii_statchg (struct device *self)
   1483 {
   1484 	/* struct gfe_softc *sc = (struct gfe_softc *) self; */
   1485 	/* do nothing? */
   1486 }
   1487 
   1488 int
   1490 gfe_whack(struct gfe_softc *sc, enum gfe_whack_op op)
   1491 {
   1492 	int error = 0;
   1493 	GE_FUNC_ENTER(sc, "gfe_whack");
   1494 
   1495 	switch (op) {
   1496 	case GE_WHACK_RESTART:
   1497 #ifndef GE_NOTX
   1498 		gfe_tx_stop(sc, op);
   1499 #endif
   1500 		/* sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING; */
   1501 		/* FALLTHROUGH */
   1502 	case GE_WHACK_START:
   1503 #ifndef GE_NOHASH
   1504 		if (error == 0 && sc->sc_hashtable == NULL) {
   1505 			error = gfe_hash_alloc(sc);
   1506 			if (error)
   1507 				break;
   1508 		}
   1509 		if (op != GE_WHACK_RESTART)
   1510 			gfe_hash_fill(sc);
   1511 #endif
   1512 #ifndef GE_NORX
   1513 		if (op != GE_WHACK_RESTART) {
   1514 			error = gfe_rx_prime(sc);
   1515 			if (error)
   1516 				break;
   1517 		}
   1518 #endif
   1519 #ifndef GE_NOTX
   1520 		error = gfe_tx_start(sc, GE_TXPRIO_HI);
   1521 		if (error)
   1522 			break;
   1523 #endif
   1524 		sc->sc_ec.ec_if.if_flags |= IFF_RUNNING;
   1525 		GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
   1526 		GE_WRITE(sc, EPCXR, sc->sc_pcxr);
   1527 		GE_WRITE(sc, EICR, 0);
   1528 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
   1529 #ifndef GE_NOHASH
   1530 		GE_WRITE(sc, EHTPR, sc->sc_hash_mem.gdm_map->dm_segs->ds_addr);
   1531 #endif
   1532 #ifndef GE_NORX
   1533 		GE_WRITE(sc, ESDCMR, ETH_ESDCMR_ERD);
   1534 		sc->sc_flags |= GE_RXACTIVE;
   1535 #endif
   1536 		/* FALLTHROUGH */
   1537 	case GE_WHACK_CHANGE:
   1538 		GE_DPRINTF(sc, ("(pcr=%#x,imr=%#x)",
   1539 		    GE_READ(sc, EPCR), GE_READ(sc, EIMR)));
   1540 		GE_WRITE(sc, EPCR, sc->sc_pcr | ETH_EPCR_EN);
   1541 		GE_WRITE(sc, EIMR, sc->sc_intrmask);
   1542 		gfe_ifstart(&sc->sc_ec.ec_if);
   1543 		GE_DPRINTF(sc, ("(ectdp0=%#x, ectdp1=%#x)",
   1544 		    GE_READ(sc, ECTDP0), GE_READ(sc, ECTDP1)));
   1545 		GE_FUNC_EXIT(sc, "");
   1546 		return error;
   1547 	case GE_WHACK_STOP:
   1548 		break;
   1549 	}
   1550 
   1551 #ifdef GE_DEBUG
   1552 	if (error)
   1553 		GE_DPRINTF(sc, (" failed: %d\n", error));
   1554 #endif
   1555 	GE_WRITE(sc, EPCR, sc->sc_pcr);
   1556 	GE_WRITE(sc, EIMR, 0);
   1557 	sc->sc_ec.ec_if.if_flags &= ~IFF_RUNNING;
   1558 #ifndef GE_NOTX
   1559 	gfe_tx_stop(sc, GE_WHACK_STOP);
   1560 #endif
   1561 #ifndef GE_NORX
   1562 	gfe_rx_stop(sc, GE_WHACK_STOP);
   1563 #endif
   1564 #ifndef GE_NOHASH
   1565 	gfe_dmamem_free(sc, &sc->sc_hash_mem);
   1566 	sc->sc_hashtable = NULL;
   1567 #endif
   1568 
   1569 	GE_FUNC_EXIT(sc, "");
   1570 	return error;
   1571 }
   1572 
   1573 int
   1575 gfe_hash_compute(struct gfe_softc *sc, const uint8_t eaddr[ETHER_ADDR_LEN])
   1576 {
   1577 	uint32_t w0, add0, add1;
   1578 	uint32_t result;
   1579 
   1580 	GE_FUNC_ENTER(sc, "gfe_hash_compute");
   1581 	add0 = ((uint32_t) eaddr[5] <<  0) |
   1582 	       ((uint32_t) eaddr[4] <<  8) |
   1583 	       ((uint32_t) eaddr[3] << 16);
   1584 
   1585 	add0 = ((add0 & 0x00f0f0f0) >> 4) | ((add0 & 0x000f0f0f) << 4);
   1586 	add0 = ((add0 & 0x00cccccc) >> 2) | ((add0 & 0x00333333) << 2);
   1587 	add0 = ((add0 & 0x00aaaaaa) >> 1) | ((add0 & 0x00555555) << 1);
   1588 
   1589 	add1 = ((uint32_t) eaddr[2] <<  0) |
   1590 	       ((uint32_t) eaddr[1] <<  8) |
   1591 	       ((uint32_t) eaddr[0] << 16);
   1592 
   1593 	add1 = ((add1 & 0x00f0f0f0) >> 4) | ((add1 & 0x000f0f0f) << 4);
   1594 	add1 = ((add1 & 0x00cccccc) >> 2) | ((add1 & 0x00333333) << 2);
   1595 	add1 = ((add1 & 0x00aaaaaa) >> 1) | ((add1 & 0x00555555) << 1);
   1596 
   1597 	GE_DPRINTF(sc, ("%s=", ether_sprintf(eaddr)));
   1598 	/*
   1599 	 * hashResult is the 15 bits Hash entry address.
   1600 	 * ethernetADD is a 48 bit number, which is derived from the Ethernet
   1601 	 *	MAC address, by nibble swapping in every byte (i.e MAC address
   1602 	 *	of 0x123456789abc translates to ethernetADD of 0x21436587a9cb).
   1603 	 */
   1604 
   1605 	if ((sc->sc_pcr & ETH_EPCR_HM) == 0) {
   1606 		/*
   1607 		 * hashResult[14:0] = hashFunc0(ethernetADD[47:0])
   1608 		 *
   1609 		 * hashFunc0 calculates the hashResult in the following manner:
   1610 		 *   hashResult[ 8:0] = ethernetADD[14:8,1,0]
   1611 		 *		XOR ethernetADD[23:15] XOR ethernetADD[32:24]
   1612 		 */
   1613 		result = (add0 & 3) | ((add0 >> 6) & ~3);
   1614 		result ^= (add0 >> 15) ^ (add1 >>  0);
   1615 		result &= 0x1ff;
   1616 		/*
   1617 		 *   hashResult[14:9] = ethernetADD[7:2]
   1618 		 */
   1619 		result |= (add0 & ~3) << 7;	/* excess bits will be masked */
   1620 		GE_DPRINTF(sc, ("0(%#x)", result & 0x7fff));
   1621 	} else {
   1622 #define	TRIBITFLIP	073516240	/* yes its in octal */
   1623 		/*
   1624 		 * hashResult[14:0] = hashFunc1(ethernetADD[47:0])
   1625 		 *
   1626 		 * hashFunc1 calculates the hashResult in the following manner:
   1627 		 *   hashResult[08:00] = ethernetADD[06:14]
   1628 		 *		XOR ethernetADD[15:23] XOR ethernetADD[24:32]
   1629 		 */
   1630 		w0 = ((add0 >> 6) ^ (add0 >> 15) ^ (add1)) & 0x1ff;
   1631 		/*
   1632 		 * Now bitswap those 9 bits
   1633 		 */
   1634 		result = 0;
   1635 		result |= ((TRIBITFLIP >> (((w0 >> 0) & 7) * 3)) & 7) << 6;
   1636 		result |= ((TRIBITFLIP >> (((w0 >> 3) & 7) * 3)) & 7) << 3;
   1637 		result |= ((TRIBITFLIP >> (((w0 >> 6) & 7) * 3)) & 7) << 0;
   1638 
   1639 		/*
   1640 		 *   hashResult[14:09] = ethernetADD[00:05]
   1641 		 */
   1642 		result |= ((TRIBITFLIP >> (((add0 >> 0) & 7) * 3)) & 7) << 12;
   1643 		result |= ((TRIBITFLIP >> (((add0 >> 3) & 7) * 3)) & 7) << 9;
   1644 		GE_DPRINTF(sc, ("1(%#x)", result));
   1645 	}
   1646 	GE_FUNC_EXIT(sc, "");
   1647 	return result & ((sc->sc_pcr & ETH_EPCR_HS_512) ? 0x7ff : 0x7fff);
   1648 }
   1649 
   1650 int
   1651 gfe_hash_entry_op(struct gfe_softc *sc, enum gfe_hash_op op,
   1652 	enum gfe_rxprio prio, const u_int8_t eaddr[ETHER_ADDR_LEN])
   1653 {
   1654 	uint64_t he;
   1655 	uint64_t *maybe_he_p = NULL;
   1656 	int limit;
   1657 	int hash;
   1658 	int maybe_hash = 0;
   1659 
   1660 	GE_FUNC_ENTER(sc, "gfe_hash_entry_op");
   1661 
   1662 	hash = gfe_hash_compute(sc, eaddr);
   1663 
   1664 	if (sc->sc_hashtable == NULL) {
   1665 		panic("%s:%d: hashtable == NULL!", sc->sc_dev.dv_xname,
   1666 			__LINE__);
   1667 	}
   1668 
   1669 	/*
   1670 	 * Assume we are going to insert so create the hash entry we
   1671 	 * are going to insert.  We also use it to match entries we
   1672 	 * will be removing.
   1673 	 */
   1674 	he = ((uint64_t) eaddr[5] << 43) |
   1675 	     ((uint64_t) eaddr[4] << 35) |
   1676 	     ((uint64_t) eaddr[3] << 27) |
   1677 	     ((uint64_t) eaddr[2] << 19) |
   1678 	     ((uint64_t) eaddr[1] << 11) |
   1679 	     ((uint64_t) eaddr[0] <<  3) |
   1680 	     HSH_PRIO_INS(prio) | HSH_V | HSH_R;
   1681 
   1682 	/*
   1683 	 * The GT will search upto 12 entries for a hit, so we must mimic that.
   1684 	 */
   1685 	hash &= sc->sc_hashmask / sizeof(he);
   1686 	for (limit = HSH_LIMIT; limit > 0 ; --limit) {
   1687 		/*
   1688 		 * Does the GT wrap at the end, stop at the, or overrun the
   1689 		 * end?  Assume it wraps for now.  Stash a copy of the
   1690 		 * current hash entry.
   1691 		 */
   1692 		uint64_t *he_p = &sc->sc_hashtable[hash];
   1693 		uint64_t thishe = *he_p;
   1694 
   1695 		/*
   1696 		 * If the hash entry isn't valid, that break the chain.  And
   1697 		 * this entry a good candidate for reuse.
   1698 		 */
   1699 		if ((thishe & HSH_V) == 0) {
   1700 			maybe_he_p = he_p;
   1701 			break;
   1702 		}
   1703 
   1704 		/*
   1705 		 * If the hash entry has the same address we are looking for
   1706 		 * then ...  if we are removing and the skip bit is set, its
   1707 		 * already been removed.  if are adding and the skip bit is
   1708 		 * clear, then its already added.  In either return EBUSY
   1709 		 * indicating the op has already been done.  Otherwise flip
   1710 		 * the skip bit and return 0.
   1711 		 */
   1712 		if (((he ^ thishe) & HSH_ADDR_MASK) == 0) {
   1713 			if (((op == GE_HASH_REMOVE) && (thishe & HSH_S)) ||
   1714 			    ((op == GE_HASH_ADD) && (thishe & HSH_S) == 0))
   1715 				return EBUSY;
   1716 			*he_p = thishe ^ HSH_S;
   1717 			bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
   1718 			    hash * sizeof(he), sizeof(he),
   1719 			    BUS_DMASYNC_PREWRITE);
   1720 			GE_FUNC_EXIT(sc, "^");
   1721 			return 0;
   1722 		}
   1723 
   1724 		/*
   1725 		 * If we haven't found a slot for the entry and this entry
   1726 		 * is currently being skipped, return this entry.
   1727 		 */
   1728 		if (maybe_he_p == NULL && (thishe & HSH_S)) {
   1729 			maybe_he_p = he_p;
   1730 			maybe_hash = hash;
   1731 		}
   1732 
   1733 		hash = (hash + 1) & (sc->sc_hashmask / sizeof(he));
   1734 	}
   1735 
   1736 	/*
   1737 	 * If we got here, then there was no entry to remove.
   1738 	 */
   1739 	if (op == GE_HASH_REMOVE) {
   1740 		GE_FUNC_EXIT(sc, "?");
   1741 		return ENOENT;
   1742 	}
   1743 
   1744 	/*
   1745 	 * If we couldn't find a slot, return an error.
   1746 	 */
   1747 	if (maybe_he_p == NULL) {
   1748 		GE_FUNC_EXIT(sc, "!");
   1749 		return ENOSPC;
   1750 	}
   1751 
   1752 	/* Update the entry.
   1753 	 */
   1754 	*maybe_he_p = he;
   1755 	bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
   1756 	    maybe_hash * sizeof(he), sizeof(he), BUS_DMASYNC_PREWRITE);
   1757 	GE_FUNC_EXIT(sc, "+");
   1758 	return 0;
   1759 }
   1760 
   1761 int
   1762 gfe_hash_multichg(struct ethercom *ec, const struct ether_multi *enm, u_long cmd)
   1763 {
   1764 	struct gfe_softc * const sc = ec->ec_if.if_softc;
   1765 	int error;
   1766 	enum gfe_hash_op op;
   1767 	enum gfe_rxprio prio;
   1768 
   1769 	GE_FUNC_ENTER(sc, "hash_multichg");
   1770 	/*
   1771 	 * Is this a wildcard entry?  If so and its being removed, recompute.
   1772 	 */
   1773 	if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN) != 0) {
   1774 		if (cmd == SIOCDELMULTI) {
   1775 			GE_FUNC_EXIT(sc, "");
   1776 			return ENETRESET;
   1777 		}
   1778 
   1779 		/*
   1780 		 * Switch in
   1781 		 */
   1782 		sc->sc_flags |= GE_ALLMULTI;
   1783 		if ((sc->sc_pcr & ETH_EPCR_PM) == 0) {
   1784 			sc->sc_pcr |= ETH_EPCR_PM;
   1785 			GE_WRITE(sc, EPCR, sc->sc_pcr);
   1786 			GE_FUNC_EXIT(sc, "");
   1787 			return 0;
   1788 		}
   1789 		GE_FUNC_EXIT(sc, "");
   1790 		return ENETRESET;
   1791 	}
   1792 
   1793 	prio = GE_RXPRIO_MEDLO;
   1794 	op = (cmd == SIOCDELMULTI ? GE_HASH_REMOVE : GE_HASH_ADD);
   1795 
   1796 	if (sc->sc_hashtable == NULL) {
   1797 		GE_FUNC_EXIT(sc, "");
   1798 		return 0;
   1799 	}
   1800 
   1801 	error = gfe_hash_entry_op(sc, op, prio, enm->enm_addrlo);
   1802 	if (error == EBUSY) {
   1803 		printf("%s: multichg: tried to %s %s again\n",
   1804 		       sc->sc_dev.dv_xname,
   1805 		       cmd == SIOCDELMULTI ? "remove" : "add",
   1806 		       ether_sprintf(enm->enm_addrlo));
   1807 		GE_FUNC_EXIT(sc, "");
   1808 		return 0;
   1809 	}
   1810 
   1811 	if (error == ENOENT) {
   1812 		printf("%s: multichg: failed to remove %s: not in table\n",
   1813 		       sc->sc_dev.dv_xname,
   1814 		       ether_sprintf(enm->enm_addrlo));
   1815 		GE_FUNC_EXIT(sc, "");
   1816 		return 0;
   1817 	}
   1818 
   1819 	if (error == ENOSPC) {
   1820 		printf("%s: multichg: failed to add %s: no space; regenerating table\n",
   1821 		       sc->sc_dev.dv_xname,
   1822 		       ether_sprintf(enm->enm_addrlo));
   1823 		GE_FUNC_EXIT(sc, "");
   1824 		return ENETRESET;
   1825 	}
   1826 	GE_DPRINTF(sc, ("%s: multichg: %s: %s succeeded\n",
   1827 	       sc->sc_dev.dv_xname,
   1828 	       cmd == SIOCDELMULTI ? "remove" : "add",
   1829 	       ether_sprintf(enm->enm_addrlo)));
   1830 	GE_FUNC_EXIT(sc, "");
   1831 	return 0;
   1832 }
   1833 
   1834 int
   1835 gfe_hash_fill(struct gfe_softc *sc)
   1836 {
   1837 	struct ether_multistep step;
   1838 	struct ether_multi *enm;
   1839 	int error;
   1840 
   1841 	GE_FUNC_ENTER(sc, "gfe_hash_fill");
   1842 
   1843 	error = gfe_hash_entry_op(sc, GE_HASH_ADD, GE_RXPRIO_HI,
   1844 	    LLADDR(sc->sc_ec.ec_if.if_sadl));
   1845 	if (error)
   1846 		GE_FUNC_EXIT(sc, "!");
   1847 		return error;
   1848 
   1849 	sc->sc_flags &= ~GE_ALLMULTI;
   1850 	if ((sc->sc_ec.ec_if.if_flags & IFF_PROMISC) == 0)
   1851 		sc->sc_pcr &= ~ETH_EPCR_PM;
   1852 	ETHER_FIRST_MULTI(step, &sc->sc_ec, enm);
   1853 	while (enm != NULL) {
   1854 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   1855 			sc->sc_flags |= GE_ALLMULTI;
   1856 			sc->sc_pcr |= ETH_EPCR_PM;
   1857 		} else {
   1858 			error = gfe_hash_entry_op(sc, GE_HASH_ADD,
   1859 			    GE_RXPRIO_MEDLO, enm->enm_addrlo);
   1860 			if (error == ENOSPC)
   1861 				break;
   1862 		}
   1863 		ETHER_NEXT_MULTI(step, enm);
   1864 	}
   1865 
   1866 	GE_FUNC_EXIT(sc, "");
   1867 	return error;
   1868 }
   1869 
   1870 int
   1871 gfe_hash_alloc(struct gfe_softc *sc)
   1872 {
   1873 	int error;
   1874 	GE_FUNC_ENTER(sc, "gfe_hash_alloc");
   1875 	sc->sc_hashmask = (sc->sc_pcr & ETH_EPCR_HS_512 ? 16 : 256)*1024 - 1;
   1876 	error = gfe_dmamem_alloc(sc, &sc->sc_hash_mem, 1, sc->sc_hashmask + 1,
   1877 	    BUS_DMA_NOCACHE);
   1878 	if (error) {
   1879 		printf("%s: failed to allocate %d bytes for hash table: %d\n",
   1880 		    sc->sc_dev.dv_xname, sc->sc_hashmask + 1, error);
   1881 		GE_FUNC_EXIT(sc, "");
   1882 		return error;
   1883 	}
   1884 	sc->sc_hashtable = (uint64_t *) sc->sc_hash_mem.gdm_kva;
   1885 	memset(sc->sc_hashtable, 0, sc->sc_hashmask + 1);
   1886 	bus_dmamap_sync(sc->sc_dmat, sc->sc_hash_mem.gdm_map,
   1887 	    0, sc->sc_hashmask + 1, BUS_DMASYNC_PREWRITE);
   1888 	GE_FUNC_EXIT(sc, "");
   1889 	return 0;
   1890 }
   1891