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