xref: /freebsd/sys/dev/e1000/e1000_osdep.h (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1 /******************************************************************************
2   SPDX-License-Identifier: BSD-3-Clause
3 
4   Copyright (c) 2001-2015, 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/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 #include <machine/bus.h>
52 #include <sys/rman.h>
53 #include <machine/resource.h>
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <machine/clock.h>
57 #include <dev/pci/pcivar.h>
58 #include <dev/pci/pcireg.h>
59 
60 
61 #define ASSERT(x) if(!(x)) panic("EM: x")
62 
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))
67 
68 /* Enable/disable debugging statements in shared code */
69 #define DBG		0
70 
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")
78 
79 #define STATIC			static
80 #define FALSE			0
81 #define TRUE			1
82 #define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
83 #define PCI_COMMAND_REGISTER	PCIR_COMMAND
84 
85 /* Mutex used in the shared code */
86 #define E1000_MUTEX                     struct mtx
87 #define E1000_MUTEX_INIT(mutex)         mtx_init((mutex), #mutex, \
88                                             MTX_NETWORK_LOCK, \
89 					    MTX_DEF | MTX_DUPOK)
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)
94 
95 typedef uint64_t	u64;
96 typedef uint32_t	u32;
97 typedef uint16_t	u16;
98 typedef uint8_t		u8;
99 typedef int64_t		s64;
100 typedef int32_t		s32;
101 typedef int16_t		s16;
102 typedef int8_t		s8;
103 
104 #define __le16		u16
105 #define __le32		u32
106 #define __le64		u64
107 
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")
113 #else
114 #define mb()
115 #define rmb()
116 #define wmb()
117 #endif
118 #endif /*__FreeBSD_version < 800000 */
119 
120 #if defined(__i386__) || defined(__amd64__)
121 static __inline
122 void prefetch(void *x)
123 {
124 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
125 }
126 #else
127 #define prefetch(x)
128 #endif
129 
130 struct e1000_osdep
131 {
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;
138 	device_t	   dev;
139 };
140 
141 #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
142     ? reg : e1000_translate_register_82542(reg))
143 
144 #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
145 
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)
150 
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)
155 
156 /* Register READ/WRITE macros */
157 
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))
162 
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)
167 
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))
172 
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)
177 
178 #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
179 #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
180 
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)
185 
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)
190 
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)
195 
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)
203 
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)
207 
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)
211 
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)
215 
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)
219 
220 #endif  /* _FREEBSD_OS_H_ */
221 
222