xref: /freebsd/sys/dev/e1000/e1000_osdep.h (revision 81b22a98)
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 /*$FreeBSD$*/
35 
36 
37 #ifndef _FREEBSD_OS_H_
38 #define _FREEBSD_OS_H_
39 
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.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 
53 #include <net/ethernet.h>
54 #include <net/if.h>
55 #include <net/if_var.h>
56 #include <net/iflib.h>
57 
58 
59 
60 #include <machine/bus.h>
61 #include <sys/rman.h>
62 #include <machine/resource.h>
63 #include <vm/vm.h>
64 #include <vm/pmap.h>
65 #include <machine/clock.h>
66 #include <dev/pci/pcivar.h>
67 #include <dev/pci/pcireg.h>
68 
69 
70 #define ASSERT(x) if(!(x)) panic("EM: x")
71 #define us_scale(x)  max(1, (x/(1000000/hz)))
72 static inline int
73 ms_scale(int x) {
74 	if (hz == 1000) {
75 		return (x);
76 	} else if (hz > 1000) {
77 		return (x*(hz/1000));
78 	} else {
79 		return (max(1, x/(1000/hz)));
80 	}
81 }
82 
83 static inline void
84 safe_pause_us(int x) {
85 	if (cold) {
86 		DELAY(x);
87 	} else {
88 		pause("e1000_delay", max(1,  x/(1000000/hz)));
89 	}
90 }
91 
92 static inline void
93 safe_pause_ms(int x) {
94 	if (cold) {
95 		DELAY(x*1000);
96 	} else {
97 		pause("e1000_delay", ms_scale(x));
98 	}
99 }
100 
101 #define usec_delay(x) safe_pause_us(x)
102 #define usec_delay_irq(x) usec_delay(x)
103 #define msec_delay(x) safe_pause_ms(x)
104 #define msec_delay_irq(x) msec_delay(x)
105 
106 /* Enable/disable debugging statements in shared code */
107 #define DBG		0
108 
109 #define DEBUGOUT(...) \
110     do { if (DBG) printf(__VA_ARGS__); } while (0)
111 #define DEBUGOUT1(...)			DEBUGOUT(__VA_ARGS__)
112 #define DEBUGOUT2(...)			DEBUGOUT(__VA_ARGS__)
113 #define DEBUGOUT3(...)			DEBUGOUT(__VA_ARGS__)
114 #define DEBUGOUT7(...)			DEBUGOUT(__VA_ARGS__)
115 #define DEBUGFUNC(F)			DEBUGOUT(F "\n")
116 
117 #define STATIC			static
118 #define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
119 #define PCI_COMMAND_REGISTER	PCIR_COMMAND
120 
121 typedef uint64_t	u64;
122 typedef uint32_t	u32;
123 typedef uint16_t	u16;
124 typedef uint8_t		u8;
125 typedef int64_t		s64;
126 typedef int32_t		s32;
127 typedef int16_t		s16;
128 typedef int8_t		s8;
129 
130 #define __le16		u16
131 #define __le32		u32
132 #define __le64		u64
133 
134 #if __FreeBSD_version < 800000
135 #if defined(__i386__) || defined(__amd64__)
136 #define mb()	__asm volatile("mfence" ::: "memory")
137 #define wmb()	__asm volatile("sfence" ::: "memory")
138 #define rmb()	__asm volatile("lfence" ::: "memory")
139 #else
140 #define mb()
141 #define rmb()
142 #define wmb()
143 #endif
144 #endif /*__FreeBSD_version < 800000 */
145 
146 #ifdef INVARIANTS
147 #define ASSERT_CTX_LOCK_HELD(hw) (sx_assert(iflib_ctx_lock_get(((struct e1000_osdep *)hw->back)->ctx), SX_XLOCKED))
148 #else
149 #define ASSERT_CTX_LOCK_HELD(hw)
150 #endif
151 
152 #if defined(__i386__) || defined(__amd64__)
153 static __inline
154 void prefetch(void *x)
155 {
156 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
157 }
158 #else
159 #define prefetch(x)
160 #endif
161 
162 struct e1000_osdep
163 {
164 	bus_space_tag_t    mem_bus_space_tag;
165 	bus_space_handle_t mem_bus_space_handle;
166 	bus_space_tag_t    io_bus_space_tag;
167 	bus_space_handle_t io_bus_space_handle;
168 	bus_space_tag_t    flash_bus_space_tag;
169 	bus_space_handle_t flash_bus_space_handle;
170 	device_t	   dev;
171 	if_ctx_t	   ctx;
172 };
173 
174 #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
175     ? reg : e1000_translate_register_82542(reg))
176 
177 #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
178 
179 /* Read from an absolute offset in the adapter's memory space */
180 #define E1000_READ_OFFSET(hw, offset) \
181     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
182     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
183 
184 /* Write to an absolute offset in the adapter's memory space */
185 #define E1000_WRITE_OFFSET(hw, offset, value) \
186     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
187     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
188 
189 /* Register READ/WRITE macros */
190 
191 #define E1000_READ_REG(hw, reg) \
192     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
193         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
194         E1000_REGISTER(hw, reg))
195 
196 #define E1000_WRITE_REG(hw, reg, value) \
197     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
198         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
199         E1000_REGISTER(hw, reg), value)
200 
201 #define E1000_READ_REG_ARRAY(hw, reg, index) \
202     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
203         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
204         E1000_REGISTER(hw, reg) + ((index)<< 2))
205 
206 #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
207     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
208         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
209         E1000_REGISTER(hw, reg) + ((index)<< 2), value)
210 
211 #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
212 #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
213 
214 #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
215     bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
216         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
217         E1000_REGISTER(hw, reg) + index)
218 
219 #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
220     bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
221         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
222         E1000_REGISTER(hw, reg) + index, value)
223 
224 #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
225     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
226         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
227         E1000_REGISTER(hw, reg) + (index << 1), value)
228 
229 #define E1000_WRITE_REG_IO(hw, reg, value) do {\
230     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
231         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
232         (hw)->io_base, reg); \
233     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
234         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
235         (hw)->io_base + 4, value); } while (0)
236 
237 #define E1000_READ_FLASH_REG(hw, reg) \
238     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
239         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
240 
241 #define E1000_READ_FLASH_REG16(hw, reg) \
242     bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
243         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
244 
245 #define E1000_WRITE_FLASH_REG(hw, reg, value) \
246     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
247         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
248 
249 #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
250     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
251         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
252 
253 
254 #if defined(INVARIANTS)
255 #include <sys/proc.h>
256 
257 #define ASSERT_NO_LOCKS()				\
258 	do {						\
259 	     int unknown_locks = curthread->td_locks - mtx_owned(&Giant);	\
260 	     if (unknown_locks > 0) {					\
261 		     WITNESS_WARN(WARN_GIANTOK|WARN_SLEEPOK|WARN_PANIC, NULL, "unexpected non-sleepable lock"); \
262 	     }								\
263 	     MPASS(curthread->td_rw_rlocks == 0);			\
264 	     MPASS(curthread->td_lk_slocks == 0);			\
265 	} while (0)
266 #else
267 #define ASSERT_NO_LOCKS()
268 #endif
269 
270 #endif  /* _FREEBSD_OS_H_ */
271 
272