1 /******************************************************************************
2 SPDX-License-Identifier: BSD-3-Clause
4 Copyright (c) 2001-2015, Intel Corporation
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 1. Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
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.
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.
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.
33 ******************************************************************************/
37 #ifndef _FREEBSD_OS_H_
38 #define _FREEBSD_OS_H_
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/mutex.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
51 #include <machine/bus.h>
53 #include <machine/resource.h>
56 #include <machine/clock.h>
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pcireg.h>
61 #define ASSERT(x) if(!(x)) panic("EM: x")
63 #define usec_delay(x) DELAY(x)
64 #define usec_delay_irq(x) usec_delay(x)
65 #define msec_delay(x) DELAY(1000*(x))
66 #define msec_delay_irq(x) DELAY(1000*(x))
68 /* Enable/disable debugging statements in shared code */
71 #define DEBUGOUT(...) \
72 do { if (DBG) printf(__VA_ARGS__); } while (0)
73 #define DEBUGOUT1(...) DEBUGOUT(__VA_ARGS__)
74 #define DEBUGOUT2(...) DEBUGOUT(__VA_ARGS__)
75 #define DEBUGOUT3(...) DEBUGOUT(__VA_ARGS__)
76 #define DEBUGOUT7(...) DEBUGOUT(__VA_ARGS__)
77 #define DEBUGFUNC(F) DEBUGOUT(F "\n")
82 #define CMD_MEM_WRT_INVALIDATE 0x0010 /* BIT_4 */
83 #define PCI_COMMAND_REGISTER PCIR_COMMAND
85 /* Mutex used in the shared code */
86 #define E1000_MUTEX struct mtx
87 #define E1000_MUTEX_INIT(mutex) mtx_init((mutex), #mutex, \
90 #define E1000_MUTEX_DESTROY(mutex) mtx_destroy(mutex)
91 #define E1000_MUTEX_LOCK(mutex) mtx_lock(mutex)
92 #define E1000_MUTEX_TRYLOCK(mutex) mtx_trylock(mutex)
93 #define E1000_MUTEX_UNLOCK(mutex) mtx_unlock(mutex)
108 #if __FreeBSD_version < 800000
109 #if defined(__i386__) || defined(__amd64__)
110 #define mb() __asm volatile("mfence" ::: "memory")
111 #define wmb() __asm volatile("sfence" ::: "memory")
112 #define rmb() __asm volatile("lfence" ::: "memory")
118 #endif /*__FreeBSD_version < 800000 */
120 #if defined(__i386__) || defined(__amd64__)
122 void prefetch(void *x)
124 __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
132 bus_space_tag_t mem_bus_space_tag;
133 bus_space_handle_t mem_bus_space_handle;
134 bus_space_tag_t io_bus_space_tag;
135 bus_space_handle_t io_bus_space_handle;
136 bus_space_tag_t flash_bus_space_tag;
137 bus_space_handle_t flash_bus_space_handle;
141 #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
142 ? reg : e1000_translate_register_82542(reg))
144 #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
146 /* Read from an absolute offset in the adapter's memory space */
147 #define E1000_READ_OFFSET(hw, offset) \
148 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
149 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
151 /* Write to an absolute offset in the adapter's memory space */
152 #define E1000_WRITE_OFFSET(hw, offset, value) \
153 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
154 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
156 /* Register READ/WRITE macros */
158 #define E1000_READ_REG(hw, reg) \
159 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
160 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
161 E1000_REGISTER(hw, reg))
163 #define E1000_WRITE_REG(hw, reg, value) \
164 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
165 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
166 E1000_REGISTER(hw, reg), value)
168 #define E1000_READ_REG_ARRAY(hw, reg, index) \
169 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
170 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
171 E1000_REGISTER(hw, reg) + ((index)<< 2))
173 #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
174 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
175 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
176 E1000_REGISTER(hw, reg) + ((index)<< 2), value)
178 #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
179 #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
181 #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
182 bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
183 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
184 E1000_REGISTER(hw, reg) + index)
186 #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
187 bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
188 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
189 E1000_REGISTER(hw, reg) + index, value)
191 #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
192 bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
193 ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
194 E1000_REGISTER(hw, reg) + (index << 1), value)
196 #define E1000_WRITE_REG_IO(hw, reg, value) do {\
197 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
198 ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
199 (hw)->io_base, reg); \
200 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
201 ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
202 (hw)->io_base + 4, value); } while (0)
204 #define E1000_READ_FLASH_REG(hw, reg) \
205 bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
206 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
208 #define E1000_READ_FLASH_REG16(hw, reg) \
209 bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
210 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
212 #define E1000_WRITE_FLASH_REG(hw, reg, value) \
213 bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
214 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
216 #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
217 bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
218 ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
220 #endif /* _FREEBSD_OS_H_ */