xref: /freebsd/sys/dev/gem/if_gemvar.h (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2001 Eduardo Horvath.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	from: NetBSD: gemvar.h,v 1.8 2002/05/15 02:36:12 matt Exp
29  *
30  * $FreeBSD$
31  */
32 
33 #ifndef	_IF_GEMVAR_H
34 #define	_IF_GEMVAR_H
35 
36 #include <sys/queue.h>
37 #include <sys/callout.h>
38 
39 /*
40  * Transmit descriptor ring size - this is arbitrary, but allocate
41  * enough descriptors for 64 pending transmissions and 16 segments
42  * per packet.  This limit is not actually enforced (packets with
43  * more segments can be sent, depending on the busdma backend); it
44  * is however used as an estimate for the TX window size.
45  */
46 #define	GEM_NTXSEGS		16
47 
48 #define	GEM_TXQUEUELEN		64
49 #define	GEM_NTXDESC		(GEM_TXQUEUELEN * GEM_NTXSEGS)
50 #define	GEM_MAXTXFREE		(GEM_NTXDESC - 1)
51 #define	GEM_NTXDESC_MASK	(GEM_NTXDESC - 1)
52 #define	GEM_NEXTTX(x)		((x + 1) & GEM_NTXDESC_MASK)
53 
54 /*
55  * Receive descriptor ring size - we have one RX buffer per incoming
56  * packet, so this logic is a little simpler.
57  */
58 #define	GEM_NRXDESC		256
59 #define	GEM_NRXDESC_MASK	(GEM_NRXDESC - 1)
60 #define	GEM_NEXTRX(x)		((x + 1) & GEM_NRXDESC_MASK)
61 
62 /*
63  * How many ticks to wait until to retry on a RX descriptor that is
64  * still owned by the hardware.
65  */
66 #define	GEM_RXOWN_TICKS		(hz / 50)
67 
68 /*
69  * Control structures are DMA'd to the chip.  We allocate them
70  * in a single clump that maps to a single DMA segment to make
71  * several things easier.
72  */
73 struct gem_control_data {
74 	struct gem_desc gcd_txdescs[GEM_NTXDESC];	/* TX descriptors */
75 	struct gem_desc gcd_rxdescs[GEM_NRXDESC];	/* RX descriptors */
76 };
77 
78 #define	GEM_CDOFF(x)		offsetof(struct gem_control_data, x)
79 #define	GEM_CDTXOFF(x)		GEM_CDOFF(gcd_txdescs[(x)])
80 #define	GEM_CDRXOFF(x)		GEM_CDOFF(gcd_rxdescs[(x)])
81 
82 /*
83  * software state for transmit job mbufs (may be elements of mbuf chains)
84  */
85 struct gem_txsoft {
86 	struct mbuf *txs_mbuf;		/* head of our mbuf chain */
87 	bus_dmamap_t txs_dmamap;	/* our DMA map */
88 	u_int txs_firstdesc;		/* first descriptor in packet */
89 	u_int txs_lastdesc;		/* last descriptor in packet */
90 	u_int txs_ndescs;		/* number of descriptors */
91 	STAILQ_ENTRY(gem_txsoft) txs_q;
92 };
93 
94 STAILQ_HEAD(gem_txsq, gem_txsoft);
95 
96 /*
97  * software state for receive jobs
98  */
99 struct gem_rxsoft {
100 	struct mbuf *rxs_mbuf;		/* head of our mbuf chain */
101 	bus_dmamap_t rxs_dmamap;	/* our DMA map */
102 	bus_addr_t rxs_paddr;		/* physical address of the segment */
103 };
104 
105 /*
106  * software state per device
107  */
108 struct gem_softc {
109 	if_t		sc_ifp;
110 	struct mtx	sc_mtx;
111 	device_t	sc_miibus;
112 	struct mii_data	*sc_mii;	/* MII media control */
113 	device_t	sc_dev;		/* generic device information */
114 	u_char		sc_enaddr[ETHER_ADDR_LEN];
115 	struct callout	sc_tick_ch;	/* tick callout */
116 	struct callout	sc_rx_ch;	/* delayed RX callout */
117 	u_int		sc_wdog_timer;	/* watchdog timer */
118 
119 	void		*sc_ih;
120 	struct resource *sc_res[2];
121 #define	GEM_RES_INTR		0
122 #define	GEM_RES_MEM		1
123 
124 	bus_dma_tag_t	sc_pdmatag;	/* parent bus DMA tag */
125 	bus_dma_tag_t	sc_rdmatag;	/* RX bus DMA tag */
126 	bus_dma_tag_t	sc_tdmatag;	/* TX bus DMA tag */
127 	bus_dma_tag_t	sc_cdmatag;	/* control data bus DMA tag */
128 	bus_dmamap_t	sc_dmamap;	/* bus DMA handle */
129 
130 	u_int		sc_variant;
131 #define	GEM_UNKNOWN		0	/* don't know */
132 #define	GEM_SUN_GEM		1	/* Sun GEM */
133 #define	GEM_APPLE_GMAC		2	/* Apple GMAC */
134 #define	GEM_APPLE_K2_GMAC	3	/* Apple K2 GMAC */
135 
136 #define	GEM_IS_APPLE(sc)						\
137 	((sc)->sc_variant == GEM_APPLE_GMAC ||				\
138 	(sc)->sc_variant == GEM_APPLE_K2_GMAC)
139 
140 	u_int		sc_flags;
141 #define	GEM_INITED	(1 << 0)	/* reset persistent regs init'ed */
142 #define	GEM_LINK	(1 << 1)	/* link is up */
143 #define	GEM_PCI66	(1 << 2)	/* PCI bus runs at 66MHz */
144 #define	GEM_SERDES	(1 << 3)	/* use the SERDES */
145 
146 	/*
147 	 * ring buffer DMA stuff
148 	 */
149 	bus_dmamap_t	sc_cddmamap;	/* control data DMA map */
150 	bus_addr_t	sc_cddma;
151 
152 	/*
153 	 * software state for transmit and receive descriptors
154 	 */
155 	struct gem_txsoft sc_txsoft[GEM_TXQUEUELEN];
156 	struct gem_rxsoft sc_rxsoft[GEM_NRXDESC];
157 
158 	/*
159 	 * control data structures
160 	 */
161 	struct gem_control_data *sc_control_data;
162 #define	sc_txdescs	sc_control_data->gcd_txdescs
163 #define	sc_rxdescs	sc_control_data->gcd_rxdescs
164 
165 	u_int		sc_txfree;	/* number of free TX descriptors */
166 	u_int		sc_txnext;	/* next ready TX descriptor */
167 	u_int		sc_txwin;	/* TX desc. since last TX intr. */
168 
169 	struct gem_txsq	sc_txfreeq;	/* free TX descsofts */
170 	struct gem_txsq	sc_txdirtyq;	/* dirty TX descsofts */
171 
172 	u_int		sc_rxptr;	/* next ready RX descriptor/state */
173 	u_int		sc_rxfifosize;	/* RX FIFO size (bytes) */
174 
175 	uint32_t	sc_mac_rxcfg;	/* RX MAC conf. % GEM_MAC_RX_ENABLE */
176 
177 	int		sc_ifflags;
178 	u_long		sc_csum_features;
179 };
180 
181 #define	GEM_BARRIER(sc, offs, len, flags)				\
182 	bus_barrier((sc)->sc_res[GEM_RES_MEM], (offs), (len), (flags))
183 
184 #define	GEM_READ_N(n, sc, offs)						\
185 	bus_read_ ## n((sc)->sc_res[GEM_RES_MEM], (offs))
186 #define	GEM_READ_1(sc, offs)						\
187 	GEM_READ_N(1, (sc), (offs))
188 #define	GEM_READ_2(sc, offs)						\
189 	GEM_READ_N(2, (sc), (offs))
190 #define	GEM_READ_4(sc, offs)						\
191 	GEM_READ_N(4, (sc), (offs))
192 #define	GEM_READ_1(sc, offs)						\
193 	GEM_READ_N(1, (sc), (offs))
194 #define	GEM_READ_2(sc, offs)						\
195 	GEM_READ_N(2, (sc), (offs))
196 #define	GEM_READ_4(sc, offs)						\
197 	GEM_READ_N(4, (sc), (offs))
198 
199 #define	GEM_WRITE_N(n, sc, offs, v)					\
200 	bus_write_ ## n((sc)->sc_res[GEM_RES_MEM], (offs), (v))
201 #define	GEM_WRITE_1(sc, offs, v)					\
202 	GEM_WRITE_N(1, (sc), (offs), (v))
203 #define	GEM_WRITE_2(sc, offs, v)					\
204 	GEM_WRITE_N(2, (sc), (offs), (v))
205 #define	GEM_WRITE_4(sc, offs, v)					\
206 	GEM_WRITE_N(4, (sc), (offs), (v))
207 #define	GEM_WRITE_1(sc, offs, v)					\
208 	GEM_WRITE_N(1, (sc), (offs), (v))
209 #define	GEM_WRITE_2(sc, offs, v)					\
210 	GEM_WRITE_N(2, (sc), (offs), (v))
211 #define	GEM_WRITE_4(sc, offs, v)					\
212 	GEM_WRITE_N(4, (sc), (offs), (v))
213 
214 #define	GEM_CDTXADDR(sc, x)	((sc)->sc_cddma + GEM_CDTXOFF((x)))
215 #define	GEM_CDRXADDR(sc, x)	((sc)->sc_cddma + GEM_CDRXOFF((x)))
216 
217 #define	GEM_CDSYNC(sc, ops)						\
218 	bus_dmamap_sync((sc)->sc_cdmatag, (sc)->sc_cddmamap, (ops));
219 
220 #define	GEM_INIT_RXDESC(sc, x)						\
221 do {									\
222 	struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)];			\
223 	struct gem_desc *__rxd = &sc->sc_rxdescs[(x)];			\
224 	struct mbuf *__m = __rxs->rxs_mbuf;				\
225 									\
226 	__m->m_data = __m->m_ext.ext_buf;				\
227 	__rxd->gd_addr = htole64(__rxs->rxs_paddr);			\
228 	__rxd->gd_flags = htole64((((__m->m_ext.ext_size) <<		\
229 	    GEM_RD_BUFSHIFT) & GEM_RD_BUFSIZE) | GEM_RD_OWN);		\
230 } while (0)
231 
232 #define	GEM_UPDATE_RXDESC(sc, x)					\
233 do {									\
234 	struct gem_rxsoft *__rxs = &sc->sc_rxsoft[(x)];			\
235 	struct gem_desc *__rxd = &sc->sc_rxdescs[(x)];			\
236 	struct mbuf *__m = __rxs->rxs_mbuf;				\
237 									\
238 	__rxd->gd_flags = htole64((((__m->m_ext.ext_size) <<		\
239 	    GEM_RD_BUFSHIFT) & GEM_RD_BUFSIZE) | GEM_RD_OWN);		\
240 } while (0)
241 
242 #define	GEM_LOCK_INIT(_sc, _name)					\
243 	mtx_init(&(_sc)->sc_mtx, _name, MTX_NETWORK_LOCK, MTX_DEF)
244 #define	GEM_LOCK(_sc)			mtx_lock(&(_sc)->sc_mtx)
245 #define	GEM_UNLOCK(_sc)			mtx_unlock(&(_sc)->sc_mtx)
246 #define	GEM_LOCK_ASSERT(_sc, _what)	mtx_assert(&(_sc)->sc_mtx, (_what))
247 #define	GEM_LOCK_DESTROY(_sc)		mtx_destroy(&(_sc)->sc_mtx)
248 
249 #ifdef _KERNEL
250 int	gem_attach(struct gem_softc *sc);
251 void	gem_detach(struct gem_softc *sc);
252 void	gem_intr(void *v);
253 void	gem_resume(struct gem_softc *sc);
254 void	gem_suspend(struct gem_softc *sc);
255 
256 int	gem_mediachange(if_t ifp);
257 void	gem_mediastatus(if_t ifp, struct ifmediareq *ifmr);
258 
259 /* MII methods & callbacks */
260 int	gem_mii_readreg(device_t dev, int phy, int reg);
261 void	gem_mii_statchg(device_t dev);
262 int	gem_mii_writereg(device_t dev, int phy, int reg, int val);
263 
264 #endif /* _KERNEL */
265 
266 #endif
267