xref: /freebsd/sys/dev/et/if_etvar.h (revision 0f2bd1e89db1a2f09268edea21e0ead329e092df)
1 /*-
2  * Copyright (c) 2007 Sepherosa Ziehau.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Sepherosa Ziehau <sepherosa@gmail.com>
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  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * $DragonFly: src/sys/dev/netif/et/if_etvar.h,v 1.4 2007/10/23 14:28:42 sephe Exp $
35  * $FreeBSD$
36  */
37 
38 #ifndef _IF_ETVAR_H
39 #define _IF_ETVAR_H
40 
41 /* DragonFly compatibility */
42 #define EVL_ENCAPLEN		ETHER_VLAN_ENCAP_LEN
43 
44 /*
45  * Allocate the right type of mbuf for the desired total length.
46  */
47 static __inline struct mbuf *
48 m_getl(int len, int how, int type, int flags, int *psize)
49 {
50 	struct mbuf *m;
51 	int size;
52 
53 	if (len >= MINCLSIZE) {
54 		m = m_getcl(how, type, flags);
55 		size = MCLBYTES;
56 	} else if (flags & M_PKTHDR) {
57 		m = m_gethdr(how, type);
58 		size = MHLEN;
59 	} else {
60 		m = m_get(how, type);
61 		size = MLEN;
62 	}
63 	if (psize != NULL)
64 		*psize = size;
65 	return (m);
66 }
67 
68 
69 #define ET_ALIGN		0x1000
70 #define ET_NSEG_MAX		32	/* XXX no limit actually */
71 #define ET_NSEG_SPARE		8
72 
73 #define ET_TX_NDESC		512
74 #define ET_RX_NDESC		512
75 #define ET_RX_NRING		2
76 #define ET_RX_NSTAT		(ET_RX_NRING * ET_RX_NDESC)
77 
78 #define ET_TX_RING_SIZE		(ET_TX_NDESC * sizeof(struct et_txdesc))
79 #define ET_RX_RING_SIZE		(ET_RX_NDESC * sizeof(struct et_rxdesc))
80 #define ET_RXSTAT_RING_SIZE	(ET_RX_NSTAT * sizeof(struct et_rxstat))
81 
82 #define ET_JUMBO_FRAMELEN	(ET_MEM_SIZE - ET_MEM_RXSIZE_MIN -	\
83 				 ET_MEM_TXSIZE_EX)
84 #define ET_JUMBO_MTU		(ET_JUMBO_FRAMELEN - ETHER_HDR_LEN -	\
85 				 EVL_ENCAPLEN - ETHER_CRC_LEN)
86 
87 #define ET_FRAMELEN(mtu)	(ETHER_HDR_LEN + EVL_ENCAPLEN + (mtu) +	\
88 				 ETHER_CRC_LEN)
89 
90 #define ET_JSLOTS		(ET_RX_NDESC + 128)
91 #define ET_JLEN			(ET_JUMBO_FRAMELEN + ETHER_ALIGN)
92 #define ET_JUMBO_MEM_SIZE	(ET_JSLOTS * ET_JLEN)
93 
94 #define CSR_WRITE_4(sc, reg, val)	\
95 	bus_write_4((sc)->sc_mem_res, (reg), (val))
96 #define CSR_READ_4(sc, reg)		\
97 	bus_read_4((sc)->sc_mem_res, (reg))
98 
99 #define ET_ADDR_HI(addr)	((uint64_t) (addr) >> 32)
100 #define ET_ADDR_LO(addr)	((uint64_t) (addr) & 0xffffffff)
101 
102 struct et_txdesc {
103 	uint32_t	td_addr_hi;
104 	uint32_t	td_addr_lo;
105 	uint32_t	td_ctrl1;	/* ET_TDCTRL1_ */
106 	uint32_t	td_ctrl2;	/* ET_TDCTRL2_ */
107 };
108 
109 #define ET_TDCTRL1_LEN_MASK	0x0000FFFF
110 
111 #define ET_TDCTRL2_LAST_FRAG	0x00000001
112 #define ET_TDCTRL2_FIRST_FRAG	0x00000002
113 #define ET_TDCTRL2_INTR		0x00000004
114 #define ET_TDCTRL2_CTRL_WORD	0x00000008
115 #define ET_TDCTRL2_HDX_BACKP	0x00000010
116 #define ET_TDCTRL2_XMIT_PAUSE	0x00000020
117 #define ET_TDCTRL2_FRAME_ERR	0x00000040
118 #define ET_TDCTRL2_NO_CRC	0x00000080
119 #define ET_TDCTRL2_MAC_OVRRD	0x00000100
120 #define ET_TDCTRL2_PAD_PACKET	0x00000200
121 #define ET_TDCTRL2_JUMBO_PACKET	0x00000400
122 #define ET_TDCTRL2_INS_VLAN	0x00000800
123 #define ET_TDCTRL2_CSUM_IP	0x00001000
124 #define ET_TDCTRL2_CSUM_TCP	0x00002000
125 #define ET_TDCTRL2_CSUM_UDP	0x00004000
126 
127 struct et_rxdesc {
128 	uint32_t	rd_addr_lo;
129 	uint32_t	rd_addr_hi;
130 	uint32_t	rd_ctrl;	/* ET_RDCTRL_ */
131 };
132 
133 #define ET_RDCTRL_BUFIDX_MASK	0x000003FF
134 
135 struct et_rxstat {
136 	uint32_t	rxst_info1;
137 	uint32_t	rxst_info2;	/* ET_RXST_INFO2_ */
138 };
139 
140 #define ET_RXST_INFO2_LEN_MASK	0x0000FFFF
141 #define ET_RXST_INFO2_LEN_SHIFT	0
142 #define ET_RXST_INFO2_BUFIDX_MASK	0x03FF0000
143 #define ET_RXST_INFO2_BUFIDX_SHIFT	16
144 #define ET_RXST_INFO2_RINGIDX_MASK	0x0C000000
145 #define ET_RXST_INFO2_RINGIDX_SHIFT	26
146 
147 struct et_rxstatus {
148 	uint32_t	rxs_ring;
149 	uint32_t	rxs_stat_ring;	/* ET_RXS_STATRING_ */
150 };
151 
152 #define ET_RXS_STATRING_INDEX_MASK	0x0FFF0000
153 #define ET_RXS_STATRING_INDEX_SHIFT	16
154 #define ET_RXS_STATRING_WRAP	0x10000000
155 
156 struct et_dmamap_ctx {
157 	int		nsegs;
158 	bus_dma_segment_t *segs;
159 };
160 
161 struct et_txbuf {
162 	struct mbuf		*tb_mbuf;
163 	bus_dmamap_t		tb_dmap;
164 };
165 
166 struct et_rxbuf {
167 	struct mbuf		*rb_mbuf;
168 	bus_dmamap_t		rb_dmap;
169 	bus_addr_t		rb_paddr;
170 };
171 
172 struct et_txstatus_data {
173 	uint32_t		*txsd_status;
174 	bus_addr_t		txsd_paddr;
175 	bus_dma_tag_t		txsd_dtag;
176 	bus_dmamap_t		txsd_dmap;
177 };
178 
179 struct et_rxstatus_data {
180 	struct et_rxstatus	*rxsd_status;
181 	bus_addr_t		rxsd_paddr;
182 	bus_dma_tag_t		rxsd_dtag;
183 	bus_dmamap_t		rxsd_dmap;
184 };
185 
186 struct et_rxstat_ring {
187 	struct et_rxstat	*rsr_stat;
188 	bus_addr_t		rsr_paddr;
189 	bus_dma_tag_t		rsr_dtag;
190 	bus_dmamap_t		rsr_dmap;
191 
192 	int			rsr_index;
193 	int			rsr_wrap;
194 };
195 
196 struct et_txdesc_ring {
197 	struct et_txdesc	*tr_desc;
198 	bus_addr_t		tr_paddr;
199 	bus_dma_tag_t		tr_dtag;
200 	bus_dmamap_t		tr_dmap;
201 
202 	int			tr_ready_index;
203 	int			tr_ready_wrap;
204 };
205 
206 struct et_rxdesc_ring {
207 	struct et_rxdesc	*rr_desc;
208 	bus_addr_t		rr_paddr;
209 	bus_dma_tag_t		rr_dtag;
210 	bus_dmamap_t		rr_dmap;
211 
212 	uint32_t		rr_posreg;
213 	int			rr_index;
214 	int			rr_wrap;
215 };
216 
217 struct et_txbuf_data {
218 	struct et_txbuf		tbd_buf[ET_TX_NDESC];
219 
220 	int			tbd_start_index;
221 	int			tbd_start_wrap;
222 	int			tbd_used;
223 };
224 
225 struct et_softc;
226 struct et_rxbuf_data;
227 typedef int	(*et_newbuf_t)(struct et_rxbuf_data *, int, int);
228 
229 struct et_rxbuf_data {
230 	struct et_rxbuf		rbd_buf[ET_RX_NDESC];
231 
232 	struct et_softc		*rbd_softc;
233 	struct et_rxdesc_ring	*rbd_ring;
234 
235 	int			rbd_bufsize;
236 	et_newbuf_t		rbd_newbuf;
237 };
238 
239 struct et_softc {
240 	struct ifnet		*ifp;
241 	device_t		dev;
242 	struct mtx		sc_mtx;
243 	device_t		sc_miibus;
244 	void			*sc_irq_handle;
245 	struct resource		*sc_irq_res;
246 	struct resource		*sc_mem_res;
247 
248 	struct arpcom		arpcom;
249 	int			sc_if_flags;
250 	uint32_t		sc_flags;	/* ET_FLAG_ */
251 	int			sc_expcap;
252 
253 	int			sc_mem_rid;
254 
255 	int			sc_irq_rid;
256 
257 	struct callout		sc_tick;
258 
259 	int			watchdog_timer;
260 
261 	bus_dma_tag_t		sc_dtag;
262 
263 	struct et_rxdesc_ring	sc_rx_ring[ET_RX_NRING];
264 	struct et_rxstat_ring	sc_rxstat_ring;
265 	struct et_rxstatus_data	sc_rx_status;
266 
267 	struct et_txdesc_ring	sc_tx_ring;
268 	struct et_txstatus_data	sc_tx_status;
269 
270 	bus_dma_tag_t		sc_mbuf_dtag;
271 	bus_dmamap_t		sc_mbuf_tmp_dmap;
272 	struct et_rxbuf_data	sc_rx_data[ET_RX_NRING];
273 	struct et_txbuf_data	sc_tx_data;
274 
275 	uint32_t		sc_tx;
276 	uint32_t		sc_tx_intr;
277 
278 	/*
279 	 * Sysctl variables
280 	 */
281 	int			sc_rx_intr_npkts;
282 	int			sc_rx_intr_delay;
283 	int			sc_tx_intr_nsegs;
284 	uint32_t		sc_timer;
285 };
286 
287 #define ET_LOCK(_sc)		mtx_lock(&(_sc)->sc_mtx)
288 #define ET_UNLOCK(_sc)		mtx_unlock(&(_sc)->sc_mtx)
289 #define ET_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
290 
291 #define ET_FLAG_PCIE		0x0001
292 #define ET_FLAG_MSI		0x0002
293 #define ET_FLAG_TXRX_ENABLED	0x0100
294 #define ET_FLAG_JUMBO		0x0200
295 
296 #endif	/* !_IF_ETVAR_H */
297