1/*******************************************************************************
2SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3
4Copyright (c) 2006-2013, Myricom Inc.
5All rights reserved.
6
7Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are met:
9
10 1. Redistributions of source code must retain the above copyright notice,
11    this list of conditions and the following disclaimer.
12
13 2. Neither the name of the Myricom Inc, nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27POSSIBILITY OF SUCH DAMAGE.
28
29$FreeBSD$
30
31***************************************************************************/
32
33#define MXGE_ETH_STOPPED 0
34#define MXGE_ETH_STOPPING 1
35#define MXGE_ETH_STARTING 2
36#define MXGE_ETH_RUNNING 3
37#define MXGE_ETH_OPEN_FAILED 4
38
39#define MXGE_FW_OFFSET 1024*1024
40#define MXGE_EEPROM_STRINGS_SIZE 256
41#define MXGE_MAX_SEND_DESC 128
42
43#if ((__FreeBSD_version > 800000 && __FreeBSD_version < 800005) \
44     || __FreeBSD_version < 700111)
45#define MXGE_VIRT_JUMBOS 1
46#else
47#define MXGE_VIRT_JUMBOS 0
48#endif
49
50#if (__FreeBSD_version > 800082)
51#define IFNET_BUF_RING 1
52#endif
53
54#if (__FreeBSD_version < 1000020)
55#undef IF_Kbps
56#undef IF_Mbps
57#undef IF_Gbps
58#define	IF_Kbps(x)	((uintmax_t)(x) * 1000)	/* kilobits/sec. */
59#define	IF_Mbps(x)	(IF_Kbps((x) * 1000))	/* megabits/sec. */
60#define	IF_Gbps(x)	(IF_Mbps((x) * 1000))	/* gigabits/sec. */
61#endif
62
63#ifndef VLAN_CAPABILITIES
64#define VLAN_CAPABILITIES(ifp)
65#define mxge_vlans_active(sc) (sc)->ifp->if_nvlans
66#else
67#define mxge_vlans_active(sc) (sc)->ifp->if_vlantrunk
68#endif
69
70#ifndef VLAN_TAG_VALUE
71#define MXGE_NEW_VLAN_API
72#endif
73
74#ifndef IFCAP_LRO
75#define IFCAP_LRO 0
76#endif
77
78#ifndef IFCAP_TSO
79#define IFCAP_TSO 0
80#endif
81
82#ifndef IFCAP_TSO4
83#define IFCAP_TSO4 0
84#endif
85
86#ifndef IFCAP_TSO6
87#define IFCAP_TSO6 0
88#endif
89
90#ifndef IFCAP_TXCSUM_IPV6
91#define IFCAP_TXCSUM_IPV6 0
92#endif
93
94#ifndef IFCAP_RXCSUM_IPV6
95#define IFCAP_RXCSUM_IPV6 0
96#endif
97
98#ifndef CSUM_TSO
99#define CSUM_TSO 0
100#endif
101
102#ifndef CSUM_TCP_IPV6
103#define CSUM_TCP_IPV6 0
104#endif
105
106#ifndef CSUM_UDP_IPV6
107#define CSUM_UDP_IPV6 0
108#endif
109
110#ifndef CSUM_DELAY_DATA_IPV6
111#define CSUM_DELAY_DATA_IPV6 0
112#endif
113
114typedef struct {
115	void *addr;
116	bus_addr_t bus_addr;
117	bus_dma_tag_t dmat;
118	bus_dmamap_t map;
119} mxge_dma_t;
120
121
122typedef struct {
123	mcp_slot_t *entry;
124	mxge_dma_t dma;
125	int cnt;
126	int idx;
127	int mask;
128} mxge_rx_done_t;
129
130typedef struct
131{
132  uint32_t data0;
133  uint32_t data1;
134  uint32_t data2;
135} mxge_cmd_t;
136
137struct mxge_rx_buffer_state {
138	struct mbuf *m;
139	bus_dmamap_t map;
140};
141
142struct mxge_tx_buffer_state {
143	struct mbuf *m;
144	bus_dmamap_t map;
145	int flag;
146};
147
148typedef struct
149{
150	volatile mcp_kreq_ether_recv_t *lanai;	/* lanai ptr for recv ring */
151	mcp_kreq_ether_recv_t *shadow;	/* host shadow of recv ring */
152	struct mxge_rx_buffer_state *info;
153	bus_dma_tag_t dmat;
154	bus_dmamap_t extra_map;
155	int cnt;
156	int nbufs;
157	int cl_size;
158	int alloc_fail;
159	int mask;			/* number of rx slots -1 */
160	int mlen;
161} mxge_rx_ring_t;
162
163typedef struct
164{
165	struct mtx mtx;
166#ifdef IFNET_BUF_RING
167	struct buf_ring *br;
168#endif
169	volatile mcp_kreq_ether_send_t *lanai;	/* lanai ptr for sendq	*/
170	volatile uint32_t *send_go;		/* doorbell for sendq */
171	volatile uint32_t *send_stop;		/* doorbell for sendq */
172	mcp_kreq_ether_send_t *req_list;	/* host shadow of sendq */
173	char *req_bytes;
174	bus_dma_segment_t *seg_list;
175	struct mxge_tx_buffer_state *info;
176	bus_dma_tag_t dmat;
177	int req;			/* transmits submitted	*/
178	int mask;			/* number of transmit slots -1 */
179	int done;			/* transmits completed	*/
180	int pkt_done;			/* packets completed */
181	int max_desc;			/* max descriptors per xmit */
182	int queue_active;		/* fw currently polling this queue*/
183	int activate;
184	int deactivate;
185	int stall;			/* #times hw queue exhausted */
186	int wake;			/* #times irq re-enabled xmit */
187	int watchdog_req;		/* cache of req */
188	int watchdog_done;		/* cache of done */
189	int watchdog_rx_pause;		/* cache of pause rq recvd */
190	int defrag;
191	char mtx_name[16];
192} mxge_tx_ring_t;
193
194struct mxge_softc;
195typedef struct mxge_softc mxge_softc_t;
196
197struct mxge_slice_state {
198	mxge_softc_t *sc;
199	mxge_tx_ring_t tx;		/* transmit ring 	*/
200	mxge_rx_ring_t rx_small;
201	mxge_rx_ring_t rx_big;
202	mxge_rx_done_t rx_done;
203	mcp_irq_data_t *fw_stats;
204	volatile uint32_t *irq_claim;
205	u_long ipackets;
206	u_long opackets;
207	u_long obytes;
208	u_long omcasts;
209	u_long oerrors;
210	int if_drv_flags;
211	struct lro_ctrl lc;
212	mxge_dma_t fw_stats_dma;
213	struct sysctl_oid *sysctl_tree;
214	struct sysctl_ctx_list sysctl_ctx;
215	char scratch[256];
216};
217
218struct mxge_softc {
219	struct ifnet* ifp;
220	struct mxge_slice_state *ss;
221	int tx_boundary;		/* boundary transmits cannot cross*/
222	int lro_cnt;
223	bus_dma_tag_t	parent_dmat;
224	volatile uint8_t *sram;
225	int sram_size;
226	volatile uint32_t *irq_deassert;
227	mcp_cmd_response_t *cmd;
228	mxge_dma_t cmd_dma;
229	mxge_dma_t zeropad_dma;
230	struct pci_dev *pdev;
231	int legacy_irq;
232	int link_state;
233	unsigned int rdma_tags_available;
234	int intr_coal_delay;
235	volatile uint32_t *intr_coal_delay_ptr;
236	int wc;
237	struct mtx cmd_mtx;
238	struct mtx driver_mtx;
239	int wake_queue;
240	int stop_queue;
241	int down_cnt;
242	int watchdog_resets;
243	int watchdog_countdown;
244	int pause;
245	struct resource *mem_res;
246	struct resource *irq_res;
247	struct resource **msix_irq_res;
248	struct resource *msix_table_res;
249	struct resource *msix_pba_res;
250	void *ih;
251	void **msix_ih;
252	char *fw_name;
253	char eeprom_strings[MXGE_EEPROM_STRINGS_SIZE];
254	char fw_version[128];
255	int fw_ver_major;
256	int fw_ver_minor;
257	int fw_ver_tiny;
258	int adopted_rx_filter_bug;
259	device_t dev;
260	struct ifmedia media;
261	int read_dma;
262	int write_dma;
263	int read_write_dma;
264	int fw_multicast_support;
265	int link_width;
266	int max_mtu;
267	int throttle;
268	int tx_defrag;
269	int media_flags;
270	int need_media_probe;
271	int num_slices;
272	int rx_ring_size;
273	int dying;
274	int connector;
275	int current_media;
276	int max_tso6_hlen;
277	mxge_dma_t dmabench_dma;
278	struct callout co_hdl;
279	struct taskqueue *tq;
280	struct task watchdog_task;
281	struct sysctl_oid *slice_sysctl_tree;
282	struct sysctl_ctx_list slice_sysctl_ctx;
283	char *mac_addr_string;
284	uint8_t	mac_addr[6];		/* eeprom mac address */
285	uint16_t pectl;			/* save PCIe CTL state */
286	char product_code_string[64];
287	char serial_number_string[64];
288	char cmd_mtx_name[16];
289	char driver_mtx_name[16];
290};
291
292#define MXGE_PCI_VENDOR_MYRICOM 	0x14c1
293#define MXGE_PCI_DEVICE_Z8E 	0x0008
294#define MXGE_PCI_DEVICE_Z8E_9 	0x0009
295#define MXGE_PCI_REV_Z8E	0
296#define MXGE_PCI_REV_Z8ES	1
297#define MXGE_XFP_COMPLIANCE_BYTE	131
298#define MXGE_SFP_COMPLIANCE_BYTE	  3
299#define MXGE_MIN_THROTTLE	416
300#define MXGE_MAX_THROTTLE	4096
301
302/* Types of connectors on NICs supported by this driver */
303#define MXGE_CX4 0
304#define MXGE_XFP 1
305#define MXGE_SFP 2
306#define MXGE_QRF 3
307
308#define MXGE_HIGHPART_TO_U32(X) \
309(sizeof (X) == 8) ? ((uint32_t)((uint64_t)(X) >> 32)) : (0)
310#define MXGE_LOWPART_TO_U32(X) ((uint32_t)(X))
311
312struct mxge_media_type
313{
314	int flag;
315	uint8_t bitmask;
316	char *name;
317};
318
319struct mxge_pkt_info {
320	int ip_off;
321	int ip_hlen;
322	struct ip *ip;
323	struct ip6_hdr *ip6;
324	struct tcphdr *tcp;
325};
326
327
328/* implement our own memory barriers, since bus_space_barrier
329   cannot handle write-combining regions */
330
331#if __FreeBSD_version < 800053
332
333#if defined (__GNUC__)
334  #if #cpu(i386) || defined __i386 || defined i386 || defined __i386__ || #cpu(x86_64) || defined __x86_64__
335    #define wmb()  __asm__ __volatile__ ("sfence;": : :"memory")
336  #elif #cpu(sparc64) || defined sparc64 || defined __sparcv9
337    #define wmb()  __asm__ __volatile__ ("membar #MemIssue": : :"memory")
338  #elif #cpu(sparc) || defined sparc || defined __sparc__
339    #define wmb()  __asm__ __volatile__ ("stbar;": : :"memory")
340  #else
341    #define wmb() 	/* XXX just to make this compile */
342  #endif
343#else
344  #error "unknown compiler"
345#endif
346
347#endif
348
349static inline void
350mxge_pio_copy(volatile void *to_v, void *from_v, size_t size)
351{
352  register volatile uintptr_t *to;
353  volatile uintptr_t *from;
354  size_t i;
355
356  to = (volatile uintptr_t *) to_v;
357  from = from_v;
358  for (i = (size / sizeof (uintptr_t)); i; i--) {
359	  *to = *from;
360	  to++;
361	  from++;
362  }
363
364}
365
366void mxge_lro_flush(struct mxge_slice_state *ss, struct lro_entry *lro);
367int mxge_lro_rx(struct mxge_slice_state *ss, struct mbuf *m_head,
368		uint32_t csum);
369
370
371
372/*
373  This file uses Myri10GE driver indentation.
374
375  Local Variables:
376  c-file-style:"linux"
377  tab-width:8
378  End:
379*/
380