1098ca2bdSWarner Losh /*- 242c1b001SThomas Moestl * Copyright (C) 2001 Eduardo Horvath. 342c1b001SThomas Moestl * All rights reserved. 442c1b001SThomas Moestl * 542c1b001SThomas Moestl * Redistribution and use in source and binary forms, with or without 642c1b001SThomas Moestl * modification, are permitted provided that the following conditions 742c1b001SThomas Moestl * are met: 842c1b001SThomas Moestl * 1. Redistributions of source code must retain the above copyright 942c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer. 1042c1b001SThomas Moestl * 2. Redistributions in binary form must reproduce the above copyright 1142c1b001SThomas Moestl * notice, this list of conditions and the following disclaimer in the 1242c1b001SThomas Moestl * documentation and/or other materials provided with the distribution. 1342c1b001SThomas Moestl * 1442c1b001SThomas Moestl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 1542c1b001SThomas Moestl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1642c1b001SThomas Moestl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1742c1b001SThomas Moestl * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 1842c1b001SThomas Moestl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1942c1b001SThomas Moestl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2042c1b001SThomas Moestl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2142c1b001SThomas Moestl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2242c1b001SThomas Moestl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2342c1b001SThomas Moestl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2442c1b001SThomas Moestl * SUCH DAMAGE. 2542c1b001SThomas Moestl * 26336cca9eSBenno Rice * from: NetBSD: gemvar.h,v 1.8 2002/05/15 02:36:12 matt Exp 2742c1b001SThomas Moestl * 2842c1b001SThomas Moestl * $FreeBSD$ 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 /* 3842c1b001SThomas Moestl * Transmit descriptor list 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 /* 532a79fd39SMarius Strobl * Receive descriptor list 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 /* 672a79fd39SMarius Strobl * Control structures are DMA'd to the GEM 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 */ 8642c1b001SThomas Moestl int txs_firstdesc; /* first descriptor in packet */ 8742c1b001SThomas Moestl int txs_lastdesc; /* last descriptor in packet */ 8842c1b001SThomas Moestl 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 { 107fc74a9f9SBrooks Davis struct ifnet *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 */ 1158cb37876SMarius Strobl int sc_wdog_timer; /* watchdog timer */ 11642c1b001SThomas Moestl 117e1bb13cdSPoul-Henning Kamp void *sc_ih; 118bd3d9826SMarius Strobl struct resource *sc_res[3]; 119bd3d9826SMarius Strobl #define GEM_RES_INTR 0 120bd3d9826SMarius Strobl #define GEM_RES_BANK1 1 121bd3d9826SMarius Strobl #define GEM_RES_BANK2 2 122bd3d9826SMarius Strobl 1232a79fd39SMarius Strobl bus_dma_tag_t sc_pdmatag; /* parent bus DMA tag */ 1242a79fd39SMarius Strobl bus_dma_tag_t sc_rdmatag; /* RX bus DMA tag */ 1252a79fd39SMarius Strobl bus_dma_tag_t sc_tdmatag; /* TX bus DMA tag */ 1262a79fd39SMarius Strobl bus_dma_tag_t sc_cdmatag; /* control data bus DMA tag */ 1272a79fd39SMarius Strobl bus_dmamap_t sc_dmamap; /* bus DMA handle */ 12842c1b001SThomas Moestl 1292a79fd39SMarius Strobl int sc_phyad; /* PHY to use or -1 for any */ 13042c1b001SThomas Moestl 1312a79fd39SMarius Strobl u_int sc_variant; 132336cca9eSBenno Rice #define GEM_UNKNOWN 0 /* don't know */ 1331ed3fed7SMarius Strobl #define GEM_SUN_GEM 1 /* Sun GEM */ 1341ed3fed7SMarius Strobl #define GEM_SUN_ERI 2 /* Sun ERI */ 1351ed3fed7SMarius Strobl #define GEM_APPLE_GMAC 3 /* Apple GMAC */ 1361ed3fed7SMarius Strobl #define GEM_APPLE_K2_GMAC 4 /* Apple K2 GMAC */ 1371ed3fed7SMarius Strobl 1381ed3fed7SMarius Strobl #define GEM_IS_APPLE(sc) \ 1391ed3fed7SMarius Strobl ((sc)->sc_variant == GEM_APPLE_GMAC || \ 1401ed3fed7SMarius Strobl (sc)->sc_variant == GEM_APPLE_K2_GMAC) 141336cca9eSBenno Rice 1422a79fd39SMarius Strobl u_int sc_flags; 1432a79fd39SMarius Strobl #define GEM_INITED (1 << 0) /* reset persistent regs init'ed */ 1441ed3fed7SMarius Strobl #define GEM_LINK (1 << 1) /* link is up */ 1452a79fd39SMarius Strobl #define GEM_PCI (1 << 2) /* PCI busses are little-endian */ 1461ed3fed7SMarius Strobl #define GEM_SERDES (1 << 3) /* use the SERDES */ 14742c1b001SThomas Moestl 14842c1b001SThomas Moestl /* 1492a79fd39SMarius Strobl * ring buffer DMA stuff 15042c1b001SThomas Moestl */ 15142c1b001SThomas Moestl bus_dma_segment_t sc_cdseg; /* control data memory */ 15242c1b001SThomas Moestl int sc_cdnseg; /* number of segments */ 15342c1b001SThomas Moestl bus_dmamap_t sc_cddmamap; /* control data DMA map */ 15442c1b001SThomas Moestl bus_addr_t sc_cddma; 15542c1b001SThomas Moestl 15642c1b001SThomas Moestl /* 1572a79fd39SMarius Strobl * software state for transmit and receive descriptors 15842c1b001SThomas Moestl */ 15942c1b001SThomas Moestl struct gem_txsoft sc_txsoft[GEM_TXQUEUELEN]; 16042c1b001SThomas Moestl struct gem_rxsoft sc_rxsoft[GEM_NRXDESC]; 16142c1b001SThomas Moestl 16242c1b001SThomas Moestl /* 1632a79fd39SMarius Strobl * control data structures 16442c1b001SThomas Moestl */ 16542c1b001SThomas Moestl struct gem_control_data *sc_control_data; 16642c1b001SThomas Moestl #define sc_txdescs sc_control_data->gcd_txdescs 16742c1b001SThomas Moestl #define sc_rxdescs sc_control_data->gcd_rxdescs 16842c1b001SThomas Moestl 1692a79fd39SMarius Strobl int sc_txfree; /* number of free TX descriptors */ 1702a79fd39SMarius Strobl int sc_txnext; /* next ready TX descriptor */ 1712a79fd39SMarius Strobl int sc_txwin; /* TX desc. since last TX intr. */ 17242c1b001SThomas Moestl 1732a79fd39SMarius Strobl struct gem_txsq sc_txfreeq; /* free TX descsofts */ 1742a79fd39SMarius Strobl struct gem_txsq sc_txdirtyq; /* dirty TX descsofts */ 17542c1b001SThomas Moestl 1762a79fd39SMarius Strobl int sc_rxptr; /* next ready RX desc./descsoft */ 1772a79fd39SMarius Strobl int sc_rxfifosize; /* RX FIFO size (bytes) */ 17842c1b001SThomas Moestl 179336cca9eSBenno Rice int sc_ifflags; 18012fb0330SPyun YongHyeon int sc_csum_features; 18142c1b001SThomas Moestl }; 18242c1b001SThomas Moestl 183bd3d9826SMarius Strobl #define GEM_BANKN_BARRIER(n, sc, offs, len, flags) \ 184bd3d9826SMarius Strobl bus_barrier((sc)->sc_res[(n)], (offs), (len), (flags)) 185bd3d9826SMarius Strobl #define GEM_BANK1_BARRIER(sc, offs, len, flags) \ 186bd3d9826SMarius Strobl GEM_BANKN_BARRIER(GEM_RES_BANK1, (sc), (offs), (len), (flags)) 187bd3d9826SMarius Strobl #define GEM_BANK2_BARRIER(sc, offs, len, flags) \ 188bd3d9826SMarius Strobl GEM_BANKN_BARRIER(GEM_RES_BANK2, (sc), (offs), (len), (flags)) 189bd3d9826SMarius Strobl 190bd3d9826SMarius Strobl #define GEM_BANKN_READ_M(n, m, sc, offs) \ 191bd3d9826SMarius Strobl bus_read_ ## m((sc)->sc_res[(n)], (offs)) 192bd3d9826SMarius Strobl #define GEM_BANK1_READ_1(sc, offs) \ 193bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK1, 1, (sc), (offs)) 194bd3d9826SMarius Strobl #define GEM_BANK1_READ_2(sc, offs) \ 195bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK1, 2, (sc), (offs)) 196bd3d9826SMarius Strobl #define GEM_BANK1_READ_4(sc, offs) \ 197bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK1, 4, (sc), (offs)) 198bd3d9826SMarius Strobl #define GEM_BANK2_READ_1(sc, offs) \ 199bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK2, 1, (sc), (offs)) 200bd3d9826SMarius Strobl #define GEM_BANK2_READ_2(sc, offs) \ 201bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK2, 2, (sc), (offs)) 202bd3d9826SMarius Strobl #define GEM_BANK2_READ_4(sc, offs) \ 203bd3d9826SMarius Strobl GEM_BANKN_READ_M(GEM_RES_BANK2, 4, (sc), (offs)) 204bd3d9826SMarius Strobl 205bd3d9826SMarius Strobl #define GEM_BANKN_WRITE_M(n, m, sc, offs, v) \ 206bd3d9826SMarius Strobl bus_write_ ## m((sc)->sc_res[n], (offs), (v)) 207bd3d9826SMarius Strobl #define GEM_BANK1_WRITE_1(sc, offs, v) \ 208bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK1, 1, (sc), (offs), (v)) 209bd3d9826SMarius Strobl #define GEM_BANK1_WRITE_2(sc, offs, v) \ 210bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK1, 2, (sc), (offs), (v)) 211bd3d9826SMarius Strobl #define GEM_BANK1_WRITE_4(sc, offs, v) \ 212bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK1, 4, (sc), (offs), (v)) 213bd3d9826SMarius Strobl #define GEM_BANK2_WRITE_1(sc, offs, v) \ 214bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK2, 1, (sc), (offs), (v)) 215bd3d9826SMarius Strobl #define GEM_BANK2_WRITE_2(sc, offs, v) \ 216bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK2, 2, (sc), (offs), (v)) 217bd3d9826SMarius Strobl #define GEM_BANK2_WRITE_4(sc, offs, v) \ 218bd3d9826SMarius Strobl GEM_BANKN_WRITE_M(GEM_RES_BANK2, 4, (sc), (offs), (v)) 219bd3d9826SMarius Strobl 2202a79fd39SMarius Strobl /* XXX this should be handled by bus_dma(9). */ 2211ed3fed7SMarius Strobl #define GEM_DMA_READ(sc, v) \ 2221ed3fed7SMarius Strobl ((((sc)->sc_flags & GEM_PCI) != 0) ? le64toh(v) : be64toh(v)) 2231ed3fed7SMarius Strobl #define GEM_DMA_WRITE(sc, v) \ 2241ed3fed7SMarius Strobl ((((sc)->sc_flags & GEM_PCI) != 0) ? htole64(v) : htobe64(v)) 22542c1b001SThomas Moestl 22642c1b001SThomas Moestl #define GEM_CDTXADDR(sc, x) ((sc)->sc_cddma + GEM_CDTXOFF((x))) 22742c1b001SThomas Moestl #define GEM_CDRXADDR(sc, x) ((sc)->sc_cddma + GEM_CDRXOFF((x))) 22842c1b001SThomas Moestl 229b2d59f42SThomas Moestl #define GEM_CDSYNC(sc, ops) \ 2301ed3fed7SMarius Strobl bus_dmamap_sync((sc)->sc_cdmatag, (sc)->sc_cddmamap, (ops)); 23142c1b001SThomas Moestl 23242c1b001SThomas Moestl #define GEM_INIT_RXDESC(sc, x) \ 23342c1b001SThomas Moestl do { \ 23442c1b001SThomas Moestl struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)]; \ 23542c1b001SThomas Moestl struct gem_desc *__rxd = &sc->sc_rxdescs[(x)]; \ 23642c1b001SThomas Moestl struct mbuf *__m = __rxs->rxs_mbuf; \ 23742c1b001SThomas Moestl \ 23842c1b001SThomas Moestl __m->m_data = __m->m_ext.ext_buf; \ 23942c1b001SThomas Moestl __rxd->gd_addr = \ 24042c1b001SThomas Moestl GEM_DMA_WRITE((sc), __rxs->rxs_paddr); \ 24142c1b001SThomas Moestl __rxd->gd_flags = \ 24242c1b001SThomas Moestl GEM_DMA_WRITE((sc), \ 24342c1b001SThomas Moestl (((__m->m_ext.ext_size) << GEM_RD_BUFSHIFT) \ 24442c1b001SThomas Moestl & GEM_RD_BUFSIZE) | GEM_RD_OWN); \ 24542c1b001SThomas Moestl } while (0) 24642c1b001SThomas Moestl 2471ed3fed7SMarius Strobl #define GEM_UPDATE_RXDESC(sc, x) \ 2481ed3fed7SMarius Strobl do { \ 2491ed3fed7SMarius Strobl struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)]; \ 2501ed3fed7SMarius Strobl struct gem_desc *__rxd = &sc->sc_rxdescs[(x)]; \ 2511ed3fed7SMarius Strobl struct mbuf *__m = __rxs->rxs_mbuf; \ 2521ed3fed7SMarius Strobl \ 2531ed3fed7SMarius Strobl __rxd->gd_flags = \ 2541ed3fed7SMarius Strobl GEM_DMA_WRITE((sc), \ 2551ed3fed7SMarius Strobl (((__m->m_ext.ext_size) << GEM_RD_BUFSHIFT) \ 2561ed3fed7SMarius Strobl & GEM_RD_BUFSIZE) | GEM_RD_OWN); \ 2571ed3fed7SMarius Strobl } while (0) 2581ed3fed7SMarius Strobl 2598cfaff7dSMarius Strobl #define GEM_LOCK_INIT(_sc, _name) \ 2608cfaff7dSMarius Strobl mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF) 2618cfaff7dSMarius Strobl #define GEM_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 2628cfaff7dSMarius Strobl #define GEM_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 2638cfaff7dSMarius Strobl #define GEM_LOCK_ASSERT(_sc, _what) mtx_assert(&(_sc)->sc_mtx, (_what)) 2648cfaff7dSMarius Strobl #define GEM_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx) 2658cfaff7dSMarius Strobl 26642c1b001SThomas Moestl #ifdef _KERNEL 26742c1b001SThomas Moestl extern devclass_t gem_devclass; 26842c1b001SThomas Moestl 2692a79fd39SMarius Strobl int gem_attach(struct gem_softc *sc); 2702a79fd39SMarius Strobl void gem_detach(struct gem_softc *sc); 2712a79fd39SMarius Strobl void gem_intr(void *v); 2722a79fd39SMarius Strobl void gem_resume(struct gem_softc *sc); 2732a79fd39SMarius Strobl void gem_suspend(struct gem_softc *sc); 27442c1b001SThomas Moestl 2752a79fd39SMarius Strobl int gem_mediachange(struct ifnet *ifp); 2762a79fd39SMarius Strobl void gem_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr); 27742c1b001SThomas Moestl 27842c1b001SThomas Moestl /* MII methods & callbacks */ 2792a79fd39SMarius Strobl int gem_mii_readreg(device_t dev, int phy, int reg); 2802a79fd39SMarius Strobl void gem_mii_statchg(device_t dev); 2812a79fd39SMarius Strobl int gem_mii_writereg(device_t dev, int phy, int reg, int val); 28242c1b001SThomas Moestl 28342c1b001SThomas Moestl #endif /* _KERNEL */ 28442c1b001SThomas Moestl 28542c1b001SThomas Moestl #endif 286