1aad970f1SDavid E. O'Brien /*- 242c1b001SThomas Moestl * Copyright (C) 2001 Eduardo Horvath. 3305f2c06SThomas Moestl * Copyright (c) 2001-2003 Thomas Moestl 442c1b001SThomas Moestl * All rights reserved. 542c1b001SThomas Moestl * 642c1b001SThomas Moestl * Redistribution and use in source and binary forms, with or without 742c1b001SThomas Moestl * modification, are permitted provided that the following conditions 842c1b001SThomas Moestl * are met: 942c1b001SThomas Moestl * 1. Redistributions of source code must retain the above copyright 1042c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer. 1142c1b001SThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 1242c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer in the 1342c1b001SThomas Moestl * documentation and/or other materials provided with the distribution. 1442c1b001SThomas Moestl * 1542c1b001SThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 1642c1b001SThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1742c1b001SThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1842c1b001SThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 1942c1b001SThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2042c1b001SThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2142c1b001SThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2242c1b001SThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2342c1b001SThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2442c1b001SThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2542c1b001SThomas Moestl * SUCH DAMAGE. 2642c1b001SThomas Moestl * 27336cca9eSBenno Rice * from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp 2842c1b001SThomas Moestl */ 2942c1b001SThomas Moestl 30aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 32aad970f1SDavid E. O'Brien 3342c1b001SThomas Moestl /* 3442c1b001SThomas Moestl * Driver for Sun GEM ethernet controllers. 3542c1b001SThomas Moestl */ 3642c1b001SThomas Moestl 3718100346SThomas Moestl #if 0 3842c1b001SThomas Moestl #define GEM_DEBUG 3918100346SThomas Moestl #endif 4042c1b001SThomas Moestl 41c3d5598aSMarius Strobl #if 0 /* XXX: In case of emergency, re-enable this. */ 42c3d5598aSMarius Strobl #define GEM_RINT_TIMEOUT 43c3d5598aSMarius Strobl #endif 44c3d5598aSMarius Strobl 4542c1b001SThomas Moestl #include <sys/param.h> 4642c1b001SThomas Moestl #include <sys/systm.h> 4742c1b001SThomas Moestl #include <sys/bus.h> 4842c1b001SThomas Moestl #include <sys/callout.h> 49a30d4b32SMike Barcroft #include <sys/endian.h> 5042c1b001SThomas Moestl #include <sys/mbuf.h> 5142c1b001SThomas Moestl #include <sys/malloc.h> 5242c1b001SThomas Moestl #include <sys/kernel.h> 538cfaff7dSMarius Strobl #include <sys/lock.h> 54186f2b9eSPoul-Henning Kamp #include <sys/module.h> 558cfaff7dSMarius Strobl #include <sys/mutex.h> 5642c1b001SThomas Moestl #include <sys/socket.h> 5742c1b001SThomas Moestl #include <sys/sockio.h> 58e1bb13cdSPoul-Henning Kamp #include <sys/rman.h> 5942c1b001SThomas Moestl 6008e0fdebSThomas Moestl #include <net/bpf.h> 6142c1b001SThomas Moestl #include <net/ethernet.h> 6242c1b001SThomas Moestl #include <net/if.h> 6342c1b001SThomas Moestl #include <net/if_arp.h> 6442c1b001SThomas Moestl #include <net/if_dl.h> 6542c1b001SThomas Moestl #include <net/if_media.h> 66fc74a9f9SBrooks Davis #include <net/if_types.h> 6700d12766SMarius Strobl #include <net/if_vlan_var.h> 6842c1b001SThomas Moestl 6942c1b001SThomas Moestl #include <machine/bus.h> 7042c1b001SThomas Moestl 7142c1b001SThomas Moestl #include <dev/mii/mii.h> 7242c1b001SThomas Moestl #include <dev/mii/miivar.h> 7342c1b001SThomas Moestl 74681f7d03SWarner Losh #include <dev/gem/if_gemreg.h> 75681f7d03SWarner Losh #include <dev/gem/if_gemvar.h> 7642c1b001SThomas Moestl 7742c1b001SThomas Moestl #define TRIES 10000 7842c1b001SThomas Moestl 79e51a25f8SAlfred Perlstein static void gem_start(struct ifnet *); 808cfaff7dSMarius Strobl static void gem_start_locked(struct ifnet *); 81e51a25f8SAlfred Perlstein static void gem_stop(struct ifnet *, int); 82e51a25f8SAlfred Perlstein static int gem_ioctl(struct ifnet *, u_long, caddr_t); 83e51a25f8SAlfred Perlstein static void gem_cddma_callback(void *, bus_dma_segment_t *, int, int); 84305f2c06SThomas Moestl static void gem_txdma_callback(void *, bus_dma_segment_t *, int, 85305f2c06SThomas Moestl bus_size_t, int); 86e51a25f8SAlfred Perlstein static void gem_tick(void *); 878cb37876SMarius Strobl static int gem_watchdog(struct gem_softc *); 88e51a25f8SAlfred Perlstein static void gem_init(void *); 898cb37876SMarius Strobl static void gem_init_locked(struct gem_softc *); 908cb37876SMarius Strobl static void gem_init_regs(struct gem_softc *); 91e51a25f8SAlfred Perlstein static int gem_ringsize(int sz); 92e51a25f8SAlfred Perlstein static int gem_meminit(struct gem_softc *); 93305f2c06SThomas Moestl static int gem_load_txmbuf(struct gem_softc *, struct mbuf *); 94e51a25f8SAlfred Perlstein static void gem_mifinit(struct gem_softc *); 958cb37876SMarius Strobl static int gem_bitwait(struct gem_softc *, bus_addr_t, u_int32_t, 968cb37876SMarius Strobl u_int32_t); 97e51a25f8SAlfred Perlstein static int gem_reset_rx(struct gem_softc *); 98e51a25f8SAlfred Perlstein static int gem_reset_tx(struct gem_softc *); 99e51a25f8SAlfred Perlstein static int gem_disable_rx(struct gem_softc *); 100e51a25f8SAlfred Perlstein static int gem_disable_tx(struct gem_softc *); 101e51a25f8SAlfred Perlstein static void gem_rxdrain(struct gem_softc *); 102e51a25f8SAlfred Perlstein static int gem_add_rxbuf(struct gem_softc *, int); 103e51a25f8SAlfred Perlstein static void gem_setladrf(struct gem_softc *); 10442c1b001SThomas Moestl 105e51a25f8SAlfred Perlstein struct mbuf *gem_get(struct gem_softc *, int, int); 106e51a25f8SAlfred Perlstein static void gem_eint(struct gem_softc *, u_int); 107e51a25f8SAlfred Perlstein static void gem_rint(struct gem_softc *); 108c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 1090d80b9bdSThomas Moestl static void gem_rint_timeout(void *); 11011e3f060SJake Burkholder #endif 111e51a25f8SAlfred Perlstein static void gem_tint(struct gem_softc *); 11242c1b001SThomas Moestl #ifdef notyet 113e51a25f8SAlfred Perlstein static void gem_power(int, void *); 11442c1b001SThomas Moestl #endif 11542c1b001SThomas Moestl 11642c1b001SThomas Moestl devclass_t gem_devclass; 11742c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0); 11842c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1); 11942c1b001SThomas Moestl 12042c1b001SThomas Moestl #ifdef GEM_DEBUG 12142c1b001SThomas Moestl #include <sys/ktr.h> 12242c1b001SThomas Moestl #define KTR_GEM KTR_CT2 12342c1b001SThomas Moestl #endif 12442c1b001SThomas Moestl 12518100346SThomas Moestl #define GEM_NSEGS GEM_NTXDESC 12642c1b001SThomas Moestl 12742c1b001SThomas Moestl /* 12842c1b001SThomas Moestl * gem_attach: 12942c1b001SThomas Moestl * 13042c1b001SThomas Moestl * Attach a Gem interface to the system. 13142c1b001SThomas Moestl */ 13242c1b001SThomas Moestl int 13342c1b001SThomas Moestl gem_attach(sc) 13442c1b001SThomas Moestl struct gem_softc *sc; 13542c1b001SThomas Moestl { 136fc74a9f9SBrooks Davis struct ifnet *ifp; 13742c1b001SThomas Moestl struct mii_softc *child; 13842c1b001SThomas Moestl int i, error; 139336cca9eSBenno Rice u_int32_t v; 14042c1b001SThomas Moestl 141fc74a9f9SBrooks Davis ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 142fc74a9f9SBrooks Davis if (ifp == NULL) 143fc74a9f9SBrooks Davis return (ENOSPC); 144fc74a9f9SBrooks Davis 1451f317bf9SMarius Strobl callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0); 1461f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 1471f317bf9SMarius Strobl callout_init_mtx(&sc->sc_rx_ch, &sc->sc_mtx, 0); 1481f317bf9SMarius Strobl #endif 1491f317bf9SMarius Strobl 15042c1b001SThomas Moestl /* Make sure the chip is stopped. */ 15142c1b001SThomas Moestl ifp->if_softc = sc; 1528cfaff7dSMarius Strobl GEM_LOCK(sc); 1531f317bf9SMarius Strobl gem_stop(ifp, 0); 15442c1b001SThomas Moestl gem_reset(sc); 1558cfaff7dSMarius Strobl GEM_UNLOCK(sc); 15642c1b001SThomas Moestl 157378f231eSJohn-Mark Gurney error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 158378f231eSJohn-Mark Gurney BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 159378f231eSJohn-Mark Gurney MCLBYTES, GEM_NSEGS, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, 160378f231eSJohn-Mark Gurney &sc->sc_pdmatag); 16142c1b001SThomas Moestl if (error) 162fc74a9f9SBrooks Davis goto fail_ifnet; 16342c1b001SThomas Moestl 16442c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 16542c1b001SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE, 166f6b1c44dSScott Long 1, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW, NULL, NULL, 167305f2c06SThomas Moestl &sc->sc_rdmatag); 16842c1b001SThomas Moestl if (error) 169305f2c06SThomas Moestl goto fail_ptag; 170305f2c06SThomas Moestl 171305f2c06SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 172305f2c06SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 17318100346SThomas Moestl GEM_TD_BUFSIZE, GEM_NTXDESC, BUS_SPACE_MAXSIZE_32BIT, 174f6b1c44dSScott Long BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag); 175305f2c06SThomas Moestl if (error) 176305f2c06SThomas Moestl goto fail_rtag; 17742c1b001SThomas Moestl 17842c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0, 17942c1b001SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 18042c1b001SThomas Moestl sizeof(struct gem_control_data), 1, 18142c1b001SThomas Moestl sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW, 1821f317bf9SMarius Strobl busdma_lock_mutex, &sc->sc_mtx, &sc->sc_cdmatag); 18342c1b001SThomas Moestl if (error) 184305f2c06SThomas Moestl goto fail_ttag; 18542c1b001SThomas Moestl 18642c1b001SThomas Moestl /* 18742c1b001SThomas Moestl * Allocate the control data structures, and create and load the 18842c1b001SThomas Moestl * DMA map for it. 18942c1b001SThomas Moestl */ 19042c1b001SThomas Moestl if ((error = bus_dmamem_alloc(sc->sc_cdmatag, 19142c1b001SThomas Moestl (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) { 19242c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to allocate control data," 19342c1b001SThomas Moestl " error = %d\n", error); 194305f2c06SThomas Moestl goto fail_ctag; 19542c1b001SThomas Moestl } 19642c1b001SThomas Moestl 19742c1b001SThomas Moestl sc->sc_cddma = 0; 19842c1b001SThomas Moestl if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap, 19942c1b001SThomas Moestl sc->sc_control_data, sizeof(struct gem_control_data), 20042c1b001SThomas Moestl gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) { 20142c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to load control data DMA " 20242c1b001SThomas Moestl "map, error = %d\n", error); 203305f2c06SThomas Moestl goto fail_cmem; 20442c1b001SThomas Moestl } 20542c1b001SThomas Moestl 20642c1b001SThomas Moestl /* 20742c1b001SThomas Moestl * Initialize the transmit job descriptors. 20842c1b001SThomas Moestl */ 20942c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txfreeq); 21042c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txdirtyq); 21142c1b001SThomas Moestl 21242c1b001SThomas Moestl /* 21342c1b001SThomas Moestl * Create the transmit buffer DMA maps. 21442c1b001SThomas Moestl */ 21542c1b001SThomas Moestl error = ENOMEM; 21642c1b001SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 21742c1b001SThomas Moestl struct gem_txsoft *txs; 21842c1b001SThomas Moestl 21942c1b001SThomas Moestl txs = &sc->sc_txsoft[i]; 22042c1b001SThomas Moestl txs->txs_mbuf = NULL; 22142c1b001SThomas Moestl txs->txs_ndescs = 0; 222305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_tdmatag, 0, 22342c1b001SThomas Moestl &txs->txs_dmamap)) != 0) { 22442c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to create tx DMA map " 22542c1b001SThomas Moestl "%d, error = %d\n", i, error); 226305f2c06SThomas Moestl goto fail_txd; 22742c1b001SThomas Moestl } 22842c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 22942c1b001SThomas Moestl } 23042c1b001SThomas Moestl 23142c1b001SThomas Moestl /* 23242c1b001SThomas Moestl * Create the receive buffer DMA maps. 23342c1b001SThomas Moestl */ 23442c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 235305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_rdmatag, 0, 23642c1b001SThomas Moestl &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 23742c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to create rx DMA map " 23842c1b001SThomas Moestl "%d, error = %d\n", i, error); 239305f2c06SThomas Moestl goto fail_rxd; 24042c1b001SThomas Moestl } 24142c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_mbuf = NULL; 24242c1b001SThomas Moestl } 24342c1b001SThomas Moestl 24442c1b001SThomas Moestl gem_mifinit(sc); 24542c1b001SThomas Moestl 24642c1b001SThomas Moestl if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange, 24742c1b001SThomas Moestl gem_mediastatus)) != 0) { 24842c1b001SThomas Moestl device_printf(sc->sc_dev, "phy probe failed: %d\n", error); 249305f2c06SThomas Moestl goto fail_rxd; 25042c1b001SThomas Moestl } 25142c1b001SThomas Moestl sc->sc_mii = device_get_softc(sc->sc_miibus); 25242c1b001SThomas Moestl 25342c1b001SThomas Moestl /* 25442c1b001SThomas Moestl * From this point forward, the attachment cannot fail. A failure 25542c1b001SThomas Moestl * before this point releases all resources that may have been 25642c1b001SThomas Moestl * allocated. 25742c1b001SThomas Moestl */ 25842c1b001SThomas Moestl 259336cca9eSBenno Rice /* Get RX FIFO size */ 260336cca9eSBenno Rice sc->sc_rxfifosize = 64 * 261e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_RX_FIFO_SIZE); 262336cca9eSBenno Rice 263336cca9eSBenno Rice /* Get TX FIFO size */ 264e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_TX_FIFO_SIZE); 2653a5aee5aSThomas Moestl device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n", 2663a5aee5aSThomas Moestl sc->sc_rxfifosize / 1024, v / 16); 26742c1b001SThomas Moestl 26842c1b001SThomas Moestl /* Initialize ifnet structure. */ 26942c1b001SThomas Moestl ifp->if_softc = sc; 2709bf40edeSBrooks Davis if_initname(ifp, device_get_name(sc->sc_dev), 2719bf40edeSBrooks Davis device_get_unit(sc->sc_dev)); 2728cfaff7dSMarius Strobl ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 27342c1b001SThomas Moestl ifp->if_start = gem_start; 27442c1b001SThomas Moestl ifp->if_ioctl = gem_ioctl; 27542c1b001SThomas Moestl ifp->if_init = gem_init; 27642c1b001SThomas Moestl ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN; 27742c1b001SThomas Moestl /* 27842c1b001SThomas Moestl * Walk along the list of attached MII devices and 27942c1b001SThomas Moestl * establish an `MII instance' to `phy number' 28042c1b001SThomas Moestl * mapping. We'll use this mapping in media change 28142c1b001SThomas Moestl * requests to determine which phy to use to program 28242c1b001SThomas Moestl * the MIF configuration register. 28342c1b001SThomas Moestl */ 28442c1b001SThomas Moestl for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL; 28542c1b001SThomas Moestl child = LIST_NEXT(child, mii_list)) { 28642c1b001SThomas Moestl /* 28742c1b001SThomas Moestl * Note: we support just two PHYs: the built-in 28842c1b001SThomas Moestl * internal device and an external on the MII 28942c1b001SThomas Moestl * connector. 29042c1b001SThomas Moestl */ 29142c1b001SThomas Moestl if (child->mii_phy > 1 || child->mii_inst > 1) { 29242c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot accomodate " 29342c1b001SThomas Moestl "MII device %s at phy %d, instance %d\n", 29442c1b001SThomas Moestl device_get_name(child->mii_dev), 29542c1b001SThomas Moestl child->mii_phy, child->mii_inst); 29642c1b001SThomas Moestl continue; 29742c1b001SThomas Moestl } 29842c1b001SThomas Moestl 29942c1b001SThomas Moestl sc->sc_phys[child->mii_inst] = child->mii_phy; 30042c1b001SThomas Moestl } 30142c1b001SThomas Moestl 30242c1b001SThomas Moestl /* 30342c1b001SThomas Moestl * Now select and activate the PHY we will use. 30442c1b001SThomas Moestl * 30542c1b001SThomas Moestl * The order of preference is External (MDI1), 30642c1b001SThomas Moestl * Internal (MDI0), Serial Link (no MII). 30742c1b001SThomas Moestl */ 30842c1b001SThomas Moestl if (sc->sc_phys[1]) { 30942c1b001SThomas Moestl #ifdef GEM_DEBUG 31042c1b001SThomas Moestl printf("using external phy\n"); 31142c1b001SThomas Moestl #endif 31242c1b001SThomas Moestl sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL; 31342c1b001SThomas Moestl } else { 31442c1b001SThomas Moestl #ifdef GEM_DEBUG 31542c1b001SThomas Moestl printf("using internal phy\n"); 31642c1b001SThomas Moestl #endif 31742c1b001SThomas Moestl sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL; 31842c1b001SThomas Moestl } 319e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_CONFIG, 32042c1b001SThomas Moestl sc->sc_mif_config); 32142c1b001SThomas Moestl /* Attach the interface. */ 322fc74a9f9SBrooks Davis ether_ifattach(ifp, sc->sc_enaddr); 32342c1b001SThomas Moestl 324342ed5d9SRuslan Ermilov #ifdef notyet 32542c1b001SThomas Moestl /* 32642c1b001SThomas Moestl * Add a suspend hook to make sure we come back up after a 32742c1b001SThomas Moestl * resume. 32842c1b001SThomas Moestl */ 32942c1b001SThomas Moestl sc->sc_powerhook = powerhook_establish(gem_power, sc); 33042c1b001SThomas Moestl if (sc->sc_powerhook == NULL) 33142c1b001SThomas Moestl device_printf(sc->sc_dev, "WARNING: unable to establish power " 33242c1b001SThomas Moestl "hook\n"); 33342c1b001SThomas Moestl #endif 33442c1b001SThomas Moestl 33500d12766SMarius Strobl /* 33600d12766SMarius Strobl * Tell the upper layer(s) we support long frames. 33700d12766SMarius Strobl */ 33800d12766SMarius Strobl ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 33900d12766SMarius Strobl ifp->if_capabilities |= IFCAP_VLAN_MTU; 34000d12766SMarius Strobl ifp->if_capenable |= IFCAP_VLAN_MTU; 34100d12766SMarius Strobl 34242c1b001SThomas Moestl return (0); 34342c1b001SThomas Moestl 34442c1b001SThomas Moestl /* 34542c1b001SThomas Moestl * Free any resources we've allocated during the failed attach 34642c1b001SThomas Moestl * attempt. Do this in reverse order and fall through. 34742c1b001SThomas Moestl */ 348305f2c06SThomas Moestl fail_rxd: 34942c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 35042c1b001SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 351305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 35242c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 35342c1b001SThomas Moestl } 354305f2c06SThomas Moestl fail_txd: 35542c1b001SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 35642c1b001SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 357305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 35842c1b001SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 35942c1b001SThomas Moestl } 360305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 361305f2c06SThomas Moestl fail_cmem: 36242c1b001SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 36342c1b001SThomas Moestl sc->sc_cddmamap); 364305f2c06SThomas Moestl fail_ctag: 36542c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 366305f2c06SThomas Moestl fail_ttag: 367305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 368305f2c06SThomas Moestl fail_rtag: 369305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 370305f2c06SThomas Moestl fail_ptag: 37142c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 372fc74a9f9SBrooks Davis fail_ifnet: 373fc74a9f9SBrooks Davis if_free(ifp); 37442c1b001SThomas Moestl return (error); 37542c1b001SThomas Moestl } 37642c1b001SThomas Moestl 377cbbdf236SThomas Moestl void 378cbbdf236SThomas Moestl gem_detach(sc) 379cbbdf236SThomas Moestl struct gem_softc *sc; 380cbbdf236SThomas Moestl { 381fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 382cbbdf236SThomas Moestl int i; 383cbbdf236SThomas Moestl 3848cfaff7dSMarius Strobl GEM_LOCK(sc); 38525bd46d0SBrooks Davis gem_stop(ifp, 1); 3868cfaff7dSMarius Strobl GEM_UNLOCK(sc); 3871f317bf9SMarius Strobl callout_drain(&sc->sc_tick_ch); 3881f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 3891f317bf9SMarius Strobl callout_drain(&sc->sc_rx_ch); 3901f317bf9SMarius Strobl #endif 391cbbdf236SThomas Moestl ether_ifdetach(ifp); 392fc74a9f9SBrooks Davis if_free(ifp); 393cbbdf236SThomas Moestl device_delete_child(sc->sc_dev, sc->sc_miibus); 394cbbdf236SThomas Moestl 395cbbdf236SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 396cbbdf236SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 397cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 398cbbdf236SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 399cbbdf236SThomas Moestl } 400cbbdf236SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 401cbbdf236SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 402cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 403cbbdf236SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 404cbbdf236SThomas Moestl } 405b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 406b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTWRITE); 407cbbdf236SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 408cbbdf236SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 409cbbdf236SThomas Moestl sc->sc_cddmamap); 410cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 411cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 412cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 413cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 414cbbdf236SThomas Moestl } 415cbbdf236SThomas Moestl 416cbbdf236SThomas Moestl void 417cbbdf236SThomas Moestl gem_suspend(sc) 418cbbdf236SThomas Moestl struct gem_softc *sc; 419cbbdf236SThomas Moestl { 420fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 421cbbdf236SThomas Moestl 4228cfaff7dSMarius Strobl GEM_LOCK(sc); 423cbbdf236SThomas Moestl gem_stop(ifp, 0); 4248cfaff7dSMarius Strobl GEM_UNLOCK(sc); 425cbbdf236SThomas Moestl } 426cbbdf236SThomas Moestl 427cbbdf236SThomas Moestl void 428cbbdf236SThomas Moestl gem_resume(sc) 429cbbdf236SThomas Moestl struct gem_softc *sc; 430cbbdf236SThomas Moestl { 431fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 432cbbdf236SThomas Moestl 4338cfaff7dSMarius Strobl GEM_LOCK(sc); 43400d12766SMarius Strobl /* 43500d12766SMarius Strobl * On resume all registers have to be initialized again like 43600d12766SMarius Strobl * after power-on. 43700d12766SMarius Strobl */ 43800d12766SMarius Strobl sc->sc_inited = 0; 439cbbdf236SThomas Moestl if (ifp->if_flags & IFF_UP) 4408cfaff7dSMarius Strobl gem_init_locked(sc); 4418cfaff7dSMarius Strobl GEM_UNLOCK(sc); 442cbbdf236SThomas Moestl } 443cbbdf236SThomas Moestl 44442c1b001SThomas Moestl static void 44542c1b001SThomas Moestl gem_cddma_callback(xsc, segs, nsegs, error) 44642c1b001SThomas Moestl void *xsc; 44742c1b001SThomas Moestl bus_dma_segment_t *segs; 44842c1b001SThomas Moestl int nsegs; 44942c1b001SThomas Moestl int error; 45042c1b001SThomas Moestl { 45142c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)xsc; 45242c1b001SThomas Moestl 45342c1b001SThomas Moestl if (error != 0) 45442c1b001SThomas Moestl return; 45542c1b001SThomas Moestl if (nsegs != 1) { 45642c1b001SThomas Moestl /* can't happen... */ 45742c1b001SThomas Moestl panic("gem_cddma_callback: bad control buffer segment count"); 45842c1b001SThomas Moestl } 45942c1b001SThomas Moestl sc->sc_cddma = segs[0].ds_addr; 46042c1b001SThomas Moestl } 46142c1b001SThomas Moestl 46242c1b001SThomas Moestl static void 463305f2c06SThomas Moestl gem_txdma_callback(xsc, segs, nsegs, totsz, error) 46442c1b001SThomas Moestl void *xsc; 46542c1b001SThomas Moestl bus_dma_segment_t *segs; 46642c1b001SThomas Moestl int nsegs; 467305f2c06SThomas Moestl bus_size_t totsz; 46842c1b001SThomas Moestl int error; 46942c1b001SThomas Moestl { 470305f2c06SThomas Moestl struct gem_txdma *txd = (struct gem_txdma *)xsc; 471305f2c06SThomas Moestl struct gem_softc *sc = txd->txd_sc; 472305f2c06SThomas Moestl struct gem_txsoft *txs = txd->txd_txs; 473305f2c06SThomas Moestl bus_size_t len = 0; 474305f2c06SThomas Moestl uint64_t flags = 0; 475305f2c06SThomas Moestl int seg, nexttx; 47642c1b001SThomas Moestl 47742c1b001SThomas Moestl if (error != 0) 47842c1b001SThomas Moestl return; 479305f2c06SThomas Moestl /* 480305f2c06SThomas Moestl * Ensure we have enough descriptors free to describe 481305f2c06SThomas Moestl * the packet. Note, we always reserve one descriptor 482305f2c06SThomas Moestl * at the end of the ring as a termination point, to 483305f2c06SThomas Moestl * prevent wrap-around. 484305f2c06SThomas Moestl */ 485305f2c06SThomas Moestl if (nsegs > sc->sc_txfree - 1) { 486305f2c06SThomas Moestl txs->txs_ndescs = -1; 487305f2c06SThomas Moestl return; 488305f2c06SThomas Moestl } 489305f2c06SThomas Moestl txs->txs_ndescs = nsegs; 49042c1b001SThomas Moestl 491305f2c06SThomas Moestl nexttx = txs->txs_firstdesc; 49242c1b001SThomas Moestl /* 49342c1b001SThomas Moestl * Initialize the transmit descriptors. 49442c1b001SThomas Moestl */ 49542c1b001SThomas Moestl for (seg = 0; seg < nsegs; 496305f2c06SThomas Moestl seg++, nexttx = GEM_NEXTTX(nexttx)) { 49718100346SThomas Moestl #ifdef GEM_DEBUG 49842c1b001SThomas Moestl CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len " 499305f2c06SThomas Moestl "%lx, addr %#lx (%#lx)", seg, nexttx, 50042c1b001SThomas Moestl segs[seg].ds_len, segs[seg].ds_addr, 501305f2c06SThomas Moestl GEM_DMA_WRITE(sc, segs[seg].ds_addr)); 50218100346SThomas Moestl #endif 503305f2c06SThomas Moestl 504305f2c06SThomas Moestl if (segs[seg].ds_len == 0) 505305f2c06SThomas Moestl continue; 506305f2c06SThomas Moestl sc->sc_txdescs[nexttx].gd_addr = 507305f2c06SThomas Moestl GEM_DMA_WRITE(sc, segs[seg].ds_addr); 508305f2c06SThomas Moestl KASSERT(segs[seg].ds_len < GEM_TD_BUFSIZE, 509305f2c06SThomas Moestl ("gem_txdma_callback: segment size too large!")); 51042c1b001SThomas Moestl flags = segs[seg].ds_len & GEM_TD_BUFSIZE; 511305f2c06SThomas Moestl if (len == 0) { 51218100346SThomas Moestl #ifdef GEM_DEBUG 51342c1b001SThomas Moestl CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, " 514305f2c06SThomas Moestl "tx %d", seg, nexttx); 51518100346SThomas Moestl #endif 51642c1b001SThomas Moestl flags |= GEM_TD_START_OF_PACKET; 517305f2c06SThomas Moestl if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 518305f2c06SThomas Moestl sc->sc_txwin = 0; 519336cca9eSBenno Rice flags |= GEM_TD_INTERRUPT_ME; 520336cca9eSBenno Rice } 52142c1b001SThomas Moestl } 522305f2c06SThomas Moestl if (len + segs[seg].ds_len == totsz) { 52318100346SThomas Moestl #ifdef GEM_DEBUG 52442c1b001SThomas Moestl CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, " 525305f2c06SThomas Moestl "tx %d", seg, nexttx); 52618100346SThomas Moestl #endif 52742c1b001SThomas Moestl flags |= GEM_TD_END_OF_PACKET; 52842c1b001SThomas Moestl } 529305f2c06SThomas Moestl sc->sc_txdescs[nexttx].gd_flags = GEM_DMA_WRITE(sc, flags); 530305f2c06SThomas Moestl txs->txs_lastdesc = nexttx; 531305f2c06SThomas Moestl len += segs[seg].ds_len; 53242c1b001SThomas Moestl } 533305f2c06SThomas Moestl KASSERT((flags & GEM_TD_END_OF_PACKET) != 0, 534305f2c06SThomas Moestl ("gem_txdma_callback: missed end of packet!")); 53542c1b001SThomas Moestl } 53642c1b001SThomas Moestl 53742c1b001SThomas Moestl static void 53842c1b001SThomas Moestl gem_tick(arg) 53942c1b001SThomas Moestl void *arg; 54042c1b001SThomas Moestl { 54142c1b001SThomas Moestl struct gem_softc *sc = arg; 54242c1b001SThomas Moestl 5431f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 54442c1b001SThomas Moestl mii_tick(sc->sc_mii); 54542c1b001SThomas Moestl 5468cb37876SMarius Strobl if (gem_watchdog(sc) == EJUSTRETURN) 5478cb37876SMarius Strobl return; 5488cb37876SMarius Strobl 54942c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 55042c1b001SThomas Moestl } 55142c1b001SThomas Moestl 55242c1b001SThomas Moestl static int 55342c1b001SThomas Moestl gem_bitwait(sc, r, clr, set) 55442c1b001SThomas Moestl struct gem_softc *sc; 55542c1b001SThomas Moestl bus_addr_t r; 55642c1b001SThomas Moestl u_int32_t clr; 55742c1b001SThomas Moestl u_int32_t set; 55842c1b001SThomas Moestl { 55942c1b001SThomas Moestl int i; 56042c1b001SThomas Moestl u_int32_t reg; 56142c1b001SThomas Moestl 56242c1b001SThomas Moestl for (i = TRIES; i--; DELAY(100)) { 563e1bb13cdSPoul-Henning Kamp reg = bus_read_4(sc->sc_res[0], r); 56442c1b001SThomas Moestl if ((r & clr) == 0 && (r & set) == set) 56542c1b001SThomas Moestl return (1); 56642c1b001SThomas Moestl } 56742c1b001SThomas Moestl return (0); 56842c1b001SThomas Moestl } 56942c1b001SThomas Moestl 57042c1b001SThomas Moestl void 57142c1b001SThomas Moestl gem_reset(sc) 57242c1b001SThomas Moestl struct gem_softc *sc; 57342c1b001SThomas Moestl { 57442c1b001SThomas Moestl 57518100346SThomas Moestl #ifdef GEM_DEBUG 57642c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev)); 57718100346SThomas Moestl #endif 57842c1b001SThomas Moestl gem_reset_rx(sc); 57942c1b001SThomas Moestl gem_reset_tx(sc); 58042c1b001SThomas Moestl 58142c1b001SThomas Moestl /* Do a full reset */ 582e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 58342c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 58442c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset device\n"); 58542c1b001SThomas Moestl } 58642c1b001SThomas Moestl 58742c1b001SThomas Moestl 58842c1b001SThomas Moestl /* 58942c1b001SThomas Moestl * gem_rxdrain: 59042c1b001SThomas Moestl * 59142c1b001SThomas Moestl * Drain the receive queue. 59242c1b001SThomas Moestl */ 59342c1b001SThomas Moestl static void 59442c1b001SThomas Moestl gem_rxdrain(sc) 59542c1b001SThomas Moestl struct gem_softc *sc; 59642c1b001SThomas Moestl { 59742c1b001SThomas Moestl struct gem_rxsoft *rxs; 59842c1b001SThomas Moestl int i; 59942c1b001SThomas Moestl 60042c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 60142c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 60242c1b001SThomas Moestl if (rxs->rxs_mbuf != NULL) { 603b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 604b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 605305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 60642c1b001SThomas Moestl m_freem(rxs->rxs_mbuf); 60742c1b001SThomas Moestl rxs->rxs_mbuf = NULL; 60842c1b001SThomas Moestl } 60942c1b001SThomas Moestl } 61042c1b001SThomas Moestl } 61142c1b001SThomas Moestl 61242c1b001SThomas Moestl /* 61342c1b001SThomas Moestl * Reset the whole thing. 61442c1b001SThomas Moestl */ 61542c1b001SThomas Moestl static void 61642c1b001SThomas Moestl gem_stop(ifp, disable) 61742c1b001SThomas Moestl struct ifnet *ifp; 61842c1b001SThomas Moestl int disable; 61942c1b001SThomas Moestl { 62042c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 62142c1b001SThomas Moestl struct gem_txsoft *txs; 62242c1b001SThomas Moestl 62318100346SThomas Moestl #ifdef GEM_DEBUG 62442c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev)); 62518100346SThomas Moestl #endif 62642c1b001SThomas Moestl 62742c1b001SThomas Moestl callout_stop(&sc->sc_tick_ch); 6281f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 6291f317bf9SMarius Strobl callout_stop(&sc->sc_rx_ch); 6301f317bf9SMarius Strobl #endif 63142c1b001SThomas Moestl 63242c1b001SThomas Moestl /* XXX - Should we reset these instead? */ 63342c1b001SThomas Moestl gem_disable_tx(sc); 63442c1b001SThomas Moestl gem_disable_rx(sc); 63542c1b001SThomas Moestl 63642c1b001SThomas Moestl /* 63742c1b001SThomas Moestl * Release any queued transmit buffers. 63842c1b001SThomas Moestl */ 63942c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 64042c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 64142c1b001SThomas Moestl if (txs->txs_ndescs != 0) { 642b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 643b2d59f42SThomas Moestl BUS_DMASYNC_POSTWRITE); 644305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 64542c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 64642c1b001SThomas Moestl m_freem(txs->txs_mbuf); 64742c1b001SThomas Moestl txs->txs_mbuf = NULL; 64842c1b001SThomas Moestl } 64942c1b001SThomas Moestl } 65042c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 65142c1b001SThomas Moestl } 65242c1b001SThomas Moestl 65342c1b001SThomas Moestl if (disable) 65442c1b001SThomas Moestl gem_rxdrain(sc); 65542c1b001SThomas Moestl 65642c1b001SThomas Moestl /* 65742c1b001SThomas Moestl * Mark the interface down and cancel the watchdog timer. 65842c1b001SThomas Moestl */ 65913f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 6608cb37876SMarius Strobl sc->sc_wdog_timer = 0; 66142c1b001SThomas Moestl } 66242c1b001SThomas Moestl 66342c1b001SThomas Moestl /* 66442c1b001SThomas Moestl * Reset the receiver 66542c1b001SThomas Moestl */ 66642c1b001SThomas Moestl int 66742c1b001SThomas Moestl gem_reset_rx(sc) 66842c1b001SThomas Moestl struct gem_softc *sc; 66942c1b001SThomas Moestl { 67042c1b001SThomas Moestl 67142c1b001SThomas Moestl /* 67242c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 67342c1b001SThomas Moestl * disable DMA first. 67442c1b001SThomas Moestl */ 67542c1b001SThomas Moestl gem_disable_rx(sc); 676e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_CONFIG, 0); 67742c1b001SThomas Moestl /* Wait till it finishes */ 67842c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0)) 67942c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot disable read dma\n"); 68042c1b001SThomas Moestl 68142c1b001SThomas Moestl /* Wait 5ms extra. */ 68242c1b001SThomas Moestl DELAY(5000); 68342c1b001SThomas Moestl 68442c1b001SThomas Moestl /* Finally, reset the ERX */ 685e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RESET, GEM_RESET_RX); 68642c1b001SThomas Moestl /* Wait till it finishes */ 68742c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 68842c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 68942c1b001SThomas Moestl return (1); 69042c1b001SThomas Moestl } 69142c1b001SThomas Moestl return (0); 69242c1b001SThomas Moestl } 69342c1b001SThomas Moestl 69442c1b001SThomas Moestl 69542c1b001SThomas Moestl /* 69642c1b001SThomas Moestl * Reset the transmitter 69742c1b001SThomas Moestl */ 69842c1b001SThomas Moestl static int 69942c1b001SThomas Moestl gem_reset_tx(sc) 70042c1b001SThomas Moestl struct gem_softc *sc; 70142c1b001SThomas Moestl { 70242c1b001SThomas Moestl int i; 70342c1b001SThomas Moestl 70442c1b001SThomas Moestl /* 70542c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 70642c1b001SThomas Moestl * disable DMA first. 70742c1b001SThomas Moestl */ 70842c1b001SThomas Moestl gem_disable_tx(sc); 709e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_TX_CONFIG, 0); 71042c1b001SThomas Moestl /* Wait till it finishes */ 71142c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0)) 71242c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot disable read dma\n"); 71342c1b001SThomas Moestl 71442c1b001SThomas Moestl /* Wait 5ms extra. */ 71542c1b001SThomas Moestl DELAY(5000); 71642c1b001SThomas Moestl 71742c1b001SThomas Moestl /* Finally, reset the ETX */ 718e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RESET, GEM_RESET_TX); 71942c1b001SThomas Moestl /* Wait till it finishes */ 72042c1b001SThomas Moestl for (i = TRIES; i--; DELAY(100)) 721e1bb13cdSPoul-Henning Kamp if ((bus_read_4(sc->sc_res[0], GEM_RESET) & GEM_RESET_TX) == 0) 72242c1b001SThomas Moestl break; 72342c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 72442c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 72542c1b001SThomas Moestl return (1); 72642c1b001SThomas Moestl } 72742c1b001SThomas Moestl return (0); 72842c1b001SThomas Moestl } 72942c1b001SThomas Moestl 73042c1b001SThomas Moestl /* 73142c1b001SThomas Moestl * disable receiver. 73242c1b001SThomas Moestl */ 73342c1b001SThomas Moestl static int 73442c1b001SThomas Moestl gem_disable_rx(sc) 73542c1b001SThomas Moestl struct gem_softc *sc; 73642c1b001SThomas Moestl { 73742c1b001SThomas Moestl u_int32_t cfg; 73842c1b001SThomas Moestl 73942c1b001SThomas Moestl /* Flip the enable bit */ 740e1bb13cdSPoul-Henning Kamp cfg = bus_read_4(sc->sc_res[0], GEM_MAC_RX_CONFIG); 74142c1b001SThomas Moestl cfg &= ~GEM_MAC_RX_ENABLE; 742e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_CONFIG, cfg); 74342c1b001SThomas Moestl 74442c1b001SThomas Moestl /* Wait for it to finish */ 74542c1b001SThomas Moestl return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)); 74642c1b001SThomas Moestl } 74742c1b001SThomas Moestl 74842c1b001SThomas Moestl /* 74942c1b001SThomas Moestl * disable transmitter. 75042c1b001SThomas Moestl */ 75142c1b001SThomas Moestl static int 75242c1b001SThomas Moestl gem_disable_tx(sc) 75342c1b001SThomas Moestl struct gem_softc *sc; 75442c1b001SThomas Moestl { 75542c1b001SThomas Moestl u_int32_t cfg; 75642c1b001SThomas Moestl 75742c1b001SThomas Moestl /* Flip the enable bit */ 758e1bb13cdSPoul-Henning Kamp cfg = bus_read_4(sc->sc_res[0], GEM_MAC_TX_CONFIG); 75942c1b001SThomas Moestl cfg &= ~GEM_MAC_TX_ENABLE; 760e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_TX_CONFIG, cfg); 76142c1b001SThomas Moestl 76242c1b001SThomas Moestl /* Wait for it to finish */ 76342c1b001SThomas Moestl return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)); 76442c1b001SThomas Moestl } 76542c1b001SThomas Moestl 76642c1b001SThomas Moestl /* 76742c1b001SThomas Moestl * Initialize interface. 76842c1b001SThomas Moestl */ 76942c1b001SThomas Moestl static int 77042c1b001SThomas Moestl gem_meminit(sc) 77142c1b001SThomas Moestl struct gem_softc *sc; 77242c1b001SThomas Moestl { 77342c1b001SThomas Moestl struct gem_rxsoft *rxs; 77442c1b001SThomas Moestl int i, error; 77542c1b001SThomas Moestl 77642c1b001SThomas Moestl /* 77742c1b001SThomas Moestl * Initialize the transmit descriptor ring. 77842c1b001SThomas Moestl */ 77942c1b001SThomas Moestl for (i = 0; i < GEM_NTXDESC; i++) { 78042c1b001SThomas Moestl sc->sc_txdescs[i].gd_flags = 0; 78142c1b001SThomas Moestl sc->sc_txdescs[i].gd_addr = 0; 78242c1b001SThomas Moestl } 783305f2c06SThomas Moestl sc->sc_txfree = GEM_MAXTXFREE; 78442c1b001SThomas Moestl sc->sc_txnext = 0; 785336cca9eSBenno Rice sc->sc_txwin = 0; 78642c1b001SThomas Moestl 78742c1b001SThomas Moestl /* 78842c1b001SThomas Moestl * Initialize the receive descriptor and receive job 78942c1b001SThomas Moestl * descriptor rings. 79042c1b001SThomas Moestl */ 79142c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 79242c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 79342c1b001SThomas Moestl if (rxs->rxs_mbuf == NULL) { 79442c1b001SThomas Moestl if ((error = gem_add_rxbuf(sc, i)) != 0) { 79542c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to " 79642c1b001SThomas Moestl "allocate or map rx buffer %d, error = " 79742c1b001SThomas Moestl "%d\n", i, error); 79842c1b001SThomas Moestl /* 79942c1b001SThomas Moestl * XXX Should attempt to run with fewer receive 80042c1b001SThomas Moestl * XXX buffers instead of just failing. 80142c1b001SThomas Moestl */ 80242c1b001SThomas Moestl gem_rxdrain(sc); 80342c1b001SThomas Moestl return (1); 80442c1b001SThomas Moestl } 80542c1b001SThomas Moestl } else 80642c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 80742c1b001SThomas Moestl } 80842c1b001SThomas Moestl sc->sc_rxptr = 0; 809b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 810b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD); 81142c1b001SThomas Moestl 81242c1b001SThomas Moestl return (0); 81342c1b001SThomas Moestl } 81442c1b001SThomas Moestl 81542c1b001SThomas Moestl static int 81642c1b001SThomas Moestl gem_ringsize(sz) 81742c1b001SThomas Moestl int sz; 81842c1b001SThomas Moestl { 81942c1b001SThomas Moestl int v = 0; 82042c1b001SThomas Moestl 82142c1b001SThomas Moestl switch (sz) { 82242c1b001SThomas Moestl case 32: 82342c1b001SThomas Moestl v = GEM_RING_SZ_32; 82442c1b001SThomas Moestl break; 82542c1b001SThomas Moestl case 64: 82642c1b001SThomas Moestl v = GEM_RING_SZ_64; 82742c1b001SThomas Moestl break; 82842c1b001SThomas Moestl case 128: 82942c1b001SThomas Moestl v = GEM_RING_SZ_128; 83042c1b001SThomas Moestl break; 83142c1b001SThomas Moestl case 256: 83242c1b001SThomas Moestl v = GEM_RING_SZ_256; 83342c1b001SThomas Moestl break; 83442c1b001SThomas Moestl case 512: 83542c1b001SThomas Moestl v = GEM_RING_SZ_512; 83642c1b001SThomas Moestl break; 83742c1b001SThomas Moestl case 1024: 83842c1b001SThomas Moestl v = GEM_RING_SZ_1024; 83942c1b001SThomas Moestl break; 84042c1b001SThomas Moestl case 2048: 84142c1b001SThomas Moestl v = GEM_RING_SZ_2048; 84242c1b001SThomas Moestl break; 84342c1b001SThomas Moestl case 4096: 84442c1b001SThomas Moestl v = GEM_RING_SZ_4096; 84542c1b001SThomas Moestl break; 84642c1b001SThomas Moestl case 8192: 84742c1b001SThomas Moestl v = GEM_RING_SZ_8192; 84842c1b001SThomas Moestl break; 84942c1b001SThomas Moestl default: 85042c1b001SThomas Moestl printf("gem: invalid Receive Descriptor ring size\n"); 85142c1b001SThomas Moestl break; 85242c1b001SThomas Moestl } 85342c1b001SThomas Moestl return (v); 85442c1b001SThomas Moestl } 85542c1b001SThomas Moestl 85642c1b001SThomas Moestl static void 85742c1b001SThomas Moestl gem_init(xsc) 85842c1b001SThomas Moestl void *xsc; 85942c1b001SThomas Moestl { 86042c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)xsc; 8618cfaff7dSMarius Strobl 8628cfaff7dSMarius Strobl GEM_LOCK(sc); 8638cfaff7dSMarius Strobl gem_init_locked(sc); 8648cfaff7dSMarius Strobl GEM_UNLOCK(sc); 8658cfaff7dSMarius Strobl } 8668cfaff7dSMarius Strobl 8678cfaff7dSMarius Strobl /* 8688cfaff7dSMarius Strobl * Initialization of interface; set up initialization block 8698cfaff7dSMarius Strobl * and transmit/receive descriptor rings. 8708cfaff7dSMarius Strobl */ 8718cfaff7dSMarius Strobl static void 8728cfaff7dSMarius Strobl gem_init_locked(sc) 8738cfaff7dSMarius Strobl struct gem_softc *sc; 8748cfaff7dSMarius Strobl { 875fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 87642c1b001SThomas Moestl u_int32_t v; 87742c1b001SThomas Moestl 8788cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 87942c1b001SThomas Moestl 88018100346SThomas Moestl #ifdef GEM_DEBUG 88142c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev)); 88218100346SThomas Moestl #endif 88342c1b001SThomas Moestl /* 88442c1b001SThomas Moestl * Initialization sequence. The numbered steps below correspond 88542c1b001SThomas Moestl * to the sequence outlined in section 6.3.5.1 in the Ethernet 88642c1b001SThomas Moestl * Channel Engine manual (part of the PCIO manual). 88742c1b001SThomas Moestl * See also the STP2002-STQ document from Sun Microsystems. 88842c1b001SThomas Moestl */ 88942c1b001SThomas Moestl 89042c1b001SThomas Moestl /* step 1 & 2. Reset the Ethernet Channel */ 891fc74a9f9SBrooks Davis gem_stop(sc->sc_ifp, 0); 89242c1b001SThomas Moestl gem_reset(sc); 89318100346SThomas Moestl #ifdef GEM_DEBUG 89442c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev)); 89518100346SThomas Moestl #endif 89642c1b001SThomas Moestl 89742c1b001SThomas Moestl /* Re-initialize the MIF */ 89842c1b001SThomas Moestl gem_mifinit(sc); 89942c1b001SThomas Moestl 90042c1b001SThomas Moestl /* step 3. Setup data structures in host memory */ 90142c1b001SThomas Moestl gem_meminit(sc); 90242c1b001SThomas Moestl 90342c1b001SThomas Moestl /* step 4. TX MAC registers & counters */ 90442c1b001SThomas Moestl gem_init_regs(sc); 90542c1b001SThomas Moestl 90642c1b001SThomas Moestl /* step 5. RX MAC registers & counters */ 90742c1b001SThomas Moestl gem_setladrf(sc); 90842c1b001SThomas Moestl 90942c1b001SThomas Moestl /* step 6 & 7. Program Descriptor Ring Base Addresses */ 91042c1b001SThomas Moestl /* NOTE: we use only 32-bit DMA addresses here. */ 911e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_TX_RING_PTR_HI, 0); 912e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 91342c1b001SThomas Moestl 914e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_RING_PTR_HI, 0); 915e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 91618100346SThomas Moestl #ifdef GEM_DEBUG 91742c1b001SThomas Moestl CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx", 91842c1b001SThomas Moestl GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 91918100346SThomas Moestl #endif 92042c1b001SThomas Moestl 92142c1b001SThomas Moestl /* step 8. Global Configuration & Interrupt Mask */ 922e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_INTMASK, 92342c1b001SThomas Moestl ~(GEM_INTR_TX_INTME| 92442c1b001SThomas Moestl GEM_INTR_TX_EMPTY| 92542c1b001SThomas Moestl GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF| 92642c1b001SThomas Moestl GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS| 92742c1b001SThomas Moestl GEM_INTR_MAC_CONTROL|GEM_INTR_MIF| 92842c1b001SThomas Moestl GEM_INTR_BERR)); 929e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_MASK, 930336cca9eSBenno Rice GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT); 931e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_TX_MASK, 0xffff); /* XXXX */ 932e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_CONTROL_MASK, 0); /* XXXX */ 93342c1b001SThomas Moestl 93442c1b001SThomas Moestl /* step 9. ETX Configuration: use mostly default values */ 93542c1b001SThomas Moestl 93642c1b001SThomas Moestl /* Enable DMA */ 93742c1b001SThomas Moestl v = gem_ringsize(GEM_NTXDESC /*XXX*/); 938e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_TX_CONFIG, 93942c1b001SThomas Moestl v|GEM_TX_CONFIG_TXDMA_EN| 94042c1b001SThomas Moestl ((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH)); 94142c1b001SThomas Moestl 94242c1b001SThomas Moestl /* step 10. ERX Configuration */ 94342c1b001SThomas Moestl 94442c1b001SThomas Moestl /* Encode Receive Descriptor ring size: four possible values */ 94542c1b001SThomas Moestl v = gem_ringsize(GEM_NRXDESC /*XXX*/); 94642c1b001SThomas Moestl 94742c1b001SThomas Moestl /* Enable DMA */ 948e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_CONFIG, 94942c1b001SThomas Moestl v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)| 95042c1b001SThomas Moestl (2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN| 95142c1b001SThomas Moestl (0<<GEM_RX_CONFIG_CXM_START_SHFT)); 95242c1b001SThomas Moestl /* 953336cca9eSBenno Rice * The following value is for an OFF Threshold of about 3/4 full 954336cca9eSBenno Rice * and an ON Threshold of 1/4 full. 95542c1b001SThomas Moestl */ 956e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_PAUSE_THRESH, 957336cca9eSBenno Rice (3 * sc->sc_rxfifosize / 256) | 958336cca9eSBenno Rice ( (sc->sc_rxfifosize / 256) << 12)); 959e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_BLANKING, (6<<12)|6); 96042c1b001SThomas Moestl 96142c1b001SThomas Moestl /* step 11. Configure Media */ 962336cca9eSBenno Rice mii_mediachg(sc->sc_mii); 96342c1b001SThomas Moestl 96442c1b001SThomas Moestl /* step 12. RX_MAC Configuration Register */ 965e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MAC_RX_CONFIG); 96642c1b001SThomas Moestl v |= GEM_MAC_RX_ENABLE; 967e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_CONFIG, v); 96842c1b001SThomas Moestl 96942c1b001SThomas Moestl /* step 14. Issue Transmit Pending command */ 97042c1b001SThomas Moestl 97142c1b001SThomas Moestl /* step 15. Give the reciever a swift kick */ 972e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_KICK, GEM_NRXDESC-4); 97342c1b001SThomas Moestl 97442c1b001SThomas Moestl /* Start the one second timer. */ 9758cb37876SMarius Strobl sc->sc_wdog_timer = 0; 97642c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 97742c1b001SThomas Moestl 97813f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 97913f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 980336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 98142c1b001SThomas Moestl } 98242c1b001SThomas Moestl 98342c1b001SThomas Moestl static int 984305f2c06SThomas Moestl gem_load_txmbuf(sc, m0) 98542c1b001SThomas Moestl struct gem_softc *sc; 98642c1b001SThomas Moestl struct mbuf *m0; 98742c1b001SThomas Moestl { 98842c1b001SThomas Moestl struct gem_txdma txd; 98942c1b001SThomas Moestl struct gem_txsoft *txs; 990305f2c06SThomas Moestl int error; 99142c1b001SThomas Moestl 99242c1b001SThomas Moestl /* Get a work queue entry. */ 99342c1b001SThomas Moestl if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 994305f2c06SThomas Moestl /* Ran out of descriptors. */ 995305f2c06SThomas Moestl return (-1); 996305f2c06SThomas Moestl } 997305f2c06SThomas Moestl txd.txd_sc = sc; 998305f2c06SThomas Moestl txd.txd_txs = txs; 999305f2c06SThomas Moestl txs->txs_firstdesc = sc->sc_txnext; 1000305f2c06SThomas Moestl error = bus_dmamap_load_mbuf(sc->sc_tdmatag, txs->txs_dmamap, m0, 1001305f2c06SThomas Moestl gem_txdma_callback, &txd, BUS_DMA_NOWAIT); 1002305f2c06SThomas Moestl if (error != 0) 1003305f2c06SThomas Moestl goto fail; 1004305f2c06SThomas Moestl if (txs->txs_ndescs == -1) { 100542c1b001SThomas Moestl error = -1; 100642c1b001SThomas Moestl goto fail; 100742c1b001SThomas Moestl } 1008305f2c06SThomas Moestl 100942c1b001SThomas Moestl /* Sync the DMA map. */ 1010305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 101142c1b001SThomas Moestl BUS_DMASYNC_PREWRITE); 1012305f2c06SThomas Moestl 101318100346SThomas Moestl #ifdef GEM_DEBUG 101442c1b001SThomas Moestl CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, " 101542c1b001SThomas Moestl "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc, 101642c1b001SThomas Moestl txs->txs_ndescs); 101718100346SThomas Moestl #endif 101842c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 1019305f2c06SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 1020c3d5598aSMarius Strobl txs->txs_mbuf = m0; 1021305f2c06SThomas Moestl 1022305f2c06SThomas Moestl sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc); 1023305f2c06SThomas Moestl sc->sc_txfree -= txs->txs_ndescs; 102442c1b001SThomas Moestl return (0); 102542c1b001SThomas Moestl 102642c1b001SThomas Moestl fail: 102718100346SThomas Moestl #ifdef GEM_DEBUG 1028305f2c06SThomas Moestl CTR1(KTR_GEM, "gem_load_txmbuf failed (%d)", error); 102918100346SThomas Moestl #endif 1030305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 103142c1b001SThomas Moestl return (error); 103242c1b001SThomas Moestl } 103342c1b001SThomas Moestl 103442c1b001SThomas Moestl static void 103542c1b001SThomas Moestl gem_init_regs(sc) 103642c1b001SThomas Moestl struct gem_softc *sc; 103742c1b001SThomas Moestl { 10384a0d6638SRuslan Ermilov const u_char *laddr = IF_LLADDR(sc->sc_ifp); 1039336cca9eSBenno Rice u_int32_t v; 104042c1b001SThomas Moestl 104142c1b001SThomas Moestl /* These regs are not cleared on reset */ 104242c1b001SThomas Moestl if (!sc->sc_inited) { 104342c1b001SThomas Moestl 104442c1b001SThomas Moestl /* Wooo. Magic values. */ 1045e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_IPG0, 0); 1046e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_IPG1, 8); 1047e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_IPG2, 4); 104842c1b001SThomas Moestl 1049e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 105042c1b001SThomas Moestl /* Max frame and max burst size */ 1051e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_MAC_MAX_FRAME, 105200d12766SMarius Strobl (ETHER_MAX_LEN + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN) | 105300d12766SMarius Strobl (0x2000 << 16)); 1054336cca9eSBenno Rice 1055e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_PREAMBLE_LEN, 0x7); 1056e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_JAM_SIZE, 0x4); 1057e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ATTEMPT_LIMIT, 0x10); 105842c1b001SThomas Moestl /* Dunno.... */ 1059e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_CONTROL_TYPE, 0x8088); 1060e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RANDOM_SEED, 1061336cca9eSBenno Rice ((laddr[5]<<8)|laddr[4])&0x3ff); 1062336cca9eSBenno Rice 106342c1b001SThomas Moestl /* Secondary MAC addr set to 0:0:0:0:0:0 */ 1064e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR3, 0); 1065e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR4, 0); 1066e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR5, 0); 1067336cca9eSBenno Rice 1068336cca9eSBenno Rice /* MAC control addr set to 01:80:c2:00:00:01 */ 1069e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR6, 0x0001); 1070e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR7, 0xc200); 1071e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR8, 0x0180); 107242c1b001SThomas Moestl 107342c1b001SThomas Moestl /* MAC filter addr set to 0:0:0:0:0:0 */ 1074e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR_FILTER0, 0); 1075e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR_FILTER1, 0); 1076e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR_FILTER2, 0); 107742c1b001SThomas Moestl 1078e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADR_FLT_MASK1_2, 0); 1079e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADR_FLT_MASK0, 0); 108042c1b001SThomas Moestl 108142c1b001SThomas Moestl sc->sc_inited = 1; 108242c1b001SThomas Moestl } 108342c1b001SThomas Moestl 108442c1b001SThomas Moestl /* Counters need to be zeroed */ 1085e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_NORM_COLL_CNT, 0); 1086e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_FIRST_COLL_CNT, 0); 1087e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_EXCESS_COLL_CNT, 0); 1088e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_LATE_COLL_CNT, 0); 1089e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_DEFER_TMR_CNT, 0); 1090e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_PEAK_ATTEMPTS, 0); 1091e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_FRAME_COUNT, 0); 1092e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_LEN_ERR_CNT, 0); 1093e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_ALIGN_ERR, 0); 1094e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_CRC_ERR_CNT, 0); 1095e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_CODE_VIOL, 0); 109642c1b001SThomas Moestl 109742c1b001SThomas Moestl /* Un-pause stuff */ 109842c1b001SThomas Moestl #if 0 1099e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 110042c1b001SThomas Moestl #else 1101e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_SEND_PAUSE_CMD, 0); 110242c1b001SThomas Moestl #endif 110342c1b001SThomas Moestl 110442c1b001SThomas Moestl /* 110542c1b001SThomas Moestl * Set the station address. 110642c1b001SThomas Moestl */ 1107e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]); 1108e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]); 1109e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]); 1110336cca9eSBenno Rice 1111336cca9eSBenno Rice /* 1112336cca9eSBenno Rice * Enable MII outputs. Enable GMII if there is a gigabit PHY. 1113336cca9eSBenno Rice */ 1114e1bb13cdSPoul-Henning Kamp sc->sc_mif_config = bus_read_4(sc->sc_res[0], GEM_MIF_CONFIG); 1115336cca9eSBenno Rice v = GEM_MAC_XIF_TX_MII_ENA; 1116336cca9eSBenno Rice if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) { 1117336cca9eSBenno Rice v |= GEM_MAC_XIF_FDPLX_LED; 1118336cca9eSBenno Rice if (sc->sc_flags & GEM_GIGABIT) 1119336cca9eSBenno Rice v |= GEM_MAC_XIF_GMII_MODE; 1120336cca9eSBenno Rice } 1121e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_XIF_CONFIG, v); 112242c1b001SThomas Moestl } 112342c1b001SThomas Moestl 112442c1b001SThomas Moestl static void 112542c1b001SThomas Moestl gem_start(ifp) 112642c1b001SThomas Moestl struct ifnet *ifp; 112742c1b001SThomas Moestl { 112842c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 11298cfaff7dSMarius Strobl 11308cfaff7dSMarius Strobl GEM_LOCK(sc); 11318cfaff7dSMarius Strobl gem_start_locked(ifp); 11328cfaff7dSMarius Strobl GEM_UNLOCK(sc); 11338cfaff7dSMarius Strobl } 11348cfaff7dSMarius Strobl 11358cfaff7dSMarius Strobl static void 11368cfaff7dSMarius Strobl gem_start_locked(ifp) 11378cfaff7dSMarius Strobl struct ifnet *ifp; 11388cfaff7dSMarius Strobl { 11398cfaff7dSMarius Strobl struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 1140305f2c06SThomas Moestl struct mbuf *m0 = NULL; 114118100346SThomas Moestl int firsttx, ntx = 0, ofree, txmfail; 114242c1b001SThomas Moestl 114313f4c340SRobert Watson if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 114413f4c340SRobert Watson IFF_DRV_RUNNING) 114542c1b001SThomas Moestl return; 114642c1b001SThomas Moestl 114742c1b001SThomas Moestl /* 114842c1b001SThomas Moestl * Remember the previous number of free descriptors and 114942c1b001SThomas Moestl * the first descriptor we'll use. 115042c1b001SThomas Moestl */ 115142c1b001SThomas Moestl ofree = sc->sc_txfree; 115242c1b001SThomas Moestl firsttx = sc->sc_txnext; 115342c1b001SThomas Moestl 115418100346SThomas Moestl #ifdef GEM_DEBUG 115542c1b001SThomas Moestl CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d", 115642c1b001SThomas Moestl device_get_name(sc->sc_dev), ofree, firsttx); 115718100346SThomas Moestl #endif 115842c1b001SThomas Moestl 115942c1b001SThomas Moestl /* 116042c1b001SThomas Moestl * Loop through the send queue, setting up transmit descriptors 116142c1b001SThomas Moestl * until we drain the queue, or use up all available transmit 116242c1b001SThomas Moestl * descriptors. 116342c1b001SThomas Moestl */ 116442c1b001SThomas Moestl txmfail = 0; 116518100346SThomas Moestl do { 116642c1b001SThomas Moestl /* 116742c1b001SThomas Moestl * Grab a packet off the queue. 116842c1b001SThomas Moestl */ 116942c1b001SThomas Moestl IF_DEQUEUE(&ifp->if_snd, m0); 117042c1b001SThomas Moestl if (m0 == NULL) 117142c1b001SThomas Moestl break; 117242c1b001SThomas Moestl 1173305f2c06SThomas Moestl txmfail = gem_load_txmbuf(sc, m0); 1174305f2c06SThomas Moestl if (txmfail > 0) { 1175305f2c06SThomas Moestl /* Drop the mbuf and complain. */ 1176305f2c06SThomas Moestl printf("gem_start: error %d while loading mbuf dma " 1177305f2c06SThomas Moestl "map\n", txmfail); 1178305f2c06SThomas Moestl continue; 1179305f2c06SThomas Moestl } 1180305f2c06SThomas Moestl /* Not enough descriptors. */ 118142c1b001SThomas Moestl if (txmfail == -1) { 1182305f2c06SThomas Moestl if (sc->sc_txfree == GEM_MAXTXFREE) 1183305f2c06SThomas Moestl panic("gem_start: mbuf chain too long!"); 118442c1b001SThomas Moestl IF_PREPEND(&ifp->if_snd, m0); 118542c1b001SThomas Moestl break; 118642c1b001SThomas Moestl } 118742c1b001SThomas Moestl 118818100346SThomas Moestl ntx++; 1189305f2c06SThomas Moestl /* Kick the transmitter. */ 119018100346SThomas Moestl #ifdef GEM_DEBUG 1191305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: gem_start: kicking tx %d", 1192305f2c06SThomas Moestl device_get_name(sc->sc_dev), sc->sc_txnext); 119318100346SThomas Moestl #endif 1194e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_TX_KICK, 119542c1b001SThomas Moestl sc->sc_txnext); 119642c1b001SThomas Moestl 1197ff046a6cSSam Leffler BPF_MTAP(ifp, m0); 119818100346SThomas Moestl } while (1); 1199305f2c06SThomas Moestl 1200305f2c06SThomas Moestl if (txmfail == -1 || sc->sc_txfree == 0) { 1201305f2c06SThomas Moestl /* No more slots left; notify upper layer. */ 120213f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1203305f2c06SThomas Moestl } 1204305f2c06SThomas Moestl 1205305f2c06SThomas Moestl if (ntx > 0) { 1206b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 1207b2d59f42SThomas Moestl 120818100346SThomas Moestl #ifdef GEM_DEBUG 1209305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d", 1210305f2c06SThomas Moestl device_get_name(sc->sc_dev), firsttx); 121118100346SThomas Moestl #endif 1212305f2c06SThomas Moestl 121342c1b001SThomas Moestl /* Set a watchdog timer in case the chip flakes out. */ 12148cb37876SMarius Strobl sc->sc_wdog_timer = 5; 121518100346SThomas Moestl #ifdef GEM_DEBUG 121642c1b001SThomas Moestl CTR2(KTR_GEM, "%s: gem_start: watchdog %d", 12178cb37876SMarius Strobl device_get_name(sc->sc_dev), sc->sc_wdog_timer); 121818100346SThomas Moestl #endif 121942c1b001SThomas Moestl } 122042c1b001SThomas Moestl } 122142c1b001SThomas Moestl 122242c1b001SThomas Moestl /* 122342c1b001SThomas Moestl * Transmit interrupt. 122442c1b001SThomas Moestl */ 122542c1b001SThomas Moestl static void 122642c1b001SThomas Moestl gem_tint(sc) 122742c1b001SThomas Moestl struct gem_softc *sc; 122842c1b001SThomas Moestl { 1229fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 123042c1b001SThomas Moestl struct gem_txsoft *txs; 123142c1b001SThomas Moestl int txlast; 1232336cca9eSBenno Rice int progress = 0; 123342c1b001SThomas Moestl 123442c1b001SThomas Moestl 123518100346SThomas Moestl #ifdef GEM_DEBUG 123642c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev)); 123718100346SThomas Moestl #endif 123842c1b001SThomas Moestl 123942c1b001SThomas Moestl /* 124042c1b001SThomas Moestl * Unload collision counters 124142c1b001SThomas Moestl */ 124242c1b001SThomas Moestl ifp->if_collisions += 1243e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_NORM_COLL_CNT) + 1244e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_FIRST_COLL_CNT) + 1245e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_EXCESS_COLL_CNT) + 1246e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_LATE_COLL_CNT); 124742c1b001SThomas Moestl 124842c1b001SThomas Moestl /* 124942c1b001SThomas Moestl * then clear the hardware counters. 125042c1b001SThomas Moestl */ 1251e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_NORM_COLL_CNT, 0); 1252e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_FIRST_COLL_CNT, 0); 1253e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_EXCESS_COLL_CNT, 0); 1254e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_LATE_COLL_CNT, 0); 125542c1b001SThomas Moestl 125642c1b001SThomas Moestl /* 125742c1b001SThomas Moestl * Go through our Tx list and free mbufs for those 125842c1b001SThomas Moestl * frames that have been transmitted. 125942c1b001SThomas Moestl */ 1260b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 126142c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 126242c1b001SThomas Moestl 126342c1b001SThomas Moestl #ifdef GEM_DEBUG 126442c1b001SThomas Moestl if (ifp->if_flags & IFF_DEBUG) { 126542c1b001SThomas Moestl int i; 126642c1b001SThomas Moestl printf(" txsoft %p transmit chain:\n", txs); 126742c1b001SThomas Moestl for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 126842c1b001SThomas Moestl printf("descriptor %d: ", i); 126942c1b001SThomas Moestl printf("gd_flags: 0x%016llx\t", (long long) 127042c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags)); 127142c1b001SThomas Moestl printf("gd_addr: 0x%016llx\n", (long long) 127242c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr)); 127342c1b001SThomas Moestl if (i == txs->txs_lastdesc) 127442c1b001SThomas Moestl break; 127542c1b001SThomas Moestl } 127642c1b001SThomas Moestl } 127742c1b001SThomas Moestl #endif 127842c1b001SThomas Moestl 127942c1b001SThomas Moestl /* 128042c1b001SThomas Moestl * In theory, we could harveast some descriptors before 128142c1b001SThomas Moestl * the ring is empty, but that's a bit complicated. 128242c1b001SThomas Moestl * 128342c1b001SThomas Moestl * GEM_TX_COMPLETION points to the last descriptor 128442c1b001SThomas Moestl * processed +1. 128542c1b001SThomas Moestl */ 1286e1bb13cdSPoul-Henning Kamp txlast = bus_read_4(sc->sc_res[0], GEM_TX_COMPLETION); 128718100346SThomas Moestl #ifdef GEM_DEBUG 128842c1b001SThomas Moestl CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, " 128942c1b001SThomas Moestl "txs->txs_lastdesc = %d, txlast = %d", 129042c1b001SThomas Moestl txs->txs_firstdesc, txs->txs_lastdesc, txlast); 129118100346SThomas Moestl #endif 129242c1b001SThomas Moestl if (txs->txs_firstdesc <= txs->txs_lastdesc) { 129342c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) && 129442c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 129542c1b001SThomas Moestl break; 129642c1b001SThomas Moestl } else { 129742c1b001SThomas Moestl /* Ick -- this command wraps */ 129842c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) || 129942c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 130042c1b001SThomas Moestl break; 130142c1b001SThomas Moestl } 130242c1b001SThomas Moestl 130318100346SThomas Moestl #ifdef GEM_DEBUG 130442c1b001SThomas Moestl CTR0(KTR_GEM, "gem_tint: releasing a desc"); 130518100346SThomas Moestl #endif 130642c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 130742c1b001SThomas Moestl 130842c1b001SThomas Moestl sc->sc_txfree += txs->txs_ndescs; 130942c1b001SThomas Moestl 1310305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 131142c1b001SThomas Moestl BUS_DMASYNC_POSTWRITE); 1312305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 131342c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 131442c1b001SThomas Moestl m_freem(txs->txs_mbuf); 131542c1b001SThomas Moestl txs->txs_mbuf = NULL; 131642c1b001SThomas Moestl } 131742c1b001SThomas Moestl 131842c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 131942c1b001SThomas Moestl 132042c1b001SThomas Moestl ifp->if_opackets++; 1321336cca9eSBenno Rice progress = 1; 132242c1b001SThomas Moestl } 132342c1b001SThomas Moestl 132418100346SThomas Moestl #ifdef GEM_DEBUG 132542c1b001SThomas Moestl CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x " 132642c1b001SThomas Moestl "GEM_TX_DATA_PTR %llx " 132742c1b001SThomas Moestl "GEM_TX_COMPLETION %x", 1328e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_TX_STATE_MACHINE), 1329e1bb13cdSPoul-Henning Kamp ((long long) bus_read_4(sc->sc_res[0], 133042c1b001SThomas Moestl GEM_TX_DATA_PTR_HI) << 32) | 1331e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], 133242c1b001SThomas Moestl GEM_TX_DATA_PTR_LO), 1333e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_TX_COMPLETION)); 133418100346SThomas Moestl #endif 133542c1b001SThomas Moestl 1336336cca9eSBenno Rice if (progress) { 1337336cca9eSBenno Rice if (sc->sc_txfree == GEM_NTXDESC - 1) 1338336cca9eSBenno Rice sc->sc_txwin = 0; 133942c1b001SThomas Moestl 134013f4c340SRobert Watson /* Freed some descriptors, so reset IFF_DRV_OACTIVE and restart. */ 134113f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 13428cfaff7dSMarius Strobl gem_start_locked(ifp); 1343336cca9eSBenno Rice 13448cb37876SMarius Strobl sc->sc_wdog_timer = STAILQ_EMPTY(&sc->sc_txdirtyq) ? 0 : 5; 1345336cca9eSBenno Rice } 134642c1b001SThomas Moestl 134718100346SThomas Moestl #ifdef GEM_DEBUG 134842c1b001SThomas Moestl CTR2(KTR_GEM, "%s: gem_tint: watchdog %d", 13498cb37876SMarius Strobl device_get_name(sc->sc_dev), sc->sc_wdog_timer); 135018100346SThomas Moestl #endif 135142c1b001SThomas Moestl } 135242c1b001SThomas Moestl 1353c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 13540d80b9bdSThomas Moestl static void 13550d80b9bdSThomas Moestl gem_rint_timeout(arg) 13560d80b9bdSThomas Moestl void *arg; 13570d80b9bdSThomas Moestl { 13588cfaff7dSMarius Strobl struct gem_softc *sc = (struct gem_softc *)arg; 13590d80b9bdSThomas Moestl 13601f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 13618cfaff7dSMarius Strobl gem_rint(sc); 13620d80b9bdSThomas Moestl } 136311e3f060SJake Burkholder #endif 13640d80b9bdSThomas Moestl 136542c1b001SThomas Moestl /* 136642c1b001SThomas Moestl * Receive interrupt. 136742c1b001SThomas Moestl */ 136842c1b001SThomas Moestl static void 136942c1b001SThomas Moestl gem_rint(sc) 137042c1b001SThomas Moestl struct gem_softc *sc; 137142c1b001SThomas Moestl { 1372fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 137342c1b001SThomas Moestl struct gem_rxsoft *rxs; 137442c1b001SThomas Moestl struct mbuf *m; 137542c1b001SThomas Moestl u_int64_t rxstat; 1376336cca9eSBenno Rice u_int32_t rxcomp; 1377336cca9eSBenno Rice int i, len, progress = 0; 137842c1b001SThomas Moestl 1379c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 13800d80b9bdSThomas Moestl callout_stop(&sc->sc_rx_ch); 1381c3d5598aSMarius Strobl #endif 138218100346SThomas Moestl #ifdef GEM_DEBUG 138342c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev)); 138418100346SThomas Moestl #endif 1385336cca9eSBenno Rice 1386336cca9eSBenno Rice /* 1387336cca9eSBenno Rice * Read the completion register once. This limits 1388336cca9eSBenno Rice * how long the following loop can execute. 1389336cca9eSBenno Rice */ 1390e1bb13cdSPoul-Henning Kamp rxcomp = bus_read_4(sc->sc_res[0], GEM_RX_COMPLETION); 1391336cca9eSBenno Rice 139218100346SThomas Moestl #ifdef GEM_DEBUG 139342c1b001SThomas Moestl CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d", 1394336cca9eSBenno Rice sc->sc_rxptr, rxcomp); 139518100346SThomas Moestl #endif 1396b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 1397336cca9eSBenno Rice for (i = sc->sc_rxptr; i != rxcomp; 139842c1b001SThomas Moestl i = GEM_NEXTRX(i)) { 139942c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 140042c1b001SThomas Moestl 140142c1b001SThomas Moestl rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags); 140242c1b001SThomas Moestl 140342c1b001SThomas Moestl if (rxstat & GEM_RD_OWN) { 1404c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 140542c1b001SThomas Moestl /* 14060d80b9bdSThomas Moestl * The descriptor is still marked as owned, although 14070d80b9bdSThomas Moestl * it is supposed to have completed. This has been 14080d80b9bdSThomas Moestl * observed on some machines. Just exiting here 14090d80b9bdSThomas Moestl * might leave the packet sitting around until another 14100d80b9bdSThomas Moestl * one arrives to trigger a new interrupt, which is 14110d80b9bdSThomas Moestl * generally undesirable, so set up a timeout. 141242c1b001SThomas Moestl */ 14130d80b9bdSThomas Moestl callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 14140d80b9bdSThomas Moestl gem_rint_timeout, sc); 1415336cca9eSBenno Rice #endif 141642c1b001SThomas Moestl break; 141742c1b001SThomas Moestl } 141842c1b001SThomas Moestl 1419336cca9eSBenno Rice progress++; 1420336cca9eSBenno Rice ifp->if_ipackets++; 1421336cca9eSBenno Rice 142242c1b001SThomas Moestl if (rxstat & GEM_RD_BAD_CRC) { 1423336cca9eSBenno Rice ifp->if_ierrors++; 142442c1b001SThomas Moestl device_printf(sc->sc_dev, "receive error: CRC error\n"); 142542c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 142642c1b001SThomas Moestl continue; 142742c1b001SThomas Moestl } 142842c1b001SThomas Moestl 142942c1b001SThomas Moestl #ifdef GEM_DEBUG 143042c1b001SThomas Moestl if (ifp->if_flags & IFF_DEBUG) { 143142c1b001SThomas Moestl printf(" rxsoft %p descriptor %d: ", rxs, i); 143242c1b001SThomas Moestl printf("gd_flags: 0x%016llx\t", (long long) 143342c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags)); 143442c1b001SThomas Moestl printf("gd_addr: 0x%016llx\n", (long long) 143542c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr)); 143642c1b001SThomas Moestl } 143742c1b001SThomas Moestl #endif 143842c1b001SThomas Moestl 143942c1b001SThomas Moestl /* 144042c1b001SThomas Moestl * No errors; receive the packet. Note the Gem 144142c1b001SThomas Moestl * includes the CRC with every packet. 144242c1b001SThomas Moestl */ 144342c1b001SThomas Moestl len = GEM_RD_BUFLEN(rxstat); 144442c1b001SThomas Moestl 144542c1b001SThomas Moestl /* 144642c1b001SThomas Moestl * Allocate a new mbuf cluster. If that fails, we are 144742c1b001SThomas Moestl * out of memory, and must drop the packet and recycle 144842c1b001SThomas Moestl * the buffer that's already attached to this descriptor. 144942c1b001SThomas Moestl */ 145042c1b001SThomas Moestl m = rxs->rxs_mbuf; 145142c1b001SThomas Moestl if (gem_add_rxbuf(sc, i) != 0) { 145242c1b001SThomas Moestl ifp->if_ierrors++; 145342c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 145442c1b001SThomas Moestl continue; 145542c1b001SThomas Moestl } 145642c1b001SThomas Moestl m->m_data += 2; /* We're already off by two */ 145742c1b001SThomas Moestl 145842c1b001SThomas Moestl m->m_pkthdr.rcvif = ifp; 145942c1b001SThomas Moestl m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN; 146042c1b001SThomas Moestl 146142c1b001SThomas Moestl /* Pass it on. */ 14628cfaff7dSMarius Strobl GEM_UNLOCK(sc); 1463673d9191SSam Leffler (*ifp->if_input)(ifp, m); 14648cfaff7dSMarius Strobl GEM_LOCK(sc); 146542c1b001SThomas Moestl } 146642c1b001SThomas Moestl 1467336cca9eSBenno Rice if (progress) { 1468b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 146942c1b001SThomas Moestl /* Update the receive pointer. */ 1470336cca9eSBenno Rice if (i == sc->sc_rxptr) { 1471336cca9eSBenno Rice device_printf(sc->sc_dev, "rint: ring wrap\n"); 1472336cca9eSBenno Rice } 147342c1b001SThomas Moestl sc->sc_rxptr = i; 1474e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_RX_KICK, GEM_PREVRX(i)); 1475336cca9eSBenno Rice } 147642c1b001SThomas Moestl 147718100346SThomas Moestl #ifdef GEM_DEBUG 147842c1b001SThomas Moestl CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d", 1479e1bb13cdSPoul-Henning Kamp sc->sc_rxptr, bus_read_4(sc->sc_res[0], GEM_RX_COMPLETION)); 148018100346SThomas Moestl #endif 148142c1b001SThomas Moestl } 148242c1b001SThomas Moestl 148342c1b001SThomas Moestl 148442c1b001SThomas Moestl /* 148542c1b001SThomas Moestl * gem_add_rxbuf: 148642c1b001SThomas Moestl * 148742c1b001SThomas Moestl * Add a receive buffer to the indicated descriptor. 148842c1b001SThomas Moestl */ 148942c1b001SThomas Moestl static int 149042c1b001SThomas Moestl gem_add_rxbuf(sc, idx) 149142c1b001SThomas Moestl struct gem_softc *sc; 149242c1b001SThomas Moestl int idx; 149342c1b001SThomas Moestl { 149442c1b001SThomas Moestl struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 149542c1b001SThomas Moestl struct mbuf *m; 1496c3d5598aSMarius Strobl bus_dma_segment_t segs[1]; 1497c3d5598aSMarius Strobl int error, nsegs; 149842c1b001SThomas Moestl 1499a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 150042c1b001SThomas Moestl if (m == NULL) 150142c1b001SThomas Moestl return (ENOBUFS); 1502305f2c06SThomas Moestl m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 150342c1b001SThomas Moestl 150442c1b001SThomas Moestl #ifdef GEM_DEBUG 150542c1b001SThomas Moestl /* bzero the packet to check dma */ 150642c1b001SThomas Moestl memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 150742c1b001SThomas Moestl #endif 150842c1b001SThomas Moestl 1509b2d59f42SThomas Moestl if (rxs->rxs_mbuf != NULL) { 1510b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 1511b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 1512305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 1513b2d59f42SThomas Moestl } 151442c1b001SThomas Moestl 151542c1b001SThomas Moestl rxs->rxs_mbuf = m; 151642c1b001SThomas Moestl 1517c3d5598aSMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap, 1518c3d5598aSMarius Strobl m, segs, &nsegs, BUS_DMA_NOWAIT); 1519c3d5598aSMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1520c3d5598aSMarius Strobl KASSERT(nsegs == 1, ("Too many segments returned!")); 1521c3d5598aSMarius Strobl if (error != 0) { 152242c1b001SThomas Moestl device_printf(sc->sc_dev, "can't load rx DMA map %d, error = " 152342c1b001SThomas Moestl "%d\n", idx, error); 1524c3d5598aSMarius Strobl m_freem(m); 1525c3d5598aSMarius Strobl return (ENOBUFS); 152642c1b001SThomas Moestl } 1527c3d5598aSMarius Strobl rxs->rxs_paddr = segs[0].ds_addr; 152842c1b001SThomas Moestl 1529305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD); 153042c1b001SThomas Moestl 153142c1b001SThomas Moestl GEM_INIT_RXDESC(sc, idx); 153242c1b001SThomas Moestl 153342c1b001SThomas Moestl return (0); 153442c1b001SThomas Moestl } 153542c1b001SThomas Moestl 153642c1b001SThomas Moestl 153742c1b001SThomas Moestl static void 153842c1b001SThomas Moestl gem_eint(sc, status) 153942c1b001SThomas Moestl struct gem_softc *sc; 154042c1b001SThomas Moestl u_int status; 154142c1b001SThomas Moestl { 154242c1b001SThomas Moestl 154342c1b001SThomas Moestl if ((status & GEM_INTR_MIF) != 0) { 154442c1b001SThomas Moestl device_printf(sc->sc_dev, "XXXlink status changed\n"); 154542c1b001SThomas Moestl return; 154642c1b001SThomas Moestl } 154742c1b001SThomas Moestl 154842c1b001SThomas Moestl device_printf(sc->sc_dev, "status=%x\n", status); 154942c1b001SThomas Moestl } 155042c1b001SThomas Moestl 155142c1b001SThomas Moestl 155242c1b001SThomas Moestl void 155342c1b001SThomas Moestl gem_intr(v) 155442c1b001SThomas Moestl void *v; 155542c1b001SThomas Moestl { 155642c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)v; 155742c1b001SThomas Moestl u_int32_t status; 155842c1b001SThomas Moestl 15598cfaff7dSMarius Strobl GEM_LOCK(sc); 1560e1bb13cdSPoul-Henning Kamp status = bus_read_4(sc->sc_res[0], GEM_STATUS); 156118100346SThomas Moestl #ifdef GEM_DEBUG 156242c1b001SThomas Moestl CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x", 156342c1b001SThomas Moestl device_get_name(sc->sc_dev), (status>>19), 156442c1b001SThomas Moestl (u_int)status); 156518100346SThomas Moestl #endif 156642c1b001SThomas Moestl 156742c1b001SThomas Moestl if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0) 156842c1b001SThomas Moestl gem_eint(sc, status); 156942c1b001SThomas Moestl 157042c1b001SThomas Moestl if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 157142c1b001SThomas Moestl gem_tint(sc); 157242c1b001SThomas Moestl 157342c1b001SThomas Moestl if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 157442c1b001SThomas Moestl gem_rint(sc); 157542c1b001SThomas Moestl 157642c1b001SThomas Moestl /* We should eventually do more than just print out error stats. */ 157742c1b001SThomas Moestl if (status & GEM_INTR_TX_MAC) { 1578e1bb13cdSPoul-Henning Kamp int txstat = bus_read_4(sc->sc_res[0], GEM_MAC_TX_STATUS); 157942c1b001SThomas Moestl if (txstat & ~GEM_MAC_TX_XMIT_DONE) 1580336cca9eSBenno Rice device_printf(sc->sc_dev, "MAC tx fault, status %x\n", 1581336cca9eSBenno Rice txstat); 15829bb711b9SThomas Moestl if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) 15838cfaff7dSMarius Strobl gem_init_locked(sc); 158442c1b001SThomas Moestl } 158542c1b001SThomas Moestl if (status & GEM_INTR_RX_MAC) { 1586e1bb13cdSPoul-Henning Kamp int rxstat = bus_read_4(sc->sc_res[0], GEM_MAC_RX_STATUS); 158700d12766SMarius Strobl /* 158800d12766SMarius Strobl * On some chip revisions GEM_MAC_RX_OVERFLOW happen often 158900d12766SMarius Strobl * due to a silicon bug so handle them silently. 159000d12766SMarius Strobl */ 159100d12766SMarius Strobl if (rxstat & GEM_MAC_RX_OVERFLOW) 159200d12766SMarius Strobl gem_init_locked(sc); 159300d12766SMarius Strobl else if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) 1594336cca9eSBenno Rice device_printf(sc->sc_dev, "MAC rx fault, status %x\n", 1595336cca9eSBenno Rice rxstat); 159642c1b001SThomas Moestl } 15978cfaff7dSMarius Strobl GEM_UNLOCK(sc); 159842c1b001SThomas Moestl } 159942c1b001SThomas Moestl 16008cb37876SMarius Strobl static int 16018cb37876SMarius Strobl gem_watchdog(sc) 16028cb37876SMarius Strobl struct gem_softc *sc; 160342c1b001SThomas Moestl { 160442c1b001SThomas Moestl 16058cb37876SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 16068cb37876SMarius Strobl 160718100346SThomas Moestl #ifdef GEM_DEBUG 160842c1b001SThomas Moestl CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x " 160942c1b001SThomas Moestl "GEM_MAC_RX_CONFIG %x", 1610e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_RX_CONFIG), 1611e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_RX_STATUS), 1612e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_RX_CONFIG)); 161342c1b001SThomas Moestl CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x " 161442c1b001SThomas Moestl "GEM_MAC_TX_CONFIG %x", 1615e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_TX_CONFIG), 1616e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_TX_STATUS), 1617e1bb13cdSPoul-Henning Kamp bus_read_4(sc->sc_res[0], GEM_MAC_TX_CONFIG)); 161818100346SThomas Moestl #endif 161942c1b001SThomas Moestl 16208cb37876SMarius Strobl if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) 16218cb37876SMarius Strobl return (0); 16228cb37876SMarius Strobl 162342c1b001SThomas Moestl device_printf(sc->sc_dev, "device timeout\n"); 16248cb37876SMarius Strobl ++sc->sc_ifp->if_oerrors; 162542c1b001SThomas Moestl 162642c1b001SThomas Moestl /* Try to get more packets going. */ 16278cfaff7dSMarius Strobl gem_init_locked(sc); 16288cb37876SMarius Strobl return (EJUSTRETURN); 162942c1b001SThomas Moestl } 163042c1b001SThomas Moestl 163142c1b001SThomas Moestl /* 163242c1b001SThomas Moestl * Initialize the MII Management Interface 163342c1b001SThomas Moestl */ 163442c1b001SThomas Moestl static void 163542c1b001SThomas Moestl gem_mifinit(sc) 163642c1b001SThomas Moestl struct gem_softc *sc; 163742c1b001SThomas Moestl { 163842c1b001SThomas Moestl 163942c1b001SThomas Moestl /* Configure the MIF in frame mode */ 1640e1bb13cdSPoul-Henning Kamp sc->sc_mif_config = bus_read_4(sc->sc_res[0], GEM_MIF_CONFIG); 164142c1b001SThomas Moestl sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA; 1642e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_CONFIG, sc->sc_mif_config); 164342c1b001SThomas Moestl } 164442c1b001SThomas Moestl 164542c1b001SThomas Moestl /* 164642c1b001SThomas Moestl * MII interface 164742c1b001SThomas Moestl * 164842c1b001SThomas Moestl * The GEM MII interface supports at least three different operating modes: 164942c1b001SThomas Moestl * 165042c1b001SThomas Moestl * Bitbang mode is implemented using data, clock and output enable registers. 165142c1b001SThomas Moestl * 165242c1b001SThomas Moestl * Frame mode is implemented by loading a complete frame into the frame 165342c1b001SThomas Moestl * register and polling the valid bit for completion. 165442c1b001SThomas Moestl * 165542c1b001SThomas Moestl * Polling mode uses the frame register but completion is indicated by 165642c1b001SThomas Moestl * an interrupt. 165742c1b001SThomas Moestl * 165842c1b001SThomas Moestl */ 165942c1b001SThomas Moestl int 166042c1b001SThomas Moestl gem_mii_readreg(dev, phy, reg) 166142c1b001SThomas Moestl device_t dev; 166242c1b001SThomas Moestl int phy, reg; 166342c1b001SThomas Moestl { 166442c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 166542c1b001SThomas Moestl int n; 166642c1b001SThomas Moestl u_int32_t v; 166742c1b001SThomas Moestl 166842c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 166942c1b001SThomas Moestl printf("gem_mii_readreg: phy %d reg %d\n", phy, reg); 167042c1b001SThomas Moestl #endif 167142c1b001SThomas Moestl 167242c1b001SThomas Moestl #if 0 167342c1b001SThomas Moestl /* Select the desired PHY in the MIF configuration register */ 1674e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MIF_CONFIG); 167542c1b001SThomas Moestl /* Clear PHY select bit */ 167642c1b001SThomas Moestl v &= ~GEM_MIF_CONFIG_PHY_SEL; 167742c1b001SThomas Moestl if (phy == GEM_PHYAD_EXTERNAL) 167842c1b001SThomas Moestl /* Set PHY select bit to get at external device */ 167942c1b001SThomas Moestl v |= GEM_MIF_CONFIG_PHY_SEL; 1680e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_CONFIG, v); 168142c1b001SThomas Moestl #endif 168242c1b001SThomas Moestl 168342c1b001SThomas Moestl /* Construct the frame command */ 168442c1b001SThomas Moestl v = (reg << GEM_MIF_REG_SHIFT) | (phy << GEM_MIF_PHY_SHIFT) | 168542c1b001SThomas Moestl GEM_MIF_FRAME_READ; 168642c1b001SThomas Moestl 1687e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_FRAME, v); 168842c1b001SThomas Moestl for (n = 0; n < 100; n++) { 168942c1b001SThomas Moestl DELAY(1); 1690e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MIF_FRAME); 16911f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 169242c1b001SThomas Moestl return (v & GEM_MIF_FRAME_DATA); 169342c1b001SThomas Moestl } 169442c1b001SThomas Moestl 169542c1b001SThomas Moestl device_printf(sc->sc_dev, "mii_read timeout\n"); 169642c1b001SThomas Moestl return (0); 169742c1b001SThomas Moestl } 169842c1b001SThomas Moestl 169942c1b001SThomas Moestl int 170042c1b001SThomas Moestl gem_mii_writereg(dev, phy, reg, val) 170142c1b001SThomas Moestl device_t dev; 170242c1b001SThomas Moestl int phy, reg, val; 170342c1b001SThomas Moestl { 170442c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 170542c1b001SThomas Moestl int n; 170642c1b001SThomas Moestl u_int32_t v; 170742c1b001SThomas Moestl 170842c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 170942c1b001SThomas Moestl printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val); 171042c1b001SThomas Moestl #endif 171142c1b001SThomas Moestl 171242c1b001SThomas Moestl #if 0 171342c1b001SThomas Moestl /* Select the desired PHY in the MIF configuration register */ 1714e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MIF_CONFIG); 171542c1b001SThomas Moestl /* Clear PHY select bit */ 171642c1b001SThomas Moestl v &= ~GEM_MIF_CONFIG_PHY_SEL; 171742c1b001SThomas Moestl if (phy == GEM_PHYAD_EXTERNAL) 171842c1b001SThomas Moestl /* Set PHY select bit to get at external device */ 171942c1b001SThomas Moestl v |= GEM_MIF_CONFIG_PHY_SEL; 1720e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_CONFIG, v); 172142c1b001SThomas Moestl #endif 172242c1b001SThomas Moestl /* Construct the frame command */ 172342c1b001SThomas Moestl v = GEM_MIF_FRAME_WRITE | 172442c1b001SThomas Moestl (phy << GEM_MIF_PHY_SHIFT) | 172542c1b001SThomas Moestl (reg << GEM_MIF_REG_SHIFT) | 172642c1b001SThomas Moestl (val & GEM_MIF_FRAME_DATA); 172742c1b001SThomas Moestl 1728e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MIF_FRAME, v); 172942c1b001SThomas Moestl for (n = 0; n < 100; n++) { 173042c1b001SThomas Moestl DELAY(1); 1731e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MIF_FRAME); 17321f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 173342c1b001SThomas Moestl return (1); 173442c1b001SThomas Moestl } 173542c1b001SThomas Moestl 173642c1b001SThomas Moestl device_printf(sc->sc_dev, "mii_write timeout\n"); 173742c1b001SThomas Moestl return (0); 173842c1b001SThomas Moestl } 173942c1b001SThomas Moestl 174042c1b001SThomas Moestl void 174142c1b001SThomas Moestl gem_mii_statchg(dev) 174242c1b001SThomas Moestl device_t dev; 174342c1b001SThomas Moestl { 174442c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 174542c1b001SThomas Moestl #ifdef GEM_DEBUG 17468cfaff7dSMarius Strobl int instance; 174742c1b001SThomas Moestl #endif 174842c1b001SThomas Moestl u_int32_t v; 174942c1b001SThomas Moestl 175042c1b001SThomas Moestl #ifdef GEM_DEBUG 17518cfaff7dSMarius Strobl instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media); 175242c1b001SThomas Moestl if (sc->sc_debug) 175342c1b001SThomas Moestl printf("gem_mii_statchg: status change: phy = %d\n", 175442c1b001SThomas Moestl sc->sc_phys[instance]); 175542c1b001SThomas Moestl #endif 175642c1b001SThomas Moestl 175742c1b001SThomas Moestl /* Set tx full duplex options */ 1758e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_TX_CONFIG, 0); 175942c1b001SThomas Moestl DELAY(10000); /* reg must be cleared and delay before changing. */ 176042c1b001SThomas Moestl v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT| 176142c1b001SThomas Moestl GEM_MAC_TX_ENABLE; 176242c1b001SThomas Moestl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) { 176342c1b001SThomas Moestl v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS; 176442c1b001SThomas Moestl } 1765e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_TX_CONFIG, v); 176642c1b001SThomas Moestl 176742c1b001SThomas Moestl /* XIF Configuration */ 176842c1b001SThomas Moestl v = GEM_MAC_XIF_LINK_LED; 176942c1b001SThomas Moestl v |= GEM_MAC_XIF_TX_MII_ENA; 1770336cca9eSBenno Rice 177142c1b001SThomas Moestl /* If an external transceiver is connected, enable its MII drivers */ 1772e1bb13cdSPoul-Henning Kamp sc->sc_mif_config = bus_read_4(sc->sc_res[0], GEM_MIF_CONFIG); 177342c1b001SThomas Moestl if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) { 177442c1b001SThomas Moestl /* External MII needs echo disable if half duplex. */ 177542c1b001SThomas Moestl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 177642c1b001SThomas Moestl /* turn on full duplex LED */ 177742c1b001SThomas Moestl v |= GEM_MAC_XIF_FDPLX_LED; 177842c1b001SThomas Moestl else 177942c1b001SThomas Moestl /* half duplex -- disable echo */ 178042c1b001SThomas Moestl v |= GEM_MAC_XIF_ECHO_DISABL; 1781336cca9eSBenno Rice 1782336cca9eSBenno Rice if (IFM_SUBTYPE(sc->sc_mii->mii_media_active) == IFM_1000_T) 1783336cca9eSBenno Rice v |= GEM_MAC_XIF_GMII_MODE; 1784336cca9eSBenno Rice else 1785336cca9eSBenno Rice v &= ~GEM_MAC_XIF_GMII_MODE; 178642c1b001SThomas Moestl } else { 178742c1b001SThomas Moestl /* Internal MII needs buf enable */ 178842c1b001SThomas Moestl v |= GEM_MAC_XIF_MII_BUF_ENA; 178942c1b001SThomas Moestl } 1790e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_XIF_CONFIG, v); 179142c1b001SThomas Moestl } 179242c1b001SThomas Moestl 179342c1b001SThomas Moestl int 179442c1b001SThomas Moestl gem_mediachange(ifp) 179542c1b001SThomas Moestl struct ifnet *ifp; 179642c1b001SThomas Moestl { 179742c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 17981f317bf9SMarius Strobl int error; 179942c1b001SThomas Moestl 180042c1b001SThomas Moestl /* XXX Add support for serial media. */ 180142c1b001SThomas Moestl 18021f317bf9SMarius Strobl GEM_LOCK(sc); 18031f317bf9SMarius Strobl error = mii_mediachg(sc->sc_mii); 18041f317bf9SMarius Strobl GEM_UNLOCK(sc); 18051f317bf9SMarius Strobl return (error); 180642c1b001SThomas Moestl } 180742c1b001SThomas Moestl 180842c1b001SThomas Moestl void 180942c1b001SThomas Moestl gem_mediastatus(ifp, ifmr) 181042c1b001SThomas Moestl struct ifnet *ifp; 181142c1b001SThomas Moestl struct ifmediareq *ifmr; 181242c1b001SThomas Moestl { 181342c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 181442c1b001SThomas Moestl 18158cfaff7dSMarius Strobl GEM_LOCK(sc); 18168cfaff7dSMarius Strobl if ((ifp->if_flags & IFF_UP) == 0) { 18178cfaff7dSMarius Strobl GEM_UNLOCK(sc); 181842c1b001SThomas Moestl return; 18198cfaff7dSMarius Strobl } 182042c1b001SThomas Moestl 182142c1b001SThomas Moestl mii_pollstat(sc->sc_mii); 182242c1b001SThomas Moestl ifmr->ifm_active = sc->sc_mii->mii_media_active; 182342c1b001SThomas Moestl ifmr->ifm_status = sc->sc_mii->mii_media_status; 18248cfaff7dSMarius Strobl GEM_UNLOCK(sc); 182542c1b001SThomas Moestl } 182642c1b001SThomas Moestl 182742c1b001SThomas Moestl /* 182842c1b001SThomas Moestl * Process an ioctl request. 182942c1b001SThomas Moestl */ 183042c1b001SThomas Moestl static int 183142c1b001SThomas Moestl gem_ioctl(ifp, cmd, data) 183242c1b001SThomas Moestl struct ifnet *ifp; 183342c1b001SThomas Moestl u_long cmd; 183442c1b001SThomas Moestl caddr_t data; 183542c1b001SThomas Moestl { 183642c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 183742c1b001SThomas Moestl struct ifreq *ifr = (struct ifreq *)data; 18388cfaff7dSMarius Strobl int error = 0; 18398cfaff7dSMarius Strobl 184042c1b001SThomas Moestl switch (cmd) { 184142c1b001SThomas Moestl case SIOCSIFFLAGS: 18421f317bf9SMarius Strobl GEM_LOCK(sc); 184342c1b001SThomas Moestl if (ifp->if_flags & IFF_UP) { 1844336cca9eSBenno Rice if ((sc->sc_ifflags ^ ifp->if_flags) == IFF_PROMISC) 184542c1b001SThomas Moestl gem_setladrf(sc); 184642c1b001SThomas Moestl else 18478cfaff7dSMarius Strobl gem_init_locked(sc); 184842c1b001SThomas Moestl } else { 184913f4c340SRobert Watson if (ifp->if_drv_flags & IFF_DRV_RUNNING) 185042c1b001SThomas Moestl gem_stop(ifp, 0); 185142c1b001SThomas Moestl } 1852336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 18531f317bf9SMarius Strobl GEM_UNLOCK(sc); 185442c1b001SThomas Moestl break; 185542c1b001SThomas Moestl case SIOCADDMULTI: 185642c1b001SThomas Moestl case SIOCDELMULTI: 18571f317bf9SMarius Strobl GEM_LOCK(sc); 185842c1b001SThomas Moestl gem_setladrf(sc); 18591f317bf9SMarius Strobl GEM_UNLOCK(sc); 186042c1b001SThomas Moestl break; 186142c1b001SThomas Moestl case SIOCGIFMEDIA: 186242c1b001SThomas Moestl case SIOCSIFMEDIA: 186342c1b001SThomas Moestl error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 186442c1b001SThomas Moestl break; 186542c1b001SThomas Moestl default: 18661f317bf9SMarius Strobl error = ether_ioctl(ifp, cmd, data); 186742c1b001SThomas Moestl break; 186842c1b001SThomas Moestl } 186942c1b001SThomas Moestl 187042c1b001SThomas Moestl /* Try to get things going again */ 18711f317bf9SMarius Strobl GEM_LOCK(sc); 187242c1b001SThomas Moestl if (ifp->if_flags & IFF_UP) 18738cfaff7dSMarius Strobl gem_start_locked(ifp); 18748cfaff7dSMarius Strobl GEM_UNLOCK(sc); 187542c1b001SThomas Moestl return (error); 187642c1b001SThomas Moestl } 187742c1b001SThomas Moestl 187842c1b001SThomas Moestl /* 187942c1b001SThomas Moestl * Set up the logical address filter. 188042c1b001SThomas Moestl */ 188142c1b001SThomas Moestl static void 188242c1b001SThomas Moestl gem_setladrf(sc) 188342c1b001SThomas Moestl struct gem_softc *sc; 188442c1b001SThomas Moestl { 1885fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 188642c1b001SThomas Moestl struct ifmultiaddr *inm; 188742c1b001SThomas Moestl u_int32_t crc; 188842c1b001SThomas Moestl u_int32_t hash[16]; 188942c1b001SThomas Moestl u_int32_t v; 1890336cca9eSBenno Rice int i; 189142c1b001SThomas Moestl 18928cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 18938cfaff7dSMarius Strobl 189442c1b001SThomas Moestl /* Get current RX configuration */ 1895e1bb13cdSPoul-Henning Kamp v = bus_read_4(sc->sc_res[0], GEM_MAC_RX_CONFIG); 189642c1b001SThomas Moestl 1897336cca9eSBenno Rice /* 1898336cca9eSBenno Rice * Turn off promiscuous mode, promiscuous group mode (all multicast), 1899336cca9eSBenno Rice * and hash filter. Depending on the case, the right bit will be 1900336cca9eSBenno Rice * enabled. 1901336cca9eSBenno Rice */ 1902336cca9eSBenno Rice v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER| 1903336cca9eSBenno Rice GEM_MAC_RX_PROMISC_GRP); 1904336cca9eSBenno Rice 190542c1b001SThomas Moestl if ((ifp->if_flags & IFF_PROMISC) != 0) { 1906336cca9eSBenno Rice /* Turn on promiscuous mode */ 190742c1b001SThomas Moestl v |= GEM_MAC_RX_PROMISCUOUS; 190842c1b001SThomas Moestl goto chipit; 190942c1b001SThomas Moestl } 191042c1b001SThomas Moestl if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 191142c1b001SThomas Moestl hash[3] = hash[2] = hash[1] = hash[0] = 0xffff; 191242c1b001SThomas Moestl ifp->if_flags |= IFF_ALLMULTI; 1913336cca9eSBenno Rice v |= GEM_MAC_RX_PROMISC_GRP; 191442c1b001SThomas Moestl goto chipit; 191542c1b001SThomas Moestl } 191642c1b001SThomas Moestl 191742c1b001SThomas Moestl /* 191842c1b001SThomas Moestl * Set up multicast address filter by passing all multicast addresses 1919336cca9eSBenno Rice * through a crc generator, and then using the high order 8 bits as an 1920336cca9eSBenno Rice * index into the 256 bit logical address filter. The high order 4 1921336cca9eSBenno Rice * bits selects the word, while the other 4 bits select the bit within 1922336cca9eSBenno Rice * the word (where bit 0 is the MSB). 192342c1b001SThomas Moestl */ 192442c1b001SThomas Moestl 1925336cca9eSBenno Rice /* Clear hash table */ 1926336cca9eSBenno Rice memset(hash, 0, sizeof(hash)); 1927336cca9eSBenno Rice 192813b203d0SRobert Watson IF_ADDR_LOCK(ifp); 1929fc74a9f9SBrooks Davis TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) { 193042c1b001SThomas Moestl if (inm->ifma_addr->sa_family != AF_LINK) 193142c1b001SThomas Moestl continue; 1932c240bd8cSMarius Strobl crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 1933c240bd8cSMarius Strobl inm->ifma_addr), ETHER_ADDR_LEN); 193442c1b001SThomas Moestl 193542c1b001SThomas Moestl /* Just want the 8 most significant bits. */ 193642c1b001SThomas Moestl crc >>= 24; 193742c1b001SThomas Moestl 193842c1b001SThomas Moestl /* Set the corresponding bit in the filter. */ 1939336cca9eSBenno Rice hash[crc >> 4] |= 1 << (15 - (crc & 15)); 1940336cca9eSBenno Rice } 194113b203d0SRobert Watson IF_ADDR_UNLOCK(ifp); 1942336cca9eSBenno Rice 1943336cca9eSBenno Rice v |= GEM_MAC_RX_HASH_FILTER; 1944336cca9eSBenno Rice ifp->if_flags &= ~IFF_ALLMULTI; 1945336cca9eSBenno Rice 1946336cca9eSBenno Rice /* Now load the hash table into the chip (if we are using it) */ 1947336cca9eSBenno Rice for (i = 0; i < 16; i++) { 1948e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], 1949336cca9eSBenno Rice GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0), 1950336cca9eSBenno Rice hash[i]); 195142c1b001SThomas Moestl } 195242c1b001SThomas Moestl 195342c1b001SThomas Moestl chipit: 1954e1bb13cdSPoul-Henning Kamp bus_write_4(sc->sc_res[0], GEM_MAC_RX_CONFIG, v); 195542c1b001SThomas Moestl } 1956