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