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 up to 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		35
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
99struct 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
106struct jme_rxdesc {
107	struct mbuf 		*rx_m;
108	bus_dmamap_t		rx_dmamap;
109	struct jme_desc		*rx_desc;
110};
111
112struct 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
137struct 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. */
158struct 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 */
172struct 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		0x00000001
189#define	JME_FLAG_PCIE		0x00000002
190#define	JME_FLAG_PCIX		0x00000004
191#define	JME_FLAG_MSI		0x00000008
192#define	JME_FLAG_MSIX		0x00000010
193#define	JME_FLAG_PMCAP		0x00000020
194#define	JME_FLAG_FASTETH	0x00000040
195#define	JME_FLAG_NOJUMBO	0x00000080
196#define	JME_FLAG_RXCLK		0x00000100
197#define	JME_FLAG_TXCLK		0x00000200
198#define	JME_FLAG_DMA32BIT	0x00000400
199#define	JME_FLAG_HWMIB		0x00000800
200#define	JME_FLAG_EFUSE		0x00001000
201#define	JME_FLAG_PCCPCD		0x00002000
202#define	JME_FLAG_DETACH		0x40000000
203#define	JME_FLAG_LINK		0x80000000
204
205	struct jme_hw_stats	jme_ostats;
206	struct jme_hw_stats	jme_stats;
207	struct callout		jme_tick_ch;
208	struct jme_chain_data	jme_cdata;
209	struct jme_ring_data	jme_rdata;
210	int			jme_if_flags;
211	int			jme_watchdog_timer;
212	uint32_t		jme_txcsr;
213	uint32_t		jme_rxcsr;
214	int			jme_process_limit;
215	int			jme_tx_coal_to;
216	int			jme_tx_pcd_to;
217	int			jme_tx_coal_pkt;
218	int			jme_rx_coal_to;
219	int			jme_rx_pcd_to;
220	int			jme_rx_coal_pkt;
221	volatile int		jme_morework;
222
223	struct task		jme_int_task;
224	struct task		jme_link_task;
225	struct taskqueue	*jme_tq;
226	struct mtx		jme_mtx;
227};
228
229/* Register access macros. */
230#define	CSR_WRITE_4(_sc, reg, val)	\
231	bus_write_4((_sc)->jme_res[0], (reg), (val))
232#define	CSR_READ_4(_sc, reg)		\
233	bus_read_4((_sc)->jme_res[0], (reg))
234
235#define	JME_LOCK(_sc)		mtx_lock(&(_sc)->jme_mtx)
236#define	JME_UNLOCK(_sc)		mtx_unlock(&(_sc)->jme_mtx)
237#define	JME_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->jme_mtx, MA_OWNED)
238
239#define	JME_MAXERR	5
240
241#define	JME_RXCHAIN_RESET(_sc)						\
242do {									\
243	(_sc)->jme_cdata.jme_rxhead = NULL;				\
244	(_sc)->jme_cdata.jme_rxtail = NULL;				\
245	(_sc)->jme_cdata.jme_rxlen = 0;					\
246} while (0)
247
248#define	JME_TX_TIMEOUT		5
249#define	JME_TIMEOUT		1000
250#define	JME_PHY_TIMEOUT		1000
251#define	JME_EEPROM_TIMEOUT	1000
252
253#endif
254