xref: /dflybsd-src/sys/dev/netif/jme/if_jmevar.h (revision 7b0400926d0f4475a0a1ffb2e8bc51290561f585)
1 /*-
2  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/jme/if_jmevar.h,v 1.1 2008/05/27 01:42:01 yongari Exp $
28  * $DragonFly: src/sys/dev/netif/jme/if_jmevar.h,v 1.8 2008/11/26 11:55:18 sephe Exp $
29  */
30 
31 #ifndef	_IF_JMEVAR_H
32 #define	_IF_JMEVAR_H
33 
34 #include <sys/queue.h>
35 #include <sys/callout.h>
36 #include <sys/taskqueue.h>
37 
38 /*
39  * JMC250 supports upto JME_NDESC_MAX descriptors and the number of
40  * descriptors should be multiple of JME_NDESC_ALIGN.
41  */
42 #define	JME_TX_DESC_CNT_DEF	384
43 #define	JME_RX_DESC_CNT_DEF	256
44 
45 #define JME_NDESC_ALIGN		16
46 #define JME_NDESC_MAX		1024
47 
48 #define JME_NRXRING_1		1
49 #define JME_NRXRING_2		2
50 #define JME_NRXRING_4		4
51 
52 #define JME_NRXRING_MIN		JME_NRXRING_1
53 #define JME_NRXRING_MAX		JME_NRXRING_4
54 
55 #define JME_NSERIALIZE		(JME_NRXRING_MAX + 2)
56 
57 #define JME_NMSIX		(JME_NRXRING_MAX + 1)
58 
59 /*
60  * Tx/Rx descriptor queue base should be 16bytes aligned and
61  * should not cross 4G bytes boundary on the 64bits address
62  * mode.
63  */
64 #define	JME_TX_RING_ALIGN	16
65 #define	JME_RX_RING_ALIGN	16
66 #define	JME_MAXSEGSIZE		4096
67 #define	JME_TSO_MAXSIZE		(65535 + sizeof(struct ether_vlan_header))
68 #define	JME_MAXTXSEGS		32
69 #define	JME_RX_BUF_ALIGN	sizeof(uint64_t)
70 #define	JME_SSB_ALIGN		16
71 
72 #if (BUS_SPACE_MAXADDR != BUS_SPACE_MAXADDR_32BIT)
73 #define JME_RING_BOUNDARY	0x100000000ULL
74 #else
75 #define JME_RING_BOUNDARY	0
76 #endif
77 
78 #define	JME_ADDR_LO(x)		((uint64_t) (x) & 0xFFFFFFFF)
79 #define	JME_ADDR_HI(x)		((uint64_t) (x) >> 32)
80 
81 #define	JME_MSI_MESSAGES	8
82 #define	JME_MSIX_MESSAGES	8
83 
84 /* Water mark to kick reclaiming Tx buffers. */
85 #define	JME_TX_DESC_HIWAT(sc)	\
86 	((sc)->jme_tx_desc_cnt - (((sc)->jme_tx_desc_cnt * 3) / 10))
87 
88 /*
89  * JMC250 can send 9K jumbo frame on Tx path and can receive
90  * 65535 bytes.
91  */
92 #define JME_JUMBO_FRAMELEN	9216
93 #define JME_JUMBO_MTU							\
94 	(JME_JUMBO_FRAMELEN - sizeof(struct ether_vlan_header) -	\
95 	 ETHER_HDR_LEN - ETHER_CRC_LEN)
96 #define	JME_MAX_MTU							\
97 	(ETHER_MAX_LEN + sizeof(struct ether_vlan_header) -		\
98 	 ETHER_HDR_LEN - ETHER_CRC_LEN)
99 /*
100  * JMC250 can't handle Tx checksum offload/TSO if frame length
101  * is larger than its FIFO size(2K). It's also good idea to not
102  * use jumbo frame if hardware is running at half-duplex media.
103  * Because the jumbo frame may not fit into the Tx FIFO,
104  * collisions make hardware fetch frame from host memory with
105  * DMA again which in turn slows down Tx performance
106  * significantly.
107  */
108 #define	JME_TX_FIFO_SIZE	2000
109 /*
110  * JMC250 has just 4K Rx FIFO. To support jumbo frame that is
111  * larger than 4K bytes in length, Rx FIFO threshold should be
112  * adjusted to minimize Rx FIFO overrun.
113  */
114 #define	JME_RX_FIFO_SIZE	4000
115 
116 #define	JME_DESC_INC(x, y)	((x) = ((x) + 1) % (y))
117 
118 struct jme_txdesc {
119 	struct mbuf		*tx_m;
120 	bus_dmamap_t		tx_dmamap;
121 	int			tx_ndesc;
122 	struct jme_desc		*tx_desc;
123 };
124 
125 struct jme_rxdesc {
126 	struct mbuf 		*rx_m;
127 	bus_dmamap_t		rx_dmamap;
128 	struct jme_desc		*rx_desc;
129 };
130 
131 struct jme_softc;
132 
133 /*
134  * RX ring/descs
135  */
136 struct jme_rxdata {
137 	struct lwkt_serialize	jme_rx_serialize;
138 	struct jme_softc	*jme_sc;
139 
140 	uint32_t		jme_rx_coal;
141 	uint32_t		jme_rx_comp;
142 	uint32_t		jme_rx_empty;
143 	int			jme_rx_idx;
144 
145 	bus_dma_tag_t		jme_rx_tag;	/* RX mbuf tag */
146 	bus_dmamap_t		jme_rx_sparemap;
147 	struct jme_rxdesc	*jme_rxdesc;
148 
149 	struct jme_desc		*jme_rx_ring;
150 	bus_addr_t		jme_rx_ring_paddr;
151 	bus_dma_tag_t		jme_rx_ring_tag;
152 	bus_dmamap_t		jme_rx_ring_map;
153 
154 	int			jme_rx_cons;
155 	int			jme_rx_desc_cnt;
156 
157 	int			jme_rxlen;
158 	struct mbuf		*jme_rxhead;
159 	struct mbuf		*jme_rxtail;
160 
161 	u_long			jme_rx_pkt;
162 };
163 
164 struct jme_chain_data {
165 	/*
166 	 * Top level tags
167 	 */
168 	bus_dma_tag_t		jme_ring_tag;	/* parent ring tag */
169 	bus_dma_tag_t		jme_buffer_tag;	/* parent mbuf/ssb tag */
170 
171 	/*
172 	 * Shadow status block
173 	 */
174 	struct jme_ssb		*jme_ssb_block;
175 	bus_addr_t		jme_ssb_block_paddr;
176 	bus_dma_tag_t		jme_ssb_tag;
177 	bus_dmamap_t		jme_ssb_map;
178 
179 	/*
180 	 * TX ring/descs
181 	 */
182 	struct lwkt_serialize	jme_tx_serialize;
183 	struct jme_softc	*jme_sc;
184 	bus_dma_tag_t		jme_tx_tag;	/* TX mbuf tag */
185 	struct jme_txdesc	*jme_txdesc;
186 
187 	struct jme_desc		*jme_tx_ring;
188 	bus_addr_t		jme_tx_ring_paddr;
189 	bus_dma_tag_t		jme_tx_ring_tag;
190 	bus_dmamap_t		jme_tx_ring_map;
191 
192 	int			jme_tx_prod;
193 	int			jme_tx_cons;
194 	int			jme_tx_cnt;
195 
196 	int			jme_rx_ring_cnt;
197 	struct jme_rxdata	jme_rx_data[JME_NRXRING_MAX];
198 };
199 
200 struct jme_msix_data {
201 	int			jme_msix_rid;
202 	int			jme_msix_cpuid;
203 	u_int			jme_msix_vector;
204 	uint32_t		jme_msix_intrs;
205 	struct resource		*jme_msix_res;
206 	void			*jme_msix_handle;
207 	struct lwkt_serialize	*jme_msix_serialize;
208 	char			jme_msix_desc[64];
209 
210 	driver_intr_t		*jme_msix_func;
211 	void			*jme_msix_arg;
212 };
213 
214 #define JME_TX_RING_SIZE(sc)	\
215     (sizeof(struct jme_desc) * (sc)->jme_tx_desc_cnt)
216 #define JME_RX_RING_SIZE(rdata)	\
217     (sizeof(struct jme_desc) * (rdata)->jme_rx_desc_cnt)
218 #define	JME_SSB_SIZE		sizeof(struct jme_ssb)
219 
220 /*
221  * Software state per device.
222  */
223 struct jme_softc {
224 	struct arpcom		arpcom;
225 	device_t		jme_dev;
226 
227 	int			jme_mem_rid;
228 	struct resource		*jme_mem_res;
229 	bus_space_tag_t		jme_mem_bt;
230 	bus_space_handle_t	jme_mem_bh;
231 
232 	int			jme_irq_type;
233 	int			jme_irq_rid;
234 	struct resource		*jme_irq_res;
235 	void			*jme_irq_handle;
236 	struct jme_msix_data	jme_msix[JME_NMSIX];
237 	int			jme_msix_cnt;
238 	uint32_t		jme_msinum[JME_MSINUM_CNT];
239 
240 	device_t		jme_miibus;
241 	int			jme_phyaddr;
242 	bus_addr_t		jme_lowaddr;
243 
244 	uint32_t		jme_clksrc;
245 	uint32_t		jme_clksrc_1000;
246 	uint32_t		jme_tx_dma_size;
247 	uint32_t		jme_rx_dma_size;
248 
249 	uint32_t		jme_caps;
250 #define	JME_CAP_FPGA		0x0001
251 #define	JME_CAP_PCIE		0x0002
252 #define	JME_CAP_PMCAP		0x0004
253 #define	JME_CAP_FASTETH		0x0008
254 #define	JME_CAP_JUMBO		0x0010
255 
256 	uint32_t		jme_workaround;
257 #define JME_WA_EXTFIFO		0x0001
258 #define JME_WA_HDX		0x0002
259 
260 	uint32_t		jme_flags;
261 #define	JME_FLAG_MSI		0x0001
262 #define	JME_FLAG_MSIX		0x0002
263 #define	JME_FLAG_DETACH		0x0004
264 #define	JME_FLAG_LINK		0x0008
265 
266 	struct lwkt_serialize	jme_serialize;
267 	struct lwkt_serialize	*jme_serialize_arr[JME_NSERIALIZE];
268 	int			jme_serialize_cnt;
269 
270 	struct callout		jme_tick_ch;
271 	struct jme_chain_data	jme_cdata;
272 	int			jme_if_flags;
273 	uint32_t		jme_txcsr;
274 	uint32_t		jme_rxcsr;
275 
276 	int			jme_txd_spare;
277 
278 	struct sysctl_ctx_list	jme_sysctl_ctx;
279 	struct sysctl_oid	*jme_sysctl_tree;
280 
281 	/*
282 	 * Sysctl variables
283 	 */
284 	int			jme_tx_coal_to;
285 	int			jme_tx_coal_pkt;
286 	int			jme_rx_coal_to;
287 	int			jme_rx_coal_pkt;
288 	int			jme_tx_desc_cnt;
289 	int			jme_rss_debug;
290 };
291 
292 /* Register access macros. */
293 #define CSR_WRITE_4(_sc, reg, val)	\
294 	bus_space_write_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg), (val))
295 #define CSR_READ_4(_sc, reg)		\
296 	bus_space_read_4((_sc)->jme_mem_bt, (_sc)->jme_mem_bh, (reg))
297 
298 #define	JME_MAXERR	5
299 
300 #define	JME_RXCHAIN_RESET(rdata)	\
301 do {					\
302 	(rdata)->jme_rxhead = NULL;	\
303 	(rdata)->jme_rxtail = NULL;	\
304 	(rdata)->jme_rxlen = 0;		\
305 } while (0)
306 
307 #define	JME_TX_TIMEOUT		5
308 #define JME_TIMEOUT		1000
309 #define JME_PHY_TIMEOUT		1000
310 #define JME_EEPROM_TIMEOUT	1000
311 
312 #define JME_TXD_RSVD		1
313 
314 #endif
315