1098ca2bdSWarner Losh /*- 2*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 442c1b001SThomas Moestl * Copyright (C) 2001 Eduardo Horvath. 542c1b001SThomas Moestl * All rights reserved. 642c1b001SThomas Moestl * 742c1b001SThomas Moestl * Redistribution and use in source and binary forms, with or without 842c1b001SThomas Moestl * modification, are permitted provided that the following conditions 942c1b001SThomas Moestl * are met: 1042c1b001SThomas Moestl * 1. Redistributions of source code must retain the above copyright 1142c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer. 1242c1b001SThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 1342c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer in the 1442c1b001SThomas Moestl * documentation and/or other materials provided with the distribution. 1542c1b001SThomas Moestl * 1642c1b001SThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 1742c1b001SThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1842c1b001SThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1942c1b001SThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 2042c1b001SThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2142c1b001SThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2242c1b001SThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2342c1b001SThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2442c1b001SThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2542c1b001SThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2642c1b001SThomas Moestl * SUCH DAMAGE. 2742c1b001SThomas Moestl * 28336cca9eSBenno Rice * from: NetBSD: gemvar.h,v 1.8 2002/05/15 02:36:12 matt Exp 2942c1b001SThomas Moestl */ 3042c1b001SThomas Moestl 3142c1b001SThomas Moestl #ifndef _IF_GEMVAR_H 3242c1b001SThomas Moestl #define _IF_GEMVAR_H 3342c1b001SThomas Moestl 3442c1b001SThomas Moestl #include <sys/queue.h> 3542c1b001SThomas Moestl #include <sys/callout.h> 3642c1b001SThomas Moestl 3742c1b001SThomas Moestl /* 389ba2b298SMarius Strobl * Transmit descriptor ring size - this is arbitrary, but allocate 3942c1b001SThomas Moestl * enough descriptors for 64 pending transmissions and 16 segments 402a79fd39SMarius Strobl * per packet. This limit is not actually enforced (packets with 412a79fd39SMarius Strobl * more segments can be sent, depending on the busdma backend); it 422a79fd39SMarius Strobl * is however used as an estimate for the TX window size. 4342c1b001SThomas Moestl */ 4442c1b001SThomas Moestl #define GEM_NTXSEGS 16 4542c1b001SThomas Moestl 4642c1b001SThomas Moestl #define GEM_TXQUEUELEN 64 4742c1b001SThomas Moestl #define GEM_NTXDESC (GEM_TXQUEUELEN * GEM_NTXSEGS) 48305f2c06SThomas Moestl #define GEM_MAXTXFREE (GEM_NTXDESC - 1) 4942c1b001SThomas Moestl #define GEM_NTXDESC_MASK (GEM_NTXDESC - 1) 5042c1b001SThomas Moestl #define GEM_NEXTTX(x) ((x + 1) & GEM_NTXDESC_MASK) 5142c1b001SThomas Moestl 5242c1b001SThomas Moestl /* 539ba2b298SMarius Strobl * Receive descriptor ring size - we have one RX buffer per incoming 5442c1b001SThomas Moestl * packet, so this logic is a little simpler. 5542c1b001SThomas Moestl */ 561ed3fed7SMarius Strobl #define GEM_NRXDESC 256 5742c1b001SThomas Moestl #define GEM_NRXDESC_MASK (GEM_NRXDESC - 1) 5842c1b001SThomas Moestl #define GEM_NEXTRX(x) ((x + 1) & GEM_NRXDESC_MASK) 5942c1b001SThomas Moestl 6042c1b001SThomas Moestl /* 612a79fd39SMarius Strobl * How many ticks to wait until to retry on a RX descriptor that is 622a79fd39SMarius Strobl * still owned by the hardware. 630d80b9bdSThomas Moestl */ 640d80b9bdSThomas Moestl #define GEM_RXOWN_TICKS (hz / 50) 650d80b9bdSThomas Moestl 660d80b9bdSThomas Moestl /* 679ba2b298SMarius Strobl * Control structures are DMA'd to the chip. We allocate them 682a79fd39SMarius Strobl * in a single clump that maps to a single DMA segment to make 692a79fd39SMarius Strobl * several things easier. 7042c1b001SThomas Moestl */ 7142c1b001SThomas Moestl struct gem_control_data { 722a79fd39SMarius Strobl struct gem_desc gcd_txdescs[GEM_NTXDESC]; /* TX descriptors */ 732a79fd39SMarius Strobl struct gem_desc gcd_rxdescs[GEM_NRXDESC]; /* RX descriptors */ 7442c1b001SThomas Moestl }; 7542c1b001SThomas Moestl 7642c1b001SThomas Moestl #define GEM_CDOFF(x) offsetof(struct gem_control_data, x) 7742c1b001SThomas Moestl #define GEM_CDTXOFF(x) GEM_CDOFF(gcd_txdescs[(x)]) 7842c1b001SThomas Moestl #define GEM_CDRXOFF(x) GEM_CDOFF(gcd_rxdescs[(x)]) 7942c1b001SThomas Moestl 8042c1b001SThomas Moestl /* 812a79fd39SMarius Strobl * software state for transmit job mbufs (may be elements of mbuf chains) 8242c1b001SThomas Moestl */ 8342c1b001SThomas Moestl struct gem_txsoft { 8442c1b001SThomas Moestl struct mbuf *txs_mbuf; /* head of our mbuf chain */ 8542c1b001SThomas Moestl bus_dmamap_t txs_dmamap; /* our DMA map */ 869ba2b298SMarius Strobl u_int txs_firstdesc; /* first descriptor in packet */ 879ba2b298SMarius Strobl u_int txs_lastdesc; /* last descriptor in packet */ 889ba2b298SMarius Strobl u_int txs_ndescs; /* number of descriptors */ 8942c1b001SThomas Moestl STAILQ_ENTRY(gem_txsoft) txs_q; 9042c1b001SThomas Moestl }; 9142c1b001SThomas Moestl 9242c1b001SThomas Moestl STAILQ_HEAD(gem_txsq, gem_txsoft); 9342c1b001SThomas Moestl 9442c1b001SThomas Moestl /* 952a79fd39SMarius Strobl * software state for receive jobs 9642c1b001SThomas Moestl */ 9742c1b001SThomas Moestl struct gem_rxsoft { 9842c1b001SThomas Moestl struct mbuf *rxs_mbuf; /* head of our mbuf chain */ 9942c1b001SThomas Moestl bus_dmamap_t rxs_dmamap; /* our DMA map */ 10042c1b001SThomas Moestl bus_addr_t rxs_paddr; /* physical address of the segment */ 10142c1b001SThomas Moestl }; 10242c1b001SThomas Moestl 10342c1b001SThomas Moestl /* 1042a79fd39SMarius Strobl * software state per device 10542c1b001SThomas Moestl */ 10642c1b001SThomas Moestl struct gem_softc { 1079f012efbSJustin Hibbits if_t sc_ifp; 1081ed3fed7SMarius Strobl struct mtx sc_mtx; 10942c1b001SThomas Moestl device_t sc_miibus; 11042c1b001SThomas Moestl struct mii_data *sc_mii; /* MII media control */ 11142c1b001SThomas Moestl device_t sc_dev; /* generic device information */ 1121ed3fed7SMarius Strobl u_char sc_enaddr[ETHER_ADDR_LEN]; 11342c1b001SThomas Moestl struct callout sc_tick_ch; /* tick callout */ 1142a79fd39SMarius Strobl struct callout sc_rx_ch; /* delayed RX callout */ 1159ba2b298SMarius Strobl u_int sc_wdog_timer; /* watchdog timer */ 11642c1b001SThomas Moestl 117e1bb13cdSPoul-Henning Kamp void *sc_ih; 1188defc88cSMarius Strobl struct resource *sc_res[2]; 119bd3d9826SMarius Strobl #define GEM_RES_INTR 0 1208defc88cSMarius Strobl #define GEM_RES_MEM 1 121bd3d9826SMarius Strobl 1222a79fd39SMarius Strobl bus_dma_tag_t sc_pdmatag; /* parent bus DMA tag */ 1232a79fd39SMarius Strobl bus_dma_tag_t sc_rdmatag; /* RX bus DMA tag */ 1242a79fd39SMarius Strobl bus_dma_tag_t sc_tdmatag; /* TX bus DMA tag */ 1252a79fd39SMarius Strobl bus_dma_tag_t sc_cdmatag; /* control data bus DMA tag */ 1262a79fd39SMarius Strobl bus_dmamap_t sc_dmamap; /* bus DMA handle */ 12742c1b001SThomas Moestl 1282a79fd39SMarius Strobl u_int sc_variant; 129336cca9eSBenno Rice #define GEM_UNKNOWN 0 /* don't know */ 1301ed3fed7SMarius Strobl #define GEM_SUN_GEM 1 /* Sun GEM */ 1318defc88cSMarius Strobl #define GEM_APPLE_GMAC 2 /* Apple GMAC */ 1328defc88cSMarius Strobl #define GEM_APPLE_K2_GMAC 3 /* Apple K2 GMAC */ 1331ed3fed7SMarius Strobl 1341ed3fed7SMarius Strobl #define GEM_IS_APPLE(sc) \ 1351ed3fed7SMarius Strobl ((sc)->sc_variant == GEM_APPLE_GMAC || \ 1361ed3fed7SMarius Strobl (sc)->sc_variant == GEM_APPLE_K2_GMAC) 137336cca9eSBenno Rice 1382a79fd39SMarius Strobl u_int sc_flags; 1392a79fd39SMarius Strobl #define GEM_INITED (1 << 0) /* reset persistent regs init'ed */ 140b3a1f860SMarius Strobl #define GEM_LINK (1 << 1) /* link is up */ 1418defc88cSMarius Strobl #define GEM_PCI66 (1 << 2) /* PCI bus runs at 66MHz */ 1428defc88cSMarius Strobl #define GEM_SERDES (1 << 3) /* use the SERDES */ 14342c1b001SThomas Moestl 14442c1b001SThomas Moestl /* 1452a79fd39SMarius Strobl * ring buffer DMA stuff 14642c1b001SThomas Moestl */ 14742c1b001SThomas Moestl bus_dmamap_t sc_cddmamap; /* control data DMA map */ 14842c1b001SThomas Moestl bus_addr_t sc_cddma; 14942c1b001SThomas Moestl 15042c1b001SThomas Moestl /* 1512a79fd39SMarius Strobl * software state for transmit and receive descriptors 15242c1b001SThomas Moestl */ 15342c1b001SThomas Moestl struct gem_txsoft sc_txsoft[GEM_TXQUEUELEN]; 15442c1b001SThomas Moestl struct gem_rxsoft sc_rxsoft[GEM_NRXDESC]; 15542c1b001SThomas Moestl 15642c1b001SThomas Moestl /* 1572a79fd39SMarius Strobl * control data structures 15842c1b001SThomas Moestl */ 15942c1b001SThomas Moestl struct gem_control_data *sc_control_data; 16042c1b001SThomas Moestl #define sc_txdescs sc_control_data->gcd_txdescs 16142c1b001SThomas Moestl #define sc_rxdescs sc_control_data->gcd_rxdescs 16242c1b001SThomas Moestl 1639ba2b298SMarius Strobl u_int sc_txfree; /* number of free TX descriptors */ 1649ba2b298SMarius Strobl u_int sc_txnext; /* next ready TX descriptor */ 1659ba2b298SMarius Strobl u_int sc_txwin; /* TX desc. since last TX intr. */ 16642c1b001SThomas Moestl 1672a79fd39SMarius Strobl struct gem_txsq sc_txfreeq; /* free TX descsofts */ 1682a79fd39SMarius Strobl struct gem_txsq sc_txdirtyq; /* dirty TX descsofts */ 16942c1b001SThomas Moestl 1709ba2b298SMarius Strobl u_int sc_rxptr; /* next ready RX descriptor/state */ 1719ba2b298SMarius Strobl u_int sc_rxfifosize; /* RX FIFO size (bytes) */ 17242c1b001SThomas Moestl 173c0e3e9d4SMarius Strobl uint32_t sc_mac_rxcfg; /* RX MAC conf. % GEM_MAC_RX_ENABLE */ 174c0e3e9d4SMarius Strobl 175336cca9eSBenno Rice int sc_ifflags; 1769ba2b298SMarius Strobl u_long sc_csum_features; 17742c1b001SThomas Moestl }; 17842c1b001SThomas Moestl 1798defc88cSMarius Strobl #define GEM_BARRIER(sc, offs, len, flags) \ 1808defc88cSMarius Strobl bus_barrier((sc)->sc_res[GEM_RES_MEM], (offs), (len), (flags)) 181bd3d9826SMarius Strobl 1828defc88cSMarius Strobl #define GEM_READ_N(n, sc, offs) \ 1838defc88cSMarius Strobl bus_read_ ## n((sc)->sc_res[GEM_RES_MEM], (offs)) 1848defc88cSMarius Strobl #define GEM_READ_1(sc, offs) \ 1858defc88cSMarius Strobl GEM_READ_N(1, (sc), (offs)) 1868defc88cSMarius Strobl #define GEM_READ_2(sc, offs) \ 1878defc88cSMarius Strobl GEM_READ_N(2, (sc), (offs)) 1888defc88cSMarius Strobl #define GEM_READ_4(sc, offs) \ 1898defc88cSMarius Strobl GEM_READ_N(4, (sc), (offs)) 1908defc88cSMarius Strobl #define GEM_READ_1(sc, offs) \ 1918defc88cSMarius Strobl GEM_READ_N(1, (sc), (offs)) 1928defc88cSMarius Strobl #define GEM_READ_2(sc, offs) \ 1938defc88cSMarius Strobl GEM_READ_N(2, (sc), (offs)) 1948defc88cSMarius Strobl #define GEM_READ_4(sc, offs) \ 1958defc88cSMarius Strobl GEM_READ_N(4, (sc), (offs)) 196bd3d9826SMarius Strobl 1978defc88cSMarius Strobl #define GEM_WRITE_N(n, sc, offs, v) \ 1988defc88cSMarius Strobl bus_write_ ## n((sc)->sc_res[GEM_RES_MEM], (offs), (v)) 1998defc88cSMarius Strobl #define GEM_WRITE_1(sc, offs, v) \ 2008defc88cSMarius Strobl GEM_WRITE_N(1, (sc), (offs), (v)) 2018defc88cSMarius Strobl #define GEM_WRITE_2(sc, offs, v) \ 2028defc88cSMarius Strobl GEM_WRITE_N(2, (sc), (offs), (v)) 2038defc88cSMarius Strobl #define GEM_WRITE_4(sc, offs, v) \ 2048defc88cSMarius Strobl GEM_WRITE_N(4, (sc), (offs), (v)) 2058defc88cSMarius Strobl #define GEM_WRITE_1(sc, offs, v) \ 2068defc88cSMarius Strobl GEM_WRITE_N(1, (sc), (offs), (v)) 2078defc88cSMarius Strobl #define GEM_WRITE_2(sc, offs, v) \ 2088defc88cSMarius Strobl GEM_WRITE_N(2, (sc), (offs), (v)) 2098defc88cSMarius Strobl #define GEM_WRITE_4(sc, offs, v) \ 2108defc88cSMarius Strobl GEM_WRITE_N(4, (sc), (offs), (v)) 21142c1b001SThomas Moestl 21242c1b001SThomas Moestl #define GEM_CDTXADDR(sc, x) ((sc)->sc_cddma + GEM_CDTXOFF((x))) 21342c1b001SThomas Moestl #define GEM_CDRXADDR(sc, x) ((sc)->sc_cddma + GEM_CDRXOFF((x))) 21442c1b001SThomas Moestl 215b2d59f42SThomas Moestl #define GEM_CDSYNC(sc, ops) \ 2161ed3fed7SMarius Strobl bus_dmamap_sync((sc)->sc_cdmatag, (sc)->sc_cddmamap, (ops)); 21742c1b001SThomas Moestl 21842c1b001SThomas Moestl #define GEM_INIT_RXDESC(sc, x) \ 21942c1b001SThomas Moestl do { \ 22042c1b001SThomas Moestl struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)]; \ 22142c1b001SThomas Moestl struct gem_desc *__rxd = &sc->sc_rxdescs[(x)]; \ 22242c1b001SThomas Moestl struct mbuf *__m = __rxs->rxs_mbuf; \ 22342c1b001SThomas Moestl \ 22442c1b001SThomas Moestl __m->m_data = __m->m_ext.ext_buf; \ 2258defc88cSMarius Strobl __rxd->gd_addr = htole64(__rxs->rxs_paddr); \ 2268defc88cSMarius Strobl __rxd->gd_flags = htole64((((__m->m_ext.ext_size) << \ 2278defc88cSMarius Strobl GEM_RD_BUFSHIFT) & GEM_RD_BUFSIZE) | GEM_RD_OWN); \ 22842c1b001SThomas Moestl } while (0) 22942c1b001SThomas Moestl 2301ed3fed7SMarius Strobl #define GEM_UPDATE_RXDESC(sc, x) \ 2311ed3fed7SMarius Strobl do { \ 2321ed3fed7SMarius Strobl struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)]; \ 2331ed3fed7SMarius Strobl struct gem_desc *__rxd = &sc->sc_rxdescs[(x)]; \ 2341ed3fed7SMarius Strobl struct mbuf *__m = __rxs->rxs_mbuf; \ 2351ed3fed7SMarius Strobl \ 2368defc88cSMarius Strobl __rxd->gd_flags = htole64((((__m->m_ext.ext_size) << \ 2378defc88cSMarius Strobl GEM_RD_BUFSHIFT) & GEM_RD_BUFSIZE) | GEM_RD_OWN); \ 2381ed3fed7SMarius Strobl } while (0) 2391ed3fed7SMarius Strobl 2408cfaff7dSMarius Strobl #define GEM_LOCK_INIT(_sc, _name) \ 2418cfaff7dSMarius Strobl mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF) 2428cfaff7dSMarius Strobl #define GEM_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 2438cfaff7dSMarius Strobl #define GEM_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 2448cfaff7dSMarius Strobl #define GEM_LOCK_ASSERT(_sc, _what) mtx_assert(&(_sc)->sc_mtx, (_what)) 2458cfaff7dSMarius Strobl #define GEM_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 2468cfaff7dSMarius Strobl 24742c1b001SThomas Moestl #ifdef _KERNEL 2482a79fd39SMarius Strobl int gem_attach(struct gem_softc *sc); 2492a79fd39SMarius Strobl void gem_detach(struct gem_softc *sc); 2502a79fd39SMarius Strobl void gem_intr(void *v); 2512a79fd39SMarius Strobl void gem_resume(struct gem_softc *sc); 2522a79fd39SMarius Strobl void gem_suspend(struct gem_softc *sc); 25342c1b001SThomas Moestl 2549f012efbSJustin Hibbits int gem_mediachange(if_t ifp); 2559f012efbSJustin Hibbits void gem_mediastatus(if_t ifp, struct ifmediareq *ifmr); 25642c1b001SThomas Moestl 25742c1b001SThomas Moestl /* MII methods & callbacks */ 2582a79fd39SMarius Strobl int gem_mii_readreg(device_t dev, int phy, int reg); 2592a79fd39SMarius Strobl void gem_mii_statchg(device_t dev); 2602a79fd39SMarius Strobl int gem_mii_writereg(device_t dev, int phy, int reg, int val); 26142c1b001SThomas Moestl 26242c1b001SThomas Moestl #endif /* _KERNEL */ 26342c1b001SThomas Moestl 26442c1b001SThomas Moestl #endif 265