Home | History | Annotate | Line # | Download | only in ic
gem.c revision 1.134
      1  1.134  christos /*	$NetBSD: gem.c,v 1.134 2021/03/10 18:26:16 christos Exp $ */
      2    1.1       eeh 
      3    1.1       eeh /*
      4   1.31      heas  *
      5    1.1       eeh  * Copyright (C) 2001 Eduardo Horvath.
      6   1.68       jdc  * Copyright (c) 2001-2003 Thomas Moestl
      7    1.1       eeh  * All rights reserved.
      8    1.1       eeh  *
      9    1.1       eeh  *
     10    1.1       eeh  * Redistribution and use in source and binary forms, with or without
     11    1.1       eeh  * modification, are permitted provided that the following conditions
     12    1.1       eeh  * are met:
     13    1.1       eeh  * 1. Redistributions of source code must retain the above copyright
     14    1.1       eeh  *    notice, this list of conditions and the following disclaimer.
     15    1.1       eeh  * 2. Redistributions in binary form must reproduce the above copyright
     16    1.1       eeh  *    notice, this list of conditions and the following disclaimer in the
     17    1.1       eeh  *    documentation and/or other materials provided with the distribution.
     18   1.31      heas  *
     19    1.1       eeh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
     20    1.1       eeh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21    1.1       eeh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22    1.1       eeh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
     23    1.1       eeh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24    1.1       eeh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25    1.1       eeh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26    1.1       eeh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27    1.1       eeh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28    1.1       eeh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29    1.1       eeh  * SUCH DAMAGE.
     30    1.1       eeh  *
     31    1.1       eeh  */
     32    1.1       eeh 
     33    1.1       eeh /*
     34   1.68       jdc  * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers
     35   1.68       jdc  * See `GEM Gigabit Ethernet ASIC Specification'
     36   1.68       jdc  *   http://www.sun.com/processors/manuals/ge.pdf
     37    1.1       eeh  */
     38   1.10     lukem 
     39   1.10     lukem #include <sys/cdefs.h>
     40  1.134  christos __KERNEL_RCSID(0, "$NetBSD: gem.c,v 1.134 2021/03/10 18:26:16 christos Exp $");
     41    1.1       eeh 
     42   1.35      heas #include "opt_inet.h"
     43    1.1       eeh 
     44    1.1       eeh #include <sys/param.h>
     45   1.31      heas #include <sys/systm.h>
     46    1.1       eeh #include <sys/callout.h>
     47   1.31      heas #include <sys/mbuf.h>
     48    1.1       eeh #include <sys/syslog.h>
     49    1.1       eeh #include <sys/malloc.h>
     50    1.1       eeh #include <sys/kernel.h>
     51    1.1       eeh #include <sys/socket.h>
     52    1.1       eeh #include <sys/ioctl.h>
     53    1.1       eeh #include <sys/errno.h>
     54    1.1       eeh #include <sys/device.h>
     55    1.1       eeh 
     56    1.1       eeh #include <machine/endian.h>
     57    1.1       eeh 
     58    1.1       eeh #include <net/if.h>
     59    1.1       eeh #include <net/if_dl.h>
     60    1.1       eeh #include <net/if_media.h>
     61    1.1       eeh #include <net/if_ether.h>
     62    1.1       eeh 
     63   1.35      heas #ifdef INET
     64   1.35      heas #include <netinet/in.h>
     65   1.35      heas #include <netinet/in_systm.h>
     66   1.35      heas #include <netinet/in_var.h>
     67   1.35      heas #include <netinet/ip.h>
     68   1.35      heas #include <netinet/tcp.h>
     69   1.35      heas #include <netinet/udp.h>
     70   1.35      heas #endif
     71   1.35      heas 
     72    1.1       eeh #include <net/bpf.h>
     73    1.1       eeh 
     74   1.60        ad #include <sys/bus.h>
     75   1.60        ad #include <sys/intr.h>
     76    1.1       eeh 
     77    1.1       eeh #include <dev/mii/mii.h>
     78    1.1       eeh #include <dev/mii/miivar.h>
     79    1.1       eeh #include <dev/mii/mii_bitbang.h>
     80    1.1       eeh 
     81    1.1       eeh #include <dev/ic/gemreg.h>
     82    1.1       eeh #include <dev/ic/gemvar.h>
     83    1.1       eeh 
     84    1.1       eeh #define TRIES	10000
     85    1.1       eeh 
     86   1.85    dyoung static void	gem_inten(struct gem_softc *);
     87   1.41  christos static void	gem_start(struct ifnet *);
     88   1.41  christos static void	gem_stop(struct ifnet *, int);
     89   1.53  christos int		gem_ioctl(struct ifnet *, u_long, void *);
     90   1.34     perry void		gem_tick(void *);
     91   1.34     perry void		gem_watchdog(struct ifnet *);
     92   1.99       jdc void		gem_rx_watchdog(void *);
     93   1.68       jdc void		gem_pcs_start(struct gem_softc *sc);
     94   1.68       jdc void		gem_pcs_stop(struct gem_softc *sc, int);
     95   1.34     perry int		gem_init(struct ifnet *);
     96    1.1       eeh void		gem_init_regs(struct gem_softc *sc);
     97    1.1       eeh static int	gem_ringsize(int sz);
     98   1.41  christos static int	gem_meminit(struct gem_softc *);
     99   1.34     perry void		gem_mifinit(struct gem_softc *);
    100   1.50    martin static int	gem_bitwait(struct gem_softc *sc, bus_space_handle_t, int,
    101  1.112   msaitoh 		    uint32_t, uint32_t);
    102   1.34     perry void		gem_reset(struct gem_softc *);
    103    1.1       eeh int		gem_reset_rx(struct gem_softc *sc);
    104   1.68       jdc static void	gem_reset_rxdma(struct gem_softc *sc);
    105   1.68       jdc static void	gem_rx_common(struct gem_softc *sc);
    106    1.1       eeh int		gem_reset_tx(struct gem_softc *sc);
    107    1.1       eeh int		gem_disable_rx(struct gem_softc *sc);
    108    1.1       eeh int		gem_disable_tx(struct gem_softc *sc);
    109   1.41  christos static void	gem_rxdrain(struct gem_softc *sc);
    110    1.1       eeh int		gem_add_rxbuf(struct gem_softc *sc, int idx);
    111   1.34     perry void		gem_setladrf(struct gem_softc *);
    112    1.1       eeh 
    113    1.1       eeh /* MII methods & callbacks */
    114  1.113   msaitoh static int	gem_mii_readreg(device_t, int, int, uint16_t *);
    115  1.113   msaitoh static int	gem_mii_writereg(device_t, int, int, uint16_t);
    116  1.100      matt static void	gem_mii_statchg(struct ifnet *);
    117   1.34     perry 
    118   1.79    dyoung static int	gem_ifflags_cb(struct ethercom *);
    119   1.79    dyoung 
    120   1.68       jdc void		gem_statuschange(struct gem_softc *);
    121   1.68       jdc 
    122   1.69    dyoung int		gem_ser_mediachange(struct ifnet *);
    123   1.69    dyoung void		gem_ser_mediastatus(struct ifnet *, struct ifmediareq *);
    124   1.34     perry 
    125   1.85    dyoung static void	gem_partial_detach(struct gem_softc *, enum gem_attach_stage);
    126   1.85    dyoung 
    127   1.34     perry struct mbuf	*gem_get(struct gem_softc *, int, int);
    128   1.34     perry int		gem_put(struct gem_softc *, int, struct mbuf *);
    129   1.34     perry void		gem_read(struct gem_softc *, int, int);
    130   1.68       jdc int		gem_pint(struct gem_softc *);
    131   1.34     perry int		gem_eint(struct gem_softc *, u_int);
    132   1.34     perry int		gem_rint(struct gem_softc *);
    133   1.34     perry int		gem_tint(struct gem_softc *);
    134   1.34     perry void		gem_power(int, void *);
    135    1.1       eeh 
    136    1.1       eeh #ifdef GEM_DEBUG
    137   1.67    dyoung static void gem_txsoft_print(const struct gem_softc *, int, int);
    138    1.1       eeh #define	DPRINTF(sc, x)	if ((sc)->sc_ethercom.ec_if.if_flags & IFF_DEBUG) \
    139    1.1       eeh 				printf x
    140    1.1       eeh #else
    141    1.1       eeh #define	DPRINTF(sc, x)	/* nothing */
    142    1.1       eeh #endif
    143    1.1       eeh 
    144   1.40    bouyer #define ETHER_MIN_TX (ETHERMIN + sizeof(struct ether_header))
    145   1.40    bouyer 
    146   1.85    dyoung int
    147   1.85    dyoung gem_detach(struct gem_softc *sc, int flags)
    148   1.85    dyoung {
    149   1.88    martin 	int i;
    150   1.85    dyoung 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    151   1.91       jdc 	bus_space_tag_t t = sc->sc_bustag;
    152   1.91       jdc 	bus_space_handle_t h = sc->sc_h1;
    153   1.85    dyoung 
    154   1.85    dyoung 	/*
    155   1.85    dyoung 	 * Free any resources we've allocated during the attach.
    156   1.85    dyoung 	 * Do this in reverse order and fall through.
    157   1.85    dyoung 	 */
    158   1.85    dyoung 	switch (sc->sc_att_stage) {
    159   1.85    dyoung 	case GEM_ATT_BACKEND_2:
    160   1.85    dyoung 	case GEM_ATT_BACKEND_1:
    161   1.85    dyoung 	case GEM_ATT_FINISHED:
    162   1.91       jdc 		bus_space_write_4(t, h, GEM_INTMASK, ~(uint32_t)0);
    163   1.85    dyoung 		gem_stop(&sc->sc_ethercom.ec_if, 1);
    164   1.85    dyoung 
    165   1.85    dyoung #ifdef GEM_COUNTERS
    166   1.85    dyoung 		for (i = __arraycount(sc->sc_ev_rxhist); --i >= 0; )
    167   1.85    dyoung 			evcnt_detach(&sc->sc_ev_rxhist[i]);
    168   1.85    dyoung 		evcnt_detach(&sc->sc_ev_rxnobuf);
    169   1.85    dyoung 		evcnt_detach(&sc->sc_ev_rxfull);
    170   1.85    dyoung 		evcnt_detach(&sc->sc_ev_rxint);
    171   1.85    dyoung 		evcnt_detach(&sc->sc_ev_txint);
    172  1.133       jdc 		evcnt_detach(&sc->sc_ev_rxoverflow);
    173   1.85    dyoung #endif
    174   1.85    dyoung 		evcnt_detach(&sc->sc_ev_intr);
    175   1.85    dyoung 
    176   1.85    dyoung 		rnd_detach_source(&sc->rnd_source);
    177   1.85    dyoung 		ether_ifdetach(ifp);
    178   1.85    dyoung 		if_detach(ifp);
    179   1.86    martin 
    180   1.86    martin 		callout_destroy(&sc->sc_tick_ch);
    181   1.99       jdc 		callout_destroy(&sc->sc_rx_watchdog);
    182   1.86    martin 
    183   1.85    dyoung 		/*FALLTHROUGH*/
    184   1.85    dyoung 	case GEM_ATT_MII:
    185   1.85    dyoung 		sc->sc_att_stage = GEM_ATT_MII;
    186   1.88    martin 		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
    187  1.132       mrg 		ifmedia_fini(&sc->sc_mii.mii_media);
    188  1.132       mrg 
    189   1.85    dyoung 		/*FALLTHROUGH*/
    190   1.85    dyoung 	case GEM_ATT_7:
    191   1.85    dyoung 		for (i = 0; i < GEM_NRXDESC; i++) {
    192   1.85    dyoung 			if (sc->sc_rxsoft[i].rxs_dmamap != NULL)
    193   1.85    dyoung 				bus_dmamap_destroy(sc->sc_dmatag,
    194   1.85    dyoung 				    sc->sc_rxsoft[i].rxs_dmamap);
    195   1.85    dyoung 		}
    196   1.85    dyoung 		/*FALLTHROUGH*/
    197   1.85    dyoung 	case GEM_ATT_6:
    198   1.85    dyoung 		for (i = 0; i < GEM_TXQUEUELEN; i++) {
    199   1.85    dyoung 			if (sc->sc_txsoft[i].txs_dmamap != NULL)
    200   1.85    dyoung 				bus_dmamap_destroy(sc->sc_dmatag,
    201   1.85    dyoung 				    sc->sc_txsoft[i].txs_dmamap);
    202   1.85    dyoung 		}
    203   1.85    dyoung 		bus_dmamap_unload(sc->sc_dmatag, sc->sc_cddmamap);
    204   1.85    dyoung 		/*FALLTHROUGH*/
    205   1.85    dyoung 	case GEM_ATT_5:
    206   1.86    martin 		bus_dmamap_unload(sc->sc_dmatag, sc->sc_nulldmamap);
    207   1.85    dyoung 		/*FALLTHROUGH*/
    208   1.85    dyoung 	case GEM_ATT_4:
    209   1.86    martin 		bus_dmamap_destroy(sc->sc_dmatag, sc->sc_nulldmamap);
    210   1.85    dyoung 		/*FALLTHROUGH*/
    211   1.85    dyoung 	case GEM_ATT_3:
    212   1.85    dyoung 		bus_dmamap_destroy(sc->sc_dmatag, sc->sc_cddmamap);
    213   1.85    dyoung 		/*FALLTHROUGH*/
    214   1.85    dyoung 	case GEM_ATT_2:
    215   1.85    dyoung 		bus_dmamem_unmap(sc->sc_dmatag, sc->sc_control_data,
    216   1.85    dyoung 		    sizeof(struct gem_control_data));
    217   1.85    dyoung 		/*FALLTHROUGH*/
    218   1.85    dyoung 	case GEM_ATT_1:
    219   1.85    dyoung 		bus_dmamem_free(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg);
    220   1.85    dyoung 		/*FALLTHROUGH*/
    221   1.85    dyoung 	case GEM_ATT_0:
    222   1.85    dyoung 		sc->sc_att_stage = GEM_ATT_0;
    223   1.85    dyoung 		/*FALLTHROUGH*/
    224   1.85    dyoung 	case GEM_ATT_BACKEND_0:
    225   1.85    dyoung 		break;
    226   1.85    dyoung 	}
    227   1.88    martin 	return 0;
    228   1.85    dyoung }
    229   1.85    dyoung 
    230   1.85    dyoung static void
    231   1.85    dyoung gem_partial_detach(struct gem_softc *sc, enum gem_attach_stage stage)
    232   1.85    dyoung {
    233   1.85    dyoung 	cfattach_t ca = device_cfattach(sc->sc_dev);
    234   1.85    dyoung 
    235   1.85    dyoung 	sc->sc_att_stage = stage;
    236   1.85    dyoung 	(*ca->ca_detach)(sc->sc_dev, 0);
    237   1.85    dyoung }
    238    1.1       eeh 
    239    1.1       eeh /*
    240    1.6   thorpej  * gem_attach:
    241    1.1       eeh  *
    242    1.1       eeh  *	Attach a Gem interface to the system.
    243    1.1       eeh  */
    244    1.1       eeh void
    245   1.81       dsl gem_attach(struct gem_softc *sc, const uint8_t *enaddr)
    246    1.1       eeh {
    247    1.1       eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    248    1.1       eeh 	struct mii_data *mii = &sc->sc_mii;
    249   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
    250   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
    251  1.122   msaitoh 	struct ifmedia_entry *ife;
    252   1.89       jdc 	int i, error, phyaddr;
    253  1.112   msaitoh 	uint32_t v;
    254   1.40    bouyer 	char *nullbuf;
    255    1.1       eeh 
    256    1.1       eeh 	/* Make sure the chip is stopped. */
    257    1.1       eeh 	ifp->if_softc = sc;
    258    1.1       eeh 	gem_reset(sc);
    259    1.1       eeh 
    260    1.1       eeh 	/*
    261    1.1       eeh 	 * Allocate the control data structures, and create and load the
    262   1.40    bouyer 	 * DMA map for it. gem_control_data is 9216 bytes, we have space for
    263   1.40    bouyer 	 * the padding buffer in the bus_dmamem_alloc()'d memory.
    264    1.1       eeh 	 */
    265    1.1       eeh 	if ((error = bus_dmamem_alloc(sc->sc_dmatag,
    266   1.40    bouyer 	    sizeof(struct gem_control_data) + ETHER_MIN_TX, PAGE_SIZE,
    267   1.40    bouyer 	    0, &sc->sc_cdseg, 1, &sc->sc_cdnseg, 0)) != 0) {
    268   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    269   1.76    cegger 		   "unable to allocate control data, error = %d\n",
    270   1.76    cegger 		    error);
    271   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_0);
    272   1.85    dyoung 		return;
    273    1.1       eeh 	}
    274    1.1       eeh 
    275   1.68       jdc 	/* XXX should map this in with correct endianness */
    276    1.1       eeh 	if ((error = bus_dmamem_map(sc->sc_dmatag, &sc->sc_cdseg, sc->sc_cdnseg,
    277   1.53  christos 	    sizeof(struct gem_control_data), (void **)&sc->sc_control_data,
    278    1.1       eeh 	    BUS_DMA_COHERENT)) != 0) {
    279   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    280   1.85    dyoung 		    "unable to map control data, error = %d\n", error);
    281   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_1);
    282   1.85    dyoung 		return;
    283    1.1       eeh 	}
    284    1.1       eeh 
    285   1.40    bouyer 	nullbuf =
    286   1.54  christos 	    (char *)sc->sc_control_data + sizeof(struct gem_control_data);
    287   1.40    bouyer 
    288    1.1       eeh 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    289    1.1       eeh 	    sizeof(struct gem_control_data), 1,
    290    1.1       eeh 	    sizeof(struct gem_control_data), 0, 0, &sc->sc_cddmamap)) != 0) {
    291   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    292   1.85    dyoung 		    "unable to create control data DMA map, error = %d\n",
    293   1.85    dyoung 		    error);
    294   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_2);
    295   1.85    dyoung 		return;
    296    1.1       eeh 	}
    297    1.1       eeh 
    298    1.1       eeh 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_cddmamap,
    299    1.1       eeh 	    sc->sc_control_data, sizeof(struct gem_control_data), NULL,
    300    1.1       eeh 	    0)) != 0) {
    301   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    302   1.76    cegger 		    "unable to load control data DMA map, error = %d\n",
    303   1.76    cegger 		    error);
    304   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_3);
    305   1.85    dyoung 		return;
    306    1.1       eeh 	}
    307    1.1       eeh 
    308   1.40    bouyer 	memset(nullbuf, 0, ETHER_MIN_TX);
    309   1.40    bouyer 	if ((error = bus_dmamap_create(sc->sc_dmatag,
    310   1.40    bouyer 	    ETHER_MIN_TX, 1, ETHER_MIN_TX, 0, 0, &sc->sc_nulldmamap)) != 0) {
    311   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    312   1.85    dyoung 		    "unable to create padding DMA map, error = %d\n", error);
    313   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_4);
    314   1.85    dyoung 		return;
    315   1.40    bouyer 	}
    316   1.40    bouyer 
    317   1.40    bouyer 	if ((error = bus_dmamap_load(sc->sc_dmatag, sc->sc_nulldmamap,
    318   1.40    bouyer 	    nullbuf, ETHER_MIN_TX, NULL, 0)) != 0) {
    319   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
    320   1.85    dyoung 		    "unable to load padding DMA map, error = %d\n", error);
    321   1.85    dyoung 		gem_partial_detach(sc, GEM_ATT_5);
    322   1.85    dyoung 		return;
    323   1.40    bouyer 	}
    324   1.40    bouyer 
    325   1.40    bouyer 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_nulldmamap, 0, ETHER_MIN_TX,
    326   1.40    bouyer 	    BUS_DMASYNC_PREWRITE);
    327   1.40    bouyer 
    328    1.1       eeh 	/*
    329    1.1       eeh 	 * Initialize the transmit job descriptors.
    330    1.1       eeh 	 */
    331    1.1       eeh 	SIMPLEQ_INIT(&sc->sc_txfreeq);
    332    1.1       eeh 	SIMPLEQ_INIT(&sc->sc_txdirtyq);
    333    1.1       eeh 
    334    1.1       eeh 	/*
    335    1.1       eeh 	 * Create the transmit buffer DMA maps.
    336    1.1       eeh 	 */
    337    1.1       eeh 	for (i = 0; i < GEM_TXQUEUELEN; i++) {
    338    1.1       eeh 		struct gem_txsoft *txs;
    339    1.1       eeh 
    340    1.1       eeh 		txs = &sc->sc_txsoft[i];
    341    1.1       eeh 		txs->txs_mbuf = NULL;
    342   1.15      matt 		if ((error = bus_dmamap_create(sc->sc_dmatag,
    343   1.15      matt 		    ETHER_MAX_LEN_JUMBO, GEM_NTXSEGS,
    344   1.15      matt 		    ETHER_MAX_LEN_JUMBO, 0, 0,
    345    1.1       eeh 		    &txs->txs_dmamap)) != 0) {
    346   1.85    dyoung 			aprint_error_dev(sc->sc_dev,
    347   1.85    dyoung 			    "unable to create tx DMA map %d, error = %d\n",
    348   1.85    dyoung 			    i, error);
    349   1.85    dyoung 			gem_partial_detach(sc, GEM_ATT_6);
    350   1.85    dyoung 			return;
    351    1.1       eeh 		}
    352    1.1       eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    353    1.1       eeh 	}
    354    1.1       eeh 
    355    1.1       eeh 	/*
    356    1.1       eeh 	 * Create the receive buffer DMA maps.
    357    1.1       eeh 	 */
    358    1.1       eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    359    1.1       eeh 		if ((error = bus_dmamap_create(sc->sc_dmatag, MCLBYTES, 1,
    360    1.1       eeh 		    MCLBYTES, 0, 0, &sc->sc_rxsoft[i].rxs_dmamap)) != 0) {
    361   1.85    dyoung 			aprint_error_dev(sc->sc_dev,
    362   1.85    dyoung 			    "unable to create rx DMA map %d, error = %d\n",
    363   1.85    dyoung 			    i, error);
    364   1.85    dyoung 			gem_partial_detach(sc, GEM_ATT_7);
    365   1.85    dyoung 			return;
    366    1.1       eeh 		}
    367    1.1       eeh 		sc->sc_rxsoft[i].rxs_mbuf = NULL;
    368    1.1       eeh 	}
    369    1.1       eeh 
    370   1.68       jdc 	/* Initialize ifmedia structures and MII info */
    371   1.68       jdc 	mii->mii_ifp = ifp;
    372   1.68       jdc 	mii->mii_readreg = gem_mii_readreg;
    373   1.68       jdc 	mii->mii_writereg = gem_mii_writereg;
    374   1.68       jdc 	mii->mii_statchg = gem_mii_statchg;
    375   1.68       jdc 
    376   1.69    dyoung 	sc->sc_ethercom.ec_mii = mii;
    377   1.68       jdc 
    378   1.68       jdc 	/*
    379   1.68       jdc 	 * Initialization based  on `GEM Gigabit Ethernet ASIC Specification'
    380   1.68       jdc 	 * Section 3.2.1 `Initialization Sequence'.
    381   1.68       jdc 	 * However, we can't assume SERDES or Serialink if neither
    382   1.68       jdc 	 * GEM_MIF_CONFIG_MDI0 nor GEM_MIF_CONFIG_MDI1 are set
    383   1.68       jdc 	 * being set, as both are set on Sun X1141A (with SERDES).  So,
    384   1.68       jdc 	 * we rely on our bus attachment setting GEM_SERDES or GEM_SERIAL.
    385   1.89       jdc 	 * Also, for variants that report 2 PHY's, we prefer the external
    386   1.89       jdc 	 * PHY over the internal PHY, so we look for that first.
    387   1.68       jdc 	 */
    388   1.68       jdc 	gem_mifinit(sc);
    389   1.68       jdc 
    390   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0) {
    391   1.69    dyoung 		ifmedia_init(&mii->mii_media, IFM_IMASK, ether_mediachange,
    392   1.69    dyoung 		    ether_mediastatus);
    393   1.89       jdc 		/* Look for external PHY */
    394   1.89       jdc 		if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
    395   1.89       jdc 			sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL;
    396   1.89       jdc 			bus_space_write_4(t, h, GEM_MIF_CONFIG,
    397   1.89       jdc 			    sc->sc_mif_config);
    398   1.89       jdc 			switch (sc->sc_variant) {
    399   1.89       jdc 			case GEM_SUN_ERI:
    400   1.89       jdc 				phyaddr = GEM_PHYAD_EXTERNAL;
    401   1.89       jdc 				break;
    402   1.89       jdc 			default:
    403   1.89       jdc 				phyaddr = MII_PHY_ANY;
    404   1.89       jdc 				break;
    405   1.89       jdc 			}
    406   1.89       jdc 			mii_attach(sc->sc_dev, mii, 0xffffffff, phyaddr,
    407   1.89       jdc 			    MII_OFFSET_ANY, MIIF_FORCEANEG);
    408   1.89       jdc 		}
    409   1.89       jdc #ifdef GEM_DEBUG
    410   1.89       jdc 		  else
    411   1.89       jdc 			aprint_debug_dev(sc->sc_dev, "using external PHY\n");
    412   1.89       jdc #endif
    413   1.89       jdc 		/* Look for internal PHY if no external PHY was found */
    414  1.117   msaitoh 		if (LIST_EMPTY(&mii->mii_phys) &&
    415  1.109  macallan 		    ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI0) ||
    416  1.109  macallan 		     (sc->sc_variant == GEM_APPLE_K2_GMAC))) {
    417   1.89       jdc 			sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL;
    418   1.89       jdc 			bus_space_write_4(t, h, GEM_MIF_CONFIG,
    419   1.89       jdc 			    sc->sc_mif_config);
    420   1.89       jdc 			switch (sc->sc_variant) {
    421   1.89       jdc 			case GEM_SUN_ERI:
    422   1.89       jdc 			case GEM_APPLE_K2_GMAC:
    423   1.89       jdc 				phyaddr = GEM_PHYAD_INTERNAL;
    424   1.89       jdc 				break;
    425   1.89       jdc 			case GEM_APPLE_GMAC:
    426   1.89       jdc 				phyaddr = GEM_PHYAD_EXTERNAL;
    427   1.89       jdc 				break;
    428   1.89       jdc 			default:
    429   1.89       jdc 				phyaddr = MII_PHY_ANY;
    430   1.89       jdc 				break;
    431   1.89       jdc 			}
    432   1.89       jdc 			mii_attach(sc->sc_dev, mii, 0xffffffff, phyaddr,
    433   1.89       jdc 			    MII_OFFSET_ANY, MIIF_FORCEANEG);
    434   1.89       jdc #ifdef GEM_DEBUG
    435   1.89       jdc 			if (!LIST_EMPTY(&mii->mii_phys))
    436   1.89       jdc 				aprint_debug_dev(sc->sc_dev,
    437   1.89       jdc 				    "using internal PHY\n");
    438   1.89       jdc #endif
    439   1.89       jdc 		}
    440   1.69    dyoung 		if (LIST_EMPTY(&mii->mii_phys)) {
    441   1.68       jdc 				/* No PHY attached */
    442   1.85    dyoung 				aprint_error_dev(sc->sc_dev,
    443   1.85    dyoung 				    "PHY probe failed\n");
    444   1.85    dyoung 				gem_partial_detach(sc, GEM_ATT_MII);
    445   1.85    dyoung 				return;
    446   1.68       jdc 		} else {
    447   1.69    dyoung 			struct mii_softc *child;
    448   1.69    dyoung 
    449   1.68       jdc 			/*
    450   1.68       jdc 			 * Walk along the list of attached MII devices and
    451   1.68       jdc 			 * establish an `MII instance' to `PHY number'
    452   1.68       jdc 			 * mapping.
    453   1.68       jdc 			 */
    454   1.69    dyoung 			LIST_FOREACH(child, &mii->mii_phys, mii_list) {
    455   1.68       jdc 				/*
    456   1.68       jdc 				 * Note: we support just one PHY: the internal
    457   1.68       jdc 				 * or external MII is already selected for us
    458   1.68       jdc 				 * by the GEM_MIF_CONFIG  register.
    459   1.68       jdc 				 */
    460   1.68       jdc 				if (child->mii_phy > 1 || child->mii_inst > 0) {
    461   1.85    dyoung 					aprint_error_dev(sc->sc_dev,
    462   1.76    cegger 					    "cannot accommodate MII device"
    463   1.68       jdc 					    " %s at PHY %d, instance %d\n",
    464   1.77   xtraeme 					       device_xname(child->mii_dev),
    465   1.68       jdc 					       child->mii_phy, child->mii_inst);
    466   1.68       jdc 					continue;
    467   1.68       jdc 				}
    468   1.68       jdc 				sc->sc_phys[child->mii_inst] = child->mii_phy;
    469   1.68       jdc 			}
    470   1.68       jdc 
    471   1.68       jdc 			if (sc->sc_variant != GEM_SUN_ERI)
    472   1.68       jdc 				bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    473   1.68       jdc 				    GEM_MII_DATAPATH_MII);
    474   1.68       jdc 
    475   1.68       jdc 			/*
    476   1.68       jdc 			 * XXX - we can really do the following ONLY if the
    477   1.68       jdc 			 * PHY indeed has the auto negotiation capability!!
    478   1.68       jdc 			 */
    479  1.117   msaitoh 			ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    480   1.68       jdc 		}
    481   1.68       jdc 	} else {
    482   1.69    dyoung 		ifmedia_init(&mii->mii_media, IFM_IMASK, gem_ser_mediachange,
    483   1.69    dyoung 		    gem_ser_mediastatus);
    484   1.68       jdc 		/* SERDES or Serialink */
    485   1.68       jdc 		if (sc->sc_flags & GEM_SERDES) {
    486   1.68       jdc 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    487   1.68       jdc 			    GEM_MII_DATAPATH_SERDES);
    488   1.68       jdc 		} else {
    489   1.68       jdc 			sc->sc_flags |= GEM_SERIAL;
    490   1.68       jdc 			bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
    491   1.68       jdc 			    GEM_MII_DATAPATH_SERIAL);
    492   1.68       jdc 		}
    493   1.68       jdc 
    494   1.85    dyoung 		aprint_normal_dev(sc->sc_dev, "using external PCS %s: ",
    495   1.68       jdc 		    sc->sc_flags & GEM_SERDES ? "SERDES" : "Serialink");
    496   1.68       jdc 
    497  1.117   msaitoh 		ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_AUTO, 0, NULL);
    498   1.68       jdc 		/* Check for FDX and HDX capabilities */
    499   1.68       jdc 		sc->sc_mii_anar = bus_space_read_4(t, h, GEM_MII_ANAR);
    500   1.68       jdc 		if (sc->sc_mii_anar & GEM_MII_ANEG_FUL_DUPLX) {
    501  1.117   msaitoh 			ifmedia_add(&mii->mii_media, IFM_ETHER |
    502  1.117   msaitoh 			    IFM_1000_SX | IFM_MANUAL | IFM_FDX, 0, NULL);
    503   1.68       jdc 			aprint_normal("1000baseSX-FDX, ");
    504   1.68       jdc 		}
    505   1.68       jdc 		if (sc->sc_mii_anar & GEM_MII_ANEG_HLF_DUPLX) {
    506  1.117   msaitoh 			ifmedia_add(&mii->mii_media, IFM_ETHER |
    507  1.117   msaitoh 			    IFM_1000_SX | IFM_MANUAL | IFM_HDX, 0, NULL);
    508   1.68       jdc 			aprint_normal("1000baseSX-HDX, ");
    509   1.68       jdc 		}
    510  1.117   msaitoh 		ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
    511   1.68       jdc 		sc->sc_mii_media = IFM_AUTO;
    512   1.68       jdc 		aprint_normal("auto\n");
    513   1.68       jdc 
    514   1.68       jdc 		gem_pcs_stop(sc, 1);
    515   1.68       jdc 	}
    516   1.68       jdc 
    517    1.1       eeh 	/*
    518    1.1       eeh 	 * From this point forward, the attachment cannot fail.  A failure
    519    1.1       eeh 	 * before this point releases all resources that may have been
    520    1.1       eeh 	 * allocated.
    521    1.1       eeh 	 */
    522    1.1       eeh 
    523    1.1       eeh 	/* Announce ourselves. */
    524   1.85    dyoung 	aprint_normal_dev(sc->sc_dev, "Ethernet address %s",
    525    1.6   thorpej 	    ether_sprintf(enaddr));
    526    1.1       eeh 
    527   1.15      matt 	/* Get RX FIFO size */
    528   1.15      matt 	sc->sc_rxfifosize = 64 *
    529   1.68       jdc 	    bus_space_read_4(t, h, GEM_RX_FIFO_SIZE);
    530   1.24   thorpej 	aprint_normal(", %uKB RX fifo", sc->sc_rxfifosize / 1024);
    531   1.15      matt 
    532   1.15      matt 	/* Get TX FIFO size */
    533   1.68       jdc 	v = bus_space_read_4(t, h, GEM_TX_FIFO_SIZE);
    534   1.24   thorpej 	aprint_normal(", %uKB TX fifo\n", v / 16);
    535   1.15      matt 
    536    1.1       eeh 	/* Initialize ifnet structure. */
    537   1.85    dyoung 	strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ);
    538    1.1       eeh 	ifp->if_softc = sc;
    539  1.114   msaitoh 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
    540   1.41  christos 	sc->sc_if_flags = ifp->if_flags;
    541   1.85    dyoung #if 0
    542   1.73       jdc 	/*
    543   1.73       jdc 	 * The GEM hardware supports basic TCP checksum offloading only.
    544   1.73       jdc 	 * Several (all?) revisions (Sun rev. 01 and Apple rev. 00 and 80)
    545   1.73       jdc 	 * have bugs in the receive checksum, so don't enable it for now.
    546   1.85    dyoung 	 */
    547   1.73       jdc 	if ((GEM_IS_SUN(sc) && sc->sc_chiprev != 1) ||
    548   1.73       jdc 	    (GEM_IS_APPLE(sc) &&
    549   1.73       jdc 	    (sc->sc_chiprev != 0 && sc->sc_chiprev != 0x80)))
    550   1.73       jdc 		ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Rx;
    551   1.85    dyoung #endif
    552   1.73       jdc 	ifp->if_capabilities |= IFCAP_CSUM_TCPv4_Tx;
    553    1.1       eeh 	ifp->if_start = gem_start;
    554    1.1       eeh 	ifp->if_ioctl = gem_ioctl;
    555    1.1       eeh 	ifp->if_watchdog = gem_watchdog;
    556    1.1       eeh 	ifp->if_stop = gem_stop;
    557    1.1       eeh 	ifp->if_init = gem_init;
    558    1.1       eeh 	IFQ_SET_READY(&ifp->if_snd);
    559    1.1       eeh 
    560   1.15      matt 	/*
    561   1.15      matt 	 * If we support GigE media, we support jumbo frames too.
    562   1.15      matt 	 * Unless we are Apple.
    563   1.15      matt 	 */
    564  1.122   msaitoh 	TAILQ_FOREACH(ife, &mii->mii_media.ifm_list, ifm_list) {
    565  1.122   msaitoh 		if (IFM_SUBTYPE(ife->ifm_media) == IFM_1000_T ||
    566  1.122   msaitoh 		    IFM_SUBTYPE(ife->ifm_media) == IFM_1000_SX ||
    567  1.122   msaitoh 		    IFM_SUBTYPE(ife->ifm_media) == IFM_1000_LX ||
    568  1.122   msaitoh 		    IFM_SUBTYPE(ife->ifm_media) == IFM_1000_CX) {
    569   1.70       jdc 			if (!GEM_IS_APPLE(sc))
    570   1.15      matt 				sc->sc_ethercom.ec_capabilities
    571   1.15      matt 				    |= ETHERCAP_JUMBO_MTU;
    572   1.15      matt 			sc->sc_flags |= GEM_GIGABIT;
    573   1.15      matt 			break;
    574   1.15      matt 		}
    575   1.15      matt 	}
    576   1.15      matt 
    577    1.1       eeh 	/* claim 802.1q capability */
    578    1.1       eeh 	sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
    579    1.1       eeh 
    580    1.1       eeh 	/* Attach the interface. */
    581    1.1       eeh 	if_attach(ifp);
    582  1.108     ozaki 	if_deferred_start_init(ifp, NULL);
    583    1.6   thorpej 	ether_ifattach(ifp, enaddr);
    584   1.79    dyoung 	ether_set_ifflags_cb(&sc->sc_ethercom, gem_ifflags_cb);
    585    1.1       eeh 
    586   1.85    dyoung 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    587  1.102       tls 			  RND_TYPE_NET, RND_FLAG_DEFAULT);
    588    1.1       eeh 
    589   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_intr, EVCNT_TYPE_INTR,
    590   1.85    dyoung 	    NULL, device_xname(sc->sc_dev), "interrupts");
    591   1.19      matt #ifdef GEM_COUNTERS
    592   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_txint, EVCNT_TYPE_INTR,
    593   1.85    dyoung 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "tx interrupts");
    594   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxint, EVCNT_TYPE_INTR,
    595   1.85    dyoung 	    &sc->sc_ev_intr, device_xname(sc->sc_dev), "rx interrupts");
    596   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxfull, EVCNT_TYPE_INTR,
    597   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx ring full");
    598   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxnobuf, EVCNT_TYPE_INTR,
    599   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx malloc failure");
    600  1.133       jdc 	evcnt_attach_dynamic(&sc->sc_ev_rxoverflow, EVCNT_TYPE_INTR,
    601  1.133       jdc 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx overflow");
    602   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[0], EVCNT_TYPE_INTR,
    603   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx 0desc");
    604   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[1], EVCNT_TYPE_INTR,
    605   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx 1desc");
    606   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[2], EVCNT_TYPE_INTR,
    607   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx 2desc");
    608   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[3], EVCNT_TYPE_INTR,
    609   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx 3desc");
    610   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[4], EVCNT_TYPE_INTR,
    611   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx >3desc");
    612   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[5], EVCNT_TYPE_INTR,
    613   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx >7desc");
    614   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[6], EVCNT_TYPE_INTR,
    615   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx >15desc");
    616   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[7], EVCNT_TYPE_INTR,
    617   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx >31desc");
    618   1.18      matt 	evcnt_attach_dynamic(&sc->sc_ev_rxhist[8], EVCNT_TYPE_INTR,
    619   1.85    dyoung 	    &sc->sc_ev_rxint, device_xname(sc->sc_dev), "rx >63desc");
    620   1.19      matt #endif
    621    1.1       eeh 
    622   1.85    dyoung 	callout_init(&sc->sc_tick_ch, 0);
    623  1.128   thorpej 	callout_setfunc(&sc->sc_tick_ch, gem_tick, sc);
    624  1.128   thorpej 
    625   1.99       jdc 	callout_init(&sc->sc_rx_watchdog, 0);
    626   1.99       jdc 	callout_setfunc(&sc->sc_rx_watchdog, gem_rx_watchdog, sc);
    627    1.1       eeh 
    628   1.85    dyoung 	sc->sc_att_stage = GEM_ATT_FINISHED;
    629    1.1       eeh 
    630    1.1       eeh 	return;
    631    1.1       eeh }
    632    1.1       eeh 
    633    1.1       eeh void
    634   1.81       dsl gem_tick(void *arg)
    635    1.1       eeh {
    636    1.1       eeh 	struct gem_softc *sc = arg;
    637    1.1       eeh 	int s;
    638    1.1       eeh 
    639   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0) {
    640   1.68       jdc 		/*
    641   1.68       jdc 		 * We have to reset everything if we failed to get a
    642   1.68       jdc 		 * PCS interrupt.  Restarting the callout is handled
    643   1.68       jdc 		 * in gem_pcs_start().
    644   1.68       jdc 		 */
    645   1.68       jdc 		gem_init(&sc->sc_ethercom.ec_if);
    646   1.68       jdc 	} else {
    647   1.68       jdc 		s = splnet();
    648   1.68       jdc 		mii_tick(&sc->sc_mii);
    649   1.68       jdc 		splx(s);
    650  1.128   thorpej 		callout_schedule(&sc->sc_tick_ch, hz);
    651   1.68       jdc 	}
    652    1.1       eeh }
    653    1.1       eeh 
    654   1.41  christos static int
    655  1.112   msaitoh gem_bitwait(struct gem_softc *sc, bus_space_handle_t h, int r, uint32_t clr,
    656  1.112   msaitoh     uint32_t set)
    657   1.41  christos {
    658   1.41  christos 	int i;
    659  1.112   msaitoh 	uint32_t reg;
    660   1.46     blymn 
    661   1.41  christos 	for (i = TRIES; i--; DELAY(100)) {
    662   1.50    martin 		reg = bus_space_read_4(sc->sc_bustag, h, r);
    663   1.50    martin 		if ((reg & clr) == 0 && (reg & set) == set)
    664   1.41  christos 			return (1);
    665   1.41  christos 	}
    666   1.41  christos 	return (0);
    667   1.41  christos }
    668   1.41  christos 
    669    1.1       eeh void
    670   1.81       dsl gem_reset(struct gem_softc *sc)
    671    1.1       eeh {
    672    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
    673   1.50    martin 	bus_space_handle_t h = sc->sc_h2;
    674    1.1       eeh 	int s;
    675    1.1       eeh 
    676    1.1       eeh 	s = splnet();
    677   1.85    dyoung 	DPRINTF(sc, ("%s: gem_reset\n", device_xname(sc->sc_dev)));
    678    1.1       eeh 	gem_reset_rx(sc);
    679    1.1       eeh 	gem_reset_tx(sc);
    680    1.1       eeh 
    681    1.1       eeh 	/* Do a full reset */
    682  1.117   msaitoh 	bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX);
    683   1.50    martin 	if (!gem_bitwait(sc, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0))
    684   1.85    dyoung 		aprint_error_dev(sc->sc_dev, "cannot reset device\n");
    685    1.1       eeh 	splx(s);
    686    1.1       eeh }
    687    1.1       eeh 
    688    1.1       eeh 
    689    1.1       eeh /*
    690    1.1       eeh  * gem_rxdrain:
    691    1.1       eeh  *
    692    1.1       eeh  *	Drain the receive queue.
    693    1.1       eeh  */
    694   1.41  christos static void
    695    1.1       eeh gem_rxdrain(struct gem_softc *sc)
    696    1.1       eeh {
    697    1.1       eeh 	struct gem_rxsoft *rxs;
    698    1.1       eeh 	int i;
    699    1.1       eeh 
    700    1.1       eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    701    1.1       eeh 		rxs = &sc->sc_rxsoft[i];
    702    1.1       eeh 		if (rxs->rxs_mbuf != NULL) {
    703   1.41  christos 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
    704   1.41  christos 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
    705    1.1       eeh 			bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
    706    1.1       eeh 			m_freem(rxs->rxs_mbuf);
    707    1.1       eeh 			rxs->rxs_mbuf = NULL;
    708    1.1       eeh 		}
    709    1.1       eeh 	}
    710    1.1       eeh }
    711    1.1       eeh 
    712   1.31      heas /*
    713    1.1       eeh  * Reset the whole thing.
    714    1.1       eeh  */
    715   1.41  christos static void
    716    1.1       eeh gem_stop(struct ifnet *ifp, int disable)
    717    1.1       eeh {
    718   1.85    dyoung 	struct gem_softc *sc = ifp->if_softc;
    719    1.1       eeh 	struct gem_txsoft *txs;
    720    1.1       eeh 
    721   1.85    dyoung 	DPRINTF(sc, ("%s: gem_stop\n", device_xname(sc->sc_dev)));
    722    1.1       eeh 
    723   1.95    martin 	callout_halt(&sc->sc_tick_ch, NULL);
    724  1.101       jdc 	callout_halt(&sc->sc_rx_watchdog, NULL);
    725   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
    726   1.68       jdc 		gem_pcs_stop(sc, disable);
    727   1.68       jdc 	else
    728   1.68       jdc 		mii_down(&sc->sc_mii);
    729    1.1       eeh 
    730    1.1       eeh 	/* XXX - Should we reset these instead? */
    731   1.68       jdc 	gem_disable_tx(sc);
    732    1.1       eeh 	gem_disable_rx(sc);
    733    1.1       eeh 
    734    1.1       eeh 	/*
    735    1.1       eeh 	 * Release any queued transmit buffers.
    736    1.1       eeh 	 */
    737    1.1       eeh 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
    738   1.21     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
    739    1.1       eeh 		if (txs->txs_mbuf != NULL) {
    740   1.41  christos 			bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap, 0,
    741   1.41  christos 			    txs->txs_dmamap->dm_mapsize, BUS_DMASYNC_POSTWRITE);
    742    1.1       eeh 			bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
    743    1.1       eeh 			m_freem(txs->txs_mbuf);
    744    1.1       eeh 			txs->txs_mbuf = NULL;
    745    1.1       eeh 		}
    746    1.1       eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
    747    1.1       eeh 	}
    748    1.1       eeh 
    749    1.1       eeh 	/*
    750    1.1       eeh 	 * Mark the interface down and cancel the watchdog timer.
    751    1.1       eeh 	 */
    752  1.130   thorpej 	ifp->if_flags &= ~IFF_RUNNING;
    753   1.41  christos 	sc->sc_if_flags = ifp->if_flags;
    754    1.1       eeh 	ifp->if_timer = 0;
    755   1.75    dyoung 
    756   1.75    dyoung 	if (disable)
    757   1.75    dyoung 		gem_rxdrain(sc);
    758    1.1       eeh }
    759    1.1       eeh 
    760    1.1       eeh 
    761    1.1       eeh /*
    762    1.1       eeh  * Reset the receiver
    763    1.1       eeh  */
    764    1.1       eeh int
    765    1.1       eeh gem_reset_rx(struct gem_softc *sc)
    766    1.1       eeh {
    767    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
    768   1.50    martin 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
    769    1.1       eeh 
    770    1.1       eeh 	/*
    771    1.1       eeh 	 * Resetting while DMA is in progress can cause a bus hang, so we
    772    1.1       eeh 	 * disable DMA first.
    773    1.1       eeh 	 */
    774    1.1       eeh 	gem_disable_rx(sc);
    775    1.1       eeh 	bus_space_write_4(t, h, GEM_RX_CONFIG, 0);
    776   1.68       jdc 	bus_space_barrier(t, h, GEM_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    777    1.1       eeh 	/* Wait till it finishes */
    778   1.50    martin 	if (!gem_bitwait(sc, h, GEM_RX_CONFIG, 1, 0))
    779  1.123   msaitoh 		aprint_error_dev(sc->sc_dev, "cannot disable rx dma\n");
    780   1.99       jdc 	/* Wait 5ms extra. */
    781   1.99       jdc 	delay(5000);
    782    1.1       eeh 
    783    1.1       eeh 	/* Finally, reset the ERX */
    784   1.50    martin 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_RX);
    785   1.68       jdc 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
    786    1.1       eeh 	/* Wait till it finishes */
    787   1.50    martin 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_RX, 0)) {
    788   1.85    dyoung 		aprint_error_dev(sc->sc_dev, "cannot reset receiver\n");
    789    1.1       eeh 		return (1);
    790    1.1       eeh 	}
    791    1.1       eeh 	return (0);
    792    1.1       eeh }
    793    1.1       eeh 
    794    1.1       eeh 
    795    1.1       eeh /*
    796   1.68       jdc  * Reset the receiver DMA engine.
    797   1.68       jdc  *
    798   1.68       jdc  * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW
    799   1.68       jdc  * etc in order to reset the receiver DMA engine only and not do a full
    800   1.68       jdc  * reset which amongst others also downs the link and clears the FIFOs.
    801   1.68       jdc  */
    802   1.68       jdc static void
    803   1.68       jdc gem_reset_rxdma(struct gem_softc *sc)
    804   1.68       jdc {
    805   1.68       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
    806   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
    807   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
    808   1.68       jdc 	int i;
    809   1.68       jdc 
    810   1.68       jdc 	if (gem_reset_rx(sc) != 0) {
    811   1.68       jdc 		gem_init(ifp);
    812   1.68       jdc 		return;
    813   1.68       jdc 	}
    814   1.68       jdc 	for (i = 0; i < GEM_NRXDESC; i++)
    815   1.68       jdc 		if (sc->sc_rxsoft[i].rxs_mbuf != NULL)
    816   1.68       jdc 			GEM_UPDATE_RXDESC(sc, i);
    817   1.68       jdc 	sc->sc_rxptr = 0;
    818   1.68       jdc 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
    819   1.68       jdc 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
    820   1.68       jdc 
    821   1.68       jdc 	/* Reprogram Descriptor Ring Base Addresses */
    822  1.129   thorpej 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI,
    823  1.129   thorpej 	    ((uint64_t)GEM_CDRXADDR(sc, 0)) >> 32);
    824   1.68       jdc 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
    825   1.68       jdc 
    826   1.68       jdc 	/* Redo ERX Configuration */
    827   1.68       jdc 	gem_rx_common(sc);
    828   1.68       jdc 
    829  1.120   msaitoh 	/* Give the receiver a swift kick */
    830   1.68       jdc 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC - 4);
    831   1.68       jdc }
    832   1.68       jdc 
    833   1.68       jdc /*
    834   1.68       jdc  * Common RX configuration for gem_init() and gem_reset_rxdma().
    835   1.68       jdc  */
    836   1.68       jdc static void
    837   1.68       jdc gem_rx_common(struct gem_softc *sc)
    838   1.68       jdc {
    839   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
    840   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
    841  1.112   msaitoh 	uint32_t v;
    842   1.68       jdc 
    843   1.68       jdc 	/* Encode Receive Descriptor ring size: four possible values */
    844   1.68       jdc 	v = gem_ringsize(GEM_NRXDESC /*XXX*/);
    845   1.68       jdc 
    846   1.68       jdc 	/* Set receive h/w checksum offset */
    847   1.68       jdc #ifdef INET
    848   1.68       jdc 	v |= (ETHER_HDR_LEN + sizeof(struct ip) +
    849   1.68       jdc 	    ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
    850   1.68       jdc 	    ETHER_VLAN_ENCAP_LEN : 0)) << GEM_RX_CONFIG_CXM_START_SHFT;
    851   1.68       jdc #endif
    852   1.68       jdc 
    853   1.68       jdc 	/* Enable RX DMA */
    854   1.68       jdc 	bus_space_write_4(t, h, GEM_RX_CONFIG,
    855   1.68       jdc 	    v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) |
    856   1.68       jdc 	    (2 << GEM_RX_CONFIG_FBOFF_SHFT) | GEM_RX_CONFIG_RXDMA_EN);
    857   1.68       jdc 
    858   1.68       jdc 	/*
    859   1.68       jdc 	 * The following value is for an OFF Threshold of about 3/4 full
    860   1.68       jdc 	 * and an ON Threshold of 1/4 full.
    861   1.68       jdc 	 */
    862   1.68       jdc 	bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH,
    863   1.68       jdc 	    (3 * sc->sc_rxfifosize / 256) |
    864   1.68       jdc 	    ((sc->sc_rxfifosize / 256) << 12));
    865   1.68       jdc 	bus_space_write_4(t, h, GEM_RX_BLANKING,
    866   1.99       jdc 	    (6 << GEM_RX_BLANKING_TIME_SHIFT) | 8);
    867   1.68       jdc }
    868   1.68       jdc 
    869   1.68       jdc /*
    870    1.1       eeh  * Reset the transmitter
    871    1.1       eeh  */
    872    1.1       eeh int
    873    1.1       eeh gem_reset_tx(struct gem_softc *sc)
    874    1.1       eeh {
    875    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
    876   1.50    martin 	bus_space_handle_t h = sc->sc_h1, h2 = sc->sc_h2;
    877    1.1       eeh 
    878    1.1       eeh 	/*
    879    1.1       eeh 	 * Resetting while DMA is in progress can cause a bus hang, so we
    880    1.1       eeh 	 * disable DMA first.
    881    1.1       eeh 	 */
    882    1.1       eeh 	gem_disable_tx(sc);
    883    1.1       eeh 	bus_space_write_4(t, h, GEM_TX_CONFIG, 0);
    884   1.68       jdc 	bus_space_barrier(t, h, GEM_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    885    1.1       eeh 	/* Wait till it finishes */
    886   1.50    martin 	if (!gem_bitwait(sc, h, GEM_TX_CONFIG, 1, 0))
    887  1.131   msaitoh 		aprint_error_dev(sc->sc_dev, "cannot disable tx dma\n");
    888    1.1       eeh 	/* Wait 5ms extra. */
    889    1.1       eeh 	delay(5000);
    890    1.1       eeh 
    891    1.1       eeh 	/* Finally, reset the ETX */
    892   1.50    martin 	bus_space_write_4(t, h2, GEM_RESET, GEM_RESET_TX);
    893   1.68       jdc 	bus_space_barrier(t, h, GEM_RESET, 4, BUS_SPACE_BARRIER_WRITE);
    894    1.1       eeh 	/* Wait till it finishes */
    895   1.50    martin 	if (!gem_bitwait(sc, h2, GEM_RESET, GEM_RESET_TX, 0)) {
    896  1.131   msaitoh 		aprint_error_dev(sc->sc_dev, "cannot reset transmitter\n");
    897    1.1       eeh 		return (1);
    898    1.1       eeh 	}
    899    1.1       eeh 	return (0);
    900    1.1       eeh }
    901    1.1       eeh 
    902    1.1       eeh /*
    903    1.1       eeh  * disable receiver.
    904    1.1       eeh  */
    905    1.1       eeh int
    906    1.1       eeh gem_disable_rx(struct gem_softc *sc)
    907    1.1       eeh {
    908    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
    909   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
    910  1.112   msaitoh 	uint32_t cfg;
    911    1.1       eeh 
    912    1.1       eeh 	/* Flip the enable bit */
    913    1.1       eeh 	cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
    914    1.1       eeh 	cfg &= ~GEM_MAC_RX_ENABLE;
    915    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg);
    916   1.68       jdc 	bus_space_barrier(t, h, GEM_MAC_RX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    917    1.1       eeh 	/* Wait for it to finish */
    918   1.50    martin 	return (gem_bitwait(sc, h, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0));
    919    1.1       eeh }
    920    1.1       eeh 
    921    1.1       eeh /*
    922    1.1       eeh  * disable transmitter.
    923    1.1       eeh  */
    924    1.1       eeh int
    925    1.1       eeh gem_disable_tx(struct gem_softc *sc)
    926    1.1       eeh {
    927    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
    928   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
    929  1.112   msaitoh 	uint32_t cfg;
    930    1.1       eeh 
    931    1.1       eeh 	/* Flip the enable bit */
    932    1.1       eeh 	cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG);
    933    1.1       eeh 	cfg &= ~GEM_MAC_TX_ENABLE;
    934    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg);
    935   1.68       jdc 	bus_space_barrier(t, h, GEM_MAC_TX_CONFIG, 4, BUS_SPACE_BARRIER_WRITE);
    936    1.1       eeh 	/* Wait for it to finish */
    937   1.50    martin 	return (gem_bitwait(sc, h, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0));
    938    1.1       eeh }
    939    1.1       eeh 
    940    1.1       eeh /*
    941    1.1       eeh  * Initialize interface.
    942    1.1       eeh  */
    943    1.1       eeh int
    944    1.1       eeh gem_meminit(struct gem_softc *sc)
    945    1.1       eeh {
    946    1.1       eeh 	struct gem_rxsoft *rxs;
    947    1.1       eeh 	int i, error;
    948    1.1       eeh 
    949    1.1       eeh 	/*
    950    1.1       eeh 	 * Initialize the transmit descriptor ring.
    951    1.1       eeh 	 */
    952   1.85    dyoung 	memset(sc->sc_txdescs, 0, sizeof(sc->sc_txdescs));
    953    1.1       eeh 	for (i = 0; i < GEM_NTXDESC; i++) {
    954    1.1       eeh 		sc->sc_txdescs[i].gd_flags = 0;
    955    1.1       eeh 		sc->sc_txdescs[i].gd_addr = 0;
    956    1.1       eeh 	}
    957    1.1       eeh 	GEM_CDTXSYNC(sc, 0, GEM_NTXDESC,
    958  1.117   msaitoh 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
    959   1.14      matt 	sc->sc_txfree = GEM_NTXDESC-1;
    960    1.1       eeh 	sc->sc_txnext = 0;
    961   1.14      matt 	sc->sc_txwin = 0;
    962    1.1       eeh 
    963    1.1       eeh 	/*
    964    1.1       eeh 	 * Initialize the receive descriptor and receive job
    965    1.1       eeh 	 * descriptor rings.
    966    1.1       eeh 	 */
    967    1.1       eeh 	for (i = 0; i < GEM_NRXDESC; i++) {
    968    1.1       eeh 		rxs = &sc->sc_rxsoft[i];
    969    1.1       eeh 		if (rxs->rxs_mbuf == NULL) {
    970    1.1       eeh 			if ((error = gem_add_rxbuf(sc, i)) != 0) {
    971   1.85    dyoung 				aprint_error_dev(sc->sc_dev,
    972   1.85    dyoung 				    "unable to allocate or map rx "
    973    1.1       eeh 				    "buffer %d, error = %d\n",
    974   1.76    cegger 				    i, error);
    975    1.1       eeh 				/*
    976    1.1       eeh 				 * XXX Should attempt to run with fewer receive
    977    1.1       eeh 				 * XXX buffers instead of just failing.
    978    1.1       eeh 				 */
    979    1.1       eeh 				gem_rxdrain(sc);
    980    1.1       eeh 				return (1);
    981    1.1       eeh 			}
    982    1.1       eeh 		} else
    983    1.1       eeh 			GEM_INIT_RXDESC(sc, i);
    984    1.1       eeh 	}
    985    1.1       eeh 	sc->sc_rxptr = 0;
    986   1.68       jdc 	sc->sc_meminited = 1;
    987   1.68       jdc 	GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE);
    988   1.68       jdc 	GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD);
    989    1.1       eeh 
    990    1.1       eeh 	return (0);
    991    1.1       eeh }
    992    1.1       eeh 
    993    1.1       eeh static int
    994    1.1       eeh gem_ringsize(int sz)
    995    1.1       eeh {
    996    1.1       eeh 	switch (sz) {
    997    1.1       eeh 	case 32:
    998   1.29  christos 		return GEM_RING_SZ_32;
    999    1.1       eeh 	case 64:
   1000   1.29  christos 		return GEM_RING_SZ_64;
   1001    1.1       eeh 	case 128:
   1002   1.29  christos 		return GEM_RING_SZ_128;
   1003    1.1       eeh 	case 256:
   1004   1.29  christos 		return GEM_RING_SZ_256;
   1005    1.1       eeh 	case 512:
   1006   1.29  christos 		return GEM_RING_SZ_512;
   1007    1.1       eeh 	case 1024:
   1008   1.29  christos 		return GEM_RING_SZ_1024;
   1009    1.1       eeh 	case 2048:
   1010   1.29  christos 		return GEM_RING_SZ_2048;
   1011    1.1       eeh 	case 4096:
   1012   1.29  christos 		return GEM_RING_SZ_4096;
   1013    1.1       eeh 	case 8192:
   1014   1.29  christos 		return GEM_RING_SZ_8192;
   1015    1.1       eeh 	default:
   1016   1.29  christos 		printf("gem: invalid Receive Descriptor ring size %d\n", sz);
   1017   1.29  christos 		return GEM_RING_SZ_32;
   1018    1.1       eeh 	}
   1019    1.1       eeh }
   1020    1.1       eeh 
   1021   1.68       jdc 
   1022   1.68       jdc /*
   1023   1.68       jdc  * Start PCS
   1024   1.68       jdc  */
   1025   1.68       jdc void
   1026   1.68       jdc gem_pcs_start(struct gem_softc *sc)
   1027   1.68       jdc {
   1028   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
   1029   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
   1030   1.68       jdc 	uint32_t v;
   1031   1.68       jdc 
   1032   1.68       jdc #ifdef GEM_DEBUG
   1033   1.85    dyoung 	aprint_debug_dev(sc->sc_dev, "gem_pcs_start()\n");
   1034   1.68       jdc #endif
   1035   1.68       jdc 
   1036   1.68       jdc 	/*
   1037   1.68       jdc 	 * Set up.  We must disable the MII before modifying the
   1038   1.68       jdc 	 * GEM_MII_ANAR register
   1039   1.68       jdc 	 */
   1040   1.68       jdc 	if (sc->sc_flags & GEM_SERDES) {
   1041   1.68       jdc 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
   1042   1.68       jdc 		    GEM_MII_DATAPATH_SERDES);
   1043   1.68       jdc 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
   1044   1.68       jdc 		    GEM_MII_SLINK_LOOPBACK);
   1045   1.68       jdc 	} else {
   1046   1.68       jdc 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
   1047   1.68       jdc 		    GEM_MII_DATAPATH_SERIAL);
   1048   1.68       jdc 		bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL, 0);
   1049   1.68       jdc 	}
   1050   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
   1051   1.68       jdc 	v = bus_space_read_4(t, h, GEM_MII_ANAR);
   1052   1.68       jdc 	v |= (GEM_MII_ANEG_SYM_PAUSE | GEM_MII_ANEG_ASYM_PAUSE);
   1053  1.115   msaitoh 	if (IFM_SUBTYPE(sc->sc_mii_media) == IFM_AUTO)
   1054   1.68       jdc 		v |= (GEM_MII_ANEG_FUL_DUPLX | GEM_MII_ANEG_HLF_DUPLX);
   1055  1.115   msaitoh 	else if ((IFM_OPTIONS(sc->sc_mii_media) & IFM_FDX) != 0) {
   1056   1.68       jdc 		v |= GEM_MII_ANEG_FUL_DUPLX;
   1057   1.68       jdc 		v &= ~GEM_MII_ANEG_HLF_DUPLX;
   1058  1.115   msaitoh 	} else if ((IFM_OPTIONS(sc->sc_mii_media) & IFM_HDX) != 0) {
   1059   1.68       jdc 		v &= ~GEM_MII_ANEG_FUL_DUPLX;
   1060   1.68       jdc 		v |= GEM_MII_ANEG_HLF_DUPLX;
   1061   1.68       jdc 	}
   1062   1.68       jdc 
   1063   1.68       jdc 	/* Configure link. */
   1064   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_ANAR, v);
   1065   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONTROL,
   1066   1.68       jdc 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
   1067   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE);
   1068   1.68       jdc 	gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_ANEG_CPT);
   1069   1.68       jdc 
   1070   1.68       jdc 	/* Start the 10 second timer */
   1071  1.128   thorpej 	callout_schedule(&sc->sc_tick_ch, hz * 10);
   1072   1.68       jdc }
   1073   1.68       jdc 
   1074   1.68       jdc /*
   1075   1.68       jdc  * Stop PCS
   1076   1.68       jdc  */
   1077   1.68       jdc void
   1078   1.68       jdc gem_pcs_stop(struct gem_softc *sc, int disable)
   1079   1.68       jdc {
   1080   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
   1081   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
   1082   1.68       jdc 
   1083   1.68       jdc #ifdef GEM_DEBUG
   1084   1.85    dyoung 	aprint_debug_dev(sc->sc_dev, "gem_pcs_stop()\n");
   1085   1.68       jdc #endif
   1086   1.68       jdc 
   1087   1.68       jdc 	/* Tell link partner that we're going away */
   1088   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_ANAR, GEM_MII_ANEG_RF);
   1089   1.68       jdc 
   1090   1.68       jdc 	/*
   1091   1.68       jdc 	 * Disable PCS MII.  The documentation suggests that setting
   1092   1.68       jdc 	 * GEM_MII_CONFIG_ENABLE to zero and then restarting auto-
   1093   1.68       jdc 	 * negotiation will shut down the link.  However, it appears
   1094   1.68       jdc 	 * that we also need to unset the datapath mode.
   1095   1.68       jdc 	 */
   1096   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
   1097   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONTROL,
   1098   1.68       jdc 	    GEM_MII_CONTROL_AUTONEG | GEM_MII_CONTROL_RAN);
   1099   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE, GEM_MII_DATAPATH_MII);
   1100   1.68       jdc 	bus_space_write_4(t, h, GEM_MII_CONFIG, 0);
   1101   1.68       jdc 
   1102   1.68       jdc 	if (disable) {
   1103   1.68       jdc 		if (sc->sc_flags & GEM_SERDES)
   1104   1.68       jdc 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
   1105   1.68       jdc 				GEM_MII_SLINK_POWER_OFF);
   1106   1.68       jdc 		else
   1107   1.68       jdc 			bus_space_write_4(t, h, GEM_MII_SLINK_CONTROL,
   1108   1.68       jdc 			    GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_POWER_OFF);
   1109   1.68       jdc 	}
   1110   1.68       jdc 
   1111   1.68       jdc 	sc->sc_flags &= ~GEM_LINK;
   1112   1.68       jdc 	sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
   1113   1.68       jdc 	sc->sc_mii.mii_media_status = IFM_AVALID;
   1114   1.68       jdc }
   1115   1.68       jdc 
   1116   1.68       jdc 
   1117    1.1       eeh /*
   1118    1.1       eeh  * Initialization of interface; set up initialization block
   1119    1.1       eeh  * and transmit/receive descriptor rings.
   1120    1.1       eeh  */
   1121    1.1       eeh int
   1122    1.1       eeh gem_init(struct ifnet *ifp)
   1123    1.1       eeh {
   1124   1.85    dyoung 	struct gem_softc *sc = ifp->if_softc;
   1125    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   1126   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
   1127   1.69    dyoung 	int rc = 0, s;
   1128   1.15      matt 	u_int max_frame_size;
   1129  1.112   msaitoh 	uint32_t v;
   1130    1.1       eeh 
   1131    1.1       eeh 	s = splnet();
   1132    1.1       eeh 
   1133   1.85    dyoung 	DPRINTF(sc, ("%s: gem_init: calling stop\n", device_xname(sc->sc_dev)));
   1134    1.1       eeh 	/*
   1135    1.1       eeh 	 * Initialization sequence. The numbered steps below correspond
   1136    1.1       eeh 	 * to the sequence outlined in section 6.3.5.1 in the Ethernet
   1137    1.1       eeh 	 * Channel Engine manual (part of the PCIO manual).
   1138    1.1       eeh 	 * See also the STP2002-STQ document from Sun Microsystems.
   1139    1.1       eeh 	 */
   1140    1.1       eeh 
   1141    1.1       eeh 	/* step 1 & 2. Reset the Ethernet Channel */
   1142    1.1       eeh 	gem_stop(ifp, 0);
   1143    1.1       eeh 	gem_reset(sc);
   1144   1.85    dyoung 	DPRINTF(sc, ("%s: gem_init: restarting\n", device_xname(sc->sc_dev)));
   1145    1.1       eeh 
   1146    1.1       eeh 	/* Re-initialize the MIF */
   1147    1.1       eeh 	gem_mifinit(sc);
   1148    1.1       eeh 
   1149   1.68       jdc 	/* Set up correct datapath for non-SERDES/Serialink */
   1150   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0 &&
   1151   1.68       jdc 	    sc->sc_variant != GEM_SUN_ERI)
   1152   1.68       jdc 		bus_space_write_4(t, h, GEM_MII_DATAPATH_MODE,
   1153   1.68       jdc 		    GEM_MII_DATAPATH_MII);
   1154   1.68       jdc 
   1155    1.1       eeh 	/* Call MI reset function if any */
   1156    1.1       eeh 	if (sc->sc_hwreset)
   1157    1.1       eeh 		(*sc->sc_hwreset)(sc);
   1158    1.1       eeh 
   1159    1.1       eeh 	/* step 3. Setup data structures in host memory */
   1160  1.103  dholland 	if (gem_meminit(sc) != 0) {
   1161  1.103  dholland 		splx(s);
   1162   1.68       jdc 		return 1;
   1163  1.103  dholland 	}
   1164    1.1       eeh 
   1165    1.1       eeh 	/* step 4. TX MAC registers & counters */
   1166    1.1       eeh 	gem_init_regs(sc);
   1167  1.111  riastrad 	max_frame_size = uimax(sc->sc_ethercom.ec_if.if_mtu, ETHERMTU);
   1168   1.15      matt 	max_frame_size += ETHER_HDR_LEN + ETHER_CRC_LEN;
   1169   1.15      matt 	if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU)
   1170   1.15      matt 		max_frame_size += ETHER_VLAN_ENCAP_LEN;
   1171    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
   1172   1.15      matt 	    max_frame_size|/* burst size */(0x2000<<16));
   1173    1.1       eeh 
   1174    1.1       eeh 	/* step 5. RX MAC registers & counters */
   1175    1.1       eeh 	gem_setladrf(sc);
   1176    1.1       eeh 
   1177    1.1       eeh 	/* step 6 & 7. Program Descriptor Ring Base Addresses */
   1178  1.129   thorpej 	bus_space_write_4(t, h, GEM_TX_RING_PTR_HI,
   1179  1.129   thorpej 	    ((uint64_t)GEM_CDTXADDR(sc, 0)) >> 32);
   1180    1.4   thorpej 	bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0));
   1181    1.4   thorpej 
   1182  1.129   thorpej 	bus_space_write_4(t, h, GEM_RX_RING_PTR_HI,
   1183  1.129   thorpej 	    ((uint64_t)GEM_CDRXADDR(sc, 0)) >> 32);
   1184    1.4   thorpej 	bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0));
   1185    1.1       eeh 
   1186    1.1       eeh 	/* step 8. Global Configuration & Interrupt Mask */
   1187   1.85    dyoung 	gem_inten(sc);
   1188   1.16      matt 	bus_space_write_4(t, h, GEM_MAC_RX_MASK,
   1189   1.68       jdc 			GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT);
   1190   1.68       jdc 	bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXX */
   1191   1.68       jdc 	bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK,
   1192   1.68       jdc 	    GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME);
   1193    1.5   thorpej 
   1194    1.1       eeh 	/* step 9. ETX Configuration: use mostly default values */
   1195    1.1       eeh 
   1196   1.68       jdc 	/* Enable TX DMA */
   1197    1.1       eeh 	v = gem_ringsize(GEM_NTXDESC /*XXX*/);
   1198   1.31      heas 	bus_space_write_4(t, h, GEM_TX_CONFIG,
   1199   1.87       jdc 	    v | GEM_TX_CONFIG_TXDMA_EN |
   1200   1.87       jdc 	    (((sc->sc_flags & GEM_GIGABIT ? 0x4FF : 0x100) << 10) &
   1201   1.87       jdc 	    GEM_TX_CONFIG_TXFIFO_TH));
   1202    1.1       eeh 	bus_space_write_4(t, h, GEM_TX_KICK, sc->sc_txnext);
   1203    1.1       eeh 
   1204    1.1       eeh 	/* step 10. ERX Configuration */
   1205   1.68       jdc 	gem_rx_common(sc);
   1206    1.1       eeh 
   1207    1.1       eeh 	/* step 11. Configure Media */
   1208   1.69    dyoung 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0 &&
   1209   1.69    dyoung 	    (rc = mii_ifmedia_change(&sc->sc_mii)) != 0)
   1210   1.69    dyoung 		goto out;
   1211    1.1       eeh 
   1212    1.1       eeh 	/* step 12. RX_MAC Configuration Register */
   1213    1.1       eeh 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
   1214   1.35      heas 	v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC;
   1215    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
   1216    1.1       eeh 
   1217    1.1       eeh 	/* step 14. Issue Transmit Pending command */
   1218    1.1       eeh 
   1219    1.1       eeh 	/* Call MI initialization function if any */
   1220    1.1       eeh 	if (sc->sc_hwinit)
   1221    1.1       eeh 		(*sc->sc_hwinit)(sc);
   1222    1.1       eeh 
   1223  1.120   msaitoh 	/* step 15.  Give the receiver a swift kick */
   1224    1.1       eeh 	bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4);
   1225    1.1       eeh 
   1226   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
   1227   1.68       jdc 		/* Configure PCS */
   1228   1.68       jdc 		gem_pcs_start(sc);
   1229   1.68       jdc 	else
   1230   1.68       jdc 		/* Start the one second timer. */
   1231  1.128   thorpej 		callout_schedule(&sc->sc_tick_ch, hz);
   1232    1.1       eeh 
   1233   1.68       jdc 	sc->sc_flags &= ~GEM_LINK;
   1234    1.1       eeh 	ifp->if_flags |= IFF_RUNNING;
   1235    1.1       eeh 	ifp->if_timer = 0;
   1236   1.41  christos 	sc->sc_if_flags = ifp->if_flags;
   1237   1.69    dyoung out:
   1238    1.1       eeh 	splx(s);
   1239    1.1       eeh 
   1240    1.1       eeh 	return (0);
   1241    1.1       eeh }
   1242    1.1       eeh 
   1243    1.1       eeh void
   1244    1.1       eeh gem_init_regs(struct gem_softc *sc)
   1245    1.1       eeh {
   1246    1.1       eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1247    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   1248   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
   1249   1.58    dyoung 	const u_char *laddr = CLLADDR(ifp->if_sadl);
   1250  1.112   msaitoh 	uint32_t v;
   1251    1.1       eeh 
   1252    1.1       eeh 	/* These regs are not cleared on reset */
   1253    1.1       eeh 	if (!sc->sc_inited) {
   1254    1.1       eeh 
   1255   1.68       jdc 		/* Load recommended values */
   1256   1.68       jdc 		bus_space_write_4(t, h, GEM_MAC_IPG0, 0x00);
   1257   1.68       jdc 		bus_space_write_4(t, h, GEM_MAC_IPG1, 0x08);
   1258   1.68       jdc 		bus_space_write_4(t, h, GEM_MAC_IPG2, 0x04);
   1259    1.1       eeh 
   1260    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN);
   1261    1.1       eeh 		/* Max frame and max burst size */
   1262    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME,
   1263   1.68       jdc 		    ETHER_MAX_LEN | (0x2000<<16));
   1264   1.15      matt 
   1265   1.68       jdc 		bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x07);
   1266   1.68       jdc 		bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x04);
   1267    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10);
   1268    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088);
   1269    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED,
   1270   1.15      matt 		    ((laddr[5]<<8)|laddr[4])&0x3ff);
   1271   1.13      matt 
   1272    1.1       eeh 		/* Secondary MAC addr set to 0:0:0:0:0:0 */
   1273    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR3, 0);
   1274    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR4, 0);
   1275    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR5, 0);
   1276   1.13      matt 
   1277   1.13      matt 		/* MAC control addr set to 01:80:c2:00:00:01 */
   1278    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001);
   1279    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200);
   1280    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180);
   1281    1.1       eeh 
   1282    1.1       eeh 		/* MAC filter addr set to 0:0:0:0:0:0 */
   1283    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0);
   1284    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0);
   1285    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0);
   1286    1.1       eeh 
   1287    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0);
   1288    1.1       eeh 		bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0);
   1289    1.1       eeh 
   1290    1.1       eeh 		sc->sc_inited = 1;
   1291    1.1       eeh 	}
   1292    1.1       eeh 
   1293    1.1       eeh 	/* Counters need to be zeroed */
   1294    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0);
   1295    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0);
   1296    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0);
   1297    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0);
   1298    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0);
   1299    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0);
   1300    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0);
   1301    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
   1302    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
   1303    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
   1304    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
   1305    1.1       eeh 
   1306   1.68       jdc 	/* Set XOFF PAUSE time. */
   1307    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0);
   1308   1.68       jdc 
   1309   1.68       jdc 	/*
   1310   1.68       jdc 	 * Set the internal arbitration to "infinite" bursts of the
   1311   1.68       jdc 	 * maximum length of 31 * 64 bytes so DMA transfers aren't
   1312   1.68       jdc 	 * split up in cache line size chunks. This greatly improves
   1313   1.68       jdc 	 * especially RX performance.
   1314   1.68       jdc 	 * Enable silicon bug workarounds for the Apple variants.
   1315   1.68       jdc 	 */
   1316   1.68       jdc 	bus_space_write_4(t, h, GEM_CONFIG,
   1317   1.68       jdc 	    GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT |
   1318   1.78       jdc 	    ((sc->sc_flags & GEM_PCI) ?
   1319   1.78       jdc 	    GEM_CONFIG_BURST_INF : GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ?
   1320   1.68       jdc 	    GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0));
   1321    1.1       eeh 
   1322    1.1       eeh 	/*
   1323    1.1       eeh 	 * Set the station address.
   1324    1.1       eeh 	 */
   1325   1.13      matt 	bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]);
   1326   1.13      matt 	bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]);
   1327   1.13      matt 	bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]);
   1328    1.1       eeh 
   1329   1.15      matt 	/*
   1330   1.15      matt 	 * Enable MII outputs.  Enable GMII if there is a gigabit PHY.
   1331   1.15      matt 	 */
   1332   1.70       jdc 	sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG);
   1333   1.15      matt 	v = GEM_MAC_XIF_TX_MII_ENA;
   1334  1.118   msaitoh 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0) {
   1335   1.70       jdc 		if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) {
   1336   1.70       jdc 			v |= GEM_MAC_XIF_FDPLX_LED;
   1337   1.70       jdc 				if (sc->sc_flags & GEM_GIGABIT)
   1338   1.70       jdc 					v |= GEM_MAC_XIF_GMII_MODE;
   1339   1.70       jdc 		}
   1340   1.70       jdc 	} else {
   1341   1.68       jdc 		v |= GEM_MAC_XIF_GMII_MODE;
   1342   1.70       jdc 	}
   1343   1.15      matt 	bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v);
   1344    1.1       eeh }
   1345    1.1       eeh 
   1346   1.67    dyoung #ifdef GEM_DEBUG
   1347   1.67    dyoung static void
   1348   1.67    dyoung gem_txsoft_print(const struct gem_softc *sc, int firstdesc, int lastdesc)
   1349   1.67    dyoung {
   1350   1.67    dyoung 	int i;
   1351   1.67    dyoung 
   1352   1.67    dyoung 	for (i = firstdesc;; i = GEM_NEXTTX(i)) {
   1353   1.67    dyoung 		printf("descriptor %d:\t", i);
   1354   1.67    dyoung 		printf("gd_flags:   0x%016" PRIx64 "\t",
   1355   1.67    dyoung 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags));
   1356   1.67    dyoung 		printf("gd_addr: 0x%016" PRIx64 "\n",
   1357   1.67    dyoung 			GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr));
   1358   1.67    dyoung 		if (i == lastdesc)
   1359   1.67    dyoung 			break;
   1360   1.67    dyoung 	}
   1361   1.67    dyoung }
   1362   1.67    dyoung #endif
   1363   1.67    dyoung 
   1364   1.41  christos static void
   1365   1.81       dsl gem_start(struct ifnet *ifp)
   1366    1.1       eeh {
   1367   1.85    dyoung 	struct gem_softc *sc = ifp->if_softc;
   1368    1.1       eeh 	struct mbuf *m0, *m;
   1369   1.64    dyoung 	struct gem_txsoft *txs;
   1370    1.1       eeh 	bus_dmamap_t dmamap;
   1371   1.49    martin 	int error, firsttx, nexttx = -1, lasttx = -1, ofree, seg;
   1372  1.105       jdc #ifdef GEM_DEBUG
   1373  1.105       jdc 	int otxnext;
   1374  1.105       jdc #endif
   1375   1.40    bouyer 	uint64_t flags = 0;
   1376    1.1       eeh 
   1377  1.130   thorpej 	if ((ifp->if_flags & IFF_RUNNING) != IFF_RUNNING)
   1378    1.1       eeh 		return;
   1379    1.1       eeh 
   1380    1.1       eeh 	/*
   1381    1.1       eeh 	 * Remember the previous number of free descriptors and
   1382    1.1       eeh 	 * the first descriptor we'll use.
   1383    1.1       eeh 	 */
   1384    1.1       eeh 	ofree = sc->sc_txfree;
   1385  1.105       jdc #ifdef GEM_DEBUG
   1386  1.105       jdc 	otxnext = sc->sc_txnext;
   1387  1.105       jdc #endif
   1388    1.1       eeh 
   1389    1.1       eeh 	DPRINTF(sc, ("%s: gem_start: txfree %d, txnext %d\n",
   1390  1.105       jdc 	    device_xname(sc->sc_dev), ofree, otxnext));
   1391    1.1       eeh 
   1392    1.1       eeh 	/*
   1393    1.1       eeh 	 * Loop through the send queue, setting up transmit descriptors
   1394    1.1       eeh 	 * until we drain the queue, or use up all available transmit
   1395    1.1       eeh 	 * descriptors.
   1396    1.1       eeh 	 */
   1397  1.126  christos #ifdef INET
   1398  1.124   msaitoh next:
   1399  1.126  christos #endif
   1400   1.11   thorpej 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txfreeq)) != NULL &&
   1401   1.68       jdc 	    sc->sc_txfree != 0) {
   1402    1.1       eeh 		/*
   1403    1.1       eeh 		 * Grab a packet off the queue.
   1404    1.1       eeh 		 */
   1405    1.1       eeh 		IFQ_POLL(&ifp->if_snd, m0);
   1406    1.1       eeh 		if (m0 == NULL)
   1407    1.1       eeh 			break;
   1408    1.1       eeh 		m = NULL;
   1409    1.1       eeh 
   1410    1.1       eeh 		dmamap = txs->txs_dmamap;
   1411    1.1       eeh 
   1412    1.1       eeh 		/*
   1413    1.1       eeh 		 * Load the DMA map.  If this fails, the packet either
   1414    1.1       eeh 		 * didn't fit in the alloted number of segments, or we were
   1415    1.1       eeh 		 * short on resources.  In this case, we'll copy and try
   1416    1.1       eeh 		 * again.
   1417    1.1       eeh 		 */
   1418    1.1       eeh 		if (bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap, m0,
   1419  1.117   msaitoh 		      BUS_DMA_WRITE | BUS_DMA_NOWAIT) != 0 ||
   1420   1.40    bouyer 		      (m0->m_pkthdr.len < ETHER_MIN_TX &&
   1421   1.40    bouyer 		       dmamap->dm_nsegs == GEM_NTXSEGS)) {
   1422   1.15      matt 			if (m0->m_pkthdr.len > MCLBYTES) {
   1423   1.85    dyoung 				aprint_error_dev(sc->sc_dev,
   1424   1.85    dyoung 				    "unable to allocate jumbo Tx cluster\n");
   1425   1.15      matt 				IFQ_DEQUEUE(&ifp->if_snd, m0);
   1426   1.15      matt 				m_freem(m0);
   1427   1.15      matt 				continue;
   1428   1.15      matt 			}
   1429    1.1       eeh 			MGETHDR(m, M_DONTWAIT, MT_DATA);
   1430    1.1       eeh 			if (m == NULL) {
   1431   1.85    dyoung 				aprint_error_dev(sc->sc_dev,
   1432   1.85    dyoung 				    "unable to allocate Tx mbuf\n");
   1433    1.1       eeh 				break;
   1434    1.1       eeh 			}
   1435   1.26      matt 			MCLAIM(m, &sc->sc_ethercom.ec_tx_mowner);
   1436    1.1       eeh 			if (m0->m_pkthdr.len > MHLEN) {
   1437    1.1       eeh 				MCLGET(m, M_DONTWAIT);
   1438    1.1       eeh 				if ((m->m_flags & M_EXT) == 0) {
   1439   1.85    dyoung 					aprint_error_dev(sc->sc_dev,
   1440   1.85    dyoung 					    "unable to allocate Tx cluster\n");
   1441    1.1       eeh 					m_freem(m);
   1442    1.1       eeh 					break;
   1443    1.1       eeh 				}
   1444    1.1       eeh 			}
   1445   1.53  christos 			m_copydata(m0, 0, m0->m_pkthdr.len, mtod(m, void *));
   1446    1.1       eeh 			m->m_pkthdr.len = m->m_len = m0->m_pkthdr.len;
   1447    1.1       eeh 			error = bus_dmamap_load_mbuf(sc->sc_dmatag, dmamap,
   1448  1.117   msaitoh 			    m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
   1449    1.1       eeh 			if (error) {
   1450   1.85    dyoung 				aprint_error_dev(sc->sc_dev,
   1451   1.85    dyoung 				    "unable to load Tx buffer, error = %d\n",
   1452   1.85    dyoung 				    error);
   1453    1.1       eeh 				break;
   1454    1.1       eeh 			}
   1455    1.1       eeh 		}
   1456    1.1       eeh 
   1457    1.1       eeh 		/*
   1458    1.1       eeh 		 * Ensure we have enough descriptors free to describe
   1459   1.11   thorpej 		 * the packet.
   1460    1.1       eeh 		 */
   1461   1.40    bouyer 		if (dmamap->dm_nsegs > ((m0->m_pkthdr.len < ETHER_MIN_TX) ?
   1462   1.40    bouyer 		     (sc->sc_txfree - 1) : sc->sc_txfree)) {
   1463    1.1       eeh 			/*
   1464    1.1       eeh 			 * Not enough free descriptors to transmit this
   1465  1.130   thorpej 			 * packet.
   1466    1.1       eeh 			 */
   1467    1.1       eeh 			bus_dmamap_unload(sc->sc_dmatag, dmamap);
   1468    1.1       eeh 			if (m != NULL)
   1469    1.1       eeh 				m_freem(m);
   1470    1.1       eeh 			break;
   1471    1.1       eeh 		}
   1472    1.1       eeh 
   1473    1.1       eeh 		IFQ_DEQUEUE(&ifp->if_snd, m0);
   1474    1.1       eeh 		if (m != NULL) {
   1475    1.1       eeh 			m_freem(m0);
   1476    1.1       eeh 			m0 = m;
   1477    1.1       eeh 		}
   1478    1.1       eeh 
   1479    1.1       eeh 		/*
   1480    1.1       eeh 		 * WE ARE NOW COMMITTED TO TRANSMITTING THE PACKET.
   1481    1.1       eeh 		 */
   1482    1.1       eeh 
   1483    1.1       eeh 		/* Sync the DMA map. */
   1484    1.1       eeh 		bus_dmamap_sync(sc->sc_dmatag, dmamap, 0, dmamap->dm_mapsize,
   1485    1.1       eeh 		    BUS_DMASYNC_PREWRITE);
   1486    1.1       eeh 
   1487    1.1       eeh 		/*
   1488    1.1       eeh 		 * Initialize the transmit descriptors.
   1489    1.1       eeh 		 */
   1490  1.105       jdc 		firsttx = sc->sc_txnext;
   1491  1.105       jdc 		for (nexttx = firsttx, seg = 0;
   1492    1.1       eeh 		     seg < dmamap->dm_nsegs;
   1493    1.1       eeh 		     seg++, nexttx = GEM_NEXTTX(nexttx)) {
   1494    1.1       eeh 
   1495    1.1       eeh 			/*
   1496    1.1       eeh 			 * If this is the first descriptor we're
   1497    1.1       eeh 			 * enqueueing, set the start of packet flag,
   1498    1.1       eeh 			 * and the checksum stuff if we want the hardware
   1499    1.1       eeh 			 * to do it.
   1500    1.1       eeh 			 */
   1501    1.1       eeh 			flags = dmamap->dm_segs[seg].ds_len & GEM_TD_BUFSIZE;
   1502    1.1       eeh 			if (nexttx == firsttx) {
   1503    1.1       eeh 				flags |= GEM_TD_START_OF_PACKET;
   1504   1.35      heas #ifdef INET
   1505   1.35      heas 				/* h/w checksum */
   1506   1.68       jdc 				if (ifp->if_csum_flags_tx & M_CSUM_TCPv4 &&
   1507   1.68       jdc 				    m0->m_pkthdr.csum_flags & M_CSUM_TCPv4) {
   1508   1.35      heas 					struct ether_header *eh;
   1509   1.35      heas 					uint16_t offset, start;
   1510   1.35      heas 
   1511   1.35      heas 					eh = mtod(m0, struct ether_header *);
   1512   1.35      heas 					switch (ntohs(eh->ether_type)) {
   1513   1.35      heas 					case ETHERTYPE_IP:
   1514   1.35      heas 						start = ETHER_HDR_LEN;
   1515   1.35      heas 						break;
   1516   1.35      heas 					case ETHERTYPE_VLAN:
   1517   1.35      heas 						start = ETHER_HDR_LEN +
   1518   1.35      heas 							ETHER_VLAN_ENCAP_LEN;
   1519   1.37     perry 						break;
   1520   1.35      heas 					default:
   1521   1.37     perry 						/* unsupported, drop it */
   1522  1.124   msaitoh 						bus_dmamap_unload(sc->sc_dmatag,
   1523  1.124   msaitoh 							dmamap);
   1524  1.124   msaitoh 						m_freem(m0);
   1525  1.124   msaitoh 						goto next;
   1526   1.35      heas 					}
   1527   1.36   thorpej 					start += M_CSUM_DATA_IPv4_IPHL(m0->m_pkthdr.csum_data);
   1528   1.36   thorpej 					offset = M_CSUM_DATA_IPv4_OFFSET(m0->m_pkthdr.csum_data) + start;
   1529   1.35      heas 					flags |= (start <<
   1530   1.35      heas 						  GEM_TD_CXSUM_STARTSHFT) |
   1531   1.35      heas 						 (offset <<
   1532   1.35      heas 						  GEM_TD_CXSUM_STUFFSHFT) |
   1533   1.35      heas 						 GEM_TD_CXSUM_ENABLE;
   1534   1.35      heas 				}
   1535   1.35      heas #endif
   1536  1.124   msaitoh 				if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) {
   1537  1.124   msaitoh 					sc->sc_txwin = 0;
   1538  1.124   msaitoh 					flags |= GEM_TD_INTERRUPT_ME;
   1539  1.124   msaitoh 				}
   1540    1.1       eeh 			}
   1541  1.124   msaitoh 			sc->sc_txdescs[nexttx].gd_addr =
   1542  1.124   msaitoh 			    GEM_DMA_WRITE(sc, dmamap->dm_segs[seg].ds_addr);
   1543    1.1       eeh 			if (seg == dmamap->dm_nsegs - 1) {
   1544    1.1       eeh 				flags |= GEM_TD_END_OF_PACKET;
   1545   1.40    bouyer 			} else {
   1546   1.40    bouyer 				/* last flag set outside of loop */
   1547   1.40    bouyer 				sc->sc_txdescs[nexttx].gd_flags =
   1548   1.40    bouyer 					GEM_DMA_WRITE(sc, flags);
   1549    1.1       eeh 			}
   1550    1.1       eeh 			lasttx = nexttx;
   1551    1.1       eeh 		}
   1552   1.40    bouyer 		if (m0->m_pkthdr.len < ETHER_MIN_TX) {
   1553   1.40    bouyer 			/* add padding buffer at end of chain */
   1554   1.40    bouyer 			flags &= ~GEM_TD_END_OF_PACKET;
   1555   1.40    bouyer 			sc->sc_txdescs[lasttx].gd_flags =
   1556   1.40    bouyer 			    GEM_DMA_WRITE(sc, flags);
   1557   1.40    bouyer 
   1558   1.40    bouyer 			sc->sc_txdescs[nexttx].gd_addr =
   1559   1.40    bouyer 			    GEM_DMA_WRITE(sc,
   1560   1.40    bouyer 			    sc->sc_nulldmamap->dm_segs[0].ds_addr);
   1561   1.40    bouyer 			flags = ((ETHER_MIN_TX - m0->m_pkthdr.len) &
   1562   1.40    bouyer 			    GEM_TD_BUFSIZE) | GEM_TD_END_OF_PACKET;
   1563   1.40    bouyer 			lasttx = nexttx;
   1564   1.40    bouyer 			nexttx = GEM_NEXTTX(nexttx);
   1565   1.40    bouyer 			seg++;
   1566   1.40    bouyer 		}
   1567   1.40    bouyer 		sc->sc_txdescs[lasttx].gd_flags = GEM_DMA_WRITE(sc, flags);
   1568   1.30  christos 
   1569   1.30  christos 		KASSERT(lasttx != -1);
   1570    1.1       eeh 
   1571   1.40    bouyer 		/*
   1572   1.40    bouyer 		 * Store a pointer to the packet so we can free it later,
   1573   1.40    bouyer 		 * and remember what txdirty will be once the packet is
   1574   1.40    bouyer 		 * done.
   1575   1.40    bouyer 		 */
   1576   1.40    bouyer 		txs->txs_mbuf = m0;
   1577   1.40    bouyer 		txs->txs_firstdesc = sc->sc_txnext;
   1578   1.40    bouyer 		txs->txs_lastdesc = lasttx;
   1579   1.40    bouyer 		txs->txs_ndescs = seg;
   1580   1.40    bouyer 
   1581    1.1       eeh #ifdef GEM_DEBUG
   1582    1.1       eeh 		if (ifp->if_flags & IFF_DEBUG) {
   1583    1.1       eeh 			printf("     gem_start %p transmit chain:\n", txs);
   1584   1.67    dyoung 			gem_txsoft_print(sc, txs->txs_firstdesc,
   1585   1.67    dyoung 			    txs->txs_lastdesc);
   1586    1.1       eeh 		}
   1587    1.1       eeh #endif
   1588    1.1       eeh 
   1589    1.1       eeh 		/* Sync the descriptors we're using. */
   1590   1.65    dyoung 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
   1591  1.117   msaitoh 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
   1592    1.1       eeh 
   1593    1.1       eeh 		/* Advance the tx pointer. */
   1594   1.40    bouyer 		sc->sc_txfree -= txs->txs_ndescs;
   1595    1.1       eeh 		sc->sc_txnext = nexttx;
   1596    1.1       eeh 
   1597   1.21     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q);
   1598    1.1       eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q);
   1599    1.1       eeh 
   1600    1.1       eeh 		/*
   1601    1.1       eeh 		 * Pass the packet to any BPF listeners.
   1602    1.1       eeh 		 */
   1603  1.110   msaitoh 		bpf_mtap(ifp, m0, BPF_D_OUT);
   1604    1.1       eeh 	}
   1605    1.1       eeh 
   1606    1.1       eeh 	if (sc->sc_txfree != ofree) {
   1607    1.1       eeh 		DPRINTF(sc, ("%s: packets enqueued, IC on %d, OWN on %d\n",
   1608  1.105       jdc 		    device_xname(sc->sc_dev), lasttx, otxnext));
   1609    1.1       eeh 		/*
   1610   1.31      heas 		 * The entire packet chain is set up.
   1611    1.1       eeh 		 * Kick the transmitter.
   1612    1.1       eeh 		 */
   1613    1.1       eeh 		DPRINTF(sc, ("%s: gem_start: kicking tx %d\n",
   1614   1.85    dyoung 			device_xname(sc->sc_dev), nexttx));
   1615   1.50    martin 		bus_space_write_4(sc->sc_bustag, sc->sc_h1, GEM_TX_KICK,
   1616    1.1       eeh 			sc->sc_txnext);
   1617    1.1       eeh 
   1618    1.1       eeh 		/* Set a watchdog timer in case the chip flakes out. */
   1619    1.1       eeh 		ifp->if_timer = 5;
   1620    1.1       eeh 		DPRINTF(sc, ("%s: gem_start: watchdog %d\n",
   1621   1.85    dyoung 			device_xname(sc->sc_dev), ifp->if_timer));
   1622    1.1       eeh 	}
   1623    1.1       eeh }
   1624    1.1       eeh 
   1625    1.1       eeh /*
   1626    1.1       eeh  * Transmit interrupt.
   1627    1.1       eeh  */
   1628    1.1       eeh int
   1629   1.81       dsl gem_tint(struct gem_softc *sc)
   1630    1.1       eeh {
   1631    1.1       eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1632    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   1633   1.50    martin 	bus_space_handle_t mac = sc->sc_h1;
   1634    1.1       eeh 	struct gem_txsoft *txs;
   1635    1.1       eeh 	int txlast;
   1636   1.14      matt 	int progress = 0;
   1637  1.112   msaitoh 	uint32_t v;
   1638    1.1       eeh 
   1639  1.125   thorpej 	net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
   1640  1.125   thorpej 
   1641   1.85    dyoung 	DPRINTF(sc, ("%s: gem_tint\n", device_xname(sc->sc_dev)));
   1642    1.1       eeh 
   1643   1.71       jdc 	/* Unload collision counters ... */
   1644   1.71       jdc 	v = bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) +
   1645   1.71       jdc 	    bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT);
   1646  1.125   thorpej 	if_statadd_ref(nsr, if_collisions, v +
   1647   1.71       jdc 	    bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) +
   1648  1.125   thorpej 	    bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT));
   1649  1.125   thorpej 	if_statadd_ref(nsr, if_oerrors, v);
   1650    1.1       eeh 
   1651   1.71       jdc 	/* ... then clear the hardware counters. */
   1652    1.1       eeh 	bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0);
   1653    1.1       eeh 	bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0);
   1654    1.1       eeh 	bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0);
   1655    1.1       eeh 	bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0);
   1656    1.1       eeh 
   1657    1.1       eeh 	/*
   1658    1.1       eeh 	 * Go through our Tx list and free mbufs for those
   1659    1.1       eeh 	 * frames that have been transmitted.
   1660    1.1       eeh 	 */
   1661    1.1       eeh 	while ((txs = SIMPLEQ_FIRST(&sc->sc_txdirtyq)) != NULL) {
   1662    1.1       eeh 		/*
   1663   1.68       jdc 		 * In theory, we could harvest some descriptors before
   1664    1.1       eeh 		 * the ring is empty, but that's a bit complicated.
   1665    1.1       eeh 		 *
   1666    1.1       eeh 		 * GEM_TX_COMPLETION points to the last descriptor
   1667    1.1       eeh 		 * processed +1.
   1668   1.62    dyoung 		 *
   1669   1.62    dyoung 		 * Let's assume that the NIC writes back to the Tx
   1670   1.62    dyoung 		 * descriptors before it updates the completion
   1671   1.62    dyoung 		 * register.  If the NIC has posted writes to the
   1672   1.62    dyoung 		 * Tx descriptors, PCI ordering requires that the
   1673   1.62    dyoung 		 * posted writes flush to RAM before the register-read
   1674   1.62    dyoung 		 * finishes.  So let's read the completion register,
   1675   1.62    dyoung 		 * before syncing the descriptors, so that we
   1676   1.62    dyoung 		 * examine Tx descriptors that are at least as
   1677   1.62    dyoung 		 * current as the completion register.
   1678    1.1       eeh 		 */
   1679    1.1       eeh 		txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION);
   1680    1.1       eeh 		DPRINTF(sc,
   1681    1.1       eeh 			("gem_tint: txs->txs_lastdesc = %d, txlast = %d\n",
   1682    1.1       eeh 				txs->txs_lastdesc, txlast));
   1683    1.1       eeh 		if (txs->txs_firstdesc <= txs->txs_lastdesc) {
   1684   1.63    dyoung 			if (txlast >= txs->txs_firstdesc &&
   1685   1.63    dyoung 			    txlast <= txs->txs_lastdesc)
   1686    1.1       eeh 				break;
   1687   1.63    dyoung 		} else if (txlast >= txs->txs_firstdesc ||
   1688   1.68       jdc 			   txlast <= txs->txs_lastdesc)
   1689   1.63    dyoung 			break;
   1690    1.1       eeh 
   1691   1.66    dyoung 		GEM_CDTXSYNC(sc, txs->txs_firstdesc, txs->txs_ndescs,
   1692  1.117   msaitoh 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1693   1.62    dyoung 
   1694   1.62    dyoung #ifdef GEM_DEBUG	/* XXX DMA synchronization? */
   1695   1.62    dyoung 		if (ifp->if_flags & IFF_DEBUG) {
   1696   1.62    dyoung 			printf("    txsoft %p transmit chain:\n", txs);
   1697   1.67    dyoung 			gem_txsoft_print(sc, txs->txs_firstdesc,
   1698   1.67    dyoung 			    txs->txs_lastdesc);
   1699   1.62    dyoung 		}
   1700   1.62    dyoung #endif
   1701   1.62    dyoung 
   1702   1.62    dyoung 
   1703    1.1       eeh 		DPRINTF(sc, ("gem_tint: releasing a desc\n"));
   1704   1.21     lukem 		SIMPLEQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q);
   1705    1.1       eeh 
   1706    1.1       eeh 		sc->sc_txfree += txs->txs_ndescs;
   1707    1.1       eeh 
   1708    1.1       eeh 		bus_dmamap_sync(sc->sc_dmatag, txs->txs_dmamap,
   1709    1.1       eeh 		    0, txs->txs_dmamap->dm_mapsize,
   1710    1.1       eeh 		    BUS_DMASYNC_POSTWRITE);
   1711    1.1       eeh 		bus_dmamap_unload(sc->sc_dmatag, txs->txs_dmamap);
   1712   1.68       jdc 		if (txs->txs_mbuf != NULL) {
   1713   1.68       jdc 			m_freem(txs->txs_mbuf);
   1714   1.68       jdc 			txs->txs_mbuf = NULL;
   1715   1.68       jdc 		}
   1716    1.1       eeh 
   1717    1.1       eeh 		SIMPLEQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q);
   1718    1.1       eeh 
   1719  1.125   thorpej 		if_statinc_ref(nsr, if_opackets);
   1720   1.14      matt 		progress = 1;
   1721    1.1       eeh 	}
   1722    1.1       eeh 
   1723  1.125   thorpej 	IF_STAT_PUTREF(ifp);
   1724  1.125   thorpej 
   1725   1.28       chs #if 0
   1726    1.1       eeh 	DPRINTF(sc, ("gem_tint: GEM_TX_STATE_MACHINE %x "
   1727   1.55    dyoung 		"GEM_TX_DATA_PTR %" PRIx64 "GEM_TX_COMPLETION %" PRIx32 "\n",
   1728   1.50    martin 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_STATE_MACHINE),
   1729   1.55    dyoung 		((uint64_t)bus_space_read_4(sc->sc_bustag, sc->sc_h1,
   1730    1.4   thorpej 			GEM_TX_DATA_PTR_HI) << 32) |
   1731   1.50    martin 			     bus_space_read_4(sc->sc_bustag, sc->sc_h1,
   1732    1.4   thorpej 			GEM_TX_DATA_PTR_LO),
   1733   1.50    martin 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_TX_COMPLETION)));
   1734   1.28       chs #endif
   1735    1.1       eeh 
   1736   1.14      matt 	if (progress) {
   1737   1.14      matt 		if (sc->sc_txfree == GEM_NTXDESC - 1)
   1738   1.14      matt 			sc->sc_txwin = 0;
   1739   1.14      matt 
   1740   1.68       jdc 		ifp->if_timer = SIMPLEQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5;
   1741  1.108     ozaki 		if_schedule_deferred_start(ifp);
   1742   1.14      matt 	}
   1743    1.1       eeh 	DPRINTF(sc, ("%s: gem_tint: watchdog %d\n",
   1744   1.85    dyoung 		device_xname(sc->sc_dev), ifp->if_timer));
   1745    1.1       eeh 
   1746    1.1       eeh 	return (1);
   1747    1.1       eeh }
   1748    1.1       eeh 
   1749    1.1       eeh /*
   1750    1.1       eeh  * Receive interrupt.
   1751    1.1       eeh  */
   1752    1.1       eeh int
   1753   1.81       dsl gem_rint(struct gem_softc *sc)
   1754    1.1       eeh {
   1755    1.1       eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   1756    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   1757   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
   1758    1.1       eeh 	struct gem_rxsoft *rxs;
   1759    1.1       eeh 	struct mbuf *m;
   1760  1.112   msaitoh 	uint64_t rxstat;
   1761  1.112   msaitoh 	uint32_t rxcomp;
   1762   1.18      matt 	int i, len, progress = 0;
   1763    1.1       eeh 
   1764   1.85    dyoung 	DPRINTF(sc, ("%s: gem_rint\n", device_xname(sc->sc_dev)));
   1765   1.18      matt 
   1766   1.18      matt 	/*
   1767   1.68       jdc 	 * Ignore spurious interrupt that sometimes occurs before
   1768   1.68       jdc 	 * we are set up when we network boot.
   1769   1.68       jdc 	 */
   1770   1.68       jdc 	if (!sc->sc_meminited)
   1771   1.68       jdc 		return 1;
   1772   1.68       jdc 
   1773   1.68       jdc 	/*
   1774   1.18      matt 	 * Read the completion register once.  This limits
   1775   1.18      matt 	 * how long the following loop can execute.
   1776   1.18      matt 	 */
   1777   1.18      matt 	rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION);
   1778   1.18      matt 
   1779    1.1       eeh 	/*
   1780   1.68       jdc 	 * XXX Read the lastrx only once at the top for speed.
   1781    1.1       eeh 	 */
   1782    1.1       eeh 	DPRINTF(sc, ("gem_rint: sc->rxptr %d, complete %d\n",
   1783   1.18      matt 		sc->sc_rxptr, rxcomp));
   1784   1.18      matt 
   1785   1.18      matt 	/*
   1786   1.18      matt 	 * Go into the loop at least once.
   1787   1.18      matt 	 */
   1788   1.18      matt 	for (i = sc->sc_rxptr; i == sc->sc_rxptr || i != rxcomp;
   1789    1.1       eeh 	     i = GEM_NEXTRX(i)) {
   1790    1.1       eeh 		rxs = &sc->sc_rxsoft[i];
   1791    1.1       eeh 
   1792    1.1       eeh 		GEM_CDRXSYNC(sc, i,
   1793  1.117   msaitoh 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
   1794    1.1       eeh 
   1795    1.2       eeh 		rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags);
   1796    1.1       eeh 
   1797    1.1       eeh 		if (rxstat & GEM_RD_OWN) {
   1798   1.56    dyoung 			GEM_CDRXSYNC(sc, i, BUS_DMASYNC_PREREAD);
   1799    1.1       eeh 			/*
   1800    1.1       eeh 			 * We have processed all of the receive buffers.
   1801    1.1       eeh 			 */
   1802    1.1       eeh 			break;
   1803    1.1       eeh 		}
   1804    1.1       eeh 
   1805   1.18      matt 		progress++;
   1806   1.18      matt 
   1807    1.1       eeh 		if (rxstat & GEM_RD_BAD_CRC) {
   1808  1.125   thorpej 			if_statinc(ifp, if_ierrors);
   1809  1.133       jdc 			DPRINTF(sc, ("%s: receive error: CRC error\n",
   1810  1.133       jdc 			    device_xname(sc->sc_dev)));
   1811    1.1       eeh 			GEM_INIT_RXDESC(sc, i);
   1812    1.1       eeh 			continue;
   1813    1.1       eeh 		}
   1814    1.1       eeh 
   1815    1.1       eeh 		bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1816    1.1       eeh 		    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_POSTREAD);
   1817    1.1       eeh #ifdef GEM_DEBUG
   1818    1.1       eeh 		if (ifp->if_flags & IFF_DEBUG) {
   1819    1.1       eeh 			printf("    rxsoft %p descriptor %d: ", rxs, i);
   1820    1.1       eeh 			printf("gd_flags: 0x%016llx\t", (long long)
   1821    1.2       eeh 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags));
   1822    1.1       eeh 			printf("gd_addr: 0x%016llx\n", (long long)
   1823    1.2       eeh 				GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr));
   1824    1.1       eeh 		}
   1825    1.1       eeh #endif
   1826    1.1       eeh 
   1827   1.35      heas 		/* No errors; receive the packet. */
   1828   1.35      heas 		len = GEM_RD_BUFLEN(rxstat);
   1829    1.1       eeh 
   1830    1.1       eeh 		/*
   1831    1.1       eeh 		 * Allocate a new mbuf cluster.  If that fails, we are
   1832    1.1       eeh 		 * out of memory, and must drop the packet and recycle
   1833    1.1       eeh 		 * the buffer that's already attached to this descriptor.
   1834    1.1       eeh 		 */
   1835    1.1       eeh 		m = rxs->rxs_mbuf;
   1836    1.1       eeh 		if (gem_add_rxbuf(sc, i) != 0) {
   1837   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxnobuf);
   1838  1.125   thorpej 			if_statinc(ifp, if_ierrors);
   1839   1.99       jdc 			aprint_error_dev(sc->sc_dev,
   1840   1.99       jdc 			    "receive error: RX no buffer space\n");
   1841    1.1       eeh 			GEM_INIT_RXDESC(sc, i);
   1842    1.1       eeh 			bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   1843    1.1       eeh 			    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   1844    1.1       eeh 			continue;
   1845    1.1       eeh 		}
   1846    1.1       eeh 		m->m_data += 2; /* We're already off by two */
   1847    1.1       eeh 
   1848  1.106     ozaki 		m_set_rcvif(m, ifp);
   1849    1.1       eeh 		m->m_pkthdr.len = m->m_len = len;
   1850    1.1       eeh 
   1851   1.35      heas #ifdef INET
   1852   1.35      heas 		/* hardware checksum */
   1853   1.68       jdc 		if (ifp->if_csum_flags_rx & M_CSUM_TCPv4) {
   1854   1.35      heas 			struct ether_header *eh;
   1855   1.35      heas 			struct ip *ip;
   1856   1.35      heas 			int32_t hlen, pktlen;
   1857   1.35      heas 
   1858   1.35      heas 			if (sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) {
   1859   1.35      heas 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN -
   1860   1.35      heas 					 ETHER_VLAN_ENCAP_LEN;
   1861   1.72       jdc 				eh = (struct ether_header *) (mtod(m, char *) +
   1862   1.72       jdc 					ETHER_VLAN_ENCAP_LEN);
   1863   1.35      heas 			} else {
   1864   1.35      heas 				pktlen = m->m_pkthdr.len - ETHER_HDR_LEN;
   1865   1.35      heas 				eh = mtod(m, struct ether_header *);
   1866   1.35      heas 			}
   1867   1.35      heas 			if (ntohs(eh->ether_type) != ETHERTYPE_IP)
   1868   1.35      heas 				goto swcsum;
   1869   1.54  christos 			ip = (struct ip *) ((char *)eh + ETHER_HDR_LEN);
   1870   1.35      heas 
   1871   1.35      heas 			/* IPv4 only */
   1872   1.35      heas 			if (ip->ip_v != IPVERSION)
   1873   1.35      heas 				goto swcsum;
   1874   1.35      heas 
   1875   1.35      heas 			hlen = ip->ip_hl << 2;
   1876   1.35      heas 			if (hlen < sizeof(struct ip))
   1877   1.35      heas 				goto swcsum;
   1878   1.35      heas 
   1879   1.38      heas 			/*
   1880   1.38      heas 			 * bail if too short, has random trailing garbage,
   1881   1.38      heas 			 * truncated, fragment, or has ethernet pad.
   1882   1.38      heas 			 */
   1883   1.35      heas 			if ((ntohs(ip->ip_len) < hlen) ||
   1884   1.38      heas 			    (ntohs(ip->ip_len) != pktlen) ||
   1885   1.35      heas 			    (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)))
   1886   1.35      heas 				goto swcsum;
   1887   1.35      heas 
   1888   1.35      heas 			switch (ip->ip_p) {
   1889   1.35      heas 			case IPPROTO_TCP:
   1890   1.35      heas 				if (! (ifp->if_csum_flags_rx & M_CSUM_TCPv4))
   1891   1.35      heas 					goto swcsum;
   1892   1.35      heas 				if (pktlen < (hlen + sizeof(struct tcphdr)))
   1893   1.35      heas 					goto swcsum;
   1894   1.35      heas 				m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
   1895   1.35      heas 				break;
   1896   1.35      heas 			case IPPROTO_UDP:
   1897   1.68       jdc 				/* FALLTHROUGH */
   1898   1.35      heas 			default:
   1899   1.35      heas 				goto swcsum;
   1900   1.35      heas 			}
   1901   1.35      heas 
   1902   1.35      heas 			/* the uncomplemented sum is expected */
   1903   1.35      heas 			m->m_pkthdr.csum_data = (~rxstat) & GEM_RD_CHECKSUM;
   1904   1.35      heas 
   1905   1.35      heas 			/* if the pkt had ip options, we have to deduct them */
   1906   1.35      heas 			if (hlen > sizeof(struct ip)) {
   1907   1.35      heas 				uint16_t *opts;
   1908   1.35      heas 				uint32_t optsum, temp;
   1909   1.35      heas 
   1910   1.35      heas 				optsum = 0;
   1911   1.35      heas 				temp = hlen - sizeof(struct ip);
   1912   1.54  christos 				opts = (uint16_t *) ((char *) ip +
   1913   1.35      heas 					sizeof(struct ip));
   1914   1.35      heas 
   1915   1.35      heas 				while (temp > 1) {
   1916   1.35      heas 					optsum += ntohs(*opts++);
   1917   1.35      heas 					temp -= 2;
   1918   1.35      heas 				}
   1919   1.35      heas 				while (optsum >> 16)
   1920   1.35      heas 					optsum = (optsum >> 16) +
   1921   1.35      heas 						 (optsum & 0xffff);
   1922   1.35      heas 
   1923   1.83   tsutsui 				/* Deduct ip opts sum from hwsum. */
   1924   1.83   tsutsui 				m->m_pkthdr.csum_data += (uint16_t)~optsum;
   1925   1.35      heas 
   1926   1.35      heas 				while (m->m_pkthdr.csum_data >> 16)
   1927   1.35      heas 					m->m_pkthdr.csum_data =
   1928   1.35      heas 						(m->m_pkthdr.csum_data >> 16) +
   1929   1.35      heas 						(m->m_pkthdr.csum_data &
   1930   1.35      heas 						 0xffff);
   1931   1.35      heas 			}
   1932   1.35      heas 
   1933   1.35      heas 			m->m_pkthdr.csum_flags |= M_CSUM_DATA |
   1934   1.35      heas 						  M_CSUM_NO_PSEUDOHDR;
   1935   1.35      heas 		} else
   1936   1.35      heas swcsum:
   1937   1.35      heas 			m->m_pkthdr.csum_flags = 0;
   1938   1.35      heas #endif
   1939    1.1       eeh 		/* Pass it on. */
   1940  1.104     ozaki 		if_percpuq_enqueue(ifp->if_percpuq, m);
   1941    1.1       eeh 	}
   1942    1.1       eeh 
   1943   1.18      matt 	if (progress) {
   1944   1.18      matt 		/* Update the receive pointer. */
   1945   1.18      matt 		if (i == sc->sc_rxptr) {
   1946   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxfull);
   1947   1.19      matt #ifdef GEM_DEBUG
   1948   1.28       chs 			if (ifp->if_flags & IFF_DEBUG)
   1949   1.19      matt 				printf("%s: rint: ring wrap\n",
   1950   1.85    dyoung 				    device_xname(sc->sc_dev));
   1951   1.19      matt #endif
   1952   1.18      matt 		}
   1953   1.18      matt 		sc->sc_rxptr = i;
   1954   1.18      matt 		bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i));
   1955   1.18      matt 	}
   1956   1.19      matt #ifdef GEM_COUNTERS
   1957   1.18      matt 	if (progress <= 4) {
   1958   1.19      matt 		GEM_COUNTER_INCR(sc, sc_ev_rxhist[progress]);
   1959   1.28       chs 	} else if (progress < 32) {
   1960   1.18      matt 		if (progress < 16)
   1961   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[5]);
   1962   1.18      matt 		else
   1963   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[6]);
   1964   1.31      heas 
   1965   1.18      matt 	} else {
   1966   1.18      matt 		if (progress < 64)
   1967   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[7]);
   1968   1.18      matt 		else
   1969   1.19      matt 			GEM_COUNTER_INCR(sc, sc_ev_rxhist[8]);
   1970   1.18      matt 	}
   1971   1.19      matt #endif
   1972    1.1       eeh 
   1973    1.1       eeh 	DPRINTF(sc, ("gem_rint: done sc->rxptr %d, complete %d\n",
   1974    1.1       eeh 		sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)));
   1975    1.1       eeh 
   1976   1.71       jdc 	/* Read error counters ... */
   1977  1.125   thorpej 	if_statadd(ifp, if_ierrors,
   1978   1.71       jdc 	    bus_space_read_4(t, h, GEM_MAC_RX_LEN_ERR_CNT) +
   1979   1.71       jdc 	    bus_space_read_4(t, h, GEM_MAC_RX_ALIGN_ERR) +
   1980   1.71       jdc 	    bus_space_read_4(t, h, GEM_MAC_RX_CRC_ERR_CNT) +
   1981  1.125   thorpej 	    bus_space_read_4(t, h, GEM_MAC_RX_CODE_VIOL));
   1982   1.71       jdc 
   1983   1.71       jdc 	/* ... then clear the hardware counters. */
   1984   1.71       jdc 	bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0);
   1985   1.71       jdc 	bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0);
   1986   1.71       jdc 	bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0);
   1987   1.71       jdc 	bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0);
   1988   1.71       jdc 
   1989    1.1       eeh 	return (1);
   1990    1.1       eeh }
   1991    1.1       eeh 
   1992    1.1       eeh 
   1993    1.1       eeh /*
   1994    1.1       eeh  * gem_add_rxbuf:
   1995    1.1       eeh  *
   1996    1.1       eeh  *	Add a receive buffer to the indicated descriptor.
   1997    1.1       eeh  */
   1998    1.1       eeh int
   1999    1.1       eeh gem_add_rxbuf(struct gem_softc *sc, int idx)
   2000    1.1       eeh {
   2001    1.1       eeh 	struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx];
   2002    1.1       eeh 	struct mbuf *m;
   2003    1.1       eeh 	int error;
   2004    1.1       eeh 
   2005    1.1       eeh 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   2006    1.1       eeh 	if (m == NULL)
   2007    1.1       eeh 		return (ENOBUFS);
   2008    1.1       eeh 
   2009   1.26      matt 	MCLAIM(m, &sc->sc_ethercom.ec_rx_mowner);
   2010    1.1       eeh 	MCLGET(m, M_DONTWAIT);
   2011    1.1       eeh 	if ((m->m_flags & M_EXT) == 0) {
   2012    1.1       eeh 		m_freem(m);
   2013    1.1       eeh 		return (ENOBUFS);
   2014    1.1       eeh 	}
   2015    1.1       eeh 
   2016    1.1       eeh #ifdef GEM_DEBUG
   2017   1.27       wiz /* bzero the packet to check DMA */
   2018    1.1       eeh 	memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size);
   2019    1.1       eeh #endif
   2020    1.1       eeh 
   2021    1.1       eeh 	if (rxs->rxs_mbuf != NULL)
   2022    1.1       eeh 		bus_dmamap_unload(sc->sc_dmatag, rxs->rxs_dmamap);
   2023    1.1       eeh 
   2024    1.1       eeh 	rxs->rxs_mbuf = m;
   2025    1.1       eeh 
   2026    1.1       eeh 	error = bus_dmamap_load(sc->sc_dmatag, rxs->rxs_dmamap,
   2027    1.1       eeh 	    m->m_ext.ext_buf, m->m_ext.ext_size, NULL,
   2028  1.117   msaitoh 	    BUS_DMA_READ | BUS_DMA_NOWAIT);
   2029    1.1       eeh 	if (error) {
   2030   1.85    dyoung 		aprint_error_dev(sc->sc_dev,
   2031   1.85    dyoung 		    "can't load rx DMA map %d, error = %d\n", idx, error);
   2032    1.1       eeh 		panic("gem_add_rxbuf");	/* XXX */
   2033    1.1       eeh 	}
   2034    1.1       eeh 
   2035    1.1       eeh 	bus_dmamap_sync(sc->sc_dmatag, rxs->rxs_dmamap, 0,
   2036    1.1       eeh 	    rxs->rxs_dmamap->dm_mapsize, BUS_DMASYNC_PREREAD);
   2037    1.1       eeh 
   2038    1.1       eeh 	GEM_INIT_RXDESC(sc, idx);
   2039    1.1       eeh 
   2040    1.1       eeh 	return (0);
   2041    1.1       eeh }
   2042    1.1       eeh 
   2043    1.1       eeh 
   2044    1.1       eeh int
   2045   1.68       jdc gem_eint(struct gem_softc *sc, u_int status)
   2046    1.1       eeh {
   2047    1.1       eeh 	char bits[128];
   2048  1.112   msaitoh 	uint32_t r, v;
   2049    1.1       eeh 
   2050    1.1       eeh 	if ((status & GEM_INTR_MIF) != 0) {
   2051   1.85    dyoung 		printf("%s: XXXlink status changed\n", device_xname(sc->sc_dev));
   2052    1.1       eeh 		return (1);
   2053    1.1       eeh 	}
   2054    1.1       eeh 
   2055   1.68       jdc 	if ((status & GEM_INTR_RX_TAG_ERR) != 0) {
   2056   1.68       jdc 		gem_reset_rxdma(sc);
   2057   1.68       jdc 		return (1);
   2058   1.68       jdc 	}
   2059   1.68       jdc 
   2060   1.68       jdc 	if (status & GEM_INTR_BERR) {
   2061   1.78       jdc 		if (sc->sc_flags & GEM_PCI)
   2062   1.78       jdc 			r = GEM_ERROR_STATUS;
   2063   1.78       jdc 		else
   2064   1.78       jdc 			r = GEM_SBUS_ERROR_STATUS;
   2065   1.78       jdc 		bus_space_read_4(sc->sc_bustag, sc->sc_h2, r);
   2066   1.78       jdc 		v = bus_space_read_4(sc->sc_bustag, sc->sc_h2, r);
   2067   1.85    dyoung 		aprint_error_dev(sc->sc_dev, "bus error interrupt: 0x%02x\n",
   2068   1.76    cegger 		    v);
   2069   1.68       jdc 		return (1);
   2070   1.68       jdc 	}
   2071   1.80  christos 	snprintb(bits, sizeof(bits), GEM_INTR_BITS, status);
   2072   1.85    dyoung 	printf("%s: status=%s\n", device_xname(sc->sc_dev), bits);
   2073  1.117   msaitoh 
   2074    1.1       eeh 	return (1);
   2075    1.1       eeh }
   2076    1.1       eeh 
   2077    1.1       eeh 
   2078   1.68       jdc /*
   2079   1.68       jdc  * PCS interrupts.
   2080   1.68       jdc  * We should receive these when the link status changes, but sometimes
   2081   1.68       jdc  * we don't receive them for link up.  We compensate for this in the
   2082   1.68       jdc  * gem_tick() callout.
   2083   1.68       jdc  */
   2084   1.68       jdc int
   2085   1.68       jdc gem_pint(struct gem_softc *sc)
   2086   1.68       jdc {
   2087   1.68       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2088   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
   2089   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
   2090  1.112   msaitoh 	uint32_t v, v2;
   2091   1.68       jdc 
   2092   1.68       jdc 	/*
   2093   1.68       jdc 	 * Clear the PCS interrupt from GEM_STATUS.  The PCS register is
   2094   1.68       jdc 	 * latched, so we have to read it twice.  There is only one bit in
   2095   1.68       jdc 	 * use, so the value is meaningless.
   2096   1.68       jdc 	 */
   2097   1.68       jdc 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
   2098   1.68       jdc 	bus_space_read_4(t, h, GEM_MII_INTERRUP_STATUS);
   2099   1.68       jdc 
   2100   1.68       jdc 	if ((ifp->if_flags & IFF_UP) == 0)
   2101   1.68       jdc 		return 1;
   2102   1.68       jdc 
   2103   1.68       jdc 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) == 0)
   2104   1.68       jdc 		return 1;
   2105   1.68       jdc 
   2106   1.68       jdc 	v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2107   1.68       jdc 	/* If we see remote fault, our link partner is probably going away */
   2108   1.68       jdc 	if ((v & GEM_MII_STATUS_REM_FLT) != 0) {
   2109   1.68       jdc 		gem_bitwait(sc, h, GEM_MII_STATUS, GEM_MII_STATUS_REM_FLT, 0);
   2110   1.68       jdc 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2111   1.68       jdc 	/* Otherwise, we may need to wait after auto-negotiation completes */
   2112   1.68       jdc 	} else if ((v & (GEM_MII_STATUS_LINK_STS | GEM_MII_STATUS_ANEG_CPT)) ==
   2113   1.68       jdc 	    GEM_MII_STATUS_ANEG_CPT) {
   2114   1.68       jdc 		gem_bitwait(sc, h, GEM_MII_STATUS, 0, GEM_MII_STATUS_LINK_STS);
   2115   1.68       jdc 		v = bus_space_read_4(t, h, GEM_MII_STATUS);
   2116   1.68       jdc 	}
   2117   1.68       jdc 	if ((v & GEM_MII_STATUS_LINK_STS) != 0) {
   2118   1.68       jdc 		if (sc->sc_flags & GEM_LINK) {
   2119   1.68       jdc 			return 1;
   2120   1.68       jdc 		}
   2121   1.68       jdc 		callout_stop(&sc->sc_tick_ch);
   2122   1.68       jdc 		v = bus_space_read_4(t, h, GEM_MII_ANAR);
   2123   1.68       jdc 		v2 = bus_space_read_4(t, h, GEM_MII_ANLPAR);
   2124   1.68       jdc 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_1000_SX;
   2125   1.68       jdc 		sc->sc_mii.mii_media_status = IFM_AVALID | IFM_ACTIVE;
   2126   1.68       jdc 		v &= v2;
   2127   1.68       jdc 		if (v & GEM_MII_ANEG_FUL_DUPLX) {
   2128   1.68       jdc 			sc->sc_mii.mii_media_active |= IFM_FDX;
   2129   1.68       jdc #ifdef GEM_DEBUG
   2130   1.85    dyoung 			aprint_debug_dev(sc->sc_dev, "link up: full duplex\n");
   2131   1.68       jdc #endif
   2132   1.68       jdc 		} else if (v & GEM_MII_ANEG_HLF_DUPLX) {
   2133   1.68       jdc 			sc->sc_mii.mii_media_active |= IFM_HDX;
   2134   1.68       jdc #ifdef GEM_DEBUG
   2135   1.85    dyoung 			aprint_debug_dev(sc->sc_dev, "link up: half duplex\n");
   2136   1.68       jdc #endif
   2137   1.68       jdc 		} else {
   2138   1.68       jdc #ifdef GEM_DEBUG
   2139   1.85    dyoung 			aprint_debug_dev(sc->sc_dev, "duplex mismatch\n");
   2140   1.68       jdc #endif
   2141   1.68       jdc 		}
   2142   1.68       jdc 		gem_statuschange(sc);
   2143   1.68       jdc 	} else {
   2144   1.68       jdc 		if ((sc->sc_flags & GEM_LINK) == 0) {
   2145   1.68       jdc 			return 1;
   2146   1.68       jdc 		}
   2147   1.68       jdc 		sc->sc_mii.mii_media_active = IFM_ETHER | IFM_NONE;
   2148   1.68       jdc 		sc->sc_mii.mii_media_status = IFM_AVALID;
   2149   1.68       jdc #ifdef GEM_DEBUG
   2150   1.85    dyoung 			aprint_debug_dev(sc->sc_dev, "link down\n");
   2151   1.68       jdc #endif
   2152   1.68       jdc 		gem_statuschange(sc);
   2153   1.68       jdc 
   2154   1.68       jdc 		/* Start the 10 second timer */
   2155  1.128   thorpej 		callout_schedule(&sc->sc_tick_ch, hz * 10);
   2156   1.68       jdc 	}
   2157   1.68       jdc 	return 1;
   2158   1.68       jdc }
   2159   1.68       jdc 
   2160   1.68       jdc 
   2161   1.68       jdc 
   2162    1.1       eeh int
   2163   1.81       dsl gem_intr(void *v)
   2164    1.1       eeh {
   2165   1.85    dyoung 	struct gem_softc *sc = v;
   2166   1.41  christos 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2167    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   2168   1.68       jdc 	bus_space_handle_t h = sc->sc_h1;
   2169  1.112   msaitoh 	uint32_t status;
   2170    1.1       eeh 	int r = 0;
   2171    1.3       eeh #ifdef GEM_DEBUG
   2172    1.1       eeh 	char bits[128];
   2173    1.3       eeh #endif
   2174    1.1       eeh 
   2175   1.68       jdc 	/* XXX We should probably mask out interrupts until we're done */
   2176   1.68       jdc 
   2177   1.19      matt 	sc->sc_ev_intr.ev_count++;
   2178   1.19      matt 
   2179   1.68       jdc 	status = bus_space_read_4(t, h, GEM_STATUS);
   2180   1.80  christos #ifdef GEM_DEBUG
   2181   1.80  christos 	snprintb(bits, sizeof(bits), GEM_INTR_BITS, status);
   2182   1.80  christos #endif
   2183   1.28       chs 	DPRINTF(sc, ("%s: gem_intr: cplt 0x%x status %s\n",
   2184   1.85    dyoung 		device_xname(sc->sc_dev), (status >> 19), bits));
   2185    1.1       eeh 
   2186    1.1       eeh 	if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0)
   2187    1.1       eeh 		r |= gem_eint(sc, status);
   2188    1.1       eeh 
   2189   1.68       jdc 	/* We don't bother with GEM_INTR_TX_DONE */
   2190   1.18      matt 	if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) {
   2191   1.19      matt 		GEM_COUNTER_INCR(sc, sc_ev_txint);
   2192    1.1       eeh 		r |= gem_tint(sc);
   2193   1.18      matt 	}
   2194    1.1       eeh 
   2195   1.18      matt 	if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) {
   2196   1.19      matt 		GEM_COUNTER_INCR(sc, sc_ev_rxint);
   2197    1.1       eeh 		r |= gem_rint(sc);
   2198   1.18      matt 	}
   2199    1.1       eeh 
   2200    1.1       eeh 	/* We should eventually do more than just print out error stats. */
   2201    1.1       eeh 	if (status & GEM_INTR_TX_MAC) {
   2202   1.68       jdc 		int txstat = bus_space_read_4(t, h, GEM_MAC_TX_STATUS);
   2203    1.1       eeh 		if (txstat & ~GEM_MAC_TX_XMIT_DONE)
   2204   1.14      matt 			printf("%s: MAC tx fault, status %x\n",
   2205   1.85    dyoung 			    device_xname(sc->sc_dev), txstat);
   2206   1.41  christos 		if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG))
   2207   1.41  christos 			gem_init(ifp);
   2208    1.1       eeh 	}
   2209    1.1       eeh 	if (status & GEM_INTR_RX_MAC) {
   2210   1.68       jdc 		int rxstat = bus_space_read_4(t, h, GEM_MAC_RX_STATUS);
   2211   1.41  christos 		/*
   2212   1.68       jdc 		 * At least with GEM_SUN_GEM and some GEM_SUN_ERI
   2213   1.68       jdc 		 * revisions GEM_MAC_RX_OVERFLOW happen often due to a
   2214   1.99       jdc 		 * silicon bug so handle them silently.  So if we detect
   2215   1.99       jdc 		 * an RX FIFO overflow, we fire off a timer, and check
   2216   1.99       jdc 		 * whether we're still making progress by looking at the
   2217   1.99       jdc 		 * RX FIFO write and read pointers.
   2218   1.41  christos 		 */
   2219   1.68       jdc 		if (rxstat & GEM_MAC_RX_OVERFLOW) {
   2220  1.125   thorpej 			if_statinc(ifp, if_ierrors);
   2221  1.133       jdc 			GEM_COUNTER_INCR(sc, sc_ev_rxoverflow);
   2222  1.133       jdc #ifdef GEM_DEBUG
   2223   1.99       jdc 			aprint_error_dev(sc->sc_dev,
   2224   1.99       jdc 			    "receive error: RX overflow sc->rxptr %d, complete %d\n", sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION));
   2225  1.133       jdc #endif
   2226   1.99       jdc 			sc->sc_rx_fifo_wr_ptr =
   2227   1.99       jdc 				bus_space_read_4(t, h, GEM_RX_FIFO_WR_PTR);
   2228   1.99       jdc 			sc->sc_rx_fifo_rd_ptr =
   2229   1.99       jdc 				bus_space_read_4(t, h, GEM_RX_FIFO_RD_PTR);
   2230   1.99       jdc 			callout_schedule(&sc->sc_rx_watchdog, 400);
   2231   1.68       jdc 		} else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT))
   2232   1.73       jdc 			printf("%s: MAC rx fault, status 0x%02x\n",
   2233   1.85    dyoung 			    device_xname(sc->sc_dev), rxstat);
   2234    1.1       eeh 	}
   2235   1.68       jdc 	if (status & GEM_INTR_PCS) {
   2236   1.68       jdc 		r |= gem_pint(sc);
   2237   1.68       jdc 	}
   2238   1.68       jdc 
   2239   1.68       jdc /* Do we need to do anything with these?
   2240   1.68       jdc 	if ((status & GEM_MAC_CONTROL_STATUS) != 0) {
   2241   1.68       jdc 		status2 = bus_read_4(sc->sc_res[0], GEM_MAC_CONTROL_STATUS);
   2242   1.68       jdc 		if ((status2 & GEM_MAC_PAUSED) != 0)
   2243   1.85    dyoung 			aprintf_debug_dev(sc->sc_dev, "PAUSE received (%d slots)\n",
   2244   1.76    cegger 			    GEM_MAC_PAUSE_TIME(status2));
   2245   1.68       jdc 		if ((status2 & GEM_MAC_PAUSE) != 0)
   2246   1.85    dyoung 			aprintf_debug_dev(sc->sc_dev, "transited to PAUSE state\n");
   2247   1.68       jdc 		if ((status2 & GEM_MAC_RESUME) != 0)
   2248   1.85    dyoung 			aprintf_debug_dev(sc->sc_dev, "transited to non-PAUSE state\n");
   2249   1.68       jdc 	}
   2250   1.68       jdc 	if ((status & GEM_INTR_MIF) != 0)
   2251   1.85    dyoung 		aprintf_debug_dev(sc->sc_dev, "MIF interrupt\n");
   2252   1.68       jdc */
   2253   1.45      heas 	rnd_add_uint32(&sc->rnd_source, status);
   2254    1.1       eeh 	return (r);
   2255    1.1       eeh }
   2256    1.1       eeh 
   2257   1.99       jdc void
   2258   1.99       jdc gem_rx_watchdog(void *arg)
   2259   1.99       jdc {
   2260   1.99       jdc 	struct gem_softc *sc = arg;
   2261   1.99       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2262   1.99       jdc 	bus_space_tag_t t = sc->sc_bustag;
   2263   1.99       jdc 	bus_space_handle_t h = sc->sc_h1;
   2264  1.112   msaitoh 	uint32_t rx_fifo_wr_ptr;
   2265  1.112   msaitoh 	uint32_t rx_fifo_rd_ptr;
   2266  1.112   msaitoh 	uint32_t state;
   2267   1.99       jdc 
   2268   1.99       jdc 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
   2269   1.99       jdc 		aprint_error_dev(sc->sc_dev, "receiver not running\n");
   2270   1.99       jdc 		return;
   2271   1.99       jdc 	}
   2272   1.99       jdc 
   2273   1.99       jdc 	rx_fifo_wr_ptr = bus_space_read_4(t, h, GEM_RX_FIFO_WR_PTR);
   2274   1.99       jdc 	rx_fifo_rd_ptr = bus_space_read_4(t, h, GEM_RX_FIFO_RD_PTR);
   2275   1.99       jdc 	state = bus_space_read_4(t, h, GEM_MAC_MAC_STATE);
   2276   1.99       jdc 	if ((state & GEM_MAC_STATE_OVERFLOW) == GEM_MAC_STATE_OVERFLOW &&
   2277   1.99       jdc 	    ((rx_fifo_wr_ptr == rx_fifo_rd_ptr) ||
   2278   1.99       jdc 	     ((sc->sc_rx_fifo_wr_ptr == rx_fifo_wr_ptr) &&
   2279   1.99       jdc 	      (sc->sc_rx_fifo_rd_ptr == rx_fifo_rd_ptr))))
   2280   1.99       jdc 	{
   2281   1.99       jdc 		/*
   2282   1.99       jdc 		 * The RX state machine is still in overflow state and
   2283   1.99       jdc 		 * the RX FIFO write and read pointers seem to be
   2284   1.99       jdc 		 * stuck.  Whack the chip over the head to get things
   2285   1.99       jdc 		 * going again.
   2286   1.99       jdc 		 */
   2287   1.99       jdc 		aprint_error_dev(sc->sc_dev,
   2288   1.99       jdc 		    "receiver stuck in overflow, resetting\n");
   2289   1.99       jdc 		gem_init(ifp);
   2290   1.99       jdc 	} else {
   2291  1.133       jdc 		int needreset = 1;
   2292   1.99       jdc 		if ((state & GEM_MAC_STATE_OVERFLOW) != GEM_MAC_STATE_OVERFLOW) {
   2293  1.133       jdc 			DPRINTF(sc,
   2294  1.133       jdc 			    ("%s: rx_watchdog: not in overflow state: 0x%x\n",
   2295  1.133       jdc 			    device_xname(sc->sc_dev), state));
   2296   1.99       jdc 		}
   2297   1.99       jdc 		if (rx_fifo_wr_ptr != rx_fifo_rd_ptr) {
   2298  1.133       jdc 			DPRINTF(sc,
   2299  1.133       jdc 			    ("%s: rx_watchdog: wr & rd ptr different\n",
   2300  1.134  christos 			    device_xname(sc->sc_dev)));
   2301  1.133       jdc 			needreset = 0;
   2302   1.99       jdc 		}
   2303   1.99       jdc 		if (sc->sc_rx_fifo_wr_ptr != rx_fifo_wr_ptr) {
   2304  1.133       jdc 			DPRINTF(sc, ("%s: rx_watchdog: wr pointer != saved\n",
   2305  1.134  christos 			    device_xname(sc->sc_dev)));
   2306  1.133       jdc 			needreset = 0;
   2307   1.99       jdc 		}
   2308   1.99       jdc 		if (sc->sc_rx_fifo_rd_ptr != rx_fifo_rd_ptr) {
   2309  1.133       jdc 			DPRINTF(sc, ("%s: rx_watchdog: rd pointer != saved\n",
   2310  1.134  christos 			    device_xname(sc->sc_dev)));
   2311  1.133       jdc 			needreset = 0;
   2312  1.133       jdc 		}
   2313  1.133       jdc 		if (needreset) {
   2314   1.99       jdc 			aprint_error_dev(sc->sc_dev,
   2315  1.133       jdc 			    "rx_watchdog: resetting anyway\n");
   2316  1.133       jdc 			gem_init(ifp);
   2317   1.99       jdc 		}
   2318   1.99       jdc 	}
   2319   1.99       jdc }
   2320    1.1       eeh 
   2321    1.1       eeh void
   2322   1.81       dsl gem_watchdog(struct ifnet *ifp)
   2323    1.1       eeh {
   2324    1.1       eeh 	struct gem_softc *sc = ifp->if_softc;
   2325    1.1       eeh 
   2326    1.1       eeh 	DPRINTF(sc, ("gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x "
   2327    1.1       eeh 		"GEM_MAC_RX_CONFIG %x\n",
   2328   1.50    martin 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_RX_CONFIG),
   2329   1.50    martin 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_STATUS),
   2330   1.50    martin 		bus_space_read_4(sc->sc_bustag, sc->sc_h1, GEM_MAC_RX_CONFIG)));
   2331    1.1       eeh 
   2332   1.85    dyoung 	log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
   2333  1.125   thorpej 	if_statinc(ifp, if_oerrors);
   2334    1.1       eeh 
   2335    1.1       eeh 	/* Try to get more packets going. */
   2336   1.99       jdc 	gem_init(ifp);
   2337    1.1       eeh 	gem_start(ifp);
   2338    1.1       eeh }
   2339    1.1       eeh 
   2340    1.1       eeh /*
   2341    1.1       eeh  * Initialize the MII Management Interface
   2342    1.1       eeh  */
   2343    1.1       eeh void
   2344   1.81       dsl gem_mifinit(struct gem_softc *sc)
   2345    1.1       eeh {
   2346    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   2347   1.50    martin 	bus_space_handle_t mif = sc->sc_h1;
   2348    1.1       eeh 
   2349    1.1       eeh 	/* Configure the MIF in frame mode */
   2350    1.1       eeh 	sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG);
   2351    1.1       eeh 	sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA;
   2352    1.1       eeh 	bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config);
   2353    1.1       eeh }
   2354    1.1       eeh 
   2355    1.1       eeh /*
   2356    1.1       eeh  * MII interface
   2357    1.1       eeh  *
   2358    1.1       eeh  * The GEM MII interface supports at least three different operating modes:
   2359    1.1       eeh  *
   2360    1.1       eeh  * Bitbang mode is implemented using data, clock and output enable registers.
   2361    1.1       eeh  *
   2362    1.1       eeh  * Frame mode is implemented by loading a complete frame into the frame
   2363    1.1       eeh  * register and polling the valid bit for completion.
   2364    1.1       eeh  *
   2365    1.1       eeh  * Polling mode uses the frame register but completion is indicated by
   2366    1.1       eeh  * an interrupt.
   2367    1.1       eeh  *
   2368    1.1       eeh  */
   2369    1.1       eeh static int
   2370  1.113   msaitoh gem_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
   2371    1.1       eeh {
   2372   1.85    dyoung 	struct gem_softc *sc = device_private(self);
   2373    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   2374   1.50    martin 	bus_space_handle_t mif = sc->sc_h1;
   2375    1.1       eeh 	int n;
   2376  1.112   msaitoh 	uint32_t v;
   2377    1.1       eeh 
   2378    1.1       eeh #ifdef GEM_DEBUG1
   2379    1.1       eeh 	if (sc->sc_debug)
   2380   1.68       jdc 		printf("gem_mii_readreg: PHY %d reg %d\n", phy, reg);
   2381    1.1       eeh #endif
   2382    1.1       eeh 
   2383    1.1       eeh 	/* Construct the frame command */
   2384    1.1       eeh 	v = (reg << GEM_MIF_REG_SHIFT)	| (phy << GEM_MIF_PHY_SHIFT) |
   2385    1.1       eeh 		GEM_MIF_FRAME_READ;
   2386    1.1       eeh 
   2387    1.1       eeh 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   2388    1.1       eeh 	for (n = 0; n < 100; n++) {
   2389    1.1       eeh 		DELAY(1);
   2390    1.1       eeh 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   2391  1.113   msaitoh 		if (v & GEM_MIF_FRAME_TA0) {
   2392  1.113   msaitoh 			*val = v & GEM_MIF_FRAME_DATA;
   2393  1.113   msaitoh 			return 0;
   2394  1.113   msaitoh 		}
   2395    1.1       eeh 	}
   2396    1.1       eeh 
   2397   1.85    dyoung 	printf("%s: mii_read timeout\n", device_xname(sc->sc_dev));
   2398  1.113   msaitoh 	return ETIMEDOUT;
   2399    1.1       eeh }
   2400    1.1       eeh 
   2401  1.113   msaitoh static int
   2402  1.113   msaitoh gem_mii_writereg(device_t self, int phy, int reg, uint16_t val)
   2403    1.1       eeh {
   2404   1.85    dyoung 	struct gem_softc *sc = device_private(self);
   2405    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   2406   1.50    martin 	bus_space_handle_t mif = sc->sc_h1;
   2407    1.1       eeh 	int n;
   2408  1.112   msaitoh 	uint32_t v;
   2409    1.1       eeh 
   2410    1.1       eeh #ifdef GEM_DEBUG1
   2411    1.1       eeh 	if (sc->sc_debug)
   2412   1.68       jdc 		printf("gem_mii_writereg: PHY %d reg %d val %x\n",
   2413    1.1       eeh 			phy, reg, val);
   2414    1.1       eeh #endif
   2415    1.1       eeh 
   2416    1.1       eeh 	/* Construct the frame command */
   2417    1.1       eeh 	v = GEM_MIF_FRAME_WRITE			|
   2418    1.1       eeh 	    (phy << GEM_MIF_PHY_SHIFT)		|
   2419    1.1       eeh 	    (reg << GEM_MIF_REG_SHIFT)		|
   2420    1.1       eeh 	    (val & GEM_MIF_FRAME_DATA);
   2421    1.1       eeh 
   2422    1.1       eeh 	bus_space_write_4(t, mif, GEM_MIF_FRAME, v);
   2423    1.1       eeh 	for (n = 0; n < 100; n++) {
   2424    1.1       eeh 		DELAY(1);
   2425    1.1       eeh 		v = bus_space_read_4(t, mif, GEM_MIF_FRAME);
   2426    1.1       eeh 		if (v & GEM_MIF_FRAME_TA0)
   2427  1.113   msaitoh 			return 0;
   2428    1.1       eeh 	}
   2429    1.1       eeh 
   2430   1.85    dyoung 	printf("%s: mii_write timeout\n", device_xname(sc->sc_dev));
   2431  1.113   msaitoh 	return ETIMEDOUT;
   2432    1.1       eeh }
   2433    1.1       eeh 
   2434    1.1       eeh static void
   2435  1.100      matt gem_mii_statchg(struct ifnet *ifp)
   2436    1.1       eeh {
   2437  1.100      matt 	struct gem_softc *sc = ifp->if_softc;
   2438    1.3       eeh #ifdef GEM_DEBUG
   2439    1.1       eeh 	int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
   2440    1.3       eeh #endif
   2441    1.1       eeh 
   2442    1.1       eeh #ifdef GEM_DEBUG
   2443    1.1       eeh 	if (sc->sc_debug)
   2444   1.31      heas 		printf("gem_mii_statchg: status change: phy = %d\n",
   2445   1.28       chs 			sc->sc_phys[instance]);
   2446    1.1       eeh #endif
   2447   1.68       jdc 	gem_statuschange(sc);
   2448   1.68       jdc }
   2449    1.1       eeh 
   2450   1.68       jdc /*
   2451   1.68       jdc  * Common status change for gem_mii_statchg() and gem_pint()
   2452   1.68       jdc  */
   2453   1.68       jdc void
   2454   1.68       jdc gem_statuschange(struct gem_softc* sc)
   2455   1.68       jdc {
   2456   1.68       jdc 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2457   1.68       jdc 	bus_space_tag_t t = sc->sc_bustag;
   2458   1.68       jdc 	bus_space_handle_t mac = sc->sc_h1;
   2459   1.68       jdc 	int gigabit;
   2460  1.112   msaitoh 	uint32_t rxcfg, txcfg, v;
   2461   1.68       jdc 
   2462   1.68       jdc 	if ((sc->sc_mii.mii_media_status & IFM_ACTIVE) != 0 &&
   2463   1.68       jdc 	    IFM_SUBTYPE(sc->sc_mii.mii_media_active) != IFM_NONE)
   2464   1.68       jdc 		sc->sc_flags |= GEM_LINK;
   2465   1.68       jdc 	else
   2466   1.68       jdc 		sc->sc_flags &= ~GEM_LINK;
   2467   1.68       jdc 
   2468   1.70       jdc 	if (sc->sc_ethercom.ec_if.if_baudrate == IF_Mbps(1000))
   2469   1.68       jdc 		gigabit = 1;
   2470   1.70       jdc 	else
   2471   1.68       jdc 		gigabit = 0;
   2472    1.1       eeh 
   2473   1.68       jdc 	/*
   2474   1.68       jdc 	 * The configuration done here corresponds to the steps F) and
   2475   1.68       jdc 	 * G) and as far as enabling of RX and TX MAC goes also step H)
   2476   1.68       jdc 	 * of the initialization sequence outlined in section 3.2.1 of
   2477   1.68       jdc 	 * the GEM Gigabit Ethernet ASIC Specification.
   2478   1.68       jdc 	 */
   2479   1.68       jdc 
   2480   1.68       jdc 	rxcfg = bus_space_read_4(t, mac, GEM_MAC_RX_CONFIG);
   2481   1.68       jdc 	rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE);
   2482   1.68       jdc 	txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT;
   2483   1.68       jdc 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   2484   1.68       jdc 		txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS;
   2485   1.68       jdc 	else if (gigabit) {
   2486   1.68       jdc 		rxcfg |= GEM_MAC_RX_CARR_EXTEND;
   2487   1.68       jdc 		txcfg |= GEM_MAC_RX_CARR_EXTEND;
   2488   1.68       jdc 	}
   2489    1.1       eeh 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0);
   2490   1.68       jdc 	bus_space_barrier(t, mac, GEM_MAC_TX_CONFIG, 4,
   2491   1.68       jdc 	    BUS_SPACE_BARRIER_WRITE);
   2492   1.68       jdc 	if (!gem_bitwait(sc, mac, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0))
   2493   1.85    dyoung 		aprint_normal_dev(sc->sc_dev, "cannot disable TX MAC\n");
   2494   1.68       jdc 	bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, txcfg);
   2495   1.68       jdc 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, 0);
   2496   1.68       jdc 	bus_space_barrier(t, mac, GEM_MAC_RX_CONFIG, 4,
   2497   1.68       jdc 	    BUS_SPACE_BARRIER_WRITE);
   2498   1.68       jdc 	if (!gem_bitwait(sc, mac, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0))
   2499   1.85    dyoung 		aprint_normal_dev(sc->sc_dev, "cannot disable RX MAC\n");
   2500   1.68       jdc 	bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG, rxcfg);
   2501   1.68       jdc 
   2502   1.68       jdc 	v = bus_space_read_4(t, mac, GEM_MAC_CONTROL_CONFIG) &
   2503   1.68       jdc 	    ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE);
   2504   1.68       jdc 	bus_space_write_4(t, mac, GEM_MAC_CONTROL_CONFIG, v);
   2505   1.68       jdc 
   2506   1.68       jdc 	if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) == 0 &&
   2507   1.68       jdc 	    gigabit != 0)
   2508   1.68       jdc 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
   2509   1.68       jdc 		    GEM_MAC_SLOT_TIME_CARR_EXTEND);
   2510   1.68       jdc 	else
   2511   1.68       jdc 		bus_space_write_4(t, mac, GEM_MAC_SLOT_TIME,
   2512   1.68       jdc 		    GEM_MAC_SLOT_TIME_NORMAL);
   2513    1.1       eeh 
   2514    1.1       eeh 	/* XIF Configuration */
   2515   1.68       jdc 	if (sc->sc_flags & GEM_LINK)
   2516   1.68       jdc 		v = GEM_MAC_XIF_LINK_LED;
   2517   1.68       jdc 	else
   2518   1.68       jdc 		v = 0;
   2519    1.1       eeh 	v |= GEM_MAC_XIF_TX_MII_ENA;
   2520   1.70       jdc 
   2521   1.70       jdc 	/* If an external transceiver is connected, enable its MII drivers */
   2522   1.70       jdc 	sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG);
   2523   1.70       jdc 	if ((sc->sc_flags &(GEM_SERDES | GEM_SERIAL)) == 0) {
   2524   1.70       jdc 		if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) {
   2525   1.70       jdc 			if (gigabit)
   2526   1.70       jdc 				v |= GEM_MAC_XIF_GMII_MODE;
   2527   1.70       jdc 			else
   2528   1.70       jdc 				v &= ~GEM_MAC_XIF_GMII_MODE;
   2529   1.70       jdc 		} else
   2530   1.70       jdc 			/* Internal MII needs buf enable */
   2531   1.70       jdc 			v |= GEM_MAC_XIF_MII_BUF_ENA;
   2532   1.97       jdc 		/* MII needs echo disable if half duplex. */
   2533   1.97       jdc 		if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   2534   1.97       jdc 			/* turn on full duplex LED */
   2535   1.97       jdc 			v |= GEM_MAC_XIF_FDPLX_LED;
   2536   1.97       jdc 		else
   2537   1.97       jdc 			/* half duplex -- disable echo */
   2538   1.97       jdc 			v |= GEM_MAC_XIF_ECHO_DISABL;
   2539   1.68       jdc 	} else {
   2540   1.70       jdc 		if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
   2541   1.70       jdc 			v |= GEM_MAC_XIF_FDPLX_LED;
   2542   1.70       jdc 		v |= GEM_MAC_XIF_GMII_MODE;
   2543   1.68       jdc 	}
   2544   1.70       jdc 	bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v);
   2545   1.70       jdc 
   2546   1.68       jdc 	if ((ifp->if_flags & IFF_RUNNING) != 0 &&
   2547   1.68       jdc 	    (sc->sc_flags & GEM_LINK) != 0) {
   2548   1.68       jdc 		bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG,
   2549   1.68       jdc 		    txcfg | GEM_MAC_TX_ENABLE);
   2550   1.68       jdc 		bus_space_write_4(t, mac, GEM_MAC_RX_CONFIG,
   2551   1.68       jdc 		    rxcfg | GEM_MAC_RX_ENABLE);
   2552   1.68       jdc 	}
   2553    1.1       eeh }
   2554    1.1       eeh 
   2555    1.1       eeh int
   2556   1.69    dyoung gem_ser_mediachange(struct ifnet *ifp)
   2557    1.1       eeh {
   2558    1.1       eeh 	struct gem_softc *sc = ifp->if_softc;
   2559   1.68       jdc 	u_int s, t;
   2560    1.1       eeh 
   2561   1.69    dyoung 	if (IFM_TYPE(sc->sc_mii.mii_media.ifm_media) != IFM_ETHER)
   2562   1.68       jdc 		return EINVAL;
   2563    1.1       eeh 
   2564   1.69    dyoung 	s = IFM_SUBTYPE(sc->sc_mii.mii_media.ifm_media);
   2565   1.69    dyoung 	if (s == IFM_AUTO) {
   2566   1.69    dyoung 		if (sc->sc_mii_media != s) {
   2567   1.69    dyoung #ifdef GEM_DEBUG
   2568   1.85    dyoung 			aprint_debug_dev(sc->sc_dev, "setting media to auto\n");
   2569   1.69    dyoung #endif
   2570   1.69    dyoung 			sc->sc_mii_media = s;
   2571   1.69    dyoung 			if (ifp->if_flags & IFF_UP) {
   2572   1.69    dyoung 				gem_pcs_stop(sc, 0);
   2573   1.69    dyoung 				gem_pcs_start(sc);
   2574   1.69    dyoung 			}
   2575   1.69    dyoung 		}
   2576   1.69    dyoung 		return 0;
   2577   1.69    dyoung 	}
   2578   1.69    dyoung 	if (s == IFM_1000_SX) {
   2579  1.116   msaitoh 		t = IFM_OPTIONS(sc->sc_mii.mii_media.ifm_media)
   2580  1.116   msaitoh 		    & (IFM_FDX | IFM_HDX);
   2581  1.116   msaitoh 		if ((sc->sc_mii_media & (IFM_FDX | IFM_HDX)) != t) {
   2582  1.116   msaitoh 			sc->sc_mii_media &= ~(IFM_FDX | IFM_HDX);
   2583  1.116   msaitoh 			sc->sc_mii_media |= t;
   2584  1.116   msaitoh #ifdef GEM_DEBUG
   2585  1.116   msaitoh 			aprint_debug_dev(sc->sc_dev,
   2586  1.116   msaitoh 			    "setting media to 1000baseSX-%s\n",
   2587  1.116   msaitoh 			    t == IFM_FDX ? "FDX" : "HDX");
   2588   1.68       jdc #endif
   2589  1.116   msaitoh 			if (ifp->if_flags & IFF_UP) {
   2590  1.116   msaitoh 				gem_pcs_stop(sc, 0);
   2591  1.116   msaitoh 				gem_pcs_start(sc);
   2592   1.68       jdc 			}
   2593   1.68       jdc 		}
   2594  1.116   msaitoh 		return 0;
   2595   1.69    dyoung 	}
   2596   1.69    dyoung 	return EINVAL;
   2597    1.1       eeh }
   2598    1.1       eeh 
   2599    1.1       eeh void
   2600   1.69    dyoung gem_ser_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
   2601    1.1       eeh {
   2602    1.1       eeh 	struct gem_softc *sc = ifp->if_softc;
   2603    1.1       eeh 
   2604    1.1       eeh 	if ((ifp->if_flags & IFF_UP) == 0)
   2605    1.1       eeh 		return;
   2606    1.1       eeh 	ifmr->ifm_active = sc->sc_mii.mii_media_active;
   2607    1.1       eeh 	ifmr->ifm_status = sc->sc_mii.mii_media_status;
   2608    1.1       eeh }
   2609    1.1       eeh 
   2610   1.79    dyoung static int
   2611   1.79    dyoung gem_ifflags_cb(struct ethercom *ec)
   2612   1.79    dyoung {
   2613   1.79    dyoung 	struct ifnet *ifp = &ec->ec_if;
   2614   1.79    dyoung 	struct gem_softc *sc = ifp->if_softc;
   2615  1.121   msaitoh 	u_short change = ifp->if_flags ^ sc->sc_if_flags;
   2616   1.79    dyoung 
   2617  1.117   msaitoh 	if ((change & ~(IFF_CANTCHANGE | IFF_DEBUG)) != 0)
   2618   1.79    dyoung 		return ENETRESET;
   2619   1.79    dyoung 	else if ((change & IFF_PROMISC) != 0)
   2620   1.79    dyoung 		gem_setladrf(sc);
   2621   1.79    dyoung 	return 0;
   2622   1.79    dyoung }
   2623   1.79    dyoung 
   2624    1.1       eeh /*
   2625    1.1       eeh  * Process an ioctl request.
   2626    1.1       eeh  */
   2627    1.1       eeh int
   2628   1.79    dyoung gem_ioctl(struct ifnet *ifp, unsigned long cmd, void *data)
   2629    1.1       eeh {
   2630    1.1       eeh 	struct gem_softc *sc = ifp->if_softc;
   2631    1.1       eeh 	int s, error = 0;
   2632    1.1       eeh 
   2633   1.20      matt 	s = splnet();
   2634    1.1       eeh 
   2635   1.79    dyoung 	if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
   2636   1.74    dyoung 		error = 0;
   2637   1.74    dyoung 		if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
   2638   1.74    dyoung 			;
   2639   1.74    dyoung 		else if (ifp->if_flags & IFF_RUNNING) {
   2640    1.1       eeh 			/*
   2641    1.1       eeh 			 * Multicast list has changed; set the hardware filter
   2642    1.1       eeh 			 * accordingly.
   2643    1.1       eeh 			 */
   2644   1.74    dyoung 			gem_setladrf(sc);
   2645    1.1       eeh 		}
   2646    1.1       eeh 	}
   2647    1.1       eeh 
   2648    1.1       eeh 	/* Try to get things going again */
   2649   1.43  christos 	if (ifp->if_flags & IFF_UP)
   2650    1.1       eeh 		gem_start(ifp);
   2651    1.1       eeh 	splx(s);
   2652    1.1       eeh 	return (error);
   2653    1.1       eeh }
   2654    1.1       eeh 
   2655   1.85    dyoung static void
   2656   1.85    dyoung gem_inten(struct gem_softc *sc)
   2657   1.85    dyoung {
   2658   1.85    dyoung 	bus_space_tag_t t = sc->sc_bustag;
   2659   1.85    dyoung 	bus_space_handle_t h = sc->sc_h1;
   2660   1.85    dyoung 	uint32_t v;
   2661   1.85    dyoung 
   2662   1.85    dyoung 	if ((sc->sc_flags & (GEM_SERDES | GEM_SERIAL)) != 0)
   2663   1.85    dyoung 		v = GEM_INTR_PCS;
   2664   1.85    dyoung 	else
   2665   1.85    dyoung 		v = GEM_INTR_MIF;
   2666   1.85    dyoung 	bus_space_write_4(t, h, GEM_INTMASK,
   2667   1.85    dyoung 		      ~(GEM_INTR_TX_INTME |
   2668   1.85    dyoung 			GEM_INTR_TX_EMPTY |
   2669   1.85    dyoung 			GEM_INTR_TX_MAC |
   2670  1.117   msaitoh 			GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF |
   2671  1.117   msaitoh 			GEM_INTR_RX_TAG_ERR | GEM_INTR_MAC_CONTROL |
   2672   1.85    dyoung 			GEM_INTR_BERR | v));
   2673   1.85    dyoung }
   2674   1.85    dyoung 
   2675   1.85    dyoung bool
   2676   1.93    dyoung gem_resume(device_t self, const pmf_qual_t *qual)
   2677   1.85    dyoung {
   2678   1.85    dyoung 	struct gem_softc *sc = device_private(self);
   2679   1.85    dyoung 
   2680   1.85    dyoung 	gem_inten(sc);
   2681   1.85    dyoung 
   2682   1.85    dyoung 	return true;
   2683   1.85    dyoung }
   2684   1.85    dyoung 
   2685   1.85    dyoung bool
   2686   1.93    dyoung gem_suspend(device_t self, const pmf_qual_t *qual)
   2687   1.85    dyoung {
   2688   1.85    dyoung 	struct gem_softc *sc = device_private(self);
   2689   1.85    dyoung 	bus_space_tag_t t = sc->sc_bustag;
   2690   1.85    dyoung 	bus_space_handle_t h = sc->sc_h1;
   2691   1.85    dyoung 
   2692   1.85    dyoung 	bus_space_write_4(t, h, GEM_INTMASK, ~(uint32_t)0);
   2693   1.85    dyoung 
   2694   1.85    dyoung 	return true;
   2695   1.85    dyoung }
   2696    1.1       eeh 
   2697   1.85    dyoung bool
   2698   1.85    dyoung gem_shutdown(device_t self, int howto)
   2699    1.1       eeh {
   2700   1.85    dyoung 	struct gem_softc *sc = device_private(self);
   2701    1.1       eeh 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
   2702    1.1       eeh 
   2703    1.1       eeh 	gem_stop(ifp, 1);
   2704   1.85    dyoung 
   2705   1.85    dyoung 	return true;
   2706    1.1       eeh }
   2707    1.1       eeh 
   2708    1.1       eeh /*
   2709    1.1       eeh  * Set up the logical address filter.
   2710    1.1       eeh  */
   2711    1.1       eeh void
   2712   1.81       dsl gem_setladrf(struct gem_softc *sc)
   2713    1.1       eeh {
   2714   1.15      matt 	struct ethercom *ec = &sc->sc_ethercom;
   2715   1.15      matt 	struct ifnet *ifp = &ec->ec_if;
   2716    1.1       eeh 	struct ether_multi *enm;
   2717    1.1       eeh 	struct ether_multistep step;
   2718    1.1       eeh 	bus_space_tag_t t = sc->sc_bustag;
   2719   1.50    martin 	bus_space_handle_t h = sc->sc_h1;
   2720  1.112   msaitoh 	uint32_t crc;
   2721  1.112   msaitoh 	uint32_t hash[16];
   2722  1.112   msaitoh 	uint32_t v;
   2723   1.15      matt 	int i;
   2724    1.1       eeh 
   2725    1.1       eeh 	/* Get current RX configuration */
   2726    1.1       eeh 	v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG);
   2727    1.1       eeh 
   2728   1.15      matt 	/*
   2729   1.15      matt 	 * Turn off promiscuous mode, promiscuous group mode (all multicast),
   2730   1.15      matt 	 * and hash filter.  Depending on the case, the right bit will be
   2731   1.15      matt 	 * enabled.
   2732   1.15      matt 	 */
   2733  1.117   msaitoh 	v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_HASH_FILTER |
   2734   1.15      matt 	    GEM_MAC_RX_PROMISC_GRP);
   2735   1.15      matt 
   2736    1.1       eeh 	if ((ifp->if_flags & IFF_PROMISC) != 0) {
   2737   1.15      matt 		/* Turn on promiscuous mode */
   2738    1.1       eeh 		v |= GEM_MAC_RX_PROMISCUOUS;
   2739    1.1       eeh 		ifp->if_flags |= IFF_ALLMULTI;
   2740    1.1       eeh 		goto chipit;
   2741    1.1       eeh 	}
   2742    1.1       eeh 
   2743    1.1       eeh 	/*
   2744    1.1       eeh 	 * Set up multicast address filter by passing all multicast addresses
   2745   1.15      matt 	 * through a crc generator, and then using the high order 8 bits as an
   2746   1.15      matt 	 * index into the 256 bit logical address filter.  The high order 4
   2747   1.41  christos 	 * bits selects the word, while the other 4 bits select the bit within
   2748   1.15      matt 	 * the word (where bit 0 is the MSB).
   2749    1.1       eeh 	 */
   2750    1.1       eeh 
   2751   1.15      matt 	/* Clear hash table */
   2752   1.15      matt 	memset(hash, 0, sizeof(hash));
   2753   1.15      matt 
   2754  1.119   msaitoh 	ETHER_LOCK(ec);
   2755    1.1       eeh 	ETHER_FIRST_MULTI(step, ec, enm);
   2756    1.1       eeh 	while (enm != NULL) {
   2757    1.6   thorpej 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
   2758    1.1       eeh 			/*
   2759    1.1       eeh 			 * We must listen to a range of multicast addresses.
   2760    1.1       eeh 			 * For now, just accept all multicasts, rather than
   2761    1.1       eeh 			 * trying to set only those filter bits needed to match
   2762    1.1       eeh 			 * the range.  (At this time, the only use of address
   2763    1.1       eeh 			 * ranges is for IP multicast routing, for which the
   2764    1.1       eeh 			 * range is big enough to require all bits set.)
   2765   1.68       jdc 			 * XXX should use the address filters for this
   2766    1.1       eeh 			 */
   2767    1.1       eeh 			ifp->if_flags |= IFF_ALLMULTI;
   2768   1.15      matt 			v |= GEM_MAC_RX_PROMISC_GRP;
   2769  1.119   msaitoh 			ETHER_UNLOCK(ec);
   2770    1.1       eeh 			goto chipit;
   2771    1.1       eeh 		}
   2772    1.1       eeh 
   2773   1.15      matt 		/* Get the LE CRC32 of the address */
   2774   1.15      matt 		crc = ether_crc32_le(enm->enm_addrlo, sizeof(enm->enm_addrlo));
   2775    1.1       eeh 
   2776    1.1       eeh 		/* Just want the 8 most significant bits. */
   2777    1.1       eeh 		crc >>= 24;
   2778    1.1       eeh 
   2779    1.1       eeh 		/* Set the corresponding bit in the filter. */
   2780   1.15      matt 		hash[crc >> 4] |= 1 << (15 - (crc & 15));
   2781    1.1       eeh 
   2782    1.1       eeh 		ETHER_NEXT_MULTI(step, enm);
   2783    1.1       eeh 	}
   2784  1.119   msaitoh 	ETHER_UNLOCK(ec);
   2785    1.1       eeh 
   2786   1.15      matt 	v |= GEM_MAC_RX_HASH_FILTER;
   2787    1.1       eeh 	ifp->if_flags &= ~IFF_ALLMULTI;
   2788    1.1       eeh 
   2789   1.15      matt 	/* Now load the hash table into the chip (if we are using it) */
   2790   1.15      matt 	for (i = 0; i < 16; i++) {
   2791   1.15      matt 		bus_space_write_4(t, h,
   2792   1.15      matt 		    GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0),
   2793   1.15      matt 		    hash[i]);
   2794   1.15      matt 	}
   2795   1.15      matt 
   2796    1.1       eeh chipit:
   2797   1.41  christos 	sc->sc_if_flags = ifp->if_flags;
   2798    1.1       eeh 	bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v);
   2799    1.1       eeh }
   2800