1 /******************************************************************************
2 SPDX-License-Identifier: BSD-3-Clause
3
4 Copyright (c) 2001-2020, Intel Corporation
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 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. 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 3. Neither the name of the Intel Corporation nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 ******************************************************************************/
34
35
36 #ifndef _FREEBSD_OS_H_
37 #define _FREEBSD_OS_H_
38
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/bus.h>
51
52 #include <net/ethernet.h>
53 #include <net/if.h>
54 #include <net/if_var.h>
55 #include <net/iflib.h>
56
57
58
59 #include <machine/bus.h>
60 #include <sys/rman.h>
61 #include <machine/resource.h>
62 #include <vm/vm.h>
63 #include <vm/pmap.h>
64 #include <machine/clock.h>
65 #include <dev/pci/pcivar.h>
66 #include <dev/pci/pcireg.h>
67
68
69 #define ASSERT(x) if(!(x)) panic("EM: x")
70 #define us_scale(x) max(1, (x/(1000000/hz)))
71 static inline int
ms_scale(int x)72 ms_scale(int x) {
73 if (hz == 1000) {
74 return (x);
75 } else if (hz > 1000) {
76 return (x*(hz/1000));
77 } else {
78 return (max(1, x/(1000/hz)));
79 }
80 }
81
82 extern int e1000_use_pause_delay;
83
84 static inline void
safe_pause_us(int x)85 safe_pause_us(int x) {
86 if (!e1000_use_pause_delay) {
87 DELAY(x);
88 } else {
89 pause("e1000_delay", max(1, x/(1000000/hz)));
90 }
91 }
92
93 static inline void
safe_pause_ms(int x)94 safe_pause_ms(int x) {
95 if (!e1000_use_pause_delay) {
96 DELAY(x*1000);
97 } else {
98 pause("e1000_delay", ms_scale(x));
99 }
100 }
101
102 #define usec_delay(x) safe_pause_us(x)
103 #define usec_delay_irq(x) usec_delay(x)
104 #define msec_delay(x) safe_pause_ms(x)
105 #define msec_delay_irq(x) msec_delay(x)
106
107 /* Enable/disable debugging statements in shared code */
108 #define DBG 0
109
110 #define DEBUGOUT(...) \
111 do { if (DBG) printf(__VA_ARGS__); } while (0)
112 #define DEBUGOUT1(...) DEBUGOUT(__VA_ARGS__)
113 #define DEBUGOUT2(...) DEBUGOUT(__VA_ARGS__)
114 #define DEBUGOUT3(...) DEBUGOUT(__VA_ARGS__)
115 #define DEBUGOUT7(...) DEBUGOUT(__VA_ARGS__)
116 #define DEBUGFUNC(F) DEBUGOUT(F "\n")
117
118 #define STATIC static
119 #define CMD_MEM_WRT_INVALIDATE 0x0010 /* BIT_4 */
120 #define PCI_COMMAND_REGISTER PCIR_COMMAND
121
122 typedef uint64_t u64;
123 typedef uint32_t u32;
124 typedef uint16_t u16;
125 typedef uint8_t u8;
126 typedef int64_t s64;
127 typedef int32_t s32;
128 typedef int16_t s16;
129 typedef int8_t s8;
130
131 #define __le16 u16
132 #define __le32 u32
133 #define __le64 u64
134
135 #if __FreeBSD_version < 800000
136 #if defined(__i386__) || defined(__amd64__)
137 #define mb() __asm volatile("mfence" ::: "memory")
138 #define wmb() __asm volatile("sfence" ::: "memory")
139 #define rmb() __asm volatile("lfence" ::: "memory")
140 #else
141 #define mb()
142 #define rmb()
143 #define wmb()
144 #endif
145 #endif /*__FreeBSD_version < 800000 */
146
147 #ifdef INVARIANTS
148 #define ASSERT_CTX_LOCK_HELD(hw) (sx_assert(iflib_ctx_lock_get(((struct e1000_osdep *)hw->back)->ctx), SX_XLOCKED))
149 #else
150 #define ASSERT_CTX_LOCK_HELD(hw)
151 #endif
152
153 #if defined(__i386__) || defined(__amd64__)
154 static __inline
prefetch(void * x)155 void prefetch(void *x)
156 {
157 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
158 }
159 #else
160 #define prefetch(x)
161 #endif
162
163 struct e1000_osdep
164 {
165 bus_space_tag_t mem_bus_space_tag;
166 bus_space_handle_t mem_bus_space_handle;
167 bus_space_tag_t io_bus_space_tag;
168 bus_space_handle_t io_bus_space_handle;
169 bus_space_tag_t flash_bus_space_tag;
170 bus_space_handle_t flash_bus_space_handle;
171 device_t dev;
172 if_ctx_t ctx;
173 };
174
175 #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
176 ? reg : e1000_translate_register_82542(reg))
177
178 #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
179
180 /* Read from an absolute offset in the adapter's memory space */
181 #define E1000_READ_OFFSET(hw, offset) \
182 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
183 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
184
185 /* Write to an absolute offset in the adapter's memory space */
186 #define E1000_WRITE_OFFSET(hw, offset, value) \
187 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
188 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
189
190 /* Register READ/WRITE macros */
191
192 #define E1000_READ_REG(hw, reg) \
193 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
194 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
195 E1000_REGISTER(hw, reg))
196
197 #define E1000_WRITE_REG(hw, reg, value) \
198 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
199 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
200 E1000_REGISTER(hw, reg), value)
201
202 #define E1000_READ_REG_ARRAY(hw, reg, index) \
203 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
204 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
205 E1000_REGISTER(hw, reg) + ((index)<< 2))
206
207 #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
208 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
209 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
210 E1000_REGISTER(hw, reg) + ((index)<< 2), value)
211
212 #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
213 #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
214
215 #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
216 bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
217 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
218 E1000_REGISTER(hw, reg) + index)
219
220 #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
221 bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
222 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
223 E1000_REGISTER(hw, reg) + index, value)
224
225 #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
226 bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
227 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
228 E1000_REGISTER(hw, reg) + (index << 1), value)
229
230 #define E1000_WRITE_REG_IO(hw, reg, value) do {\
231 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
232 ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
233 (hw)->io_base, reg); \
234 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
235 ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
236 (hw)->io_base + 4, value); } while (0)
237
238 #define E1000_READ_FLASH_REG(hw, reg) \
239 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
240 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
241
242 #define E1000_READ_FLASH_REG16(hw, reg) \
243 bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
244 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
245
246 #define E1000_WRITE_FLASH_REG(hw, reg, value) \
247 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
248 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
249
250 #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
251 bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
252 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
253
254
255 #if defined(INVARIANTS)
256 #include <sys/proc.h>
257
258 #define ASSERT_NO_LOCKS() \
259 do { \
260 int unknown_locks = curthread->td_locks - mtx_owned(&Giant); \
261 if (unknown_locks > 0) { \
262 WITNESS_WARN(WARN_GIANTOK|WARN_SLEEPOK|WARN_PANIC, NULL, "unexpected non-sleepable lock"); \
263 } \
264 MPASS(curthread->td_rw_rlocks == 0); \
265 MPASS(curthread->td_lk_slocks == 0); \
266 } while (0)
267 #else
268 #define ASSERT_NO_LOCKS()
269 #endif
270
271 #endif /* _FREEBSD_OS_H_ */
272
273