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. 3001ed3fed7SMarius Strobl */ 3011ed3fed7SMarius Strobl if (error != 0 && (v & GEM_MIF_CONFIG_MDI0) != 0) { 3021ed3fed7SMarius Strobl v &= ~GEM_MIF_CONFIG_PHY_SEL; 303bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v); 3041ed3fed7SMarius Strobl switch (sc->sc_variant) { 3051ed3fed7SMarius Strobl case GEM_SUN_ERI: 3061ed3fed7SMarius Strobl case GEM_APPLE_K2_GMAC: 3071ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_INTERNAL; 3081ed3fed7SMarius Strobl break; 3091ed3fed7SMarius Strobl case GEM_APPLE_GMAC: 3101ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3111ed3fed7SMarius Strobl break; 3121ed3fed7SMarius Strobl default: 3131ed3fed7SMarius Strobl sc->sc_phyad = -1; 3141ed3fed7SMarius Strobl break; 3151ed3fed7SMarius Strobl } 3161ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3171ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3181ed3fed7SMarius Strobl } 3191ed3fed7SMarius Strobl 3201ed3fed7SMarius Strobl /* 3211ed3fed7SMarius Strobl * Try the external PCS SERDES if we didn't find any PHYs. 3221ed3fed7SMarius Strobl */ 3231ed3fed7SMarius Strobl if (error != 0 && sc->sc_variant == GEM_SUN_GEM) { 324bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE, 3251ed3fed7SMarius Strobl GEM_MII_DATAPATH_SERDES); 326bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 3271ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 328bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE); 3291ed3fed7SMarius Strobl sc->sc_flags |= GEM_SERDES; 3301ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3311ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3321ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3331ed3fed7SMarius Strobl } 3341ed3fed7SMarius Strobl 3351ed3fed7SMarius Strobl if (error != 0) { 3361ed3fed7SMarius Strobl device_printf(sc->sc_dev, "PHY probe failed: %d\n", error); 337305f2c06SThomas Moestl goto fail_rxd; 33842c1b001SThomas Moestl } 33942c1b001SThomas Moestl sc->sc_mii = device_get_softc(sc->sc_miibus); 34042c1b001SThomas Moestl 34142c1b001SThomas Moestl /* 34242c1b001SThomas Moestl * From this point forward, the attachment cannot fail. A failure 34342c1b001SThomas Moestl * before this point releases all resources that may have been 34442c1b001SThomas Moestl * allocated. 34542c1b001SThomas Moestl */ 34642c1b001SThomas Moestl 347801772ecSMarius Strobl /* Get RX FIFO size. */ 348336cca9eSBenno Rice sc->sc_rxfifosize = 64 * 349bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_FIFO_SIZE); 350336cca9eSBenno Rice 351801772ecSMarius Strobl /* Get TX FIFO size. */ 352bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_TX_FIFO_SIZE); 3533a5aee5aSThomas Moestl device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n", 3543a5aee5aSThomas Moestl sc->sc_rxfifosize / 1024, v / 16); 35542c1b001SThomas Moestl 35642c1b001SThomas Moestl /* Attach the interface. */ 357fc74a9f9SBrooks Davis ether_ifattach(ifp, sc->sc_enaddr); 35842c1b001SThomas Moestl 35900d12766SMarius Strobl /* 36012fb0330SPyun YongHyeon * Tell the upper layer(s) we support long frames/checksum offloads. 36100d12766SMarius Strobl */ 36200d12766SMarius Strobl ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 36312fb0330SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 36412fb0330SPyun YongHyeon ifp->if_hwassist |= sc->sc_csum_features; 36512fb0330SPyun YongHyeon ifp->if_capenable |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 36600d12766SMarius Strobl 36742c1b001SThomas Moestl return (0); 36842c1b001SThomas Moestl 36942c1b001SThomas Moestl /* 37042c1b001SThomas Moestl * Free any resources we've allocated during the failed attach 37142c1b001SThomas Moestl * attempt. Do this in reverse order and fall through. 37242c1b001SThomas Moestl */ 373305f2c06SThomas Moestl fail_rxd: 3742a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 37542c1b001SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 376305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 37742c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 378305f2c06SThomas Moestl fail_txd: 3792a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 38042c1b001SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 381305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 38242c1b001SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 383305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 384305f2c06SThomas Moestl fail_cmem: 38542c1b001SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 38642c1b001SThomas Moestl sc->sc_cddmamap); 387305f2c06SThomas Moestl fail_ctag: 38842c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 389305f2c06SThomas Moestl fail_ttag: 390305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 391305f2c06SThomas Moestl fail_rtag: 392305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 393305f2c06SThomas Moestl fail_ptag: 39442c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 395fc74a9f9SBrooks Davis fail_ifnet: 396fc74a9f9SBrooks Davis if_free(ifp); 39742c1b001SThomas Moestl return (error); 39842c1b001SThomas Moestl } 39942c1b001SThomas Moestl 400cbbdf236SThomas Moestl void 4012a79fd39SMarius Strobl gem_detach(struct gem_softc *sc) 402cbbdf236SThomas Moestl { 403fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 404cbbdf236SThomas Moestl int i; 405cbbdf236SThomas Moestl 406b3a1f860SMarius Strobl ether_ifdetach(ifp); 4078cfaff7dSMarius Strobl GEM_LOCK(sc); 40825bd46d0SBrooks Davis gem_stop(ifp, 1); 4098cfaff7dSMarius Strobl GEM_UNLOCK(sc); 4101f317bf9SMarius Strobl callout_drain(&sc->sc_tick_ch); 4111f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 4121f317bf9SMarius Strobl callout_drain(&sc->sc_rx_ch); 4131f317bf9SMarius Strobl #endif 414fc74a9f9SBrooks Davis if_free(ifp); 415cbbdf236SThomas Moestl device_delete_child(sc->sc_dev, sc->sc_miibus); 416cbbdf236SThomas Moestl 4172a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 418cbbdf236SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 419cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 420cbbdf236SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 4212a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 422cbbdf236SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 423cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 424cbbdf236SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 425ccb1212aSMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 426cbbdf236SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 427cbbdf236SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 428cbbdf236SThomas Moestl sc->sc_cddmamap); 429cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 430cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 431cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 432cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 433cbbdf236SThomas Moestl } 434cbbdf236SThomas Moestl 435cbbdf236SThomas Moestl void 4362a79fd39SMarius Strobl gem_suspend(struct gem_softc *sc) 437cbbdf236SThomas Moestl { 438fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 439cbbdf236SThomas Moestl 4408cfaff7dSMarius Strobl GEM_LOCK(sc); 441cbbdf236SThomas Moestl gem_stop(ifp, 0); 4428cfaff7dSMarius Strobl GEM_UNLOCK(sc); 443cbbdf236SThomas Moestl } 444cbbdf236SThomas Moestl 445cbbdf236SThomas Moestl void 4462a79fd39SMarius Strobl gem_resume(struct gem_softc *sc) 447cbbdf236SThomas Moestl { 448fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 449cbbdf236SThomas Moestl 4508cfaff7dSMarius Strobl GEM_LOCK(sc); 45100d12766SMarius Strobl /* 45200d12766SMarius Strobl * On resume all registers have to be initialized again like 45300d12766SMarius Strobl * after power-on. 45400d12766SMarius Strobl */ 4551ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_INITED; 456cbbdf236SThomas Moestl if (ifp->if_flags & IFF_UP) 4578cfaff7dSMarius Strobl gem_init_locked(sc); 4588cfaff7dSMarius Strobl GEM_UNLOCK(sc); 459cbbdf236SThomas Moestl } 460cbbdf236SThomas Moestl 4619ba2b298SMarius Strobl static inline void 46212fb0330SPyun YongHyeon gem_rxcksum(struct mbuf *m, uint64_t flags) 46312fb0330SPyun YongHyeon { 46412fb0330SPyun YongHyeon struct ether_header *eh; 46512fb0330SPyun YongHyeon struct ip *ip; 46612fb0330SPyun YongHyeon struct udphdr *uh; 4672a79fd39SMarius Strobl uint16_t *opts; 46812fb0330SPyun YongHyeon int32_t hlen, len, pktlen; 46912fb0330SPyun YongHyeon uint32_t temp32; 4702a79fd39SMarius Strobl uint16_t cksum; 47112fb0330SPyun YongHyeon 47212fb0330SPyun YongHyeon pktlen = m->m_pkthdr.len; 47312fb0330SPyun YongHyeon if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 47412fb0330SPyun YongHyeon return; 47512fb0330SPyun YongHyeon eh = mtod(m, struct ether_header *); 47612fb0330SPyun YongHyeon if (eh->ether_type != htons(ETHERTYPE_IP)) 47712fb0330SPyun YongHyeon return; 47812fb0330SPyun YongHyeon ip = (struct ip *)(eh + 1); 47912fb0330SPyun YongHyeon if (ip->ip_v != IPVERSION) 48012fb0330SPyun YongHyeon return; 48112fb0330SPyun YongHyeon 48212fb0330SPyun YongHyeon hlen = ip->ip_hl << 2; 48312fb0330SPyun YongHyeon pktlen -= sizeof(struct ether_header); 48412fb0330SPyun YongHyeon if (hlen < sizeof(struct ip)) 48512fb0330SPyun YongHyeon return; 48612fb0330SPyun YongHyeon if (ntohs(ip->ip_len) < hlen) 48712fb0330SPyun YongHyeon return; 48812fb0330SPyun YongHyeon if (ntohs(ip->ip_len) != pktlen) 48912fb0330SPyun YongHyeon return; 49012fb0330SPyun YongHyeon if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 4912a79fd39SMarius Strobl return; /* Cannot handle fragmented packet. */ 49212fb0330SPyun YongHyeon 49312fb0330SPyun YongHyeon switch (ip->ip_p) { 49412fb0330SPyun YongHyeon case IPPROTO_TCP: 49512fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct tcphdr))) 49612fb0330SPyun YongHyeon return; 49712fb0330SPyun YongHyeon break; 49812fb0330SPyun YongHyeon case IPPROTO_UDP: 49912fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct udphdr))) 50012fb0330SPyun YongHyeon return; 50112fb0330SPyun YongHyeon uh = (struct udphdr *)((uint8_t *)ip + hlen); 50212fb0330SPyun YongHyeon if (uh->uh_sum == 0) 50312fb0330SPyun YongHyeon return; /* no checksum */ 50412fb0330SPyun YongHyeon break; 50512fb0330SPyun YongHyeon default: 50612fb0330SPyun YongHyeon return; 50712fb0330SPyun YongHyeon } 50812fb0330SPyun YongHyeon 50912fb0330SPyun YongHyeon cksum = ~(flags & GEM_RD_CHECKSUM); 51012fb0330SPyun YongHyeon /* checksum fixup for IP options */ 51112fb0330SPyun YongHyeon len = hlen - sizeof(struct ip); 51212fb0330SPyun YongHyeon if (len > 0) { 51312fb0330SPyun YongHyeon opts = (uint16_t *)(ip + 1); 51412fb0330SPyun YongHyeon for (; len > 0; len -= sizeof(uint16_t), opts++) { 51512fb0330SPyun YongHyeon temp32 = cksum - *opts; 51612fb0330SPyun YongHyeon temp32 = (temp32 >> 16) + (temp32 & 65535); 51712fb0330SPyun YongHyeon cksum = temp32 & 65535; 51812fb0330SPyun YongHyeon } 51912fb0330SPyun YongHyeon } 52012fb0330SPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 52112fb0330SPyun YongHyeon m->m_pkthdr.csum_data = cksum; 52212fb0330SPyun YongHyeon } 52312fb0330SPyun YongHyeon 52442c1b001SThomas Moestl static void 5252a79fd39SMarius Strobl gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 52642c1b001SThomas Moestl { 5272a79fd39SMarius Strobl struct gem_softc *sc = xsc; 52842c1b001SThomas Moestl 52942c1b001SThomas Moestl if (error != 0) 53042c1b001SThomas Moestl return; 5312a79fd39SMarius Strobl if (nsegs != 1) 5321ed3fed7SMarius Strobl panic("%s: bad control buffer segment count", __func__); 53342c1b001SThomas Moestl sc->sc_cddma = segs[0].ds_addr; 53442c1b001SThomas Moestl } 53542c1b001SThomas Moestl 53642c1b001SThomas Moestl static void 5372a79fd39SMarius Strobl gem_tick(void *arg) 53842c1b001SThomas Moestl { 53942c1b001SThomas Moestl struct gem_softc *sc = arg; 5409ba2b298SMarius Strobl struct ifnet *ifp = sc->sc_ifp; 54178d22f42SMarius Strobl uint32_t v; 54242c1b001SThomas Moestl 5431f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 54412fb0330SPyun YongHyeon 54512fb0330SPyun YongHyeon /* 54678d22f42SMarius Strobl * Unload collision and error counters. 54712fb0330SPyun YongHyeon */ 54812fb0330SPyun YongHyeon ifp->if_collisions += 549bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_NORM_COLL_CNT) + 55078d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_FIRST_COLL_CNT); 55178d22f42SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_EXCESS_COLL_CNT) + 552bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_LATE_COLL_CNT); 55378d22f42SMarius Strobl ifp->if_collisions += v; 55478d22f42SMarius Strobl ifp->if_oerrors += v; 55578d22f42SMarius Strobl ifp->if_ierrors += 55678d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_LEN_ERR_CNT) + 55778d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_ALIGN_ERR) + 55878d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CRC_ERR_CNT) + 55978d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CODE_VIOL); 56012fb0330SPyun YongHyeon 56112fb0330SPyun YongHyeon /* 562801772ecSMarius Strobl * Then clear the hardware counters. 56312fb0330SPyun YongHyeon */ 564bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 565bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 566bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 567bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 56878d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 56978d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 57078d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 57178d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 57212fb0330SPyun YongHyeon 57342c1b001SThomas Moestl mii_tick(sc->sc_mii); 57442c1b001SThomas Moestl 5758cb37876SMarius Strobl if (gem_watchdog(sc) == EJUSTRETURN) 5768cb37876SMarius Strobl return; 5778cb37876SMarius Strobl 57842c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 57942c1b001SThomas Moestl } 58042c1b001SThomas Moestl 58142c1b001SThomas Moestl static int 582bd3d9826SMarius Strobl gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, uint32_t clr, 583bd3d9826SMarius Strobl uint32_t set) 58442c1b001SThomas Moestl { 58542c1b001SThomas Moestl int i; 5862a79fd39SMarius Strobl uint32_t reg; 58742c1b001SThomas Moestl 5889ba2b298SMarius Strobl for (i = GEM_TRIES; i--; DELAY(100)) { 589bd3d9826SMarius Strobl reg = GEM_BANKN_READ_M(bank, 4, sc, r); 590e87137e1SMarius Strobl if ((reg & clr) == 0 && (reg & set) == set) 59142c1b001SThomas Moestl return (1); 59242c1b001SThomas Moestl } 59342c1b001SThomas Moestl return (0); 59442c1b001SThomas Moestl } 59542c1b001SThomas Moestl 5961ed3fed7SMarius Strobl static void 5979ba2b298SMarius Strobl gem_reset(struct gem_softc *sc) 59842c1b001SThomas Moestl { 59942c1b001SThomas Moestl 60018100346SThomas Moestl #ifdef GEM_DEBUG 60112fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 60218100346SThomas Moestl #endif 60342c1b001SThomas Moestl gem_reset_rx(sc); 60442c1b001SThomas Moestl gem_reset_tx(sc); 60542c1b001SThomas Moestl 6062a79fd39SMarius Strobl /* Do a full reset. */ 607bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 608ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 609ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 610bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 61142c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset device\n"); 61242c1b001SThomas Moestl } 61342c1b001SThomas Moestl 61442c1b001SThomas Moestl static void 6152a79fd39SMarius Strobl gem_rxdrain(struct gem_softc *sc) 61642c1b001SThomas Moestl { 61742c1b001SThomas Moestl struct gem_rxsoft *rxs; 61842c1b001SThomas Moestl int i; 61942c1b001SThomas Moestl 62042c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 62142c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 62242c1b001SThomas Moestl if (rxs->rxs_mbuf != NULL) { 623b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 624b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 625305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 62642c1b001SThomas Moestl m_freem(rxs->rxs_mbuf); 62742c1b001SThomas Moestl rxs->rxs_mbuf = NULL; 62842c1b001SThomas Moestl } 62942c1b001SThomas Moestl } 63042c1b001SThomas Moestl } 63142c1b001SThomas Moestl 63242c1b001SThomas Moestl static void 6332a79fd39SMarius Strobl gem_stop(struct ifnet *ifp, int disable) 63442c1b001SThomas Moestl { 6352a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 63642c1b001SThomas Moestl struct gem_txsoft *txs; 63742c1b001SThomas Moestl 63818100346SThomas Moestl #ifdef GEM_DEBUG 63912fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 64018100346SThomas Moestl #endif 64142c1b001SThomas Moestl 64242c1b001SThomas Moestl callout_stop(&sc->sc_tick_ch); 6431f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 6441f317bf9SMarius Strobl callout_stop(&sc->sc_rx_ch); 6451f317bf9SMarius Strobl #endif 64642c1b001SThomas Moestl 6479ba2b298SMarius Strobl gem_reset_tx(sc); 6489ba2b298SMarius Strobl gem_reset_rx(sc); 64942c1b001SThomas Moestl 65042c1b001SThomas Moestl /* 65142c1b001SThomas Moestl * Release any queued transmit buffers. 65242c1b001SThomas Moestl */ 65342c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 65442c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 65542c1b001SThomas Moestl if (txs->txs_ndescs != 0) { 656b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 657b2d59f42SThomas Moestl BUS_DMASYNC_POSTWRITE); 658305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 65942c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 66042c1b001SThomas Moestl m_freem(txs->txs_mbuf); 66142c1b001SThomas Moestl txs->txs_mbuf = NULL; 66242c1b001SThomas Moestl } 66342c1b001SThomas Moestl } 66442c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 66542c1b001SThomas Moestl } 66642c1b001SThomas Moestl 66742c1b001SThomas Moestl if (disable) 66842c1b001SThomas Moestl gem_rxdrain(sc); 66942c1b001SThomas Moestl 67042c1b001SThomas Moestl /* 67142c1b001SThomas Moestl * Mark the interface down and cancel the watchdog timer. 67242c1b001SThomas Moestl */ 67313f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 6741ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 6758cb37876SMarius Strobl sc->sc_wdog_timer = 0; 67642c1b001SThomas Moestl } 67742c1b001SThomas Moestl 6781ed3fed7SMarius Strobl static int 6792a79fd39SMarius Strobl gem_reset_rx(struct gem_softc *sc) 68042c1b001SThomas Moestl { 68142c1b001SThomas Moestl 68242c1b001SThomas Moestl /* 68342c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 68442c1b001SThomas Moestl * disable DMA first. 68542c1b001SThomas Moestl */ 68642c1b001SThomas Moestl gem_disable_rx(sc); 687bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 0); 688ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_RX_CONFIG, 4, 689ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 690bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_RX_CONFIG, GEM_RX_CONFIG_RXDMA_EN, 0)) 6911ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX DMA\n"); 69242c1b001SThomas Moestl 693801772ecSMarius Strobl /* Finally, reset the ERX. */ 694bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX); 695ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 696ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 697bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 698bd3d9826SMarius Strobl 0)) { 69942c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 70042c1b001SThomas Moestl return (1); 70142c1b001SThomas Moestl } 70242c1b001SThomas Moestl return (0); 70342c1b001SThomas Moestl } 70442c1b001SThomas Moestl 7051ed3fed7SMarius Strobl /* 7061ed3fed7SMarius Strobl * Reset the receiver DMA engine. 7071ed3fed7SMarius Strobl * 7081ed3fed7SMarius Strobl * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW 7091ed3fed7SMarius Strobl * etc in order to reset the receiver DMA engine only and not do a full 7101ed3fed7SMarius Strobl * reset which amongst others also downs the link and clears the FIFOs. 7111ed3fed7SMarius Strobl */ 7121ed3fed7SMarius Strobl static void 7131ed3fed7SMarius Strobl gem_reset_rxdma(struct gem_softc *sc) 7141ed3fed7SMarius Strobl { 7151ed3fed7SMarius Strobl int i; 7161ed3fed7SMarius Strobl 7171ed3fed7SMarius Strobl if (gem_reset_rx(sc) != 0) 7181ed3fed7SMarius Strobl return (gem_init_locked(sc)); 7191ed3fed7SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 7201ed3fed7SMarius Strobl if (sc->sc_rxsoft[i].rxs_mbuf != NULL) 7211ed3fed7SMarius Strobl GEM_UPDATE_RXDESC(sc, i); 7221ed3fed7SMarius Strobl sc->sc_rxptr = 0; 7239ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 7241ed3fed7SMarius Strobl 7251ed3fed7SMarius Strobl /* NOTE: we use only 32-bit DMA addresses here. */ 726bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 727bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 728bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 729bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 7301ed3fed7SMarius Strobl gem_ringsize(GEM_NRXDESC /* XXX */) | 7311ed3fed7SMarius Strobl ((ETHER_HDR_LEN + sizeof(struct ip)) << 7321ed3fed7SMarius Strobl GEM_RX_CONFIG_CXM_START_SHFT) | 7331ed3fed7SMarius Strobl (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 7349ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT)); 7359ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 736bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 7379ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 7389ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 739bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 7402a79fd39SMarius Strobl (3 * sc->sc_rxfifosize / 256) | 7412a79fd39SMarius Strobl ((sc->sc_rxfifosize / 256) << 12)); 742bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 743bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_CONFIG) | GEM_RX_CONFIG_RXDMA_EN); 744bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 7451ed3fed7SMarius Strobl GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 746bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 747bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) | GEM_MAC_RX_ENABLE); 7481ed3fed7SMarius Strobl } 74942c1b001SThomas Moestl 75042c1b001SThomas Moestl static int 7512a79fd39SMarius Strobl gem_reset_tx(struct gem_softc *sc) 75242c1b001SThomas Moestl { 75342c1b001SThomas Moestl 75442c1b001SThomas Moestl /* 75542c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 75642c1b001SThomas Moestl * disable DMA first. 75742c1b001SThomas Moestl */ 75842c1b001SThomas Moestl gem_disable_tx(sc); 759bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, 0); 760ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_TX_CONFIG, 4, 761ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 762bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_TX_CONFIG, GEM_TX_CONFIG_TXDMA_EN, 0)) 7631ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX DMA\n"); 76442c1b001SThomas Moestl 765801772ecSMarius Strobl /* Finally, reset the ETX. */ 766bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_TX); 767ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 768ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 769bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 770bd3d9826SMarius Strobl 0)) { 7711ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot reset transmitter\n"); 77242c1b001SThomas Moestl return (1); 77342c1b001SThomas Moestl } 77442c1b001SThomas Moestl return (0); 77542c1b001SThomas Moestl } 77642c1b001SThomas Moestl 77742c1b001SThomas Moestl static int 7782a79fd39SMarius Strobl gem_disable_rx(struct gem_softc *sc) 77942c1b001SThomas Moestl { 78042c1b001SThomas Moestl 781bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 782bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) & ~GEM_MAC_RX_ENABLE); 783ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 784ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 785bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 786bd3d9826SMarius Strobl 0)); 78742c1b001SThomas Moestl } 78842c1b001SThomas Moestl 78942c1b001SThomas Moestl static int 7902a79fd39SMarius Strobl gem_disable_tx(struct gem_softc *sc) 79142c1b001SThomas Moestl { 79242c1b001SThomas Moestl 793bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 794bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG) & ~GEM_MAC_TX_ENABLE); 795ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 796ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 797bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 798bd3d9826SMarius Strobl 0)); 79942c1b001SThomas Moestl } 80042c1b001SThomas Moestl 80142c1b001SThomas Moestl static int 8029ba2b298SMarius Strobl gem_meminit(struct gem_softc *sc) 80342c1b001SThomas Moestl { 80442c1b001SThomas Moestl struct gem_rxsoft *rxs; 8052a79fd39SMarius Strobl int error, i; 80642c1b001SThomas Moestl 8079ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 8089ba2b298SMarius Strobl 80942c1b001SThomas Moestl /* 81042c1b001SThomas Moestl * Initialize the transmit descriptor ring. 81142c1b001SThomas Moestl */ 81242c1b001SThomas Moestl for (i = 0; i < GEM_NTXDESC; i++) { 81342c1b001SThomas Moestl sc->sc_txdescs[i].gd_flags = 0; 81442c1b001SThomas Moestl sc->sc_txdescs[i].gd_addr = 0; 81542c1b001SThomas Moestl } 816305f2c06SThomas Moestl sc->sc_txfree = GEM_MAXTXFREE; 81742c1b001SThomas Moestl sc->sc_txnext = 0; 818336cca9eSBenno Rice sc->sc_txwin = 0; 81942c1b001SThomas Moestl 82042c1b001SThomas Moestl /* 82142c1b001SThomas Moestl * Initialize the receive descriptor and receive job 82242c1b001SThomas Moestl * descriptor rings. 82342c1b001SThomas Moestl */ 82442c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 82542c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 82642c1b001SThomas Moestl if (rxs->rxs_mbuf == NULL) { 82742c1b001SThomas Moestl if ((error = gem_add_rxbuf(sc, i)) != 0) { 8282a79fd39SMarius Strobl device_printf(sc->sc_dev, 8292a79fd39SMarius Strobl "unable to allocate or map RX buffer %d, " 8302a79fd39SMarius Strobl "error = %d\n", i, error); 83142c1b001SThomas Moestl /* 8322a79fd39SMarius Strobl * XXX we should attempt to run with fewer 8332a79fd39SMarius Strobl * receive buffers instead of just failing. 83442c1b001SThomas Moestl */ 83542c1b001SThomas Moestl gem_rxdrain(sc); 83642c1b001SThomas Moestl return (1); 83742c1b001SThomas Moestl } 83842c1b001SThomas Moestl } else 83942c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 84042c1b001SThomas Moestl } 84142c1b001SThomas Moestl sc->sc_rxptr = 0; 8429ba2b298SMarius Strobl 8439ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 84442c1b001SThomas Moestl 84542c1b001SThomas Moestl return (0); 84642c1b001SThomas Moestl } 84742c1b001SThomas Moestl 8481ed3fed7SMarius Strobl static u_int 8492a79fd39SMarius Strobl gem_ringsize(u_int sz) 85042c1b001SThomas Moestl { 85142c1b001SThomas Moestl 85242c1b001SThomas Moestl switch (sz) { 85342c1b001SThomas Moestl case 32: 8541ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 85542c1b001SThomas Moestl case 64: 8561ed3fed7SMarius Strobl return (GEM_RING_SZ_64); 85742c1b001SThomas Moestl case 128: 8581ed3fed7SMarius Strobl return (GEM_RING_SZ_128); 85942c1b001SThomas Moestl case 256: 8601ed3fed7SMarius Strobl return (GEM_RING_SZ_256); 86142c1b001SThomas Moestl case 512: 8621ed3fed7SMarius Strobl return (GEM_RING_SZ_512); 86342c1b001SThomas Moestl case 1024: 8641ed3fed7SMarius Strobl return (GEM_RING_SZ_1024); 86542c1b001SThomas Moestl case 2048: 8661ed3fed7SMarius Strobl return (GEM_RING_SZ_2048); 86742c1b001SThomas Moestl case 4096: 8681ed3fed7SMarius Strobl return (GEM_RING_SZ_4096); 86942c1b001SThomas Moestl case 8192: 8701ed3fed7SMarius Strobl return (GEM_RING_SZ_8192); 87142c1b001SThomas Moestl default: 8721ed3fed7SMarius Strobl printf("%s: invalid ring size %d\n", __func__, sz); 8731ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 87442c1b001SThomas Moestl } 87542c1b001SThomas Moestl } 87642c1b001SThomas Moestl 87742c1b001SThomas Moestl static void 8782a79fd39SMarius Strobl gem_init(void *xsc) 87942c1b001SThomas Moestl { 8802a79fd39SMarius Strobl struct gem_softc *sc = xsc; 8818cfaff7dSMarius Strobl 8828cfaff7dSMarius Strobl GEM_LOCK(sc); 8838cfaff7dSMarius Strobl gem_init_locked(sc); 8848cfaff7dSMarius Strobl GEM_UNLOCK(sc); 8858cfaff7dSMarius Strobl } 8868cfaff7dSMarius Strobl 8878cfaff7dSMarius Strobl /* 8888cfaff7dSMarius Strobl * Initialization of interface; set up initialization block 8898cfaff7dSMarius Strobl * and transmit/receive descriptor rings. 8908cfaff7dSMarius Strobl */ 8918cfaff7dSMarius Strobl static void 8922a79fd39SMarius Strobl gem_init_locked(struct gem_softc *sc) 8938cfaff7dSMarius Strobl { 894fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 8952a79fd39SMarius Strobl uint32_t v; 89642c1b001SThomas Moestl 8978cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 89842c1b001SThomas Moestl 89918100346SThomas Moestl #ifdef GEM_DEBUG 90012fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: calling stop", device_get_name(sc->sc_dev), 90112fb0330SPyun YongHyeon __func__); 90218100346SThomas Moestl #endif 90342c1b001SThomas Moestl /* 90442c1b001SThomas Moestl * Initialization sequence. The numbered steps below correspond 90542c1b001SThomas Moestl * to the sequence outlined in section 6.3.5.1 in the Ethernet 90642c1b001SThomas Moestl * Channel Engine manual (part of the PCIO manual). 90742c1b001SThomas Moestl * See also the STP2002-STQ document from Sun Microsystems. 90842c1b001SThomas Moestl */ 90942c1b001SThomas Moestl 9102a79fd39SMarius Strobl /* step 1 & 2. Reset the Ethernet Channel. */ 911ccb1212aSMarius Strobl gem_stop(ifp, 0); 91242c1b001SThomas Moestl gem_reset(sc); 91318100346SThomas Moestl #ifdef GEM_DEBUG 91412fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: restarting", device_get_name(sc->sc_dev), 91512fb0330SPyun YongHyeon __func__); 91618100346SThomas Moestl #endif 91742c1b001SThomas Moestl 9182a79fd39SMarius Strobl /* Re-initialize the MIF. */ 91942c1b001SThomas Moestl gem_mifinit(sc); 92042c1b001SThomas Moestl 9212a79fd39SMarius Strobl /* step 3. Setup data structures in host memory. */ 9221ed3fed7SMarius Strobl if (gem_meminit(sc) != 0) 9231ed3fed7SMarius Strobl return; 92442c1b001SThomas Moestl 92542c1b001SThomas Moestl /* step 4. TX MAC registers & counters */ 92642c1b001SThomas Moestl gem_init_regs(sc); 92742c1b001SThomas Moestl 92842c1b001SThomas Moestl /* step 5. RX MAC registers & counters */ 92942c1b001SThomas Moestl gem_setladrf(sc); 93042c1b001SThomas Moestl 9312a79fd39SMarius Strobl /* step 6 & 7. Program Descriptor Ring Base Addresses. */ 93242c1b001SThomas Moestl /* NOTE: we use only 32-bit DMA addresses here. */ 933bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_HI, 0); 934bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 93542c1b001SThomas Moestl 936bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 937bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 93818100346SThomas Moestl #ifdef GEM_DEBUG 9392a79fd39SMarius Strobl CTR3(KTR_GEM, "loading RX ring %lx, TX ring %lx, cddma %lx", 94042c1b001SThomas Moestl GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 94118100346SThomas Moestl #endif 94242c1b001SThomas Moestl 94342c1b001SThomas Moestl /* step 8. Global Configuration & Interrupt Mask */ 9449ba2b298SMarius Strobl 9459ba2b298SMarius Strobl /* 9469ba2b298SMarius Strobl * Set the internal arbitration to "infinite" bursts of the 9479ba2b298SMarius Strobl * maximum length of 31 * 64 bytes so DMA transfers aren't 9489ba2b298SMarius Strobl * split up in cache line size chunks. This greatly improves 9499ba2b298SMarius Strobl * RX performance. 9509ba2b298SMarius Strobl * Enable silicon bug workarounds for the Apple variants. 9519ba2b298SMarius Strobl */ 9529ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_CONFIG, 9539ba2b298SMarius Strobl GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT | 9549ba2b298SMarius Strobl ((sc->sc_flags & GEM_PCI) != 0 ? GEM_CONFIG_BURST_INF : 9559ba2b298SMarius Strobl GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ? 9569ba2b298SMarius Strobl GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0)); 9579ba2b298SMarius Strobl 958bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_INTMASK, 9591ed3fed7SMarius Strobl ~(GEM_INTR_TX_INTME | GEM_INTR_TX_EMPTY | GEM_INTR_RX_DONE | 9601ed3fed7SMarius Strobl GEM_INTR_RX_NOBUF | GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | 9611ed3fed7SMarius Strobl GEM_INTR_BERR 9621ed3fed7SMarius Strobl #ifdef GEM_DEBUG 9631ed3fed7SMarius Strobl | GEM_INTR_PCS | GEM_INTR_MIF 9641ed3fed7SMarius Strobl #endif 9651ed3fed7SMarius Strobl )); 966bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 967336cca9eSBenno Rice GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 968bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_MASK, 9699ba2b298SMarius Strobl GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 9709ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP); 9711ed3fed7SMarius Strobl #ifdef GEM_DEBUG 972bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9731ed3fed7SMarius Strobl ~(GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME)); 9741ed3fed7SMarius Strobl #else 975bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9761ed3fed7SMarius Strobl GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME); 9771ed3fed7SMarius Strobl #endif 97842c1b001SThomas Moestl 9792a79fd39SMarius Strobl /* step 9. ETX Configuration: use mostly default values. */ 98042c1b001SThomas Moestl 9812a79fd39SMarius Strobl /* Enable DMA. */ 9829ba2b298SMarius Strobl v = gem_ringsize(GEM_NTXDESC); 9839ba2b298SMarius Strobl /* Set TX FIFO threshold and enable DMA. */ 984ccb1212aSMarius Strobl v |= ((sc->sc_variant == GEM_SUN_ERI ? 0x100 : 0x4ff) << 10) & 985ccb1212aSMarius Strobl GEM_TX_CONFIG_TXFIFO_TH; 986ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, v | GEM_TX_CONFIG_TXDMA_EN); 98742c1b001SThomas Moestl 98842c1b001SThomas Moestl /* step 10. ERX Configuration */ 98942c1b001SThomas Moestl 9901ed3fed7SMarius Strobl /* Encode Receive Descriptor ring size. */ 99142c1b001SThomas Moestl v = gem_ringsize(GEM_NRXDESC /* XXX */); 9922a79fd39SMarius Strobl /* RX TCP/UDP checksum offset */ 99312fb0330SPyun YongHyeon v |= ((ETHER_HDR_LEN + sizeof(struct ip)) << 99412fb0330SPyun YongHyeon GEM_RX_CONFIG_CXM_START_SHFT); 9959ba2b298SMarius Strobl /* Set RX FIFO threshold, set first byte offset and enable DMA. */ 996bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 99742c1b001SThomas Moestl v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 9989ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT) | 9999ba2b298SMarius Strobl GEM_RX_CONFIG_RXDMA_EN); 10001ed3fed7SMarius Strobl 10019ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 1002bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 10039ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 10049ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 10051ed3fed7SMarius Strobl 100642c1b001SThomas Moestl /* 1007336cca9eSBenno Rice * The following value is for an OFF Threshold of about 3/4 full 1008336cca9eSBenno Rice * and an ON Threshold of 1/4 full. 100942c1b001SThomas Moestl */ 1010bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 1011336cca9eSBenno Rice (3 * sc->sc_rxfifosize / 256) | 1012336cca9eSBenno Rice ((sc->sc_rxfifosize / 256) << 12)); 101342c1b001SThomas Moestl 10142a79fd39SMarius Strobl /* step 11. Configure Media. */ 101542c1b001SThomas Moestl 101642c1b001SThomas Moestl /* step 12. RX_MAC Configuration Register */ 1017bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 1018ccb1212aSMarius Strobl v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC; 1019bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 1020ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 1021ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1022bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 1023ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure RX MAC\n"); 1024bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 102542c1b001SThomas Moestl 1026ccb1212aSMarius Strobl /* step 13. TX_MAC Configuration Register */ 1027ccb1212aSMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG); 1028ccb1212aSMarius Strobl v |= GEM_MAC_TX_ENABLE; 1029ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 1030ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 1031ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1032ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 1033ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure TX MAC\n"); 1034ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, v); 1035ccb1212aSMarius Strobl 10362a79fd39SMarius Strobl /* step 14. Issue Transmit Pending command. */ 103742c1b001SThomas Moestl 10382a79fd39SMarius Strobl /* step 15. Give the reciever a swift kick. */ 1039bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 104042c1b001SThomas Moestl 104113f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 104213f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 10431ed3fed7SMarius Strobl 10441ed3fed7SMarius Strobl mii_mediachg(sc->sc_mii); 10451ed3fed7SMarius Strobl 10461ed3fed7SMarius Strobl /* Start the one second timer. */ 10471ed3fed7SMarius Strobl sc->sc_wdog_timer = 0; 10481ed3fed7SMarius Strobl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 104942c1b001SThomas Moestl } 105042c1b001SThomas Moestl 105112fb0330SPyun YongHyeon static int 10522a79fd39SMarius Strobl gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head) 105312fb0330SPyun YongHyeon { 105412fb0330SPyun YongHyeon bus_dma_segment_t txsegs[GEM_NTXSEGS]; 10552a79fd39SMarius Strobl struct gem_txsoft *txs; 1056ccb1212aSMarius Strobl struct ip *ip; 105712fb0330SPyun YongHyeon struct mbuf *m; 10582a79fd39SMarius Strobl uint64_t cflags, flags; 1059ccb1212aSMarius Strobl int error, nexttx, nsegs, offset, seg; 106042c1b001SThomas Moestl 10619ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 10629ba2b298SMarius Strobl 106342c1b001SThomas Moestl /* Get a work queue entry. */ 106442c1b001SThomas Moestl if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 1065305f2c06SThomas Moestl /* Ran out of descriptors. */ 106612fb0330SPyun YongHyeon return (ENOBUFS); 1067305f2c06SThomas Moestl } 1068ccb1212aSMarius Strobl 1069ccb1212aSMarius Strobl cflags = 0; 1070ccb1212aSMarius Strobl if (((*m_head)->m_pkthdr.csum_flags & sc->sc_csum_features) != 0) { 1071ccb1212aSMarius Strobl if (M_WRITABLE(*m_head) == 0) { 1072ccb1212aSMarius Strobl m = m_dup(*m_head, M_DONTWAIT); 1073ccb1212aSMarius Strobl m_freem(*m_head); 1074ccb1212aSMarius Strobl *m_head = m; 1075ccb1212aSMarius Strobl if (m == NULL) 1076ccb1212aSMarius Strobl return (ENOBUFS); 1077ccb1212aSMarius Strobl } 1078ccb1212aSMarius Strobl offset = sizeof(struct ether_header); 1079ccb1212aSMarius Strobl m = m_pullup(*m_head, offset + sizeof(struct ip)); 1080ccb1212aSMarius Strobl if (m == NULL) { 1081ccb1212aSMarius Strobl *m_head = NULL; 1082ccb1212aSMarius Strobl return (ENOBUFS); 1083ccb1212aSMarius Strobl } 1084ccb1212aSMarius Strobl ip = (struct ip *)(mtod(m, caddr_t) + offset); 1085ccb1212aSMarius Strobl offset += (ip->ip_hl << 2); 1086ccb1212aSMarius Strobl cflags = offset << GEM_TD_CXSUM_STARTSHFT | 1087ccb1212aSMarius Strobl ((offset + m->m_pkthdr.csum_data) << 1088ccb1212aSMarius Strobl GEM_TD_CXSUM_STUFFSHFT) | GEM_TD_CXSUM_ENABLE; 1089ccb1212aSMarius Strobl *m_head = m; 1090ccb1212aSMarius Strobl } 1091ccb1212aSMarius Strobl 109212fb0330SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap, 109312fb0330SPyun YongHyeon *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 109412fb0330SPyun YongHyeon if (error == EFBIG) { 1095304a4c6fSJohn Baldwin m = m_collapse(*m_head, M_DONTWAIT, GEM_NTXSEGS); 109612fb0330SPyun YongHyeon if (m == NULL) { 109712fb0330SPyun YongHyeon m_freem(*m_head); 109812fb0330SPyun YongHyeon *m_head = NULL; 109912fb0330SPyun YongHyeon return (ENOBUFS); 110012fb0330SPyun YongHyeon } 110112fb0330SPyun YongHyeon *m_head = m; 11022a79fd39SMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, 11032a79fd39SMarius Strobl txs->txs_dmamap, *m_head, txsegs, &nsegs, 11042a79fd39SMarius Strobl BUS_DMA_NOWAIT); 110512fb0330SPyun YongHyeon if (error != 0) { 110612fb0330SPyun YongHyeon m_freem(*m_head); 110712fb0330SPyun YongHyeon *m_head = NULL; 110812fb0330SPyun YongHyeon return (error); 110912fb0330SPyun YongHyeon } 111012fb0330SPyun YongHyeon } else if (error != 0) 111112fb0330SPyun YongHyeon return (error); 1112801772ecSMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1113801772ecSMarius Strobl KASSERT(nsegs <= GEM_NTXSEGS, 1114801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 111512fb0330SPyun YongHyeon if (nsegs == 0) { 111612fb0330SPyun YongHyeon m_freem(*m_head); 111712fb0330SPyun YongHyeon *m_head = NULL; 111812fb0330SPyun YongHyeon return (EIO); 111912fb0330SPyun YongHyeon } 112012fb0330SPyun YongHyeon 112112fb0330SPyun YongHyeon /* 112212fb0330SPyun YongHyeon * Ensure we have enough descriptors free to describe 112312fb0330SPyun YongHyeon * the packet. Note, we always reserve one descriptor 11242a79fd39SMarius Strobl * at the end of the ring as a termination point, in 11252a79fd39SMarius Strobl * order to prevent wrap-around. 112612fb0330SPyun YongHyeon */ 112712fb0330SPyun YongHyeon if (nsegs > sc->sc_txfree - 1) { 112812fb0330SPyun YongHyeon txs->txs_ndescs = 0; 112912fb0330SPyun YongHyeon bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 113012fb0330SPyun YongHyeon return (ENOBUFS); 113112fb0330SPyun YongHyeon } 113212fb0330SPyun YongHyeon 113312fb0330SPyun YongHyeon txs->txs_ndescs = nsegs; 1134305f2c06SThomas Moestl txs->txs_firstdesc = sc->sc_txnext; 113512fb0330SPyun YongHyeon nexttx = txs->txs_firstdesc; 113612fb0330SPyun YongHyeon for (seg = 0; seg < nsegs; seg++, nexttx = GEM_NEXTTX(nexttx)) { 113712fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11382a79fd39SMarius Strobl CTR6(KTR_GEM, 11392a79fd39SMarius Strobl "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)", 11402a79fd39SMarius Strobl __func__, seg, nexttx, txsegs[seg].ds_len, 11412a79fd39SMarius Strobl txsegs[seg].ds_addr, 114212fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr)); 114312fb0330SPyun YongHyeon #endif 114412fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_addr = 114512fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr); 114612fb0330SPyun YongHyeon KASSERT(txsegs[seg].ds_len < GEM_TD_BUFSIZE, 114712fb0330SPyun YongHyeon ("%s: segment size too large!", __func__)); 114812fb0330SPyun YongHyeon flags = txsegs[seg].ds_len & GEM_TD_BUFSIZE; 114912fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_flags = 115012fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, flags | cflags); 115112fb0330SPyun YongHyeon txs->txs_lastdesc = nexttx; 115242c1b001SThomas Moestl } 1153305f2c06SThomas Moestl 11542a79fd39SMarius Strobl /* Set EOP on the last descriptor. */ 115512fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11562a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: end of packet at segment %d, TX %d", 11572a79fd39SMarius Strobl __func__, seg, nexttx); 115812fb0330SPyun YongHyeon #endif 115912fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_lastdesc].gd_flags |= 116012fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_END_OF_PACKET); 116112fb0330SPyun YongHyeon 11622a79fd39SMarius Strobl /* Lastly set SOP on the first descriptor. */ 116312fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11642a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: start of packet at segment %d, TX %d", 11652a79fd39SMarius Strobl __func__, seg, nexttx); 116612fb0330SPyun YongHyeon #endif 116712fb0330SPyun YongHyeon if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 116812fb0330SPyun YongHyeon sc->sc_txwin = 0; 116912fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 117012fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_INTERRUPT_ME | 117112fb0330SPyun YongHyeon GEM_TD_START_OF_PACKET); 117212fb0330SPyun YongHyeon } else 117312fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 117412fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_START_OF_PACKET); 117512fb0330SPyun YongHyeon 117642c1b001SThomas Moestl /* Sync the DMA map. */ 11772a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 11782a79fd39SMarius Strobl BUS_DMASYNC_PREWRITE); 1179305f2c06SThomas Moestl 118018100346SThomas Moestl #ifdef GEM_DEBUG 118112fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d", 11822a79fd39SMarius Strobl __func__, txs->txs_firstdesc, txs->txs_lastdesc, 11832a79fd39SMarius Strobl txs->txs_ndescs); 118418100346SThomas Moestl #endif 118542c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 1186305f2c06SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 118712fb0330SPyun YongHyeon txs->txs_mbuf = *m_head; 1188305f2c06SThomas Moestl 1189305f2c06SThomas Moestl sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc); 1190305f2c06SThomas Moestl sc->sc_txfree -= txs->txs_ndescs; 119142c1b001SThomas Moestl 119212fb0330SPyun YongHyeon return (0); 119342c1b001SThomas Moestl } 119442c1b001SThomas Moestl 119542c1b001SThomas Moestl static void 11962a79fd39SMarius Strobl gem_init_regs(struct gem_softc *sc) 119742c1b001SThomas Moestl { 11984a0d6638SRuslan Ermilov const u_char *laddr = IF_LLADDR(sc->sc_ifp); 119942c1b001SThomas Moestl 12009ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 12019ba2b298SMarius Strobl 12022a79fd39SMarius Strobl /* These registers are not cleared on reset. */ 12031ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_INITED) == 0) { 12042a79fd39SMarius Strobl /* magic values */ 1205bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG0, 0); 1206bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG1, 8); 1207bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG2, 4); 120842c1b001SThomas Moestl 12099ba2b298SMarius Strobl /* min frame length */ 1210bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 12119ba2b298SMarius Strobl /* max frame length and max burst size */ 1212bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MAX_FRAME, 12131ed3fed7SMarius Strobl (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) | (0x2000 << 16)); 1214336cca9eSBenno Rice 12159ba2b298SMarius Strobl /* more magic values */ 1216bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PREAMBLE_LEN, 0x7); 1217bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_JAM_SIZE, 0x4); 1218bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ATTEMPT_LIMIT, 0x10); 1219bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_TYPE, 0x8088); 12209ba2b298SMarius Strobl 12219ba2b298SMarius Strobl /* random number seed */ 1222bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RANDOM_SEED, 1223336cca9eSBenno Rice ((laddr[5] << 8) | laddr[4]) & 0x3ff); 1224336cca9eSBenno Rice 12252a79fd39SMarius Strobl /* secondary MAC address: 0:0:0:0:0:0 */ 1226bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR3, 0); 1227bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR4, 0); 1228bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR5, 0); 1229336cca9eSBenno Rice 12302a79fd39SMarius Strobl /* MAC control address: 01:80:c2:00:00:01 */ 1231bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR6, 0x0001); 1232bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR7, 0xc200); 1233bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR8, 0x0180); 123442c1b001SThomas Moestl 12352a79fd39SMarius Strobl /* MAC filter address: 0:0:0:0:0:0 */ 1236bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER0, 0); 1237bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER1, 0); 1238bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER2, 0); 1239bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK1_2, 0); 1240bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK0, 0); 124142c1b001SThomas Moestl 12421ed3fed7SMarius Strobl sc->sc_flags |= GEM_INITED; 124342c1b001SThomas Moestl } 124442c1b001SThomas Moestl 12452a79fd39SMarius Strobl /* Counters need to be zeroed. */ 1246bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 1247bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 1248bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 1249bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 1250bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_DEFER_TMR_CNT, 0); 1251bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PEAK_ATTEMPTS, 0); 1252bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_FRAME_COUNT, 0); 1253bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 1254bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 1255bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 1256bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 125742c1b001SThomas Moestl 12581ed3fed7SMarius Strobl /* Set XOFF PAUSE time. */ 1259bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 12601ed3fed7SMarius Strobl 12612a79fd39SMarius Strobl /* Set the station address. */ 1262bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR0, (laddr[4] << 8) | laddr[5]); 1263bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR1, (laddr[2] << 8) | laddr[3]); 1264bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR2, (laddr[0] << 8) | laddr[1]); 1265336cca9eSBenno Rice 12661ed3fed7SMarius Strobl /* Enable MII outputs. */ 1267bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, GEM_MAC_XIF_TX_MII_ENA); 126842c1b001SThomas Moestl } 126942c1b001SThomas Moestl 127042c1b001SThomas Moestl static void 12712a79fd39SMarius Strobl gem_start(struct ifnet *ifp) 127242c1b001SThomas Moestl { 12732a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 12748cfaff7dSMarius Strobl 12758cfaff7dSMarius Strobl GEM_LOCK(sc); 12768cfaff7dSMarius Strobl gem_start_locked(ifp); 12778cfaff7dSMarius Strobl GEM_UNLOCK(sc); 12788cfaff7dSMarius Strobl } 12798cfaff7dSMarius Strobl 12809ba2b298SMarius Strobl static inline void 12819ba2b298SMarius Strobl gem_txkick(struct gem_softc *sc) 12829ba2b298SMarius Strobl { 12839ba2b298SMarius Strobl 12849ba2b298SMarius Strobl /* 12859ba2b298SMarius Strobl * Update the TX kick register. This register has to point to the 12869ba2b298SMarius Strobl * descriptor after the last valid one and for optimum performance 12879ba2b298SMarius Strobl * should be incremented in multiples of 4 (the DMA engine fetches/ 12889ba2b298SMarius Strobl * updates descriptors in batches of 4). 12899ba2b298SMarius Strobl */ 12909ba2b298SMarius Strobl #ifdef GEM_DEBUG 12919ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: %s: kicking TX %d", 12929ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txnext); 12939ba2b298SMarius Strobl #endif 12949ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 12959ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_KICK, sc->sc_txnext); 12969ba2b298SMarius Strobl } 12979ba2b298SMarius Strobl 12988cfaff7dSMarius Strobl static void 12992a79fd39SMarius Strobl gem_start_locked(struct ifnet *ifp) 13008cfaff7dSMarius Strobl { 13012a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 130212fb0330SPyun YongHyeon struct mbuf *m; 13039ba2b298SMarius Strobl int kicked, ntx; 13049ba2b298SMarius Strobl 13059ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 130642c1b001SThomas Moestl 130713f4c340SRobert Watson if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 13081ed3fed7SMarius Strobl IFF_DRV_RUNNING || (sc->sc_flags & GEM_LINK) == 0) 130942c1b001SThomas Moestl return; 131042c1b001SThomas Moestl 131118100346SThomas Moestl #ifdef GEM_DEBUG 131212fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: txfree %d, txnext %d", 13131ed3fed7SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txfree, 13141ed3fed7SMarius Strobl sc->sc_txnext); 131518100346SThomas Moestl #endif 13162a79fd39SMarius Strobl ntx = 0; 13179ba2b298SMarius Strobl kicked = 0; 131812fb0330SPyun YongHyeon for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && sc->sc_txfree > 1;) { 131912fb0330SPyun YongHyeon IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 132012fb0330SPyun YongHyeon if (m == NULL) 132142c1b001SThomas Moestl break; 13221ed3fed7SMarius Strobl if (gem_load_txmbuf(sc, &m) != 0) { 132312fb0330SPyun YongHyeon if (m == NULL) 132412fb0330SPyun YongHyeon break; 132512fb0330SPyun YongHyeon ifp->if_drv_flags |= IFF_DRV_OACTIVE; 132612fb0330SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m); 132742c1b001SThomas Moestl break; 132842c1b001SThomas Moestl } 13299ba2b298SMarius Strobl if ((sc->sc_txnext % 4) == 0) { 13309ba2b298SMarius Strobl gem_txkick(sc); 13319ba2b298SMarius Strobl kicked = 1; 13329ba2b298SMarius Strobl } else 13339ba2b298SMarius Strobl kicked = 0; 133418100346SThomas Moestl ntx++; 133512fb0330SPyun YongHyeon BPF_MTAP(ifp, m); 1336305f2c06SThomas Moestl } 1337305f2c06SThomas Moestl 1338305f2c06SThomas Moestl if (ntx > 0) { 13399ba2b298SMarius Strobl if (kicked == 0) 13409ba2b298SMarius Strobl gem_txkick(sc); 134118100346SThomas Moestl #ifdef GEM_DEBUG 1342305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d", 13431ed3fed7SMarius Strobl device_get_name(sc->sc_dev), sc->sc_txnext); 134418100346SThomas Moestl #endif 1345305f2c06SThomas Moestl 134642c1b001SThomas Moestl /* Set a watchdog timer in case the chip flakes out. */ 13478cb37876SMarius Strobl sc->sc_wdog_timer = 5; 134818100346SThomas Moestl #ifdef GEM_DEBUG 134912fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 13502a79fd39SMarius Strobl device_get_name(sc->sc_dev), __func__, 13512a79fd39SMarius Strobl sc->sc_wdog_timer); 135218100346SThomas Moestl #endif 135342c1b001SThomas Moestl } 135442c1b001SThomas Moestl } 135542c1b001SThomas Moestl 135642c1b001SThomas Moestl static void 13572a79fd39SMarius Strobl gem_tint(struct gem_softc *sc) 135842c1b001SThomas Moestl { 1359fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 136042c1b001SThomas Moestl struct gem_txsoft *txs; 13619ba2b298SMarius Strobl int progress; 13629ba2b298SMarius Strobl uint32_t txlast; 136318100346SThomas Moestl #ifdef GEM_DEBUG 13642a79fd39SMarius Strobl int i; 13652a79fd39SMarius Strobl 13669ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 13679ba2b298SMarius Strobl 136812fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 136918100346SThomas Moestl #endif 137042c1b001SThomas Moestl 137142c1b001SThomas Moestl /* 13722a79fd39SMarius Strobl * Go through our TX list and free mbufs for those 137342c1b001SThomas Moestl * frames that have been transmitted. 137442c1b001SThomas Moestl */ 13752a79fd39SMarius Strobl progress = 0; 1376b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 137742c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 137842c1b001SThomas Moestl #ifdef GEM_DEBUG 13792a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 138042c1b001SThomas Moestl printf(" txsoft %p transmit chain:\n", txs); 138142c1b001SThomas Moestl for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 138242c1b001SThomas Moestl printf("descriptor %d: ", i); 13832a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 13842a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 13852a79fd39SMarius Strobl sc->sc_txdescs[i].gd_flags)); 13862a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 13872a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 13882a79fd39SMarius Strobl sc->sc_txdescs[i].gd_addr)); 138942c1b001SThomas Moestl if (i == txs->txs_lastdesc) 139042c1b001SThomas Moestl break; 139142c1b001SThomas Moestl } 139242c1b001SThomas Moestl } 139342c1b001SThomas Moestl #endif 139442c1b001SThomas Moestl 139542c1b001SThomas Moestl /* 13961ed3fed7SMarius Strobl * In theory, we could harvest some descriptors before 139742c1b001SThomas Moestl * the ring is empty, but that's a bit complicated. 139842c1b001SThomas Moestl * 139942c1b001SThomas Moestl * GEM_TX_COMPLETION points to the last descriptor 140042c1b001SThomas Moestl * processed + 1. 140142c1b001SThomas Moestl */ 1402bd3d9826SMarius Strobl txlast = GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION); 140318100346SThomas Moestl #ifdef GEM_DEBUG 140412fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: txs->txs_firstdesc = %d, " 140542c1b001SThomas Moestl "txs->txs_lastdesc = %d, txlast = %d", 140612fb0330SPyun YongHyeon __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast); 140718100346SThomas Moestl #endif 140842c1b001SThomas Moestl if (txs->txs_firstdesc <= txs->txs_lastdesc) { 140942c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) && 141042c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 141142c1b001SThomas Moestl break; 141242c1b001SThomas Moestl } else { 14132a79fd39SMarius Strobl /* Ick -- this command wraps. */ 141442c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) || 141542c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 141642c1b001SThomas Moestl break; 141742c1b001SThomas Moestl } 141842c1b001SThomas Moestl 141918100346SThomas Moestl #ifdef GEM_DEBUG 14202a79fd39SMarius Strobl CTR1(KTR_GEM, "%s: releasing a descriptor", __func__); 142118100346SThomas Moestl #endif 142242c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 142342c1b001SThomas Moestl 142442c1b001SThomas Moestl sc->sc_txfree += txs->txs_ndescs; 142542c1b001SThomas Moestl 1426305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 142742c1b001SThomas Moestl BUS_DMASYNC_POSTWRITE); 1428305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 142942c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 143042c1b001SThomas Moestl m_freem(txs->txs_mbuf); 143142c1b001SThomas Moestl txs->txs_mbuf = NULL; 143242c1b001SThomas Moestl } 143342c1b001SThomas Moestl 143442c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 143542c1b001SThomas Moestl 143642c1b001SThomas Moestl ifp->if_opackets++; 1437336cca9eSBenno Rice progress = 1; 143842c1b001SThomas Moestl } 143942c1b001SThomas Moestl 144018100346SThomas Moestl #ifdef GEM_DEBUG 14412a79fd39SMarius Strobl CTR4(KTR_GEM, "%s: GEM_TX_STATE_MACHINE %x GEM_TX_DATA_PTR %llx " 144242c1b001SThomas Moestl "GEM_TX_COMPLETION %x", 1443bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_STATE_MACHINE), 1444bd3d9826SMarius Strobl ((long long)GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_HI) << 32) | 1445bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_LO), 1446bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION)); 144718100346SThomas Moestl #endif 144842c1b001SThomas Moestl 1449336cca9eSBenno Rice if (progress) { 1450336cca9eSBenno Rice if (sc->sc_txfree == GEM_NTXDESC - 1) 1451336cca9eSBenno Rice sc->sc_txwin = 0; 145242c1b001SThomas Moestl 14532a79fd39SMarius Strobl /* 14542a79fd39SMarius Strobl * We freed some descriptors, so reset IFF_DRV_OACTIVE 14552a79fd39SMarius Strobl * and restart. 14562a79fd39SMarius Strobl */ 145713f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 14589ba2b298SMarius Strobl if (STAILQ_EMPTY(&sc->sc_txdirtyq)) 14599ba2b298SMarius Strobl sc->sc_wdog_timer = 0; 146012fb0330SPyun YongHyeon gem_start_locked(ifp); 1461336cca9eSBenno Rice } 146242c1b001SThomas Moestl 146318100346SThomas Moestl #ifdef GEM_DEBUG 146412fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 146512fb0330SPyun YongHyeon device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer); 146618100346SThomas Moestl #endif 146742c1b001SThomas Moestl } 146842c1b001SThomas Moestl 1469c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 14700d80b9bdSThomas Moestl static void 14712a79fd39SMarius Strobl gem_rint_timeout(void *arg) 14720d80b9bdSThomas Moestl { 14732a79fd39SMarius Strobl struct gem_softc *sc = arg; 14740d80b9bdSThomas Moestl 14751f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 14769ba2b298SMarius Strobl 14778cfaff7dSMarius Strobl gem_rint(sc); 14780d80b9bdSThomas Moestl } 147911e3f060SJake Burkholder #endif 14800d80b9bdSThomas Moestl 148142c1b001SThomas Moestl static void 14822a79fd39SMarius Strobl gem_rint(struct gem_softc *sc) 148342c1b001SThomas Moestl { 1484fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 148542c1b001SThomas Moestl struct mbuf *m; 14862a79fd39SMarius Strobl uint64_t rxstat; 14872a79fd39SMarius Strobl uint32_t rxcomp; 148842c1b001SThomas Moestl 14899ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 14909ba2b298SMarius Strobl 1491c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 14920d80b9bdSThomas Moestl callout_stop(&sc->sc_rx_ch); 1493c3d5598aSMarius Strobl #endif 149418100346SThomas Moestl #ifdef GEM_DEBUG 149512fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 149618100346SThomas Moestl #endif 1497336cca9eSBenno Rice 1498336cca9eSBenno Rice /* 1499336cca9eSBenno Rice * Read the completion register once. This limits 1500336cca9eSBenno Rice * how long the following loop can execute. 1501336cca9eSBenno Rice */ 1502bd3d9826SMarius Strobl rxcomp = GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION); 150318100346SThomas Moestl #ifdef GEM_DEBUG 15049ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: sc->sc_rxptr %d, complete %d", 150512fb0330SPyun YongHyeon __func__, sc->sc_rxptr, rxcomp); 150618100346SThomas Moestl #endif 15079ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 15081ed3fed7SMarius Strobl for (; sc->sc_rxptr != rxcomp;) { 15091ed3fed7SMarius Strobl m = sc->sc_rxsoft[sc->sc_rxptr].rxs_mbuf; 15101ed3fed7SMarius Strobl rxstat = GEM_DMA_READ(sc, 15111ed3fed7SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags); 151242c1b001SThomas Moestl 151342c1b001SThomas Moestl if (rxstat & GEM_RD_OWN) { 1514c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 151542c1b001SThomas Moestl /* 15160d80b9bdSThomas Moestl * The descriptor is still marked as owned, although 15170d80b9bdSThomas Moestl * it is supposed to have completed. This has been 15180d80b9bdSThomas Moestl * observed on some machines. Just exiting here 15190d80b9bdSThomas Moestl * might leave the packet sitting around until another 15200d80b9bdSThomas Moestl * one arrives to trigger a new interrupt, which is 15210d80b9bdSThomas Moestl * generally undesirable, so set up a timeout. 152242c1b001SThomas Moestl */ 15230d80b9bdSThomas Moestl callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 15240d80b9bdSThomas Moestl gem_rint_timeout, sc); 1525336cca9eSBenno Rice #endif 15261ed3fed7SMarius Strobl m = NULL; 15271ed3fed7SMarius Strobl goto kickit; 152842c1b001SThomas Moestl } 152942c1b001SThomas Moestl 153042c1b001SThomas Moestl if (rxstat & GEM_RD_BAD_CRC) { 1531336cca9eSBenno Rice ifp->if_ierrors++; 153242c1b001SThomas Moestl device_printf(sc->sc_dev, "receive error: CRC error\n"); 15331ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15341ed3fed7SMarius Strobl m = NULL; 15351ed3fed7SMarius Strobl goto kickit; 153642c1b001SThomas Moestl } 153742c1b001SThomas Moestl 153842c1b001SThomas Moestl #ifdef GEM_DEBUG 15392a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 15401ed3fed7SMarius Strobl printf(" rxsoft %p descriptor %d: ", 15411ed3fed7SMarius Strobl &sc->sc_rxsoft[sc->sc_rxptr], sc->sc_rxptr); 15422a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 15432a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15442a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags)); 15452a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 15462a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15472a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_addr)); 154842c1b001SThomas Moestl } 154942c1b001SThomas Moestl #endif 155042c1b001SThomas Moestl 155142c1b001SThomas Moestl /* 155242c1b001SThomas Moestl * Allocate a new mbuf cluster. If that fails, we are 155342c1b001SThomas Moestl * out of memory, and must drop the packet and recycle 155442c1b001SThomas Moestl * the buffer that's already attached to this descriptor. 155542c1b001SThomas Moestl */ 15561ed3fed7SMarius Strobl if (gem_add_rxbuf(sc, sc->sc_rxptr) != 0) { 155742c1b001SThomas Moestl ifp->if_ierrors++; 15581ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15591ed3fed7SMarius Strobl m = NULL; 15601ed3fed7SMarius Strobl } 15611ed3fed7SMarius Strobl 15621ed3fed7SMarius Strobl kickit: 15631ed3fed7SMarius Strobl /* 15641ed3fed7SMarius Strobl * Update the RX kick register. This register has to point 15651ed3fed7SMarius Strobl * to the descriptor after the last valid one (before the 15669ba2b298SMarius Strobl * current batch) and for optimum performance should be 15679ba2b298SMarius Strobl * incremented in multiples of 4 (the DMA engine fetches/ 15689ba2b298SMarius Strobl * updates descriptors in batches of 4). 15691ed3fed7SMarius Strobl */ 15701ed3fed7SMarius Strobl sc->sc_rxptr = GEM_NEXTRX(sc->sc_rxptr); 15711ed3fed7SMarius Strobl if ((sc->sc_rxptr % 4) == 0) { 1572ccb1212aSMarius Strobl GEM_CDSYNC(sc, 1573ccb1212aSMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1574bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, 15751ed3fed7SMarius Strobl (sc->sc_rxptr + GEM_NRXDESC - 4) & 15761ed3fed7SMarius Strobl GEM_NRXDESC_MASK); 15771ed3fed7SMarius Strobl } 15781ed3fed7SMarius Strobl 15791ed3fed7SMarius Strobl if (m == NULL) { 15801ed3fed7SMarius Strobl if (rxstat & GEM_RD_OWN) 15811ed3fed7SMarius Strobl break; 158242c1b001SThomas Moestl continue; 158342c1b001SThomas Moestl } 158442c1b001SThomas Moestl 15851ed3fed7SMarius Strobl ifp->if_ipackets++; 15869ba2b298SMarius Strobl m->m_data += ETHER_ALIGN; /* first byte offset */ 158742c1b001SThomas Moestl m->m_pkthdr.rcvif = ifp; 15881ed3fed7SMarius Strobl m->m_pkthdr.len = m->m_len = GEM_RD_BUFLEN(rxstat); 158912fb0330SPyun YongHyeon 159012fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 159112fb0330SPyun YongHyeon gem_rxcksum(m, rxstat); 159242c1b001SThomas Moestl 159342c1b001SThomas Moestl /* Pass it on. */ 15948cfaff7dSMarius Strobl GEM_UNLOCK(sc); 1595673d9191SSam Leffler (*ifp->if_input)(ifp, m); 15968cfaff7dSMarius Strobl GEM_LOCK(sc); 159742c1b001SThomas Moestl } 159842c1b001SThomas Moestl 159918100346SThomas Moestl #ifdef GEM_DEBUG 16009ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: done sc->sc_rxptr %d, complete %d", __func__, 1601bd3d9826SMarius Strobl sc->sc_rxptr, GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION)); 160218100346SThomas Moestl #endif 160342c1b001SThomas Moestl } 160442c1b001SThomas Moestl 160542c1b001SThomas Moestl static int 16062a79fd39SMarius Strobl gem_add_rxbuf(struct gem_softc *sc, int idx) 160742c1b001SThomas Moestl { 160842c1b001SThomas Moestl struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 160942c1b001SThomas Moestl struct mbuf *m; 1610c3d5598aSMarius Strobl bus_dma_segment_t segs[1]; 1611c3d5598aSMarius Strobl int error, nsegs; 161242c1b001SThomas Moestl 16139ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 16149ba2b298SMarius Strobl 1615a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 161642c1b001SThomas Moestl if (m == NULL) 161742c1b001SThomas Moestl return (ENOBUFS); 1618305f2c06SThomas Moestl m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 161942c1b001SThomas Moestl 162042c1b001SThomas Moestl #ifdef GEM_DEBUG 16212a79fd39SMarius Strobl /* Bzero the packet to check DMA. */ 162242c1b001SThomas Moestl memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 162342c1b001SThomas Moestl #endif 162442c1b001SThomas Moestl 1625b2d59f42SThomas Moestl if (rxs->rxs_mbuf != NULL) { 1626b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 1627b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 1628305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 1629b2d59f42SThomas Moestl } 163042c1b001SThomas Moestl 1631c3d5598aSMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap, 1632c3d5598aSMarius Strobl m, segs, &nsegs, BUS_DMA_NOWAIT); 1633c3d5598aSMarius Strobl if (error != 0) { 16342a79fd39SMarius Strobl device_printf(sc->sc_dev, 16352a79fd39SMarius Strobl "cannot load RS DMA map %d, error = %d\n", idx, error); 1636c3d5598aSMarius Strobl m_freem(m); 16371ed3fed7SMarius Strobl return (error); 163842c1b001SThomas Moestl } 16392a79fd39SMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1640801772ecSMarius Strobl KASSERT(nsegs == 1, 1641801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 16421ed3fed7SMarius Strobl rxs->rxs_mbuf = m; 1643c3d5598aSMarius Strobl rxs->rxs_paddr = segs[0].ds_addr; 164442c1b001SThomas Moestl 16452a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 16462a79fd39SMarius Strobl BUS_DMASYNC_PREREAD); 164742c1b001SThomas Moestl 164842c1b001SThomas Moestl GEM_INIT_RXDESC(sc, idx); 164942c1b001SThomas Moestl 165042c1b001SThomas Moestl return (0); 165142c1b001SThomas Moestl } 165242c1b001SThomas Moestl 165342c1b001SThomas Moestl static void 16542a79fd39SMarius Strobl gem_eint(struct gem_softc *sc, u_int status) 165542c1b001SThomas Moestl { 165642c1b001SThomas Moestl 16571ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 16581ed3fed7SMarius Strobl if ((status & GEM_INTR_RX_TAG_ERR) != 0) { 16591ed3fed7SMarius Strobl gem_reset_rxdma(sc); 166042c1b001SThomas Moestl return; 166142c1b001SThomas Moestl } 166242c1b001SThomas Moestl 16639ba2b298SMarius Strobl device_printf(sc->sc_dev, "%s: status 0x%x", __func__, status); 16649ba2b298SMarius Strobl if ((status & GEM_INTR_BERR) != 0) { 16659ba2b298SMarius Strobl if ((sc->sc_flags & GEM_PCI) != 0) 16669ba2b298SMarius Strobl printf(", PCI bus error 0x%x\n", 16679ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_PCI_ERROR_STATUS)); 16689ba2b298SMarius Strobl else 16699ba2b298SMarius Strobl printf(", SBus error 0x%x\n", 16709ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_SBUS_STATUS)); 16719ba2b298SMarius Strobl } 167242c1b001SThomas Moestl } 167342c1b001SThomas Moestl 167442c1b001SThomas Moestl void 16752a79fd39SMarius Strobl gem_intr(void *v) 167642c1b001SThomas Moestl { 16772a79fd39SMarius Strobl struct gem_softc *sc = v; 16781ed3fed7SMarius Strobl uint32_t status, status2; 167942c1b001SThomas Moestl 16808cfaff7dSMarius Strobl GEM_LOCK(sc); 1681bd3d9826SMarius Strobl status = GEM_BANK1_READ_4(sc, GEM_STATUS); 16821ed3fed7SMarius Strobl 168318100346SThomas Moestl #ifdef GEM_DEBUG 168412fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: cplt %x, status %x", 16859ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, 16869ba2b298SMarius Strobl (status >> GEM_STATUS_TX_COMPLETION_SHFT), (u_int)status); 16871ed3fed7SMarius Strobl 16881ed3fed7SMarius Strobl /* 16891ed3fed7SMarius Strobl * PCS interrupts must be cleared, otherwise no traffic is passed! 16901ed3fed7SMarius Strobl */ 16911ed3fed7SMarius Strobl if ((status & GEM_INTR_PCS) != 0) { 16922a79fd39SMarius Strobl status2 = 1693bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS) | 1694bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS); 16951ed3fed7SMarius Strobl if ((status2 & GEM_MII_INTERRUP_LINK) != 0) 16961ed3fed7SMarius Strobl device_printf(sc->sc_dev, 16971ed3fed7SMarius Strobl "%s: PCS link status changed\n", __func__); 16981ed3fed7SMarius Strobl } 16991ed3fed7SMarius Strobl if ((status & GEM_MAC_CONTROL_STATUS) != 0) { 1700bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_STATUS); 17011ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSED) != 0) 17021ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17031ed3fed7SMarius Strobl "%s: PAUSE received (PAUSE time %d slots)\n", 17041ed3fed7SMarius Strobl __func__, GEM_MAC_PAUSE_TIME(status2)); 17051ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSE) != 0) 17061ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17071ed3fed7SMarius Strobl "%s: transited to PAUSE state\n", __func__); 17081ed3fed7SMarius Strobl if ((status2 & GEM_MAC_RESUME) != 0) 17091ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17101ed3fed7SMarius Strobl "%s: transited to non-PAUSE state\n", __func__); 17111ed3fed7SMarius Strobl } 17121ed3fed7SMarius Strobl if ((status & GEM_INTR_MIF) != 0) 17131ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__); 171418100346SThomas Moestl #endif 171542c1b001SThomas Moestl 17169ba2b298SMarius Strobl if (__predict_false(status & 17171ed3fed7SMarius Strobl (GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | GEM_INTR_BERR)) != 0) 171842c1b001SThomas Moestl gem_eint(sc, status); 171942c1b001SThomas Moestl 172042c1b001SThomas Moestl if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 172142c1b001SThomas Moestl gem_rint(sc); 172242c1b001SThomas Moestl 17231ed3fed7SMarius Strobl if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 17241ed3fed7SMarius Strobl gem_tint(sc); 17251ed3fed7SMarius Strobl 17269ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_TX_MAC) != 0)) { 1727bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS); 17282a79fd39SMarius Strobl if ((status2 & 17299ba2b298SMarius Strobl ~(GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 17309ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP)) != 0) 17312a79fd39SMarius Strobl device_printf(sc->sc_dev, 17322a79fd39SMarius Strobl "MAC TX fault, status %x\n", status2); 17332a79fd39SMarius Strobl if ((status2 & 17349ba2b298SMarius Strobl (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) != 0) { 17359ba2b298SMarius Strobl sc->sc_ifp->if_oerrors++; 17368cfaff7dSMarius Strobl gem_init_locked(sc); 173742c1b001SThomas Moestl } 17389ba2b298SMarius Strobl } 17399ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_RX_MAC) != 0)) { 1740bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS); 174100d12766SMarius Strobl /* 17421ed3fed7SMarius Strobl * At least with GEM_SUN_GEM and some GEM_SUN_ERI 17431ed3fed7SMarius Strobl * revisions GEM_MAC_RX_OVERFLOW happen often due to a 17441ed3fed7SMarius Strobl * silicon bug so handle them silently. Moreover, it's 17451ed3fed7SMarius Strobl * likely that the receiver has hung so we reset it. 174600d12766SMarius Strobl */ 17472a79fd39SMarius Strobl if ((status2 & GEM_MAC_RX_OVERFLOW) != 0) { 17481ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 17491ed3fed7SMarius Strobl gem_reset_rxdma(sc); 17502a79fd39SMarius Strobl } else if ((status2 & 17512a79fd39SMarius Strobl ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) != 0) 17522a79fd39SMarius Strobl device_printf(sc->sc_dev, 17532a79fd39SMarius Strobl "MAC RX fault, status %x\n", status2); 175442c1b001SThomas Moestl } 17558cfaff7dSMarius Strobl GEM_UNLOCK(sc); 175642c1b001SThomas Moestl } 175742c1b001SThomas Moestl 17588cb37876SMarius Strobl static int 17592a79fd39SMarius Strobl gem_watchdog(struct gem_softc *sc) 176042c1b001SThomas Moestl { 1761ccb1212aSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 176242c1b001SThomas Moestl 17638cb37876SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 17648cb37876SMarius Strobl 176518100346SThomas Moestl #ifdef GEM_DEBUG 17662a79fd39SMarius Strobl CTR4(KTR_GEM, 17672a79fd39SMarius Strobl "%s: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x GEM_MAC_RX_CONFIG %x", 1768bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_RX_CONFIG), 1769bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS), 1770bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG)); 17712a79fd39SMarius Strobl CTR4(KTR_GEM, 17722a79fd39SMarius Strobl "%s: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x GEM_MAC_TX_CONFIG %x", 1773bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_CONFIG), 1774bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS), 1775bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG)); 177618100346SThomas Moestl #endif 177742c1b001SThomas Moestl 17788cb37876SMarius Strobl if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) 17798cb37876SMarius Strobl return (0); 17808cb37876SMarius Strobl 17811ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_LINK) != 0) 178242c1b001SThomas Moestl device_printf(sc->sc_dev, "device timeout\n"); 17831ed3fed7SMarius Strobl else if (bootverbose) 17841ed3fed7SMarius Strobl device_printf(sc->sc_dev, "device timeout (no link)\n"); 1785ccb1212aSMarius Strobl ++ifp->if_oerrors; 178642c1b001SThomas Moestl 178742c1b001SThomas Moestl /* Try to get more packets going. */ 17888cfaff7dSMarius Strobl gem_init_locked(sc); 1789ccb1212aSMarius Strobl gem_start_locked(ifp); 17908cb37876SMarius Strobl return (EJUSTRETURN); 179142c1b001SThomas Moestl } 179242c1b001SThomas Moestl 179342c1b001SThomas Moestl static void 17942a79fd39SMarius Strobl gem_mifinit(struct gem_softc *sc) 179542c1b001SThomas Moestl { 179642c1b001SThomas Moestl 1797801772ecSMarius Strobl /* Configure the MIF in frame mode. */ 1798bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, 1799bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & ~GEM_MIF_CONFIG_BB_ENA); 180042c1b001SThomas Moestl } 180142c1b001SThomas Moestl 180242c1b001SThomas Moestl /* 180342c1b001SThomas Moestl * MII interface 180442c1b001SThomas Moestl * 180578d22f42SMarius Strobl * The MII interface supports at least three different operating modes: 180642c1b001SThomas Moestl * 180742c1b001SThomas Moestl * Bitbang mode is implemented using data, clock and output enable registers. 180842c1b001SThomas Moestl * 180942c1b001SThomas Moestl * Frame mode is implemented by loading a complete frame into the frame 181042c1b001SThomas Moestl * register and polling the valid bit for completion. 181142c1b001SThomas Moestl * 181242c1b001SThomas Moestl * Polling mode uses the frame register but completion is indicated by 181342c1b001SThomas Moestl * an interrupt. 181442c1b001SThomas Moestl * 181542c1b001SThomas Moestl */ 181642c1b001SThomas Moestl int 18172a79fd39SMarius Strobl gem_mii_readreg(device_t dev, int phy, int reg) 181842c1b001SThomas Moestl { 18192a79fd39SMarius Strobl struct gem_softc *sc; 182042c1b001SThomas Moestl int n; 18212a79fd39SMarius Strobl uint32_t v; 182242c1b001SThomas Moestl 182342c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 18241ed3fed7SMarius Strobl printf("%s: phy %d reg %d\n", __func__, phy, reg); 182542c1b001SThomas Moestl #endif 182642c1b001SThomas Moestl 18272a79fd39SMarius Strobl sc = device_get_softc(dev); 18281ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 18291ed3fed7SMarius Strobl return (0); 18301ed3fed7SMarius Strobl 18311ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 18321ed3fed7SMarius Strobl switch (reg) { 18331ed3fed7SMarius Strobl case MII_BMCR: 18341ed3fed7SMarius Strobl reg = GEM_MII_CONTROL; 18351ed3fed7SMarius Strobl break; 18361ed3fed7SMarius Strobl case MII_BMSR: 18371ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 18381ed3fed7SMarius Strobl break; 18391ed3fed7SMarius Strobl case MII_PHYIDR1: 18401ed3fed7SMarius Strobl case MII_PHYIDR2: 18411ed3fed7SMarius Strobl return (0); 18421ed3fed7SMarius Strobl case MII_ANAR: 18431ed3fed7SMarius Strobl reg = GEM_MII_ANAR; 18441ed3fed7SMarius Strobl break; 18451ed3fed7SMarius Strobl case MII_ANLPAR: 18461ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 18471ed3fed7SMarius Strobl break; 18481ed3fed7SMarius Strobl case MII_EXTSR: 18491ed3fed7SMarius Strobl return (EXTSR_1000XFDX | EXTSR_1000XHDX); 18501ed3fed7SMarius Strobl default: 18511ed3fed7SMarius Strobl device_printf(sc->sc_dev, 18521ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 18531ed3fed7SMarius Strobl return (0); 18541ed3fed7SMarius Strobl } 1855bd3d9826SMarius Strobl return (GEM_BANK1_READ_4(sc, reg)); 18561ed3fed7SMarius Strobl } 185742c1b001SThomas Moestl 18582a79fd39SMarius Strobl /* Construct the frame command. */ 18591ed3fed7SMarius Strobl v = GEM_MIF_FRAME_READ | 18601ed3fed7SMarius Strobl (phy << GEM_MIF_PHY_SHIFT) | 18611ed3fed7SMarius Strobl (reg << GEM_MIF_REG_SHIFT); 186242c1b001SThomas Moestl 1863bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1864ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1865ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 186642c1b001SThomas Moestl for (n = 0; n < 100; n++) { 186742c1b001SThomas Moestl DELAY(1); 1868bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 18691f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 187042c1b001SThomas Moestl return (v & GEM_MIF_FRAME_DATA); 187142c1b001SThomas Moestl } 187242c1b001SThomas Moestl 18732a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 187442c1b001SThomas Moestl return (0); 187542c1b001SThomas Moestl } 187642c1b001SThomas Moestl 187742c1b001SThomas Moestl int 18782a79fd39SMarius Strobl gem_mii_writereg(device_t dev, int phy, int reg, int val) 187942c1b001SThomas Moestl { 18802a79fd39SMarius Strobl struct gem_softc *sc; 188142c1b001SThomas Moestl int n; 18822a79fd39SMarius Strobl uint32_t v; 188342c1b001SThomas Moestl 188442c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 18851ed3fed7SMarius Strobl printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__); 188642c1b001SThomas Moestl #endif 188742c1b001SThomas Moestl 18882a79fd39SMarius Strobl sc = device_get_softc(dev); 18891ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 18901ed3fed7SMarius Strobl return (0); 18911ed3fed7SMarius Strobl 18921ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 18931ed3fed7SMarius Strobl switch (reg) { 18941ed3fed7SMarius Strobl case MII_BMSR: 18951ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 18961ed3fed7SMarius Strobl break; 1897ccb1212aSMarius Strobl case MII_BMCR: 1898ccb1212aSMarius Strobl reg = GEM_MII_CONTROL; 1899ccb1212aSMarius Strobl if ((val & GEM_MII_CONTROL_RESET) == 0) 1900ccb1212aSMarius Strobl break; 1901ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONTROL, val); 1902ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONTROL, 4, 1903ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1904ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MII_CONTROL, 1905ccb1212aSMarius Strobl GEM_MII_CONTROL_RESET, 0)) 1906ccb1212aSMarius Strobl device_printf(sc->sc_dev, 1907ccb1212aSMarius Strobl "cannot reset PCS\n"); 1908ccb1212aSMarius Strobl /* FALLTHROUGH */ 19091ed3fed7SMarius Strobl case MII_ANAR: 1910bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 0); 1911bd3d9826SMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4, 19121ed3fed7SMarius Strobl BUS_SPACE_BARRIER_WRITE); 1913bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_ANAR, val); 1914bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 19151ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 1916bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 19171ed3fed7SMarius Strobl GEM_MII_CONFIG_ENABLE); 19181ed3fed7SMarius Strobl return (0); 19191ed3fed7SMarius Strobl case MII_ANLPAR: 19201ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 19211ed3fed7SMarius Strobl break; 19221ed3fed7SMarius Strobl default: 19231ed3fed7SMarius Strobl device_printf(sc->sc_dev, 19241ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 19251ed3fed7SMarius Strobl return (0); 19261ed3fed7SMarius Strobl } 1927bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, reg, val); 19281ed3fed7SMarius Strobl return (0); 19291ed3fed7SMarius Strobl } 19301ed3fed7SMarius Strobl 19312a79fd39SMarius Strobl /* Construct the frame command. */ 193242c1b001SThomas Moestl v = GEM_MIF_FRAME_WRITE | 193342c1b001SThomas Moestl (phy << GEM_MIF_PHY_SHIFT) | 193442c1b001SThomas Moestl (reg << GEM_MIF_REG_SHIFT) | 193542c1b001SThomas Moestl (val & GEM_MIF_FRAME_DATA); 193642c1b001SThomas Moestl 1937bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1938ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1939ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 194042c1b001SThomas Moestl for (n = 0; n < 100; n++) { 194142c1b001SThomas Moestl DELAY(1); 1942bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 19431f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 194442c1b001SThomas Moestl return (1); 194542c1b001SThomas Moestl } 194642c1b001SThomas Moestl 19472a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 194842c1b001SThomas Moestl return (0); 194942c1b001SThomas Moestl } 195042c1b001SThomas Moestl 195142c1b001SThomas Moestl void 19522a79fd39SMarius Strobl gem_mii_statchg(device_t dev) 195342c1b001SThomas Moestl { 19542a79fd39SMarius Strobl struct gem_softc *sc; 19551ed3fed7SMarius Strobl int gigabit; 19561ed3fed7SMarius Strobl uint32_t rxcfg, txcfg, v; 195742c1b001SThomas Moestl 19582a79fd39SMarius Strobl sc = device_get_softc(dev); 19592a79fd39SMarius Strobl 19609ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 19619ba2b298SMarius Strobl 196242c1b001SThomas Moestl #ifdef GEM_DEBUG 19632a79fd39SMarius Strobl if ((sc->sc_ifp->if_flags & IFF_DEBUG) != 0) 19641ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: status change: PHY = %d\n", 19651ed3fed7SMarius Strobl __func__, sc->sc_phyad); 196642c1b001SThomas Moestl #endif 196742c1b001SThomas Moestl 19681ed3fed7SMarius Strobl if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 && 19691ed3fed7SMarius Strobl IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE) 19701ed3fed7SMarius Strobl sc->sc_flags |= GEM_LINK; 19711ed3fed7SMarius Strobl else 19721ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 19731ed3fed7SMarius Strobl 19741ed3fed7SMarius Strobl switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) { 19751ed3fed7SMarius Strobl case IFM_1000_SX: 19761ed3fed7SMarius Strobl case IFM_1000_LX: 19771ed3fed7SMarius Strobl case IFM_1000_CX: 19781ed3fed7SMarius Strobl case IFM_1000_T: 19791ed3fed7SMarius Strobl gigabit = 1; 19801ed3fed7SMarius Strobl break; 19811ed3fed7SMarius Strobl default: 19821ed3fed7SMarius Strobl gigabit = 0; 198342c1b001SThomas Moestl } 19841ed3fed7SMarius Strobl 19851ed3fed7SMarius Strobl /* 19861ed3fed7SMarius Strobl * The configuration done here corresponds to the steps F) and 19871ed3fed7SMarius Strobl * G) and as far as enabling of RX and TX MAC goes also step H) 19881ed3fed7SMarius Strobl * of the initialization sequence outlined in section 3.2.1 of 19891ed3fed7SMarius Strobl * the GEM Gigabit Ethernet ASIC Specification. 19901ed3fed7SMarius Strobl */ 19911ed3fed7SMarius Strobl 1992bd3d9826SMarius Strobl rxcfg = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 19931ed3fed7SMarius Strobl rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE); 19941ed3fed7SMarius Strobl txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT; 19951ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 19961ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS; 19971ed3fed7SMarius Strobl else if (gigabit != 0) { 19981ed3fed7SMarius Strobl rxcfg |= GEM_MAC_RX_CARR_EXTEND; 19991ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_CARR_EXTEND; 20001ed3fed7SMarius Strobl } 2001bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 2002ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 2003ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2004bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 20051ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX MAC\n"); 2006bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, txcfg); 2007bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 2008ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2009ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2010bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 20111ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX MAC\n"); 2012bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, rxcfg); 20131ed3fed7SMarius Strobl 2014bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_CONFIG) & 20151ed3fed7SMarius Strobl ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE); 20161ed3fed7SMarius Strobl #ifdef notyet 20172a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20182a79fd39SMarius Strobl IFM_ETH_RXPAUSE) != 0) 20191ed3fed7SMarius Strobl v |= GEM_MAC_CC_RX_PAUSE; 20202a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20212a79fd39SMarius Strobl IFM_ETH_TXPAUSE) != 0) 20221ed3fed7SMarius Strobl v |= GEM_MAC_CC_TX_PAUSE; 20231ed3fed7SMarius Strobl #endif 2024bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_CONFIG, v); 20251ed3fed7SMarius Strobl 20261ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 && 20271ed3fed7SMarius Strobl gigabit != 0) 2028bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20291ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_CARR_EXTEND); 20301ed3fed7SMarius Strobl else 2031bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20321ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_NORMAL); 203342c1b001SThomas Moestl 203442c1b001SThomas Moestl /* XIF Configuration */ 203542c1b001SThomas Moestl v = GEM_MAC_XIF_LINK_LED; 203642c1b001SThomas Moestl v |= GEM_MAC_XIF_TX_MII_ENA; 20371ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) == 0) { 2038bd3d9826SMarius Strobl if ((GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & 203978d22f42SMarius Strobl GEM_MIF_CONFIG_PHY_SEL) != 0) { 204042c1b001SThomas Moestl /* External MII needs echo disable if half duplex. */ 204178d22f42SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 204278d22f42SMarius Strobl IFM_FDX) == 0) 204342c1b001SThomas Moestl v |= GEM_MAC_XIF_ECHO_DISABL; 204478d22f42SMarius Strobl } else 20451ed3fed7SMarius Strobl /* 20461ed3fed7SMarius Strobl * Internal MII needs buffer enable. 20471ed3fed7SMarius Strobl * XXX buffer enable makes only sense for an 20481ed3fed7SMarius Strobl * external PHY. 20491ed3fed7SMarius Strobl */ 205042c1b001SThomas Moestl v |= GEM_MAC_XIF_MII_BUF_ENA; 205142c1b001SThomas Moestl } 20521ed3fed7SMarius Strobl if (gigabit != 0) 20531ed3fed7SMarius Strobl v |= GEM_MAC_XIF_GMII_MODE; 20541ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 20551ed3fed7SMarius Strobl v |= GEM_MAC_XIF_FDPLX_LED; 2056bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, v); 20571ed3fed7SMarius Strobl 20581ed3fed7SMarius Strobl if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 20591ed3fed7SMarius Strobl (sc->sc_flags & GEM_LINK) != 0) { 2060bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 20611ed3fed7SMarius Strobl txcfg | GEM_MAC_TX_ENABLE); 2062bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 20631ed3fed7SMarius Strobl rxcfg | GEM_MAC_RX_ENABLE); 20641ed3fed7SMarius Strobl } 206542c1b001SThomas Moestl } 206642c1b001SThomas Moestl 206742c1b001SThomas Moestl int 20682a79fd39SMarius Strobl gem_mediachange(struct ifnet *ifp) 206942c1b001SThomas Moestl { 207042c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 20711f317bf9SMarius Strobl int error; 207242c1b001SThomas Moestl 20732a79fd39SMarius Strobl /* XXX add support for serial media. */ 207442c1b001SThomas Moestl 20751f317bf9SMarius Strobl GEM_LOCK(sc); 20761f317bf9SMarius Strobl error = mii_mediachg(sc->sc_mii); 20771f317bf9SMarius Strobl GEM_UNLOCK(sc); 20781f317bf9SMarius Strobl return (error); 207942c1b001SThomas Moestl } 208042c1b001SThomas Moestl 208142c1b001SThomas Moestl void 20822a79fd39SMarius Strobl gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 208342c1b001SThomas Moestl { 208442c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 208542c1b001SThomas Moestl 20868cfaff7dSMarius Strobl GEM_LOCK(sc); 20878cfaff7dSMarius Strobl if ((ifp->if_flags & IFF_UP) == 0) { 20888cfaff7dSMarius Strobl GEM_UNLOCK(sc); 208942c1b001SThomas Moestl return; 20908cfaff7dSMarius Strobl } 209142c1b001SThomas Moestl 209242c1b001SThomas Moestl mii_pollstat(sc->sc_mii); 209342c1b001SThomas Moestl ifmr->ifm_active = sc->sc_mii->mii_media_active; 209442c1b001SThomas Moestl ifmr->ifm_status = sc->sc_mii->mii_media_status; 20958cfaff7dSMarius Strobl GEM_UNLOCK(sc); 209642c1b001SThomas Moestl } 209742c1b001SThomas Moestl 209842c1b001SThomas Moestl static int 20992a79fd39SMarius Strobl gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 210042c1b001SThomas Moestl { 210142c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 210242c1b001SThomas Moestl struct ifreq *ifr = (struct ifreq *)data; 21032a79fd39SMarius Strobl int error; 21048cfaff7dSMarius Strobl 21052a79fd39SMarius Strobl error = 0; 210642c1b001SThomas Moestl switch (cmd) { 210742c1b001SThomas Moestl case SIOCSIFFLAGS: 21081f317bf9SMarius Strobl GEM_LOCK(sc); 21092a79fd39SMarius Strobl if ((ifp->if_flags & IFF_UP) != 0) { 21101ed3fed7SMarius Strobl if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 21111ed3fed7SMarius Strobl ((ifp->if_flags ^ sc->sc_ifflags) & 21121ed3fed7SMarius Strobl (IFF_ALLMULTI | IFF_PROMISC)) != 0) 211342c1b001SThomas Moestl gem_setladrf(sc); 211442c1b001SThomas Moestl else 21158cfaff7dSMarius Strobl gem_init_locked(sc); 21162a79fd39SMarius Strobl } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 211742c1b001SThomas Moestl gem_stop(ifp, 0); 211812fb0330SPyun YongHyeon if ((ifp->if_flags & IFF_LINK0) != 0) 211912fb0330SPyun YongHyeon sc->sc_csum_features |= CSUM_UDP; 212012fb0330SPyun YongHyeon else 212112fb0330SPyun YongHyeon sc->sc_csum_features &= ~CSUM_UDP; 212212fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 212312fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 2124336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 21251f317bf9SMarius Strobl GEM_UNLOCK(sc); 212642c1b001SThomas Moestl break; 212742c1b001SThomas Moestl case SIOCADDMULTI: 212842c1b001SThomas Moestl case SIOCDELMULTI: 21291f317bf9SMarius Strobl GEM_LOCK(sc); 213042c1b001SThomas Moestl gem_setladrf(sc); 21311f317bf9SMarius Strobl GEM_UNLOCK(sc); 213242c1b001SThomas Moestl break; 213342c1b001SThomas Moestl case SIOCGIFMEDIA: 213442c1b001SThomas Moestl case SIOCSIFMEDIA: 213542c1b001SThomas Moestl error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 213642c1b001SThomas Moestl break; 213712fb0330SPyun YongHyeon case SIOCSIFCAP: 213812fb0330SPyun YongHyeon GEM_LOCK(sc); 213912fb0330SPyun YongHyeon ifp->if_capenable = ifr->ifr_reqcap; 214012fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 214112fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 214212fb0330SPyun YongHyeon else 214312fb0330SPyun YongHyeon ifp->if_hwassist = 0; 214412fb0330SPyun YongHyeon GEM_UNLOCK(sc); 214512fb0330SPyun YongHyeon break; 214642c1b001SThomas Moestl default: 21471f317bf9SMarius Strobl error = ether_ioctl(ifp, cmd, data); 214842c1b001SThomas Moestl break; 214942c1b001SThomas Moestl } 215042c1b001SThomas Moestl 215142c1b001SThomas Moestl return (error); 215242c1b001SThomas Moestl } 215342c1b001SThomas Moestl 215442c1b001SThomas Moestl static void 21552a79fd39SMarius Strobl gem_setladrf(struct gem_softc *sc) 215642c1b001SThomas Moestl { 2157fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 215842c1b001SThomas Moestl struct ifmultiaddr *inm; 2159336cca9eSBenno Rice int i; 21602a79fd39SMarius Strobl uint32_t hash[16]; 21612a79fd39SMarius Strobl uint32_t crc, v; 216242c1b001SThomas Moestl 21638cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 21648cfaff7dSMarius Strobl 21652a79fd39SMarius Strobl /* Get the current RX configuration. */ 2166bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 216742c1b001SThomas Moestl 2168336cca9eSBenno Rice /* 2169336cca9eSBenno Rice * Turn off promiscuous mode, promiscuous group mode (all multicast), 2170336cca9eSBenno Rice * and hash filter. Depending on the case, the right bit will be 2171336cca9eSBenno Rice * enabled. 2172336cca9eSBenno Rice */ 2173336cca9eSBenno Rice v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_HASH_FILTER | 2174336cca9eSBenno Rice GEM_MAC_RX_PROMISC_GRP); 2175336cca9eSBenno Rice 2176bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 2177ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2178ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2179bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_HASH_FILTER, 2180bd3d9826SMarius Strobl 0)) 21811ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX hash filter\n"); 21821ed3fed7SMarius Strobl 218342c1b001SThomas Moestl if ((ifp->if_flags & IFF_PROMISC) != 0) { 218442c1b001SThomas Moestl v |= GEM_MAC_RX_PROMISCUOUS; 218542c1b001SThomas Moestl goto chipit; 218642c1b001SThomas Moestl } 218742c1b001SThomas Moestl if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 2188336cca9eSBenno Rice v |= GEM_MAC_RX_PROMISC_GRP; 218942c1b001SThomas Moestl goto chipit; 219042c1b001SThomas Moestl } 219142c1b001SThomas Moestl 219242c1b001SThomas Moestl /* 21932a79fd39SMarius Strobl * Set up multicast address filter by passing all multicast 21942a79fd39SMarius Strobl * addresses through a crc generator, and then using the high 21952a79fd39SMarius Strobl * order 8 bits as an index into the 256 bit logical address 21962a79fd39SMarius Strobl * filter. The high order 4 bits selects the word, while the 21972a79fd39SMarius Strobl * other 4 bits select the bit within the word (where bit 0 21982a79fd39SMarius Strobl * is the MSB). 219942c1b001SThomas Moestl */ 220042c1b001SThomas Moestl 22012a79fd39SMarius Strobl /* Clear the hash table. */ 2202336cca9eSBenno Rice memset(hash, 0, sizeof(hash)); 2203336cca9eSBenno Rice 220413b203d0SRobert Watson IF_ADDR_LOCK(ifp); 2205fc74a9f9SBrooks Davis TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) { 220642c1b001SThomas Moestl if (inm->ifma_addr->sa_family != AF_LINK) 220742c1b001SThomas Moestl continue; 2208c240bd8cSMarius Strobl crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 2209c240bd8cSMarius Strobl inm->ifma_addr), ETHER_ADDR_LEN); 221042c1b001SThomas Moestl 22112a79fd39SMarius Strobl /* We just want the 8 most significant bits. */ 221242c1b001SThomas Moestl crc >>= 24; 221342c1b001SThomas Moestl 221442c1b001SThomas Moestl /* Set the corresponding bit in the filter. */ 2215336cca9eSBenno Rice hash[crc >> 4] |= 1 << (15 - (crc & 15)); 2216336cca9eSBenno Rice } 221713b203d0SRobert Watson IF_ADDR_UNLOCK(ifp); 2218336cca9eSBenno Rice 2219336cca9eSBenno Rice v |= GEM_MAC_RX_HASH_FILTER; 2220336cca9eSBenno Rice 22212a79fd39SMarius Strobl /* Now load the hash table into the chip (if we are using it). */ 22222a79fd39SMarius Strobl for (i = 0; i < 16; i++) 2223bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, 2224336cca9eSBenno Rice GEM_MAC_HASH0 + i * (GEM_MAC_HASH1 - GEM_MAC_HASH0), 2225336cca9eSBenno Rice hash[i]); 222642c1b001SThomas Moestl 222742c1b001SThomas Moestl chipit: 2228bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 222942c1b001SThomas Moestl } 2230