1*9d26e4fcSRobert Mustacchi /*
2*9d26e4fcSRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*9d26e4fcSRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*9d26e4fcSRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*9d26e4fcSRobert Mustacchi  * 1.0 of the CDDL.
6*9d26e4fcSRobert Mustacchi  *
7*9d26e4fcSRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*9d26e4fcSRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*9d26e4fcSRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*9d26e4fcSRobert Mustacchi  */
11*9d26e4fcSRobert Mustacchi 
12*9d26e4fcSRobert Mustacchi /*
13*9d26e4fcSRobert Mustacchi  * Copyright 2015 OmniTI Computer Consulting, Inc. All rights reserved.
14*9d26e4fcSRobert Mustacchi  * Copyright 2016 Joyent, Inc.
15*9d26e4fcSRobert Mustacchi  */
16*9d26e4fcSRobert Mustacchi 
17*9d26e4fcSRobert Mustacchi #ifndef _I40E_OSDEP_H
18*9d26e4fcSRobert Mustacchi #define	_I40E_OSDEP_H
19*9d26e4fcSRobert Mustacchi 
20*9d26e4fcSRobert Mustacchi #ifdef __cplusplus
21*9d26e4fcSRobert Mustacchi extern "C" {
22*9d26e4fcSRobert Mustacchi #endif
23*9d26e4fcSRobert Mustacchi 
24*9d26e4fcSRobert Mustacchi #include <sys/types.h>
25*9d26e4fcSRobert Mustacchi #include <sys/ddi.h>
26*9d26e4fcSRobert Mustacchi #include <sys/sunddi.h>
27*9d26e4fcSRobert Mustacchi #include <sys/pci_cap.h>
28*9d26e4fcSRobert Mustacchi #include <sys/sysmacros.h>
29*9d26e4fcSRobert Mustacchi 
30*9d26e4fcSRobert Mustacchi #define	DEBUGOUT(S)				i40e_debug(NULL, 0, S)
31*9d26e4fcSRobert Mustacchi #define	DEBUGOUT1(S, A)				i40e_debug(NULL, 0, S, A)
32*9d26e4fcSRobert Mustacchi #define	DEBUGOUT2(S, A, B)			i40e_debug(NULL, 0, S, A, B)
33*9d26e4fcSRobert Mustacchi #define	DEBUGOUT3(S, A, B, C)			i40e_debug(NULL, 0, S, A, B, C)
34*9d26e4fcSRobert Mustacchi #define	DEBUGOUT4(S, A, B, C, D)		\
35*9d26e4fcSRobert Mustacchi 	i40e_debug(NULL, 0, S, A, B, C, D)
36*9d26e4fcSRobert Mustacchi #define	DEBUGOUT5(S, A, B, C, D, E)		\
37*9d26e4fcSRobert Mustacchi 	i40e_debug(NULL, 0, S, A, B, C, D, E)
38*9d26e4fcSRobert Mustacchi #define	DEBUGOUT6(S, A, B, C, D, E, F)		\
39*9d26e4fcSRobert Mustacchi 	i40e_debug(NULL, 0, S, A, B, C, D, E, F)
40*9d26e4fcSRobert Mustacchi #define	DEBUGOUT7(S, A, B, C, D, E, F, G)	\
41*9d26e4fcSRobert Mustacchi 	i40e_debug(NULL, 0, S, A, B, C, D, E, F, G)
42*9d26e4fcSRobert Mustacchi #define	DEBUGFUNC(F)				DEBUGOUT(F);
43*9d26e4fcSRobert Mustacchi 
44*9d26e4fcSRobert Mustacchi 
45*9d26e4fcSRobert Mustacchi #define	UNREFERENCED_PARAMETER(x)		_NOTE(ARGUNUSED(x))
46*9d26e4fcSRobert Mustacchi #define	UNREFERENCED_1PARAMETER(_p)		UNREFERENCED_PARAMETER(_p)
47*9d26e4fcSRobert Mustacchi #define	UNREFERENCED_2PARAMETER(_p, _q)		_NOTE(ARGUNUSED(_p, _q))
48*9d26e4fcSRobert Mustacchi #define	UNREFERENCED_3PARAMETER(_p, _q, _r)	_NOTE(ARGUNUSED(_p, _q, _r))
49*9d26e4fcSRobert Mustacchi #define	UNREFERENCED_4PARAMETER(_p, _q, _r, _s)	_NOTE(ARGUNUSED(_p, _q, _r, _s))
50*9d26e4fcSRobert Mustacchi 
51*9d26e4fcSRobert Mustacchi #define	INLINE  inline
52*9d26e4fcSRobert Mustacchi 
53*9d26e4fcSRobert Mustacchi /*
54*9d26e4fcSRobert Mustacchi  * The mdb dmod needs to use this code as well, but mdb already defines TRUE and
55*9d26e4fcSRobert Mustacchi  * FALSE in the module API. Thus we don't define these if we're building the
56*9d26e4fcSRobert Mustacchi  * dmod, as indicated by _I40E_MDB_DMOD. However, if we don't define these, then
57*9d26e4fcSRobert Mustacchi  * the shared code will be upset.
58*9d26e4fcSRobert Mustacchi  */
59*9d26e4fcSRobert Mustacchi #ifndef _I40E_MDB_DMOD
60*9d26e4fcSRobert Mustacchi #define	FALSE	B_FALSE
61*9d26e4fcSRobert Mustacchi #define	false	B_FALSE
62*9d26e4fcSRobert Mustacchi #define	TRUE	B_TRUE
63*9d26e4fcSRobert Mustacchi #define	true	B_TRUE
64*9d26e4fcSRobert Mustacchi #endif /* _I40E_MDB_DMOD */
65*9d26e4fcSRobert Mustacchi 
66*9d26e4fcSRobert Mustacchi 
67*9d26e4fcSRobert Mustacchi #define	CPU_TO_LE16(o)	LE_16(o)
68*9d26e4fcSRobert Mustacchi #define	CPU_TO_LE32(s)	LE_32(s)
69*9d26e4fcSRobert Mustacchi #define	CPU_TO_LE64(h)	LE_64(h)
70*9d26e4fcSRobert Mustacchi #define	LE16_TO_CPU(a)	LE_16(a)
71*9d26e4fcSRobert Mustacchi #define	LE32_TO_CPU(c)	LE_32(c)
72*9d26e4fcSRobert Mustacchi #define	LE64_TO_CPU(k)	LE_64(k)
73*9d26e4fcSRobert Mustacchi 
74*9d26e4fcSRobert Mustacchi #define	I40E_NTOHS(a)	ntohs(a)
75*9d26e4fcSRobert Mustacchi #define	I40E_NTOHL(a)	ntohl(a)
76*9d26e4fcSRobert Mustacchi #define	I40E_HTONS(a)	htons(a)
77*9d26e4fcSRobert Mustacchi #define	I40E_HTONL(a)	htonl(a)
78*9d26e4fcSRobert Mustacchi 
79*9d26e4fcSRobert Mustacchi #define	i40e_memset(a, b, c, d)  memset((a), (b), (c))
80*9d26e4fcSRobert Mustacchi #define	i40e_memcpy(a, b, c, d)  bcopy((b), (a), (c))
81*9d26e4fcSRobert Mustacchi 
82*9d26e4fcSRobert Mustacchi #define	i40e_usec_delay(x) drv_usecwait(x)
83*9d26e4fcSRobert Mustacchi #define	i40e_msec_delay(x) drv_usecwait(1000 * (x))
84*9d26e4fcSRobert Mustacchi 
85*9d26e4fcSRobert Mustacchi #define	FIELD_SIZEOF(x, y) (sizeof (((x*)0)->y))
86*9d26e4fcSRobert Mustacchi 
87*9d26e4fcSRobert Mustacchi #define	BIT(a) 		(1UL << (a))
88*9d26e4fcSRobert Mustacchi #define	BIT_ULL(a) 	(1ULL << (a))
89*9d26e4fcSRobert Mustacchi 
90*9d26e4fcSRobert Mustacchi typedef boolean_t	bool;
91*9d26e4fcSRobert Mustacchi 
92*9d26e4fcSRobert Mustacchi typedef uint8_t		u8;
93*9d26e4fcSRobert Mustacchi typedef int8_t		s8;
94*9d26e4fcSRobert Mustacchi typedef uint16_t	u16;
95*9d26e4fcSRobert Mustacchi typedef int16_t		s16;
96*9d26e4fcSRobert Mustacchi typedef uint32_t	u32;
97*9d26e4fcSRobert Mustacchi typedef int32_t		s32;
98*9d26e4fcSRobert Mustacchi typedef uint64_t	u64;
99*9d26e4fcSRobert Mustacchi 
100*9d26e4fcSRobert Mustacchi /* long string relief */
101*9d26e4fcSRobert Mustacchi typedef enum i40e_status_code i40e_status;
102*9d26e4fcSRobert Mustacchi 
103*9d26e4fcSRobert Mustacchi #define	__le16  u16
104*9d26e4fcSRobert Mustacchi #define	__le32  u32
105*9d26e4fcSRobert Mustacchi #define	__le64  u64
106*9d26e4fcSRobert Mustacchi #define	__be16  u16
107*9d26e4fcSRobert Mustacchi #define	__be32  u32
108*9d26e4fcSRobert Mustacchi #define	__be64  u64
109*9d26e4fcSRobert Mustacchi 
110*9d26e4fcSRobert Mustacchi /*
111*9d26e4fcSRobert Mustacchi  * Most other systems use spin locks for interrupts. However, illumos always
112*9d26e4fcSRobert Mustacchi  * uses a single kmutex_t for both and we decide what to do based on IPL (hint:
113*9d26e4fcSRobert Mustacchi  * it's not going to be a true spin lock, we'll use an adaptive mutex).
114*9d26e4fcSRobert Mustacchi  */
115*9d26e4fcSRobert Mustacchi struct i40e_spinlock {
116*9d26e4fcSRobert Mustacchi 	kmutex_t ispl_mutex;
117*9d26e4fcSRobert Mustacchi };
118*9d26e4fcSRobert Mustacchi 
119*9d26e4fcSRobert Mustacchi /*
120*9d26e4fcSRobert Mustacchi  * Note, while prefetch is strictly not present on all architectures, (it's an
121*9d26e4fcSRobert Mustacchi  * SSE extension on i386), it is expected that the platforms provide it.
122*9d26e4fcSRobert Mustacchi  */
123*9d26e4fcSRobert Mustacchi #define	prefetch(x) prefetch_read_many(x)
124*9d26e4fcSRobert Mustacchi 
125*9d26e4fcSRobert Mustacchi struct i40e_osdep {
126*9d26e4fcSRobert Mustacchi 	off_t			ios_reg_size;
127*9d26e4fcSRobert Mustacchi 	ddi_acc_handle_t 	ios_reg_handle;
128*9d26e4fcSRobert Mustacchi 	ddi_acc_handle_t 	ios_cfg_handle;
129*9d26e4fcSRobert Mustacchi 	struct i40e		*ios_i40e;
130*9d26e4fcSRobert Mustacchi };
131*9d26e4fcSRobert Mustacchi 
132*9d26e4fcSRobert Mustacchi /*
133*9d26e4fcSRobert Mustacchi  * This structure and its members are defined by the common code. This means we
134*9d26e4fcSRobert Mustacchi  * cannot structure prefix it, even if we want to.
135*9d26e4fcSRobert Mustacchi  */
136*9d26e4fcSRobert Mustacchi struct i40e_virt_mem {
137*9d26e4fcSRobert Mustacchi 	void 	*va;
138*9d26e4fcSRobert Mustacchi 	u32	size;
139*9d26e4fcSRobert Mustacchi };
140*9d26e4fcSRobert Mustacchi 
141*9d26e4fcSRobert Mustacchi /*
142*9d26e4fcSRobert Mustacchi  * The first three members of this structure are defined by the common code.
143*9d26e4fcSRobert Mustacchi  * This means we cannot structure prefix them, even if we wanted to.
144*9d26e4fcSRobert Mustacchi  */
145*9d26e4fcSRobert Mustacchi struct i40e_dma_mem {
146*9d26e4fcSRobert Mustacchi 	void			*va;	/* Virtual address. */
147*9d26e4fcSRobert Mustacchi 	u64			pa;	/* Physical (DMA/Hardware) address. */
148*9d26e4fcSRobert Mustacchi 	size_t			size;	/* Buffer size. */
149*9d26e4fcSRobert Mustacchi 
150*9d26e4fcSRobert Mustacchi 	/* illumos-private members */
151*9d26e4fcSRobert Mustacchi 	ddi_acc_handle_t	idm_acc_handle;	/* Data access handle */
152*9d26e4fcSRobert Mustacchi 	ddi_dma_handle_t	idm_dma_handle;	/* DMA handle */
153*9d26e4fcSRobert Mustacchi 	uint32_t		idm_alignment;	/* Requested alignment */
154*9d26e4fcSRobert Mustacchi };
155*9d26e4fcSRobert Mustacchi 
156*9d26e4fcSRobert Mustacchi struct i40e_hw; /* forward decl */
157*9d26e4fcSRobert Mustacchi 
158*9d26e4fcSRobert Mustacchi #define	OS_DEP(hw) ((struct i40e_osdep *)((hw)->back))
159*9d26e4fcSRobert Mustacchi #define	i40e_read_pci_cfg(hw, reg) \
160*9d26e4fcSRobert Mustacchi 	(pci_config_get16(OS_DEP(hw)->ios_cfg_handle, (reg)))
161*9d26e4fcSRobert Mustacchi #define	i40e_write_pci_cfg(hw, reg, value) \
162*9d26e4fcSRobert Mustacchi 	(pci_config_put16(OS_DEP(hw)->ios_cfg_handle, (reg), (value)))
163*9d26e4fcSRobert Mustacchi 
164*9d26e4fcSRobert Mustacchi /*
165*9d26e4fcSRobert Mustacchi  * Intel expects that the symbol wr32 and rd32 be defined to something which can
166*9d26e4fcSRobert Mustacchi  * read and write the 32-bit register in PCI space.
167*9d26e4fcSRobert Mustacchi  *
168*9d26e4fcSRobert Mustacchi  * To make it easier for readers and satisfy the general agreement that macros
169*9d26e4fcSRobert Mustacchi  * should be in all capitals, we use our own versions of these macros.
170*9d26e4fcSRobert Mustacchi  */
171*9d26e4fcSRobert Mustacchi #define	wr32(hw, reg, value) \
172*9d26e4fcSRobert Mustacchi 	ddi_put32(OS_DEP(hw)->ios_reg_handle, \
173*9d26e4fcSRobert Mustacchi 	    (uint32_t *)((uintptr_t)(hw)->hw_addr + (reg)), (value))
174*9d26e4fcSRobert Mustacchi #define	rd32(hw, reg) \
175*9d26e4fcSRobert Mustacchi 	ddi_get32(OS_DEP(hw)->ios_reg_handle, \
176*9d26e4fcSRobert Mustacchi 	    (uint32_t *)((uintptr_t)(hw)->hw_addr + (reg)))
177*9d26e4fcSRobert Mustacchi #define	I40E_WRITE_REG	wr32
178*9d26e4fcSRobert Mustacchi #define	I40E_READ_REG	rd32
179*9d26e4fcSRobert Mustacchi 
180*9d26e4fcSRobert Mustacchi /*
181*9d26e4fcSRobert Mustacchi  * The use of GLGEN_STAT presumes that we're only using this file for a PF
182*9d26e4fcSRobert Mustacchi  * driver. If we end up doing a VF driver, then we'll want to logically change
183*9d26e4fcSRobert Mustacchi  * this.
184*9d26e4fcSRobert Mustacchi  */
185*9d26e4fcSRobert Mustacchi #define	i40e_flush(hw) (void) rd32(hw, I40E_GLGEN_STAT)
186*9d26e4fcSRobert Mustacchi 
187*9d26e4fcSRobert Mustacchi extern void i40e_debug(void *, u32, char *, ...);
188*9d26e4fcSRobert Mustacchi extern boolean_t i40e_set_hw_bus_info(struct i40e_hw *);
189*9d26e4fcSRobert Mustacchi 
190*9d26e4fcSRobert Mustacchi #ifdef __cplusplus
191*9d26e4fcSRobert Mustacchi }
192*9d26e4fcSRobert Mustacchi #endif
193*9d26e4fcSRobert Mustacchi 
194*9d26e4fcSRobert Mustacchi #endif /* _I40E_OSDEP_H */