xref: /freebsd/sys/dev/jme/if_jmevar.h (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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$
28  */
29 
30 #ifndef	_IF_JMEVAR_H
31 #define	_IF_JMEVAR_H
32 
33 #include <sys/queue.h>
34 #include <sys/callout.h>
35 #include <sys/taskqueue.h>
36 
37 /*
38  * JMC250 supports upto 1024 descriptors and the number of
39  * descriptors should be multiple of 16.
40  */
41 #define	JME_TX_RING_CNT		384
42 #define	JME_RX_RING_CNT		256
43 /*
44  * Tx/Rx descriptor queue base should be 16bytes aligned and
45  * should not cross 4G bytes boundary on the 64bits address
46  * mode.
47  */
48 #define	JME_TX_RING_ALIGN	16
49 #define	JME_RX_RING_ALIGN	16
50 #define	JME_TSO_MAXSEGSIZE	4096
51 #define	JME_TSO_MAXSIZE		(65535 + sizeof(struct ether_vlan_header))
52 #define	JME_MAXTXSEGS		32
53 #define	JME_RX_BUF_ALIGN	sizeof(uint64_t)
54 #define	JME_SSB_ALIGN		16
55 
56 #define	JME_ADDR_LO(x)		((uint64_t) (x) & 0xFFFFFFFF)
57 #define	JME_ADDR_HI(x)		((uint64_t) (x) >> 32)
58 
59 #define	JME_MSI_MESSAGES	8
60 #define	JME_MSIX_MESSAGES	8
61 
62 /* Water mark to kick reclaiming Tx buffers. */
63 #define	JME_TX_DESC_HIWAT	(JME_TX_RING_CNT - (((JME_TX_RING_CNT) * 3) / 10))
64 
65 /*
66  * JMC250 can send 9K jumbo frame on Tx path and can receive
67  * 65535 bytes.
68  */
69 #define JME_JUMBO_FRAMELEN	9216
70 #define JME_JUMBO_MTU							\
71 	(JME_JUMBO_FRAMELEN - sizeof(struct ether_vlan_header) -	\
72 	 ETHER_HDR_LEN - ETHER_CRC_LEN)
73 #define	JME_MAX_MTU							\
74 	(ETHER_MAX_LEN + sizeof(struct ether_vlan_header) -		\
75 	 ETHER_HDR_LEN - ETHER_CRC_LEN)
76 /*
77  * JMC250 can't handle Tx checksum offload/TSO if frame length
78  * is larger than its FIFO size(2K). It's also good idea to not
79  * use jumbo frame if hardware is running at half-duplex media.
80  * Because the jumbo frame may not fit into the Tx FIFO,
81  * collisions make hardware fetch frame from host memory with
82  * DMA again which in turn slows down Tx performance
83  * significantly.
84  */
85 #define	JME_TX_FIFO_SIZE	2000
86 /*
87  * JMC250 has just 4K Rx FIFO. To support jumbo frame that is
88  * larger than 4K bytes in length, Rx FIFO threshold should be
89  * adjusted to minimize Rx FIFO overrun.
90  */
91 #define	JME_RX_FIFO_SIZE	4000
92 
93 #define	JME_DESC_INC(x, y)	((x) = ((x) + 1) % (y))
94 
95 #define	JME_PROC_MIN		10
96 #define	JME_PROC_DEFAULT	(JME_RX_RING_CNT / 2)
97 #define	JME_PROC_MAX		(JME_RX_RING_CNT - 1)
98 
99 struct jme_txdesc {
100 	struct mbuf		*tx_m;
101 	bus_dmamap_t		tx_dmamap;
102 	int			tx_ndesc;
103 	struct jme_desc		*tx_desc;
104 };
105 
106 struct jme_rxdesc {
107 	struct mbuf 		*rx_m;
108 	bus_dmamap_t		rx_dmamap;
109 	struct jme_desc		*rx_desc;
110 };
111 
112 struct jme_chain_data{
113 	bus_dma_tag_t		jme_ring_tag;
114 	bus_dma_tag_t		jme_buffer_tag;
115 	bus_dma_tag_t		jme_ssb_tag;
116 	bus_dmamap_t		jme_ssb_map;
117 	bus_dma_tag_t		jme_tx_tag;
118 	struct jme_txdesc	jme_txdesc[JME_TX_RING_CNT];
119 	bus_dma_tag_t		jme_rx_tag;
120 	struct jme_rxdesc	jme_rxdesc[JME_RX_RING_CNT];
121 	bus_dma_tag_t		jme_tx_ring_tag;
122 	bus_dmamap_t		jme_tx_ring_map;
123 	bus_dma_tag_t		jme_rx_ring_tag;
124 	bus_dmamap_t		jme_rx_ring_map;
125 	bus_dmamap_t		jme_rx_sparemap;
126 
127 	int			jme_tx_prod;
128 	int			jme_tx_cons;
129 	int			jme_tx_cnt;
130 	int			jme_rx_cons;
131 	int			jme_rxlen;
132 
133 	struct mbuf		*jme_rxhead;
134 	struct mbuf		*jme_rxtail;
135 };
136 
137 struct jme_ring_data {
138 	struct jme_desc		*jme_tx_ring;
139 	bus_addr_t		jme_tx_ring_paddr;
140 	struct jme_desc		*jme_rx_ring;
141 	bus_addr_t		jme_rx_ring_paddr;
142 	struct jme_ssb		*jme_ssb_block;
143 	bus_addr_t		jme_ssb_block_paddr;
144 };
145 
146 #define JME_TX_RING_ADDR(sc, i)	\
147     ((sc)->jme_rdata.jme_tx_ring_paddr + sizeof(struct jme_desc) * (i))
148 #define JME_RX_RING_ADDR(sc, i)	\
149     ((sc)->jme_rdata.jme_rx_ring_paddr + sizeof(struct jme_desc) * (i))
150 
151 #define JME_TX_RING_SIZE	\
152     (sizeof(struct jme_desc) * JME_TX_RING_CNT)
153 #define JME_RX_RING_SIZE	\
154     (sizeof(struct jme_desc) * JME_RX_RING_CNT)
155 #define	JME_SSB_SIZE		sizeof(struct jme_ssb)
156 
157 /* Statistics counters. */
158 struct jme_hw_stats {
159 	uint32_t		rx_good_frames;
160 	uint32_t		rx_crc_errs;
161 	uint32_t		rx_mii_errs;
162 	uint32_t		rx_fifo_oflows;
163 	uint32_t		rx_desc_empty;
164 	uint32_t		rx_bad_frames;
165 	uint32_t		tx_good_frames;
166 	uint32_t		tx_bad_frames;
167 };
168 
169 /*
170  * Software state per device.
171  */
172 struct jme_softc {
173 	struct ifnet 		*jme_ifp;
174 	device_t		jme_dev;
175 	device_t		jme_miibus;
176 	struct resource		*jme_res[1];
177 	struct resource_spec	*jme_res_spec;
178 	struct resource		*jme_irq[JME_MSI_MESSAGES];
179 	struct resource_spec	*jme_irq_spec;
180 	void			*jme_intrhand[JME_MSI_MESSAGES];
181 	int			jme_rev;
182 	int			jme_chip_rev;
183 	int			jme_phyaddr;
184 	uint8_t			jme_eaddr[ETHER_ADDR_LEN];
185 	uint32_t		jme_tx_dma_size;
186 	uint32_t		jme_rx_dma_size;
187 	int			jme_flags;
188 #define	JME_FLAG_FPGA		0x0001
189 #define	JME_FLAG_PCIE		0x0002
190 #define	JME_FLAG_PCIX		0x0003
191 #define	JME_FLAG_MSI		0x0004
192 #define	JME_FLAG_MSIX		0x0010
193 #define	JME_FLAG_PMCAP		0x0020
194 #define	JME_FLAG_FASTETH	0x0040
195 #define	JME_FLAG_NOJUMBO	0x0080
196 #define	JME_FLAG_TXCLK		0x0100
197 #define	JME_FLAG_DMA32BIT	0x0200
198 #define	JME_FLAG_HWMIB		0x0400
199 #define	JME_FLAG_DETACH		0x4000
200 #define	JME_FLAG_LINK		0x8000
201 
202 	struct jme_hw_stats	jme_ostats;
203 	struct jme_hw_stats	jme_stats;
204 	struct callout		jme_tick_ch;
205 	struct jme_chain_data	jme_cdata;
206 	struct jme_ring_data	jme_rdata;
207 	int			jme_if_flags;
208 	int			jme_watchdog_timer;
209 	uint32_t		jme_txcsr;
210 	uint32_t		jme_rxcsr;
211 	int			jme_process_limit;
212 	int			jme_tx_coal_to;
213 	int			jme_tx_coal_pkt;
214 	int			jme_rx_coal_to;
215 	int			jme_rx_coal_pkt;
216 	volatile int		jme_morework;
217 
218 	struct task		jme_int_task;
219 	struct task		jme_tx_task;
220 	struct task		jme_link_task;
221 	struct taskqueue	*jme_tq;
222 	struct mtx		jme_mtx;
223 };
224 
225 /* Register access macros. */
226 #define CSR_WRITE_4(_sc, reg, val)	\
227 	bus_write_4((_sc)->jme_res[0], (reg), (val))
228 #define CSR_READ_4(_sc, reg)		\
229 	bus_read_4((_sc)->jme_res[0], (reg))
230 
231 #define JME_LOCK(_sc)		mtx_lock(&(_sc)->jme_mtx)
232 #define JME_UNLOCK(_sc)		mtx_unlock(&(_sc)->jme_mtx)
233 #define JME_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->jme_mtx, MA_OWNED)
234 
235 #define	JME_MAXERR	5
236 
237 #define	JME_RXCHAIN_RESET(_sc)						\
238 do {									\
239 	(_sc)->jme_cdata.jme_rxhead = NULL;				\
240 	(_sc)->jme_cdata.jme_rxtail = NULL;				\
241 	(_sc)->jme_cdata.jme_rxlen = 0;					\
242 } while (0)
243 
244 #define	JME_TX_TIMEOUT		5
245 #define JME_TIMEOUT		1000
246 #define JME_PHY_TIMEOUT		1000
247 #define JME_EEPROM_TIMEOUT	1000
248 
249 #endif
250