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