1 /* $OpenBSD: gemvar.h,v 1.21 2009/03/22 21:46:31 kettenis Exp $ */ 2 /* $NetBSD: gemvar.h,v 1.1 2001/09/16 00:11:43 eeh Exp $ */ 3 4 /* 5 * 6 * Copyright (C) 2001 Eduardo Horvath. 7 * All rights reserved. 8 * 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 */ 32 33 #ifndef _IF_GEMVAR_H 34 #define _IF_GEMVAR_H 35 36 #include <sys/queue.h> 37 #include <sys/timeout.h> 38 39 /* 40 * Misc. definitions for the Sun ``Gem'' Ethernet controller family driver. 41 */ 42 43 /* 44 * Transmit descriptor list size. This is arbitrary, but allocate 45 * enough descriptors for 64 pending transmissions and 16 segments 46 * per packet. 47 */ 48 #define GEM_NTXSEGS 16 49 50 #define GEM_TXQUEUELEN 64 51 #define GEM_NTXDESC (GEM_TXQUEUELEN * GEM_NTXSEGS) 52 #define GEM_NTXDESC_MASK (GEM_NTXDESC - 1) 53 #define GEM_NEXTTX(x) ((x + 1) & GEM_NTXDESC_MASK) 54 55 struct gem_sxd { 56 struct mbuf *sd_mbuf; 57 bus_dmamap_t sd_map; 58 }; 59 60 /* 61 * Receive descriptor list size. We have one Rx buffer per incoming 62 * packet, so this logic is a little simpler. 63 */ 64 #define GEM_NRXDESC 128 65 #define GEM_NRXDESC_MASK (GEM_NRXDESC - 1) 66 #define GEM_NEXTRX(x) ((x + 1) & GEM_NRXDESC_MASK) 67 68 /* 69 * Control structures are DMA'd to the GEM chip. We allocate them in 70 * a single clump that maps to a single DMA segment to make several things 71 * easier. 72 */ 73 struct gem_control_data { 74 /* 75 * The transmit descriptors. 76 */ 77 struct gem_desc gcd_txdescs[GEM_NTXDESC]; 78 79 /* 80 * The receive descriptors. 81 */ 82 struct gem_desc gcd_rxdescs[GEM_NRXDESC]; 83 }; 84 85 #define GEM_CDOFF(x) offsetof(struct gem_control_data, x) 86 #define GEM_CDTXOFF(x) GEM_CDOFF(gcd_txdescs[(x)]) 87 #define GEM_CDRXOFF(x) GEM_CDOFF(gcd_rxdescs[(x)]) 88 89 /* 90 * Software state for receive jobs. 91 */ 92 struct gem_rxsoft { 93 struct mbuf *rxs_mbuf; /* head of our mbuf chain */ 94 bus_dmamap_t rxs_dmamap; /* our DMA map */ 95 }; 96 97 98 /* 99 * Table which describes the transmit threshold mode. We generally 100 * start at index 0. Whenever we get a transmit underrun, we increment 101 * our index, falling back if we encounter the NULL terminator. 102 */ 103 struct gem_txthresh_tab { 104 u_int32_t txth_opmode; /* OPMODE bits */ 105 const char *txth_name; /* name of mode */ 106 }; 107 108 /* 109 * Some misc. statics, useful for debugging. 110 */ 111 struct gem_stats { 112 u_long ts_tx_uf; /* transmit underflow errors */ 113 u_long ts_tx_to; /* transmit jabber timeouts */ 114 u_long ts_tx_ec; /* excessive collision count */ 115 u_long ts_tx_lc; /* late collision count */ 116 }; 117 118 /* 119 * Software state per device. 120 */ 121 struct gem_softc { 122 struct device sc_dev; /* generic device information */ 123 struct arpcom sc_arpcom; /* ethernet common data */ 124 struct mii_data sc_mii; /* MII media control */ 125 #define sc_media sc_mii.mii_media/* shorthand */ 126 struct timeout sc_tick_ch; /* tick callout */ 127 128 /* The following bus handles are to be provided by the bus front-end */ 129 bus_space_tag_t sc_bustag; /* bus tag */ 130 bus_dma_tag_t sc_dmatag; /* bus dma tag */ 131 bus_dmamap_t sc_dmamap; /* bus dma handle */ 132 bus_space_handle_t sc_h1; /* bus space handle for bank 1 regs */ 133 bus_space_handle_t sc_h2; /* bus space handle for bank 2 regs */ 134 #if 0 135 /* The following may be needed for SBus */ 136 bus_space_handle_t sc_seb; /* HME Global registers */ 137 bus_space_handle_t sc_erx; /* HME ERX registers */ 138 bus_space_handle_t sc_etx; /* HME ETX registers */ 139 bus_space_handle_t sc_mac; /* HME MAC registers */ 140 bus_space_handle_t sc_mif; /* HME MIF registers */ 141 #endif 142 int sc_burst; /* DVMA burst size in effect */ 143 144 int sc_mif_config; /* Selected MII reg setting */ 145 146 int sc_pci; /* XXXXX -- PCI buses are LE. */ 147 u_int sc_variant; /* which GEM are we dealing with? */ 148 #define GEM_UNKNOWN 0 /* don't know */ 149 #define GEM_SUN_GEM 1 /* Sun GEM */ 150 #define GEM_SUN_ERI 2 /* Sun ERI */ 151 #define GEM_APPLE_GMAC 3 /* Apple GMAC */ 152 #define GEM_APPLE_K2_GMAC 4 /* Apple K2 GMAC */ 153 154 u_int sc_flags; /* */ 155 #define GEM_GIGABIT 0x0001 /* has a gigabit PHY */ 156 157 158 void *sc_sdhook; /* shutdown hook */ 159 void *sc_powerhook; /* power management hook */ 160 161 struct gem_stats sc_stats; /* debugging stats */ 162 163 /* 164 * Ring buffer DMA stuff. 165 */ 166 bus_dma_segment_t sc_cdseg; /* control data memory */ 167 int sc_cdnseg; /* number of segments */ 168 bus_dmamap_t sc_cddmamap; /* control data DMA map */ 169 #define sc_cddma sc_cddmamap->dm_segs[0].ds_addr 170 171 /* 172 * Software state for transmit and receive descriptors. 173 */ 174 struct gem_sxd sc_txd[GEM_NTXDESC]; 175 u_int32_t sc_tx_cnt, sc_tx_prod, sc_tx_cons; 176 177 struct gem_rxsoft sc_rxsoft[GEM_NRXDESC]; 178 u_int32_t sc_rx_cnt, sc_rx_prod, sc_rx_cons; 179 180 /* 181 * Control data structures. 182 */ 183 struct gem_control_data *sc_control_data; 184 #define sc_txdescs sc_control_data->gcd_txdescs 185 #define sc_rxdescs sc_control_data->gcd_rxdescs 186 187 int sc_txfree; /* number of free Tx descriptors */ 188 int sc_txnext; /* next ready Tx descriptor */ 189 190 u_int32_t sc_tdctl_ch; /* conditional desc chaining */ 191 u_int32_t sc_tdctl_er; /* conditional desc end-of-ring */ 192 193 u_int32_t sc_setup_fsls; /* FS|LS on setup descriptor */ 194 195 int sc_rxfifosize; 196 197 u_int32_t sc_rx_fifo_wr_ptr; 198 u_int32_t sc_rx_fifo_rd_ptr; 199 struct timeout sc_rx_watchdog; 200 201 /* ========== */ 202 int sc_inited; 203 int sc_debug; 204 void *sc_sh; /* shutdownhook cookie */ 205 206 /* Special hardware hooks */ 207 void (*sc_hwreset)(struct gem_softc *); 208 void (*sc_hwinit)(struct gem_softc *); 209 }; 210 211 #define GEM_DMA_READ(sc, v) (((sc)->sc_pci) ? letoh64(v) : betoh64(v)) 212 #define GEM_DMA_WRITE(sc, v) (((sc)->sc_pci) ? htole64(v) : htobe64(v)) 213 214 /* 215 * This macro returns the current media entry for *non-MII* media. 216 */ 217 #define GEM_CURRENT_MEDIA(sc) \ 218 (IFM_SUBTYPE((sc)->sc_mii.mii_media.ifm_cur->ifm_media) != IFM_AUTO ? \ 219 (sc)->sc_mii.mii_media.ifm_cur : (sc)->sc_nway_active) 220 221 /* 222 * This macro determines if a change to media-related OPMODE bits requires 223 * a chip reset. 224 */ 225 #define GEM_MEDIA_NEEDSRESET(sc, newbits) \ 226 (((sc)->sc_opmode & OPMODE_MEDIA_BITS) != \ 227 ((newbits) & OPMODE_MEDIA_BITS)) 228 229 #define GEM_CDTXADDR(sc, x) ((sc)->sc_cddma + GEM_CDTXOFF((x))) 230 #define GEM_CDRXADDR(sc, x) ((sc)->sc_cddma + GEM_CDRXOFF((x))) 231 232 #define GEM_CDSPADDR(sc) ((sc)->sc_cddma + GEM_CDSPOFF) 233 234 #define GEM_CDTXSYNC(sc, x, n, ops) \ 235 do { \ 236 int __x, __n; \ 237 \ 238 __x = (x); \ 239 __n = (n); \ 240 \ 241 /* If it will wrap around, sync to the end of the ring. */ \ 242 if ((__x + __n) > GEM_NTXDESC) { \ 243 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 244 GEM_CDTXOFF(__x), sizeof(struct gem_desc) * \ 245 (GEM_NTXDESC - __x), (ops)); \ 246 __n -= (GEM_NTXDESC - __x); \ 247 __x = 0; \ 248 } \ 249 \ 250 /* Now sync whatever is left. */ \ 251 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 252 GEM_CDTXOFF(__x), sizeof(struct gem_desc) * __n, (ops)); \ 253 } while (0) 254 255 #define GEM_CDRXSYNC(sc, x, ops) \ 256 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 257 GEM_CDRXOFF((x)), sizeof(struct gem_desc), (ops)) 258 259 #define GEM_CDSPSYNC(sc, ops) \ 260 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 261 GEM_CDSPOFF, GEM_SETUP_PACKET_LEN, (ops)) 262 263 #define GEM_INIT_RXDESC(sc, x) \ 264 do { \ 265 struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)]; \ 266 struct gem_desc *__rxd = &sc->sc_rxdescs[(x)]; \ 267 struct mbuf *__m = __rxs->rxs_mbuf; \ 268 \ 269 __rxd->gd_addr = \ 270 GEM_DMA_WRITE((sc), __rxs->rxs_dmamap->dm_segs[0].ds_addr); \ 271 __rxd->gd_flags = \ 272 GEM_DMA_WRITE((sc), \ 273 (((__m->m_ext.ext_size)<<GEM_RD_BUFSHIFT) \ 274 & GEM_RD_BUFSIZE) | GEM_RD_OWN); \ 275 GEM_CDRXSYNC((sc), (x), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \ 276 } while (0) 277 278 #ifdef _KERNEL 279 void gem_attach(struct gem_softc *, const u_int8_t *); 280 int gem_activate(struct device *, enum devact); 281 int gem_detach(struct gem_softc *); 282 int gem_intr(void *); 283 int gem_read_srom(struct gem_softc *); 284 int gem_srom_crcok(const u_int8_t *); 285 int gem_isv_srom(const u_int8_t *); 286 int gem_isv_srom_enaddr(struct gem_softc *, u_int8_t *); 287 int gem_parse_old_srom(struct gem_softc *, u_int8_t *); 288 289 int gem_mediachange(struct ifnet *); 290 void gem_mediastatus(struct ifnet *, struct ifmediareq *); 291 292 void gem_config(struct gem_softc *); 293 void gem_reset(struct gem_softc *); 294 int gem_intr(void *); 295 #endif /* _KERNEL */ 296 297 #endif 298