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> 139651aa2d8SAttilio Rao #define KTR_GEM KTR_SPARE2 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 271*65f2c0ffSMarius Strobl /* Bypass probing PHYs if we already know for sure to use a SERDES. */ 272*65f2c0ffSMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) 273*65f2c0ffSMarius Strobl goto serdes; 274*65f2c0ffSMarius Strobl 2751ed3fed7SMarius Strobl /* Bad things will happen when touching this register on ERI. */ 276*65f2c0ffSMarius Strobl if (sc->sc_variant != GEM_SUN_ERI) { 277bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE, 2781ed3fed7SMarius Strobl GEM_MII_DATAPATH_MII); 279*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_DATAPATH_MODE, 4, 280*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 281*65f2c0ffSMarius Strobl } 2821ed3fed7SMarius Strobl 28342c1b001SThomas Moestl gem_mifinit(sc); 28442c1b001SThomas Moestl 2851ed3fed7SMarius Strobl /* 2861ed3fed7SMarius Strobl * Look for an external PHY. 2871ed3fed7SMarius Strobl */ 2881ed3fed7SMarius Strobl error = ENXIO; 289bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG); 2901ed3fed7SMarius Strobl if ((v & GEM_MIF_CONFIG_MDI1) != 0) { 2911ed3fed7SMarius Strobl v |= GEM_MIF_CONFIG_PHY_SEL; 292bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v); 293*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4, 294*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2951ed3fed7SMarius Strobl switch (sc->sc_variant) { 2961ed3fed7SMarius Strobl case GEM_SUN_ERI: 2971ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 2981ed3fed7SMarius Strobl break; 2991ed3fed7SMarius Strobl default: 3001ed3fed7SMarius Strobl sc->sc_phyad = -1; 3011ed3fed7SMarius Strobl break; 3021ed3fed7SMarius Strobl } 3031ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3041ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3051ed3fed7SMarius Strobl } 3061ed3fed7SMarius Strobl 3071ed3fed7SMarius Strobl /* 3081ed3fed7SMarius Strobl * Fall back on an internal PHY if no external PHY was found. 3099e48f1e7SMarius Strobl * Note that with Apple (K2) GMACs GEM_MIF_CONFIG_MDI0 can't be 3109e48f1e7SMarius Strobl * trusted when the firmware has powered down the chip. 3111ed3fed7SMarius Strobl */ 3129e48f1e7SMarius Strobl if (error != 0 && 3139e48f1e7SMarius Strobl ((v & GEM_MIF_CONFIG_MDI0) != 0 || GEM_IS_APPLE(sc))) { 3141ed3fed7SMarius Strobl v &= ~GEM_MIF_CONFIG_PHY_SEL; 315bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, v); 316*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4, 317*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 3181ed3fed7SMarius Strobl switch (sc->sc_variant) { 3191ed3fed7SMarius Strobl case GEM_SUN_ERI: 3201ed3fed7SMarius Strobl case GEM_APPLE_K2_GMAC: 3211ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_INTERNAL; 3221ed3fed7SMarius Strobl break; 3231ed3fed7SMarius Strobl case GEM_APPLE_GMAC: 3241ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3251ed3fed7SMarius Strobl break; 3261ed3fed7SMarius Strobl default: 3271ed3fed7SMarius Strobl sc->sc_phyad = -1; 3281ed3fed7SMarius Strobl break; 3291ed3fed7SMarius Strobl } 3301ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3311ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3321ed3fed7SMarius Strobl } 3331ed3fed7SMarius Strobl 3341ed3fed7SMarius Strobl /* 3351ed3fed7SMarius Strobl * Try the external PCS SERDES if we didn't find any PHYs. 3361ed3fed7SMarius Strobl */ 3371ed3fed7SMarius Strobl if (error != 0 && sc->sc_variant == GEM_SUN_GEM) { 338*65f2c0ffSMarius Strobl serdes: 339bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_DATAPATH_MODE, 3401ed3fed7SMarius Strobl GEM_MII_DATAPATH_SERDES); 341*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_DATAPATH_MODE, 4, 342*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 343bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 3441ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 345*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_SLINK_CONTROL, 4, 346*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 347bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, GEM_MII_CONFIG_ENABLE); 348*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4, 349*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 3501ed3fed7SMarius Strobl sc->sc_flags |= GEM_SERDES; 3511ed3fed7SMarius Strobl sc->sc_phyad = GEM_PHYAD_EXTERNAL; 3521ed3fed7SMarius Strobl error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, 3531ed3fed7SMarius Strobl gem_mediachange, gem_mediastatus); 3541ed3fed7SMarius Strobl } 3551ed3fed7SMarius Strobl if (error != 0) { 3561ed3fed7SMarius Strobl device_printf(sc->sc_dev, "PHY probe failed: %d\n", error); 357305f2c06SThomas Moestl goto fail_rxd; 35842c1b001SThomas Moestl } 35942c1b001SThomas Moestl sc->sc_mii = device_get_softc(sc->sc_miibus); 36042c1b001SThomas Moestl 36142c1b001SThomas Moestl /* 36242c1b001SThomas Moestl * From this point forward, the attachment cannot fail. A failure 36342c1b001SThomas Moestl * before this point releases all resources that may have been 36442c1b001SThomas Moestl * allocated. 36542c1b001SThomas Moestl */ 36642c1b001SThomas Moestl 367801772ecSMarius Strobl /* Get RX FIFO size. */ 368336cca9eSBenno Rice sc->sc_rxfifosize = 64 * 369bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_FIFO_SIZE); 370336cca9eSBenno Rice 371801772ecSMarius Strobl /* Get TX FIFO size. */ 372bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_TX_FIFO_SIZE); 3733a5aee5aSThomas Moestl device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n", 3743a5aee5aSThomas Moestl sc->sc_rxfifosize / 1024, v / 16); 37542c1b001SThomas Moestl 37642c1b001SThomas Moestl /* Attach the interface. */ 377fc74a9f9SBrooks Davis ether_ifattach(ifp, sc->sc_enaddr); 37842c1b001SThomas Moestl 37900d12766SMarius Strobl /* 38012fb0330SPyun YongHyeon * Tell the upper layer(s) we support long frames/checksum offloads. 38100d12766SMarius Strobl */ 38200d12766SMarius Strobl ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 38312fb0330SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 38412fb0330SPyun YongHyeon ifp->if_hwassist |= sc->sc_csum_features; 38512fb0330SPyun YongHyeon ifp->if_capenable |= IFCAP_VLAN_MTU | IFCAP_HWCSUM; 38600d12766SMarius Strobl 38742c1b001SThomas Moestl return (0); 38842c1b001SThomas Moestl 38942c1b001SThomas Moestl /* 39042c1b001SThomas Moestl * Free any resources we've allocated during the failed attach 39142c1b001SThomas Moestl * attempt. Do this in reverse order and fall through. 39242c1b001SThomas Moestl */ 393305f2c06SThomas Moestl fail_rxd: 3942a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 39542c1b001SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 396305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 39742c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 398305f2c06SThomas Moestl fail_txd: 3992a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 40042c1b001SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 401305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 40242c1b001SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 403305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 404305f2c06SThomas Moestl fail_cmem: 40542c1b001SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 40642c1b001SThomas Moestl sc->sc_cddmamap); 407305f2c06SThomas Moestl fail_ctag: 40842c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 409305f2c06SThomas Moestl fail_ttag: 410305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 411305f2c06SThomas Moestl fail_rtag: 412305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 413305f2c06SThomas Moestl fail_ptag: 41442c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 415fc74a9f9SBrooks Davis fail_ifnet: 416fc74a9f9SBrooks Davis if_free(ifp); 41742c1b001SThomas Moestl return (error); 41842c1b001SThomas Moestl } 41942c1b001SThomas Moestl 420cbbdf236SThomas Moestl void 4212a79fd39SMarius Strobl gem_detach(struct gem_softc *sc) 422cbbdf236SThomas Moestl { 423fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 424cbbdf236SThomas Moestl int i; 425cbbdf236SThomas Moestl 426b3a1f860SMarius Strobl ether_ifdetach(ifp); 4278cfaff7dSMarius Strobl GEM_LOCK(sc); 42825bd46d0SBrooks Davis gem_stop(ifp, 1); 4298cfaff7dSMarius Strobl GEM_UNLOCK(sc); 4301f317bf9SMarius Strobl callout_drain(&sc->sc_tick_ch); 4311f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 4321f317bf9SMarius Strobl callout_drain(&sc->sc_rx_ch); 4331f317bf9SMarius Strobl #endif 434fc74a9f9SBrooks Davis if_free(ifp); 435cbbdf236SThomas Moestl device_delete_child(sc->sc_dev, sc->sc_miibus); 436cbbdf236SThomas Moestl 4372a79fd39SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 438cbbdf236SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 439cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 440cbbdf236SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 4412a79fd39SMarius Strobl for (i = 0; i < GEM_TXQUEUELEN; i++) 442cbbdf236SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 443cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 444cbbdf236SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 445ccb1212aSMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 446cbbdf236SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 447cbbdf236SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 448cbbdf236SThomas Moestl sc->sc_cddmamap); 449cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 450cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 451cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 452cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 453cbbdf236SThomas Moestl } 454cbbdf236SThomas Moestl 455cbbdf236SThomas Moestl void 4562a79fd39SMarius Strobl gem_suspend(struct gem_softc *sc) 457cbbdf236SThomas Moestl { 458fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 459cbbdf236SThomas Moestl 4608cfaff7dSMarius Strobl GEM_LOCK(sc); 461cbbdf236SThomas Moestl gem_stop(ifp, 0); 4628cfaff7dSMarius Strobl GEM_UNLOCK(sc); 463cbbdf236SThomas Moestl } 464cbbdf236SThomas Moestl 465cbbdf236SThomas Moestl void 4662a79fd39SMarius Strobl gem_resume(struct gem_softc *sc) 467cbbdf236SThomas Moestl { 468fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 469cbbdf236SThomas Moestl 4708cfaff7dSMarius Strobl GEM_LOCK(sc); 47100d12766SMarius Strobl /* 47200d12766SMarius Strobl * On resume all registers have to be initialized again like 47300d12766SMarius Strobl * after power-on. 47400d12766SMarius Strobl */ 4751ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_INITED; 476cbbdf236SThomas Moestl if (ifp->if_flags & IFF_UP) 4778cfaff7dSMarius Strobl gem_init_locked(sc); 4788cfaff7dSMarius Strobl GEM_UNLOCK(sc); 479cbbdf236SThomas Moestl } 480cbbdf236SThomas Moestl 4819ba2b298SMarius Strobl static inline void 48212fb0330SPyun YongHyeon gem_rxcksum(struct mbuf *m, uint64_t flags) 48312fb0330SPyun YongHyeon { 48412fb0330SPyun YongHyeon struct ether_header *eh; 48512fb0330SPyun YongHyeon struct ip *ip; 48612fb0330SPyun YongHyeon struct udphdr *uh; 4872a79fd39SMarius Strobl uint16_t *opts; 48812fb0330SPyun YongHyeon int32_t hlen, len, pktlen; 48912fb0330SPyun YongHyeon uint32_t temp32; 4902a79fd39SMarius Strobl uint16_t cksum; 49112fb0330SPyun YongHyeon 49212fb0330SPyun YongHyeon pktlen = m->m_pkthdr.len; 49312fb0330SPyun YongHyeon if (pktlen < sizeof(struct ether_header) + sizeof(struct ip)) 49412fb0330SPyun YongHyeon return; 49512fb0330SPyun YongHyeon eh = mtod(m, struct ether_header *); 49612fb0330SPyun YongHyeon if (eh->ether_type != htons(ETHERTYPE_IP)) 49712fb0330SPyun YongHyeon return; 49812fb0330SPyun YongHyeon ip = (struct ip *)(eh + 1); 49912fb0330SPyun YongHyeon if (ip->ip_v != IPVERSION) 50012fb0330SPyun YongHyeon return; 50112fb0330SPyun YongHyeon 50212fb0330SPyun YongHyeon hlen = ip->ip_hl << 2; 50312fb0330SPyun YongHyeon pktlen -= sizeof(struct ether_header); 50412fb0330SPyun YongHyeon if (hlen < sizeof(struct ip)) 50512fb0330SPyun YongHyeon return; 50612fb0330SPyun YongHyeon if (ntohs(ip->ip_len) < hlen) 50712fb0330SPyun YongHyeon return; 50812fb0330SPyun YongHyeon if (ntohs(ip->ip_len) != pktlen) 50912fb0330SPyun YongHyeon return; 51012fb0330SPyun YongHyeon if (ip->ip_off & htons(IP_MF | IP_OFFMASK)) 5112a79fd39SMarius Strobl return; /* Cannot handle fragmented packet. */ 51212fb0330SPyun YongHyeon 51312fb0330SPyun YongHyeon switch (ip->ip_p) { 51412fb0330SPyun YongHyeon case IPPROTO_TCP: 51512fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct tcphdr))) 51612fb0330SPyun YongHyeon return; 51712fb0330SPyun YongHyeon break; 51812fb0330SPyun YongHyeon case IPPROTO_UDP: 51912fb0330SPyun YongHyeon if (pktlen < (hlen + sizeof(struct udphdr))) 52012fb0330SPyun YongHyeon return; 52112fb0330SPyun YongHyeon uh = (struct udphdr *)((uint8_t *)ip + hlen); 52212fb0330SPyun YongHyeon if (uh->uh_sum == 0) 52312fb0330SPyun YongHyeon return; /* no checksum */ 52412fb0330SPyun YongHyeon break; 52512fb0330SPyun YongHyeon default: 52612fb0330SPyun YongHyeon return; 52712fb0330SPyun YongHyeon } 52812fb0330SPyun YongHyeon 52912fb0330SPyun YongHyeon cksum = ~(flags & GEM_RD_CHECKSUM); 53012fb0330SPyun YongHyeon /* checksum fixup for IP options */ 53112fb0330SPyun YongHyeon len = hlen - sizeof(struct ip); 53212fb0330SPyun YongHyeon if (len > 0) { 53312fb0330SPyun YongHyeon opts = (uint16_t *)(ip + 1); 53412fb0330SPyun YongHyeon for (; len > 0; len -= sizeof(uint16_t), opts++) { 53512fb0330SPyun YongHyeon temp32 = cksum - *opts; 53612fb0330SPyun YongHyeon temp32 = (temp32 >> 16) + (temp32 & 65535); 53712fb0330SPyun YongHyeon cksum = temp32 & 65535; 53812fb0330SPyun YongHyeon } 53912fb0330SPyun YongHyeon } 54012fb0330SPyun YongHyeon m->m_pkthdr.csum_flags |= CSUM_DATA_VALID; 54112fb0330SPyun YongHyeon m->m_pkthdr.csum_data = cksum; 54212fb0330SPyun YongHyeon } 54312fb0330SPyun YongHyeon 54442c1b001SThomas Moestl static void 5452a79fd39SMarius Strobl gem_cddma_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 54642c1b001SThomas Moestl { 5472a79fd39SMarius Strobl struct gem_softc *sc = xsc; 54842c1b001SThomas Moestl 54942c1b001SThomas Moestl if (error != 0) 55042c1b001SThomas Moestl return; 5512a79fd39SMarius Strobl if (nsegs != 1) 5521ed3fed7SMarius Strobl panic("%s: bad control buffer segment count", __func__); 55342c1b001SThomas Moestl sc->sc_cddma = segs[0].ds_addr; 55442c1b001SThomas Moestl } 55542c1b001SThomas Moestl 55642c1b001SThomas Moestl static void 5572a79fd39SMarius Strobl gem_tick(void *arg) 55842c1b001SThomas Moestl { 55942c1b001SThomas Moestl struct gem_softc *sc = arg; 5609ba2b298SMarius Strobl struct ifnet *ifp = sc->sc_ifp; 56178d22f42SMarius Strobl uint32_t v; 56242c1b001SThomas Moestl 5631f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 56412fb0330SPyun YongHyeon 56512fb0330SPyun YongHyeon /* 56678d22f42SMarius Strobl * Unload collision and error counters. 56712fb0330SPyun YongHyeon */ 56812fb0330SPyun YongHyeon ifp->if_collisions += 569bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_NORM_COLL_CNT) + 57078d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_FIRST_COLL_CNT); 57178d22f42SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_EXCESS_COLL_CNT) + 572bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_LATE_COLL_CNT); 57378d22f42SMarius Strobl ifp->if_collisions += v; 57478d22f42SMarius Strobl ifp->if_oerrors += v; 57578d22f42SMarius Strobl ifp->if_ierrors += 57678d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_LEN_ERR_CNT) + 57778d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_ALIGN_ERR) + 57878d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CRC_ERR_CNT) + 57978d22f42SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CODE_VIOL); 58012fb0330SPyun YongHyeon 58112fb0330SPyun YongHyeon /* 582801772ecSMarius Strobl * Then clear the hardware counters. 58312fb0330SPyun YongHyeon */ 584bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 585bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 586bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 587bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 58878d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 58978d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 59078d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 59178d22f42SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 59212fb0330SPyun YongHyeon 59342c1b001SThomas Moestl mii_tick(sc->sc_mii); 59442c1b001SThomas Moestl 5958cb37876SMarius Strobl if (gem_watchdog(sc) == EJUSTRETURN) 5968cb37876SMarius Strobl return; 5978cb37876SMarius Strobl 59842c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 59942c1b001SThomas Moestl } 60042c1b001SThomas Moestl 60142c1b001SThomas Moestl static int 602bd3d9826SMarius Strobl gem_bitwait(struct gem_softc *sc, u_int bank, bus_addr_t r, uint32_t clr, 603bd3d9826SMarius Strobl uint32_t set) 60442c1b001SThomas Moestl { 60542c1b001SThomas Moestl int i; 6062a79fd39SMarius Strobl uint32_t reg; 60742c1b001SThomas Moestl 6089ba2b298SMarius Strobl for (i = GEM_TRIES; i--; DELAY(100)) { 609bd3d9826SMarius Strobl reg = GEM_BANKN_READ_M(bank, 4, sc, r); 610e87137e1SMarius Strobl if ((reg & clr) == 0 && (reg & set) == set) 61142c1b001SThomas Moestl return (1); 61242c1b001SThomas Moestl } 61342c1b001SThomas Moestl return (0); 61442c1b001SThomas Moestl } 61542c1b001SThomas Moestl 6161ed3fed7SMarius Strobl static void 6179ba2b298SMarius Strobl gem_reset(struct gem_softc *sc) 61842c1b001SThomas Moestl { 61942c1b001SThomas Moestl 62018100346SThomas Moestl #ifdef GEM_DEBUG 62112fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 62218100346SThomas Moestl #endif 62342c1b001SThomas Moestl gem_reset_rx(sc); 62442c1b001SThomas Moestl gem_reset_tx(sc); 62542c1b001SThomas Moestl 6262a79fd39SMarius Strobl /* Do a full reset. */ 627bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 628ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 629ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 630bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 63142c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset device\n"); 63242c1b001SThomas Moestl } 63342c1b001SThomas Moestl 63442c1b001SThomas Moestl static void 6352a79fd39SMarius Strobl gem_rxdrain(struct gem_softc *sc) 63642c1b001SThomas Moestl { 63742c1b001SThomas Moestl struct gem_rxsoft *rxs; 63842c1b001SThomas Moestl int i; 63942c1b001SThomas Moestl 64042c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 64142c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 64242c1b001SThomas Moestl if (rxs->rxs_mbuf != NULL) { 643b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 644b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 645305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 64642c1b001SThomas Moestl m_freem(rxs->rxs_mbuf); 64742c1b001SThomas Moestl rxs->rxs_mbuf = NULL; 64842c1b001SThomas Moestl } 64942c1b001SThomas Moestl } 65042c1b001SThomas Moestl } 65142c1b001SThomas Moestl 65242c1b001SThomas Moestl static void 6532a79fd39SMarius Strobl gem_stop(struct ifnet *ifp, int disable) 65442c1b001SThomas Moestl { 6552a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 65642c1b001SThomas Moestl struct gem_txsoft *txs; 65742c1b001SThomas Moestl 65818100346SThomas Moestl #ifdef GEM_DEBUG 65912fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 66018100346SThomas Moestl #endif 66142c1b001SThomas Moestl 66242c1b001SThomas Moestl callout_stop(&sc->sc_tick_ch); 6631f317bf9SMarius Strobl #ifdef GEM_RINT_TIMEOUT 6641f317bf9SMarius Strobl callout_stop(&sc->sc_rx_ch); 6651f317bf9SMarius Strobl #endif 66642c1b001SThomas Moestl 6679ba2b298SMarius Strobl gem_reset_tx(sc); 6689ba2b298SMarius Strobl gem_reset_rx(sc); 66942c1b001SThomas Moestl 67042c1b001SThomas Moestl /* 67142c1b001SThomas Moestl * Release any queued transmit buffers. 67242c1b001SThomas Moestl */ 67342c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 67442c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 67542c1b001SThomas Moestl if (txs->txs_ndescs != 0) { 676b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 677b2d59f42SThomas Moestl BUS_DMASYNC_POSTWRITE); 678305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 67942c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 68042c1b001SThomas Moestl m_freem(txs->txs_mbuf); 68142c1b001SThomas Moestl txs->txs_mbuf = NULL; 68242c1b001SThomas Moestl } 68342c1b001SThomas Moestl } 68442c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 68542c1b001SThomas Moestl } 68642c1b001SThomas Moestl 68742c1b001SThomas Moestl if (disable) 68842c1b001SThomas Moestl gem_rxdrain(sc); 68942c1b001SThomas Moestl 69042c1b001SThomas Moestl /* 69142c1b001SThomas Moestl * Mark the interface down and cancel the watchdog timer. 69242c1b001SThomas Moestl */ 69313f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 6941ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 6958cb37876SMarius Strobl sc->sc_wdog_timer = 0; 69642c1b001SThomas Moestl } 69742c1b001SThomas Moestl 6981ed3fed7SMarius Strobl static int 6992a79fd39SMarius Strobl gem_reset_rx(struct gem_softc *sc) 70042c1b001SThomas Moestl { 70142c1b001SThomas Moestl 70242c1b001SThomas Moestl /* 70342c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 70442c1b001SThomas Moestl * disable DMA first. 70542c1b001SThomas Moestl */ 70642c1b001SThomas Moestl gem_disable_rx(sc); 707bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 0); 708ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_RX_CONFIG, 4, 709ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 710bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_RX_CONFIG, GEM_RX_CONFIG_RXDMA_EN, 0)) 7111ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX DMA\n"); 71242c1b001SThomas Moestl 713801772ecSMarius Strobl /* Finally, reset the ERX. */ 714bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_RX); 715ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 716ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 717bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 718bd3d9826SMarius Strobl 0)) { 71942c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 72042c1b001SThomas Moestl return (1); 72142c1b001SThomas Moestl } 72242c1b001SThomas Moestl return (0); 72342c1b001SThomas Moestl } 72442c1b001SThomas Moestl 7251ed3fed7SMarius Strobl /* 7261ed3fed7SMarius Strobl * Reset the receiver DMA engine. 7271ed3fed7SMarius Strobl * 7281ed3fed7SMarius Strobl * Intended to be used in case of GEM_INTR_RX_TAG_ERR, GEM_MAC_RX_OVERFLOW 7291ed3fed7SMarius Strobl * etc in order to reset the receiver DMA engine only and not do a full 7301ed3fed7SMarius Strobl * reset which amongst others also downs the link and clears the FIFOs. 7311ed3fed7SMarius Strobl */ 7321ed3fed7SMarius Strobl static void 7331ed3fed7SMarius Strobl gem_reset_rxdma(struct gem_softc *sc) 7341ed3fed7SMarius Strobl { 7351ed3fed7SMarius Strobl int i; 7361ed3fed7SMarius Strobl 7371ed3fed7SMarius Strobl if (gem_reset_rx(sc) != 0) 7381ed3fed7SMarius Strobl return (gem_init_locked(sc)); 7391ed3fed7SMarius Strobl for (i = 0; i < GEM_NRXDESC; i++) 7401ed3fed7SMarius Strobl if (sc->sc_rxsoft[i].rxs_mbuf != NULL) 7411ed3fed7SMarius Strobl GEM_UPDATE_RXDESC(sc, i); 7421ed3fed7SMarius Strobl sc->sc_rxptr = 0; 7439ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 7441ed3fed7SMarius Strobl 7451ed3fed7SMarius Strobl /* NOTE: we use only 32-bit DMA addresses here. */ 746bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 747bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 748bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 749bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 7501ed3fed7SMarius Strobl gem_ringsize(GEM_NRXDESC /* XXX */) | 7511ed3fed7SMarius Strobl ((ETHER_HDR_LEN + sizeof(struct ip)) << 7521ed3fed7SMarius Strobl GEM_RX_CONFIG_CXM_START_SHFT) | 7531ed3fed7SMarius Strobl (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 7549ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT)); 7559ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 756bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 7579ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 7589ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 759bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 7602a79fd39SMarius Strobl (3 * sc->sc_rxfifosize / 256) | 7612a79fd39SMarius Strobl ((sc->sc_rxfifosize / 256) << 12)); 762bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 763bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_RX_CONFIG) | GEM_RX_CONFIG_RXDMA_EN); 764bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 7651ed3fed7SMarius Strobl GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 766bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 767bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) | GEM_MAC_RX_ENABLE); 7681ed3fed7SMarius Strobl } 76942c1b001SThomas Moestl 77042c1b001SThomas Moestl static int 7712a79fd39SMarius Strobl gem_reset_tx(struct gem_softc *sc) 77242c1b001SThomas Moestl { 77342c1b001SThomas Moestl 77442c1b001SThomas Moestl /* 77542c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 77642c1b001SThomas Moestl * disable DMA first. 77742c1b001SThomas Moestl */ 77842c1b001SThomas Moestl gem_disable_tx(sc); 779bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, 0); 780ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_TX_CONFIG, 4, 781ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 782bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_TX_CONFIG, GEM_TX_CONFIG_TXDMA_EN, 0)) 7831ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX DMA\n"); 78442c1b001SThomas Moestl 785801772ecSMarius Strobl /* Finally, reset the ETX. */ 786bd3d9826SMarius Strobl GEM_BANK2_WRITE_4(sc, GEM_RESET, GEM_RESET_TX); 787ccb1212aSMarius Strobl GEM_BANK2_BARRIER(sc, GEM_RESET, 4, 788ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 789bd3d9826SMarius Strobl if (!GEM_BANK2_BITWAIT(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 790bd3d9826SMarius Strobl 0)) { 7911ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot reset transmitter\n"); 79242c1b001SThomas Moestl return (1); 79342c1b001SThomas Moestl } 79442c1b001SThomas Moestl return (0); 79542c1b001SThomas Moestl } 79642c1b001SThomas Moestl 79742c1b001SThomas Moestl static int 7982a79fd39SMarius Strobl gem_disable_rx(struct gem_softc *sc) 79942c1b001SThomas Moestl { 80042c1b001SThomas Moestl 801bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 802bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG) & ~GEM_MAC_RX_ENABLE); 803ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 804ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 805bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 806bd3d9826SMarius Strobl 0)); 80742c1b001SThomas Moestl } 80842c1b001SThomas Moestl 80942c1b001SThomas Moestl static int 8102a79fd39SMarius Strobl gem_disable_tx(struct gem_softc *sc) 81142c1b001SThomas Moestl { 81242c1b001SThomas Moestl 813bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 814bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG) & ~GEM_MAC_TX_ENABLE); 815ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 816ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 817bd3d9826SMarius Strobl return (GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 818bd3d9826SMarius Strobl 0)); 81942c1b001SThomas Moestl } 82042c1b001SThomas Moestl 82142c1b001SThomas Moestl static int 8229ba2b298SMarius Strobl gem_meminit(struct gem_softc *sc) 82342c1b001SThomas Moestl { 82442c1b001SThomas Moestl struct gem_rxsoft *rxs; 8252a79fd39SMarius Strobl int error, i; 82642c1b001SThomas Moestl 8279ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 8289ba2b298SMarius Strobl 82942c1b001SThomas Moestl /* 83042c1b001SThomas Moestl * Initialize the transmit descriptor ring. 83142c1b001SThomas Moestl */ 83242c1b001SThomas Moestl for (i = 0; i < GEM_NTXDESC; i++) { 83342c1b001SThomas Moestl sc->sc_txdescs[i].gd_flags = 0; 83442c1b001SThomas Moestl sc->sc_txdescs[i].gd_addr = 0; 83542c1b001SThomas Moestl } 836305f2c06SThomas Moestl sc->sc_txfree = GEM_MAXTXFREE; 83742c1b001SThomas Moestl sc->sc_txnext = 0; 838336cca9eSBenno Rice sc->sc_txwin = 0; 83942c1b001SThomas Moestl 84042c1b001SThomas Moestl /* 84142c1b001SThomas Moestl * Initialize the receive descriptor and receive job 84242c1b001SThomas Moestl * descriptor rings. 84342c1b001SThomas Moestl */ 84442c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 84542c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 84642c1b001SThomas Moestl if (rxs->rxs_mbuf == NULL) { 84742c1b001SThomas Moestl if ((error = gem_add_rxbuf(sc, i)) != 0) { 8482a79fd39SMarius Strobl device_printf(sc->sc_dev, 8492a79fd39SMarius Strobl "unable to allocate or map RX buffer %d, " 8502a79fd39SMarius Strobl "error = %d\n", i, error); 85142c1b001SThomas Moestl /* 8522a79fd39SMarius Strobl * XXX we should attempt to run with fewer 8532a79fd39SMarius Strobl * receive buffers instead of just failing. 85442c1b001SThomas Moestl */ 85542c1b001SThomas Moestl gem_rxdrain(sc); 85642c1b001SThomas Moestl return (1); 85742c1b001SThomas Moestl } 85842c1b001SThomas Moestl } else 85942c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 86042c1b001SThomas Moestl } 86142c1b001SThomas Moestl sc->sc_rxptr = 0; 8629ba2b298SMarius Strobl 8639ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 86442c1b001SThomas Moestl 86542c1b001SThomas Moestl return (0); 86642c1b001SThomas Moestl } 86742c1b001SThomas Moestl 8681ed3fed7SMarius Strobl static u_int 8692a79fd39SMarius Strobl gem_ringsize(u_int sz) 87042c1b001SThomas Moestl { 87142c1b001SThomas Moestl 87242c1b001SThomas Moestl switch (sz) { 87342c1b001SThomas Moestl case 32: 8741ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 87542c1b001SThomas Moestl case 64: 8761ed3fed7SMarius Strobl return (GEM_RING_SZ_64); 87742c1b001SThomas Moestl case 128: 8781ed3fed7SMarius Strobl return (GEM_RING_SZ_128); 87942c1b001SThomas Moestl case 256: 8801ed3fed7SMarius Strobl return (GEM_RING_SZ_256); 88142c1b001SThomas Moestl case 512: 8821ed3fed7SMarius Strobl return (GEM_RING_SZ_512); 88342c1b001SThomas Moestl case 1024: 8841ed3fed7SMarius Strobl return (GEM_RING_SZ_1024); 88542c1b001SThomas Moestl case 2048: 8861ed3fed7SMarius Strobl return (GEM_RING_SZ_2048); 88742c1b001SThomas Moestl case 4096: 8881ed3fed7SMarius Strobl return (GEM_RING_SZ_4096); 88942c1b001SThomas Moestl case 8192: 8901ed3fed7SMarius Strobl return (GEM_RING_SZ_8192); 89142c1b001SThomas Moestl default: 8921ed3fed7SMarius Strobl printf("%s: invalid ring size %d\n", __func__, sz); 8931ed3fed7SMarius Strobl return (GEM_RING_SZ_32); 89442c1b001SThomas Moestl } 89542c1b001SThomas Moestl } 89642c1b001SThomas Moestl 89742c1b001SThomas Moestl static void 8982a79fd39SMarius Strobl gem_init(void *xsc) 89942c1b001SThomas Moestl { 9002a79fd39SMarius Strobl struct gem_softc *sc = xsc; 9018cfaff7dSMarius Strobl 9028cfaff7dSMarius Strobl GEM_LOCK(sc); 9038cfaff7dSMarius Strobl gem_init_locked(sc); 9048cfaff7dSMarius Strobl GEM_UNLOCK(sc); 9058cfaff7dSMarius Strobl } 9068cfaff7dSMarius Strobl 9078cfaff7dSMarius Strobl /* 9088cfaff7dSMarius Strobl * Initialization of interface; set up initialization block 9098cfaff7dSMarius Strobl * and transmit/receive descriptor rings. 9108cfaff7dSMarius Strobl */ 9118cfaff7dSMarius Strobl static void 9122a79fd39SMarius Strobl gem_init_locked(struct gem_softc *sc) 9138cfaff7dSMarius Strobl { 914fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 9152a79fd39SMarius Strobl uint32_t v; 91642c1b001SThomas Moestl 9178cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 91842c1b001SThomas Moestl 91918100346SThomas Moestl #ifdef GEM_DEBUG 92012fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: calling stop", device_get_name(sc->sc_dev), 92112fb0330SPyun YongHyeon __func__); 92218100346SThomas Moestl #endif 92342c1b001SThomas Moestl /* 92442c1b001SThomas Moestl * Initialization sequence. The numbered steps below correspond 92542c1b001SThomas Moestl * to the sequence outlined in section 6.3.5.1 in the Ethernet 92642c1b001SThomas Moestl * Channel Engine manual (part of the PCIO manual). 92742c1b001SThomas Moestl * See also the STP2002-STQ document from Sun Microsystems. 92842c1b001SThomas Moestl */ 92942c1b001SThomas Moestl 9302a79fd39SMarius Strobl /* step 1 & 2. Reset the Ethernet Channel. */ 931ccb1212aSMarius Strobl gem_stop(ifp, 0); 93242c1b001SThomas Moestl gem_reset(sc); 93318100346SThomas Moestl #ifdef GEM_DEBUG 93412fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s: restarting", device_get_name(sc->sc_dev), 93512fb0330SPyun YongHyeon __func__); 93618100346SThomas Moestl #endif 93742c1b001SThomas Moestl 938*65f2c0ffSMarius Strobl if ((sc->sc_flags & GEM_SERDES) == 0) 9392a79fd39SMarius Strobl /* Re-initialize the MIF. */ 94042c1b001SThomas Moestl gem_mifinit(sc); 94142c1b001SThomas Moestl 9422a79fd39SMarius Strobl /* step 3. Setup data structures in host memory. */ 9431ed3fed7SMarius Strobl if (gem_meminit(sc) != 0) 9441ed3fed7SMarius Strobl return; 94542c1b001SThomas Moestl 94642c1b001SThomas Moestl /* step 4. TX MAC registers & counters */ 94742c1b001SThomas Moestl gem_init_regs(sc); 94842c1b001SThomas Moestl 94942c1b001SThomas Moestl /* step 5. RX MAC registers & counters */ 95042c1b001SThomas Moestl gem_setladrf(sc); 95142c1b001SThomas Moestl 9522a79fd39SMarius Strobl /* step 6 & 7. Program Descriptor Ring Base Addresses. */ 95342c1b001SThomas Moestl /* NOTE: we use only 32-bit DMA addresses here. */ 954bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_HI, 0); 955bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 95642c1b001SThomas Moestl 957bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_HI, 0); 958bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 95918100346SThomas Moestl #ifdef GEM_DEBUG 9602a79fd39SMarius Strobl CTR3(KTR_GEM, "loading RX ring %lx, TX ring %lx, cddma %lx", 96142c1b001SThomas Moestl GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 96218100346SThomas Moestl #endif 96342c1b001SThomas Moestl 96442c1b001SThomas Moestl /* step 8. Global Configuration & Interrupt Mask */ 9659ba2b298SMarius Strobl 9669ba2b298SMarius Strobl /* 9679ba2b298SMarius Strobl * Set the internal arbitration to "infinite" bursts of the 9689ba2b298SMarius Strobl * maximum length of 31 * 64 bytes so DMA transfers aren't 9699ba2b298SMarius Strobl * split up in cache line size chunks. This greatly improves 9709ba2b298SMarius Strobl * RX performance. 9719ba2b298SMarius Strobl * Enable silicon bug workarounds for the Apple variants. 9729ba2b298SMarius Strobl */ 9739ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_CONFIG, 9749ba2b298SMarius Strobl GEM_CONFIG_TXDMA_LIMIT | GEM_CONFIG_RXDMA_LIMIT | 9759ba2b298SMarius Strobl ((sc->sc_flags & GEM_PCI) != 0 ? GEM_CONFIG_BURST_INF : 9769ba2b298SMarius Strobl GEM_CONFIG_BURST_64) | (GEM_IS_APPLE(sc) ? 9779ba2b298SMarius Strobl GEM_CONFIG_RONPAULBIT | GEM_CONFIG_BUG2FIX : 0)); 9789ba2b298SMarius Strobl 979bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_INTMASK, 9801ed3fed7SMarius Strobl ~(GEM_INTR_TX_INTME | GEM_INTR_TX_EMPTY | GEM_INTR_RX_DONE | 9811ed3fed7SMarius Strobl GEM_INTR_RX_NOBUF | GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | 9821ed3fed7SMarius Strobl GEM_INTR_BERR 9831ed3fed7SMarius Strobl #ifdef GEM_DEBUG 9841ed3fed7SMarius Strobl | GEM_INTR_PCS | GEM_INTR_MIF 9851ed3fed7SMarius Strobl #endif 9861ed3fed7SMarius Strobl )); 987bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_MASK, 988336cca9eSBenno Rice GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT); 989bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_MASK, 9909ba2b298SMarius Strobl GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 9919ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP); 9921ed3fed7SMarius Strobl #ifdef GEM_DEBUG 993bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9941ed3fed7SMarius Strobl ~(GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME)); 9951ed3fed7SMarius Strobl #else 996bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_MASK, 9971ed3fed7SMarius Strobl GEM_MAC_PAUSED | GEM_MAC_PAUSE | GEM_MAC_RESUME); 9981ed3fed7SMarius Strobl #endif 99942c1b001SThomas Moestl 10002a79fd39SMarius Strobl /* step 9. ETX Configuration: use mostly default values. */ 100142c1b001SThomas Moestl 10022a79fd39SMarius Strobl /* Enable DMA. */ 10039ba2b298SMarius Strobl v = gem_ringsize(GEM_NTXDESC); 10049ba2b298SMarius Strobl /* Set TX FIFO threshold and enable DMA. */ 1005ccb1212aSMarius Strobl v |= ((sc->sc_variant == GEM_SUN_ERI ? 0x100 : 0x4ff) << 10) & 1006ccb1212aSMarius Strobl GEM_TX_CONFIG_TXFIFO_TH; 1007ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_CONFIG, v | GEM_TX_CONFIG_TXDMA_EN); 100842c1b001SThomas Moestl 100942c1b001SThomas Moestl /* step 10. ERX Configuration */ 101042c1b001SThomas Moestl 10111ed3fed7SMarius Strobl /* Encode Receive Descriptor ring size. */ 101242c1b001SThomas Moestl v = gem_ringsize(GEM_NRXDESC /* XXX */); 10132a79fd39SMarius Strobl /* RX TCP/UDP checksum offset */ 101412fb0330SPyun YongHyeon v |= ((ETHER_HDR_LEN + sizeof(struct ip)) << 101512fb0330SPyun YongHyeon GEM_RX_CONFIG_CXM_START_SHFT); 10169ba2b298SMarius Strobl /* Set RX FIFO threshold, set first byte offset and enable DMA. */ 1017bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_CONFIG, 101842c1b001SThomas Moestl v | (GEM_THRSH_1024 << GEM_RX_CONFIG_FIFO_THRS_SHIFT) | 10199ba2b298SMarius Strobl (ETHER_ALIGN << GEM_RX_CONFIG_FBOFF_SHFT) | 10209ba2b298SMarius Strobl GEM_RX_CONFIG_RXDMA_EN); 10211ed3fed7SMarius Strobl 10229ba2b298SMarius Strobl /* Adjust for the SBus clock probably isn't worth the fuzz. */ 1023bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_BLANKING, 10249ba2b298SMarius Strobl ((6 * (sc->sc_flags & GEM_PCI66) != 0 ? 2 : 1) << 10259ba2b298SMarius Strobl GEM_RX_BLANKING_TIME_SHIFT) | 6); 10261ed3fed7SMarius Strobl 102742c1b001SThomas Moestl /* 1028336cca9eSBenno Rice * The following value is for an OFF Threshold of about 3/4 full 1029336cca9eSBenno Rice * and an ON Threshold of 1/4 full. 103042c1b001SThomas Moestl */ 1031bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_PAUSE_THRESH, 1032336cca9eSBenno Rice (3 * sc->sc_rxfifosize / 256) | 1033336cca9eSBenno Rice ((sc->sc_rxfifosize / 256) << 12)); 103442c1b001SThomas Moestl 10352a79fd39SMarius Strobl /* step 11. Configure Media. */ 103642c1b001SThomas Moestl 103742c1b001SThomas Moestl /* step 12. RX_MAC Configuration Register */ 1038bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 1039ccb1212aSMarius Strobl v |= GEM_MAC_RX_ENABLE | GEM_MAC_RX_STRIP_CRC; 1040bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 1041ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 1042ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1043bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 1044ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure RX MAC\n"); 1045bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 104642c1b001SThomas Moestl 1047ccb1212aSMarius Strobl /* step 13. TX_MAC Configuration Register */ 1048ccb1212aSMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG); 1049ccb1212aSMarius Strobl v |= GEM_MAC_TX_ENABLE; 1050ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 1051ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 1052ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1053ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 1054ccb1212aSMarius Strobl device_printf(sc->sc_dev, "cannot configure TX MAC\n"); 1055ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, v); 1056ccb1212aSMarius Strobl 10572a79fd39SMarius Strobl /* step 14. Issue Transmit Pending command. */ 105842c1b001SThomas Moestl 10592a79fd39SMarius Strobl /* step 15. Give the reciever a swift kick. */ 1060bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, GEM_NRXDESC - 4); 106142c1b001SThomas Moestl 106213f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 106313f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 10641ed3fed7SMarius Strobl 10651ed3fed7SMarius Strobl mii_mediachg(sc->sc_mii); 10661ed3fed7SMarius Strobl 10671ed3fed7SMarius Strobl /* Start the one second timer. */ 10681ed3fed7SMarius Strobl sc->sc_wdog_timer = 0; 10691ed3fed7SMarius Strobl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 107042c1b001SThomas Moestl } 107142c1b001SThomas Moestl 107212fb0330SPyun YongHyeon static int 10732a79fd39SMarius Strobl gem_load_txmbuf(struct gem_softc *sc, struct mbuf **m_head) 107412fb0330SPyun YongHyeon { 107512fb0330SPyun YongHyeon bus_dma_segment_t txsegs[GEM_NTXSEGS]; 10762a79fd39SMarius Strobl struct gem_txsoft *txs; 1077ccb1212aSMarius Strobl struct ip *ip; 107812fb0330SPyun YongHyeon struct mbuf *m; 10792a79fd39SMarius Strobl uint64_t cflags, flags; 1080ccb1212aSMarius Strobl int error, nexttx, nsegs, offset, seg; 108142c1b001SThomas Moestl 10829ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 10839ba2b298SMarius Strobl 108442c1b001SThomas Moestl /* Get a work queue entry. */ 108542c1b001SThomas Moestl if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 1086305f2c06SThomas Moestl /* Ran out of descriptors. */ 108712fb0330SPyun YongHyeon return (ENOBUFS); 1088305f2c06SThomas Moestl } 1089ccb1212aSMarius Strobl 1090ccb1212aSMarius Strobl cflags = 0; 1091ccb1212aSMarius Strobl if (((*m_head)->m_pkthdr.csum_flags & sc->sc_csum_features) != 0) { 1092ccb1212aSMarius Strobl if (M_WRITABLE(*m_head) == 0) { 1093ccb1212aSMarius Strobl m = m_dup(*m_head, M_DONTWAIT); 1094ccb1212aSMarius Strobl m_freem(*m_head); 1095ccb1212aSMarius Strobl *m_head = m; 1096ccb1212aSMarius Strobl if (m == NULL) 1097ccb1212aSMarius Strobl return (ENOBUFS); 1098ccb1212aSMarius Strobl } 1099ccb1212aSMarius Strobl offset = sizeof(struct ether_header); 1100ccb1212aSMarius Strobl m = m_pullup(*m_head, offset + sizeof(struct ip)); 1101ccb1212aSMarius Strobl if (m == NULL) { 1102ccb1212aSMarius Strobl *m_head = NULL; 1103ccb1212aSMarius Strobl return (ENOBUFS); 1104ccb1212aSMarius Strobl } 1105ccb1212aSMarius Strobl ip = (struct ip *)(mtod(m, caddr_t) + offset); 1106ccb1212aSMarius Strobl offset += (ip->ip_hl << 2); 1107ccb1212aSMarius Strobl cflags = offset << GEM_TD_CXSUM_STARTSHFT | 1108ccb1212aSMarius Strobl ((offset + m->m_pkthdr.csum_data) << 1109ccb1212aSMarius Strobl GEM_TD_CXSUM_STUFFSHFT) | GEM_TD_CXSUM_ENABLE; 1110ccb1212aSMarius Strobl *m_head = m; 1111ccb1212aSMarius Strobl } 1112ccb1212aSMarius Strobl 111312fb0330SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, txs->txs_dmamap, 111412fb0330SPyun YongHyeon *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 111512fb0330SPyun YongHyeon if (error == EFBIG) { 1116304a4c6fSJohn Baldwin m = m_collapse(*m_head, M_DONTWAIT, GEM_NTXSEGS); 111712fb0330SPyun YongHyeon if (m == NULL) { 111812fb0330SPyun YongHyeon m_freem(*m_head); 111912fb0330SPyun YongHyeon *m_head = NULL; 112012fb0330SPyun YongHyeon return (ENOBUFS); 112112fb0330SPyun YongHyeon } 112212fb0330SPyun YongHyeon *m_head = m; 11232a79fd39SMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_tdmatag, 11242a79fd39SMarius Strobl txs->txs_dmamap, *m_head, txsegs, &nsegs, 11252a79fd39SMarius Strobl BUS_DMA_NOWAIT); 112612fb0330SPyun YongHyeon if (error != 0) { 112712fb0330SPyun YongHyeon m_freem(*m_head); 112812fb0330SPyun YongHyeon *m_head = NULL; 112912fb0330SPyun YongHyeon return (error); 113012fb0330SPyun YongHyeon } 113112fb0330SPyun YongHyeon } else if (error != 0) 113212fb0330SPyun YongHyeon return (error); 1133801772ecSMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1134801772ecSMarius Strobl KASSERT(nsegs <= GEM_NTXSEGS, 1135801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 113612fb0330SPyun YongHyeon if (nsegs == 0) { 113712fb0330SPyun YongHyeon m_freem(*m_head); 113812fb0330SPyun YongHyeon *m_head = NULL; 113912fb0330SPyun YongHyeon return (EIO); 114012fb0330SPyun YongHyeon } 114112fb0330SPyun YongHyeon 114212fb0330SPyun YongHyeon /* 114312fb0330SPyun YongHyeon * Ensure we have enough descriptors free to describe 114412fb0330SPyun YongHyeon * the packet. Note, we always reserve one descriptor 11452a79fd39SMarius Strobl * at the end of the ring as a termination point, in 11462a79fd39SMarius Strobl * order to prevent wrap-around. 114712fb0330SPyun YongHyeon */ 114812fb0330SPyun YongHyeon if (nsegs > sc->sc_txfree - 1) { 114912fb0330SPyun YongHyeon txs->txs_ndescs = 0; 115012fb0330SPyun YongHyeon bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 115112fb0330SPyun YongHyeon return (ENOBUFS); 115212fb0330SPyun YongHyeon } 115312fb0330SPyun YongHyeon 115412fb0330SPyun YongHyeon txs->txs_ndescs = nsegs; 1155305f2c06SThomas Moestl txs->txs_firstdesc = sc->sc_txnext; 115612fb0330SPyun YongHyeon nexttx = txs->txs_firstdesc; 115712fb0330SPyun YongHyeon for (seg = 0; seg < nsegs; seg++, nexttx = GEM_NEXTTX(nexttx)) { 115812fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11592a79fd39SMarius Strobl CTR6(KTR_GEM, 11602a79fd39SMarius Strobl "%s: mapping seg %d (txd %d), len %lx, addr %#lx (%#lx)", 11612a79fd39SMarius Strobl __func__, seg, nexttx, txsegs[seg].ds_len, 11622a79fd39SMarius Strobl txsegs[seg].ds_addr, 116312fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr)); 116412fb0330SPyun YongHyeon #endif 116512fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_addr = 116612fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, txsegs[seg].ds_addr); 116712fb0330SPyun YongHyeon KASSERT(txsegs[seg].ds_len < GEM_TD_BUFSIZE, 116812fb0330SPyun YongHyeon ("%s: segment size too large!", __func__)); 116912fb0330SPyun YongHyeon flags = txsegs[seg].ds_len & GEM_TD_BUFSIZE; 117012fb0330SPyun YongHyeon sc->sc_txdescs[nexttx].gd_flags = 117112fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, flags | cflags); 117212fb0330SPyun YongHyeon txs->txs_lastdesc = nexttx; 117342c1b001SThomas Moestl } 1174305f2c06SThomas Moestl 11752a79fd39SMarius Strobl /* Set EOP on the last descriptor. */ 117612fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11772a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: end of packet at segment %d, TX %d", 11782a79fd39SMarius Strobl __func__, seg, nexttx); 117912fb0330SPyun YongHyeon #endif 118012fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_lastdesc].gd_flags |= 118112fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_END_OF_PACKET); 118212fb0330SPyun YongHyeon 11832a79fd39SMarius Strobl /* Lastly set SOP on the first descriptor. */ 118412fb0330SPyun YongHyeon #ifdef GEM_DEBUG 11852a79fd39SMarius Strobl CTR3(KTR_GEM, "%s: start of packet at segment %d, TX %d", 11862a79fd39SMarius Strobl __func__, seg, nexttx); 118712fb0330SPyun YongHyeon #endif 118812fb0330SPyun YongHyeon if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 118912fb0330SPyun YongHyeon sc->sc_txwin = 0; 119012fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 119112fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_INTERRUPT_ME | 119212fb0330SPyun YongHyeon GEM_TD_START_OF_PACKET); 119312fb0330SPyun YongHyeon } else 119412fb0330SPyun YongHyeon sc->sc_txdescs[txs->txs_firstdesc].gd_flags |= 119512fb0330SPyun YongHyeon GEM_DMA_WRITE(sc, GEM_TD_START_OF_PACKET); 119612fb0330SPyun YongHyeon 119742c1b001SThomas Moestl /* Sync the DMA map. */ 11982a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 11992a79fd39SMarius Strobl BUS_DMASYNC_PREWRITE); 1200305f2c06SThomas Moestl 120118100346SThomas Moestl #ifdef GEM_DEBUG 120212fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: setting firstdesc=%d, lastdesc=%d, ndescs=%d", 12032a79fd39SMarius Strobl __func__, txs->txs_firstdesc, txs->txs_lastdesc, 12042a79fd39SMarius Strobl txs->txs_ndescs); 120518100346SThomas Moestl #endif 120642c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 1207305f2c06SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 120812fb0330SPyun YongHyeon txs->txs_mbuf = *m_head; 1209305f2c06SThomas Moestl 1210305f2c06SThomas Moestl sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc); 1211305f2c06SThomas Moestl sc->sc_txfree -= txs->txs_ndescs; 121242c1b001SThomas Moestl 121312fb0330SPyun YongHyeon return (0); 121442c1b001SThomas Moestl } 121542c1b001SThomas Moestl 121642c1b001SThomas Moestl static void 12172a79fd39SMarius Strobl gem_init_regs(struct gem_softc *sc) 121842c1b001SThomas Moestl { 12194a0d6638SRuslan Ermilov const u_char *laddr = IF_LLADDR(sc->sc_ifp); 122042c1b001SThomas Moestl 12219ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 12229ba2b298SMarius Strobl 12232a79fd39SMarius Strobl /* These registers are not cleared on reset. */ 12241ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_INITED) == 0) { 12252a79fd39SMarius Strobl /* magic values */ 1226bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG0, 0); 1227bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG1, 8); 1228bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_IPG2, 4); 122942c1b001SThomas Moestl 12309ba2b298SMarius Strobl /* min frame length */ 1231bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 12329ba2b298SMarius Strobl /* max frame length and max burst size */ 1233bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_MAC_MAX_FRAME, 12341ed3fed7SMarius Strobl (ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN) | (0x2000 << 16)); 1235336cca9eSBenno Rice 12369ba2b298SMarius Strobl /* more magic values */ 1237bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PREAMBLE_LEN, 0x7); 1238bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_JAM_SIZE, 0x4); 1239bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ATTEMPT_LIMIT, 0x10); 1240bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_TYPE, 0x8088); 12419ba2b298SMarius Strobl 12429ba2b298SMarius Strobl /* random number seed */ 1243bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RANDOM_SEED, 1244336cca9eSBenno Rice ((laddr[5] << 8) | laddr[4]) & 0x3ff); 1245336cca9eSBenno Rice 12462a79fd39SMarius Strobl /* secondary MAC address: 0:0:0:0:0:0 */ 1247bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR3, 0); 1248bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR4, 0); 1249bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR5, 0); 1250336cca9eSBenno Rice 12512a79fd39SMarius Strobl /* MAC control address: 01:80:c2:00:00:01 */ 1252bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR6, 0x0001); 1253bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR7, 0xc200); 1254bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR8, 0x0180); 125542c1b001SThomas Moestl 12562a79fd39SMarius Strobl /* MAC filter address: 0:0:0:0:0:0 */ 1257bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER0, 0); 1258bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER1, 0); 1259bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR_FILTER2, 0); 1260bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK1_2, 0); 1261bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADR_FLT_MASK0, 0); 126242c1b001SThomas Moestl 12631ed3fed7SMarius Strobl sc->sc_flags |= GEM_INITED; 126442c1b001SThomas Moestl } 126542c1b001SThomas Moestl 12662a79fd39SMarius Strobl /* Counters need to be zeroed. */ 1267bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_NORM_COLL_CNT, 0); 1268bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_FIRST_COLL_CNT, 0); 1269bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_EXCESS_COLL_CNT, 0); 1270bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_LATE_COLL_CNT, 0); 1271bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_DEFER_TMR_CNT, 0); 1272bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_PEAK_ATTEMPTS, 0); 1273bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_FRAME_COUNT, 0); 1274bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_LEN_ERR_CNT, 0); 1275bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_ALIGN_ERR, 0); 1276bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CRC_ERR_CNT, 0); 1277bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CODE_VIOL, 0); 127842c1b001SThomas Moestl 12791ed3fed7SMarius Strobl /* Set XOFF PAUSE time. */ 1280bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 12811ed3fed7SMarius Strobl 12822a79fd39SMarius Strobl /* Set the station address. */ 1283bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR0, (laddr[4] << 8) | laddr[5]); 1284bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR1, (laddr[2] << 8) | laddr[3]); 1285bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_ADDR2, (laddr[0] << 8) | laddr[1]); 1286336cca9eSBenno Rice 12871ed3fed7SMarius Strobl /* Enable MII outputs. */ 1288bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, GEM_MAC_XIF_TX_MII_ENA); 128942c1b001SThomas Moestl } 129042c1b001SThomas Moestl 129142c1b001SThomas Moestl static void 12922a79fd39SMarius Strobl gem_start(struct ifnet *ifp) 129342c1b001SThomas Moestl { 12942a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 12958cfaff7dSMarius Strobl 12968cfaff7dSMarius Strobl GEM_LOCK(sc); 12978cfaff7dSMarius Strobl gem_start_locked(ifp); 12988cfaff7dSMarius Strobl GEM_UNLOCK(sc); 12998cfaff7dSMarius Strobl } 13008cfaff7dSMarius Strobl 13019ba2b298SMarius Strobl static inline void 13029ba2b298SMarius Strobl gem_txkick(struct gem_softc *sc) 13039ba2b298SMarius Strobl { 13049ba2b298SMarius Strobl 13059ba2b298SMarius Strobl /* 13069ba2b298SMarius Strobl * Update the TX kick register. This register has to point to the 13079ba2b298SMarius Strobl * descriptor after the last valid one and for optimum performance 13089ba2b298SMarius Strobl * should be incremented in multiples of 4 (the DMA engine fetches/ 13099ba2b298SMarius Strobl * updates descriptors in batches of 4). 13109ba2b298SMarius Strobl */ 13119ba2b298SMarius Strobl #ifdef GEM_DEBUG 13129ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: %s: kicking TX %d", 13139ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txnext); 13149ba2b298SMarius Strobl #endif 13159ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 13169ba2b298SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_TX_KICK, sc->sc_txnext); 13179ba2b298SMarius Strobl } 13189ba2b298SMarius Strobl 13198cfaff7dSMarius Strobl static void 13202a79fd39SMarius Strobl gem_start_locked(struct ifnet *ifp) 13218cfaff7dSMarius Strobl { 13222a79fd39SMarius Strobl struct gem_softc *sc = ifp->if_softc; 132312fb0330SPyun YongHyeon struct mbuf *m; 13249ba2b298SMarius Strobl int kicked, ntx; 13259ba2b298SMarius Strobl 13269ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 132742c1b001SThomas Moestl 132813f4c340SRobert Watson if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 13291ed3fed7SMarius Strobl IFF_DRV_RUNNING || (sc->sc_flags & GEM_LINK) == 0) 133042c1b001SThomas Moestl return; 133142c1b001SThomas Moestl 133218100346SThomas Moestl #ifdef GEM_DEBUG 133312fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: txfree %d, txnext %d", 13341ed3fed7SMarius Strobl device_get_name(sc->sc_dev), __func__, sc->sc_txfree, 13351ed3fed7SMarius Strobl sc->sc_txnext); 133618100346SThomas Moestl #endif 13372a79fd39SMarius Strobl ntx = 0; 13389ba2b298SMarius Strobl kicked = 0; 133912fb0330SPyun YongHyeon for (; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && sc->sc_txfree > 1;) { 134012fb0330SPyun YongHyeon IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 134112fb0330SPyun YongHyeon if (m == NULL) 134242c1b001SThomas Moestl break; 13431ed3fed7SMarius Strobl if (gem_load_txmbuf(sc, &m) != 0) { 134412fb0330SPyun YongHyeon if (m == NULL) 134512fb0330SPyun YongHyeon break; 134612fb0330SPyun YongHyeon ifp->if_drv_flags |= IFF_DRV_OACTIVE; 134712fb0330SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m); 134842c1b001SThomas Moestl break; 134942c1b001SThomas Moestl } 13509ba2b298SMarius Strobl if ((sc->sc_txnext % 4) == 0) { 13519ba2b298SMarius Strobl gem_txkick(sc); 13529ba2b298SMarius Strobl kicked = 1; 13539ba2b298SMarius Strobl } else 13549ba2b298SMarius Strobl kicked = 0; 135518100346SThomas Moestl ntx++; 135612fb0330SPyun YongHyeon BPF_MTAP(ifp, m); 1357305f2c06SThomas Moestl } 1358305f2c06SThomas Moestl 1359305f2c06SThomas Moestl if (ntx > 0) { 13609ba2b298SMarius Strobl if (kicked == 0) 13619ba2b298SMarius Strobl gem_txkick(sc); 136218100346SThomas Moestl #ifdef GEM_DEBUG 1363305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d", 13641ed3fed7SMarius Strobl device_get_name(sc->sc_dev), sc->sc_txnext); 136518100346SThomas Moestl #endif 1366305f2c06SThomas Moestl 136742c1b001SThomas Moestl /* Set a watchdog timer in case the chip flakes out. */ 13688cb37876SMarius Strobl sc->sc_wdog_timer = 5; 136918100346SThomas Moestl #ifdef GEM_DEBUG 137012fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 13712a79fd39SMarius Strobl device_get_name(sc->sc_dev), __func__, 13722a79fd39SMarius Strobl sc->sc_wdog_timer); 137318100346SThomas Moestl #endif 137442c1b001SThomas Moestl } 137542c1b001SThomas Moestl } 137642c1b001SThomas Moestl 137742c1b001SThomas Moestl static void 13782a79fd39SMarius Strobl gem_tint(struct gem_softc *sc) 137942c1b001SThomas Moestl { 1380fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 138142c1b001SThomas Moestl struct gem_txsoft *txs; 13829ba2b298SMarius Strobl int progress; 13839ba2b298SMarius Strobl uint32_t txlast; 138418100346SThomas Moestl #ifdef GEM_DEBUG 13852a79fd39SMarius Strobl int i; 13862a79fd39SMarius Strobl 13879ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 13889ba2b298SMarius Strobl 138912fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 139018100346SThomas Moestl #endif 139142c1b001SThomas Moestl 139242c1b001SThomas Moestl /* 13932a79fd39SMarius Strobl * Go through our TX list and free mbufs for those 139442c1b001SThomas Moestl * frames that have been transmitted. 139542c1b001SThomas Moestl */ 13962a79fd39SMarius Strobl progress = 0; 1397b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 139842c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 139942c1b001SThomas Moestl #ifdef GEM_DEBUG 14002a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 140142c1b001SThomas Moestl printf(" txsoft %p transmit chain:\n", txs); 140242c1b001SThomas Moestl for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 140342c1b001SThomas Moestl printf("descriptor %d: ", i); 14042a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 14052a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 14062a79fd39SMarius Strobl sc->sc_txdescs[i].gd_flags)); 14072a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 14082a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 14092a79fd39SMarius Strobl sc->sc_txdescs[i].gd_addr)); 141042c1b001SThomas Moestl if (i == txs->txs_lastdesc) 141142c1b001SThomas Moestl break; 141242c1b001SThomas Moestl } 141342c1b001SThomas Moestl } 141442c1b001SThomas Moestl #endif 141542c1b001SThomas Moestl 141642c1b001SThomas Moestl /* 14171ed3fed7SMarius Strobl * In theory, we could harvest some descriptors before 141842c1b001SThomas Moestl * the ring is empty, but that's a bit complicated. 141942c1b001SThomas Moestl * 142042c1b001SThomas Moestl * GEM_TX_COMPLETION points to the last descriptor 142142c1b001SThomas Moestl * processed + 1. 142242c1b001SThomas Moestl */ 1423bd3d9826SMarius Strobl txlast = GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION); 142418100346SThomas Moestl #ifdef GEM_DEBUG 142512fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: txs->txs_firstdesc = %d, " 142642c1b001SThomas Moestl "txs->txs_lastdesc = %d, txlast = %d", 142712fb0330SPyun YongHyeon __func__, txs->txs_firstdesc, txs->txs_lastdesc, txlast); 142818100346SThomas Moestl #endif 142942c1b001SThomas Moestl if (txs->txs_firstdesc <= txs->txs_lastdesc) { 143042c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) && 143142c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 143242c1b001SThomas Moestl break; 143342c1b001SThomas Moestl } else { 14342a79fd39SMarius Strobl /* Ick -- this command wraps. */ 143542c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) || 143642c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 143742c1b001SThomas Moestl break; 143842c1b001SThomas Moestl } 143942c1b001SThomas Moestl 144018100346SThomas Moestl #ifdef GEM_DEBUG 14412a79fd39SMarius Strobl CTR1(KTR_GEM, "%s: releasing a descriptor", __func__); 144218100346SThomas Moestl #endif 144342c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 144442c1b001SThomas Moestl 144542c1b001SThomas Moestl sc->sc_txfree += txs->txs_ndescs; 144642c1b001SThomas Moestl 1447305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 144842c1b001SThomas Moestl BUS_DMASYNC_POSTWRITE); 1449305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 145042c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 145142c1b001SThomas Moestl m_freem(txs->txs_mbuf); 145242c1b001SThomas Moestl txs->txs_mbuf = NULL; 145342c1b001SThomas Moestl } 145442c1b001SThomas Moestl 145542c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 145642c1b001SThomas Moestl 145742c1b001SThomas Moestl ifp->if_opackets++; 1458336cca9eSBenno Rice progress = 1; 145942c1b001SThomas Moestl } 146042c1b001SThomas Moestl 146118100346SThomas Moestl #ifdef GEM_DEBUG 14622a79fd39SMarius Strobl CTR4(KTR_GEM, "%s: GEM_TX_STATE_MACHINE %x GEM_TX_DATA_PTR %llx " 146342c1b001SThomas Moestl "GEM_TX_COMPLETION %x", 1464bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_STATE_MACHINE), 1465bd3d9826SMarius Strobl ((long long)GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_HI) << 32) | 1466bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_DATA_PTR_LO), 1467bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_TX_COMPLETION)); 146818100346SThomas Moestl #endif 146942c1b001SThomas Moestl 1470336cca9eSBenno Rice if (progress) { 1471336cca9eSBenno Rice if (sc->sc_txfree == GEM_NTXDESC - 1) 1472336cca9eSBenno Rice sc->sc_txwin = 0; 147342c1b001SThomas Moestl 14742a79fd39SMarius Strobl /* 14752a79fd39SMarius Strobl * We freed some descriptors, so reset IFF_DRV_OACTIVE 14762a79fd39SMarius Strobl * and restart. 14772a79fd39SMarius Strobl */ 147813f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 14799ba2b298SMarius Strobl if (STAILQ_EMPTY(&sc->sc_txdirtyq)) 14809ba2b298SMarius Strobl sc->sc_wdog_timer = 0; 148112fb0330SPyun YongHyeon gem_start_locked(ifp); 1482336cca9eSBenno Rice } 148342c1b001SThomas Moestl 148418100346SThomas Moestl #ifdef GEM_DEBUG 148512fb0330SPyun YongHyeon CTR3(KTR_GEM, "%s: %s: watchdog %d", 148612fb0330SPyun YongHyeon device_get_name(sc->sc_dev), __func__, sc->sc_wdog_timer); 148718100346SThomas Moestl #endif 148842c1b001SThomas Moestl } 148942c1b001SThomas Moestl 1490c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 14910d80b9bdSThomas Moestl static void 14922a79fd39SMarius Strobl gem_rint_timeout(void *arg) 14930d80b9bdSThomas Moestl { 14942a79fd39SMarius Strobl struct gem_softc *sc = arg; 14950d80b9bdSThomas Moestl 14961f317bf9SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 14979ba2b298SMarius Strobl 14988cfaff7dSMarius Strobl gem_rint(sc); 14990d80b9bdSThomas Moestl } 150011e3f060SJake Burkholder #endif 15010d80b9bdSThomas Moestl 150242c1b001SThomas Moestl static void 15032a79fd39SMarius Strobl gem_rint(struct gem_softc *sc) 150442c1b001SThomas Moestl { 1505fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 150642c1b001SThomas Moestl struct mbuf *m; 15072a79fd39SMarius Strobl uint64_t rxstat; 15082a79fd39SMarius Strobl uint32_t rxcomp; 150942c1b001SThomas Moestl 15109ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 15119ba2b298SMarius Strobl 1512c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 15130d80b9bdSThomas Moestl callout_stop(&sc->sc_rx_ch); 1514c3d5598aSMarius Strobl #endif 151518100346SThomas Moestl #ifdef GEM_DEBUG 151612fb0330SPyun YongHyeon CTR2(KTR_GEM, "%s: %s", device_get_name(sc->sc_dev), __func__); 151718100346SThomas Moestl #endif 1518336cca9eSBenno Rice 1519336cca9eSBenno Rice /* 1520336cca9eSBenno Rice * Read the completion register once. This limits 1521336cca9eSBenno Rice * how long the following loop can execute. 1522336cca9eSBenno Rice */ 1523bd3d9826SMarius Strobl rxcomp = GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION); 152418100346SThomas Moestl #ifdef GEM_DEBUG 15259ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: sc->sc_rxptr %d, complete %d", 152612fb0330SPyun YongHyeon __func__, sc->sc_rxptr, rxcomp); 152718100346SThomas Moestl #endif 15289ba2b298SMarius Strobl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 15291ed3fed7SMarius Strobl for (; sc->sc_rxptr != rxcomp;) { 15301ed3fed7SMarius Strobl m = sc->sc_rxsoft[sc->sc_rxptr].rxs_mbuf; 15311ed3fed7SMarius Strobl rxstat = GEM_DMA_READ(sc, 15321ed3fed7SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags); 153342c1b001SThomas Moestl 153442c1b001SThomas Moestl if (rxstat & GEM_RD_OWN) { 1535c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 153642c1b001SThomas Moestl /* 15370d80b9bdSThomas Moestl * The descriptor is still marked as owned, although 15380d80b9bdSThomas Moestl * it is supposed to have completed. This has been 15390d80b9bdSThomas Moestl * observed on some machines. Just exiting here 15400d80b9bdSThomas Moestl * might leave the packet sitting around until another 15410d80b9bdSThomas Moestl * one arrives to trigger a new interrupt, which is 15420d80b9bdSThomas Moestl * generally undesirable, so set up a timeout. 154342c1b001SThomas Moestl */ 15440d80b9bdSThomas Moestl callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 15450d80b9bdSThomas Moestl gem_rint_timeout, sc); 1546336cca9eSBenno Rice #endif 15471ed3fed7SMarius Strobl m = NULL; 15481ed3fed7SMarius Strobl goto kickit; 154942c1b001SThomas Moestl } 155042c1b001SThomas Moestl 155142c1b001SThomas Moestl if (rxstat & GEM_RD_BAD_CRC) { 1552336cca9eSBenno Rice ifp->if_ierrors++; 155342c1b001SThomas Moestl device_printf(sc->sc_dev, "receive error: CRC error\n"); 15541ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15551ed3fed7SMarius Strobl m = NULL; 15561ed3fed7SMarius Strobl goto kickit; 155742c1b001SThomas Moestl } 155842c1b001SThomas Moestl 155942c1b001SThomas Moestl #ifdef GEM_DEBUG 15602a79fd39SMarius Strobl if ((ifp->if_flags & IFF_DEBUG) != 0) { 15611ed3fed7SMarius Strobl printf(" rxsoft %p descriptor %d: ", 15621ed3fed7SMarius Strobl &sc->sc_rxsoft[sc->sc_rxptr], sc->sc_rxptr); 15632a79fd39SMarius Strobl printf("gd_flags: 0x%016llx\t", 15642a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15652a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_flags)); 15662a79fd39SMarius Strobl printf("gd_addr: 0x%016llx\n", 15672a79fd39SMarius Strobl (long long)GEM_DMA_READ(sc, 15682a79fd39SMarius Strobl sc->sc_rxdescs[sc->sc_rxptr].gd_addr)); 156942c1b001SThomas Moestl } 157042c1b001SThomas Moestl #endif 157142c1b001SThomas Moestl 157242c1b001SThomas Moestl /* 157342c1b001SThomas Moestl * Allocate a new mbuf cluster. If that fails, we are 157442c1b001SThomas Moestl * out of memory, and must drop the packet and recycle 157542c1b001SThomas Moestl * the buffer that's already attached to this descriptor. 157642c1b001SThomas Moestl */ 15771ed3fed7SMarius Strobl if (gem_add_rxbuf(sc, sc->sc_rxptr) != 0) { 157842c1b001SThomas Moestl ifp->if_ierrors++; 15791ed3fed7SMarius Strobl GEM_INIT_RXDESC(sc, sc->sc_rxptr); 15801ed3fed7SMarius Strobl m = NULL; 15811ed3fed7SMarius Strobl } 15821ed3fed7SMarius Strobl 15831ed3fed7SMarius Strobl kickit: 15841ed3fed7SMarius Strobl /* 15851ed3fed7SMarius Strobl * Update the RX kick register. This register has to point 15861ed3fed7SMarius Strobl * to the descriptor after the last valid one (before the 15879ba2b298SMarius Strobl * current batch) and for optimum performance should be 15889ba2b298SMarius Strobl * incremented in multiples of 4 (the DMA engine fetches/ 15899ba2b298SMarius Strobl * updates descriptors in batches of 4). 15901ed3fed7SMarius Strobl */ 15911ed3fed7SMarius Strobl sc->sc_rxptr = GEM_NEXTRX(sc->sc_rxptr); 15921ed3fed7SMarius Strobl if ((sc->sc_rxptr % 4) == 0) { 1593ccb1212aSMarius Strobl GEM_CDSYNC(sc, 1594ccb1212aSMarius Strobl BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1595bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_RX_KICK, 15961ed3fed7SMarius Strobl (sc->sc_rxptr + GEM_NRXDESC - 4) & 15971ed3fed7SMarius Strobl GEM_NRXDESC_MASK); 15981ed3fed7SMarius Strobl } 15991ed3fed7SMarius Strobl 16001ed3fed7SMarius Strobl if (m == NULL) { 16011ed3fed7SMarius Strobl if (rxstat & GEM_RD_OWN) 16021ed3fed7SMarius Strobl break; 160342c1b001SThomas Moestl continue; 160442c1b001SThomas Moestl } 160542c1b001SThomas Moestl 16061ed3fed7SMarius Strobl ifp->if_ipackets++; 16079ba2b298SMarius Strobl m->m_data += ETHER_ALIGN; /* first byte offset */ 160842c1b001SThomas Moestl m->m_pkthdr.rcvif = ifp; 16091ed3fed7SMarius Strobl m->m_pkthdr.len = m->m_len = GEM_RD_BUFLEN(rxstat); 161012fb0330SPyun YongHyeon 161112fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 161212fb0330SPyun YongHyeon gem_rxcksum(m, rxstat); 161342c1b001SThomas Moestl 161442c1b001SThomas Moestl /* Pass it on. */ 16158cfaff7dSMarius Strobl GEM_UNLOCK(sc); 1616673d9191SSam Leffler (*ifp->if_input)(ifp, m); 16178cfaff7dSMarius Strobl GEM_LOCK(sc); 161842c1b001SThomas Moestl } 161942c1b001SThomas Moestl 162018100346SThomas Moestl #ifdef GEM_DEBUG 16219ba2b298SMarius Strobl CTR3(KTR_GEM, "%s: done sc->sc_rxptr %d, complete %d", __func__, 1622bd3d9826SMarius Strobl sc->sc_rxptr, GEM_BANK1_READ_4(sc, GEM_RX_COMPLETION)); 162318100346SThomas Moestl #endif 162442c1b001SThomas Moestl } 162542c1b001SThomas Moestl 162642c1b001SThomas Moestl static int 16272a79fd39SMarius Strobl gem_add_rxbuf(struct gem_softc *sc, int idx) 162842c1b001SThomas Moestl { 162942c1b001SThomas Moestl struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 163042c1b001SThomas Moestl struct mbuf *m; 1631c3d5598aSMarius Strobl bus_dma_segment_t segs[1]; 1632c3d5598aSMarius Strobl int error, nsegs; 163342c1b001SThomas Moestl 16349ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 16359ba2b298SMarius Strobl 1636a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 163742c1b001SThomas Moestl if (m == NULL) 163842c1b001SThomas Moestl return (ENOBUFS); 1639305f2c06SThomas Moestl m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 164042c1b001SThomas Moestl 164142c1b001SThomas Moestl #ifdef GEM_DEBUG 16422a79fd39SMarius Strobl /* Bzero the packet to check DMA. */ 164342c1b001SThomas Moestl memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 164442c1b001SThomas Moestl #endif 164542c1b001SThomas Moestl 1646b2d59f42SThomas Moestl if (rxs->rxs_mbuf != NULL) { 1647b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 1648b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 1649305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 1650b2d59f42SThomas Moestl } 165142c1b001SThomas Moestl 1652c3d5598aSMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap, 1653c3d5598aSMarius Strobl m, segs, &nsegs, BUS_DMA_NOWAIT); 1654c3d5598aSMarius Strobl if (error != 0) { 16552a79fd39SMarius Strobl device_printf(sc->sc_dev, 16562a79fd39SMarius Strobl "cannot load RS DMA map %d, error = %d\n", idx, error); 1657c3d5598aSMarius Strobl m_freem(m); 16581ed3fed7SMarius Strobl return (error); 165942c1b001SThomas Moestl } 16602a79fd39SMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1661801772ecSMarius Strobl KASSERT(nsegs == 1, 1662801772ecSMarius Strobl ("%s: too many DMA segments (%d)", __func__, nsegs)); 16631ed3fed7SMarius Strobl rxs->rxs_mbuf = m; 1664c3d5598aSMarius Strobl rxs->rxs_paddr = segs[0].ds_addr; 166542c1b001SThomas Moestl 16662a79fd39SMarius Strobl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 16672a79fd39SMarius Strobl BUS_DMASYNC_PREREAD); 166842c1b001SThomas Moestl 166942c1b001SThomas Moestl GEM_INIT_RXDESC(sc, idx); 167042c1b001SThomas Moestl 167142c1b001SThomas Moestl return (0); 167242c1b001SThomas Moestl } 167342c1b001SThomas Moestl 167442c1b001SThomas Moestl static void 16752a79fd39SMarius Strobl gem_eint(struct gem_softc *sc, u_int status) 167642c1b001SThomas Moestl { 167742c1b001SThomas Moestl 16781ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 16791ed3fed7SMarius Strobl if ((status & GEM_INTR_RX_TAG_ERR) != 0) { 16801ed3fed7SMarius Strobl gem_reset_rxdma(sc); 168142c1b001SThomas Moestl return; 168242c1b001SThomas Moestl } 168342c1b001SThomas Moestl 16849ba2b298SMarius Strobl device_printf(sc->sc_dev, "%s: status 0x%x", __func__, status); 16859ba2b298SMarius Strobl if ((status & GEM_INTR_BERR) != 0) { 16869ba2b298SMarius Strobl if ((sc->sc_flags & GEM_PCI) != 0) 16879ba2b298SMarius Strobl printf(", PCI bus error 0x%x\n", 16889ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_PCI_ERROR_STATUS)); 16899ba2b298SMarius Strobl else 16909ba2b298SMarius Strobl printf(", SBus error 0x%x\n", 16919ba2b298SMarius Strobl GEM_BANK1_READ_4(sc, GEM_SBUS_STATUS)); 16929ba2b298SMarius Strobl } 169342c1b001SThomas Moestl } 169442c1b001SThomas Moestl 169542c1b001SThomas Moestl void 16962a79fd39SMarius Strobl gem_intr(void *v) 169742c1b001SThomas Moestl { 16982a79fd39SMarius Strobl struct gem_softc *sc = v; 16991ed3fed7SMarius Strobl uint32_t status, status2; 170042c1b001SThomas Moestl 17018cfaff7dSMarius Strobl GEM_LOCK(sc); 1702bd3d9826SMarius Strobl status = GEM_BANK1_READ_4(sc, GEM_STATUS); 17031ed3fed7SMarius Strobl 170418100346SThomas Moestl #ifdef GEM_DEBUG 170512fb0330SPyun YongHyeon CTR4(KTR_GEM, "%s: %s: cplt %x, status %x", 17069ba2b298SMarius Strobl device_get_name(sc->sc_dev), __func__, 17079ba2b298SMarius Strobl (status >> GEM_STATUS_TX_COMPLETION_SHFT), (u_int)status); 17081ed3fed7SMarius Strobl 17091ed3fed7SMarius Strobl /* 17101ed3fed7SMarius Strobl * PCS interrupts must be cleared, otherwise no traffic is passed! 17111ed3fed7SMarius Strobl */ 17121ed3fed7SMarius Strobl if ((status & GEM_INTR_PCS) != 0) { 17132a79fd39SMarius Strobl status2 = 1714bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS) | 1715bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MII_INTERRUP_STATUS); 17161ed3fed7SMarius Strobl if ((status2 & GEM_MII_INTERRUP_LINK) != 0) 17171ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17181ed3fed7SMarius Strobl "%s: PCS link status changed\n", __func__); 17191ed3fed7SMarius Strobl } 17201ed3fed7SMarius Strobl if ((status & GEM_MAC_CONTROL_STATUS) != 0) { 1721bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_STATUS); 17221ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSED) != 0) 17231ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17241ed3fed7SMarius Strobl "%s: PAUSE received (PAUSE time %d slots)\n", 17251ed3fed7SMarius Strobl __func__, GEM_MAC_PAUSE_TIME(status2)); 17261ed3fed7SMarius Strobl if ((status2 & GEM_MAC_PAUSE) != 0) 17271ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17281ed3fed7SMarius Strobl "%s: transited to PAUSE state\n", __func__); 17291ed3fed7SMarius Strobl if ((status2 & GEM_MAC_RESUME) != 0) 17301ed3fed7SMarius Strobl device_printf(sc->sc_dev, 17311ed3fed7SMarius Strobl "%s: transited to non-PAUSE state\n", __func__); 17321ed3fed7SMarius Strobl } 17331ed3fed7SMarius Strobl if ((status & GEM_INTR_MIF) != 0) 17341ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: MIF interrupt\n", __func__); 173518100346SThomas Moestl #endif 173642c1b001SThomas Moestl 17379ba2b298SMarius Strobl if (__predict_false(status & 17381ed3fed7SMarius Strobl (GEM_INTR_RX_TAG_ERR | GEM_INTR_PERR | GEM_INTR_BERR)) != 0) 173942c1b001SThomas Moestl gem_eint(sc, status); 174042c1b001SThomas Moestl 174142c1b001SThomas Moestl if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 174242c1b001SThomas Moestl gem_rint(sc); 174342c1b001SThomas Moestl 17441ed3fed7SMarius Strobl if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 17451ed3fed7SMarius Strobl gem_tint(sc); 17461ed3fed7SMarius Strobl 17479ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_TX_MAC) != 0)) { 1748bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS); 17492a79fd39SMarius Strobl if ((status2 & 17509ba2b298SMarius Strobl ~(GEM_MAC_TX_XMIT_DONE | GEM_MAC_TX_DEFER_EXP | 17519ba2b298SMarius Strobl GEM_MAC_TX_PEAK_EXP)) != 0) 17522a79fd39SMarius Strobl device_printf(sc->sc_dev, 17532a79fd39SMarius Strobl "MAC TX fault, status %x\n", status2); 17542a79fd39SMarius Strobl if ((status2 & 17559ba2b298SMarius Strobl (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) != 0) { 17569ba2b298SMarius Strobl sc->sc_ifp->if_oerrors++; 17578cfaff7dSMarius Strobl gem_init_locked(sc); 175842c1b001SThomas Moestl } 17599ba2b298SMarius Strobl } 17609ba2b298SMarius Strobl if (__predict_false((status & GEM_INTR_RX_MAC) != 0)) { 1761bd3d9826SMarius Strobl status2 = GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS); 176200d12766SMarius Strobl /* 17631ed3fed7SMarius Strobl * At least with GEM_SUN_GEM and some GEM_SUN_ERI 17641ed3fed7SMarius Strobl * revisions GEM_MAC_RX_OVERFLOW happen often due to a 17651ed3fed7SMarius Strobl * silicon bug so handle them silently. Moreover, it's 17661ed3fed7SMarius Strobl * likely that the receiver has hung so we reset it. 176700d12766SMarius Strobl */ 17682a79fd39SMarius Strobl if ((status2 & GEM_MAC_RX_OVERFLOW) != 0) { 17691ed3fed7SMarius Strobl sc->sc_ifp->if_ierrors++; 17701ed3fed7SMarius Strobl gem_reset_rxdma(sc); 17712a79fd39SMarius Strobl } else if ((status2 & 17722a79fd39SMarius Strobl ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) != 0) 17732a79fd39SMarius Strobl device_printf(sc->sc_dev, 17742a79fd39SMarius Strobl "MAC RX fault, status %x\n", status2); 177542c1b001SThomas Moestl } 17768cfaff7dSMarius Strobl GEM_UNLOCK(sc); 177742c1b001SThomas Moestl } 177842c1b001SThomas Moestl 17798cb37876SMarius Strobl static int 17802a79fd39SMarius Strobl gem_watchdog(struct gem_softc *sc) 178142c1b001SThomas Moestl { 1782ccb1212aSMarius Strobl struct ifnet *ifp = sc->sc_ifp; 178342c1b001SThomas Moestl 17848cb37876SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 17858cb37876SMarius Strobl 178618100346SThomas Moestl #ifdef GEM_DEBUG 17872a79fd39SMarius Strobl CTR4(KTR_GEM, 17882a79fd39SMarius Strobl "%s: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x GEM_MAC_RX_CONFIG %x", 1789bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_RX_CONFIG), 1790bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_STATUS), 1791bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG)); 17922a79fd39SMarius Strobl CTR4(KTR_GEM, 17932a79fd39SMarius Strobl "%s: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x GEM_MAC_TX_CONFIG %x", 1794bd3d9826SMarius Strobl __func__, GEM_BANK1_READ_4(sc, GEM_TX_CONFIG), 1795bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_STATUS), 1796bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MAC_TX_CONFIG)); 179718100346SThomas Moestl #endif 179842c1b001SThomas Moestl 17998cb37876SMarius Strobl if (sc->sc_wdog_timer == 0 || --sc->sc_wdog_timer != 0) 18008cb37876SMarius Strobl return (0); 18018cb37876SMarius Strobl 18021ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_LINK) != 0) 180342c1b001SThomas Moestl device_printf(sc->sc_dev, "device timeout\n"); 18041ed3fed7SMarius Strobl else if (bootverbose) 18051ed3fed7SMarius Strobl device_printf(sc->sc_dev, "device timeout (no link)\n"); 1806ccb1212aSMarius Strobl ++ifp->if_oerrors; 180742c1b001SThomas Moestl 180842c1b001SThomas Moestl /* Try to get more packets going. */ 18098cfaff7dSMarius Strobl gem_init_locked(sc); 1810ccb1212aSMarius Strobl gem_start_locked(ifp); 18118cb37876SMarius Strobl return (EJUSTRETURN); 181242c1b001SThomas Moestl } 181342c1b001SThomas Moestl 181442c1b001SThomas Moestl static void 18152a79fd39SMarius Strobl gem_mifinit(struct gem_softc *sc) 181642c1b001SThomas Moestl { 181742c1b001SThomas Moestl 1818801772ecSMarius Strobl /* Configure the MIF in frame mode. */ 1819bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_CONFIG, 1820bd3d9826SMarius Strobl GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & ~GEM_MIF_CONFIG_BB_ENA); 1821*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_CONFIG, 4, 1822*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 182342c1b001SThomas Moestl } 182442c1b001SThomas Moestl 182542c1b001SThomas Moestl /* 182642c1b001SThomas Moestl * MII interface 182742c1b001SThomas Moestl * 182878d22f42SMarius Strobl * The MII interface supports at least three different operating modes: 182942c1b001SThomas Moestl * 183042c1b001SThomas Moestl * Bitbang mode is implemented using data, clock and output enable registers. 183142c1b001SThomas Moestl * 183242c1b001SThomas Moestl * Frame mode is implemented by loading a complete frame into the frame 183342c1b001SThomas Moestl * register and polling the valid bit for completion. 183442c1b001SThomas Moestl * 183542c1b001SThomas Moestl * Polling mode uses the frame register but completion is indicated by 183642c1b001SThomas Moestl * an interrupt. 183742c1b001SThomas Moestl * 183842c1b001SThomas Moestl */ 183942c1b001SThomas Moestl int 18402a79fd39SMarius Strobl gem_mii_readreg(device_t dev, int phy, int reg) 184142c1b001SThomas Moestl { 18422a79fd39SMarius Strobl struct gem_softc *sc; 184342c1b001SThomas Moestl int n; 18442a79fd39SMarius Strobl uint32_t v; 184542c1b001SThomas Moestl 184642c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 18471ed3fed7SMarius Strobl printf("%s: phy %d reg %d\n", __func__, phy, reg); 184842c1b001SThomas Moestl #endif 184942c1b001SThomas Moestl 18502a79fd39SMarius Strobl sc = device_get_softc(dev); 18511ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 18521ed3fed7SMarius Strobl return (0); 18531ed3fed7SMarius Strobl 18541ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 18551ed3fed7SMarius Strobl switch (reg) { 18561ed3fed7SMarius Strobl case MII_BMCR: 18571ed3fed7SMarius Strobl reg = GEM_MII_CONTROL; 18581ed3fed7SMarius Strobl break; 18591ed3fed7SMarius Strobl case MII_BMSR: 18601ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 18611ed3fed7SMarius Strobl break; 18621ed3fed7SMarius Strobl case MII_PHYIDR1: 18631ed3fed7SMarius Strobl case MII_PHYIDR2: 18641ed3fed7SMarius Strobl return (0); 18651ed3fed7SMarius Strobl case MII_ANAR: 18661ed3fed7SMarius Strobl reg = GEM_MII_ANAR; 18671ed3fed7SMarius Strobl break; 18681ed3fed7SMarius Strobl case MII_ANLPAR: 18691ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 18701ed3fed7SMarius Strobl break; 18711ed3fed7SMarius Strobl case MII_EXTSR: 18721ed3fed7SMarius Strobl return (EXTSR_1000XFDX | EXTSR_1000XHDX); 18731ed3fed7SMarius Strobl default: 18741ed3fed7SMarius Strobl device_printf(sc->sc_dev, 18751ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 18761ed3fed7SMarius Strobl return (0); 18771ed3fed7SMarius Strobl } 1878bd3d9826SMarius Strobl return (GEM_BANK1_READ_4(sc, reg)); 18791ed3fed7SMarius Strobl } 188042c1b001SThomas Moestl 18812a79fd39SMarius Strobl /* Construct the frame command. */ 18821ed3fed7SMarius Strobl v = GEM_MIF_FRAME_READ | 18831ed3fed7SMarius Strobl (phy << GEM_MIF_PHY_SHIFT) | 18841ed3fed7SMarius Strobl (reg << GEM_MIF_REG_SHIFT); 188542c1b001SThomas Moestl 1886bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1887ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1888ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 188942c1b001SThomas Moestl for (n = 0; n < 100; n++) { 189042c1b001SThomas Moestl DELAY(1); 1891bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 18921f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 189342c1b001SThomas Moestl return (v & GEM_MIF_FRAME_DATA); 189442c1b001SThomas Moestl } 189542c1b001SThomas Moestl 18962a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 189742c1b001SThomas Moestl return (0); 189842c1b001SThomas Moestl } 189942c1b001SThomas Moestl 190042c1b001SThomas Moestl int 19012a79fd39SMarius Strobl gem_mii_writereg(device_t dev, int phy, int reg, int val) 190242c1b001SThomas Moestl { 19032a79fd39SMarius Strobl struct gem_softc *sc; 190442c1b001SThomas Moestl int n; 19052a79fd39SMarius Strobl uint32_t v; 190642c1b001SThomas Moestl 190742c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 19081ed3fed7SMarius Strobl printf("%s: phy %d reg %d val %x\n", phy, reg, val, __func__); 190942c1b001SThomas Moestl #endif 191042c1b001SThomas Moestl 19112a79fd39SMarius Strobl sc = device_get_softc(dev); 19121ed3fed7SMarius Strobl if (sc->sc_phyad != -1 && phy != sc->sc_phyad) 19131ed3fed7SMarius Strobl return (0); 19141ed3fed7SMarius Strobl 19151ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) != 0) { 19161ed3fed7SMarius Strobl switch (reg) { 19171ed3fed7SMarius Strobl case MII_BMSR: 19181ed3fed7SMarius Strobl reg = GEM_MII_STATUS; 19191ed3fed7SMarius Strobl break; 1920ccb1212aSMarius Strobl case MII_BMCR: 1921ccb1212aSMarius Strobl reg = GEM_MII_CONTROL; 1922ccb1212aSMarius Strobl if ((val & GEM_MII_CONTROL_RESET) == 0) 1923ccb1212aSMarius Strobl break; 1924ccb1212aSMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONTROL, val); 1925ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONTROL, 4, 1926ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 1927ccb1212aSMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MII_CONTROL, 1928ccb1212aSMarius Strobl GEM_MII_CONTROL_RESET, 0)) 1929ccb1212aSMarius Strobl device_printf(sc->sc_dev, 1930ccb1212aSMarius Strobl "cannot reset PCS\n"); 1931ccb1212aSMarius Strobl /* FALLTHROUGH */ 19321ed3fed7SMarius Strobl case MII_ANAR: 1933bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 0); 1934bd3d9826SMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4, 19351ed3fed7SMarius Strobl BUS_SPACE_BARRIER_WRITE); 1936bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_ANAR, val); 1937*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_ANAR, 4, 1938*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 1939bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_SLINK_CONTROL, 19401ed3fed7SMarius Strobl GEM_MII_SLINK_LOOPBACK | GEM_MII_SLINK_EN_SYNC_D); 1941*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_SLINK_CONTROL, 4, 1942*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 1943bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MII_CONFIG, 19441ed3fed7SMarius Strobl GEM_MII_CONFIG_ENABLE); 1945*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MII_CONFIG, 4, 1946*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_WRITE); 19471ed3fed7SMarius Strobl return (0); 19481ed3fed7SMarius Strobl case MII_ANLPAR: 19491ed3fed7SMarius Strobl reg = GEM_MII_ANLPAR; 19501ed3fed7SMarius Strobl break; 19511ed3fed7SMarius Strobl default: 19521ed3fed7SMarius Strobl device_printf(sc->sc_dev, 19531ed3fed7SMarius Strobl "%s: unhandled register %d\n", __func__, reg); 19541ed3fed7SMarius Strobl return (0); 19551ed3fed7SMarius Strobl } 1956bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, reg, val); 1957*65f2c0ffSMarius Strobl GEM_BANK1_BARRIER(sc, reg, 4, 1958*65f2c0ffSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 19591ed3fed7SMarius Strobl return (0); 19601ed3fed7SMarius Strobl } 19611ed3fed7SMarius Strobl 19622a79fd39SMarius Strobl /* Construct the frame command. */ 196342c1b001SThomas Moestl v = GEM_MIF_FRAME_WRITE | 196442c1b001SThomas Moestl (phy << GEM_MIF_PHY_SHIFT) | 196542c1b001SThomas Moestl (reg << GEM_MIF_REG_SHIFT) | 196642c1b001SThomas Moestl (val & GEM_MIF_FRAME_DATA); 196742c1b001SThomas Moestl 1968bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MIF_FRAME, v); 1969ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MIF_FRAME, 4, 1970ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 197142c1b001SThomas Moestl for (n = 0; n < 100; n++) { 197242c1b001SThomas Moestl DELAY(1); 1973bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MIF_FRAME); 19741f317bf9SMarius Strobl if (v & GEM_MIF_FRAME_TA0) 197542c1b001SThomas Moestl return (1); 197642c1b001SThomas Moestl } 197742c1b001SThomas Moestl 19782a79fd39SMarius Strobl device_printf(sc->sc_dev, "%s: timed out\n", __func__); 197942c1b001SThomas Moestl return (0); 198042c1b001SThomas Moestl } 198142c1b001SThomas Moestl 198242c1b001SThomas Moestl void 19832a79fd39SMarius Strobl gem_mii_statchg(device_t dev) 198442c1b001SThomas Moestl { 19852a79fd39SMarius Strobl struct gem_softc *sc; 19861ed3fed7SMarius Strobl int gigabit; 19871ed3fed7SMarius Strobl uint32_t rxcfg, txcfg, v; 198842c1b001SThomas Moestl 19892a79fd39SMarius Strobl sc = device_get_softc(dev); 19902a79fd39SMarius Strobl 19919ba2b298SMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 19929ba2b298SMarius Strobl 199342c1b001SThomas Moestl #ifdef GEM_DEBUG 19942a79fd39SMarius Strobl if ((sc->sc_ifp->if_flags & IFF_DEBUG) != 0) 19951ed3fed7SMarius Strobl device_printf(sc->sc_dev, "%s: status change: PHY = %d\n", 19961ed3fed7SMarius Strobl __func__, sc->sc_phyad); 199742c1b001SThomas Moestl #endif 199842c1b001SThomas Moestl 19991ed3fed7SMarius Strobl if ((sc->sc_mii->mii_media_status & IFM_ACTIVE) != 0 && 20001ed3fed7SMarius Strobl IFM_SUBTYPE(sc->sc_mii->mii_media_active) != IFM_NONE) 20011ed3fed7SMarius Strobl sc->sc_flags |= GEM_LINK; 20021ed3fed7SMarius Strobl else 20031ed3fed7SMarius Strobl sc->sc_flags &= ~GEM_LINK; 20041ed3fed7SMarius Strobl 20051ed3fed7SMarius Strobl switch (IFM_SUBTYPE(sc->sc_mii->mii_media_active)) { 20061ed3fed7SMarius Strobl case IFM_1000_SX: 20071ed3fed7SMarius Strobl case IFM_1000_LX: 20081ed3fed7SMarius Strobl case IFM_1000_CX: 20091ed3fed7SMarius Strobl case IFM_1000_T: 20101ed3fed7SMarius Strobl gigabit = 1; 20111ed3fed7SMarius Strobl break; 20121ed3fed7SMarius Strobl default: 20131ed3fed7SMarius Strobl gigabit = 0; 201442c1b001SThomas Moestl } 20151ed3fed7SMarius Strobl 20161ed3fed7SMarius Strobl /* 20171ed3fed7SMarius Strobl * The configuration done here corresponds to the steps F) and 20181ed3fed7SMarius Strobl * G) and as far as enabling of RX and TX MAC goes also step H) 20191ed3fed7SMarius Strobl * of the initialization sequence outlined in section 3.2.1 of 20201ed3fed7SMarius Strobl * the GEM Gigabit Ethernet ASIC Specification. 20211ed3fed7SMarius Strobl */ 20221ed3fed7SMarius Strobl 2023bd3d9826SMarius Strobl rxcfg = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 20241ed3fed7SMarius Strobl rxcfg &= ~(GEM_MAC_RX_CARR_EXTEND | GEM_MAC_RX_ENABLE); 20251ed3fed7SMarius Strobl txcfg = GEM_MAC_TX_ENA_IPG0 | GEM_MAC_TX_NGU | GEM_MAC_TX_NGU_LIMIT; 20261ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 20271ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_IGN_CARRIER | GEM_MAC_TX_IGN_COLLIS; 20281ed3fed7SMarius Strobl else if (gigabit != 0) { 20291ed3fed7SMarius Strobl rxcfg |= GEM_MAC_RX_CARR_EXTEND; 20301ed3fed7SMarius Strobl txcfg |= GEM_MAC_TX_CARR_EXTEND; 20311ed3fed7SMarius Strobl } 2032bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 0); 2033ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_TX_CONFIG, 4, 2034ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2035bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)) 20361ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable TX MAC\n"); 2037bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, txcfg); 2038bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 0); 2039ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2040ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2041bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)) 20421ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX MAC\n"); 2043bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, rxcfg); 20441ed3fed7SMarius Strobl 2045bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_CONTROL_CONFIG) & 20461ed3fed7SMarius Strobl ~(GEM_MAC_CC_RX_PAUSE | GEM_MAC_CC_TX_PAUSE); 20471ed3fed7SMarius Strobl #ifdef notyet 20482a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20492a79fd39SMarius Strobl IFM_ETH_RXPAUSE) != 0) 20501ed3fed7SMarius Strobl v |= GEM_MAC_CC_RX_PAUSE; 20512a79fd39SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 20522a79fd39SMarius Strobl IFM_ETH_TXPAUSE) != 0) 20531ed3fed7SMarius Strobl v |= GEM_MAC_CC_TX_PAUSE; 20541ed3fed7SMarius Strobl #endif 2055bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_CONTROL_CONFIG, v); 20561ed3fed7SMarius Strobl 20571ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) == 0 && 20581ed3fed7SMarius Strobl gigabit != 0) 2059bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20601ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_CARR_EXTEND); 20611ed3fed7SMarius Strobl else 2062bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_SLOT_TIME, 20631ed3fed7SMarius Strobl GEM_MAC_SLOT_TIME_NORMAL); 206442c1b001SThomas Moestl 206542c1b001SThomas Moestl /* XIF Configuration */ 206642c1b001SThomas Moestl v = GEM_MAC_XIF_LINK_LED; 206742c1b001SThomas Moestl v |= GEM_MAC_XIF_TX_MII_ENA; 20681ed3fed7SMarius Strobl if ((sc->sc_flags & GEM_SERDES) == 0) { 2069bd3d9826SMarius Strobl if ((GEM_BANK1_READ_4(sc, GEM_MIF_CONFIG) & 207078d22f42SMarius Strobl GEM_MIF_CONFIG_PHY_SEL) != 0) { 207142c1b001SThomas Moestl /* External MII needs echo disable if half duplex. */ 207278d22f42SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & 207378d22f42SMarius Strobl IFM_FDX) == 0) 207442c1b001SThomas Moestl v |= GEM_MAC_XIF_ECHO_DISABL; 207578d22f42SMarius Strobl } else 20761ed3fed7SMarius Strobl /* 20771ed3fed7SMarius Strobl * Internal MII needs buffer enable. 20781ed3fed7SMarius Strobl * XXX buffer enable makes only sense for an 20791ed3fed7SMarius Strobl * external PHY. 20801ed3fed7SMarius Strobl */ 208142c1b001SThomas Moestl v |= GEM_MAC_XIF_MII_BUF_ENA; 208242c1b001SThomas Moestl } 20831ed3fed7SMarius Strobl if (gigabit != 0) 20841ed3fed7SMarius Strobl v |= GEM_MAC_XIF_GMII_MODE; 20851ed3fed7SMarius Strobl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 20861ed3fed7SMarius Strobl v |= GEM_MAC_XIF_FDPLX_LED; 2087bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_XIF_CONFIG, v); 20881ed3fed7SMarius Strobl 20891ed3fed7SMarius Strobl if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 20901ed3fed7SMarius Strobl (sc->sc_flags & GEM_LINK) != 0) { 2091bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_TX_CONFIG, 20921ed3fed7SMarius Strobl txcfg | GEM_MAC_TX_ENABLE); 2093bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, 20941ed3fed7SMarius Strobl rxcfg | GEM_MAC_RX_ENABLE); 20951ed3fed7SMarius Strobl } 209642c1b001SThomas Moestl } 209742c1b001SThomas Moestl 209842c1b001SThomas Moestl int 20992a79fd39SMarius Strobl gem_mediachange(struct ifnet *ifp) 210042c1b001SThomas Moestl { 210142c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 21021f317bf9SMarius Strobl int error; 210342c1b001SThomas Moestl 21042a79fd39SMarius Strobl /* XXX add support for serial media. */ 210542c1b001SThomas Moestl 21061f317bf9SMarius Strobl GEM_LOCK(sc); 21071f317bf9SMarius Strobl error = mii_mediachg(sc->sc_mii); 21081f317bf9SMarius Strobl GEM_UNLOCK(sc); 21091f317bf9SMarius Strobl return (error); 211042c1b001SThomas Moestl } 211142c1b001SThomas Moestl 211242c1b001SThomas Moestl void 21132a79fd39SMarius Strobl gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 211442c1b001SThomas Moestl { 211542c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 211642c1b001SThomas Moestl 21178cfaff7dSMarius Strobl GEM_LOCK(sc); 21188cfaff7dSMarius Strobl if ((ifp->if_flags & IFF_UP) == 0) { 21198cfaff7dSMarius Strobl GEM_UNLOCK(sc); 212042c1b001SThomas Moestl return; 21218cfaff7dSMarius Strobl } 212242c1b001SThomas Moestl 212342c1b001SThomas Moestl mii_pollstat(sc->sc_mii); 212442c1b001SThomas Moestl ifmr->ifm_active = sc->sc_mii->mii_media_active; 212542c1b001SThomas Moestl ifmr->ifm_status = sc->sc_mii->mii_media_status; 21268cfaff7dSMarius Strobl GEM_UNLOCK(sc); 212742c1b001SThomas Moestl } 212842c1b001SThomas Moestl 212942c1b001SThomas Moestl static int 21302a79fd39SMarius Strobl gem_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 213142c1b001SThomas Moestl { 213242c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 213342c1b001SThomas Moestl struct ifreq *ifr = (struct ifreq *)data; 21342a79fd39SMarius Strobl int error; 21358cfaff7dSMarius Strobl 21362a79fd39SMarius Strobl error = 0; 213742c1b001SThomas Moestl switch (cmd) { 213842c1b001SThomas Moestl case SIOCSIFFLAGS: 21391f317bf9SMarius Strobl GEM_LOCK(sc); 21402a79fd39SMarius Strobl if ((ifp->if_flags & IFF_UP) != 0) { 21411ed3fed7SMarius Strobl if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 21421ed3fed7SMarius Strobl ((ifp->if_flags ^ sc->sc_ifflags) & 21431ed3fed7SMarius Strobl (IFF_ALLMULTI | IFF_PROMISC)) != 0) 214442c1b001SThomas Moestl gem_setladrf(sc); 214542c1b001SThomas Moestl else 21468cfaff7dSMarius Strobl gem_init_locked(sc); 21472a79fd39SMarius Strobl } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 214842c1b001SThomas Moestl gem_stop(ifp, 0); 214912fb0330SPyun YongHyeon if ((ifp->if_flags & IFF_LINK0) != 0) 215012fb0330SPyun YongHyeon sc->sc_csum_features |= CSUM_UDP; 215112fb0330SPyun YongHyeon else 215212fb0330SPyun YongHyeon sc->sc_csum_features &= ~CSUM_UDP; 215312fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 215412fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 2155336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 21561f317bf9SMarius Strobl GEM_UNLOCK(sc); 215742c1b001SThomas Moestl break; 215842c1b001SThomas Moestl case SIOCADDMULTI: 215942c1b001SThomas Moestl case SIOCDELMULTI: 21601f317bf9SMarius Strobl GEM_LOCK(sc); 216142c1b001SThomas Moestl gem_setladrf(sc); 21621f317bf9SMarius Strobl GEM_UNLOCK(sc); 216342c1b001SThomas Moestl break; 216442c1b001SThomas Moestl case SIOCGIFMEDIA: 216542c1b001SThomas Moestl case SIOCSIFMEDIA: 216642c1b001SThomas Moestl error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 216742c1b001SThomas Moestl break; 216812fb0330SPyun YongHyeon case SIOCSIFCAP: 216912fb0330SPyun YongHyeon GEM_LOCK(sc); 217012fb0330SPyun YongHyeon ifp->if_capenable = ifr->ifr_reqcap; 217112fb0330SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) 217212fb0330SPyun YongHyeon ifp->if_hwassist = sc->sc_csum_features; 217312fb0330SPyun YongHyeon else 217412fb0330SPyun YongHyeon ifp->if_hwassist = 0; 217512fb0330SPyun YongHyeon GEM_UNLOCK(sc); 217612fb0330SPyun YongHyeon break; 217742c1b001SThomas Moestl default: 21781f317bf9SMarius Strobl error = ether_ioctl(ifp, cmd, data); 217942c1b001SThomas Moestl break; 218042c1b001SThomas Moestl } 218142c1b001SThomas Moestl 218242c1b001SThomas Moestl return (error); 218342c1b001SThomas Moestl } 218442c1b001SThomas Moestl 218542c1b001SThomas Moestl static void 21862a79fd39SMarius Strobl gem_setladrf(struct gem_softc *sc) 218742c1b001SThomas Moestl { 2188fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 218942c1b001SThomas Moestl struct ifmultiaddr *inm; 2190336cca9eSBenno Rice int i; 21912a79fd39SMarius Strobl uint32_t hash[16]; 21922a79fd39SMarius Strobl uint32_t crc, v; 219342c1b001SThomas Moestl 21948cfaff7dSMarius Strobl GEM_LOCK_ASSERT(sc, MA_OWNED); 21958cfaff7dSMarius Strobl 21962a79fd39SMarius Strobl /* Get the current RX configuration. */ 2197bd3d9826SMarius Strobl v = GEM_BANK1_READ_4(sc, GEM_MAC_RX_CONFIG); 219842c1b001SThomas Moestl 2199336cca9eSBenno Rice /* 2200336cca9eSBenno Rice * Turn off promiscuous mode, promiscuous group mode (all multicast), 2201336cca9eSBenno Rice * and hash filter. Depending on the case, the right bit will be 2202336cca9eSBenno Rice * enabled. 2203336cca9eSBenno Rice */ 2204336cca9eSBenno Rice v &= ~(GEM_MAC_RX_PROMISCUOUS | GEM_MAC_RX_HASH_FILTER | 2205336cca9eSBenno Rice GEM_MAC_RX_PROMISC_GRP); 2206336cca9eSBenno Rice 2207bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 2208ccb1212aSMarius Strobl GEM_BANK1_BARRIER(sc, GEM_MAC_RX_CONFIG, 4, 2209ccb1212aSMarius Strobl BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE); 2210bd3d9826SMarius Strobl if (!GEM_BANK1_BITWAIT(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_HASH_FILTER, 2211bd3d9826SMarius Strobl 0)) 22121ed3fed7SMarius Strobl device_printf(sc->sc_dev, "cannot disable RX hash filter\n"); 22131ed3fed7SMarius Strobl 221442c1b001SThomas Moestl if ((ifp->if_flags & IFF_PROMISC) != 0) { 221542c1b001SThomas Moestl v |= GEM_MAC_RX_PROMISCUOUS; 221642c1b001SThomas Moestl goto chipit; 221742c1b001SThomas Moestl } 221842c1b001SThomas Moestl if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 2219336cca9eSBenno Rice v |= GEM_MAC_RX_PROMISC_GRP; 222042c1b001SThomas Moestl goto chipit; 222142c1b001SThomas Moestl } 222242c1b001SThomas Moestl 222342c1b001SThomas Moestl /* 22242a79fd39SMarius Strobl * Set up multicast address filter by passing all multicast 22252a79fd39SMarius Strobl * addresses through a crc generator, and then using the high 22262a79fd39SMarius Strobl * order 8 bits as an index into the 256 bit logical address 22272a79fd39SMarius Strobl * filter. The high order 4 bits selects the word, while the 22282a79fd39SMarius Strobl * other 4 bits select the bit within the word (where bit 0 22292a79fd39SMarius Strobl * is the MSB). 223042c1b001SThomas Moestl */ 223142c1b001SThomas Moestl 22322a79fd39SMarius Strobl /* Clear the hash table. */ 2233336cca9eSBenno Rice memset(hash, 0, sizeof(hash)); 2234336cca9eSBenno Rice 2235eb956cd0SRobert Watson if_maddr_rlock(ifp); 2236fc74a9f9SBrooks Davis TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) { 223742c1b001SThomas Moestl if (inm->ifma_addr->sa_family != AF_LINK) 223842c1b001SThomas Moestl continue; 2239c240bd8cSMarius Strobl crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 2240c240bd8cSMarius Strobl inm->ifma_addr), ETHER_ADDR_LEN); 224142c1b001SThomas Moestl 22422a79fd39SMarius Strobl /* We just want the 8 most significant bits. */ 224342c1b001SThomas Moestl crc >>= 24; 224442c1b001SThomas Moestl 224542c1b001SThomas Moestl /* Set the corresponding bit in the filter. */ 2246336cca9eSBenno Rice hash[crc >> 4] |= 1 << (15 - (crc & 15)); 2247336cca9eSBenno Rice } 2248eb956cd0SRobert Watson if_maddr_runlock(ifp); 2249336cca9eSBenno Rice 2250336cca9eSBenno Rice v |= GEM_MAC_RX_HASH_FILTER; 2251336cca9eSBenno Rice 22522a79fd39SMarius Strobl /* Now load the hash table into the chip (if we are using it). */ 22532a79fd39SMarius Strobl for (i = 0; i < 16; i++) 2254bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, 2255336cca9eSBenno Rice GEM_MAC_HASH0 + i * (GEM_MAC_HASH1 - GEM_MAC_HASH0), 2256336cca9eSBenno Rice hash[i]); 225742c1b001SThomas Moestl 225842c1b001SThomas Moestl chipit: 2259bd3d9826SMarius Strobl GEM_BANK1_WRITE_4(sc, GEM_MAC_RX_CONFIG, v); 226042c1b001SThomas Moestl } 2261