1aad970f1SDavid E. O'Brien /*- 242c1b001SThomas Moestl * Copyright (C) 2001 Eduardo Horvath. 3305f2c06SThomas Moestl * Copyright (c) 2001-2003 Thomas Moestl 442c1b001SThomas Moestl * All rights reserved. 542c1b001SThomas Moestl * 642c1b001SThomas Moestl * Redistribution and use in source and binary forms, with or without 742c1b001SThomas Moestl * modification, are permitted provided that the following conditions 842c1b001SThomas Moestl * are met: 942c1b001SThomas Moestl * 1. Redistributions of source code must retain the above copyright 1042c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer. 1142c1b001SThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 1242c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer in the 1342c1b001SThomas Moestl * documentation and/or other materials provided with the distribution. 1442c1b001SThomas Moestl * 1542c1b001SThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 1642c1b001SThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1742c1b001SThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1842c1b001SThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 1942c1b001SThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2042c1b001SThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2142c1b001SThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2242c1b001SThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2342c1b001SThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2442c1b001SThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2542c1b001SThomas Moestl * SUCH DAMAGE. 2642c1b001SThomas Moestl * 27336cca9eSBenno Rice * from: NetBSD: gem.c,v 1.21 2002/06/01 23:50:58 lukem Exp 2842c1b001SThomas Moestl */ 2942c1b001SThomas Moestl 30aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 31aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 32aad970f1SDavid E. O'Brien 3342c1b001SThomas Moestl /* 3442c1b001SThomas Moestl * Driver for Sun GEM ethernet controllers. 3542c1b001SThomas Moestl */ 3642c1b001SThomas Moestl 3718100346SThomas Moestl #if 0 3842c1b001SThomas Moestl #define GEM_DEBUG 3918100346SThomas Moestl #endif 4042c1b001SThomas Moestl 41c3d5598aSMarius Strobl #if 0 /* XXX: In case of emergency, re-enable this. */ 42c3d5598aSMarius Strobl #define GEM_RINT_TIMEOUT 43c3d5598aSMarius Strobl #endif 44c3d5598aSMarius Strobl 4542c1b001SThomas Moestl #include <sys/param.h> 4642c1b001SThomas Moestl #include <sys/systm.h> 4742c1b001SThomas Moestl #include <sys/bus.h> 4842c1b001SThomas Moestl #include <sys/callout.h> 49a30d4b32SMike Barcroft #include <sys/endian.h> 5042c1b001SThomas Moestl #include <sys/mbuf.h> 5142c1b001SThomas Moestl #include <sys/malloc.h> 5242c1b001SThomas Moestl #include <sys/kernel.h> 53186f2b9eSPoul-Henning Kamp #include <sys/module.h> 5442c1b001SThomas Moestl #include <sys/socket.h> 5542c1b001SThomas Moestl #include <sys/sockio.h> 5642c1b001SThomas Moestl 5708e0fdebSThomas Moestl #include <net/bpf.h> 5842c1b001SThomas Moestl #include <net/ethernet.h> 5942c1b001SThomas Moestl #include <net/if.h> 6042c1b001SThomas Moestl #include <net/if_arp.h> 6142c1b001SThomas Moestl #include <net/if_dl.h> 6242c1b001SThomas Moestl #include <net/if_media.h> 63fc74a9f9SBrooks Davis #include <net/if_types.h> 6442c1b001SThomas Moestl 6542c1b001SThomas Moestl #include <machine/bus.h> 6642c1b001SThomas Moestl 6742c1b001SThomas Moestl #include <dev/mii/mii.h> 6842c1b001SThomas Moestl #include <dev/mii/miivar.h> 6942c1b001SThomas Moestl 70681f7d03SWarner Losh #include <dev/gem/if_gemreg.h> 71681f7d03SWarner Losh #include <dev/gem/if_gemvar.h> 7242c1b001SThomas Moestl 7342c1b001SThomas Moestl #define TRIES 10000 7442c1b001SThomas Moestl 75e51a25f8SAlfred Perlstein static void gem_start(struct ifnet *); 76e51a25f8SAlfred Perlstein static void gem_stop(struct ifnet *, int); 77e51a25f8SAlfred Perlstein static int gem_ioctl(struct ifnet *, u_long, caddr_t); 78e51a25f8SAlfred Perlstein static void gem_cddma_callback(void *, bus_dma_segment_t *, int, int); 79305f2c06SThomas Moestl static void gem_txdma_callback(void *, bus_dma_segment_t *, int, 80305f2c06SThomas Moestl bus_size_t, int); 81e51a25f8SAlfred Perlstein static void gem_tick(void *); 82e51a25f8SAlfred Perlstein static void gem_watchdog(struct ifnet *); 83e51a25f8SAlfred Perlstein static void gem_init(void *); 84e51a25f8SAlfred Perlstein static void gem_init_regs(struct gem_softc *sc); 85e51a25f8SAlfred Perlstein static int gem_ringsize(int sz); 86e51a25f8SAlfred Perlstein static int gem_meminit(struct gem_softc *); 87305f2c06SThomas Moestl static int gem_load_txmbuf(struct gem_softc *, struct mbuf *); 88e51a25f8SAlfred Perlstein static void gem_mifinit(struct gem_softc *); 89e51a25f8SAlfred Perlstein static int gem_bitwait(struct gem_softc *sc, bus_addr_t r, 90e51a25f8SAlfred Perlstein u_int32_t clr, u_int32_t set); 91e51a25f8SAlfred Perlstein static int gem_reset_rx(struct gem_softc *); 92e51a25f8SAlfred Perlstein static int gem_reset_tx(struct gem_softc *); 93e51a25f8SAlfred Perlstein static int gem_disable_rx(struct gem_softc *); 94e51a25f8SAlfred Perlstein static int gem_disable_tx(struct gem_softc *); 95e51a25f8SAlfred Perlstein static void gem_rxdrain(struct gem_softc *); 96e51a25f8SAlfred Perlstein static int gem_add_rxbuf(struct gem_softc *, int); 97e51a25f8SAlfred Perlstein static void gem_setladrf(struct gem_softc *); 9842c1b001SThomas Moestl 99e51a25f8SAlfred Perlstein struct mbuf *gem_get(struct gem_softc *, int, int); 100e51a25f8SAlfred Perlstein static void gem_eint(struct gem_softc *, u_int); 101e51a25f8SAlfred Perlstein static void gem_rint(struct gem_softc *); 102c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 1030d80b9bdSThomas Moestl static void gem_rint_timeout(void *); 10411e3f060SJake Burkholder #endif 105e51a25f8SAlfred Perlstein static void gem_tint(struct gem_softc *); 10642c1b001SThomas Moestl #ifdef notyet 107e51a25f8SAlfred Perlstein static void gem_power(int, void *); 10842c1b001SThomas Moestl #endif 10942c1b001SThomas Moestl 11042c1b001SThomas Moestl devclass_t gem_devclass; 11142c1b001SThomas Moestl DRIVER_MODULE(miibus, gem, miibus_driver, miibus_devclass, 0, 0); 11242c1b001SThomas Moestl MODULE_DEPEND(gem, miibus, 1, 1, 1); 11342c1b001SThomas Moestl 11442c1b001SThomas Moestl #ifdef GEM_DEBUG 11542c1b001SThomas Moestl #include <sys/ktr.h> 11642c1b001SThomas Moestl #define KTR_GEM KTR_CT2 11742c1b001SThomas Moestl #endif 11842c1b001SThomas Moestl 11918100346SThomas Moestl #define GEM_NSEGS GEM_NTXDESC 12042c1b001SThomas Moestl 12142c1b001SThomas Moestl /* 12242c1b001SThomas Moestl * gem_attach: 12342c1b001SThomas Moestl * 12442c1b001SThomas Moestl * Attach a Gem interface to the system. 12542c1b001SThomas Moestl */ 12642c1b001SThomas Moestl int 12742c1b001SThomas Moestl gem_attach(sc) 12842c1b001SThomas Moestl struct gem_softc *sc; 12942c1b001SThomas Moestl { 130fc74a9f9SBrooks Davis struct ifnet *ifp; 13142c1b001SThomas Moestl struct mii_softc *child; 13242c1b001SThomas Moestl int i, error; 133336cca9eSBenno Rice u_int32_t v; 13442c1b001SThomas Moestl 135fc74a9f9SBrooks Davis ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 136fc74a9f9SBrooks Davis if (ifp == NULL) 137fc74a9f9SBrooks Davis return (ENOSPC); 138fc74a9f9SBrooks Davis 13942c1b001SThomas Moestl /* Make sure the chip is stopped. */ 14042c1b001SThomas Moestl ifp->if_softc = sc; 14142c1b001SThomas Moestl gem_reset(sc); 14242c1b001SThomas Moestl 14342c1b001SThomas Moestl error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 14442c1b001SThomas Moestl BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, GEM_NSEGS, 145f6b1c44dSScott Long BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->sc_pdmatag); 14642c1b001SThomas Moestl if (error) 147fc74a9f9SBrooks Davis goto fail_ifnet; 14842c1b001SThomas Moestl 14942c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 15042c1b001SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MAXBSIZE, 151f6b1c44dSScott Long 1, BUS_SPACE_MAXSIZE_32BIT, BUS_DMA_ALLOCNOW, NULL, NULL, 152305f2c06SThomas Moestl &sc->sc_rdmatag); 15342c1b001SThomas Moestl if (error) 154305f2c06SThomas Moestl goto fail_ptag; 155305f2c06SThomas Moestl 156305f2c06SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, 1, 0, 157305f2c06SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 15818100346SThomas Moestl GEM_TD_BUFSIZE, GEM_NTXDESC, BUS_SPACE_MAXSIZE_32BIT, 159f6b1c44dSScott Long BUS_DMA_ALLOCNOW, NULL, NULL, &sc->sc_tdmatag); 160305f2c06SThomas Moestl if (error) 161305f2c06SThomas Moestl goto fail_rtag; 16242c1b001SThomas Moestl 16342c1b001SThomas Moestl error = bus_dma_tag_create(sc->sc_pdmatag, PAGE_SIZE, 0, 16442c1b001SThomas Moestl BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 16542c1b001SThomas Moestl sizeof(struct gem_control_data), 1, 16642c1b001SThomas Moestl sizeof(struct gem_control_data), BUS_DMA_ALLOCNOW, 167f6b1c44dSScott Long busdma_lock_mutex, &Giant, &sc->sc_cdmatag); 16842c1b001SThomas Moestl if (error) 169305f2c06SThomas Moestl goto fail_ttag; 17042c1b001SThomas Moestl 17142c1b001SThomas Moestl /* 17242c1b001SThomas Moestl * Allocate the control data structures, and create and load the 17342c1b001SThomas Moestl * DMA map for it. 17442c1b001SThomas Moestl */ 17542c1b001SThomas Moestl if ((error = bus_dmamem_alloc(sc->sc_cdmatag, 17642c1b001SThomas Moestl (void **)&sc->sc_control_data, 0, &sc->sc_cddmamap))) { 17742c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to allocate control data," 17842c1b001SThomas Moestl " error = %d\n", error); 179305f2c06SThomas Moestl goto fail_ctag; 18042c1b001SThomas Moestl } 18142c1b001SThomas Moestl 18242c1b001SThomas Moestl sc->sc_cddma = 0; 18342c1b001SThomas Moestl if ((error = bus_dmamap_load(sc->sc_cdmatag, sc->sc_cddmamap, 18442c1b001SThomas Moestl sc->sc_control_data, sizeof(struct gem_control_data), 18542c1b001SThomas Moestl gem_cddma_callback, sc, 0)) != 0 || sc->sc_cddma == 0) { 18642c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to load control data DMA " 18742c1b001SThomas Moestl "map, error = %d\n", error); 188305f2c06SThomas Moestl goto fail_cmem; 18942c1b001SThomas Moestl } 19042c1b001SThomas Moestl 19142c1b001SThomas Moestl /* 19242c1b001SThomas Moestl * Initialize the transmit job descriptors. 19342c1b001SThomas Moestl */ 19442c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txfreeq); 19542c1b001SThomas Moestl STAILQ_INIT(&sc->sc_txdirtyq); 19642c1b001SThomas Moestl 19742c1b001SThomas Moestl /* 19842c1b001SThomas Moestl * Create the transmit buffer DMA maps. 19942c1b001SThomas Moestl */ 20042c1b001SThomas Moestl error = ENOMEM; 20142c1b001SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 20242c1b001SThomas Moestl struct gem_txsoft *txs; 20342c1b001SThomas Moestl 20442c1b001SThomas Moestl txs = &sc->sc_txsoft[i]; 20542c1b001SThomas Moestl txs->txs_mbuf = NULL; 20642c1b001SThomas Moestl txs->txs_ndescs = 0; 207305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_tdmatag, 0, 20842c1b001SThomas Moestl &txs->txs_dmamap)) != 0) { 20942c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to create tx DMA map " 21042c1b001SThomas Moestl "%d, error = %d\n", i, error); 211305f2c06SThomas Moestl goto fail_txd; 21242c1b001SThomas Moestl } 21342c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 21442c1b001SThomas Moestl } 21542c1b001SThomas Moestl 21642c1b001SThomas Moestl /* 21742c1b001SThomas Moestl * Create the receive buffer DMA maps. 21842c1b001SThomas Moestl */ 21942c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 220305f2c06SThomas Moestl if ((error = bus_dmamap_create(sc->sc_rdmatag, 0, 22142c1b001SThomas Moestl &sc->sc_rxsoft[i].rxs_dmamap)) != 0) { 22242c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to create rx DMA map " 22342c1b001SThomas Moestl "%d, error = %d\n", i, error); 224305f2c06SThomas Moestl goto fail_rxd; 22542c1b001SThomas Moestl } 22642c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_mbuf = NULL; 22742c1b001SThomas Moestl } 22842c1b001SThomas Moestl 22942c1b001SThomas Moestl gem_mifinit(sc); 23042c1b001SThomas Moestl 23142c1b001SThomas Moestl if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, gem_mediachange, 23242c1b001SThomas Moestl gem_mediastatus)) != 0) { 23342c1b001SThomas Moestl device_printf(sc->sc_dev, "phy probe failed: %d\n", error); 234305f2c06SThomas Moestl goto fail_rxd; 23542c1b001SThomas Moestl } 23642c1b001SThomas Moestl sc->sc_mii = device_get_softc(sc->sc_miibus); 23742c1b001SThomas Moestl 23842c1b001SThomas Moestl /* 23942c1b001SThomas Moestl * From this point forward, the attachment cannot fail. A failure 24042c1b001SThomas Moestl * before this point releases all resources that may have been 24142c1b001SThomas Moestl * allocated. 24242c1b001SThomas Moestl */ 24342c1b001SThomas Moestl 244336cca9eSBenno Rice /* Get RX FIFO size */ 245336cca9eSBenno Rice sc->sc_rxfifosize = 64 * 246336cca9eSBenno Rice bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_FIFO_SIZE); 247336cca9eSBenno Rice 248336cca9eSBenno Rice /* Get TX FIFO size */ 249336cca9eSBenno Rice v = bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_FIFO_SIZE); 2503a5aee5aSThomas Moestl device_printf(sc->sc_dev, "%ukB RX FIFO, %ukB TX FIFO\n", 2513a5aee5aSThomas Moestl sc->sc_rxfifosize / 1024, v / 16); 25242c1b001SThomas Moestl 25342c1b001SThomas Moestl /* Initialize ifnet structure. */ 25442c1b001SThomas Moestl ifp->if_softc = sc; 2559bf40edeSBrooks Davis if_initname(ifp, device_get_name(sc->sc_dev), 2569bf40edeSBrooks Davis device_get_unit(sc->sc_dev)); 25742c1b001SThomas Moestl ifp->if_mtu = ETHERMTU; 258268f132aSRobert Watson ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | 259268f132aSRobert Watson IFF_NEEDSGIANT; 26042c1b001SThomas Moestl ifp->if_start = gem_start; 26142c1b001SThomas Moestl ifp->if_ioctl = gem_ioctl; 26242c1b001SThomas Moestl ifp->if_watchdog = gem_watchdog; 26342c1b001SThomas Moestl ifp->if_init = gem_init; 26442c1b001SThomas Moestl ifp->if_snd.ifq_maxlen = GEM_TXQUEUELEN; 26542c1b001SThomas Moestl /* 26642c1b001SThomas Moestl * Walk along the list of attached MII devices and 26742c1b001SThomas Moestl * establish an `MII instance' to `phy number' 26842c1b001SThomas Moestl * mapping. We'll use this mapping in media change 26942c1b001SThomas Moestl * requests to determine which phy to use to program 27042c1b001SThomas Moestl * the MIF configuration register. 27142c1b001SThomas Moestl */ 27242c1b001SThomas Moestl for (child = LIST_FIRST(&sc->sc_mii->mii_phys); child != NULL; 27342c1b001SThomas Moestl child = LIST_NEXT(child, mii_list)) { 27442c1b001SThomas Moestl /* 27542c1b001SThomas Moestl * Note: we support just two PHYs: the built-in 27642c1b001SThomas Moestl * internal device and an external on the MII 27742c1b001SThomas Moestl * connector. 27842c1b001SThomas Moestl */ 27942c1b001SThomas Moestl if (child->mii_phy > 1 || child->mii_inst > 1) { 28042c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot accomodate " 28142c1b001SThomas Moestl "MII device %s at phy %d, instance %d\n", 28242c1b001SThomas Moestl device_get_name(child->mii_dev), 28342c1b001SThomas Moestl child->mii_phy, child->mii_inst); 28442c1b001SThomas Moestl continue; 28542c1b001SThomas Moestl } 28642c1b001SThomas Moestl 28742c1b001SThomas Moestl sc->sc_phys[child->mii_inst] = child->mii_phy; 28842c1b001SThomas Moestl } 28942c1b001SThomas Moestl 29042c1b001SThomas Moestl /* 29142c1b001SThomas Moestl * Now select and activate the PHY we will use. 29242c1b001SThomas Moestl * 29342c1b001SThomas Moestl * The order of preference is External (MDI1), 29442c1b001SThomas Moestl * Internal (MDI0), Serial Link (no MII). 29542c1b001SThomas Moestl */ 29642c1b001SThomas Moestl if (sc->sc_phys[1]) { 29742c1b001SThomas Moestl #ifdef GEM_DEBUG 29842c1b001SThomas Moestl printf("using external phy\n"); 29942c1b001SThomas Moestl #endif 30042c1b001SThomas Moestl sc->sc_mif_config |= GEM_MIF_CONFIG_PHY_SEL; 30142c1b001SThomas Moestl } else { 30242c1b001SThomas Moestl #ifdef GEM_DEBUG 30342c1b001SThomas Moestl printf("using internal phy\n"); 30442c1b001SThomas Moestl #endif 30542c1b001SThomas Moestl sc->sc_mif_config &= ~GEM_MIF_CONFIG_PHY_SEL; 30642c1b001SThomas Moestl } 30742c1b001SThomas Moestl bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_MIF_CONFIG, 30842c1b001SThomas Moestl sc->sc_mif_config); 30942c1b001SThomas Moestl /* Attach the interface. */ 310fc74a9f9SBrooks Davis ether_ifattach(ifp, sc->sc_enaddr); 31142c1b001SThomas Moestl 31242c1b001SThomas Moestl #if notyet 31342c1b001SThomas Moestl /* 31442c1b001SThomas Moestl * Add a suspend hook to make sure we come back up after a 31542c1b001SThomas Moestl * resume. 31642c1b001SThomas Moestl */ 31742c1b001SThomas Moestl sc->sc_powerhook = powerhook_establish(gem_power, sc); 31842c1b001SThomas Moestl if (sc->sc_powerhook == NULL) 31942c1b001SThomas Moestl device_printf(sc->sc_dev, "WARNING: unable to establish power " 32042c1b001SThomas Moestl "hook\n"); 32142c1b001SThomas Moestl #endif 32242c1b001SThomas Moestl 32342c1b001SThomas Moestl callout_init(&sc->sc_tick_ch, 0); 324c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 3250d80b9bdSThomas Moestl callout_init(&sc->sc_rx_ch, 0); 326c3d5598aSMarius Strobl #endif 32742c1b001SThomas Moestl return (0); 32842c1b001SThomas Moestl 32942c1b001SThomas Moestl /* 33042c1b001SThomas Moestl * Free any resources we've allocated during the failed attach 33142c1b001SThomas Moestl * attempt. Do this in reverse order and fall through. 33242c1b001SThomas Moestl */ 333305f2c06SThomas Moestl fail_rxd: 33442c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 33542c1b001SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 336305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 33742c1b001SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 33842c1b001SThomas Moestl } 339305f2c06SThomas Moestl fail_txd: 34042c1b001SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 34142c1b001SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 342305f2c06SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 34342c1b001SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 34442c1b001SThomas Moestl } 345305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 346305f2c06SThomas Moestl fail_cmem: 34742c1b001SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 34842c1b001SThomas Moestl sc->sc_cddmamap); 349305f2c06SThomas Moestl fail_ctag: 35042c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 351305f2c06SThomas Moestl fail_ttag: 352305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 353305f2c06SThomas Moestl fail_rtag: 354305f2c06SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 355305f2c06SThomas Moestl fail_ptag: 35642c1b001SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 357fc74a9f9SBrooks Davis fail_ifnet: 358fc74a9f9SBrooks Davis if_free(ifp); 35942c1b001SThomas Moestl return (error); 36042c1b001SThomas Moestl } 36142c1b001SThomas Moestl 362cbbdf236SThomas Moestl void 363cbbdf236SThomas Moestl gem_detach(sc) 364cbbdf236SThomas Moestl struct gem_softc *sc; 365cbbdf236SThomas Moestl { 366fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 367cbbdf236SThomas Moestl int i; 368cbbdf236SThomas Moestl 36925bd46d0SBrooks Davis gem_stop(ifp, 1); 370cbbdf236SThomas Moestl ether_ifdetach(ifp); 371fc74a9f9SBrooks Davis if_free(ifp); 372cbbdf236SThomas Moestl device_delete_child(sc->sc_dev, sc->sc_miibus); 373cbbdf236SThomas Moestl 374cbbdf236SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 375cbbdf236SThomas Moestl if (sc->sc_rxsoft[i].rxs_dmamap != NULL) 376cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_rdmatag, 377cbbdf236SThomas Moestl sc->sc_rxsoft[i].rxs_dmamap); 378cbbdf236SThomas Moestl } 379cbbdf236SThomas Moestl for (i = 0; i < GEM_TXQUEUELEN; i++) { 380cbbdf236SThomas Moestl if (sc->sc_txsoft[i].txs_dmamap != NULL) 381cbbdf236SThomas Moestl bus_dmamap_destroy(sc->sc_tdmatag, 382cbbdf236SThomas Moestl sc->sc_txsoft[i].txs_dmamap); 383cbbdf236SThomas Moestl } 384b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 385b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTWRITE); 386cbbdf236SThomas Moestl bus_dmamap_unload(sc->sc_cdmatag, sc->sc_cddmamap); 387cbbdf236SThomas Moestl bus_dmamem_free(sc->sc_cdmatag, sc->sc_control_data, 388cbbdf236SThomas Moestl sc->sc_cddmamap); 389cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_cdmatag); 390cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_tdmatag); 391cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_rdmatag); 392cbbdf236SThomas Moestl bus_dma_tag_destroy(sc->sc_pdmatag); 393cbbdf236SThomas Moestl } 394cbbdf236SThomas Moestl 395cbbdf236SThomas Moestl void 396cbbdf236SThomas Moestl gem_suspend(sc) 397cbbdf236SThomas Moestl struct gem_softc *sc; 398cbbdf236SThomas Moestl { 399fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 400cbbdf236SThomas Moestl 401cbbdf236SThomas Moestl gem_stop(ifp, 0); 402cbbdf236SThomas Moestl } 403cbbdf236SThomas Moestl 404cbbdf236SThomas Moestl void 405cbbdf236SThomas Moestl gem_resume(sc) 406cbbdf236SThomas Moestl struct gem_softc *sc; 407cbbdf236SThomas Moestl { 408fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 409cbbdf236SThomas Moestl 410cbbdf236SThomas Moestl if (ifp->if_flags & IFF_UP) 411cbbdf236SThomas Moestl gem_init(ifp); 412cbbdf236SThomas Moestl } 413cbbdf236SThomas Moestl 41442c1b001SThomas Moestl static void 41542c1b001SThomas Moestl gem_cddma_callback(xsc, segs, nsegs, error) 41642c1b001SThomas Moestl void *xsc; 41742c1b001SThomas Moestl bus_dma_segment_t *segs; 41842c1b001SThomas Moestl int nsegs; 41942c1b001SThomas Moestl int error; 42042c1b001SThomas Moestl { 42142c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)xsc; 42242c1b001SThomas Moestl 42342c1b001SThomas Moestl if (error != 0) 42442c1b001SThomas Moestl return; 42542c1b001SThomas Moestl if (nsegs != 1) { 42642c1b001SThomas Moestl /* can't happen... */ 42742c1b001SThomas Moestl panic("gem_cddma_callback: bad control buffer segment count"); 42842c1b001SThomas Moestl } 42942c1b001SThomas Moestl sc->sc_cddma = segs[0].ds_addr; 43042c1b001SThomas Moestl } 43142c1b001SThomas Moestl 43242c1b001SThomas Moestl static void 433305f2c06SThomas Moestl gem_txdma_callback(xsc, segs, nsegs, totsz, error) 43442c1b001SThomas Moestl void *xsc; 43542c1b001SThomas Moestl bus_dma_segment_t *segs; 43642c1b001SThomas Moestl int nsegs; 437305f2c06SThomas Moestl bus_size_t totsz; 43842c1b001SThomas Moestl int error; 43942c1b001SThomas Moestl { 440305f2c06SThomas Moestl struct gem_txdma *txd = (struct gem_txdma *)xsc; 441305f2c06SThomas Moestl struct gem_softc *sc = txd->txd_sc; 442305f2c06SThomas Moestl struct gem_txsoft *txs = txd->txd_txs; 443305f2c06SThomas Moestl bus_size_t len = 0; 444305f2c06SThomas Moestl uint64_t flags = 0; 445305f2c06SThomas Moestl int seg, nexttx; 44642c1b001SThomas Moestl 44742c1b001SThomas Moestl if (error != 0) 44842c1b001SThomas Moestl return; 449305f2c06SThomas Moestl /* 450305f2c06SThomas Moestl * Ensure we have enough descriptors free to describe 451305f2c06SThomas Moestl * the packet. Note, we always reserve one descriptor 452305f2c06SThomas Moestl * at the end of the ring as a termination point, to 453305f2c06SThomas Moestl * prevent wrap-around. 454305f2c06SThomas Moestl */ 455305f2c06SThomas Moestl if (nsegs > sc->sc_txfree - 1) { 456305f2c06SThomas Moestl txs->txs_ndescs = -1; 457305f2c06SThomas Moestl return; 458305f2c06SThomas Moestl } 459305f2c06SThomas Moestl txs->txs_ndescs = nsegs; 46042c1b001SThomas Moestl 461305f2c06SThomas Moestl nexttx = txs->txs_firstdesc; 46242c1b001SThomas Moestl /* 46342c1b001SThomas Moestl * Initialize the transmit descriptors. 46442c1b001SThomas Moestl */ 46542c1b001SThomas Moestl for (seg = 0; seg < nsegs; 466305f2c06SThomas Moestl seg++, nexttx = GEM_NEXTTX(nexttx)) { 46718100346SThomas Moestl #ifdef GEM_DEBUG 46842c1b001SThomas Moestl CTR5(KTR_GEM, "txdma_cb: mapping seg %d (txd %d), len " 469305f2c06SThomas Moestl "%lx, addr %#lx (%#lx)", seg, nexttx, 47042c1b001SThomas Moestl segs[seg].ds_len, segs[seg].ds_addr, 471305f2c06SThomas Moestl GEM_DMA_WRITE(sc, segs[seg].ds_addr)); 47218100346SThomas Moestl #endif 473305f2c06SThomas Moestl 474305f2c06SThomas Moestl if (segs[seg].ds_len == 0) 475305f2c06SThomas Moestl continue; 476305f2c06SThomas Moestl sc->sc_txdescs[nexttx].gd_addr = 477305f2c06SThomas Moestl GEM_DMA_WRITE(sc, segs[seg].ds_addr); 478305f2c06SThomas Moestl KASSERT(segs[seg].ds_len < GEM_TD_BUFSIZE, 479305f2c06SThomas Moestl ("gem_txdma_callback: segment size too large!")); 48042c1b001SThomas Moestl flags = segs[seg].ds_len & GEM_TD_BUFSIZE; 481305f2c06SThomas Moestl if (len == 0) { 48218100346SThomas Moestl #ifdef GEM_DEBUG 48342c1b001SThomas Moestl CTR2(KTR_GEM, "txdma_cb: start of packet at seg %d, " 484305f2c06SThomas Moestl "tx %d", seg, nexttx); 48518100346SThomas Moestl #endif 48642c1b001SThomas Moestl flags |= GEM_TD_START_OF_PACKET; 487305f2c06SThomas Moestl if (++sc->sc_txwin > GEM_NTXSEGS * 2 / 3) { 488305f2c06SThomas Moestl sc->sc_txwin = 0; 489336cca9eSBenno Rice flags |= GEM_TD_INTERRUPT_ME; 490336cca9eSBenno Rice } 49142c1b001SThomas Moestl } 492305f2c06SThomas Moestl if (len + segs[seg].ds_len == totsz) { 49318100346SThomas Moestl #ifdef GEM_DEBUG 49442c1b001SThomas Moestl CTR2(KTR_GEM, "txdma_cb: end of packet at seg %d, " 495305f2c06SThomas Moestl "tx %d", seg, nexttx); 49618100346SThomas Moestl #endif 49742c1b001SThomas Moestl flags |= GEM_TD_END_OF_PACKET; 49842c1b001SThomas Moestl } 499305f2c06SThomas Moestl sc->sc_txdescs[nexttx].gd_flags = GEM_DMA_WRITE(sc, flags); 500305f2c06SThomas Moestl txs->txs_lastdesc = nexttx; 501305f2c06SThomas Moestl len += segs[seg].ds_len; 50242c1b001SThomas Moestl } 503305f2c06SThomas Moestl KASSERT((flags & GEM_TD_END_OF_PACKET) != 0, 504305f2c06SThomas Moestl ("gem_txdma_callback: missed end of packet!")); 50542c1b001SThomas Moestl } 50642c1b001SThomas Moestl 50742c1b001SThomas Moestl static void 50842c1b001SThomas Moestl gem_tick(arg) 50942c1b001SThomas Moestl void *arg; 51042c1b001SThomas Moestl { 51142c1b001SThomas Moestl struct gem_softc *sc = arg; 51242c1b001SThomas Moestl int s; 51342c1b001SThomas Moestl 51442c1b001SThomas Moestl s = splnet(); 51542c1b001SThomas Moestl mii_tick(sc->sc_mii); 51642c1b001SThomas Moestl splx(s); 51742c1b001SThomas Moestl 51842c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 51942c1b001SThomas Moestl } 52042c1b001SThomas Moestl 52142c1b001SThomas Moestl static int 52242c1b001SThomas Moestl gem_bitwait(sc, r, clr, set) 52342c1b001SThomas Moestl struct gem_softc *sc; 52442c1b001SThomas Moestl bus_addr_t r; 52542c1b001SThomas Moestl u_int32_t clr; 52642c1b001SThomas Moestl u_int32_t set; 52742c1b001SThomas Moestl { 52842c1b001SThomas Moestl int i; 52942c1b001SThomas Moestl u_int32_t reg; 53042c1b001SThomas Moestl 53142c1b001SThomas Moestl for (i = TRIES; i--; DELAY(100)) { 53242c1b001SThomas Moestl reg = bus_space_read_4(sc->sc_bustag, sc->sc_h, r); 53342c1b001SThomas Moestl if ((r & clr) == 0 && (r & set) == set) 53442c1b001SThomas Moestl return (1); 53542c1b001SThomas Moestl } 53642c1b001SThomas Moestl return (0); 53742c1b001SThomas Moestl } 53842c1b001SThomas Moestl 53942c1b001SThomas Moestl void 54042c1b001SThomas Moestl gem_reset(sc) 54142c1b001SThomas Moestl struct gem_softc *sc; 54242c1b001SThomas Moestl { 54342c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 54442c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 54542c1b001SThomas Moestl int s; 54642c1b001SThomas Moestl 54742c1b001SThomas Moestl s = splnet(); 54818100346SThomas Moestl #ifdef GEM_DEBUG 54942c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_reset", device_get_name(sc->sc_dev)); 55018100346SThomas Moestl #endif 55142c1b001SThomas Moestl gem_reset_rx(sc); 55242c1b001SThomas Moestl gem_reset_tx(sc); 55342c1b001SThomas Moestl 55442c1b001SThomas Moestl /* Do a full reset */ 55542c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX); 55642c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_RX | GEM_RESET_TX, 0)) 55742c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset device\n"); 55842c1b001SThomas Moestl splx(s); 55942c1b001SThomas Moestl } 56042c1b001SThomas Moestl 56142c1b001SThomas Moestl 56242c1b001SThomas Moestl /* 56342c1b001SThomas Moestl * gem_rxdrain: 56442c1b001SThomas Moestl * 56542c1b001SThomas Moestl * Drain the receive queue. 56642c1b001SThomas Moestl */ 56742c1b001SThomas Moestl static void 56842c1b001SThomas Moestl gem_rxdrain(sc) 56942c1b001SThomas Moestl struct gem_softc *sc; 57042c1b001SThomas Moestl { 57142c1b001SThomas Moestl struct gem_rxsoft *rxs; 57242c1b001SThomas Moestl int i; 57342c1b001SThomas Moestl 57442c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 57542c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 57642c1b001SThomas Moestl if (rxs->rxs_mbuf != NULL) { 577b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 578b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 579305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 58042c1b001SThomas Moestl m_freem(rxs->rxs_mbuf); 58142c1b001SThomas Moestl rxs->rxs_mbuf = NULL; 58242c1b001SThomas Moestl } 58342c1b001SThomas Moestl } 58442c1b001SThomas Moestl } 58542c1b001SThomas Moestl 58642c1b001SThomas Moestl /* 58742c1b001SThomas Moestl * Reset the whole thing. 58842c1b001SThomas Moestl */ 58942c1b001SThomas Moestl static void 59042c1b001SThomas Moestl gem_stop(ifp, disable) 59142c1b001SThomas Moestl struct ifnet *ifp; 59242c1b001SThomas Moestl int disable; 59342c1b001SThomas Moestl { 59442c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 59542c1b001SThomas Moestl struct gem_txsoft *txs; 59642c1b001SThomas Moestl 59718100346SThomas Moestl #ifdef GEM_DEBUG 59842c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_stop", device_get_name(sc->sc_dev)); 59918100346SThomas Moestl #endif 60042c1b001SThomas Moestl 60142c1b001SThomas Moestl callout_stop(&sc->sc_tick_ch); 60242c1b001SThomas Moestl 60342c1b001SThomas Moestl /* XXX - Should we reset these instead? */ 60442c1b001SThomas Moestl gem_disable_tx(sc); 60542c1b001SThomas Moestl gem_disable_rx(sc); 60642c1b001SThomas Moestl 60742c1b001SThomas Moestl /* 60842c1b001SThomas Moestl * Release any queued transmit buffers. 60942c1b001SThomas Moestl */ 61042c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 61142c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 61242c1b001SThomas Moestl if (txs->txs_ndescs != 0) { 613b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 614b2d59f42SThomas Moestl BUS_DMASYNC_POSTWRITE); 615305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 61642c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 61742c1b001SThomas Moestl m_freem(txs->txs_mbuf); 61842c1b001SThomas Moestl txs->txs_mbuf = NULL; 61942c1b001SThomas Moestl } 62042c1b001SThomas Moestl } 62142c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 62242c1b001SThomas Moestl } 62342c1b001SThomas Moestl 62442c1b001SThomas Moestl if (disable) 62542c1b001SThomas Moestl gem_rxdrain(sc); 62642c1b001SThomas Moestl 62742c1b001SThomas Moestl /* 62842c1b001SThomas Moestl * Mark the interface down and cancel the watchdog timer. 62942c1b001SThomas Moestl */ 63042c1b001SThomas Moestl ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 63142c1b001SThomas Moestl ifp->if_timer = 0; 63242c1b001SThomas Moestl } 63342c1b001SThomas Moestl 63442c1b001SThomas Moestl /* 63542c1b001SThomas Moestl * Reset the receiver 63642c1b001SThomas Moestl */ 63742c1b001SThomas Moestl int 63842c1b001SThomas Moestl gem_reset_rx(sc) 63942c1b001SThomas Moestl struct gem_softc *sc; 64042c1b001SThomas Moestl { 64142c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 64242c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 64342c1b001SThomas Moestl 64442c1b001SThomas Moestl /* 64542c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 64642c1b001SThomas Moestl * disable DMA first. 64742c1b001SThomas Moestl */ 64842c1b001SThomas Moestl gem_disable_rx(sc); 64942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RX_CONFIG, 0); 65042c1b001SThomas Moestl /* Wait till it finishes */ 65142c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RX_CONFIG, 1, 0)) 65242c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot disable read dma\n"); 65342c1b001SThomas Moestl 65442c1b001SThomas Moestl /* Wait 5ms extra. */ 65542c1b001SThomas Moestl DELAY(5000); 65642c1b001SThomas Moestl 65742c1b001SThomas Moestl /* Finally, reset the ERX */ 65842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RESET, GEM_RESET_RX); 65942c1b001SThomas Moestl /* Wait till it finishes */ 66042c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 66142c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 66242c1b001SThomas Moestl return (1); 66342c1b001SThomas Moestl } 66442c1b001SThomas Moestl return (0); 66542c1b001SThomas Moestl } 66642c1b001SThomas Moestl 66742c1b001SThomas Moestl 66842c1b001SThomas Moestl /* 66942c1b001SThomas Moestl * Reset the transmitter 67042c1b001SThomas Moestl */ 67142c1b001SThomas Moestl static int 67242c1b001SThomas Moestl gem_reset_tx(sc) 67342c1b001SThomas Moestl struct gem_softc *sc; 67442c1b001SThomas Moestl { 67542c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 67642c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 67742c1b001SThomas Moestl int i; 67842c1b001SThomas Moestl 67942c1b001SThomas Moestl /* 68042c1b001SThomas Moestl * Resetting while DMA is in progress can cause a bus hang, so we 68142c1b001SThomas Moestl * disable DMA first. 68242c1b001SThomas Moestl */ 68342c1b001SThomas Moestl gem_disable_tx(sc); 68442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_TX_CONFIG, 0); 68542c1b001SThomas Moestl /* Wait till it finishes */ 68642c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_TX_CONFIG, 1, 0)) 68742c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot disable read dma\n"); 68842c1b001SThomas Moestl 68942c1b001SThomas Moestl /* Wait 5ms extra. */ 69042c1b001SThomas Moestl DELAY(5000); 69142c1b001SThomas Moestl 69242c1b001SThomas Moestl /* Finally, reset the ETX */ 69342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RESET, GEM_RESET_TX); 69442c1b001SThomas Moestl /* Wait till it finishes */ 69542c1b001SThomas Moestl for (i = TRIES; i--; DELAY(100)) 69642c1b001SThomas Moestl if ((bus_space_read_4(t, h, GEM_RESET) & GEM_RESET_TX) == 0) 69742c1b001SThomas Moestl break; 69842c1b001SThomas Moestl if (!gem_bitwait(sc, GEM_RESET, GEM_RESET_TX, 0)) { 69942c1b001SThomas Moestl device_printf(sc->sc_dev, "cannot reset receiver\n"); 70042c1b001SThomas Moestl return (1); 70142c1b001SThomas Moestl } 70242c1b001SThomas Moestl return (0); 70342c1b001SThomas Moestl } 70442c1b001SThomas Moestl 70542c1b001SThomas Moestl /* 70642c1b001SThomas Moestl * disable receiver. 70742c1b001SThomas Moestl */ 70842c1b001SThomas Moestl static int 70942c1b001SThomas Moestl gem_disable_rx(sc) 71042c1b001SThomas Moestl struct gem_softc *sc; 71142c1b001SThomas Moestl { 71242c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 71342c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 71442c1b001SThomas Moestl u_int32_t cfg; 71542c1b001SThomas Moestl 71642c1b001SThomas Moestl /* Flip the enable bit */ 71742c1b001SThomas Moestl cfg = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 71842c1b001SThomas Moestl cfg &= ~GEM_MAC_RX_ENABLE; 71942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, cfg); 72042c1b001SThomas Moestl 72142c1b001SThomas Moestl /* Wait for it to finish */ 72242c1b001SThomas Moestl return (gem_bitwait(sc, GEM_MAC_RX_CONFIG, GEM_MAC_RX_ENABLE, 0)); 72342c1b001SThomas Moestl } 72442c1b001SThomas Moestl 72542c1b001SThomas Moestl /* 72642c1b001SThomas Moestl * disable transmitter. 72742c1b001SThomas Moestl */ 72842c1b001SThomas Moestl static int 72942c1b001SThomas Moestl gem_disable_tx(sc) 73042c1b001SThomas Moestl struct gem_softc *sc; 73142c1b001SThomas Moestl { 73242c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 73342c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 73442c1b001SThomas Moestl u_int32_t cfg; 73542c1b001SThomas Moestl 73642c1b001SThomas Moestl /* Flip the enable bit */ 73742c1b001SThomas Moestl cfg = bus_space_read_4(t, h, GEM_MAC_TX_CONFIG); 73842c1b001SThomas Moestl cfg &= ~GEM_MAC_TX_ENABLE; 73942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_TX_CONFIG, cfg); 74042c1b001SThomas Moestl 74142c1b001SThomas Moestl /* Wait for it to finish */ 74242c1b001SThomas Moestl return (gem_bitwait(sc, GEM_MAC_TX_CONFIG, GEM_MAC_TX_ENABLE, 0)); 74342c1b001SThomas Moestl } 74442c1b001SThomas Moestl 74542c1b001SThomas Moestl /* 74642c1b001SThomas Moestl * Initialize interface. 74742c1b001SThomas Moestl */ 74842c1b001SThomas Moestl static int 74942c1b001SThomas Moestl gem_meminit(sc) 75042c1b001SThomas Moestl struct gem_softc *sc; 75142c1b001SThomas Moestl { 75242c1b001SThomas Moestl struct gem_rxsoft *rxs; 75342c1b001SThomas Moestl int i, error; 75442c1b001SThomas Moestl 75542c1b001SThomas Moestl /* 75642c1b001SThomas Moestl * Initialize the transmit descriptor ring. 75742c1b001SThomas Moestl */ 75842c1b001SThomas Moestl memset((void *)sc->sc_txdescs, 0, sizeof(sc->sc_txdescs)); 75942c1b001SThomas Moestl for (i = 0; i < GEM_NTXDESC; i++) { 76042c1b001SThomas Moestl sc->sc_txdescs[i].gd_flags = 0; 76142c1b001SThomas Moestl sc->sc_txdescs[i].gd_addr = 0; 76242c1b001SThomas Moestl } 763305f2c06SThomas Moestl sc->sc_txfree = GEM_MAXTXFREE; 76442c1b001SThomas Moestl sc->sc_txnext = 0; 765336cca9eSBenno Rice sc->sc_txwin = 0; 76642c1b001SThomas Moestl 76742c1b001SThomas Moestl /* 76842c1b001SThomas Moestl * Initialize the receive descriptor and receive job 76942c1b001SThomas Moestl * descriptor rings. 77042c1b001SThomas Moestl */ 77142c1b001SThomas Moestl for (i = 0; i < GEM_NRXDESC; i++) { 77242c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 77342c1b001SThomas Moestl if (rxs->rxs_mbuf == NULL) { 77442c1b001SThomas Moestl if ((error = gem_add_rxbuf(sc, i)) != 0) { 77542c1b001SThomas Moestl device_printf(sc->sc_dev, "unable to " 77642c1b001SThomas Moestl "allocate or map rx buffer %d, error = " 77742c1b001SThomas Moestl "%d\n", i, error); 77842c1b001SThomas Moestl /* 77942c1b001SThomas Moestl * XXX Should attempt to run with fewer receive 78042c1b001SThomas Moestl * XXX buffers instead of just failing. 78142c1b001SThomas Moestl */ 78242c1b001SThomas Moestl gem_rxdrain(sc); 78342c1b001SThomas Moestl return (1); 78442c1b001SThomas Moestl } 78542c1b001SThomas Moestl } else 78642c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 78742c1b001SThomas Moestl } 78842c1b001SThomas Moestl sc->sc_rxptr = 0; 789b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 790b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREREAD); 79142c1b001SThomas Moestl 79242c1b001SThomas Moestl return (0); 79342c1b001SThomas Moestl } 79442c1b001SThomas Moestl 79542c1b001SThomas Moestl static int 79642c1b001SThomas Moestl gem_ringsize(sz) 79742c1b001SThomas Moestl int sz; 79842c1b001SThomas Moestl { 79942c1b001SThomas Moestl int v = 0; 80042c1b001SThomas Moestl 80142c1b001SThomas Moestl switch (sz) { 80242c1b001SThomas Moestl case 32: 80342c1b001SThomas Moestl v = GEM_RING_SZ_32; 80442c1b001SThomas Moestl break; 80542c1b001SThomas Moestl case 64: 80642c1b001SThomas Moestl v = GEM_RING_SZ_64; 80742c1b001SThomas Moestl break; 80842c1b001SThomas Moestl case 128: 80942c1b001SThomas Moestl v = GEM_RING_SZ_128; 81042c1b001SThomas Moestl break; 81142c1b001SThomas Moestl case 256: 81242c1b001SThomas Moestl v = GEM_RING_SZ_256; 81342c1b001SThomas Moestl break; 81442c1b001SThomas Moestl case 512: 81542c1b001SThomas Moestl v = GEM_RING_SZ_512; 81642c1b001SThomas Moestl break; 81742c1b001SThomas Moestl case 1024: 81842c1b001SThomas Moestl v = GEM_RING_SZ_1024; 81942c1b001SThomas Moestl break; 82042c1b001SThomas Moestl case 2048: 82142c1b001SThomas Moestl v = GEM_RING_SZ_2048; 82242c1b001SThomas Moestl break; 82342c1b001SThomas Moestl case 4096: 82442c1b001SThomas Moestl v = GEM_RING_SZ_4096; 82542c1b001SThomas Moestl break; 82642c1b001SThomas Moestl case 8192: 82742c1b001SThomas Moestl v = GEM_RING_SZ_8192; 82842c1b001SThomas Moestl break; 82942c1b001SThomas Moestl default: 83042c1b001SThomas Moestl printf("gem: invalid Receive Descriptor ring size\n"); 83142c1b001SThomas Moestl break; 83242c1b001SThomas Moestl } 83342c1b001SThomas Moestl return (v); 83442c1b001SThomas Moestl } 83542c1b001SThomas Moestl 83642c1b001SThomas Moestl /* 83742c1b001SThomas Moestl * Initialization of interface; set up initialization block 83842c1b001SThomas Moestl * and transmit/receive descriptor rings. 83942c1b001SThomas Moestl */ 84042c1b001SThomas Moestl static void 84142c1b001SThomas Moestl gem_init(xsc) 84242c1b001SThomas Moestl void *xsc; 84342c1b001SThomas Moestl { 84442c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)xsc; 845fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 84642c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 84742c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 84842c1b001SThomas Moestl int s; 84942c1b001SThomas Moestl u_int32_t v; 85042c1b001SThomas Moestl 85142c1b001SThomas Moestl s = splnet(); 85242c1b001SThomas Moestl 85318100346SThomas Moestl #ifdef GEM_DEBUG 85442c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_init: calling stop", device_get_name(sc->sc_dev)); 85518100346SThomas Moestl #endif 85642c1b001SThomas Moestl /* 85742c1b001SThomas Moestl * Initialization sequence. The numbered steps below correspond 85842c1b001SThomas Moestl * to the sequence outlined in section 6.3.5.1 in the Ethernet 85942c1b001SThomas Moestl * Channel Engine manual (part of the PCIO manual). 86042c1b001SThomas Moestl * See also the STP2002-STQ document from Sun Microsystems. 86142c1b001SThomas Moestl */ 86242c1b001SThomas Moestl 86342c1b001SThomas Moestl /* step 1 & 2. Reset the Ethernet Channel */ 864fc74a9f9SBrooks Davis gem_stop(sc->sc_ifp, 0); 86542c1b001SThomas Moestl gem_reset(sc); 86618100346SThomas Moestl #ifdef GEM_DEBUG 86742c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_init: restarting", device_get_name(sc->sc_dev)); 86818100346SThomas Moestl #endif 86942c1b001SThomas Moestl 87042c1b001SThomas Moestl /* Re-initialize the MIF */ 87142c1b001SThomas Moestl gem_mifinit(sc); 87242c1b001SThomas Moestl 87342c1b001SThomas Moestl /* step 3. Setup data structures in host memory */ 87442c1b001SThomas Moestl gem_meminit(sc); 87542c1b001SThomas Moestl 87642c1b001SThomas Moestl /* step 4. TX MAC registers & counters */ 87742c1b001SThomas Moestl gem_init_regs(sc); 87842c1b001SThomas Moestl /* XXX: VLAN code from NetBSD temporarily removed. */ 87942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME, 88042c1b001SThomas Moestl (ETHER_MAX_LEN + sizeof(struct ether_header)) | (0x2000<<16)); 88142c1b001SThomas Moestl 88242c1b001SThomas Moestl /* step 5. RX MAC registers & counters */ 88342c1b001SThomas Moestl gem_setladrf(sc); 88442c1b001SThomas Moestl 88542c1b001SThomas Moestl /* step 6 & 7. Program Descriptor Ring Base Addresses */ 88642c1b001SThomas Moestl /* NOTE: we use only 32-bit DMA addresses here. */ 88742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_TX_RING_PTR_HI, 0); 88842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_TX_RING_PTR_LO, GEM_CDTXADDR(sc, 0)); 88942c1b001SThomas Moestl 89042c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RX_RING_PTR_HI, 0); 89142c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RX_RING_PTR_LO, GEM_CDRXADDR(sc, 0)); 89218100346SThomas Moestl #ifdef GEM_DEBUG 89342c1b001SThomas Moestl CTR3(KTR_GEM, "loading rx ring %lx, tx ring %lx, cddma %lx", 89442c1b001SThomas Moestl GEM_CDRXADDR(sc, 0), GEM_CDTXADDR(sc, 0), sc->sc_cddma); 89518100346SThomas Moestl #endif 89642c1b001SThomas Moestl 89742c1b001SThomas Moestl /* step 8. Global Configuration & Interrupt Mask */ 89842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_INTMASK, 89942c1b001SThomas Moestl ~(GEM_INTR_TX_INTME| 90042c1b001SThomas Moestl GEM_INTR_TX_EMPTY| 90142c1b001SThomas Moestl GEM_INTR_RX_DONE|GEM_INTR_RX_NOBUF| 90242c1b001SThomas Moestl GEM_INTR_RX_TAG_ERR|GEM_INTR_PCS| 90342c1b001SThomas Moestl GEM_INTR_MAC_CONTROL|GEM_INTR_MIF| 90442c1b001SThomas Moestl GEM_INTR_BERR)); 905336cca9eSBenno Rice bus_space_write_4(t, h, GEM_MAC_RX_MASK, 906336cca9eSBenno Rice GEM_MAC_RX_DONE|GEM_MAC_RX_FRAME_CNT); 90742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_TX_MASK, 0xffff); /* XXXX */ 90842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_CONTROL_MASK, 0); /* XXXX */ 90942c1b001SThomas Moestl 91042c1b001SThomas Moestl /* step 9. ETX Configuration: use mostly default values */ 91142c1b001SThomas Moestl 91242c1b001SThomas Moestl /* Enable DMA */ 91342c1b001SThomas Moestl v = gem_ringsize(GEM_NTXDESC /*XXX*/); 91442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_TX_CONFIG, 91542c1b001SThomas Moestl v|GEM_TX_CONFIG_TXDMA_EN| 91642c1b001SThomas Moestl ((0x400<<10)&GEM_TX_CONFIG_TXFIFO_TH)); 91742c1b001SThomas Moestl 91842c1b001SThomas Moestl /* step 10. ERX Configuration */ 91942c1b001SThomas Moestl 92042c1b001SThomas Moestl /* Encode Receive Descriptor ring size: four possible values */ 92142c1b001SThomas Moestl v = gem_ringsize(GEM_NRXDESC /*XXX*/); 92242c1b001SThomas Moestl 92342c1b001SThomas Moestl /* Enable DMA */ 92442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RX_CONFIG, 92542c1b001SThomas Moestl v|(GEM_THRSH_1024<<GEM_RX_CONFIG_FIFO_THRS_SHIFT)| 92642c1b001SThomas Moestl (2<<GEM_RX_CONFIG_FBOFF_SHFT)|GEM_RX_CONFIG_RXDMA_EN| 92742c1b001SThomas Moestl (0<<GEM_RX_CONFIG_CXM_START_SHFT)); 92842c1b001SThomas Moestl /* 929336cca9eSBenno Rice * The following value is for an OFF Threshold of about 3/4 full 930336cca9eSBenno Rice * and an ON Threshold of 1/4 full. 93142c1b001SThomas Moestl */ 932336cca9eSBenno Rice bus_space_write_4(t, h, GEM_RX_PAUSE_THRESH, 933336cca9eSBenno Rice (3 * sc->sc_rxfifosize / 256) | 934336cca9eSBenno Rice ( (sc->sc_rxfifosize / 256) << 12)); 935336cca9eSBenno Rice bus_space_write_4(t, h, GEM_RX_BLANKING, (6<<12)|6); 93642c1b001SThomas Moestl 93742c1b001SThomas Moestl /* step 11. Configure Media */ 938336cca9eSBenno Rice mii_mediachg(sc->sc_mii); 93942c1b001SThomas Moestl 94042c1b001SThomas Moestl /* step 12. RX_MAC Configuration Register */ 94142c1b001SThomas Moestl v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 94242c1b001SThomas Moestl v |= GEM_MAC_RX_ENABLE; 94342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v); 94442c1b001SThomas Moestl 94542c1b001SThomas Moestl /* step 14. Issue Transmit Pending command */ 94642c1b001SThomas Moestl 94742c1b001SThomas Moestl /* step 15. Give the reciever a swift kick */ 94842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_RX_KICK, GEM_NRXDESC-4); 94942c1b001SThomas Moestl 95042c1b001SThomas Moestl /* Start the one second timer. */ 95142c1b001SThomas Moestl callout_reset(&sc->sc_tick_ch, hz, gem_tick, sc); 95242c1b001SThomas Moestl 95342c1b001SThomas Moestl ifp->if_flags |= IFF_RUNNING; 95442c1b001SThomas Moestl ifp->if_flags &= ~IFF_OACTIVE; 95542c1b001SThomas Moestl ifp->if_timer = 0; 956336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 95742c1b001SThomas Moestl splx(s); 95842c1b001SThomas Moestl } 95942c1b001SThomas Moestl 96042c1b001SThomas Moestl static int 961305f2c06SThomas Moestl gem_load_txmbuf(sc, m0) 96242c1b001SThomas Moestl struct gem_softc *sc; 96342c1b001SThomas Moestl struct mbuf *m0; 96442c1b001SThomas Moestl { 96542c1b001SThomas Moestl struct gem_txdma txd; 96642c1b001SThomas Moestl struct gem_txsoft *txs; 967305f2c06SThomas Moestl int error; 96842c1b001SThomas Moestl 96942c1b001SThomas Moestl /* Get a work queue entry. */ 97042c1b001SThomas Moestl if ((txs = STAILQ_FIRST(&sc->sc_txfreeq)) == NULL) { 971305f2c06SThomas Moestl /* Ran out of descriptors. */ 972305f2c06SThomas Moestl return (-1); 973305f2c06SThomas Moestl } 974305f2c06SThomas Moestl txd.txd_sc = sc; 975305f2c06SThomas Moestl txd.txd_txs = txs; 976305f2c06SThomas Moestl txs->txs_firstdesc = sc->sc_txnext; 977305f2c06SThomas Moestl error = bus_dmamap_load_mbuf(sc->sc_tdmatag, txs->txs_dmamap, m0, 978305f2c06SThomas Moestl gem_txdma_callback, &txd, BUS_DMA_NOWAIT); 979305f2c06SThomas Moestl if (error != 0) 980305f2c06SThomas Moestl goto fail; 981305f2c06SThomas Moestl if (txs->txs_ndescs == -1) { 98242c1b001SThomas Moestl error = -1; 98342c1b001SThomas Moestl goto fail; 98442c1b001SThomas Moestl } 985305f2c06SThomas Moestl 98642c1b001SThomas Moestl /* Sync the DMA map. */ 987305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 98842c1b001SThomas Moestl BUS_DMASYNC_PREWRITE); 989305f2c06SThomas Moestl 99018100346SThomas Moestl #ifdef GEM_DEBUG 99142c1b001SThomas Moestl CTR3(KTR_GEM, "load_mbuf: setting firstdesc=%d, lastdesc=%d, " 99242c1b001SThomas Moestl "ndescs=%d", txs->txs_firstdesc, txs->txs_lastdesc, 99342c1b001SThomas Moestl txs->txs_ndescs); 99418100346SThomas Moestl #endif 99542c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txfreeq, txs_q); 996305f2c06SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txdirtyq, txs, txs_q); 997c3d5598aSMarius Strobl txs->txs_mbuf = m0; 998305f2c06SThomas Moestl 999305f2c06SThomas Moestl sc->sc_txnext = GEM_NEXTTX(txs->txs_lastdesc); 1000305f2c06SThomas Moestl sc->sc_txfree -= txs->txs_ndescs; 100142c1b001SThomas Moestl return (0); 100242c1b001SThomas Moestl 100342c1b001SThomas Moestl fail: 100418100346SThomas Moestl #ifdef GEM_DEBUG 1005305f2c06SThomas Moestl CTR1(KTR_GEM, "gem_load_txmbuf failed (%d)", error); 100618100346SThomas Moestl #endif 1007305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 100842c1b001SThomas Moestl return (error); 100942c1b001SThomas Moestl } 101042c1b001SThomas Moestl 101142c1b001SThomas Moestl static void 101242c1b001SThomas Moestl gem_init_regs(sc) 101342c1b001SThomas Moestl struct gem_softc *sc; 101442c1b001SThomas Moestl { 101542c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 101642c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 1017fc74a9f9SBrooks Davis const u_char *laddr = IFP2ENADDR(sc->sc_ifp); 1018336cca9eSBenno Rice u_int32_t v; 101942c1b001SThomas Moestl 102042c1b001SThomas Moestl /* These regs are not cleared on reset */ 102142c1b001SThomas Moestl if (!sc->sc_inited) { 102242c1b001SThomas Moestl 102342c1b001SThomas Moestl /* Wooo. Magic values. */ 102442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_IPG0, 0); 102542c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_IPG1, 8); 102642c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_IPG2, 4); 102742c1b001SThomas Moestl 102842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_MAC_MIN_FRAME, ETHER_MIN_LEN); 102942c1b001SThomas Moestl /* Max frame and max burst size */ 103042c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_MAC_MAX_FRAME, 1031336cca9eSBenno Rice ETHER_MAX_LEN | (0x2000<<16)); 1032336cca9eSBenno Rice 103342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_PREAMBLE_LEN, 0x7); 103442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_JAM_SIZE, 0x4); 103542c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ATTEMPT_LIMIT, 0x10); 103642c1b001SThomas Moestl /* Dunno.... */ 103742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_CONTROL_TYPE, 0x8088); 103842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RANDOM_SEED, 1039336cca9eSBenno Rice ((laddr[5]<<8)|laddr[4])&0x3ff); 1040336cca9eSBenno Rice 104142c1b001SThomas Moestl /* Secondary MAC addr set to 0:0:0:0:0:0 */ 104242c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR3, 0); 104342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR4, 0); 104442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR5, 0); 1045336cca9eSBenno Rice 1046336cca9eSBenno Rice /* MAC control addr set to 01:80:c2:00:00:01 */ 104742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR6, 0x0001); 104842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR7, 0xc200); 104942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR8, 0x0180); 105042c1b001SThomas Moestl 105142c1b001SThomas Moestl /* MAC filter addr set to 0:0:0:0:0:0 */ 105242c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER0, 0); 105342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER1, 0); 105442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADDR_FILTER2, 0); 105542c1b001SThomas Moestl 105642c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK1_2, 0); 105742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_ADR_FLT_MASK0, 0); 105842c1b001SThomas Moestl 105942c1b001SThomas Moestl sc->sc_inited = 1; 106042c1b001SThomas Moestl } 106142c1b001SThomas Moestl 106242c1b001SThomas Moestl /* Counters need to be zeroed */ 106342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_NORM_COLL_CNT, 0); 106442c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_FIRST_COLL_CNT, 0); 106542c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_EXCESS_COLL_CNT, 0); 106642c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_LATE_COLL_CNT, 0); 106742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_DEFER_TMR_CNT, 0); 106842c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_PEAK_ATTEMPTS, 0); 106942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_FRAME_COUNT, 0); 107042c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_LEN_ERR_CNT, 0); 107142c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_ALIGN_ERR, 0); 107242c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_CRC_ERR_CNT, 0); 107342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_CODE_VIOL, 0); 107442c1b001SThomas Moestl 107542c1b001SThomas Moestl /* Un-pause stuff */ 107642c1b001SThomas Moestl #if 0 107742c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0x1BF0); 107842c1b001SThomas Moestl #else 107942c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_SEND_PAUSE_CMD, 0); 108042c1b001SThomas Moestl #endif 108142c1b001SThomas Moestl 108242c1b001SThomas Moestl /* 108342c1b001SThomas Moestl * Set the station address. 108442c1b001SThomas Moestl */ 1085336cca9eSBenno Rice bus_space_write_4(t, h, GEM_MAC_ADDR0, (laddr[4]<<8)|laddr[5]); 1086336cca9eSBenno Rice bus_space_write_4(t, h, GEM_MAC_ADDR1, (laddr[2]<<8)|laddr[3]); 1087336cca9eSBenno Rice bus_space_write_4(t, h, GEM_MAC_ADDR2, (laddr[0]<<8)|laddr[1]); 1088336cca9eSBenno Rice 1089336cca9eSBenno Rice /* 1090336cca9eSBenno Rice * Enable MII outputs. Enable GMII if there is a gigabit PHY. 1091336cca9eSBenno Rice */ 1092336cca9eSBenno Rice sc->sc_mif_config = bus_space_read_4(t, h, GEM_MIF_CONFIG); 1093336cca9eSBenno Rice v = GEM_MAC_XIF_TX_MII_ENA; 1094336cca9eSBenno Rice if (sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) { 1095336cca9eSBenno Rice v |= GEM_MAC_XIF_FDPLX_LED; 1096336cca9eSBenno Rice if (sc->sc_flags & GEM_GIGABIT) 1097336cca9eSBenno Rice v |= GEM_MAC_XIF_GMII_MODE; 1098336cca9eSBenno Rice } 1099336cca9eSBenno Rice bus_space_write_4(t, h, GEM_MAC_XIF_CONFIG, v); 110042c1b001SThomas Moestl } 110142c1b001SThomas Moestl 110242c1b001SThomas Moestl static void 110342c1b001SThomas Moestl gem_start(ifp) 110442c1b001SThomas Moestl struct ifnet *ifp; 110542c1b001SThomas Moestl { 110642c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)ifp->if_softc; 1107305f2c06SThomas Moestl struct mbuf *m0 = NULL; 110818100346SThomas Moestl int firsttx, ntx = 0, ofree, txmfail; 110942c1b001SThomas Moestl 111042c1b001SThomas Moestl if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 111142c1b001SThomas Moestl return; 111242c1b001SThomas Moestl 111342c1b001SThomas Moestl /* 111442c1b001SThomas Moestl * Remember the previous number of free descriptors and 111542c1b001SThomas Moestl * the first descriptor we'll use. 111642c1b001SThomas Moestl */ 111742c1b001SThomas Moestl ofree = sc->sc_txfree; 111842c1b001SThomas Moestl firsttx = sc->sc_txnext; 111942c1b001SThomas Moestl 112018100346SThomas Moestl #ifdef GEM_DEBUG 112142c1b001SThomas Moestl CTR3(KTR_GEM, "%s: gem_start: txfree %d, txnext %d", 112242c1b001SThomas Moestl device_get_name(sc->sc_dev), ofree, firsttx); 112318100346SThomas Moestl #endif 112442c1b001SThomas Moestl 112542c1b001SThomas Moestl /* 112642c1b001SThomas Moestl * Loop through the send queue, setting up transmit descriptors 112742c1b001SThomas Moestl * until we drain the queue, or use up all available transmit 112842c1b001SThomas Moestl * descriptors. 112942c1b001SThomas Moestl */ 113042c1b001SThomas Moestl txmfail = 0; 113118100346SThomas Moestl do { 113242c1b001SThomas Moestl /* 113342c1b001SThomas Moestl * Grab a packet off the queue. 113442c1b001SThomas Moestl */ 113542c1b001SThomas Moestl IF_DEQUEUE(&ifp->if_snd, m0); 113642c1b001SThomas Moestl if (m0 == NULL) 113742c1b001SThomas Moestl break; 113842c1b001SThomas Moestl 1139305f2c06SThomas Moestl txmfail = gem_load_txmbuf(sc, m0); 1140305f2c06SThomas Moestl if (txmfail > 0) { 1141305f2c06SThomas Moestl /* Drop the mbuf and complain. */ 1142305f2c06SThomas Moestl printf("gem_start: error %d while loading mbuf dma " 1143305f2c06SThomas Moestl "map\n", txmfail); 1144305f2c06SThomas Moestl continue; 1145305f2c06SThomas Moestl } 1146305f2c06SThomas Moestl /* Not enough descriptors. */ 114742c1b001SThomas Moestl if (txmfail == -1) { 1148305f2c06SThomas Moestl if (sc->sc_txfree == GEM_MAXTXFREE) 1149305f2c06SThomas Moestl panic("gem_start: mbuf chain too long!"); 115042c1b001SThomas Moestl IF_PREPEND(&ifp->if_snd, m0); 115142c1b001SThomas Moestl break; 115242c1b001SThomas Moestl } 115342c1b001SThomas Moestl 115418100346SThomas Moestl ntx++; 1155305f2c06SThomas Moestl /* Kick the transmitter. */ 115618100346SThomas Moestl #ifdef GEM_DEBUG 1157305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: gem_start: kicking tx %d", 1158305f2c06SThomas Moestl device_get_name(sc->sc_dev), sc->sc_txnext); 115918100346SThomas Moestl #endif 116042c1b001SThomas Moestl bus_space_write_4(sc->sc_bustag, sc->sc_h, GEM_TX_KICK, 116142c1b001SThomas Moestl sc->sc_txnext); 116242c1b001SThomas Moestl 1163305f2c06SThomas Moestl if (ifp->if_bpf != NULL) 1164305f2c06SThomas Moestl bpf_mtap(ifp->if_bpf, m0); 116518100346SThomas Moestl } while (1); 1166305f2c06SThomas Moestl 1167305f2c06SThomas Moestl if (txmfail == -1 || sc->sc_txfree == 0) { 1168305f2c06SThomas Moestl /* No more slots left; notify upper layer. */ 1169305f2c06SThomas Moestl ifp->if_flags |= IFF_OACTIVE; 1170305f2c06SThomas Moestl } 1171305f2c06SThomas Moestl 1172305f2c06SThomas Moestl if (ntx > 0) { 1173b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 1174b2d59f42SThomas Moestl 117518100346SThomas Moestl #ifdef GEM_DEBUG 1176305f2c06SThomas Moestl CTR2(KTR_GEM, "%s: packets enqueued, OWN on %d", 1177305f2c06SThomas Moestl device_get_name(sc->sc_dev), firsttx); 117818100346SThomas Moestl #endif 1179305f2c06SThomas Moestl 118042c1b001SThomas Moestl /* Set a watchdog timer in case the chip flakes out. */ 118142c1b001SThomas Moestl ifp->if_timer = 5; 118218100346SThomas Moestl #ifdef GEM_DEBUG 118342c1b001SThomas Moestl CTR2(KTR_GEM, "%s: gem_start: watchdog %d", 118442c1b001SThomas Moestl device_get_name(sc->sc_dev), ifp->if_timer); 118518100346SThomas Moestl #endif 118642c1b001SThomas Moestl } 118742c1b001SThomas Moestl } 118842c1b001SThomas Moestl 118942c1b001SThomas Moestl /* 119042c1b001SThomas Moestl * Transmit interrupt. 119142c1b001SThomas Moestl */ 119242c1b001SThomas Moestl static void 119342c1b001SThomas Moestl gem_tint(sc) 119442c1b001SThomas Moestl struct gem_softc *sc; 119542c1b001SThomas Moestl { 1196fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 119742c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 119842c1b001SThomas Moestl bus_space_handle_t mac = sc->sc_h; 119942c1b001SThomas Moestl struct gem_txsoft *txs; 120042c1b001SThomas Moestl int txlast; 1201336cca9eSBenno Rice int progress = 0; 120242c1b001SThomas Moestl 120342c1b001SThomas Moestl 120418100346SThomas Moestl #ifdef GEM_DEBUG 120542c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_tint", device_get_name(sc->sc_dev)); 120618100346SThomas Moestl #endif 120742c1b001SThomas Moestl 120842c1b001SThomas Moestl /* 120942c1b001SThomas Moestl * Unload collision counters 121042c1b001SThomas Moestl */ 121142c1b001SThomas Moestl ifp->if_collisions += 121242c1b001SThomas Moestl bus_space_read_4(t, mac, GEM_MAC_NORM_COLL_CNT) + 121342c1b001SThomas Moestl bus_space_read_4(t, mac, GEM_MAC_FIRST_COLL_CNT) + 121442c1b001SThomas Moestl bus_space_read_4(t, mac, GEM_MAC_EXCESS_COLL_CNT) + 121542c1b001SThomas Moestl bus_space_read_4(t, mac, GEM_MAC_LATE_COLL_CNT); 121642c1b001SThomas Moestl 121742c1b001SThomas Moestl /* 121842c1b001SThomas Moestl * then clear the hardware counters. 121942c1b001SThomas Moestl */ 122042c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_NORM_COLL_CNT, 0); 122142c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_FIRST_COLL_CNT, 0); 122242c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_EXCESS_COLL_CNT, 0); 122342c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_LATE_COLL_CNT, 0); 122442c1b001SThomas Moestl 122542c1b001SThomas Moestl /* 122642c1b001SThomas Moestl * Go through our Tx list and free mbufs for those 122742c1b001SThomas Moestl * frames that have been transmitted. 122842c1b001SThomas Moestl */ 1229b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 123042c1b001SThomas Moestl while ((txs = STAILQ_FIRST(&sc->sc_txdirtyq)) != NULL) { 123142c1b001SThomas Moestl 123242c1b001SThomas Moestl #ifdef GEM_DEBUG 123342c1b001SThomas Moestl if (ifp->if_flags & IFF_DEBUG) { 123442c1b001SThomas Moestl int i; 123542c1b001SThomas Moestl printf(" txsoft %p transmit chain:\n", txs); 123642c1b001SThomas Moestl for (i = txs->txs_firstdesc;; i = GEM_NEXTTX(i)) { 123742c1b001SThomas Moestl printf("descriptor %d: ", i); 123842c1b001SThomas Moestl printf("gd_flags: 0x%016llx\t", (long long) 123942c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_flags)); 124042c1b001SThomas Moestl printf("gd_addr: 0x%016llx\n", (long long) 124142c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_txdescs[i].gd_addr)); 124242c1b001SThomas Moestl if (i == txs->txs_lastdesc) 124342c1b001SThomas Moestl break; 124442c1b001SThomas Moestl } 124542c1b001SThomas Moestl } 124642c1b001SThomas Moestl #endif 124742c1b001SThomas Moestl 124842c1b001SThomas Moestl /* 124942c1b001SThomas Moestl * In theory, we could harveast some descriptors before 125042c1b001SThomas Moestl * the ring is empty, but that's a bit complicated. 125142c1b001SThomas Moestl * 125242c1b001SThomas Moestl * GEM_TX_COMPLETION points to the last descriptor 125342c1b001SThomas Moestl * processed +1. 125442c1b001SThomas Moestl */ 125542c1b001SThomas Moestl txlast = bus_space_read_4(t, mac, GEM_TX_COMPLETION); 125618100346SThomas Moestl #ifdef GEM_DEBUG 125742c1b001SThomas Moestl CTR3(KTR_GEM, "gem_tint: txs->txs_firstdesc = %d, " 125842c1b001SThomas Moestl "txs->txs_lastdesc = %d, txlast = %d", 125942c1b001SThomas Moestl txs->txs_firstdesc, txs->txs_lastdesc, txlast); 126018100346SThomas Moestl #endif 126142c1b001SThomas Moestl if (txs->txs_firstdesc <= txs->txs_lastdesc) { 126242c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) && 126342c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 126442c1b001SThomas Moestl break; 126542c1b001SThomas Moestl } else { 126642c1b001SThomas Moestl /* Ick -- this command wraps */ 126742c1b001SThomas Moestl if ((txlast >= txs->txs_firstdesc) || 126842c1b001SThomas Moestl (txlast <= txs->txs_lastdesc)) 126942c1b001SThomas Moestl break; 127042c1b001SThomas Moestl } 127142c1b001SThomas Moestl 127218100346SThomas Moestl #ifdef GEM_DEBUG 127342c1b001SThomas Moestl CTR0(KTR_GEM, "gem_tint: releasing a desc"); 127418100346SThomas Moestl #endif 127542c1b001SThomas Moestl STAILQ_REMOVE_HEAD(&sc->sc_txdirtyq, txs_q); 127642c1b001SThomas Moestl 127742c1b001SThomas Moestl sc->sc_txfree += txs->txs_ndescs; 127842c1b001SThomas Moestl 1279305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_tdmatag, txs->txs_dmamap, 128042c1b001SThomas Moestl BUS_DMASYNC_POSTWRITE); 1281305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_tdmatag, txs->txs_dmamap); 128242c1b001SThomas Moestl if (txs->txs_mbuf != NULL) { 128342c1b001SThomas Moestl m_freem(txs->txs_mbuf); 128442c1b001SThomas Moestl txs->txs_mbuf = NULL; 128542c1b001SThomas Moestl } 128642c1b001SThomas Moestl 128742c1b001SThomas Moestl STAILQ_INSERT_TAIL(&sc->sc_txfreeq, txs, txs_q); 128842c1b001SThomas Moestl 128942c1b001SThomas Moestl ifp->if_opackets++; 1290336cca9eSBenno Rice progress = 1; 129142c1b001SThomas Moestl } 129242c1b001SThomas Moestl 129318100346SThomas Moestl #ifdef GEM_DEBUG 129442c1b001SThomas Moestl CTR3(KTR_GEM, "gem_tint: GEM_TX_STATE_MACHINE %x " 129542c1b001SThomas Moestl "GEM_TX_DATA_PTR %llx " 129642c1b001SThomas Moestl "GEM_TX_COMPLETION %x", 129742c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_STATE_MACHINE), 129842c1b001SThomas Moestl ((long long) bus_space_read_4(sc->sc_bustag, sc->sc_h, 129942c1b001SThomas Moestl GEM_TX_DATA_PTR_HI) << 32) | 130042c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, 130142c1b001SThomas Moestl GEM_TX_DATA_PTR_LO), 130242c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_COMPLETION)); 130318100346SThomas Moestl #endif 130442c1b001SThomas Moestl 1305336cca9eSBenno Rice if (progress) { 1306336cca9eSBenno Rice if (sc->sc_txfree == GEM_NTXDESC - 1) 1307336cca9eSBenno Rice sc->sc_txwin = 0; 130842c1b001SThomas Moestl 1309336cca9eSBenno Rice /* Freed some descriptors, so reset IFF_OACTIVE and restart. */ 1310336cca9eSBenno Rice ifp->if_flags &= ~IFF_OACTIVE; 1311336cca9eSBenno Rice gem_start(ifp); 1312336cca9eSBenno Rice 1313336cca9eSBenno Rice if (STAILQ_EMPTY(&sc->sc_txdirtyq)) 1314336cca9eSBenno Rice ifp->if_timer = 0; 1315336cca9eSBenno Rice } 131642c1b001SThomas Moestl 131718100346SThomas Moestl #ifdef GEM_DEBUG 131842c1b001SThomas Moestl CTR2(KTR_GEM, "%s: gem_tint: watchdog %d", 131942c1b001SThomas Moestl device_get_name(sc->sc_dev), ifp->if_timer); 132018100346SThomas Moestl #endif 132142c1b001SThomas Moestl } 132242c1b001SThomas Moestl 1323c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 13240d80b9bdSThomas Moestl static void 13250d80b9bdSThomas Moestl gem_rint_timeout(arg) 13260d80b9bdSThomas Moestl void *arg; 13270d80b9bdSThomas Moestl { 13280d80b9bdSThomas Moestl 13290d80b9bdSThomas Moestl gem_rint((struct gem_softc *)arg); 13300d80b9bdSThomas Moestl } 133111e3f060SJake Burkholder #endif 13320d80b9bdSThomas Moestl 133342c1b001SThomas Moestl /* 133442c1b001SThomas Moestl * Receive interrupt. 133542c1b001SThomas Moestl */ 133642c1b001SThomas Moestl static void 133742c1b001SThomas Moestl gem_rint(sc) 133842c1b001SThomas Moestl struct gem_softc *sc; 133942c1b001SThomas Moestl { 1340fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 134142c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 134242c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 134342c1b001SThomas Moestl struct gem_rxsoft *rxs; 134442c1b001SThomas Moestl struct mbuf *m; 134542c1b001SThomas Moestl u_int64_t rxstat; 1346336cca9eSBenno Rice u_int32_t rxcomp; 1347336cca9eSBenno Rice int i, len, progress = 0; 134842c1b001SThomas Moestl 1349c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 13500d80b9bdSThomas Moestl callout_stop(&sc->sc_rx_ch); 1351c3d5598aSMarius Strobl #endif 135218100346SThomas Moestl #ifdef GEM_DEBUG 135342c1b001SThomas Moestl CTR1(KTR_GEM, "%s: gem_rint", device_get_name(sc->sc_dev)); 135418100346SThomas Moestl #endif 1355336cca9eSBenno Rice 1356336cca9eSBenno Rice /* 1357336cca9eSBenno Rice * Read the completion register once. This limits 1358336cca9eSBenno Rice * how long the following loop can execute. 1359336cca9eSBenno Rice */ 1360336cca9eSBenno Rice rxcomp = bus_space_read_4(t, h, GEM_RX_COMPLETION); 1361336cca9eSBenno Rice 136218100346SThomas Moestl #ifdef GEM_DEBUG 136342c1b001SThomas Moestl CTR2(KTR_GEM, "gem_rint: sc->rxptr %d, complete %d", 1364336cca9eSBenno Rice sc->sc_rxptr, rxcomp); 136518100346SThomas Moestl #endif 1366b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_POSTREAD); 1367336cca9eSBenno Rice for (i = sc->sc_rxptr; i != rxcomp; 136842c1b001SThomas Moestl i = GEM_NEXTRX(i)) { 136942c1b001SThomas Moestl rxs = &sc->sc_rxsoft[i]; 137042c1b001SThomas Moestl 137142c1b001SThomas Moestl rxstat = GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags); 137242c1b001SThomas Moestl 137342c1b001SThomas Moestl if (rxstat & GEM_RD_OWN) { 1374c3d5598aSMarius Strobl #ifdef GEM_RINT_TIMEOUT 137542c1b001SThomas Moestl /* 13760d80b9bdSThomas Moestl * The descriptor is still marked as owned, although 13770d80b9bdSThomas Moestl * it is supposed to have completed. This has been 13780d80b9bdSThomas Moestl * observed on some machines. Just exiting here 13790d80b9bdSThomas Moestl * might leave the packet sitting around until another 13800d80b9bdSThomas Moestl * one arrives to trigger a new interrupt, which is 13810d80b9bdSThomas Moestl * generally undesirable, so set up a timeout. 138242c1b001SThomas Moestl */ 13830d80b9bdSThomas Moestl callout_reset(&sc->sc_rx_ch, GEM_RXOWN_TICKS, 13840d80b9bdSThomas Moestl gem_rint_timeout, sc); 1385336cca9eSBenno Rice #endif 138642c1b001SThomas Moestl break; 138742c1b001SThomas Moestl } 138842c1b001SThomas Moestl 1389336cca9eSBenno Rice progress++; 1390336cca9eSBenno Rice ifp->if_ipackets++; 1391336cca9eSBenno Rice 139242c1b001SThomas Moestl if (rxstat & GEM_RD_BAD_CRC) { 1393336cca9eSBenno Rice ifp->if_ierrors++; 139442c1b001SThomas Moestl device_printf(sc->sc_dev, "receive error: CRC error\n"); 139542c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 139642c1b001SThomas Moestl continue; 139742c1b001SThomas Moestl } 139842c1b001SThomas Moestl 139942c1b001SThomas Moestl #ifdef GEM_DEBUG 140042c1b001SThomas Moestl if (ifp->if_flags & IFF_DEBUG) { 140142c1b001SThomas Moestl printf(" rxsoft %p descriptor %d: ", rxs, i); 140242c1b001SThomas Moestl printf("gd_flags: 0x%016llx\t", (long long) 140342c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_flags)); 140442c1b001SThomas Moestl printf("gd_addr: 0x%016llx\n", (long long) 140542c1b001SThomas Moestl GEM_DMA_READ(sc, sc->sc_rxdescs[i].gd_addr)); 140642c1b001SThomas Moestl } 140742c1b001SThomas Moestl #endif 140842c1b001SThomas Moestl 140942c1b001SThomas Moestl /* 141042c1b001SThomas Moestl * No errors; receive the packet. Note the Gem 141142c1b001SThomas Moestl * includes the CRC with every packet. 141242c1b001SThomas Moestl */ 141342c1b001SThomas Moestl len = GEM_RD_BUFLEN(rxstat); 141442c1b001SThomas Moestl 141542c1b001SThomas Moestl /* 141642c1b001SThomas Moestl * Allocate a new mbuf cluster. If that fails, we are 141742c1b001SThomas Moestl * out of memory, and must drop the packet and recycle 141842c1b001SThomas Moestl * the buffer that's already attached to this descriptor. 141942c1b001SThomas Moestl */ 142042c1b001SThomas Moestl m = rxs->rxs_mbuf; 142142c1b001SThomas Moestl if (gem_add_rxbuf(sc, i) != 0) { 142242c1b001SThomas Moestl ifp->if_ierrors++; 142342c1b001SThomas Moestl GEM_INIT_RXDESC(sc, i); 142442c1b001SThomas Moestl continue; 142542c1b001SThomas Moestl } 142642c1b001SThomas Moestl m->m_data += 2; /* We're already off by two */ 142742c1b001SThomas Moestl 142842c1b001SThomas Moestl m->m_pkthdr.rcvif = ifp; 142942c1b001SThomas Moestl m->m_pkthdr.len = m->m_len = len - ETHER_CRC_LEN; 143042c1b001SThomas Moestl 143142c1b001SThomas Moestl /* Pass it on. */ 1432673d9191SSam Leffler (*ifp->if_input)(ifp, m); 143342c1b001SThomas Moestl } 143442c1b001SThomas Moestl 1435336cca9eSBenno Rice if (progress) { 1436b2d59f42SThomas Moestl GEM_CDSYNC(sc, BUS_DMASYNC_PREWRITE); 143742c1b001SThomas Moestl /* Update the receive pointer. */ 1438336cca9eSBenno Rice if (i == sc->sc_rxptr) { 1439336cca9eSBenno Rice device_printf(sc->sc_dev, "rint: ring wrap\n"); 1440336cca9eSBenno Rice } 144142c1b001SThomas Moestl sc->sc_rxptr = i; 1442336cca9eSBenno Rice bus_space_write_4(t, h, GEM_RX_KICK, GEM_PREVRX(i)); 1443336cca9eSBenno Rice } 144442c1b001SThomas Moestl 144518100346SThomas Moestl #ifdef GEM_DEBUG 144642c1b001SThomas Moestl CTR2(KTR_GEM, "gem_rint: done sc->rxptr %d, complete %d", 144742c1b001SThomas Moestl sc->sc_rxptr, bus_space_read_4(t, h, GEM_RX_COMPLETION)); 144818100346SThomas Moestl #endif 144942c1b001SThomas Moestl } 145042c1b001SThomas Moestl 145142c1b001SThomas Moestl 145242c1b001SThomas Moestl /* 145342c1b001SThomas Moestl * gem_add_rxbuf: 145442c1b001SThomas Moestl * 145542c1b001SThomas Moestl * Add a receive buffer to the indicated descriptor. 145642c1b001SThomas Moestl */ 145742c1b001SThomas Moestl static int 145842c1b001SThomas Moestl gem_add_rxbuf(sc, idx) 145942c1b001SThomas Moestl struct gem_softc *sc; 146042c1b001SThomas Moestl int idx; 146142c1b001SThomas Moestl { 146242c1b001SThomas Moestl struct gem_rxsoft *rxs = &sc->sc_rxsoft[idx]; 146342c1b001SThomas Moestl struct mbuf *m; 1464c3d5598aSMarius Strobl bus_dma_segment_t segs[1]; 1465c3d5598aSMarius Strobl int error, nsegs; 146642c1b001SThomas Moestl 1467a163d034SWarner Losh m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 146842c1b001SThomas Moestl if (m == NULL) 146942c1b001SThomas Moestl return (ENOBUFS); 1470305f2c06SThomas Moestl m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; 147142c1b001SThomas Moestl 147242c1b001SThomas Moestl #ifdef GEM_DEBUG 147342c1b001SThomas Moestl /* bzero the packet to check dma */ 147442c1b001SThomas Moestl memset(m->m_ext.ext_buf, 0, m->m_ext.ext_size); 147542c1b001SThomas Moestl #endif 147642c1b001SThomas Moestl 1477b2d59f42SThomas Moestl if (rxs->rxs_mbuf != NULL) { 1478b2d59f42SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, 1479b2d59f42SThomas Moestl BUS_DMASYNC_POSTREAD); 1480305f2c06SThomas Moestl bus_dmamap_unload(sc->sc_rdmatag, rxs->rxs_dmamap); 1481b2d59f42SThomas Moestl } 148242c1b001SThomas Moestl 148342c1b001SThomas Moestl rxs->rxs_mbuf = m; 148442c1b001SThomas Moestl 1485c3d5598aSMarius Strobl error = bus_dmamap_load_mbuf_sg(sc->sc_rdmatag, rxs->rxs_dmamap, 1486c3d5598aSMarius Strobl m, segs, &nsegs, BUS_DMA_NOWAIT); 1487c3d5598aSMarius Strobl /* If nsegs is wrong then the stack is corrupt. */ 1488c3d5598aSMarius Strobl KASSERT(nsegs == 1, ("Too many segments returned!")); 1489c3d5598aSMarius Strobl if (error != 0) { 149042c1b001SThomas Moestl device_printf(sc->sc_dev, "can't load rx DMA map %d, error = " 149142c1b001SThomas Moestl "%d\n", idx, error); 1492c3d5598aSMarius Strobl m_freem(m); 1493c3d5598aSMarius Strobl return (ENOBUFS); 149442c1b001SThomas Moestl } 1495c3d5598aSMarius Strobl rxs->rxs_paddr = segs[0].ds_addr; 149642c1b001SThomas Moestl 1497305f2c06SThomas Moestl bus_dmamap_sync(sc->sc_rdmatag, rxs->rxs_dmamap, BUS_DMASYNC_PREREAD); 149842c1b001SThomas Moestl 149942c1b001SThomas Moestl GEM_INIT_RXDESC(sc, idx); 150042c1b001SThomas Moestl 150142c1b001SThomas Moestl return (0); 150242c1b001SThomas Moestl } 150342c1b001SThomas Moestl 150442c1b001SThomas Moestl 150542c1b001SThomas Moestl static void 150642c1b001SThomas Moestl gem_eint(sc, status) 150742c1b001SThomas Moestl struct gem_softc *sc; 150842c1b001SThomas Moestl u_int status; 150942c1b001SThomas Moestl { 151042c1b001SThomas Moestl 151142c1b001SThomas Moestl if ((status & GEM_INTR_MIF) != 0) { 151242c1b001SThomas Moestl device_printf(sc->sc_dev, "XXXlink status changed\n"); 151342c1b001SThomas Moestl return; 151442c1b001SThomas Moestl } 151542c1b001SThomas Moestl 151642c1b001SThomas Moestl device_printf(sc->sc_dev, "status=%x\n", status); 151742c1b001SThomas Moestl } 151842c1b001SThomas Moestl 151942c1b001SThomas Moestl 152042c1b001SThomas Moestl void 152142c1b001SThomas Moestl gem_intr(v) 152242c1b001SThomas Moestl void *v; 152342c1b001SThomas Moestl { 152442c1b001SThomas Moestl struct gem_softc *sc = (struct gem_softc *)v; 152542c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 152642c1b001SThomas Moestl bus_space_handle_t seb = sc->sc_h; 152742c1b001SThomas Moestl u_int32_t status; 152842c1b001SThomas Moestl 152942c1b001SThomas Moestl status = bus_space_read_4(t, seb, GEM_STATUS); 153018100346SThomas Moestl #ifdef GEM_DEBUG 153142c1b001SThomas Moestl CTR3(KTR_GEM, "%s: gem_intr: cplt %x, status %x", 153242c1b001SThomas Moestl device_get_name(sc->sc_dev), (status>>19), 153342c1b001SThomas Moestl (u_int)status); 153418100346SThomas Moestl #endif 153542c1b001SThomas Moestl 153642c1b001SThomas Moestl if ((status & (GEM_INTR_RX_TAG_ERR | GEM_INTR_BERR)) != 0) 153742c1b001SThomas Moestl gem_eint(sc, status); 153842c1b001SThomas Moestl 153942c1b001SThomas Moestl if ((status & (GEM_INTR_TX_EMPTY | GEM_INTR_TX_INTME)) != 0) 154042c1b001SThomas Moestl gem_tint(sc); 154142c1b001SThomas Moestl 154242c1b001SThomas Moestl if ((status & (GEM_INTR_RX_DONE | GEM_INTR_RX_NOBUF)) != 0) 154342c1b001SThomas Moestl gem_rint(sc); 154442c1b001SThomas Moestl 154542c1b001SThomas Moestl /* We should eventually do more than just print out error stats. */ 154642c1b001SThomas Moestl if (status & GEM_INTR_TX_MAC) { 154742c1b001SThomas Moestl int txstat = bus_space_read_4(t, seb, GEM_MAC_TX_STATUS); 154842c1b001SThomas Moestl if (txstat & ~GEM_MAC_TX_XMIT_DONE) 1549336cca9eSBenno Rice device_printf(sc->sc_dev, "MAC tx fault, status %x\n", 1550336cca9eSBenno Rice txstat); 15519bb711b9SThomas Moestl if (txstat & (GEM_MAC_TX_UNDERRUN | GEM_MAC_TX_PKT_TOO_LONG)) 15529bb711b9SThomas Moestl gem_init(sc); 155342c1b001SThomas Moestl } 155442c1b001SThomas Moestl if (status & GEM_INTR_RX_MAC) { 155542c1b001SThomas Moestl int rxstat = bus_space_read_4(t, seb, GEM_MAC_RX_STATUS); 155642c1b001SThomas Moestl if (rxstat & ~(GEM_MAC_RX_DONE | GEM_MAC_RX_FRAME_CNT)) 1557336cca9eSBenno Rice device_printf(sc->sc_dev, "MAC rx fault, status %x\n", 1558336cca9eSBenno Rice rxstat); 15599bb711b9SThomas Moestl if ((rxstat & GEM_MAC_RX_OVERFLOW) != 0) 15609bb711b9SThomas Moestl gem_init(sc); 156142c1b001SThomas Moestl } 156242c1b001SThomas Moestl } 156342c1b001SThomas Moestl 156442c1b001SThomas Moestl 156542c1b001SThomas Moestl static void 156642c1b001SThomas Moestl gem_watchdog(ifp) 156742c1b001SThomas Moestl struct ifnet *ifp; 156842c1b001SThomas Moestl { 156942c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 157042c1b001SThomas Moestl 157118100346SThomas Moestl #ifdef GEM_DEBUG 157242c1b001SThomas Moestl CTR3(KTR_GEM, "gem_watchdog: GEM_RX_CONFIG %x GEM_MAC_RX_STATUS %x " 157342c1b001SThomas Moestl "GEM_MAC_RX_CONFIG %x", 157442c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_RX_CONFIG), 157542c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_STATUS), 157642c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_RX_CONFIG)); 157742c1b001SThomas Moestl CTR3(KTR_GEM, "gem_watchdog: GEM_TX_CONFIG %x GEM_MAC_TX_STATUS %x " 157842c1b001SThomas Moestl "GEM_MAC_TX_CONFIG %x", 157942c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_TX_CONFIG), 158042c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_STATUS), 158142c1b001SThomas Moestl bus_space_read_4(sc->sc_bustag, sc->sc_h, GEM_MAC_TX_CONFIG)); 158218100346SThomas Moestl #endif 158342c1b001SThomas Moestl 158442c1b001SThomas Moestl device_printf(sc->sc_dev, "device timeout\n"); 158542c1b001SThomas Moestl ++ifp->if_oerrors; 158642c1b001SThomas Moestl 158742c1b001SThomas Moestl /* Try to get more packets going. */ 1588c3d5598aSMarius Strobl gem_init(ifp); 158942c1b001SThomas Moestl } 159042c1b001SThomas Moestl 159142c1b001SThomas Moestl /* 159242c1b001SThomas Moestl * Initialize the MII Management Interface 159342c1b001SThomas Moestl */ 159442c1b001SThomas Moestl static void 159542c1b001SThomas Moestl gem_mifinit(sc) 159642c1b001SThomas Moestl struct gem_softc *sc; 159742c1b001SThomas Moestl { 159842c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 159942c1b001SThomas Moestl bus_space_handle_t mif = sc->sc_h; 160042c1b001SThomas Moestl 160142c1b001SThomas Moestl /* Configure the MIF in frame mode */ 160242c1b001SThomas Moestl sc->sc_mif_config = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 160342c1b001SThomas Moestl sc->sc_mif_config &= ~GEM_MIF_CONFIG_BB_ENA; 160442c1b001SThomas Moestl bus_space_write_4(t, mif, GEM_MIF_CONFIG, sc->sc_mif_config); 160542c1b001SThomas Moestl } 160642c1b001SThomas Moestl 160742c1b001SThomas Moestl /* 160842c1b001SThomas Moestl * MII interface 160942c1b001SThomas Moestl * 161042c1b001SThomas Moestl * The GEM MII interface supports at least three different operating modes: 161142c1b001SThomas Moestl * 161242c1b001SThomas Moestl * Bitbang mode is implemented using data, clock and output enable registers. 161342c1b001SThomas Moestl * 161442c1b001SThomas Moestl * Frame mode is implemented by loading a complete frame into the frame 161542c1b001SThomas Moestl * register and polling the valid bit for completion. 161642c1b001SThomas Moestl * 161742c1b001SThomas Moestl * Polling mode uses the frame register but completion is indicated by 161842c1b001SThomas Moestl * an interrupt. 161942c1b001SThomas Moestl * 162042c1b001SThomas Moestl */ 162142c1b001SThomas Moestl int 162242c1b001SThomas Moestl gem_mii_readreg(dev, phy, reg) 162342c1b001SThomas Moestl device_t dev; 162442c1b001SThomas Moestl int phy, reg; 162542c1b001SThomas Moestl { 162642c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 162742c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 162842c1b001SThomas Moestl bus_space_handle_t mif = sc->sc_h; 162942c1b001SThomas Moestl int n; 163042c1b001SThomas Moestl u_int32_t v; 163142c1b001SThomas Moestl 163242c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 163342c1b001SThomas Moestl printf("gem_mii_readreg: phy %d reg %d\n", phy, reg); 163442c1b001SThomas Moestl #endif 163542c1b001SThomas Moestl 163642c1b001SThomas Moestl #if 0 163742c1b001SThomas Moestl /* Select the desired PHY in the MIF configuration register */ 163842c1b001SThomas Moestl v = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 163942c1b001SThomas Moestl /* Clear PHY select bit */ 164042c1b001SThomas Moestl v &= ~GEM_MIF_CONFIG_PHY_SEL; 164142c1b001SThomas Moestl if (phy == GEM_PHYAD_EXTERNAL) 164242c1b001SThomas Moestl /* Set PHY select bit to get at external device */ 164342c1b001SThomas Moestl v |= GEM_MIF_CONFIG_PHY_SEL; 164442c1b001SThomas Moestl bus_space_write_4(t, mif, GEM_MIF_CONFIG, v); 164542c1b001SThomas Moestl #endif 164642c1b001SThomas Moestl 164742c1b001SThomas Moestl /* Construct the frame command */ 164842c1b001SThomas Moestl v = (reg << GEM_MIF_REG_SHIFT) | (phy << GEM_MIF_PHY_SHIFT) | 164942c1b001SThomas Moestl GEM_MIF_FRAME_READ; 165042c1b001SThomas Moestl 165142c1b001SThomas Moestl bus_space_write_4(t, mif, GEM_MIF_FRAME, v); 165242c1b001SThomas Moestl for (n = 0; n < 100; n++) { 165342c1b001SThomas Moestl DELAY(1); 165442c1b001SThomas Moestl v = bus_space_read_4(t, mif, GEM_MIF_FRAME); 165542c1b001SThomas Moestl if (v & GEM_MIF_FRAME_TA0) 165642c1b001SThomas Moestl return (v & GEM_MIF_FRAME_DATA); 165742c1b001SThomas Moestl } 165842c1b001SThomas Moestl 165942c1b001SThomas Moestl device_printf(sc->sc_dev, "mii_read timeout\n"); 166042c1b001SThomas Moestl return (0); 166142c1b001SThomas Moestl } 166242c1b001SThomas Moestl 166342c1b001SThomas Moestl int 166442c1b001SThomas Moestl gem_mii_writereg(dev, phy, reg, val) 166542c1b001SThomas Moestl device_t dev; 166642c1b001SThomas Moestl int phy, reg, val; 166742c1b001SThomas Moestl { 166842c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 166942c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 167042c1b001SThomas Moestl bus_space_handle_t mif = sc->sc_h; 167142c1b001SThomas Moestl int n; 167242c1b001SThomas Moestl u_int32_t v; 167342c1b001SThomas Moestl 167442c1b001SThomas Moestl #ifdef GEM_DEBUG_PHY 167542c1b001SThomas Moestl printf("gem_mii_writereg: phy %d reg %d val %x\n", phy, reg, val); 167642c1b001SThomas Moestl #endif 167742c1b001SThomas Moestl 167842c1b001SThomas Moestl #if 0 167942c1b001SThomas Moestl /* Select the desired PHY in the MIF configuration register */ 168042c1b001SThomas Moestl v = bus_space_read_4(t, mif, GEM_MIF_CONFIG); 168142c1b001SThomas Moestl /* Clear PHY select bit */ 168242c1b001SThomas Moestl v &= ~GEM_MIF_CONFIG_PHY_SEL; 168342c1b001SThomas Moestl if (phy == GEM_PHYAD_EXTERNAL) 168442c1b001SThomas Moestl /* Set PHY select bit to get at external device */ 168542c1b001SThomas Moestl v |= GEM_MIF_CONFIG_PHY_SEL; 168642c1b001SThomas Moestl bus_space_write_4(t, mif, GEM_MIF_CONFIG, v); 168742c1b001SThomas Moestl #endif 168842c1b001SThomas Moestl /* Construct the frame command */ 168942c1b001SThomas Moestl v = GEM_MIF_FRAME_WRITE | 169042c1b001SThomas Moestl (phy << GEM_MIF_PHY_SHIFT) | 169142c1b001SThomas Moestl (reg << GEM_MIF_REG_SHIFT) | 169242c1b001SThomas Moestl (val & GEM_MIF_FRAME_DATA); 169342c1b001SThomas Moestl 169442c1b001SThomas Moestl bus_space_write_4(t, mif, GEM_MIF_FRAME, v); 169542c1b001SThomas Moestl for (n = 0; n < 100; n++) { 169642c1b001SThomas Moestl DELAY(1); 169742c1b001SThomas Moestl v = bus_space_read_4(t, mif, GEM_MIF_FRAME); 169842c1b001SThomas Moestl if (v & GEM_MIF_FRAME_TA0) 169942c1b001SThomas Moestl return (1); 170042c1b001SThomas Moestl } 170142c1b001SThomas Moestl 170242c1b001SThomas Moestl device_printf(sc->sc_dev, "mii_write timeout\n"); 170342c1b001SThomas Moestl return (0); 170442c1b001SThomas Moestl } 170542c1b001SThomas Moestl 170642c1b001SThomas Moestl void 170742c1b001SThomas Moestl gem_mii_statchg(dev) 170842c1b001SThomas Moestl device_t dev; 170942c1b001SThomas Moestl { 171042c1b001SThomas Moestl struct gem_softc *sc = device_get_softc(dev); 171142c1b001SThomas Moestl #ifdef GEM_DEBUG 171242c1b001SThomas Moestl int instance = IFM_INST(sc->sc_mii->mii_media.ifm_cur->ifm_media); 171342c1b001SThomas Moestl #endif 171442c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 171542c1b001SThomas Moestl bus_space_handle_t mac = sc->sc_h; 171642c1b001SThomas Moestl u_int32_t v; 171742c1b001SThomas Moestl 171842c1b001SThomas Moestl #ifdef GEM_DEBUG 171942c1b001SThomas Moestl if (sc->sc_debug) 172042c1b001SThomas Moestl printf("gem_mii_statchg: status change: phy = %d\n", 172142c1b001SThomas Moestl sc->sc_phys[instance]); 172242c1b001SThomas Moestl #endif 172342c1b001SThomas Moestl 172442c1b001SThomas Moestl /* Set tx full duplex options */ 172542c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, 0); 172642c1b001SThomas Moestl DELAY(10000); /* reg must be cleared and delay before changing. */ 172742c1b001SThomas Moestl v = GEM_MAC_TX_ENA_IPG0|GEM_MAC_TX_NGU|GEM_MAC_TX_NGU_LIMIT| 172842c1b001SThomas Moestl GEM_MAC_TX_ENABLE; 172942c1b001SThomas Moestl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) { 173042c1b001SThomas Moestl v |= GEM_MAC_TX_IGN_CARRIER|GEM_MAC_TX_IGN_COLLIS; 173142c1b001SThomas Moestl } 173242c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_TX_CONFIG, v); 173342c1b001SThomas Moestl 173442c1b001SThomas Moestl /* XIF Configuration */ 173542c1b001SThomas Moestl v = GEM_MAC_XIF_LINK_LED; 173642c1b001SThomas Moestl v |= GEM_MAC_XIF_TX_MII_ENA; 1737336cca9eSBenno Rice 173842c1b001SThomas Moestl /* If an external transceiver is connected, enable its MII drivers */ 173942c1b001SThomas Moestl sc->sc_mif_config = bus_space_read_4(t, mac, GEM_MIF_CONFIG); 174042c1b001SThomas Moestl if ((sc->sc_mif_config & GEM_MIF_CONFIG_MDI1) != 0) { 174142c1b001SThomas Moestl /* External MII needs echo disable if half duplex. */ 174242c1b001SThomas Moestl if ((IFM_OPTIONS(sc->sc_mii->mii_media_active) & IFM_FDX) != 0) 174342c1b001SThomas Moestl /* turn on full duplex LED */ 174442c1b001SThomas Moestl v |= GEM_MAC_XIF_FDPLX_LED; 174542c1b001SThomas Moestl else 174642c1b001SThomas Moestl /* half duplex -- disable echo */ 174742c1b001SThomas Moestl v |= GEM_MAC_XIF_ECHO_DISABL; 1748336cca9eSBenno Rice 1749336cca9eSBenno Rice if (IFM_SUBTYPE(sc->sc_mii->mii_media_active) == IFM_1000_T) 1750336cca9eSBenno Rice v |= GEM_MAC_XIF_GMII_MODE; 1751336cca9eSBenno Rice else 1752336cca9eSBenno Rice v &= ~GEM_MAC_XIF_GMII_MODE; 175342c1b001SThomas Moestl } else { 175442c1b001SThomas Moestl /* Internal MII needs buf enable */ 175542c1b001SThomas Moestl v |= GEM_MAC_XIF_MII_BUF_ENA; 175642c1b001SThomas Moestl } 175742c1b001SThomas Moestl bus_space_write_4(t, mac, GEM_MAC_XIF_CONFIG, v); 175842c1b001SThomas Moestl } 175942c1b001SThomas Moestl 176042c1b001SThomas Moestl int 176142c1b001SThomas Moestl gem_mediachange(ifp) 176242c1b001SThomas Moestl struct ifnet *ifp; 176342c1b001SThomas Moestl { 176442c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 176542c1b001SThomas Moestl 176642c1b001SThomas Moestl /* XXX Add support for serial media. */ 176742c1b001SThomas Moestl 176842c1b001SThomas Moestl return (mii_mediachg(sc->sc_mii)); 176942c1b001SThomas Moestl } 177042c1b001SThomas Moestl 177142c1b001SThomas Moestl void 177242c1b001SThomas Moestl gem_mediastatus(ifp, ifmr) 177342c1b001SThomas Moestl struct ifnet *ifp; 177442c1b001SThomas Moestl struct ifmediareq *ifmr; 177542c1b001SThomas Moestl { 177642c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 177742c1b001SThomas Moestl 177842c1b001SThomas Moestl if ((ifp->if_flags & IFF_UP) == 0) 177942c1b001SThomas Moestl return; 178042c1b001SThomas Moestl 178142c1b001SThomas Moestl mii_pollstat(sc->sc_mii); 178242c1b001SThomas Moestl ifmr->ifm_active = sc->sc_mii->mii_media_active; 178342c1b001SThomas Moestl ifmr->ifm_status = sc->sc_mii->mii_media_status; 178442c1b001SThomas Moestl } 178542c1b001SThomas Moestl 178642c1b001SThomas Moestl /* 178742c1b001SThomas Moestl * Process an ioctl request. 178842c1b001SThomas Moestl */ 178942c1b001SThomas Moestl static int 179042c1b001SThomas Moestl gem_ioctl(ifp, cmd, data) 179142c1b001SThomas Moestl struct ifnet *ifp; 179242c1b001SThomas Moestl u_long cmd; 179342c1b001SThomas Moestl caddr_t data; 179442c1b001SThomas Moestl { 179542c1b001SThomas Moestl struct gem_softc *sc = ifp->if_softc; 179642c1b001SThomas Moestl struct ifreq *ifr = (struct ifreq *)data; 179742c1b001SThomas Moestl int s, error = 0; 179842c1b001SThomas Moestl 179942c1b001SThomas Moestl switch (cmd) { 180042c1b001SThomas Moestl case SIOCSIFADDR: 180142c1b001SThomas Moestl case SIOCGIFADDR: 180242c1b001SThomas Moestl case SIOCSIFMTU: 180342c1b001SThomas Moestl error = ether_ioctl(ifp, cmd, data); 180442c1b001SThomas Moestl break; 180542c1b001SThomas Moestl case SIOCSIFFLAGS: 180642c1b001SThomas Moestl if (ifp->if_flags & IFF_UP) { 1807336cca9eSBenno Rice if ((sc->sc_ifflags ^ ifp->if_flags) == IFF_PROMISC) 180842c1b001SThomas Moestl gem_setladrf(sc); 180942c1b001SThomas Moestl else 181042c1b001SThomas Moestl gem_init(sc); 181142c1b001SThomas Moestl } else { 181242c1b001SThomas Moestl if (ifp->if_flags & IFF_RUNNING) 181342c1b001SThomas Moestl gem_stop(ifp, 0); 181442c1b001SThomas Moestl } 1815336cca9eSBenno Rice sc->sc_ifflags = ifp->if_flags; 181642c1b001SThomas Moestl error = 0; 181742c1b001SThomas Moestl break; 181842c1b001SThomas Moestl case SIOCADDMULTI: 181942c1b001SThomas Moestl case SIOCDELMULTI: 182042c1b001SThomas Moestl gem_setladrf(sc); 182142c1b001SThomas Moestl error = 0; 182242c1b001SThomas Moestl break; 182342c1b001SThomas Moestl case SIOCGIFMEDIA: 182442c1b001SThomas Moestl case SIOCSIFMEDIA: 182542c1b001SThomas Moestl error = ifmedia_ioctl(ifp, ifr, &sc->sc_mii->mii_media, cmd); 182642c1b001SThomas Moestl break; 182742c1b001SThomas Moestl default: 1828305f2c06SThomas Moestl error = ENOTTY; 182942c1b001SThomas Moestl break; 183042c1b001SThomas Moestl } 183142c1b001SThomas Moestl 183242c1b001SThomas Moestl /* Try to get things going again */ 183342c1b001SThomas Moestl if (ifp->if_flags & IFF_UP) 183442c1b001SThomas Moestl gem_start(ifp); 183542c1b001SThomas Moestl splx(s); 183642c1b001SThomas Moestl return (error); 183742c1b001SThomas Moestl } 183842c1b001SThomas Moestl 183942c1b001SThomas Moestl /* 184042c1b001SThomas Moestl * Set up the logical address filter. 184142c1b001SThomas Moestl */ 184242c1b001SThomas Moestl static void 184342c1b001SThomas Moestl gem_setladrf(sc) 184442c1b001SThomas Moestl struct gem_softc *sc; 184542c1b001SThomas Moestl { 1846fc74a9f9SBrooks Davis struct ifnet *ifp = sc->sc_ifp; 184742c1b001SThomas Moestl struct ifmultiaddr *inm; 184842c1b001SThomas Moestl bus_space_tag_t t = sc->sc_bustag; 184942c1b001SThomas Moestl bus_space_handle_t h = sc->sc_h; 185042c1b001SThomas Moestl u_int32_t crc; 185142c1b001SThomas Moestl u_int32_t hash[16]; 185242c1b001SThomas Moestl u_int32_t v; 1853336cca9eSBenno Rice int i; 185442c1b001SThomas Moestl 185542c1b001SThomas Moestl /* Get current RX configuration */ 185642c1b001SThomas Moestl v = bus_space_read_4(t, h, GEM_MAC_RX_CONFIG); 185742c1b001SThomas Moestl 1858336cca9eSBenno Rice /* 1859336cca9eSBenno Rice * Turn off promiscuous mode, promiscuous group mode (all multicast), 1860336cca9eSBenno Rice * and hash filter. Depending on the case, the right bit will be 1861336cca9eSBenno Rice * enabled. 1862336cca9eSBenno Rice */ 1863336cca9eSBenno Rice v &= ~(GEM_MAC_RX_PROMISCUOUS|GEM_MAC_RX_HASH_FILTER| 1864336cca9eSBenno Rice GEM_MAC_RX_PROMISC_GRP); 1865336cca9eSBenno Rice 186642c1b001SThomas Moestl if ((ifp->if_flags & IFF_PROMISC) != 0) { 1867336cca9eSBenno Rice /* Turn on promiscuous mode */ 186842c1b001SThomas Moestl v |= GEM_MAC_RX_PROMISCUOUS; 186942c1b001SThomas Moestl goto chipit; 187042c1b001SThomas Moestl } 187142c1b001SThomas Moestl if ((ifp->if_flags & IFF_ALLMULTI) != 0) { 187242c1b001SThomas Moestl hash[3] = hash[2] = hash[1] = hash[0] = 0xffff; 187342c1b001SThomas Moestl ifp->if_flags |= IFF_ALLMULTI; 1874336cca9eSBenno Rice v |= GEM_MAC_RX_PROMISC_GRP; 187542c1b001SThomas Moestl goto chipit; 187642c1b001SThomas Moestl } 187742c1b001SThomas Moestl 187842c1b001SThomas Moestl /* 187942c1b001SThomas Moestl * Set up multicast address filter by passing all multicast addresses 1880336cca9eSBenno Rice * through a crc generator, and then using the high order 8 bits as an 1881336cca9eSBenno Rice * index into the 256 bit logical address filter. The high order 4 1882336cca9eSBenno Rice * bits selects the word, while the other 4 bits select the bit within 1883336cca9eSBenno Rice * the word (where bit 0 is the MSB). 188442c1b001SThomas Moestl */ 188542c1b001SThomas Moestl 1886336cca9eSBenno Rice /* Clear hash table */ 1887336cca9eSBenno Rice memset(hash, 0, sizeof(hash)); 1888336cca9eSBenno Rice 1889fc74a9f9SBrooks Davis TAILQ_FOREACH(inm, &ifp->if_multiaddrs, ifma_link) { 189042c1b001SThomas Moestl if (inm->ifma_addr->sa_family != AF_LINK) 189142c1b001SThomas Moestl continue; 1892c240bd8cSMarius Strobl crc = ether_crc32_le(LLADDR((struct sockaddr_dl *) 1893c240bd8cSMarius Strobl inm->ifma_addr), ETHER_ADDR_LEN); 189442c1b001SThomas Moestl 189542c1b001SThomas Moestl /* Just want the 8 most significant bits. */ 189642c1b001SThomas Moestl crc >>= 24; 189742c1b001SThomas Moestl 189842c1b001SThomas Moestl /* Set the corresponding bit in the filter. */ 1899336cca9eSBenno Rice hash[crc >> 4] |= 1 << (15 - (crc & 15)); 1900336cca9eSBenno Rice } 1901336cca9eSBenno Rice 1902336cca9eSBenno Rice v |= GEM_MAC_RX_HASH_FILTER; 1903336cca9eSBenno Rice ifp->if_flags &= ~IFF_ALLMULTI; 1904336cca9eSBenno Rice 1905336cca9eSBenno Rice /* Now load the hash table into the chip (if we are using it) */ 1906336cca9eSBenno Rice for (i = 0; i < 16; i++) { 1907336cca9eSBenno Rice bus_space_write_4(t, h, 1908336cca9eSBenno Rice GEM_MAC_HASH0 + i * (GEM_MAC_HASH1-GEM_MAC_HASH0), 1909336cca9eSBenno Rice hash[i]); 191042c1b001SThomas Moestl } 191142c1b001SThomas Moestl 191242c1b001SThomas Moestl chipit: 191342c1b001SThomas Moestl bus_space_write_4(t, h, GEM_MAC_RX_CONFIG, v); 191442c1b001SThomas Moestl } 1915