1aad970f1SDavid E. O'Brien /*- 242c1b001SThomas Moestl * Copyright (C) 2001 Eduardo Horvath. 3305f2c06SThomas Moestl * Copyright (c) 2001-2003 Thomas Moestl 42a79fd39SMarius Strobl * Copyright (c) 2007 Marius Strobl <marius@FreeBSD.org> 542c1b001SThomas Moestl * All rights reserved. 642c1b001SThomas Moestl * 742c1b001SThomas Moestl * Redistribution and use in source and binary forms, with or without 842c1b001SThomas Moestl * modification, are permitted provided that the following conditions 942c1b001SThomas Moestl * are met: 1042c1b001SThomas Moestl * 1. Redistributions of source code must retain the above copyright 1142c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer. 1242c1b001SThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 1342c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer in the 1442c1b001SThomas Moestl * documentation and/or other materials provided with the distribution. 1542c1b001SThomas Moestl * 1642c1b001SThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 1742c1b001SThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1842c1b001SThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1942c1b001SThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 2042c1b001SThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2142c1b001SThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2242c1b001SThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2342c1b001SThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2442c1b001SThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2542c1b001SThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2642c1b001SThomas Moestl * SUCH DAMAGE. 2742c1b001SThomas Moestl * 28336cca9eSBenno Rice * from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp 2942c1b001SThomas Moestl */ 3042c1b001SThomas Moestl 31aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 32aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 33aad970f1SDavid E. O'Brien 3442c1b001SThomas Moestl /* 351ed3fed7SMarius Strobl * Driver for Apple GMAC, Sun ERI and Sun GEM Ethernet controllers 3642c1b001SThomas Moestl */ 3742c1b001SThomas Moestl 3818100346SThomas Moestl #if 0 3942c1b001SThomas Moestl #define GEM_DEBUG 4018100346SThomas Moestl #endif 4142c1b001SThomas Moestl 42c3d5598aSMarius Strobl #if 0 /* XXX: In case of emergency, re-enable this. */ 43c3d5598aSMarius Strobl #define GEM_RINT_TIMEOUT 44c3d5598aSMarius Strobl #endif 45c3d5598aSMarius Strobl 4642c1b001SThomas Moestl #include <sys/param.h> 4742c1b001SThomas Moestl #include <sys/systm.h> 4842c1b001SThomas Moestl #include <sys/bus.h> 4942c1b001SThomas Moestl #include <sys/callout.h> 50a30d4b32SMike Barcroft #include <sys/endian.h> 5142c1b001SThomas Moestl #include <sys/mbuf.h> 5242c1b001SThomas Moestl #include <sys/malloc.h> 5342c1b001SThomas Moestl #include <sys/kernel.h> 548cfaff7dSMarius Strobl #include <sys/lock.h> 55186f2b9eSPoul-Henning Kamp #include <sys/module.h> 568cfaff7dSMarius Strobl #include <sys/mutex.h> 5742c1b001SThomas Moestl #include <sys/socket.h> 5842c1b001SThomas Moestl #include <sys/sockio.h> 59e1bb13cdSPoul-Henning Kamp #include <sys/rman.h> 6042c1b001SThomas Moestl 6108e0fdebSThomas Moestl #include <net/bpf.h> 6242c1b001SThomas Moestl #include <net/ethernet.h> 6342c1b001SThomas Moestl #include <net/if.h> 6442c1b001SThomas Moestl #include <net/if_arp.h> 6542c1b001SThomas Moestl #include <net/if_dl.h> 6642c1b001SThomas Moestl #include <net/if_media.h> 67fc74a9f9SBrooks Davis #include <net/if_types.h> 6800d12766SMarius Strobl #include <net/if_vlan_var.h> 6942c1b001SThomas Moestl 7012fb0330SPyun YongHyeon #include <netinet/in.h> 7112fb0330SPyun YongHyeon #include <netinet/in_systm.h> 7212fb0330SPyun YongHyeon #include <netinet/ip.h> 7312fb0330SPyun YongHyeon #include <netinet/tcp.h> 7412fb0330SPyun YongHyeon #include <netinet/udp.h> 7512fb0330SPyun YongHyeon 7642c1b001SThomas Moestl #include <machine/bus.h> 7742c1b001SThomas Moestl 7842c1b001SThomas Moestl #include <dev/mii/mii.h> 7942c1b001SThomas Moestl #include <dev/mii/miivar.h> 8042c1b001SThomas Moestl 81681f7d03SWarner Losh #include <dev/gem/if_gemreg.h> 82681f7d03SWarner Losh #include <dev/gem/if_gemvar.h> 8342c1b001SThomas Moestl 841ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NRXDESC) && GEM_NRXDESC >= 32 && GEM_NRXDESC <= 8192); 851ed3fed7SMarius Strobl CTASSERT(powerof2(GEM_NTXDESC) && GEM_NTXDESC >= 32 && GEM_NTXDESC <= 8192); 861ed3fed7SMarius Strobl 879ba2b298SMarius Strobl #define GEM_TRIES 10000 881ed3fed7SMarius Strobl 8912fb0330SPyun YongHyeon /* 9078d22f42SMarius Strobl * The hardware supports basic TCP/UDP checksum offloading. However, 9112fb0330SPyun YongHyeon * the hardware doesn't compensate the checksum for UDP datagram which 9212fb0330SPyun YongHyeon * can yield to 0x0. As a safe guard, UDP checksum offload is disabled 9312fb0330SPyun YongHyeon * by default. It can be reactivated by setting special link option 9412fb0330SPyun YongHyeon * link0 with ifconfig(8). 9512fb0330SPyun YongHyeon */ 9612fb0330SPyun YongHyeon #define GEM_CSUM_FEATURES (CSUM_TCP) 9742c1b001SThomas Moestl 982a79fd39SMarius Strobl static int gem_add_rxbuf(struct gem_softc *sc, int idx); 99bd3d9826SMarius Strobl static int gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, 100bd3d9826SMarius Strobl uint32_t clr, uint32_t set); 1012a79fd39SMarius Strobl static void gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, 1022a79fd39SMarius Strobl int nsegs, int error); 1032a79fd39SMarius Strobl static int gem_disable_rx(struct gem_softc *sc); 1042a79fd39SMarius Strobl static int gem_disable_tx(struct gem_softc *sc); 1052a79fd39SMarius Strobl static void gem_eint(struct gem_softc *sc, u_int status); 1062a79fd39SMarius Strobl static void gem_init(void *xsc); 1072a79fd39SMarius Strobl static void gem_init_locked(struct gem_softc *sc); 1082a79fd39SMarius Strobl static void gem_init_regs(struct gem_softc *sc); 1092a79fd39SMarius Strobl static int gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data); 1102a79fd39SMarius Strobl static int gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head); 1112a79fd39SMarius Strobl static int gem_meminit(struct gem_softc *sc); 1122a79fd39SMarius Strobl static void gem_mifinit(struct gem_softc *sc); 1132a79fd39SMarius Strobl static void gem_reset(struct gem_softc *sc); 1142a79fd39SMarius Strobl static int gem_reset_rx(struct gem_softc *sc); 1151ed3fed7SMarius Strobl static void gem_reset_rxdma(struct gem_softc *sc); 1162a79fd39SMarius Strobl static int gem_reset_tx(struct gem_softc *sc); 1172a79fd39SMarius Strobl static u_int gem_ringsize(u_int sz); 1182a79fd39SMarius Strobl static void gem_rint(struct gem_softc *sc); 119c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 1202a79fd39SMarius Strobl static void gem_rint_timeout(void *arg); 12111e3f060SJake Burkholder #endif 1229ba2b298SMarius Strobl static inline void gem_rxcksum(struct mbuf *m, uint64_t flags); 1232a79fd39SMarius Strobl static void gem_rxdrain(struct gem_softc *sc); 1242a79fd39SMarius Strobl static void gem_setladrf(struct gem_softc *sc); 1252a79fd39SMarius Strobl static void gem_start(struct ifnet *ifp); 1262a79fd39SMarius Strobl static void gem_start_locked(struct ifnet *ifp); 1272a79fd39SMarius Strobl static void gem_stop(struct ifnet *ifp, int disable); 1282a79fd39SMarius Strobl static void gem_tick(void *arg); 1292a79fd39SMarius Strobl static void gem_tint(struct gem_softc *sc); 1309ba2b298SMarius Strobl static inline void gem_txkick(struct gem_softc *sc); 1312a79fd39SMarius Strobl static int gem_watchdog(struct gem_softc *sc); 13242c1b001SThomas Moestl 13342c1b001SThomas Moestl devclass_t gem_devclass; 13442c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0); 13542c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1); 13642c1b001SThomas Moestl 13742c1b001SThomas Moestl #ifdef GEM_DEBUG 13842c1b001SThomas Moestl #include <sys/ktr.h> 13942c1b001SThomas Moestl #define KTR_GEM KTR_CT2 14042c1b001SThomas Moestl #endif 14142c1b001SThomas Moestl 142bd3d9826SMarius Strobl #define GEM_BANK1_BITWAIT(sc, r, clr, set) \ 143bd3d9826SMarius Strobl gem_bitwait((sc), GEM_RES_BANK1, (r), (clr), (set)) 144bd3d9826SMarius Strobl #define GEM_BANK2_BITWAIT(sc, r, clr, set) \ 145bd3d9826SMarius Strobl gem_bitwait((sc), GEM_RES_BANK2, (r), (clr), (set)) 146bd3d9826SMarius Strobl 14742c1b001SThomas Moestl int 1482a79fd39SMarius Strobl gem_attach(struct gem_softc *sc) 14942c1b001SThomas Moestl { 1502a79fd39SMarius Strobl struct gem_txsoft *txs; 151fc74a9f9SBrooks Davis struct ifnet *ifp; 1522a79fd39SMarius Strobl int error, i; 1532a79fd39SMarius Strobl uint32_t v; 15442c1b001SThomas Moestl 1559ba2b298SMarius Strobl if (bootverbose) 1569ba2b298SMarius Strobl device_printf(sc->sc_dev, "flags=0x%x\n", sc->sc_flags); 1579ba2b298SMarius Strobl 1589ba2b298SMarius Strobl /* Set up ifnet structure. */ 159fc74a9f9SBrooks Davis ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 160fc74a9f9SBrooks Davis if (ifp == NULL) 161fc74a9f9SBrooks Davis return (ENOSPC); 1629ba2b298SMarius Strobl sc->sc_csum_features = GEM_CSUM_FEATURES; 1639ba2b298SMarius Strobl ifp->if_softc = sc; 1649ba2b298SMarius Strobl if_initname(ifp, device_get_name(sc->sc_dev), 1659ba2b298SMarius Strobl device_get_unit(sc->sc_dev)); 1669ba2b298SMarius Strobl ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1679ba2b298SMarius Strobl ifp->if_start = gem_start; 1689ba2b298SMarius Strobl ifp->if_ioctl = gem_ioctl; 1699ba2b298SMarius Strobl ifp->if_init = gem_init; 1709ba2b298SMarius Strobl IFQ_SET_MAXLEN(&ifp->if_snd, GEM_TXQUEUELEN); 1719ba2b298SMarius Strobl ifp->if_snd.ifq_drv_maxlen = GEM_TXQUEUELEN; 1729ba2b298SMarius Strobl IFQ_SET_READY(&ifp->if_snd); 173fc74a9f9SBrooks Davis 1741f317bf9SMarius Strobl callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0); 1751f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 1761f317bf9SMarius Strobl callout_init_mtx(&sc->sc_rx_ch, &sc->sc_mtx, 0); 1771f317bf9SMarius Strobl #endif 1781f317bf9SMarius Strobl 17942c1b001SThomas Moestl /* Make sure the chip is stopped. */ 18042c1b001SThomas Moestl gem_reset(sc); 18142c1b001SThomas Moestl 182378f231eSJohn-Mark Gurney error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 183378f231eSJohn-Mark Gurney BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 1842a79fd39SMarius Strobl BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0, NULL, 1852a79fd39SMarius Strobl NULL, &sc->sc_pdmatag); 1869ba2b298SMarius Strobl if (error != 0) 187fc74a9f9SBrooks Davis goto fail_ifnet; 18842c1b001SThomas Moestl 18942c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 19012fb0330SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 19112fb0330SPyun YongHyeon 1, MCLBYTES, BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_rdmatag); 1929ba2b298SMarius Strobl if (error != 0) 193305f2c06SThomas Moestl goto fail_ptag; 194305f2c06SThomas Moestl 195305f2c06SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 19612fb0330SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 19712fb0330SPyun YongHyeon MCLBYTES * GEM_NTXSEGS, GEM_NTXSEGS, MCLBYTES, 198f6b1c44dSScott Long BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag); 1999ba2b298SMarius Strobl if (error != 0) 200305f2c06SThomas Moestl goto fail_rtag; 20142c1b001SThomas Moestl 20242c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0, 20312fb0330SPyun YongHyeon BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, 20442c1b001SThomas Moestl sizeof(struct gem_control_data), 1, 20512fb0330SPyun YongHyeon sizeof(struct gem_control_data), 0, 20612fb0330SPyun YongHyeon NULL, NULL, &sc->sc_cdmatag); 2079ba2b298SMarius Strobl if (error != 0) 208305f2c06SThomas Moestl goto fail_ttag; 20942c1b001SThomas Moestl 21042c1b001SThomas Moestl /* 2112a79fd39SMarius Strobl * Allocate the control data structures, create and load the 21242c1b001SThomas Moestl * DMA map for it. 21342c1b001SThomas Moestl */ 21442c1b001SThomas Moestl if ((error = bus_dmamem_alloc(sc->sc_cdmatag, 21512fb0330SPyun YongHyeon (void **)&sc->sc_control_data, 21612fb0330SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO, 2179ba2b298SMarius Strobl &sc->sc_cddmamap)) != 0) { 2182a79fd39SMarius Strobl device_printf(sc->sc_dev, 2192a79fd39SMarius Strobl "unable to allocate control data, error = %d\n", error); 220305f2c06SThomas Moestl goto fail_ctag; 22142c1b001SThomas Moestl } 22242c1b001SThomas Moestl 22342c1b001SThomas Moestl sc->sc_cddma = 0; 22442c1b001SThomas Moestl if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap, 22542c1b001SThomas Moestl sc->sc_control_data, sizeof(struct gem_control_data), 22642c1b001SThomas Moestl gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) { 2272a79fd39SMarius Strobl device_printf(sc->sc_dev, 2282a79fd39SMarius Strobl "unable to load control data DMA map, error = %d\n", 2292a79fd39SMarius Strobl error); 230305f2c06SThomas Moestl goto fail_cmem; 23142c1b001SThomas Moestl } 23242c1b001SThomas Moestl 23342c1b001SThomas Moestl /* 23442c1b001SThomas Moestl * Initialize the transmit job descriptors. 23542c1b001SThomas Moestl */ 23642c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txfreeq); 23742c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txdirtyq); 23842c1b001SThomas Moestl 23942c1b001SThomas Moestl /* 24042c1b001SThomas Moestl * Create the transmit buffer DMA maps. 24142c1b001SThomas Moestl */ 24242c1b001SThomas Moestl error = ENOMEM; 24342c1b001SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 24442c1b001SThomas Moestl txs = &sc->sc_txsoft[i]; 24542c1b001SThomas Moestl txs->txs_mbuf = NULL; 24642c1b001SThomas Moestl txs->txs_ndescs = 0; 247305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_tdmatag, 0, 24842c1b001SThomas Moestl &txs->txs_dmamap)) != 0) { 2492a79fd39SMarius Strobl device_printf(sc->sc_dev, 2502a79fd39SMarius Strobl "unable to create TX DMA map %d, error = %d\n", 2512a79fd39SMarius Strobl i, error); 252305f2c06SThomas Moestl goto fail_txd; 25342c1b001SThomas Moestl } 25442c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 25542c1b001SThomas Moestl } 25642c1b001SThomas Moestl 25742c1b001SThomas Moestl /* 25842c1b001SThomas Moestl * Create the receive buffer DMA maps. 25942c1b001SThomas Moestl */ 26042c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 261305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_rdmatag, 0, 26242c1b001SThomas Moestl &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 2632a79fd39SMarius Strobl device_printf(sc->sc_dev, 2642a79fd39SMarius Strobl "unable to create RX DMA map %d, error = %d\n", 2652a79fd39SMarius Strobl i, error); 266305f2c06SThomas Moestl goto fail_rxd; 26742c1b001SThomas Moestl } 26842c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_mbuf = NULL; 26942c1b001SThomas Moestl } 27042c1b001SThomas Moestl 2711ed3fed7SMarius Strobl /* Bad things will happen when touching this register on ERI. */ 2721ed3fed7SMarius Strobl if (sc->sc_variant != GEM_SUN_ERI) 273bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE, 2741ed3fed7SMarius Strobl GEM_MII_DATAPATH_MII); 2751ed3fed7SMarius Strobl 27642c1b001SThomas Moestl gem_mifinit(sc); 27742c1b001SThomas Moestl 2781ed3fed7SMarius Strobl /* 2791ed3fed7SMarius Strobl * Look for an external PHY. 2801ed3fed7SMarius Strobl */ 2811ed3fed7SMarius Strobl error = ENXIO; 282bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG); 2831ed3fed7SMarius Strobl if ((v & GEM_MIF_CONFIG_MDI1) != 0) { 2841ed3fed7SMarius Strobl v |= GEM_MIF_CONFIG_PHY_SEL; 285bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v); 2861ed3fed7SMarius Strobl switch (sc->sc_variant) { 2871ed3fed7SMarius Strobl case GEM_SUN_ERI: 2881ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 2891ed3fed7SMarius Strobl break; 2901ed3fed7SMarius Strobl default: 2911ed3fed7SMarius Strobl sc->sc_phyad = -1; 2921ed3fed7SMarius Strobl break; 2931ed3fed7SMarius Strobl } 2941ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 2951ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 2961ed3fed7SMarius Strobl } 2971ed3fed7SMarius Strobl 2981ed3fed7SMarius Strobl /* 2991ed3fed7SMarius Strobl * Fall back on an internal PHY if no external PHY was found. 3009e48f1e7SMarius Strobl * Note that with Apple (K2) GMACs GEM_MIF_CONFIG_MDI0 can't be 3019e48f1e7SMarius Strobl * trusted when the firmware has powered down the chip. 3021ed3fed7SMarius Strobl */ 3039e48f1e7SMarius Strobl if (error != 0 && 3049e48f1e7SMarius Strobl ((v & GEM_MIF_CONFIG_MDI0) != 0 || GEM_IS_APPLE(sc))) { 3051ed3fed7SMarius Strobl v &= ~GEM_MIF_CONFIG_PHY_SEL; 306bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v); 3071ed3fed7SMarius Strobl switch (sc->sc_variant) { 3081ed3fed7SMarius Strobl case GEM_SUN_ERI: 3091ed3fed7SMarius Strobl case GEM_APPLE_K2_GMAC: 3101ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_INTERNAL; 3111ed3fed7SMarius Strobl break; 3121ed3fed7SMarius Strobl case GEM_APPLE_GMAC: 3131ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3141ed3fed7SMarius Strobl break; 3151ed3fed7SMarius Strobl default: 3161ed3fed7SMarius Strobl sc->sc_phyad = -1; 3171ed3fed7SMarius Strobl break; 3181ed3fed7SMarius Strobl } 3191ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3201ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3211ed3fed7SMarius Strobl } 3221ed3fed7SMarius Strobl 3231ed3fed7SMarius Strobl /* 3241ed3fed7SMarius Strobl * Try the external PCS SERDES if we didn't find any PHYs. 3251ed3fed7SMarius Strobl */ 3261ed3fed7SMarius Strobl if (error != 0 && sc->sc_variant == GEM_SUN_GEM) { 327bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE, 3281ed3fed7SMarius Strobl GEM_MII_DATAPATH_SERDES); 329bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 3301ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 331bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE); 3321ed3fed7SMarius Strobl sc->sc_flags |= GEM_SERDES; 3331ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3341ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3351ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3361ed3fed7SMarius Strobl } 3371ed3fed7SMarius Strobl 3381ed3fed7SMarius Strobl if (error != 0) { 3391ed3fed7SMarius Strobl device_printf(sc->sc_dev, "PHY probe failed: %d\n", error); 340305f2c06SThomas Moestl goto fail_rxd; 34142c1b001SThomas Moestl } 34242c1b001SThomas Moestl sc->sc_mii = device_get_softc(sc->sc_miibus); 34342c1b001SThomas Moestl 34442c1b001SThomas Moestl /* 34542c1b001SThomas Moestl * From this point forward, the attachment cannot fail. A failure 34642c1b001SThomas Moestl * before this point releases all resources that may have been 34742c1b001SThomas Moestl * allocated. 34842c1b001SThomas Moestl */ 34942c1b001SThomas Moestl 350801772ecSMarius Strobl /* Get RX FIFO size. */ 351336cca9eSBenno Rice sc->sc_rxfifosize = 64 * 352bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_FIFO_SIZE); 353336cca9eSBenno Rice 354801772ecSMarius Strobl /* Get TX FIFO size. */ 355bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_TX_FIFO_SIZE); 3563a5aee5aSThomas Moestl device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n", 3573a5aee5aSThomas Moestl sc->sc_rxfifosize / 1024, v / 16); 35842c1b001SThomas Moestl 35942c1b001SThomas Moestl /* Attach the interface. */ 360fc74a9f9SBrooks Davis ether_ifattach(ifp, sc->sc_enaddr); 36142c1b001SThomas Moestl 36200d12766SMarius Strobl /* 36312fb0330SPyun YongHyeon * Tell the upper layer(s) we support long frames/checksum offloads. 36400d12766SMarius Strobl */ 36500d12766SMarius Strobl ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 36612fb0330SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 36712fb0330SPyun YongHyeon ifp->if_hwassist |= sc->sc_csum_features; 36812fb0330SPyun YongHyeon ifp->if_capenable |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 36900d12766SMarius Strobl 37042c1b001SThomas Moestl return (0); 37142c1b001SThomas Moestl 37242c1b001SThomas Moestl /* 37342c1b001SThomas Moestl * Free any resources we've allocated during the failed attach 37442c1b001SThomas Moestl * attempt. Do this in reverse order and fall through. 37542c1b001SThomas Moestl */ 376305f2c06SThomas Moestl fail_rxd: 3772a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 37842c1b001SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 379305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 38042c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 381305f2c06SThomas Moestl fail_txd: 3822a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 38342c1b001SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 384305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 38542c1b001SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 386305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 387305f2c06SThomas Moestl fail_cmem: 38842c1b001SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 38942c1b001SThomas Moestl sc->sc_cddmamap); 390305f2c06SThomas Moestl fail_ctag: 39142c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 392305f2c06SThomas Moestl fail_ttag: 393305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 394305f2c06SThomas Moestl fail_rtag: 395305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 396305f2c06SThomas Moestl fail_ptag: 39742c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 398fc74a9f9SBrooks Davis fail_ifnet: 399fc74a9f9SBrooks Davis if_free(ifp); 40042c1b001SThomas Moestl return (error); 40142c1b001SThomas Moestl } 40242c1b001SThomas Moestl 403cbbdf236SThomas Moestl void 4042a79fd39SMarius Strobl gem_detach(struct gem_softc *sc) 405cbbdf236SThomas Moestl { 406fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 407cbbdf236SThomas Moestl int i; 408cbbdf236SThomas Moestl 409b3a1f860SMarius Strobl ether_ifdetach(ifp); 4108cfaff7dSMarius Strobl GEM_LOCK(sc); 41125bd46d0SBrooks Davis gem_stop(ifp, 1); 4128cfaff7dSMarius Strobl GEM_UNLOCK(sc); 4131f317bf9SMarius Strobl callout_drain(&sc->sc_tick_ch); 4141f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 4151f317bf9SMarius Strobl callout_drain(&sc->sc_rx_ch); 4161f317bf9SMarius Strobl #endif 417fc74a9f9SBrooks Davis if_free(ifp); 418cbbdf236SThomas Moestl device_delete_child(sc->sc_dev, sc->sc_miibus); 419cbbdf236SThomas Moestl 4202a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 421cbbdf236SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 422cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 423cbbdf236SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 4242a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 425cbbdf236SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 426cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 427cbbdf236SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 428ccb1212aSMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 429cbbdf236SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 430cbbdf236SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 431cbbdf236SThomas Moestl sc->sc_cddmamap); 432cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 433cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 434cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 435cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 436cbbdf236SThomas Moestl } 437cbbdf236SThomas Moestl 438cbbdf236SThomas Moestl void 4392a79fd39SMarius Strobl gem_suspend(struct gem_softc *sc) 440cbbdf236SThomas Moestl { 441fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 442cbbdf236SThomas Moestl 4438cfaff7dSMarius Strobl GEM_LOCK(sc); 444cbbdf236SThomas Moestl gem_stop(ifp, 0); 4458cfaff7dSMarius Strobl GEM_UNLOCK(sc); 446cbbdf236SThomas Moestl } 447cbbdf236SThomas Moestl 448cbbdf236SThomas Moestl void 4492a79fd39SMarius Strobl gem_resume(struct gem_softc *sc) 450cbbdf236SThomas Moestl { 451fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 452cbbdf236SThomas Moestl 4538cfaff7dSMarius Strobl GEM_LOCK(sc); 45400d12766SMarius Strobl /* 45500d12766SMarius Strobl * On resume all registers have to be initialized again like 45600d12766SMarius Strobl * after power-on. 45700d12766SMarius Strobl */ 4581ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_INITED; 459cbbdf236SThomas Moestl if (ifp->if_flags & IFF_UP) 4608cfaff7dSMarius Strobl gem_init_locked(sc); 4618cfaff7dSMarius Strobl GEM_UNLOCK(sc); 462cbbdf236SThomas Moestl } 463cbbdf236SThomas Moestl 4649ba2b298SMarius Strobl static inline void 46512fb0330SPyun YongHyeon gem_rxcksum(struct mbuf *m, uint64_t flags) 46612fb0330SPyun YongHyeon { 46712fb0330SPyun YongHyeon struct ether_header *eh; 46812fb0330SPyun YongHyeon struct ip *ip; 46912fb0330SPyun YongHyeon struct udphdr *uh; 4702a79fd39SMarius Strobl uint16_t *opts; 47112fb0330SPyun YongHyeon int32_t hlen, len, pktlen; 47212fb0330SPyun YongHyeon uint32_t temp32; 4732a79fd39SMarius Strobl uint16_t cksum; 47412fb0330SPyun YongHyeon 47512fb0330SPyun YongHyeon pktlen = m->m_pkthdr.len; 47612fb0330SPyun YongHyeon if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 47712fb0330SPyun YongHyeon return; 47812fb0330SPyun YongHyeon eh = mtod(m, struct ether_header *); 47912fb0330SPyun YongHyeon if (eh->ether_type != htons(ETHERTYPE_IP)) 48012fb0330SPyun YongHyeon return; 48112fb0330SPyun YongHyeon ip = (struct ip *)(eh + 1); 48212fb0330SPyun YongHyeon if (ip->ip_v != IPVERSION) 48312fb0330SPyun YongHyeon return; 48412fb0330SPyun YongHyeon 48512fb0330SPyun YongHyeon hlen = ip->ip_hl << 2; 48612fb0330SPyun YongHyeon pktlen -= sizeof(struct ether_header); 48712fb0330SPyun YongHyeon if (hlen < sizeof(struct ip)) 48812fb0330SPyun YongHyeon return; 48912fb0330SPyun YongHyeon if (ntohs(ip->ip_len) < hlen) 49012fb0330SPyun YongHyeon return; 49112fb0330SPyun YongHyeon if (ntohs(ip->ip_len) != pktlen) 49212fb0330SPyun YongHyeon return; 49312fb0330SPyun YongHyeon if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 4942a79fd39SMarius Strobl return; /* Cannot handle fragmented packet. */ 49512fb0330SPyun YongHyeon 49612fb0330SPyun YongHyeon switch (ip->ip_p) { 49712fb0330SPyun YongHyeon case IPPROTO_TCP: 49812fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct tcphdr))) 49912fb0330SPyun YongHyeon return; 50012fb0330SPyun YongHyeon break; 50112fb0330SPyun YongHyeon case IPPROTO_UDP: 50212fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct udphdr))) 50312fb0330SPyun YongHyeon return; 50412fb0330SPyun YongHyeon uh = (struct udphdr *)((uint8_t *)ip + hlen); 50512fb0330SPyun YongHyeon if (uh->uh_sum == 0) 50612fb0330SPyun YongHyeon return; /* no checksum */ 50712fb0330SPyun YongHyeon break; 50812fb0330SPyun YongHyeon default: 50912fb0330SPyun YongHyeon return; 51012fb0330SPyun YongHyeon } 51112fb0330SPyun YongHyeon 51212fb0330SPyun YongHyeon cksum = ~(flags & GEM_RD_CHECKSUM); 51312fb0330SPyun YongHyeon /* checksum fixup for IP options */ 51412fb0330SPyun YongHyeon len = hlen - sizeof(struct ip); 51512fb0330SPyun YongHyeon if (len > 0) { 51612fb0330SPyun YongHyeon opts = (uint16_t *)(ip + 1); 51712fb0330SPyun YongHyeon for (; len > 0; len -= sizeof(uint16_t), opts++) { 51812fb0330SPyun YongHyeon temp32 = cksum - *opts; 51912fb0330SPyun YongHyeon temp32 = (temp32 >> 16) + (temp32 & 65535); 52012fb0330SPyun YongHyeon cksum = temp32 & 65535; 52112fb0330SPyun YongHyeon } 52212fb0330SPyun YongHyeon } 52312fb0330SPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 52412fb0330SPyun YongHyeon m->m_pkthdr.csum_data = cksum; 52512fb0330SPyun YongHyeon } 52612fb0330SPyun YongHyeon 52742c1b001SThomas Moestl static void 5282a79fd39SMarius Strobl gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 52942c1b001SThomas Moestl { 5302a79fd39SMarius Strobl struct gem_softc *sc = xsc; 53142c1b001SThomas Moestl 53242c1b001SThomas Moestl if (error != 0) 53342c1b001SThomas Moestl return; 5342a79fd39SMarius Strobl if (nsegs != 1) 5351ed3fed7SMarius Strobl panic("%s: bad control buffer segment count", __func__); 53642c1b001SThomas Moestl sc->sc_cddma = segs[0].ds_addr; 53742c1b001SThomas Moestl } 53842c1b001SThomas Moestl 53942c1b001SThomas Moestl static void 5402a79fd39SMarius Strobl gem_tick(void *arg) 54142c1b001SThomas Moestl { 54242c1b001SThomas Moestl struct gem_softc *sc = arg; 5439ba2b298SMarius Strobl struct ifnet *ifp = sc->sc_ifp; 54478d22f42SMarius Strobl uint32_t v; 54542c1b001SThomas Moestl 5461f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 54712fb0330SPyun YongHyeon 54812fb0330SPyun YongHyeon /* 54978d22f42SMarius Strobl * Unload collision and error counters. 55012fb0330SPyun YongHyeon */ 55112fb0330SPyun YongHyeon ifp->if_collisions += 552bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_NORM_COLL_CNT) + 55378d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_FIRST_COLL_CNT); 55478d22f42SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_EXCESS_COLL_CNT) + 555bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_LATE_COLL_CNT); 55678d22f42SMarius Strobl ifp->if_collisions += v; 55778d22f42SMarius Strobl ifp->if_oerrors += v; 55878d22f42SMarius Strobl ifp->if_ierrors += 55978d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_LEN_ERR_CNT) + 56078d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_ALIGN_ERR) + 56178d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CRC_ERR_CNT) + 56278d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CODE_VIOL); 56312fb0330SPyun YongHyeon 56412fb0330SPyun YongHyeon /* 565801772ecSMarius Strobl * Then clear the hardware counters. 56612fb0330SPyun YongHyeon */ 567bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 568bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 569bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 570bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 57178d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 57278d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 57378d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 57478d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 57512fb0330SPyun YongHyeon 57642c1b001SThomas Moestl mii_tick(sc->sc_mii); 57742c1b001SThomas Moestl 5788cb37876SMarius Strobl if (gem_watchdog(sc) == EJUSTRETURN) 5798cb37876SMarius Strobl return; 5808cb37876SMarius Strobl 58142c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 58242c1b001SThomas Moestl } 58342c1b001SThomas Moestl 58442c1b001SThomas Moestl static int 585bd3d9826SMarius Strobl gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, uint32_t clr, 586bd3d9826SMarius Strobl uint32_t set) 58742c1b001SThomas Moestl { 58842c1b001SThomas Moestl int i; 5892a79fd39SMarius Strobl uint32_t reg; 59042c1b001SThomas Moestl 5919ba2b298SMarius Strobl for (i = GEM_TRIES; i--; DELAY(100)) { 592bd3d9826SMarius Strobl reg = GEM_BANKN_READ_M(bank, 4, sc, r); 593e87137e1SMarius Strobl if ((reg & clr) == 0 && (reg & set) == set) 59442c1b001SThomas Moestl return (1); 59542c1b001SThomas Moestl } 59642c1b001SThomas Moestl return (0); 59742c1b001SThomas Moestl } 59842c1b001SThomas Moestl 5991ed3fed7SMarius Strobl static void 6009ba2b298SMarius Strobl gem_reset(struct gem_softc *sc) 60142c1b001SThomas Moestl { 60242c1b001SThomas Moestl 60318100346SThomas Moestl #ifdef GEM_DEBUG 60412fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 60518100346SThomas Moestl #endif 60642c1b001SThomas Moestl gem_reset_rx(sc); 60742c1b001SThomas Moestl gem_reset_tx(sc); 60842c1b001SThomas Moestl 6092a79fd39SMarius Strobl /* Do a full reset. */ 610bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 611ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 612ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 613bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 61442c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset device\n"); 61542c1b001SThomas Moestl } 61642c1b001SThomas Moestl 61742c1b001SThomas Moestl static void 6182a79fd39SMarius Strobl gem_rxdrain(struct gem_softc *sc) 61942c1b001SThomas Moestl { 62042c1b001SThomas Moestl struct gem_rxsoft *rxs; 62142c1b001SThomas Moestl int i; 62242c1b001SThomas Moestl 62342c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 62442c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 62542c1b001SThomas Moestl if (rxs->rxs_mbuf != NULL) { 626b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 627b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 628305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 62942c1b001SThomas Moestl m_freem(rxs->rxs_mbuf); 63042c1b001SThomas Moestl rxs->rxs_mbuf = NULL; 63142c1b001SThomas Moestl } 63242c1b001SThomas Moestl } 63342c1b001SThomas Moestl } 63442c1b001SThomas Moestl 63542c1b001SThomas Moestl static void 6362a79fd39SMarius Strobl gem_stop(struct ifnet *ifp, int disable) 63742c1b001SThomas Moestl { 6382a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 63942c1b001SThomas Moestl struct gem_txsoft *txs; 64042c1b001SThomas Moestl 64118100346SThomas Moestl #ifdef GEM_DEBUG 64212fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 64318100346SThomas Moestl #endif 64442c1b001SThomas Moestl 64542c1b001SThomas Moestl callout_stop(&sc->sc_tick_ch); 6461f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 6471f317bf9SMarius Strobl callout_stop(&sc->sc_rx_ch); 6481f317bf9SMarius Strobl #endif 64942c1b001SThomas Moestl 6509ba2b298SMarius Strobl gem_reset_tx(sc); 6519ba2b298SMarius Strobl gem_reset_rx(sc); 65242c1b001SThomas Moestl 65342c1b001SThomas Moestl /* 65442c1b001SThomas Moestl * Release any queued transmit buffers. 65542c1b001SThomas Moestl */ 65642c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 65742c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 65842c1b001SThomas Moestl if (txs->txs_ndescs != 0) { 659b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 660b2d59f42SThomas Moestl BUS_DMASYNC_POSTWRITE); 661305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 66242c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 66342c1b001SThomas Moestl m_freem(txs->txs_mbuf); 66442c1b001SThomas Moestl txs->txs_mbuf = NULL; 66542c1b001SThomas Moestl } 66642c1b001SThomas Moestl } 66742c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 66842c1b001SThomas Moestl } 66942c1b001SThomas Moestl 67042c1b001SThomas Moestl if (disable) 67142c1b001SThomas Moestl gem_rxdrain(sc); 67242c1b001SThomas Moestl 67342c1b001SThomas Moestl /* 67442c1b001SThomas Moestl * Mark the interface down and cancel the watchdog timer. 67542c1b001SThomas Moestl */ 67613f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 6771ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 6788cb37876SMarius Strobl sc->sc_wdog_timer = 0; 67942c1b001SThomas Moestl } 68042c1b001SThomas Moestl 6811ed3fed7SMarius Strobl static int 6822a79fd39SMarius Strobl gem_reset_rx(struct gem_softc *sc) 68342c1b001SThomas Moestl { 68442c1b001SThomas Moestl 68542c1b001SThomas Moestl /* 68642c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 68742c1b001SThomas Moestl * disable DMA first. 68842c1b001SThomas Moestl */ 68942c1b001SThomas Moestl gem_disable_rx(sc); 690bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 0); 691ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_RX_CONFIG, 4, 692ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 693bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_RX_CONFIG, GEM_RX_CONFIG_RXDMA_EN, 0)) 6941ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX DMA\n"); 69542c1b001SThomas Moestl 696801772ecSMarius Strobl /* Finally, reset the ERX. */ 697bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX); 698ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 699ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 700bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 701bd3d9826SMarius Strobl 0)) { 70242c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 70342c1b001SThomas Moestl return (1); 70442c1b001SThomas Moestl } 70542c1b001SThomas Moestl return (0); 70642c1b001SThomas Moestl } 70742c1b001SThomas Moestl 7081ed3fed7SMarius Strobl /* 7091ed3fed7SMarius Strobl * Reset the receiver DMA engine. 7101ed3fed7SMarius Strobl * 7111ed3fed7SMarius Strobl * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW 7121ed3fed7SMarius Strobl * etc in order to reset the receiver DMA engine only and not do a full 7131ed3fed7SMarius Strobl * reset which amongst others also downs the link and clears the FIFOs. 7141ed3fed7SMarius Strobl */ 7151ed3fed7SMarius Strobl static void 7161ed3fed7SMarius Strobl gem_reset_rxdma(struct gem_softc *sc) 7171ed3fed7SMarius Strobl { 7181ed3fed7SMarius Strobl int i; 7191ed3fed7SMarius Strobl 7201ed3fed7SMarius Strobl if (gem_reset_rx(sc) != 0) 7211ed3fed7SMarius Strobl return (gem_init_locked(sc)); 7221ed3fed7SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 7231ed3fed7SMarius Strobl if (sc->sc_rxsoft[i].rxs_mbuf != NULL) 7241ed3fed7SMarius Strobl GEM_UPDATE_RXDESC(sc, i); 7251ed3fed7SMarius Strobl sc->sc_rxptr = 0; 7269ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 7271ed3fed7SMarius Strobl 7281ed3fed7SMarius Strobl /* NOTE: we use only 32-bit DMA addresses here. */ 729bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 730bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 731bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 732bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 7331ed3fed7SMarius Strobl gem_ringsize(GEM_NRXDESC /* XXX */) | 7341ed3fed7SMarius Strobl ((ETHER_HDR_LEN + sizeof(struct ip)) << 7351ed3fed7SMarius Strobl GEM_RX_CONFIG_CXM_START_SHFT) | 7361ed3fed7SMarius Strobl (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 7379ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT)); 7389ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 739bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 7409ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 7419ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 742bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 7432a79fd39SMarius Strobl (3 * sc->sc_rxfifosize / 256) | 7442a79fd39SMarius Strobl ((sc->sc_rxfifosize / 256) << 12)); 745bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 746bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_CONFIG) | GEM_RX_CONFIG_RXDMA_EN); 747bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 7481ed3fed7SMarius Strobl GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 749bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 750bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) | GEM_MAC_RX_ENABLE); 7511ed3fed7SMarius Strobl } 75242c1b001SThomas Moestl 75342c1b001SThomas Moestl static int 7542a79fd39SMarius Strobl gem_reset_tx(struct gem_softc *sc) 75542c1b001SThomas Moestl { 75642c1b001SThomas Moestl 75742c1b001SThomas Moestl /* 75842c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 75942c1b001SThomas Moestl * disable DMA first. 76042c1b001SThomas Moestl */ 76142c1b001SThomas Moestl gem_disable_tx(sc); 762bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, 0); 763ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_TX_CONFIG, 4, 764ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 765bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_TX_CONFIG, GEM_TX_CONFIG_TXDMA_EN, 0)) 7661ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX DMA\n"); 76742c1b001SThomas Moestl 768801772ecSMarius Strobl /* Finally, reset the ETX. */ 769bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_TX); 770ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 771ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 772bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 773bd3d9826SMarius Strobl 0)) { 7741ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot reset transmitter\n"); 77542c1b001SThomas Moestl return (1); 77642c1b001SThomas Moestl } 77742c1b001SThomas Moestl return (0); 77842c1b001SThomas Moestl } 77942c1b001SThomas Moestl 78042c1b001SThomas Moestl static int 7812a79fd39SMarius Strobl gem_disable_rx(struct gem_softc *sc) 78242c1b001SThomas Moestl { 78342c1b001SThomas Moestl 784bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 785bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) & ~GEM_MAC_RX_ENABLE); 786ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 787ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 788bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 789bd3d9826SMarius Strobl 0)); 79042c1b001SThomas Moestl } 79142c1b001SThomas Moestl 79242c1b001SThomas Moestl static int 7932a79fd39SMarius Strobl gem_disable_tx(struct gem_softc *sc) 79442c1b001SThomas Moestl { 79542c1b001SThomas Moestl 796bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 797bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG) & ~GEM_MAC_TX_ENABLE); 798ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 799ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 800bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 801bd3d9826SMarius Strobl 0)); 80242c1b001SThomas Moestl } 80342c1b001SThomas Moestl 80442c1b001SThomas Moestl static int 8059ba2b298SMarius Strobl gem_meminit(struct gem_softc *sc) 80642c1b001SThomas Moestl { 80742c1b001SThomas Moestl struct gem_rxsoft *rxs; 8082a79fd39SMarius Strobl int error, i; 80942c1b001SThomas Moestl 8109ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 8119ba2b298SMarius Strobl 81242c1b001SThomas Moestl /* 81342c1b001SThomas Moestl * Initialize the transmit descriptor ring. 81442c1b001SThomas Moestl */ 81542c1b001SThomas Moestl for (i = 0; i < GEM_NTXDESC; i++) { 81642c1b001SThomas Moestl sc->sc_txdescs[i].gd_flags = 0; 81742c1b001SThomas Moestl sc->sc_txdescs[i].gd_addr = 0; 81842c1b001SThomas Moestl } 819305f2c06SThomas Moestl sc->sc_txfree = GEM_MAXTXFREE; 82042c1b001SThomas Moestl sc->sc_txnext = 0; 821336cca9eSBenno Rice sc->sc_txwin = 0; 82242c1b001SThomas Moestl 82342c1b001SThomas Moestl /* 82442c1b001SThomas Moestl * Initialize the receive descriptor and receive job 82542c1b001SThomas Moestl * descriptor rings. 82642c1b001SThomas Moestl */ 82742c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 82842c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 82942c1b001SThomas Moestl if (rxs->rxs_mbuf == NULL) { 83042c1b001SThomas Moestl if ((error = gem_add_rxbuf(sc, i)) != 0) { 8312a79fd39SMarius Strobl device_printf(sc->sc_dev, 8322a79fd39SMarius Strobl "unable to allocate or map RX buffer %d, " 8332a79fd39SMarius Strobl "error = %d\n", i, error); 83442c1b001SThomas Moestl /* 8352a79fd39SMarius Strobl * XXX we should attempt to run with fewer 8362a79fd39SMarius Strobl * receive buffers instead of just failing. 83742c1b001SThomas Moestl */ 83842c1b001SThomas Moestl gem_rxdrain(sc); 83942c1b001SThomas Moestl return (1); 84042c1b001SThomas Moestl } 84142c1b001SThomas Moestl } else 84242c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 84342c1b001SThomas Moestl } 84442c1b001SThomas Moestl sc->sc_rxptr = 0; 8459ba2b298SMarius Strobl 8469ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 84742c1b001SThomas Moestl 84842c1b001SThomas Moestl return (0); 84942c1b001SThomas Moestl } 85042c1b001SThomas Moestl 8511ed3fed7SMarius Strobl static u_int 8522a79fd39SMarius Strobl gem_ringsize(u_int sz) 85342c1b001SThomas Moestl { 85442c1b001SThomas Moestl 85542c1b001SThomas Moestl switch (sz) { 85642c1b001SThomas Moestl case 32: 8571ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 85842c1b001SThomas Moestl case 64: 8591ed3fed7SMarius Strobl return (GEM_RING_SZ_64); 86042c1b001SThomas Moestl case 128: 8611ed3fed7SMarius Strobl return (GEM_RING_SZ_128); 86242c1b001SThomas Moestl case 256: 8631ed3fed7SMarius Strobl return (GEM_RING_SZ_256); 86442c1b001SThomas Moestl case 512: 8651ed3fed7SMarius Strobl return (GEM_RING_SZ_512); 86642c1b001SThomas Moestl case 1024: 8671ed3fed7SMarius Strobl return (GEM_RING_SZ_1024); 86842c1b001SThomas Moestl case 2048: 8691ed3fed7SMarius Strobl return (GEM_RING_SZ_2048); 87042c1b001SThomas Moestl case 4096: 8711ed3fed7SMarius Strobl return (GEM_RING_SZ_4096); 87242c1b001SThomas Moestl case 8192: 8731ed3fed7SMarius Strobl return (GEM_RING_SZ_8192); 87442c1b001SThomas Moestl default: 8751ed3fed7SMarius Strobl printf("%s: invalid ring size %d\n", __func__, sz); 8761ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 87742c1b001SThomas Moestl } 87842c1b001SThomas Moestl } 87942c1b001SThomas Moestl 88042c1b001SThomas Moestl static void 8812a79fd39SMarius Strobl gem_init(void *xsc) 88242c1b001SThomas Moestl { 8832a79fd39SMarius Strobl struct gem_softc *sc = xsc; 8848cfaff7dSMarius Strobl 8858cfaff7dSMarius Strobl GEM_LOCK(sc); 8868cfaff7dSMarius Strobl gem_init_locked(sc); 8878cfaff7dSMarius Strobl GEM_UNLOCK(sc); 8888cfaff7dSMarius Strobl } 8898cfaff7dSMarius Strobl 8908cfaff7dSMarius Strobl /* 8918cfaff7dSMarius Strobl * Initialization of interface; set up initialization block 8928cfaff7dSMarius Strobl * and transmit/receive descriptor rings. 8938cfaff7dSMarius Strobl */ 8948cfaff7dSMarius Strobl static void 8952a79fd39SMarius Strobl gem_init_locked(struct gem_softc *sc) 8968cfaff7dSMarius Strobl { 897fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 8982a79fd39SMarius Strobl uint32_t v; 89942c1b001SThomas Moestl 9008cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 90142c1b001SThomas Moestl 90218100346SThomas Moestl #ifdef GEM_DEBUG 90312fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: calling stop", device_get_name(sc->sc_dev), 90412fb0330SPyun YongHyeon __func__); 90518100346SThomas Moestl #endif 90642c1b001SThomas Moestl /* 90742c1b001SThomas Moestl * Initialization sequence. The numbered steps below correspond 90842c1b001SThomas Moestl * to the sequence outlined in section 6.3.5.1 in the Ethernet 90942c1b001SThomas Moestl * Channel Engine manual (part of the PCIO manual). 91042c1b001SThomas Moestl * See also the STP2002-STQ document from Sun Microsystems. 91142c1b001SThomas Moestl */ 91242c1b001SThomas Moestl 9132a79fd39SMarius Strobl /* step 1 & 2. Reset the Ethernet Channel. */ 914ccb1212aSMarius Strobl gem_stop(ifp, 0); 91542c1b001SThomas Moestl gem_reset(sc); 91618100346SThomas Moestl #ifdef GEM_DEBUG 91712fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: restarting", device_get_name(sc->sc_dev), 91812fb0330SPyun YongHyeon __func__); 91918100346SThomas Moestl #endif 92042c1b001SThomas Moestl 9212a79fd39SMarius Strobl /* Re-initialize the MIF. */ 92242c1b001SThomas Moestl gem_mifinit(sc); 92342c1b001SThomas Moestl 9242a79fd39SMarius Strobl /* step 3. Setup data structures in host memory. */ 9251ed3fed7SMarius Strobl if (gem_meminit(sc) != 0) 9261ed3fed7SMarius Strobl return; 92742c1b001SThomas Moestl 92842c1b001SThomas Moestl /* step 4. TX MAC registers & counters */ 92942c1b001SThomas Moestl gem_init_regs(sc); 93042c1b001SThomas Moestl 93142c1b001SThomas Moestl /* step 5. RX MAC registers & counters */ 93242c1b001SThomas Moestl gem_setladrf(sc); 93342c1b001SThomas Moestl 9342a79fd39SMarius Strobl /* step 6 & 7. Program Descriptor Ring Base Addresses. */ 93542c1b001SThomas Moestl /* NOTE: we use only 32-bit DMA addresses here. */ 936bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_HI, 0); 937bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 93842c1b001SThomas Moestl 939bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 940bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 94118100346SThomas Moestl #ifdef GEM_DEBUG 9422a79fd39SMarius Strobl CTR3(KTR_GEM, "loading RX ring %lx, TX ring %lx, cddma %lx", 94342c1b001SThomas Moestl GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 94418100346SThomas Moestl #endif 94542c1b001SThomas Moestl 94642c1b001SThomas Moestl /* step 8. Global Configuration & Interrupt Mask */ 9479ba2b298SMarius Strobl 9489ba2b298SMarius Strobl /* 9499ba2b298SMarius Strobl * Set the internal arbitration to "infinite" bursts of the 9509ba2b298SMarius Strobl * maximum length of 31 * 64 bytes so DMA transfers aren't 9519ba2b298SMarius Strobl * split up in cache line size chunks. This greatly improves 9529ba2b298SMarius Strobl * RX performance. 9539ba2b298SMarius Strobl * Enable silicon bug workarounds for the Apple variants. 9549ba2b298SMarius Strobl */ 9559ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_CONFIG, 9569ba2b298SMarius Strobl GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT | 9579ba2b298SMarius Strobl ((sc->sc_flags & GEM_PCI) != 0 ? GEM_CONFIG_BURST_INF : 9589ba2b298SMarius Strobl GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ? 9599ba2b298SMarius Strobl GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0)); 9609ba2b298SMarius Strobl 961bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_INTMASK, 9621ed3fed7SMarius Strobl ~(GEM_INTR_TX_INTME | GEM_INTR_TX_EMPTY | GEM_INTR_RX_DONE | 9631ed3fed7SMarius Strobl GEM_INTR_RX_NOBUF | GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | 9641ed3fed7SMarius Strobl GEM_INTR_BERR 9651ed3fed7SMarius Strobl #ifdef GEM_DEBUG 9661ed3fed7SMarius Strobl | GEM_INTR_PCS | GEM_INTR_MIF 9671ed3fed7SMarius Strobl #endif 9681ed3fed7SMarius Strobl )); 969bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 970336cca9eSBenno Rice GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 971bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_MASK, 9729ba2b298SMarius Strobl GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 9739ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP); 9741ed3fed7SMarius Strobl #ifdef GEM_DEBUG 975bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9761ed3fed7SMarius Strobl ~(GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME)); 9771ed3fed7SMarius Strobl #else 978bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9791ed3fed7SMarius Strobl GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME); 9801ed3fed7SMarius Strobl #endif 98142c1b001SThomas Moestl 9822a79fd39SMarius Strobl /* step 9. ETX Configuration: use mostly default values. */ 98342c1b001SThomas Moestl 9842a79fd39SMarius Strobl /* Enable DMA. */ 9859ba2b298SMarius Strobl v = gem_ringsize(GEM_NTXDESC); 9869ba2b298SMarius Strobl /* Set TX FIFO threshold and enable DMA. */ 987ccb1212aSMarius Strobl v |= ((sc->sc_variant == GEM_SUN_ERI ? 0x100 : 0x4ff) << 10) & 988ccb1212aSMarius Strobl GEM_TX_CONFIG_TXFIFO_TH; 989ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, v | GEM_TX_CONFIG_TXDMA_EN); 99042c1b001SThomas Moestl 99142c1b001SThomas Moestl /* step 10. ERX Configuration */ 99242c1b001SThomas Moestl 9931ed3fed7SMarius Strobl /* Encode Receive Descriptor ring size. */ 99442c1b001SThomas Moestl v = gem_ringsize(GEM_NRXDESC /* XXX */); 9952a79fd39SMarius Strobl /* RX TCP/UDP checksum offset */ 99612fb0330SPyun YongHyeon v |= ((ETHER_HDR_LEN + sizeof(struct ip)) << 99712fb0330SPyun YongHyeon GEM_RX_CONFIG_CXM_START_SHFT); 9989ba2b298SMarius Strobl /* Set RX FIFO threshold, set first byte offset and enable DMA. */ 999bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 100042c1b001SThomas Moestl v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 10019ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT) | 10029ba2b298SMarius Strobl GEM_RX_CONFIG_RXDMA_EN); 10031ed3fed7SMarius Strobl 10049ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 1005bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 10069ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 10079ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 10081ed3fed7SMarius Strobl 100942c1b001SThomas Moestl /* 1010336cca9eSBenno Rice * The following value is for an OFF Threshold of about 3/4 full 1011336cca9eSBenno Rice * and an ON Threshold of 1/4 full. 101242c1b001SThomas Moestl */ 1013bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 1014336cca9eSBenno Rice (3 * sc->sc_rxfifosize / 256) | 1015336cca9eSBenno Rice ((sc->sc_rxfifosize / 256) << 12)); 101642c1b001SThomas Moestl 10172a79fd39SMarius Strobl /* step 11. Configure Media. */ 101842c1b001SThomas Moestl 101942c1b001SThomas Moestl /* step 12. RX_MAC Configuration Register */ 1020bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 1021ccb1212aSMarius Strobl v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC; 1022bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 1023ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 1024ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1025bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 1026ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure RX MAC\n"); 1027bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 102842c1b001SThomas Moestl 1029ccb1212aSMarius Strobl /* step 13. TX_MAC Configuration Register */ 1030ccb1212aSMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG); 1031ccb1212aSMarius Strobl v |= GEM_MAC_TX_ENABLE; 1032ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 1033ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 1034ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1035ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 1036ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure TX MAC\n"); 1037ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, v); 1038ccb1212aSMarius Strobl 10392a79fd39SMarius Strobl /* step 14. Issue Transmit Pending command. */ 104042c1b001SThomas Moestl 10412a79fd39SMarius Strobl /* step 15. Give the reciever a swift kick. */ 1042bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 104342c1b001SThomas Moestl 104413f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 104513f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 10461ed3fed7SMarius Strobl 10471ed3fed7SMarius Strobl mii_mediachg(sc->sc_mii); 10481ed3fed7SMarius Strobl 10491ed3fed7SMarius Strobl /* Start the one second timer. */ 10501ed3fed7SMarius Strobl sc->sc_wdog_timer = 0; 10511ed3fed7SMarius Strobl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 105242c1b001SThomas Moestl } 105342c1b001SThomas Moestl 105412fb0330SPyun YongHyeon static int 10552a79fd39SMarius Strobl gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head) 105612fb0330SPyun YongHyeon { 105712fb0330SPyun YongHyeon bus_dma_segment_t txsegs[GEM_NTXSEGS]; 10582a79fd39SMarius Strobl struct gem_txsoft *txs; 1059ccb1212aSMarius Strobl struct ip *ip; 106012fb0330SPyun YongHyeon struct mbuf *m; 10612a79fd39SMarius Strobl uint64_t cflags, flags; 1062ccb1212aSMarius Strobl int error, nexttx, nsegs, offset, seg; 106342c1b001SThomas Moestl 10649ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 10659ba2b298SMarius Strobl 106642c1b001SThomas Moestl /* Get a work queue entry. */ 106742c1b001SThomas Moestl if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 1068305f2c06SThomas Moestl /* Ran out of descriptors. */ 106912fb0330SPyun YongHyeon return (ENOBUFS); 1070305f2c06SThomas Moestl } 1071ccb1212aSMarius Strobl 1072ccb1212aSMarius Strobl cflags = 0; 1073ccb1212aSMarius Strobl if (((*m_head)->m_pkthdr.csum_flags & sc->sc_csum_features) != 0) { 1074ccb1212aSMarius Strobl if (M_WRITABLE(*m_head) == 0) { 1075ccb1212aSMarius Strobl m = m_dup(*m_head, M_DONTWAIT); 1076ccb1212aSMarius Strobl m_freem(*m_head); 1077ccb1212aSMarius Strobl *m_head = m; 1078ccb1212aSMarius Strobl if (m == NULL) 1079ccb1212aSMarius Strobl return (ENOBUFS); 1080ccb1212aSMarius Strobl } 1081ccb1212aSMarius Strobl offset = sizeof(struct ether_header); 1082ccb1212aSMarius Strobl m = m_pullup(*m_head, offset + sizeof(struct ip)); 1083ccb1212aSMarius Strobl if (m == NULL) { 1084ccb1212aSMarius Strobl *m_head = NULL; 1085ccb1212aSMarius Strobl return (ENOBUFS); 1086ccb1212aSMarius Strobl } 1087ccb1212aSMarius Strobl ip = (struct ip *)(mtod(m, caddr_t) + offset); 1088ccb1212aSMarius Strobl offset += (ip->ip_hl << 2); 1089ccb1212aSMarius Strobl cflags = offset << GEM_TD_CXSUM_STARTSHFT | 1090ccb1212aSMarius Strobl ((offset + m->m_pkthdr.csum_data) << 1091ccb1212aSMarius Strobl GEM_TD_CXSUM_STUFFSHFT) | GEM_TD_CXSUM_ENABLE; 1092ccb1212aSMarius Strobl *m_head = m; 1093ccb1212aSMarius Strobl } 1094ccb1212aSMarius Strobl 109512fb0330SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap, 109612fb0330SPyun YongHyeon *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 109712fb0330SPyun YongHyeon if (error == EFBIG) { 1098304a4c6fSJohn Baldwin m = m_collapse(*m_head, M_DONTWAIT, GEM_NTXSEGS); 109912fb0330SPyun YongHyeon if (m == NULL) { 110012fb0330SPyun YongHyeon m_freem(*m_head); 110112fb0330SPyun YongHyeon *m_head = NULL; 110212fb0330SPyun YongHyeon return (ENOBUFS); 110312fb0330SPyun YongHyeon } 110412fb0330SPyun YongHyeon *m_head = m; 11052a79fd39SMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, 11062a79fd39SMarius Strobl txs->txs_dmamap, *m_head, txsegs, &nsegs, 11072a79fd39SMarius Strobl BUS_DMA_NOWAIT); 110812fb0330SPyun YongHyeon if (error != 0) { 110912fb0330SPyun YongHyeon m_freem(*m_head); 111012fb0330SPyun YongHyeon *m_head = NULL; 111112fb0330SPyun YongHyeon return (error); 111212fb0330SPyun YongHyeon } 111312fb0330SPyun YongHyeon } else if (error != 0) 111412fb0330SPyun YongHyeon return (error); 1115801772ecSMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1116801772ecSMarius Strobl KASSERT(nsegs <= GEM_NTXSEGS, 1117801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 111812fb0330SPyun YongHyeon if (nsegs == 0) { 111912fb0330SPyun YongHyeon m_freem(*m_head); 112012fb0330SPyun YongHyeon *m_head = NULL; 112112fb0330SPyun YongHyeon return (EIO); 112212fb0330SPyun YongHyeon } 112312fb0330SPyun YongHyeon 112412fb0330SPyun YongHyeon /* 112512fb0330SPyun YongHyeon * Ensure we have enough descriptors free to describe 112612fb0330SPyun YongHyeon * the packet. Note, we always reserve one descriptor 11272a79fd39SMarius Strobl * at the end of the ring as a termination point, in 11282a79fd39SMarius Strobl * order to prevent wrap-around. 112912fb0330SPyun YongHyeon */ 113012fb0330SPyun YongHyeon if (nsegs > sc->sc_txfree - 1) { 113112fb0330SPyun YongHyeon txs->txs_ndescs = 0; 113212fb0330SPyun YongHyeon bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 113312fb0330SPyun YongHyeon return (ENOBUFS); 113412fb0330SPyun YongHyeon } 113512fb0330SPyun YongHyeon 113612fb0330SPyun YongHyeon txs->txs_ndescs = nsegs; 1137305f2c06SThomas Moestl txs->txs_firstdesc = sc->sc_txnext; 113812fb0330SPyun YongHyeon nexttx = txs->txs_firstdesc; 113912fb0330SPyun YongHyeon for (seg = 0; seg < nsegs; seg++, nexttx = GEM_NEXTTX(nexttx)) { 114012fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11412a79fd39SMarius Strobl CTR6(KTR_GEM, 11422a79fd39SMarius Strobl "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)", 11432a79fd39SMarius Strobl __func__, seg, nexttx, txsegs[seg].ds_len, 11442a79fd39SMarius Strobl txsegs[seg].ds_addr, 114512fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr)); 114612fb0330SPyun YongHyeon #endif 114712fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_addr = 114812fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr); 114912fb0330SPyun YongHyeon KASSERT(txsegs[seg].ds_len < GEM_TD_BUFSIZE, 115012fb0330SPyun YongHyeon ("%s: segment size too large!", __func__)); 115112fb0330SPyun YongHyeon flags = txsegs[seg].ds_len & GEM_TD_BUFSIZE; 115212fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_flags = 115312fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, flags | cflags); 115412fb0330SPyun YongHyeon txs->txs_lastdesc = nexttx; 115542c1b001SThomas Moestl } 1156305f2c06SThomas Moestl 11572a79fd39SMarius Strobl /* Set EOP on the last descriptor. */ 115812fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11592a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: end of packet at segment %d, TX %d", 11602a79fd39SMarius Strobl __func__, seg, nexttx); 116112fb0330SPyun YongHyeon #endif 116212fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_lastdesc].gd_flags |= 116312fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_END_OF_PACKET); 116412fb0330SPyun YongHyeon 11652a79fd39SMarius Strobl /* Lastly set SOP on the first descriptor. */ 116612fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11672a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: start of packet at segment %d, TX %d", 11682a79fd39SMarius Strobl __func__, seg, nexttx); 116912fb0330SPyun YongHyeon #endif 117012fb0330SPyun YongHyeon if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 117112fb0330SPyun YongHyeon sc->sc_txwin = 0; 117212fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 117312fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_INTERRUPT_ME | 117412fb0330SPyun YongHyeon GEM_TD_START_OF_PACKET); 117512fb0330SPyun YongHyeon } else 117612fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 117712fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_START_OF_PACKET); 117812fb0330SPyun YongHyeon 117942c1b001SThomas Moestl /* Sync the DMA map. */ 11802a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 11812a79fd39SMarius Strobl BUS_DMASYNC_PREWRITE); 1182305f2c06SThomas Moestl 118318100346SThomas Moestl #ifdef GEM_DEBUG 118412fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d", 11852a79fd39SMarius Strobl __func__, txs->txs_firstdesc, txs->txs_lastdesc, 11862a79fd39SMarius Strobl txs->txs_ndescs); 118718100346SThomas Moestl #endif 118842c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 1189305f2c06SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 119012fb0330SPyun YongHyeon txs->txs_mbuf = *m_head; 1191305f2c06SThomas Moestl 1192305f2c06SThomas Moestl sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc); 1193305f2c06SThomas Moestl sc->sc_txfree -= txs->txs_ndescs; 119442c1b001SThomas Moestl 119512fb0330SPyun YongHyeon return (0); 119642c1b001SThomas Moestl } 119742c1b001SThomas Moestl 119842c1b001SThomas Moestl static void 11992a79fd39SMarius Strobl gem_init_regs(struct gem_softc *sc) 120042c1b001SThomas Moestl { 12014a0d6638SRuslan Ermilov const u_char *laddr = IF_LLADDR(sc->sc_ifp); 120242c1b001SThomas Moestl 12039ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 12049ba2b298SMarius Strobl 12052a79fd39SMarius Strobl /* These registers are not cleared on reset. */ 12061ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_INITED) == 0) { 12072a79fd39SMarius Strobl /* magic values */ 1208bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG0, 0); 1209bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG1, 8); 1210bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG2, 4); 121142c1b001SThomas Moestl 12129ba2b298SMarius Strobl /* min frame length */ 1213bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 12149ba2b298SMarius Strobl /* max frame length and max burst size */ 1215bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MAX_FRAME, 12161ed3fed7SMarius Strobl (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) | (0x2000 << 16)); 1217336cca9eSBenno Rice 12189ba2b298SMarius Strobl /* more magic values */ 1219bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PREAMBLE_LEN, 0x7); 1220bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_JAM_SIZE, 0x4); 1221bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ATTEMPT_LIMIT, 0x10); 1222bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_TYPE, 0x8088); 12239ba2b298SMarius Strobl 12249ba2b298SMarius Strobl /* random number seed */ 1225bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RANDOM_SEED, 1226336cca9eSBenno Rice ((laddr[5] << 8) | laddr[4]) & 0x3ff); 1227336cca9eSBenno Rice 12282a79fd39SMarius Strobl /* secondary MAC address: 0:0:0:0:0:0 */ 1229bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR3, 0); 1230bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR4, 0); 1231bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR5, 0); 1232336cca9eSBenno Rice 12332a79fd39SMarius Strobl /* MAC control address: 01:80:c2:00:00:01 */ 1234bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR6, 0x0001); 1235bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR7, 0xc200); 1236bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR8, 0x0180); 123742c1b001SThomas Moestl 12382a79fd39SMarius Strobl /* MAC filter address: 0:0:0:0:0:0 */ 1239bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER0, 0); 1240bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER1, 0); 1241bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER2, 0); 1242bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK1_2, 0); 1243bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK0, 0); 124442c1b001SThomas Moestl 12451ed3fed7SMarius Strobl sc->sc_flags |= GEM_INITED; 124642c1b001SThomas Moestl } 124742c1b001SThomas Moestl 12482a79fd39SMarius Strobl /* Counters need to be zeroed. */ 1249bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 1250bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 1251bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 1252bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 1253bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_DEFER_TMR_CNT, 0); 1254bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PEAK_ATTEMPTS, 0); 1255bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_FRAME_COUNT, 0); 1256bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 1257bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 1258bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 1259bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 126042c1b001SThomas Moestl 12611ed3fed7SMarius Strobl /* Set XOFF PAUSE time. */ 1262bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 12631ed3fed7SMarius Strobl 12642a79fd39SMarius Strobl /* Set the station address. */ 1265bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR0, (laddr[4] << 8) | laddr[5]); 1266bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR1, (laddr[2] << 8) | laddr[3]); 1267bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR2, (laddr[0] << 8) | laddr[1]); 1268336cca9eSBenno Rice 12691ed3fed7SMarius Strobl /* Enable MII outputs. */ 1270bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, GEM_MAC_XIF_TX_MII_ENA); 127142c1b001SThomas Moestl } 127242c1b001SThomas Moestl 127342c1b001SThomas Moestl static void 12742a79fd39SMarius Strobl gem_start(struct ifnet *ifp) 127542c1b001SThomas Moestl { 12762a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 12778cfaff7dSMarius Strobl 12788cfaff7dSMarius Strobl GEM_LOCK(sc); 12798cfaff7dSMarius Strobl gem_start_locked(ifp); 12808cfaff7dSMarius Strobl GEM_UNLOCK(sc); 12818cfaff7dSMarius Strobl } 12828cfaff7dSMarius Strobl 12839ba2b298SMarius Strobl static inline void 12849ba2b298SMarius Strobl gem_txkick(struct gem_softc *sc) 12859ba2b298SMarius Strobl { 12869ba2b298SMarius Strobl 12879ba2b298SMarius Strobl /* 12889ba2b298SMarius Strobl * Update the TX kick register. This register has to point to the 12899ba2b298SMarius Strobl * descriptor after the last valid one and for optimum performance 12909ba2b298SMarius Strobl * should be incremented in multiples of 4 (the DMA engine fetches/ 12919ba2b298SMarius Strobl * updates descriptors in batches of 4). 12929ba2b298SMarius Strobl */ 12939ba2b298SMarius Strobl #ifdef GEM_DEBUG 12949ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: %s: kicking TX %d", 12959ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txnext); 12969ba2b298SMarius Strobl #endif 12979ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 12989ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_KICK, sc->sc_txnext); 12999ba2b298SMarius Strobl } 13009ba2b298SMarius Strobl 13018cfaff7dSMarius Strobl static void 13022a79fd39SMarius Strobl gem_start_locked(struct ifnet *ifp) 13038cfaff7dSMarius Strobl { 13042a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 130512fb0330SPyun YongHyeon struct mbuf *m; 13069ba2b298SMarius Strobl int kicked, ntx; 13079ba2b298SMarius Strobl 13089ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 130942c1b001SThomas Moestl 131013f4c340SRobert Watson if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 13111ed3fed7SMarius Strobl IFF_DRV_RUNNING || (sc->sc_flags & GEM_LINK) == 0) 131242c1b001SThomas Moestl return; 131342c1b001SThomas Moestl 131418100346SThomas Moestl #ifdef GEM_DEBUG 131512fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: txfree %d, txnext %d", 13161ed3fed7SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txfree, 13171ed3fed7SMarius Strobl sc->sc_txnext); 131818100346SThomas Moestl #endif 13192a79fd39SMarius Strobl ntx = 0; 13209ba2b298SMarius Strobl kicked = 0; 132112fb0330SPyun YongHyeon for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && sc->sc_txfree > 1;) { 132212fb0330SPyun YongHyeon IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 132312fb0330SPyun YongHyeon if (m == NULL) 132442c1b001SThomas Moestl break; 13251ed3fed7SMarius Strobl if (gem_load_txmbuf(sc, &m) != 0) { 132612fb0330SPyun YongHyeon if (m == NULL) 132712fb0330SPyun YongHyeon break; 132812fb0330SPyun YongHyeon ifp->if_drv_flags |= IFF_DRV_OACTIVE; 132912fb0330SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m); 133042c1b001SThomas Moestl break; 133142c1b001SThomas Moestl } 13329ba2b298SMarius Strobl if ((sc->sc_txnext % 4) == 0) { 13339ba2b298SMarius Strobl gem_txkick(sc); 13349ba2b298SMarius Strobl kicked = 1; 13359ba2b298SMarius Strobl } else 13369ba2b298SMarius Strobl kicked = 0; 133718100346SThomas Moestl ntx++; 133812fb0330SPyun YongHyeon BPF_MTAP(ifp, m); 1339305f2c06SThomas Moestl } 1340305f2c06SThomas Moestl 1341305f2c06SThomas Moestl if (ntx > 0) { 13429ba2b298SMarius Strobl if (kicked == 0) 13439ba2b298SMarius Strobl gem_txkick(sc); 134418100346SThomas Moestl #ifdef GEM_DEBUG 1345305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d", 13461ed3fed7SMarius Strobl device_get_name(sc->sc_dev), sc->sc_txnext); 134718100346SThomas Moestl #endif 1348305f2c06SThomas Moestl 134942c1b001SThomas Moestl /* Set a watchdog timer in case the chip flakes out. */ 13508cb37876SMarius Strobl sc->sc_wdog_timer = 5; 135118100346SThomas Moestl #ifdef GEM_DEBUG 135212fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 13532a79fd39SMarius Strobl device_get_name(sc->sc_dev), __func__, 13542a79fd39SMarius Strobl sc->sc_wdog_timer); 135518100346SThomas Moestl #endif 135642c1b001SThomas Moestl } 135742c1b001SThomas Moestl } 135842c1b001SThomas Moestl 135942c1b001SThomas Moestl static void 13602a79fd39SMarius Strobl gem_tint(struct gem_softc *sc) 136142c1b001SThomas Moestl { 1362fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 136342c1b001SThomas Moestl struct gem_txsoft *txs; 13649ba2b298SMarius Strobl int progress; 13659ba2b298SMarius Strobl uint32_t txlast; 136618100346SThomas Moestl #ifdef GEM_DEBUG 13672a79fd39SMarius Strobl int i; 13682a79fd39SMarius Strobl 13699ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 13709ba2b298SMarius Strobl 137112fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 137218100346SThomas Moestl #endif 137342c1b001SThomas Moestl 137442c1b001SThomas Moestl /* 13752a79fd39SMarius Strobl * Go through our TX list and free mbufs for those 137642c1b001SThomas Moestl * frames that have been transmitted. 137742c1b001SThomas Moestl */ 13782a79fd39SMarius Strobl progress = 0; 1379b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 138042c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 138142c1b001SThomas Moestl #ifdef GEM_DEBUG 13822a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 138342c1b001SThomas Moestl printf(" txsoft %p transmit chain:\n", txs); 138442c1b001SThomas Moestl for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 138542c1b001SThomas Moestl printf("descriptor %d: ", i); 13862a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 13872a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 13882a79fd39SMarius Strobl sc->sc_txdescs[i].gd_flags)); 13892a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 13902a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 13912a79fd39SMarius Strobl sc->sc_txdescs[i].gd_addr)); 139242c1b001SThomas Moestl if (i == txs->txs_lastdesc) 139342c1b001SThomas Moestl break; 139442c1b001SThomas Moestl } 139542c1b001SThomas Moestl } 139642c1b001SThomas Moestl #endif 139742c1b001SThomas Moestl 139842c1b001SThomas Moestl /* 13991ed3fed7SMarius Strobl * In theory, we could harvest some descriptors before 140042c1b001SThomas Moestl * the ring is empty, but that's a bit complicated. 140142c1b001SThomas Moestl * 140242c1b001SThomas Moestl * GEM_TX_COMPLETION points to the last descriptor 140342c1b001SThomas Moestl * processed + 1. 140442c1b001SThomas Moestl */ 1405bd3d9826SMarius Strobl txlast = GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION); 140618100346SThomas Moestl #ifdef GEM_DEBUG 140712fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: txs->txs_firstdesc = %d, " 140842c1b001SThomas Moestl "txs->txs_lastdesc = %d, txlast = %d", 140912fb0330SPyun YongHyeon __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast); 141018100346SThomas Moestl #endif 141142c1b001SThomas Moestl if (txs->txs_firstdesc <= txs->txs_lastdesc) { 141242c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) && 141342c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 141442c1b001SThomas Moestl break; 141542c1b001SThomas Moestl } else { 14162a79fd39SMarius Strobl /* Ick -- this command wraps. */ 141742c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) || 141842c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 141942c1b001SThomas Moestl break; 142042c1b001SThomas Moestl } 142142c1b001SThomas Moestl 142218100346SThomas Moestl #ifdef GEM_DEBUG 14232a79fd39SMarius Strobl CTR1(KTR_GEM, "%s: releasing a descriptor", __func__); 142418100346SThomas Moestl #endif 142542c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 142642c1b001SThomas Moestl 142742c1b001SThomas Moestl sc->sc_txfree += txs->txs_ndescs; 142842c1b001SThomas Moestl 1429305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 143042c1b001SThomas Moestl BUS_DMASYNC_POSTWRITE); 1431305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 143242c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 143342c1b001SThomas Moestl m_freem(txs->txs_mbuf); 143442c1b001SThomas Moestl txs->txs_mbuf = NULL; 143542c1b001SThomas Moestl } 143642c1b001SThomas Moestl 143742c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 143842c1b001SThomas Moestl 143942c1b001SThomas Moestl ifp->if_opackets++; 1440336cca9eSBenno Rice progress = 1; 144142c1b001SThomas Moestl } 144242c1b001SThomas Moestl 144318100346SThomas Moestl #ifdef GEM_DEBUG 14442a79fd39SMarius Strobl CTR4(KTR_GEM, "%s: GEM_TX_STATE_MACHINE %x GEM_TX_DATA_PTR %llx " 144542c1b001SThomas Moestl "GEM_TX_COMPLETION %x", 1446bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_STATE_MACHINE), 1447bd3d9826SMarius Strobl ((long long)GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_HI) << 32) | 1448bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_LO), 1449bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION)); 145018100346SThomas Moestl #endif 145142c1b001SThomas Moestl 1452336cca9eSBenno Rice if (progress) { 1453336cca9eSBenno Rice if (sc->sc_txfree == GEM_NTXDESC - 1) 1454336cca9eSBenno Rice sc->sc_txwin = 0; 145542c1b001SThomas Moestl 14562a79fd39SMarius Strobl /* 14572a79fd39SMarius Strobl * We freed some descriptors, so reset IFF_DRV_OACTIVE 14582a79fd39SMarius Strobl * and restart. 14592a79fd39SMarius Strobl */ 146013f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 14619ba2b298SMarius Strobl if (STAILQ_EMPTY(&sc->sc_txdirtyq)) 14629ba2b298SMarius Strobl sc->sc_wdog_timer = 0; 146312fb0330SPyun YongHyeon gem_start_locked(ifp); 1464336cca9eSBenno Rice } 146542c1b001SThomas Moestl 146618100346SThomas Moestl #ifdef GEM_DEBUG 146712fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 146812fb0330SPyun YongHyeon device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer); 146918100346SThomas Moestl #endif 147042c1b001SThomas Moestl } 147142c1b001SThomas Moestl 1472c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 14730d80b9bdSThomas Moestl static void 14742a79fd39SMarius Strobl gem_rint_timeout(void *arg) 14750d80b9bdSThomas Moestl { 14762a79fd39SMarius Strobl struct gem_softc *sc = arg; 14770d80b9bdSThomas Moestl 14781f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 14799ba2b298SMarius Strobl 14808cfaff7dSMarius Strobl gem_rint(sc); 14810d80b9bdSThomas Moestl } 148211e3f060SJake Burkholder #endif 14830d80b9bdSThomas Moestl 148442c1b001SThomas Moestl static void 14852a79fd39SMarius Strobl gem_rint(struct gem_softc *sc) 148642c1b001SThomas Moestl { 1487fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 148842c1b001SThomas Moestl struct mbuf *m; 14892a79fd39SMarius Strobl uint64_t rxstat; 14902a79fd39SMarius Strobl uint32_t rxcomp; 149142c1b001SThomas Moestl 14929ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 14939ba2b298SMarius Strobl 1494c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 14950d80b9bdSThomas Moestl callout_stop(&sc->sc_rx_ch); 1496c3d5598aSMarius Strobl #endif 149718100346SThomas Moestl #ifdef GEM_DEBUG 149812fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 149918100346SThomas Moestl #endif 1500336cca9eSBenno Rice 1501336cca9eSBenno Rice /* 1502336cca9eSBenno Rice * Read the completion register once. This limits 1503336cca9eSBenno Rice * how long the following loop can execute. 1504336cca9eSBenno Rice */ 1505bd3d9826SMarius Strobl rxcomp = GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION); 150618100346SThomas Moestl #ifdef GEM_DEBUG 15079ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: sc->sc_rxptr %d, complete %d", 150812fb0330SPyun YongHyeon __func__, sc->sc_rxptr, rxcomp); 150918100346SThomas Moestl #endif 15109ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 15111ed3fed7SMarius Strobl for (; sc->sc_rxptr != rxcomp;) { 15121ed3fed7SMarius Strobl m = sc->sc_rxsoft[sc->sc_rxptr].rxs_mbuf; 15131ed3fed7SMarius Strobl rxstat = GEM_DMA_READ(sc, 15141ed3fed7SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags); 151542c1b001SThomas Moestl 151642c1b001SThomas Moestl if (rxstat & GEM_RD_OWN) { 1517c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 151842c1b001SThomas Moestl /* 15190d80b9bdSThomas Moestl * The descriptor is still marked as owned, although 15200d80b9bdSThomas Moestl * it is supposed to have completed. This has been 15210d80b9bdSThomas Moestl * observed on some machines. Just exiting here 15220d80b9bdSThomas Moestl * might leave the packet sitting around until another 15230d80b9bdSThomas Moestl * one arrives to trigger a new interrupt, which is 15240d80b9bdSThomas Moestl * generally undesirable, so set up a timeout. 152542c1b001SThomas Moestl */ 15260d80b9bdSThomas Moestl callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 15270d80b9bdSThomas Moestl gem_rint_timeout, sc); 1528336cca9eSBenno Rice #endif 15291ed3fed7SMarius Strobl m = NULL; 15301ed3fed7SMarius Strobl goto kickit; 153142c1b001SThomas Moestl } 153242c1b001SThomas Moestl 153342c1b001SThomas Moestl if (rxstat & GEM_RD_BAD_CRC) { 1534336cca9eSBenno Rice ifp->if_ierrors++; 153542c1b001SThomas Moestl device_printf(sc->sc_dev, "receive error: CRC error\n"); 15361ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15371ed3fed7SMarius Strobl m = NULL; 15381ed3fed7SMarius Strobl goto kickit; 153942c1b001SThomas Moestl } 154042c1b001SThomas Moestl 154142c1b001SThomas Moestl #ifdef GEM_DEBUG 15422a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 15431ed3fed7SMarius Strobl printf(" rxsoft %p descriptor %d: ", 15441ed3fed7SMarius Strobl &sc->sc_rxsoft[sc->sc_rxptr], sc->sc_rxptr); 15452a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 15462a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15472a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags)); 15482a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 15492a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15502a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_addr)); 155142c1b001SThomas Moestl } 155242c1b001SThomas Moestl #endif 155342c1b001SThomas Moestl 155442c1b001SThomas Moestl /* 155542c1b001SThomas Moestl * Allocate a new mbuf cluster. If that fails, we are 155642c1b001SThomas Moestl * out of memory, and must drop the packet and recycle 155742c1b001SThomas Moestl * the buffer that's already attached to this descriptor. 155842c1b001SThomas Moestl */ 15591ed3fed7SMarius Strobl if (gem_add_rxbuf(sc, sc->sc_rxptr) != 0) { 156042c1b001SThomas Moestl ifp->if_ierrors++; 15611ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15621ed3fed7SMarius Strobl m = NULL; 15631ed3fed7SMarius Strobl } 15641ed3fed7SMarius Strobl 15651ed3fed7SMarius Strobl kickit: 15661ed3fed7SMarius Strobl /* 15671ed3fed7SMarius Strobl * Update the RX kick register. This register has to point 15681ed3fed7SMarius Strobl * to the descriptor after the last valid one (before the 15699ba2b298SMarius Strobl * current batch) and for optimum performance should be 15709ba2b298SMarius Strobl * incremented in multiples of 4 (the DMA engine fetches/ 15719ba2b298SMarius Strobl * updates descriptors in batches of 4). 15721ed3fed7SMarius Strobl */ 15731ed3fed7SMarius Strobl sc->sc_rxptr = GEM_NEXTRX(sc->sc_rxptr); 15741ed3fed7SMarius Strobl if ((sc->sc_rxptr % 4) == 0) { 1575ccb1212aSMarius Strobl GEM_CDSYNC(sc, 1576ccb1212aSMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1577bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, 15781ed3fed7SMarius Strobl (sc->sc_rxptr + GEM_NRXDESC - 4) & 15791ed3fed7SMarius Strobl GEM_NRXDESC_MASK); 15801ed3fed7SMarius Strobl } 15811ed3fed7SMarius Strobl 15821ed3fed7SMarius Strobl if (m == NULL) { 15831ed3fed7SMarius Strobl if (rxstat & GEM_RD_OWN) 15841ed3fed7SMarius Strobl break; 158542c1b001SThomas Moestl continue; 158642c1b001SThomas Moestl } 158742c1b001SThomas Moestl 15881ed3fed7SMarius Strobl ifp->if_ipackets++; 15899ba2b298SMarius Strobl m->m_data += ETHER_ALIGN; /* first byte offset */ 159042c1b001SThomas Moestl m->m_pkthdr.rcvif = ifp; 15911ed3fed7SMarius Strobl m->m_pkthdr.len = m->m_len = GEM_RD_BUFLEN(rxstat); 159212fb0330SPyun YongHyeon 159312fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 159412fb0330SPyun YongHyeon gem_rxcksum(m, rxstat); 159542c1b001SThomas Moestl 159642c1b001SThomas Moestl /* Pass it on. */ 15978cfaff7dSMarius Strobl GEM_UNLOCK(sc); 1598673d9191SSam Leffler (*ifp->if_input)(ifp, m); 15998cfaff7dSMarius Strobl GEM_LOCK(sc); 160042c1b001SThomas Moestl } 160142c1b001SThomas Moestl 160218100346SThomas Moestl #ifdef GEM_DEBUG 16039ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: done sc->sc_rxptr %d, complete %d", __func__, 1604bd3d9826SMarius Strobl sc->sc_rxptr, GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION)); 160518100346SThomas Moestl #endif 160642c1b001SThomas Moestl } 160742c1b001SThomas Moestl 160842c1b001SThomas Moestl static int 16092a79fd39SMarius Strobl gem_add_rxbuf(struct gem_softc *sc, int idx) 161042c1b001SThomas Moestl { 161142c1b001SThomas Moestl struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 161242c1b001SThomas Moestl struct mbuf *m; 1613c3d5598aSMarius Strobl bus_dma_segment_t segs[1]; 1614c3d5598aSMarius Strobl int error, nsegs; 161542c1b001SThomas Moestl 16169ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 16179ba2b298SMarius Strobl 1618a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 161942c1b001SThomas Moestl if (m == NULL) 162042c1b001SThomas Moestl return (ENOBUFS); 1621305f2c06SThomas Moestl m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 162242c1b001SThomas Moestl 162342c1b001SThomas Moestl #ifdef GEM_DEBUG 16242a79fd39SMarius Strobl /* Bzero the packet to check DMA. */ 162542c1b001SThomas Moestl memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 162642c1b001SThomas Moestl #endif 162742c1b001SThomas Moestl 1628b2d59f42SThomas Moestl if (rxs->rxs_mbuf != NULL) { 1629b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 1630b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 1631305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 1632b2d59f42SThomas Moestl } 163342c1b001SThomas Moestl 1634c3d5598aSMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap, 1635c3d5598aSMarius Strobl m, segs, &nsegs, BUS_DMA_NOWAIT); 1636c3d5598aSMarius Strobl if (error != 0) { 16372a79fd39SMarius Strobl device_printf(sc->sc_dev, 16382a79fd39SMarius Strobl "cannot load RS DMA map %d, error = %d\n", idx, error); 1639c3d5598aSMarius Strobl m_freem(m); 16401ed3fed7SMarius Strobl return (error); 164142c1b001SThomas Moestl } 16422a79fd39SMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1643801772ecSMarius Strobl KASSERT(nsegs == 1, 1644801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 16451ed3fed7SMarius Strobl rxs->rxs_mbuf = m; 1646c3d5598aSMarius Strobl rxs->rxs_paddr = segs[0].ds_addr; 164742c1b001SThomas Moestl 16482a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 16492a79fd39SMarius Strobl BUS_DMASYNC_PREREAD); 165042c1b001SThomas Moestl 165142c1b001SThomas Moestl GEM_INIT_RXDESC(sc, idx); 165242c1b001SThomas Moestl 165342c1b001SThomas Moestl return (0); 165442c1b001SThomas Moestl } 165542c1b001SThomas Moestl 165642c1b001SThomas Moestl static void 16572a79fd39SMarius Strobl gem_eint(struct gem_softc *sc, u_int status) 165842c1b001SThomas Moestl { 165942c1b001SThomas Moestl 16601ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 16611ed3fed7SMarius Strobl if ((status & GEM_INTR_RX_TAG_ERR) != 0) { 16621ed3fed7SMarius Strobl gem_reset_rxdma(sc); 166342c1b001SThomas Moestl return; 166442c1b001SThomas Moestl } 166542c1b001SThomas Moestl 16669ba2b298SMarius Strobl device_printf(sc->sc_dev, "%s: status 0x%x", __func__, status); 16679ba2b298SMarius Strobl if ((status & GEM_INTR_BERR) != 0) { 16689ba2b298SMarius Strobl if ((sc->sc_flags & GEM_PCI) != 0) 16699ba2b298SMarius Strobl printf(", PCI bus error 0x%x\n", 16709ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_PCI_ERROR_STATUS)); 16719ba2b298SMarius Strobl else 16729ba2b298SMarius Strobl printf(", SBus error 0x%x\n", 16739ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_SBUS_STATUS)); 16749ba2b298SMarius Strobl } 167542c1b001SThomas Moestl } 167642c1b001SThomas Moestl 167742c1b001SThomas Moestl void 16782a79fd39SMarius Strobl gem_intr(void *v) 167942c1b001SThomas Moestl { 16802a79fd39SMarius Strobl struct gem_softc *sc = v; 16811ed3fed7SMarius Strobl uint32_t status, status2; 168242c1b001SThomas Moestl 16838cfaff7dSMarius Strobl GEM_LOCK(sc); 1684bd3d9826SMarius Strobl status = GEM_BANK1_READ_4(sc, GEM_STATUS); 16851ed3fed7SMarius Strobl 168618100346SThomas Moestl #ifdef GEM_DEBUG 168712fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: cplt %x, status %x", 16889ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, 16899ba2b298SMarius Strobl (status >> GEM_STATUS_TX_COMPLETION_SHFT), (u_int)status); 16901ed3fed7SMarius Strobl 16911ed3fed7SMarius Strobl /* 16921ed3fed7SMarius Strobl * PCS interrupts must be cleared, otherwise no traffic is passed! 16931ed3fed7SMarius Strobl */ 16941ed3fed7SMarius Strobl if ((status & GEM_INTR_PCS) != 0) { 16952a79fd39SMarius Strobl status2 = 1696bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS) | 1697bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS); 16981ed3fed7SMarius Strobl if ((status2 & GEM_MII_INTERRUP_LINK) != 0) 16991ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17001ed3fed7SMarius Strobl "%s: PCS link status changed\n", __func__); 17011ed3fed7SMarius Strobl } 17021ed3fed7SMarius Strobl if ((status & GEM_MAC_CONTROL_STATUS) != 0) { 1703bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_STATUS); 17041ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSED) != 0) 17051ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17061ed3fed7SMarius Strobl "%s: PAUSE received (PAUSE time %d slots)\n", 17071ed3fed7SMarius Strobl __func__, GEM_MAC_PAUSE_TIME(status2)); 17081ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSE) != 0) 17091ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17101ed3fed7SMarius Strobl "%s: transited to PAUSE state\n", __func__); 17111ed3fed7SMarius Strobl if ((status2 & GEM_MAC_RESUME) != 0) 17121ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17131ed3fed7SMarius Strobl "%s: transited to non-PAUSE state\n", __func__); 17141ed3fed7SMarius Strobl } 17151ed3fed7SMarius Strobl if ((status & GEM_INTR_MIF) != 0) 17161ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__); 171718100346SThomas Moestl #endif 171842c1b001SThomas Moestl 17199ba2b298SMarius Strobl if (__predict_false(status & 17201ed3fed7SMarius Strobl (GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | GEM_INTR_BERR)) != 0) 172142c1b001SThomas Moestl gem_eint(sc, status); 172242c1b001SThomas Moestl 172342c1b001SThomas Moestl if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 172442c1b001SThomas Moestl gem_rint(sc); 172542c1b001SThomas Moestl 17261ed3fed7SMarius Strobl if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 17271ed3fed7SMarius Strobl gem_tint(sc); 17281ed3fed7SMarius Strobl 17299ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_TX_MAC) != 0)) { 1730bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS); 17312a79fd39SMarius Strobl if ((status2 & 17329ba2b298SMarius Strobl ~(GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 17339ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP)) != 0) 17342a79fd39SMarius Strobl device_printf(sc->sc_dev, 17352a79fd39SMarius Strobl "MAC TX fault, status %x\n", status2); 17362a79fd39SMarius Strobl if ((status2 & 17379ba2b298SMarius Strobl (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) != 0) { 17389ba2b298SMarius Strobl sc->sc_ifp->if_oerrors++; 17398cfaff7dSMarius Strobl gem_init_locked(sc); 174042c1b001SThomas Moestl } 17419ba2b298SMarius Strobl } 17429ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_RX_MAC) != 0)) { 1743bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS); 174400d12766SMarius Strobl /* 17451ed3fed7SMarius Strobl * At least with GEM_SUN_GEM and some GEM_SUN_ERI 17461ed3fed7SMarius Strobl * revisions GEM_MAC_RX_OVERFLOW happen often due to a 17471ed3fed7SMarius Strobl * silicon bug so handle them silently. Moreover, it's 17481ed3fed7SMarius Strobl * likely that the receiver has hung so we reset it. 174900d12766SMarius Strobl */ 17502a79fd39SMarius Strobl if ((status2 & GEM_MAC_RX_OVERFLOW) != 0) { 17511ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 17521ed3fed7SMarius Strobl gem_reset_rxdma(sc); 17532a79fd39SMarius Strobl } else if ((status2 & 17542a79fd39SMarius Strobl ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) != 0) 17552a79fd39SMarius Strobl device_printf(sc->sc_dev, 17562a79fd39SMarius Strobl "MAC RX fault, status %x\n", status2); 175742c1b001SThomas Moestl } 17588cfaff7dSMarius Strobl GEM_UNLOCK(sc); 175942c1b001SThomas Moestl } 176042c1b001SThomas Moestl 17618cb37876SMarius Strobl static int 17622a79fd39SMarius Strobl gem_watchdog(struct gem_softc *sc) 176342c1b001SThomas Moestl { 1764ccb1212aSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 176542c1b001SThomas Moestl 17668cb37876SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 17678cb37876SMarius Strobl 176818100346SThomas Moestl #ifdef GEM_DEBUG 17692a79fd39SMarius Strobl CTR4(KTR_GEM, 17702a79fd39SMarius Strobl "%s: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x GEM_MAC_RX_CONFIG %x", 1771bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_RX_CONFIG), 1772bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS), 1773bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG)); 17742a79fd39SMarius Strobl CTR4(KTR_GEM, 17752a79fd39SMarius Strobl "%s: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x GEM_MAC_TX_CONFIG %x", 1776bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_CONFIG), 1777bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS), 1778bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG)); 177918100346SThomas Moestl #endif 178042c1b001SThomas Moestl 17818cb37876SMarius Strobl if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) 17828cb37876SMarius Strobl return (0); 17838cb37876SMarius Strobl 17841ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_LINK) != 0) 178542c1b001SThomas Moestl device_printf(sc->sc_dev, "device timeout\n"); 17861ed3fed7SMarius Strobl else if (bootverbose) 17871ed3fed7SMarius Strobl device_printf(sc->sc_dev, "device timeout (no link)\n"); 1788ccb1212aSMarius Strobl ++ifp->if_oerrors; 178942c1b001SThomas Moestl 179042c1b001SThomas Moestl /* Try to get more packets going. */ 17918cfaff7dSMarius Strobl gem_init_locked(sc); 1792ccb1212aSMarius Strobl gem_start_locked(ifp); 17938cb37876SMarius Strobl return (EJUSTRETURN); 179442c1b001SThomas Moestl } 179542c1b001SThomas Moestl 179642c1b001SThomas Moestl static void 17972a79fd39SMarius Strobl gem_mifinit(struct gem_softc *sc) 179842c1b001SThomas Moestl { 179942c1b001SThomas Moestl 1800801772ecSMarius Strobl /* Configure the MIF in frame mode. */ 1801bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, 1802bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & ~GEM_MIF_CONFIG_BB_ENA); 180342c1b001SThomas Moestl } 180442c1b001SThomas Moestl 180542c1b001SThomas Moestl /* 180642c1b001SThomas Moestl * MII interface 180742c1b001SThomas Moestl * 180878d22f42SMarius Strobl * The MII interface supports at least three different operating modes: 180942c1b001SThomas Moestl * 181042c1b001SThomas Moestl * Bitbang mode is implemented using data, clock and output enable registers. 181142c1b001SThomas Moestl * 181242c1b001SThomas Moestl * Frame mode is implemented by loading a complete frame into the frame 181342c1b001SThomas Moestl * register and polling the valid bit for completion. 181442c1b001SThomas Moestl * 181542c1b001SThomas Moestl * Polling mode uses the frame register but completion is indicated by 181642c1b001SThomas Moestl * an interrupt. 181742c1b001SThomas Moestl * 181842c1b001SThomas Moestl */ 181942c1b001SThomas Moestl int 18202a79fd39SMarius Strobl gem_mii_readreg(device_t dev, int phy, int reg) 182142c1b001SThomas Moestl { 18222a79fd39SMarius Strobl struct gem_softc *sc; 182342c1b001SThomas Moestl int n; 18242a79fd39SMarius Strobl uint32_t v; 182542c1b001SThomas Moestl 182642c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 18271ed3fed7SMarius Strobl printf("%s: phy %d reg %d\n", __func__, phy, reg); 182842c1b001SThomas Moestl #endif 182942c1b001SThomas Moestl 18302a79fd39SMarius Strobl sc = device_get_softc(dev); 18311ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 18321ed3fed7SMarius Strobl return (0); 18331ed3fed7SMarius Strobl 18341ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 18351ed3fed7SMarius Strobl switch (reg) { 18361ed3fed7SMarius Strobl case MII_BMCR: 18371ed3fed7SMarius Strobl reg = GEM_MII_CONTROL; 18381ed3fed7SMarius Strobl break; 18391ed3fed7SMarius Strobl case MII_BMSR: 18401ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 18411ed3fed7SMarius Strobl break; 18421ed3fed7SMarius Strobl case MII_PHYIDR1: 18431ed3fed7SMarius Strobl case MII_PHYIDR2: 18441ed3fed7SMarius Strobl return (0); 18451ed3fed7SMarius Strobl case MII_ANAR: 18461ed3fed7SMarius Strobl reg = GEM_MII_ANAR; 18471ed3fed7SMarius Strobl break; 18481ed3fed7SMarius Strobl case MII_ANLPAR: 18491ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 18501ed3fed7SMarius Strobl break; 18511ed3fed7SMarius Strobl case MII_EXTSR: 18521ed3fed7SMarius Strobl return (EXTSR_1000XFDX | EXTSR_1000XHDX); 18531ed3fed7SMarius Strobl default: 18541ed3fed7SMarius Strobl device_printf(sc->sc_dev, 18551ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 18561ed3fed7SMarius Strobl return (0); 18571ed3fed7SMarius Strobl } 1858bd3d9826SMarius Strobl return (GEM_BANK1_READ_4(sc, reg)); 18591ed3fed7SMarius Strobl } 186042c1b001SThomas Moestl 18612a79fd39SMarius Strobl /* Construct the frame command. */ 18621ed3fed7SMarius Strobl v = GEM_MIF_FRAME_READ | 18631ed3fed7SMarius Strobl (phy << GEM_MIF_PHY_SHIFT) | 18641ed3fed7SMarius Strobl (reg << GEM_MIF_REG_SHIFT); 186542c1b001SThomas Moestl 1866bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1867ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1868ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 186942c1b001SThomas Moestl for (n = 0; n < 100; n++) { 187042c1b001SThomas Moestl DELAY(1); 1871bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 18721f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 187342c1b001SThomas Moestl return (v & GEM_MIF_FRAME_DATA); 187442c1b001SThomas Moestl } 187542c1b001SThomas Moestl 18762a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 187742c1b001SThomas Moestl return (0); 187842c1b001SThomas Moestl } 187942c1b001SThomas Moestl 188042c1b001SThomas Moestl int 18812a79fd39SMarius Strobl gem_mii_writereg(device_t dev, int phy, int reg, int val) 188242c1b001SThomas Moestl { 18832a79fd39SMarius Strobl struct gem_softc *sc; 188442c1b001SThomas Moestl int n; 18852a79fd39SMarius Strobl uint32_t v; 188642c1b001SThomas Moestl 188742c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 18881ed3fed7SMarius Strobl printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__); 188942c1b001SThomas Moestl #endif 189042c1b001SThomas Moestl 18912a79fd39SMarius Strobl sc = device_get_softc(dev); 18921ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 18931ed3fed7SMarius Strobl return (0); 18941ed3fed7SMarius Strobl 18951ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 18961ed3fed7SMarius Strobl switch (reg) { 18971ed3fed7SMarius Strobl case MII_BMSR: 18981ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 18991ed3fed7SMarius Strobl break; 1900ccb1212aSMarius Strobl case MII_BMCR: 1901ccb1212aSMarius Strobl reg = GEM_MII_CONTROL; 1902ccb1212aSMarius Strobl if ((val & GEM_MII_CONTROL_RESET) == 0) 1903ccb1212aSMarius Strobl break; 1904ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONTROL, val); 1905ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONTROL, 4, 1906ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1907ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MII_CONTROL, 1908ccb1212aSMarius Strobl GEM_MII_CONTROL_RESET, 0)) 1909ccb1212aSMarius Strobl device_printf(sc->sc_dev, 1910ccb1212aSMarius Strobl "cannot reset PCS\n"); 1911ccb1212aSMarius Strobl /* FALLTHROUGH */ 19121ed3fed7SMarius Strobl case MII_ANAR: 1913bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 0); 1914bd3d9826SMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4, 19151ed3fed7SMarius Strobl BUS_SPACE_BARRIER_WRITE); 1916bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_ANAR, val); 1917bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 19181ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 1919bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 19201ed3fed7SMarius Strobl GEM_MII_CONFIG_ENABLE); 19211ed3fed7SMarius Strobl return (0); 19221ed3fed7SMarius Strobl case MII_ANLPAR: 19231ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 19241ed3fed7SMarius Strobl break; 19251ed3fed7SMarius Strobl default: 19261ed3fed7SMarius Strobl device_printf(sc->sc_dev, 19271ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 19281ed3fed7SMarius Strobl return (0); 19291ed3fed7SMarius Strobl } 1930bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, reg, val); 19311ed3fed7SMarius Strobl return (0); 19321ed3fed7SMarius Strobl } 19331ed3fed7SMarius Strobl 19342a79fd39SMarius Strobl /* Construct the frame command. */ 193542c1b001SThomas Moestl v = GEM_MIF_FRAME_WRITE | 193642c1b001SThomas Moestl (phy << GEM_MIF_PHY_SHIFT) | 193742c1b001SThomas Moestl (reg << GEM_MIF_REG_SHIFT) | 193842c1b001SThomas Moestl (val & GEM_MIF_FRAME_DATA); 193942c1b001SThomas Moestl 1940bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1941ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1942ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 194342c1b001SThomas Moestl for (n = 0; n < 100; n++) { 194442c1b001SThomas Moestl DELAY(1); 1945bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 19461f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 194742c1b001SThomas Moestl return (1); 194842c1b001SThomas Moestl } 194942c1b001SThomas Moestl 19502a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 195142c1b001SThomas Moestl return (0); 195242c1b001SThomas Moestl } 195342c1b001SThomas Moestl 195442c1b001SThomas Moestl void 19552a79fd39SMarius Strobl gem_mii_statchg(device_t dev) 195642c1b001SThomas Moestl { 19572a79fd39SMarius Strobl struct gem_softc *sc; 19581ed3fed7SMarius Strobl int gigabit; 19591ed3fed7SMarius Strobl uint32_t rxcfg, txcfg, v; 196042c1b001SThomas Moestl 19612a79fd39SMarius Strobl sc = device_get_softc(dev); 19622a79fd39SMarius Strobl 19639ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 19649ba2b298SMarius Strobl 196542c1b001SThomas Moestl #ifdef GEM_DEBUG 19662a79fd39SMarius Strobl if ((sc->sc_ifp->if_flags & IFF_DEBUG) != 0) 19671ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: status change: PHY = %d\n", 19681ed3fed7SMarius Strobl __func__, sc->sc_phyad); 196942c1b001SThomas Moestl #endif 197042c1b001SThomas Moestl 19711ed3fed7SMarius Strobl if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 && 19721ed3fed7SMarius Strobl IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE) 19731ed3fed7SMarius Strobl sc->sc_flags |= GEM_LINK; 19741ed3fed7SMarius Strobl else 19751ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 19761ed3fed7SMarius Strobl 19771ed3fed7SMarius Strobl switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) { 19781ed3fed7SMarius Strobl case IFM_1000_SX: 19791ed3fed7SMarius Strobl case IFM_1000_LX: 19801ed3fed7SMarius Strobl case IFM_1000_CX: 19811ed3fed7SMarius Strobl case IFM_1000_T: 19821ed3fed7SMarius Strobl gigabit = 1; 19831ed3fed7SMarius Strobl break; 19841ed3fed7SMarius Strobl default: 19851ed3fed7SMarius Strobl gigabit = 0; 198642c1b001SThomas Moestl } 19871ed3fed7SMarius Strobl 19881ed3fed7SMarius Strobl /* 19891ed3fed7SMarius Strobl * The configuration done here corresponds to the steps F) and 19901ed3fed7SMarius Strobl * G) and as far as enabling of RX and TX MAC goes also step H) 19911ed3fed7SMarius Strobl * of the initialization sequence outlined in section 3.2.1 of 19921ed3fed7SMarius Strobl * the GEM Gigabit Ethernet ASIC Specification. 19931ed3fed7SMarius Strobl */ 19941ed3fed7SMarius Strobl 1995bd3d9826SMarius Strobl rxcfg = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 19961ed3fed7SMarius Strobl rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE); 19971ed3fed7SMarius Strobl txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT; 19981ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 19991ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS; 20001ed3fed7SMarius Strobl else if (gigabit != 0) { 20011ed3fed7SMarius Strobl rxcfg |= GEM_MAC_RX_CARR_EXTEND; 20021ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_CARR_EXTEND; 20031ed3fed7SMarius Strobl } 2004bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 2005ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 2006ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2007bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 20081ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX MAC\n"); 2009bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, txcfg); 2010bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 2011ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2012ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2013bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 20141ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX MAC\n"); 2015bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, rxcfg); 20161ed3fed7SMarius Strobl 2017bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_CONFIG) & 20181ed3fed7SMarius Strobl ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE); 20191ed3fed7SMarius Strobl #ifdef notyet 20202a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20212a79fd39SMarius Strobl IFM_ETH_RXPAUSE) != 0) 20221ed3fed7SMarius Strobl v |= GEM_MAC_CC_RX_PAUSE; 20232a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20242a79fd39SMarius Strobl IFM_ETH_TXPAUSE) != 0) 20251ed3fed7SMarius Strobl v |= GEM_MAC_CC_TX_PAUSE; 20261ed3fed7SMarius Strobl #endif 2027bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_CONFIG, v); 20281ed3fed7SMarius Strobl 20291ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 && 20301ed3fed7SMarius Strobl gigabit != 0) 2031bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20321ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_CARR_EXTEND); 20331ed3fed7SMarius Strobl else 2034bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20351ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_NORMAL); 203642c1b001SThomas Moestl 203742c1b001SThomas Moestl /* XIF Configuration */ 203842c1b001SThomas Moestl v = GEM_MAC_XIF_LINK_LED; 203942c1b001SThomas Moestl v |= GEM_MAC_XIF_TX_MII_ENA; 20401ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) == 0) { 2041bd3d9826SMarius Strobl if ((GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & 204278d22f42SMarius Strobl GEM_MIF_CONFIG_PHY_SEL) != 0) { 204342c1b001SThomas Moestl /* External MII needs echo disable if half duplex. */ 204478d22f42SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 204578d22f42SMarius Strobl IFM_FDX) == 0) 204642c1b001SThomas Moestl v |= GEM_MAC_XIF_ECHO_DISABL; 204778d22f42SMarius Strobl } else 20481ed3fed7SMarius Strobl /* 20491ed3fed7SMarius Strobl * Internal MII needs buffer enable. 20501ed3fed7SMarius Strobl * XXX buffer enable makes only sense for an 20511ed3fed7SMarius Strobl * external PHY. 20521ed3fed7SMarius Strobl */ 205342c1b001SThomas Moestl v |= GEM_MAC_XIF_MII_BUF_ENA; 205442c1b001SThomas Moestl } 20551ed3fed7SMarius Strobl if (gigabit != 0) 20561ed3fed7SMarius Strobl v |= GEM_MAC_XIF_GMII_MODE; 20571ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 20581ed3fed7SMarius Strobl v |= GEM_MAC_XIF_FDPLX_LED; 2059bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, v); 20601ed3fed7SMarius Strobl 20611ed3fed7SMarius Strobl if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 20621ed3fed7SMarius Strobl (sc->sc_flags & GEM_LINK) != 0) { 2063bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 20641ed3fed7SMarius Strobl txcfg | GEM_MAC_TX_ENABLE); 2065bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 20661ed3fed7SMarius Strobl rxcfg | GEM_MAC_RX_ENABLE); 20671ed3fed7SMarius Strobl } 206842c1b001SThomas Moestl } 206942c1b001SThomas Moestl 207042c1b001SThomas Moestl int 20712a79fd39SMarius Strobl gem_mediachange(struct ifnet *ifp) 207242c1b001SThomas Moestl { 207342c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 20741f317bf9SMarius Strobl int error; 207542c1b001SThomas Moestl 20762a79fd39SMarius Strobl /* XXX add support for serial media. */ 207742c1b001SThomas Moestl 20781f317bf9SMarius Strobl GEM_LOCK(sc); 20791f317bf9SMarius Strobl error = mii_mediachg(sc->sc_mii); 20801f317bf9SMarius Strobl GEM_UNLOCK(sc); 20811f317bf9SMarius Strobl return (error); 208242c1b001SThomas Moestl } 208342c1b001SThomas Moestl 208442c1b001SThomas Moestl void 20852a79fd39SMarius Strobl gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 208642c1b001SThomas Moestl { 208742c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 208842c1b001SThomas Moestl 20898cfaff7dSMarius Strobl GEM_LOCK(sc); 20908cfaff7dSMarius Strobl if ((ifp->if_flags & IFF_UP) == 0) { 20918cfaff7dSMarius Strobl GEM_UNLOCK(sc); 209242c1b001SThomas Moestl return; 20938cfaff7dSMarius Strobl } 209442c1b001SThomas Moestl 209542c1b001SThomas Moestl mii_pollstat(sc->sc_mii); 209642c1b001SThomas Moestl ifmr->ifm_active = sc->sc_mii->mii_media_active; 209742c1b001SThomas Moestl ifmr->ifm_status = sc->sc_mii->mii_media_status; 20988cfaff7dSMarius Strobl GEM_UNLOCK(sc); 209942c1b001SThomas Moestl } 210042c1b001SThomas Moestl 210142c1b001SThomas Moestl static int 21022a79fd39SMarius Strobl gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 210342c1b001SThomas Moestl { 210442c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 210542c1b001SThomas Moestl struct ifreq *ifr = (struct ifreq *)data; 21062a79fd39SMarius Strobl int error; 21078cfaff7dSMarius Strobl 21082a79fd39SMarius Strobl error = 0; 210942c1b001SThomas Moestl switch (cmd) { 211042c1b001SThomas Moestl case SIOCSIFFLAGS: 21111f317bf9SMarius Strobl GEM_LOCK(sc); 21122a79fd39SMarius Strobl if ((ifp->if_flags & IFF_UP) != 0) { 21131ed3fed7SMarius Strobl if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 21141ed3fed7SMarius Strobl ((ifp->if_flags ^ sc->sc_ifflags) & 21151ed3fed7SMarius Strobl (IFF_ALLMULTI | IFF_PROMISC)) != 0) 211642c1b001SThomas Moestl gem_setladrf(sc); 211742c1b001SThomas Moestl else 21188cfaff7dSMarius Strobl gem_init_locked(sc); 21192a79fd39SMarius Strobl } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 212042c1b001SThomas Moestl gem_stop(ifp, 0); 212112fb0330SPyun YongHyeon if ((ifp->if_flags & IFF_LINK0) != 0) 212212fb0330SPyun YongHyeon sc->sc_csum_features |= CSUM_UDP; 212312fb0330SPyun YongHyeon else 212412fb0330SPyun YongHyeon sc->sc_csum_features &= ~CSUM_UDP; 212512fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 212612fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 2127336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 21281f317bf9SMarius Strobl GEM_UNLOCK(sc); 212942c1b001SThomas Moestl break; 213042c1b001SThomas Moestl case SIOCADDMULTI: 213142c1b001SThomas Moestl case SIOCDELMULTI: 21321f317bf9SMarius Strobl GEM_LOCK(sc); 213342c1b001SThomas Moestl gem_setladrf(sc); 21341f317bf9SMarius Strobl GEM_UNLOCK(sc); 213542c1b001SThomas Moestl break; 213642c1b001SThomas Moestl case SIOCGIFMEDIA: 213742c1b001SThomas Moestl case SIOCSIFMEDIA: 213842c1b001SThomas Moestl error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 213942c1b001SThomas Moestl break; 214012fb0330SPyun YongHyeon case SIOCSIFCAP: 214112fb0330SPyun YongHyeon GEM_LOCK(sc); 214212fb0330SPyun YongHyeon ifp->if_capenable = ifr->ifr_reqcap; 214312fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 214412fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 214512fb0330SPyun YongHyeon else 214612fb0330SPyun YongHyeon ifp->if_hwassist = 0; 214712fb0330SPyun YongHyeon GEM_UNLOCK(sc); 214812fb0330SPyun YongHyeon break; 214942c1b001SThomas Moestl default: 21501f317bf9SMarius Strobl error = ether_ioctl(ifp, cmd, data); 215142c1b001SThomas Moestl break; 215242c1b001SThomas Moestl } 215342c1b001SThomas Moestl 215442c1b001SThomas Moestl return (error); 215542c1b001SThomas Moestl } 215642c1b001SThomas Moestl 215742c1b001SThomas Moestl static void 21582a79fd39SMarius Strobl gem_setladrf(struct gem_softc *sc) 215942c1b001SThomas Moestl { 2160fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 216142c1b001SThomas Moestl struct ifmultiaddr *inm; 2162336cca9eSBenno Rice int i; 21632a79fd39SMarius Strobl uint32_t hash[16]; 21642a79fd39SMarius Strobl uint32_t crc, v; 216542c1b001SThomas Moestl 21668cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 21678cfaff7dSMarius Strobl 21682a79fd39SMarius Strobl /* Get the current RX configuration. */ 2169bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 217042c1b001SThomas Moestl 2171336cca9eSBenno Rice /* 2172336cca9eSBenno Rice * Turn off promiscuous mode, promiscuous group mode (all multicast), 2173336cca9eSBenno Rice * and hash filter. Depending on the case, the right bit will be 2174336cca9eSBenno Rice * enabled. 2175336cca9eSBenno Rice */ 2176336cca9eSBenno Rice v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_HASH_FILTER | 2177336cca9eSBenno Rice GEM_MAC_RX_PROMISC_GRP); 2178336cca9eSBenno Rice 2179bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 2180ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2181ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2182bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_HASH_FILTER, 2183bd3d9826SMarius Strobl 0)) 21841ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX hash filter\n"); 21851ed3fed7SMarius Strobl 218642c1b001SThomas Moestl if ((ifp->if_flags & IFF_PROMISC) != 0) { 218742c1b001SThomas Moestl v |= GEM_MAC_RX_PROMISCUOUS; 218842c1b001SThomas Moestl goto chipit; 218942c1b001SThomas Moestl } 219042c1b001SThomas Moestl if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 2191336cca9eSBenno Rice v |= GEM_MAC_RX_PROMISC_GRP; 219242c1b001SThomas Moestl goto chipit; 219342c1b001SThomas Moestl } 219442c1b001SThomas Moestl 219542c1b001SThomas Moestl /* 21962a79fd39SMarius Strobl * Set up multicast address filter by passing all multicast 21972a79fd39SMarius Strobl * addresses through a crc generator, and then using the high 21982a79fd39SMarius Strobl * order 8 bits as an index into the 256 bit logical address 21992a79fd39SMarius Strobl * filter. The high order 4 bits selects the word, while the 22002a79fd39SMarius Strobl * other 4 bits select the bit within the word (where bit 0 22012a79fd39SMarius Strobl * is the MSB). 220242c1b001SThomas Moestl */ 220342c1b001SThomas Moestl 22042a79fd39SMarius Strobl /* Clear the hash table. */ 2205336cca9eSBenno Rice memset(hash, 0, sizeof(hash)); 2206336cca9eSBenno Rice 2207eb956cd0SRobert Watson if_maddr_rlock(ifp); 2208fc74a9f9SBrooks Davis TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) { 220942c1b001SThomas Moestl if (inm->ifma_addr->sa_family != AF_LINK) 221042c1b001SThomas Moestl continue; 2211c240bd8cSMarius Strobl crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 2212c240bd8cSMarius Strobl inm->ifma_addr), ETHER_ADDR_LEN); 221342c1b001SThomas Moestl 22142a79fd39SMarius Strobl /* We just want the 8 most significant bits. */ 221542c1b001SThomas Moestl crc >>= 24; 221642c1b001SThomas Moestl 221742c1b001SThomas Moestl /* Set the corresponding bit in the filter. */ 2218336cca9eSBenno Rice hash[crc >> 4] |= 1 << (15 - (crc & 15)); 2219336cca9eSBenno Rice } 2220eb956cd0SRobert Watson if_maddr_runlock(ifp); 2221336cca9eSBenno Rice 2222336cca9eSBenno Rice v |= GEM_MAC_RX_HASH_FILTER; 2223336cca9eSBenno Rice 22242a79fd39SMarius Strobl /* Now load the hash table into the chip (if we are using it). */ 22252a79fd39SMarius Strobl for (i = 0; i < 16; i++) 2226bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, 2227336cca9eSBenno Rice GEM_MAC_HASH0 + i * (GEM_MAC_HASH1 - GEM_MAC_HASH0), 2228336cca9eSBenno Rice hash[i]); 222942c1b001SThomas Moestl 223042c1b001SThomas Moestl chipit: 2231bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 223242c1b001SThomas Moestl } 2233