1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
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 unmodified, this list of conditions, and the following
12 *    disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD$
30 */
31
32#ifndef	_IF_JMEVAR_H
33#define	_IF_JMEVAR_H
34
35#include <sys/queue.h>
36#include <sys/callout.h>
37#include <sys/taskqueue.h>
38
39/*
40 * JMC250 supports up to 1024 descriptors and the number of
41 * descriptors should be multiple of 16.
42 */
43#define	JME_TX_RING_CNT		384
44#define	JME_RX_RING_CNT		256
45/*
46 * Tx/Rx descriptor queue base should be 16bytes aligned and
47 * should not cross 4G bytes boundary on the 64bits address
48 * mode.
49 */
50#define	JME_TX_RING_ALIGN	16
51#define	JME_RX_RING_ALIGN	16
52#define	JME_TSO_MAXSEGSIZE	4096
53#define	JME_TSO_MAXSIZE		(65535 + sizeof(struct ether_vlan_header))
54#define	JME_MAXTXSEGS		35
55#define	JME_RX_BUF_ALIGN	sizeof(uint64_t)
56#define	JME_SSB_ALIGN		16
57
58#define	JME_ADDR_LO(x)		((uint64_t) (x) & 0xFFFFFFFF)
59#define	JME_ADDR_HI(x)		((uint64_t) (x) >> 32)
60
61#define	JME_MSI_MESSAGES	8
62#define	JME_MSIX_MESSAGES	8
63
64/* Water mark to kick reclaiming Tx buffers. */
65#define	JME_TX_DESC_HIWAT	(JME_TX_RING_CNT - (((JME_TX_RING_CNT) * 3) / 10))
66
67/*
68 * JMC250 can send 9K jumbo frame on Tx path and can receive
69 * 65535 bytes.
70 */
71#define	JME_JUMBO_FRAMELEN	9216
72#define	JME_JUMBO_MTU							\
73	(JME_JUMBO_FRAMELEN - sizeof(struct ether_vlan_header) -	\
74	 ETHER_HDR_LEN - ETHER_CRC_LEN)
75#define	JME_MAX_MTU							\
76	(ETHER_MAX_LEN + sizeof(struct ether_vlan_header) -		\
77	 ETHER_HDR_LEN - ETHER_CRC_LEN)
78/*
79 * JMC250 can't handle Tx checksum offload/TSO if frame length
80 * is larger than its FIFO size(2K). It's also good idea to not
81 * use jumbo frame if hardware is running at half-duplex media.
82 * Because the jumbo frame may not fit into the Tx FIFO,
83 * collisions make hardware fetch frame from host memory with
84 * DMA again which in turn slows down Tx performance
85 * significantly.
86 */
87#define	JME_TX_FIFO_SIZE	2000
88/*
89 * JMC250 has just 4K Rx FIFO. To support jumbo frame that is
90 * larger than 4K bytes in length, Rx FIFO threshold should be
91 * adjusted to minimize Rx FIFO overrun.
92 */
93#define	JME_RX_FIFO_SIZE	4000
94
95#define	JME_DESC_INC(x, y)	((x) = ((x) + 1) % (y))
96
97#define	JME_PROC_MIN		10
98#define	JME_PROC_DEFAULT	(JME_RX_RING_CNT / 2)
99#define	JME_PROC_MAX		(JME_RX_RING_CNT - 1)
100
101struct jme_txdesc {
102	struct mbuf		*tx_m;
103	bus_dmamap_t		tx_dmamap;
104	int			tx_ndesc;
105	struct jme_desc		*tx_desc;
106};
107
108struct jme_rxdesc {
109	struct mbuf 		*rx_m;
110	bus_dmamap_t		rx_dmamap;
111	struct jme_desc		*rx_desc;
112};
113
114struct jme_chain_data{
115	bus_dma_tag_t		jme_ring_tag;
116	bus_dma_tag_t		jme_buffer_tag;
117	bus_dma_tag_t		jme_ssb_tag;
118	bus_dmamap_t		jme_ssb_map;
119	bus_dma_tag_t		jme_tx_tag;
120	struct jme_txdesc	jme_txdesc[JME_TX_RING_CNT];
121	bus_dma_tag_t		jme_rx_tag;
122	struct jme_rxdesc	jme_rxdesc[JME_RX_RING_CNT];
123	bus_dma_tag_t		jme_tx_ring_tag;
124	bus_dmamap_t		jme_tx_ring_map;
125	bus_dma_tag_t		jme_rx_ring_tag;
126	bus_dmamap_t		jme_rx_ring_map;
127	bus_dmamap_t		jme_rx_sparemap;
128
129	int			jme_tx_prod;
130	int			jme_tx_cons;
131	int			jme_tx_cnt;
132	int			jme_rx_cons;
133	int			jme_rxlen;
134
135	struct mbuf		*jme_rxhead;
136	struct mbuf		*jme_rxtail;
137};
138
139struct jme_ring_data {
140	struct jme_desc		*jme_tx_ring;
141	bus_addr_t		jme_tx_ring_paddr;
142	struct jme_desc		*jme_rx_ring;
143	bus_addr_t		jme_rx_ring_paddr;
144	struct jme_ssb		*jme_ssb_block;
145	bus_addr_t		jme_ssb_block_paddr;
146};
147
148#define	JME_TX_RING_ADDR(sc, i)	\
149    ((sc)->jme_rdata.jme_tx_ring_paddr + sizeof(struct jme_desc) * (i))
150#define	JME_RX_RING_ADDR(sc, i)	\
151    ((sc)->jme_rdata.jme_rx_ring_paddr + sizeof(struct jme_desc) * (i))
152
153#define	JME_TX_RING_SIZE	\
154    (sizeof(struct jme_desc) * JME_TX_RING_CNT)
155#define	JME_RX_RING_SIZE	\
156    (sizeof(struct jme_desc) * JME_RX_RING_CNT)
157#define	JME_SSB_SIZE		sizeof(struct jme_ssb)
158
159/* Statistics counters. */
160struct jme_hw_stats {
161	uint32_t		rx_good_frames;
162	uint32_t		rx_crc_errs;
163	uint32_t		rx_mii_errs;
164	uint32_t		rx_fifo_oflows;
165	uint32_t		rx_desc_empty;
166	uint32_t		rx_bad_frames;
167	uint32_t		tx_good_frames;
168	uint32_t		tx_bad_frames;
169};
170
171/*
172 * Software state per device.
173 */
174struct jme_softc {
175	struct ifnet 		*jme_ifp;
176	device_t		jme_dev;
177	device_t		jme_miibus;
178	struct resource		*jme_res[1];
179	struct resource_spec	*jme_res_spec;
180	struct resource		*jme_irq[JME_MSI_MESSAGES];
181	struct resource_spec	*jme_irq_spec;
182	void			*jme_intrhand[JME_MSI_MESSAGES];
183	int			jme_rev;
184	int			jme_chip_rev;
185	int			jme_phyaddr;
186	uint8_t			jme_eaddr[ETHER_ADDR_LEN];
187	uint32_t		jme_tx_dma_size;
188	uint32_t		jme_rx_dma_size;
189	int			jme_flags;
190#define	JME_FLAG_FPGA		0x00000001
191#define	JME_FLAG_PCIE		0x00000002
192#define	JME_FLAG_PCIX		0x00000004
193#define	JME_FLAG_MSI		0x00000008
194#define	JME_FLAG_MSIX		0x00000010
195#define	JME_FLAG_PMCAP		0x00000020
196#define	JME_FLAG_FASTETH	0x00000040
197#define	JME_FLAG_NOJUMBO	0x00000080
198#define	JME_FLAG_RXCLK		0x00000100
199#define	JME_FLAG_TXCLK		0x00000200
200#define	JME_FLAG_DMA32BIT	0x00000400
201#define	JME_FLAG_HWMIB		0x00000800
202#define	JME_FLAG_EFUSE		0x00001000
203#define	JME_FLAG_PCCPCD		0x00002000
204#define	JME_FLAG_DETACH		0x40000000
205#define	JME_FLAG_LINK		0x80000000
206
207	struct jme_hw_stats	jme_ostats;
208	struct jme_hw_stats	jme_stats;
209	struct callout		jme_tick_ch;
210	struct jme_chain_data	jme_cdata;
211	struct jme_ring_data	jme_rdata;
212	int			jme_if_flags;
213	int			jme_watchdog_timer;
214	uint32_t		jme_txcsr;
215	uint32_t		jme_rxcsr;
216	int			jme_process_limit;
217	int			jme_tx_coal_to;
218	int			jme_tx_pcd_to;
219	int			jme_tx_coal_pkt;
220	int			jme_rx_coal_to;
221	int			jme_rx_pcd_to;
222	int			jme_rx_coal_pkt;
223	volatile int		jme_morework;
224
225	struct task		jme_int_task;
226	struct task		jme_link_task;
227	struct taskqueue	*jme_tq;
228	struct mtx		jme_mtx;
229};
230
231/* Register access macros. */
232#define	CSR_WRITE_4(_sc, reg, val)	\
233	bus_write_4((_sc)->jme_res[0], (reg), (val))
234#define	CSR_READ_4(_sc, reg)		\
235	bus_read_4((_sc)->jme_res[0], (reg))
236
237#define	JME_LOCK(_sc)		mtx_lock(&(_sc)->jme_mtx)
238#define	JME_UNLOCK(_sc)		mtx_unlock(&(_sc)->jme_mtx)
239#define	JME_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->jme_mtx, MA_OWNED)
240
241#define	JME_MAXERR	5
242
243#define	JME_RXCHAIN_RESET(_sc)						\
244do {									\
245	(_sc)->jme_cdata.jme_rxhead = NULL;				\
246	(_sc)->jme_cdata.jme_rxtail = NULL;				\
247	(_sc)->jme_cdata.jme_rxlen = 0;					\
248} while (0)
249
250#define	JME_TX_TIMEOUT		5
251#define	JME_TIMEOUT		1000
252#define	JME_PHY_TIMEOUT		1000
253#define	JME_EEPROM_TIMEOUT	1000
254
255#endif
256