xref: /freebsd/sys/dev/iavf/iavf_osdep.h (revision c68eed82a3dcadf0c826e9e150f59769f4c44f24)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
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 are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
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  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @file iavf_osdep.h
34  * @brief OS compatibility layer definitions
35  *
36  * Contains macros and definitions used to implement an OS compatibility layer
37  * used by some of the hardware files.
38  */
39 #ifndef _IAVF_OSDEP_H_
40 #define _IAVF_OSDEP_H_
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/endian.h>
46 #include <sys/mbuf.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/bus.h>
52 #include <machine/bus.h>
53 #include <sys/rman.h>
54 #include <machine/resource.h>
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57 #include <machine/clock.h>
58 #include <dev/pci/pcivar.h>
59 #include <dev/pci/pcireg.h>
60 
61 #include "iavf_status.h"
62 #include "iavf_debug.h"
63 
64 #define iavf_usec_delay(x) DELAY(x)
65 #define iavf_msec_delay(x) DELAY(1000 * (x))
66 
67 #define DBG 0
68 #define DEBUGFUNC(F)        DEBUGOUT(F);
69 #if DBG
70 	#define DEBUGOUT(S)         printf(S "\n")
71 	#define DEBUGOUT1(S,A)      printf(S "\n",A)
72 	#define DEBUGOUT2(S,A,B)    printf(S "\n",A,B)
73 	#define DEBUGOUT3(S,A,B,C)  printf(S "\n",A,B,C)
74 	#define DEBUGOUT7(S,A,B,C,D,E,F,G)  printf(S "\n",A,B,C,D,E,F,G)
75 #else
76 	#define DEBUGOUT(S)
77 	#define DEBUGOUT1(S,A)
78 	#define DEBUGOUT2(S,A,B)
79 	#define DEBUGOUT3(S,A,B,C)
80 	#define DEBUGOUT6(S,A,B,C,D,E,F)
81 	#define DEBUGOUT7(S,A,B,C,D,E,F,G)
82 #endif
83 
84 #define UNREFERENCED_PARAMETER(_p) _p = _p
85 #define UNREFERENCED_1PARAMETER(_p) do {			\
86 	UNREFERENCED_PARAMETER(_p);				\
87 } while (0)
88 #define UNREFERENCED_2PARAMETER(_p, _q) do {			\
89 	UNREFERENCED_PARAMETER(_p);				\
90 	UNREFERENCED_PARAMETER(_q);				\
91 } while (0)
92 #define UNREFERENCED_3PARAMETER(_p, _q, _r) do {		\
93 	UNREFERENCED_PARAMETER(_p);				\
94 	UNREFERENCED_PARAMETER(_q);				\
95 	UNREFERENCED_PARAMETER(_r);				\
96 } while (0)
97 #define UNREFERENCED_4PARAMETER(_p, _q, _r, _s) do {		\
98 	UNREFERENCED_PARAMETER(_p);				\
99 	UNREFERENCED_PARAMETER(_q);				\
100 	UNREFERENCED_PARAMETER(_r);				\
101 	UNREFERENCED_PARAMETER(_s);				\
102 } while (0)
103 #define UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t) do {	\
104 	UNREFERENCED_PARAMETER(_p);				\
105 	UNREFERENCED_PARAMETER(_q);				\
106 	UNREFERENCED_PARAMETER(_r);				\
107 	UNREFERENCED_PARAMETER(_s);				\
108 	UNREFERENCED_PARAMETER(_t);				\
109 } while (0)
110 
111 #define STATIC	static
112 #define INLINE  inline
113 
114 #define iavf_memset(a, b, c, d)  memset((a), (b), (c))
115 #define iavf_memcpy(a, b, c, d)  memcpy((a), (b), (c))
116 
117 #define CPU_TO_LE16(o)	htole16(o)
118 #define CPU_TO_LE32(s)	htole32(s)
119 #define CPU_TO_LE64(h)	htole64(h)
120 #define LE16_TO_CPU(a)	le16toh(a)
121 #define LE32_TO_CPU(c)	le32toh(c)
122 #define LE64_TO_CPU(k)	le64toh(k)
123 
124 /**
125  * @typedef u8
126  * @brief compatibility typedef for uint8_t
127  */
128 typedef uint8_t		u8;
129 
130 /**
131  * @typedef s8
132  * @brief compatibility typedef for int8_t
133  */
134 typedef int8_t		s8;
135 
136 /**
137  * @typedef u16
138  * @brief compatibility typedef for uint16_t
139  */
140 typedef uint16_t	u16;
141 
142 /**
143  * @typedef s16
144  * @brief compatibility typedef for int16_t
145  */
146 typedef int16_t		s16;
147 
148 /**
149  * @typedef u32
150  * @brief compatibility typedef for uint32_t
151  */
152 typedef uint32_t	u32;
153 
154 /**
155  * @typedef s32
156  * @brief compatibility typedef for int32_t
157  */
158 typedef int32_t		s32;
159 
160 /**
161  * @typedef u64
162  * @brief compatibility typedef for uint64_t
163  */
164 typedef uint64_t	u64;
165 
166 #define __le16  u16
167 #define __le32  u32
168 #define __le64  u64
169 #define __be16  u16
170 #define __be32  u32
171 #define __be64  u64
172 
173 /**
174  * @struct iavf_spinlock
175  * @brief OS wrapper for a non-sleeping lock
176  *
177  * Wrapper used to provide an implementation of a non-sleeping lock.
178  */
179 struct iavf_spinlock {
180         struct mtx mutex;
181 };
182 
183 /**
184  * @struct iavf_osdep
185  * @brief Storage for data used by the osdep interface
186  *
187  * Contains data used by the osdep layer. Accessed via the hw->back pointer.
188  */
189 struct iavf_osdep {
190 	bus_space_tag_t		mem_bus_space_tag;
191 	bus_space_handle_t	mem_bus_space_handle;
192 	bus_size_t		mem_bus_space_size;
193 	uint32_t		flush_reg;
194 	int			i2c_intfc_num;
195 	device_t		dev;
196 };
197 
198 /**
199  * @struct iavf_dma_mem
200  * @brief DMA memory map
201  *
202  * Structure representing a DMA memory mapping.
203  */
204 struct iavf_dma_mem {
205 	void			*va;
206 	u64			pa;
207 	bus_dma_tag_t		tag;
208 	bus_dmamap_t		map;
209 	bus_dma_segment_t	seg;
210 	bus_size_t              size;
211 	int			nseg;
212 	int                     flags;
213 };
214 
215 /**
216  * @struct iavf_virt_mem
217  * @brief Virtual memory
218  *
219  * Structure representing some virtual memory.
220  */
221 struct iavf_virt_mem {
222 	void *va;
223 	u32 size;
224 };
225 
226 struct iavf_hw; /* forward decl */
227 u16	iavf_read_pci_cfg(struct iavf_hw *, u32);
228 void	iavf_write_pci_cfg(struct iavf_hw *, u32, u16);
229 
230 /*
231 ** iavf_debug - OS dependent version of shared code debug printing
232 */
233 #define iavf_debug(h, m, s, ...)  iavf_debug_shared(h, m, s, ##__VA_ARGS__)
234 void iavf_debug_shared(struct iavf_hw *hw, uint64_t mask,
235     char *fmt_str, ...) __printflike(3, 4);
236 
237 /*
238 ** This hardware supports either 16 or 32 byte rx descriptors;
239 ** the driver only uses the 32 byte kind.
240 */
241 #define iavf_rx_desc iavf_32byte_rx_desc
242 
243 uint32_t iavf_rd32(struct iavf_hw *hw, uint32_t reg);
244 void iavf_wr32(struct iavf_hw *hw, uint32_t reg, uint32_t val);
245 void iavf_flush(struct iavf_hw *hw);
246 #define rd32(hw, reg)		iavf_rd32(hw, reg)
247 #define wr32(hw, reg, val)	iavf_wr32(hw, reg, val)
248 
249 #endif /* _IAVF_OSDEP_H_ */
250