if_casvar.h revision 1.2
1/* $OpenBSD: if_casvar.h,v 1.2 2007/02/25 21:54:52 kettenis Exp $ */ 2 3/* 4 * 5 * Copyright (C) 2007 Mark Kettenis. 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_CASVAR_H 34#define _IF_CASVAR_H 35 36#include <sys/queue.h> 37#include <sys/timeout.h> 38 39/* 40 * Misc. definitions for Sun Cassini ethernet controllers. 41 */ 42 43/* 44 * Transmit descriptor ring size. This is arbitrary, but allocate 45 * enough descriptors for 64 pending transmissions and 16 segments 46 * per packet. 47 */ 48#define CAS_NTXSEGS 16 49 50#define CAS_TXQUEUELEN 64 51#define CAS_NTXDESC (CAS_TXQUEUELEN * CAS_NTXSEGS) 52#define CAS_NTXDESC_MASK (CAS_NTXDESC - 1) 53#define CAS_NEXTTX(x) ((x + 1) & CAS_NTXDESC_MASK) 54 55struct cas_sxd { 56 struct mbuf *sd_mbuf; 57 bus_dmamap_t sd_map; 58}; 59 60/* 61 * Receive descriptor ring size. We have one Rx buffer per incoming 62 * packet, so this logic is a little simpler. 63 */ 64#define CAS_NRXDESC 128 65#define CAS_NRXDESC_MASK (CAS_NRXDESC - 1) 66 67/* 68 * Receive completion ring size. 69 */ 70#define CAS_NRXCOMP 256 71#define CAS_NRXCOMP_MASK (CAS_NRXCOMP - 1) 72#define CAS_NEXTRX(x) ((x + 1) & CAS_NRXCOMP_MASK) 73 74/* 75 * Control structures are DMA'd to the Cassini chip. We allocate them in 76 * a single clump that maps to a single DMA segment to make several things 77 * easier. 78 */ 79struct cas_control_data { 80 /* 81 * The transmit descriptors. 82 */ 83 struct cas_desc ccd_txdescs[CAS_NTXDESC]; 84 85 /* 86 * The receive completions. 87 */ 88 struct cas_comp ccd_rxcomps[CAS_NRXCOMP]; 89 90 /* 91 * The receive descriptors. 92 */ 93 struct cas_desc ccd_rxdescs[CAS_NRXDESC]; 94}; 95 96#define CAS_CDOFF(x) offsetof(struct cas_control_data, x) 97#define CAS_CDTXOFF(x) CAS_CDOFF(ccd_txdescs[(x)]) 98#define CAS_CDRXOFF(x) CAS_CDOFF(ccd_rxdescs[(x)]) 99#define CAS_CDRXCOFF(x) CAS_CDOFF(ccd_rxcomps[(x)]) 100 101/* 102 * Software state for receive jobs. 103 */ 104struct cas_rxsoft { 105 struct mbuf *rxs_mbuf; /* head of our mbuf chain */ 106 bus_dmamap_t rxs_dmamap; /* our DMA map */ 107 bus_dma_segment_t rxs_dmaseg; /* our DMA segment */ 108 caddr_t rxs_kva; 109}; 110 111 112/* 113 * Table which describes the transmit threshold mode. We generally 114 * start at index 0. Whenever we get a transmit underrun, we increment 115 * our index, falling back if we encounter the NULL terminator. 116 */ 117struct cas_txthresh_tab { 118 u_int32_t txth_opmode; /* OPMODE bits */ 119 const char *txth_name; /* name of mode */ 120}; 121 122/* 123 * Some misc. statics, useful for debugging. 124 */ 125struct cas_stats { 126 u_long ts_tx_uf; /* transmit underflow errors */ 127 u_long ts_tx_to; /* transmit jabber timeouts */ 128 u_long ts_tx_ec; /* excessive collision count */ 129 u_long ts_tx_lc; /* late collision count */ 130}; 131 132/* 133 * Software state per device. 134 */ 135struct cas_softc { 136 struct device sc_dev; /* generic device information */ 137 struct arpcom sc_arpcom; /* ethernet common data */ 138 struct mii_data sc_mii; /* MII media control */ 139#define sc_media sc_mii.mii_media/* shorthand */ 140 struct timeout sc_tick_ch; /* tick callout */ 141 142 bus_space_tag_t sc_memt; 143 bus_space_handle_t sc_memh; 144 void *sc_ih; 145 146 bus_dma_tag_t sc_dmatag; /* bus dma tag */ 147 bus_dmamap_t sc_dmamap; /* bus dma handle */ 148 int sc_burst; /* DVMA burst size in effect */ 149 int sc_phys[2]; /* MII instance -> PHY map */ 150 151 int sc_if_flags; 152 153 int sc_mif_config; /* Selected MII reg setting */ 154 155 void *sc_sdhook; /* shutdown hook */ 156 void *sc_powerhook; /* power management hook */ 157 158 struct cas_stats sc_stats; /* debugging stats */ 159 160 /* 161 * Ring buffer DMA stuff. 162 */ 163 bus_dma_segment_t sc_cdseg; /* control data memory */ 164 int sc_cdnseg; /* number of segments */ 165 bus_dmamap_t sc_cddmamap; /* control data DMA map */ 166#define sc_cddma sc_cddmamap->dm_segs[0].ds_addr 167 168 /* 169 * Software state for transmit and receive descriptors. 170 */ 171 struct cas_sxd sc_txd[CAS_NTXDESC]; 172 u_int32_t sc_tx_cnt, sc_tx_prod, sc_tx_cons; 173 174 struct cas_rxsoft sc_rxsoft[CAS_NRXDESC]; 175 176 /* 177 * Control data structures. 178 */ 179 struct cas_control_data *sc_control_data; 180#define sc_txdescs sc_control_data->ccd_txdescs 181#define sc_rxdescs sc_control_data->ccd_rxdescs 182#define sc_rxcomps sc_control_data->ccd_rxcomps 183 184 int sc_txfree; /* number of free Tx descriptors */ 185 int sc_txnext; /* next ready Tx descriptor */ 186 187 u_int32_t sc_tdctl_ch; /* conditional desc chaining */ 188 u_int32_t sc_tdctl_er; /* conditional desc end-of-ring */ 189 190 u_int32_t sc_setup_fsls; /* FS|LS on setup descriptor */ 191 192 int sc_rxptr; /* next ready RX descriptor/descsoft */ 193 int sc_rxfifosize; 194 int sc_rxdptr; 195 196 /* ========== */ 197 int sc_inited; 198 int sc_debug; 199 void *sc_sh; /* shutdownhook cookie */ 200}; 201 202#define CAS_DMA_READ(v) letoh64(v) 203#define CAS_DMA_WRITE(v) htole64(v) 204 205/* 206 * This macro returns the current media entry for *non-MII* media. 207 */ 208#define CAS_CURRENT_MEDIA(sc) \ 209 (IFM_SUBTYPE((sc)->sc_mii.mii_media.ifm_cur->ifm_media) != IFM_AUTO ? \ 210 (sc)->sc_mii.mii_media.ifm_cur : (sc)->sc_nway_active) 211 212/* 213 * This macro determines if a change to media-related OPMODE bits requires 214 * a chip reset. 215 */ 216#define CAS_MEDIA_NEEDSRESET(sc, newbits) \ 217 (((sc)->sc_opmode & OPMODE_MEDIA_BITS) != \ 218 ((newbits) & OPMODE_MEDIA_BITS)) 219 220#define CAS_CDTXADDR(sc, x) ((sc)->sc_cddma + CAS_CDTXOFF((x))) 221#define CAS_CDRXADDR(sc, x) ((sc)->sc_cddma + CAS_CDRXOFF((x))) 222#define CAS_CDRXCADDR(sc, x) ((sc)->sc_cddma + CAS_CDRXCOFF((x))) 223 224#define CAS_CDTXSYNC(sc, x, n, ops) \ 225do { \ 226 int __x, __n; \ 227 \ 228 __x = (x); \ 229 __n = (n); \ 230 \ 231 /* If it will wrap around, sync to the end of the ring. */ \ 232 if ((__x + __n) > CAS_NTXDESC) { \ 233 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 234 CAS_CDTXOFF(__x), sizeof(struct cas_desc) * \ 235 (CAS_NTXDESC - __x), (ops)); \ 236 __n -= (CAS_NTXDESC - __x); \ 237 __x = 0; \ 238 } \ 239 \ 240 /* Now sync whatever is left. */ \ 241 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 242 CAS_CDTXOFF(__x), sizeof(struct cas_desc) * __n, (ops)); \ 243} while (0) 244 245#define CAS_CDRXSYNC(sc, x, ops) \ 246 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 247 CAS_CDRXOFF((x)), sizeof(struct cas_desc), (ops)) 248 249#define CAS_CDRXCSYNC(sc, x, ops) \ 250 bus_dmamap_sync((sc)->sc_dmatag, (sc)->sc_cddmamap, \ 251 CAS_CDRXCOFF((x)), sizeof(struct cas_desc), (ops)) 252 253#define CAS_INIT_RXDESC(sc, d, s) \ 254do { \ 255 struct cas_rxsoft *__rxs = &sc->sc_rxsoft[(s)]; \ 256 struct cas_desc *__rxd = &sc->sc_rxdescs[(d)]; \ 257 \ 258 __rxd->cd_addr = \ 259 CAS_DMA_WRITE(__rxs->rxs_dmamap->dm_segs[0].ds_addr); \ 260 __rxd->cd_flags = \ 261 CAS_DMA_WRITE((s)); \ 262 CAS_CDRXSYNC((sc), (d), BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); \ 263} while (0) 264 265#ifdef _KERNEL 266int cas_mediachange(struct ifnet *); 267void cas_mediastatus(struct ifnet *, struct ifmediareq *); 268 269void cas_config(struct cas_softc *); 270void cas_reset(struct cas_softc *); 271int cas_intr(void *); 272#endif /* _KERNEL */ 273 274 275#endif 276