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