xref: /freebsd/sys/dev/e1000/e1000_osdep.h (revision acc1a9ef)
1 /******************************************************************************
2 
3   Copyright (c) 2001-2015, Intel Corporation
4   All rights reserved.
5 
6   Redistribution and use in source and binary forms, with or without
7   modification, are permitted provided that the following conditions are met:
8 
9    1. Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11 
12    2. Redistributions in binary form must reproduce the above copyright
13       notice, this list of conditions and the following disclaimer in the
14       documentation and/or other materials provided with the distribution.
15 
16    3. Neither the name of the Intel Corporation nor the names of its
17       contributors may be used to endorse or promote products derived from
18       this software without specific prior written permission.
19 
20   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   POSSIBILITY OF SUCH DAMAGE.
31 
32 ******************************************************************************/
33 /*$FreeBSD$*/
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/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/socket.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/bus.h>
50 #include <machine/bus.h>
51 #include <sys/rman.h>
52 #include <machine/resource.h>
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <machine/clock.h>
56 #include <dev/pci/pcivar.h>
57 #include <dev/pci/pcireg.h>
58 
59 
60 #define ASSERT(x) if(!(x)) panic("EM: x")
61 
62 #define usec_delay(x) DELAY(x)
63 #define usec_delay_irq(x) usec_delay(x)
64 #define msec_delay(x) DELAY(1000*(x))
65 #define msec_delay_irq(x) DELAY(1000*(x))
66 
67 /* Enable/disable debugging statements in shared code */
68 #define DBG		0
69 
70 #define DEBUGOUT(...) \
71     do { if (DBG) printf(__VA_ARGS__); } while (0)
72 #define DEBUGOUT1(...)			DEBUGOUT(__VA_ARGS__)
73 #define DEBUGOUT2(...)			DEBUGOUT(__VA_ARGS__)
74 #define DEBUGOUT3(...)			DEBUGOUT(__VA_ARGS__)
75 #define DEBUGOUT7(...)			DEBUGOUT(__VA_ARGS__)
76 #define DEBUGFUNC(F)			DEBUGOUT(F "\n")
77 
78 #define STATIC			static
79 #define FALSE			0
80 #define TRUE			1
81 #define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
82 #define PCI_COMMAND_REGISTER	PCIR_COMMAND
83 
84 /* Mutex used in the shared code */
85 #define E1000_MUTEX                     struct mtx
86 #define E1000_MUTEX_INIT(mutex)         mtx_init((mutex), #mutex, \
87                                             MTX_NETWORK_LOCK, \
88 					    MTX_DEF | MTX_DUPOK)
89 #define E1000_MUTEX_DESTROY(mutex)      mtx_destroy(mutex)
90 #define E1000_MUTEX_LOCK(mutex)         mtx_lock(mutex)
91 #define E1000_MUTEX_TRYLOCK(mutex)      mtx_trylock(mutex)
92 #define E1000_MUTEX_UNLOCK(mutex)       mtx_unlock(mutex)
93 
94 typedef uint64_t	u64;
95 typedef uint32_t	u32;
96 typedef uint16_t	u16;
97 typedef uint8_t		u8;
98 typedef int64_t		s64;
99 typedef int32_t		s32;
100 typedef int16_t		s16;
101 typedef int8_t		s8;
102 
103 #define __le16		u16
104 #define __le32		u32
105 #define __le64		u64
106 
107 #if __FreeBSD_version < 800000
108 #if defined(__i386__) || defined(__amd64__)
109 #define mb()	__asm volatile("mfence" ::: "memory")
110 #define wmb()	__asm volatile("sfence" ::: "memory")
111 #define rmb()	__asm volatile("lfence" ::: "memory")
112 #else
113 #define mb()
114 #define rmb()
115 #define wmb()
116 #endif
117 #endif /*__FreeBSD_version < 800000 */
118 
119 #if defined(__i386__) || defined(__amd64__)
120 static __inline
121 void prefetch(void *x)
122 {
123 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
124 }
125 #else
126 #define prefetch(x)
127 #endif
128 
129 struct e1000_osdep
130 {
131 	bus_space_tag_t    mem_bus_space_tag;
132 	bus_space_handle_t mem_bus_space_handle;
133 	bus_space_tag_t    io_bus_space_tag;
134 	bus_space_handle_t io_bus_space_handle;
135 	bus_space_tag_t    flash_bus_space_tag;
136 	bus_space_handle_t flash_bus_space_handle;
137 	struct device     *dev;
138 };
139 
140 #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
141     ? reg : e1000_translate_register_82542(reg))
142 
143 #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
144 
145 /* Read from an absolute offset in the adapter's memory space */
146 #define E1000_READ_OFFSET(hw, offset) \
147     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
148     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
149 
150 /* Write to an absolute offset in the adapter's memory space */
151 #define E1000_WRITE_OFFSET(hw, offset, value) \
152     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
153     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
154 
155 /* Register READ/WRITE macros */
156 
157 #define E1000_READ_REG(hw, reg) \
158     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
159         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
160         E1000_REGISTER(hw, reg))
161 
162 #define E1000_WRITE_REG(hw, reg, value) \
163     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
164         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
165         E1000_REGISTER(hw, reg), value)
166 
167 #define E1000_READ_REG_ARRAY(hw, reg, index) \
168     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
169         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
170         E1000_REGISTER(hw, reg) + ((index)<< 2))
171 
172 #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
173     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
174         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
175         E1000_REGISTER(hw, reg) + ((index)<< 2), value)
176 
177 #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
178 #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
179 
180 #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
181     bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
182         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
183         E1000_REGISTER(hw, reg) + index)
184 
185 #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
186     bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
187         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
188         E1000_REGISTER(hw, reg) + index, value)
189 
190 #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
191     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
192         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
193         E1000_REGISTER(hw, reg) + (index << 1), value)
194 
195 #define E1000_WRITE_REG_IO(hw, reg, value) do {\
196     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
197         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
198         (hw)->io_base, reg); \
199     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
200         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
201         (hw)->io_base + 4, value); } while (0)
202 
203 #define E1000_READ_FLASH_REG(hw, reg) \
204     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
205         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
206 
207 #define E1000_READ_FLASH_REG16(hw, reg) \
208     bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
209         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
210 
211 #define E1000_WRITE_FLASH_REG(hw, reg, value) \
212     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
213         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
214 
215 #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
216     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
217         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
218 
219 #endif  /* _FREEBSD_OS_H_ */
220 
221