1ab64e1faSKarsten Heimrich/******************************************************************************
267a0cb89SAugustin Cavalier  SPDX-License-Identifier: BSD-3-Clause
3ab64e1faSKarsten Heimrich
467a0cb89SAugustin Cavalier  Copyright (c) 2001-2015, Intel Corporation
5ab64e1faSKarsten Heimrich  All rights reserved.
667a0cb89SAugustin Cavalier
767a0cb89SAugustin Cavalier  Redistribution and use in source and binary forms, with or without
8ab64e1faSKarsten Heimrich  modification, are permitted provided that the following conditions are met:
967a0cb89SAugustin Cavalier
1067a0cb89SAugustin Cavalier   1. Redistributions of source code must retain the above copyright notice,
11ab64e1faSKarsten Heimrich      this list of conditions and the following disclaimer.
1267a0cb89SAugustin Cavalier
1367a0cb89SAugustin Cavalier   2. Redistributions in binary form must reproduce the above copyright
1467a0cb89SAugustin Cavalier      notice, this list of conditions and the following disclaimer in the
15ab64e1faSKarsten Heimrich      documentation and/or other materials provided with the distribution.
1667a0cb89SAugustin Cavalier
1767a0cb89SAugustin Cavalier   3. Neither the name of the Intel Corporation nor the names of its
1867a0cb89SAugustin Cavalier      contributors may be used to endorse or promote products derived from
19ab64e1faSKarsten Heimrich      this software without specific prior written permission.
2067a0cb89SAugustin Cavalier
21ab64e1faSKarsten Heimrich  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2267a0cb89SAugustin Cavalier  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2367a0cb89SAugustin Cavalier  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2467a0cb89SAugustin Cavalier  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
2567a0cb89SAugustin Cavalier  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2667a0cb89SAugustin Cavalier  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2767a0cb89SAugustin Cavalier  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2867a0cb89SAugustin Cavalier  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2967a0cb89SAugustin Cavalier  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ab64e1faSKarsten Heimrich  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31ab64e1faSKarsten Heimrich  POSSIBILITY OF SUCH DAMAGE.
32ab64e1faSKarsten Heimrich
33ab64e1faSKarsten Heimrich******************************************************************************/
3467a0cb89SAugustin Cavalier/*$FreeBSD: releng/12.0/sys/dev/e1000/e1000_osdep.h 333345 2018-05-08 01:39:45Z mmacy $*/
35ab64e1faSKarsten Heimrich
36ab64e1faSKarsten Heimrich
37ab64e1faSKarsten Heimrich#ifndef _FREEBSD_OS_H_
38ab64e1faSKarsten Heimrich#define _FREEBSD_OS_H_
39ab64e1faSKarsten Heimrich
40ab64e1faSKarsten Heimrich#include <sys/types.h>
41ab64e1faSKarsten Heimrich#include <sys/param.h>
42ab64e1faSKarsten Heimrich#include <sys/systm.h>
4367a0cb89SAugustin Cavalier#include <sys/proc.h>
44d1e17090SSiarzhuk Zharski#include <sys/lock.h>
45d1e17090SSiarzhuk Zharski#include <sys/mutex.h>
46ab64e1faSKarsten Heimrich#include <sys/mbuf.h>
47ab64e1faSKarsten Heimrich#include <sys/protosw.h>
48ab64e1faSKarsten Heimrich#include <sys/socket.h>
49ab64e1faSKarsten Heimrich#include <sys/malloc.h>
50ab64e1faSKarsten Heimrich#include <sys/kernel.h>
51ab64e1faSKarsten Heimrich#include <sys/bus.h>
5267a0cb89SAugustin Cavalier#include <sys/sx.h>
5367a0cb89SAugustin Cavalier
5467a0cb89SAugustin Cavalier#include <net/ethernet.h>
5567a0cb89SAugustin Cavalier#include <net/if.h>
5667a0cb89SAugustin Cavalier#include <net/if_var.h>
5767a0cb89SAugustin Cavalier#include <net/iflib.h>
5867a0cb89SAugustin Cavalier
5967a0cb89SAugustin Cavalier
6067a0cb89SAugustin Cavalier
61ab64e1faSKarsten Heimrich#include <machine/bus.h>
62ab64e1faSKarsten Heimrich#include <sys/rman.h>
63ab64e1faSKarsten Heimrich#include <machine/resource.h>
64ab64e1faSKarsten Heimrich#include <vm/vm.h>
65ab64e1faSKarsten Heimrich#include <vm/pmap.h>
66ab64e1faSKarsten Heimrich#include <machine/clock.h>
67ab64e1faSKarsten Heimrich#include <dev/pci/pcivar.h>
68ab64e1faSKarsten Heimrich#include <dev/pci/pcireg.h>
69ab64e1faSKarsten Heimrich
70ab64e1faSKarsten Heimrich
71ab64e1faSKarsten Heimrich#define ASSERT(x) if(!(x)) panic("EM: x")
7267a0cb89SAugustin Cavalier#define us_scale(x)  max(1, (x/(1000000/hz)))
7367a0cb89SAugustin Cavalierstatic inline int
7467a0cb89SAugustin Cavalierms_scale(int x) {
7567a0cb89SAugustin Cavalier	if (hz == 1000) {
7667a0cb89SAugustin Cavalier		return (x);
7767a0cb89SAugustin Cavalier	} else if (hz > 1000) {
7867a0cb89SAugustin Cavalier		return (x*(hz/1000));
7967a0cb89SAugustin Cavalier	} else {
8067a0cb89SAugustin Cavalier		return (max(1, x/(1000/hz)));
8167a0cb89SAugustin Cavalier	}
8267a0cb89SAugustin Cavalier}
8367a0cb89SAugustin Cavalier
8467a0cb89SAugustin Cavalierstatic inline void
8567a0cb89SAugustin Cavaliersafe_pause_us(int x) {
86a381a48fSAugustin Cavalier#ifndef __HAIKU__
8767a0cb89SAugustin Cavalier	if (cold) {
8867a0cb89SAugustin Cavalier		DELAY(x);
8967a0cb89SAugustin Cavalier	} else {
9067a0cb89SAugustin Cavalier		pause("e1000_delay", max(1,  x/(1000000/hz)));
9167a0cb89SAugustin Cavalier	}
92a381a48fSAugustin Cavalier#else
93a381a48fSAugustin Cavalier	DELAY(x);
94a381a48fSAugustin Cavalier#endif
9567a0cb89SAugustin Cavalier}
96ab64e1faSKarsten Heimrich
9767a0cb89SAugustin Cavalierstatic inline void
9867a0cb89SAugustin Cavaliersafe_pause_ms(int x) {
9967a0cb89SAugustin Cavalier	if (cold) {
10067a0cb89SAugustin Cavalier		DELAY(x*1000);
10167a0cb89SAugustin Cavalier	} else {
10267a0cb89SAugustin Cavalier		pause("e1000_delay", ms_scale(x));
10367a0cb89SAugustin Cavalier	}
10467a0cb89SAugustin Cavalier}
10567a0cb89SAugustin Cavalier
10667a0cb89SAugustin Cavalier#define usec_delay(x) safe_pause_us(x)
107b3fb200fSJérôme Duval#define usec_delay_irq(x) usec_delay(x)
10867a0cb89SAugustin Cavalier#define msec_delay(x) safe_pause_ms(x)
10967a0cb89SAugustin Cavalier#define msec_delay_irq(x) msec_delay(x)
110ab64e1faSKarsten Heimrich
111b3fb200fSJérôme Duval/* Enable/disable debugging statements in shared code */
112b3fb200fSJérôme Duval#define DBG		0
113b3fb200fSJérôme Duval
114b3fb200fSJérôme Duval#define DEBUGOUT(...) \
115b3fb200fSJérôme Duval    do { if (DBG) printf(__VA_ARGS__); } while (0)
116b3fb200fSJérôme Duval#define DEBUGOUT1(...)			DEBUGOUT(__VA_ARGS__)
117b3fb200fSJérôme Duval#define DEBUGOUT2(...)			DEBUGOUT(__VA_ARGS__)
118b3fb200fSJérôme Duval#define DEBUGOUT3(...)			DEBUGOUT(__VA_ARGS__)
119b3fb200fSJérôme Duval#define DEBUGOUT7(...)			DEBUGOUT(__VA_ARGS__)
120b3fb200fSJérôme Duval#define DEBUGFUNC(F)			DEBUGOUT(F "\n")
121ab64e1faSKarsten Heimrich
122ab64e1faSKarsten Heimrich#define STATIC			static
123ab64e1faSKarsten Heimrich#define FALSE			0
124ab64e1faSKarsten Heimrich#define TRUE			1
125ab64e1faSKarsten Heimrich#define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
126ab64e1faSKarsten Heimrich#define PCI_COMMAND_REGISTER	PCIR_COMMAND
127ab64e1faSKarsten Heimrich
128ab64e1faSKarsten Heimrichtypedef uint64_t	u64;
129ab64e1faSKarsten Heimrichtypedef uint32_t	u32;
130ab64e1faSKarsten Heimrichtypedef uint16_t	u16;
131ab64e1faSKarsten Heimrichtypedef uint8_t		u8;
132ab64e1faSKarsten Heimrichtypedef int64_t		s64;
133ab64e1faSKarsten Heimrichtypedef int32_t		s32;
134ab64e1faSKarsten Heimrichtypedef int16_t		s16;
135ab64e1faSKarsten Heimrichtypedef int8_t		s8;
136ab64e1faSKarsten Heimrich
137ab64e1faSKarsten Heimrich#define __le16		u16
138ab64e1faSKarsten Heimrich#define __le32		u32
139ab64e1faSKarsten Heimrich#define __le64		u64
140ab64e1faSKarsten Heimrich
141b3fb200fSJérôme Duval#if __FreeBSD_version < 800000
142d1e17090SSiarzhuk Zharski#if defined(__i386__) || defined(__amd64__)
143d1e17090SSiarzhuk Zharski#define mb()	__asm volatile("mfence" ::: "memory")
144d1e17090SSiarzhuk Zharski#define wmb()	__asm volatile("sfence" ::: "memory")
145d1e17090SSiarzhuk Zharski#define rmb()	__asm volatile("lfence" ::: "memory")
146d1e17090SSiarzhuk Zharski#else
147d1e17090SSiarzhuk Zharski#define mb()
148d1e17090SSiarzhuk Zharski#define rmb()
149d1e17090SSiarzhuk Zharski#define wmb()
150d1e17090SSiarzhuk Zharski#endif
151d1e17090SSiarzhuk Zharski#endif /*__FreeBSD_version < 800000 */
152d1e17090SSiarzhuk Zharski
15367a0cb89SAugustin Cavalier#ifdef INVARIANTS
15467a0cb89SAugustin Cavalier#define ASSERT_CTX_LOCK_HELD(hw) sx_assert(iflib_ctx_lock_get(((struct e1000_osdep *)hw->back)->ctx), SX_XLOCKED)
15567a0cb89SAugustin Cavalier#else
15667a0cb89SAugustin Cavalier#define ASSERT_CTX_LOCK_HELD(hw)
15767a0cb89SAugustin Cavalier#endif
15867a0cb89SAugustin Cavalier
159d1e17090SSiarzhuk Zharski#if defined(__i386__) || defined(__amd64__)
160d1e17090SSiarzhuk Zharskistatic __inline
161d1e17090SSiarzhuk Zharskivoid prefetch(void *x)
162d1e17090SSiarzhuk Zharski{
163d1e17090SSiarzhuk Zharski	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
164d1e17090SSiarzhuk Zharski}
165d1e17090SSiarzhuk Zharski#else
166d1e17090SSiarzhuk Zharski#define prefetch(x)
167d1e17090SSiarzhuk Zharski#endif
168d1e17090SSiarzhuk Zharski
169ab64e1faSKarsten Heimrichstruct e1000_osdep
170ab64e1faSKarsten Heimrich{
171ab64e1faSKarsten Heimrich	bus_space_tag_t    mem_bus_space_tag;
172ab64e1faSKarsten Heimrich	bus_space_handle_t mem_bus_space_handle;
173ab64e1faSKarsten Heimrich	bus_space_tag_t    io_bus_space_tag;
174ab64e1faSKarsten Heimrich	bus_space_handle_t io_bus_space_handle;
175ab64e1faSKarsten Heimrich	bus_space_tag_t    flash_bus_space_tag;
176ab64e1faSKarsten Heimrich	bus_space_handle_t flash_bus_space_handle;
17767a0cb89SAugustin Cavalier	device_t	   dev;
17867a0cb89SAugustin Cavalier	if_ctx_t	   ctx;
179ab64e1faSKarsten Heimrich};
180ab64e1faSKarsten Heimrich
181ab64e1faSKarsten Heimrich#define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
182ab64e1faSKarsten Heimrich    ? reg : e1000_translate_register_82542(reg))
183ab64e1faSKarsten Heimrich
184ab64e1faSKarsten Heimrich#define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
185ab64e1faSKarsten Heimrich
186ab64e1faSKarsten Heimrich/* Read from an absolute offset in the adapter's memory space */
187ab64e1faSKarsten Heimrich#define E1000_READ_OFFSET(hw, offset) \
188ab64e1faSKarsten Heimrich    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
189ab64e1faSKarsten Heimrich    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
190ab64e1faSKarsten Heimrich
191ab64e1faSKarsten Heimrich/* Write to an absolute offset in the adapter's memory space */
192ab64e1faSKarsten Heimrich#define E1000_WRITE_OFFSET(hw, offset, value) \
193ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
194ab64e1faSKarsten Heimrich    ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
195ab64e1faSKarsten Heimrich
196ab64e1faSKarsten Heimrich/* Register READ/WRITE macros */
197ab64e1faSKarsten Heimrich
198ab64e1faSKarsten Heimrich#define E1000_READ_REG(hw, reg) \
199ab64e1faSKarsten Heimrich    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
200ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
201ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg))
202ab64e1faSKarsten Heimrich
203ab64e1faSKarsten Heimrich#define E1000_WRITE_REG(hw, reg, value) \
204ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
205ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
206ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg), value)
207ab64e1faSKarsten Heimrich
208ab64e1faSKarsten Heimrich#define E1000_READ_REG_ARRAY(hw, reg, index) \
209ab64e1faSKarsten Heimrich    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
210ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
211ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg) + ((index)<< 2))
212ab64e1faSKarsten Heimrich
213ab64e1faSKarsten Heimrich#define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
214ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
215ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
216ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg) + ((index)<< 2), value)
217ab64e1faSKarsten Heimrich
218ab64e1faSKarsten Heimrich#define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
219ab64e1faSKarsten Heimrich#define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
220ab64e1faSKarsten Heimrich
221ab64e1faSKarsten Heimrich#define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
222ab64e1faSKarsten Heimrich    bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
223ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
224ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg) + index)
225ab64e1faSKarsten Heimrich
226ab64e1faSKarsten Heimrich#define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
227ab64e1faSKarsten Heimrich    bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
228ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
229ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg) + index, value)
230ab64e1faSKarsten Heimrich
231ab64e1faSKarsten Heimrich#define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
232ab64e1faSKarsten Heimrich    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
233ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
234ab64e1faSKarsten Heimrich        E1000_REGISTER(hw, reg) + (index << 1), value)
235ab64e1faSKarsten Heimrich
236ab64e1faSKarsten Heimrich#define E1000_WRITE_REG_IO(hw, reg, value) do {\
237ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
238ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
239ab64e1faSKarsten Heimrich        (hw)->io_base, reg); \
240ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
241ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
242ab64e1faSKarsten Heimrich        (hw)->io_base + 4, value); } while (0)
243ab64e1faSKarsten Heimrich
244ab64e1faSKarsten Heimrich#define E1000_READ_FLASH_REG(hw, reg) \
245ab64e1faSKarsten Heimrich    bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
246ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
247ab64e1faSKarsten Heimrich
248ab64e1faSKarsten Heimrich#define E1000_READ_FLASH_REG16(hw, reg) \
249ab64e1faSKarsten Heimrich    bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
250ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
251ab64e1faSKarsten Heimrich
252ab64e1faSKarsten Heimrich#define E1000_WRITE_FLASH_REG(hw, reg, value) \
253ab64e1faSKarsten Heimrich    bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
254ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
255ab64e1faSKarsten Heimrich
256ab64e1faSKarsten Heimrich#define E1000_WRITE_FLASH_REG16(hw, reg, value) \
257ab64e1faSKarsten Heimrich    bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
258ab64e1faSKarsten Heimrich        ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
259ab64e1faSKarsten Heimrich
26067a0cb89SAugustin Cavalier
26167a0cb89SAugustin Cavalier#if 0
26267a0cb89SAugustin Cavalier#include <sys/proc.h>
26367a0cb89SAugustin Cavalier
26467a0cb89SAugustin Cavalier#define ASSERT_NO_LOCKS()				\
26567a0cb89SAugustin Cavalier	do {						\
26667a0cb89SAugustin Cavalier	     int unknown_locks = curthread->td_locks - mtx_owned(&Giant);	\
26767a0cb89SAugustin Cavalier	     if (unknown_locks > 0) {					\
26867a0cb89SAugustin Cavalier		     WITNESS_WARN(WARN_GIANTOK|WARN_SLEEPOK|WARN_PANIC, NULL, "unexpected non-sleepable lock"); \
26967a0cb89SAugustin Cavalier	     }								\
27067a0cb89SAugustin Cavalier	     MPASS(curthread->td_rw_rlocks == 0);			\
27167a0cb89SAugustin Cavalier	     MPASS(curthread->td_lk_slocks == 0);			\
27267a0cb89SAugustin Cavalier	} while (0)
27367a0cb89SAugustin Cavalier#else
27467a0cb89SAugustin Cavalier#define ASSERT_NO_LOCKS()
27567a0cb89SAugustin Cavalier#endif
27667a0cb89SAugustin Cavalier
277ab64e1faSKarsten Heimrich#endif  /* _FREEBSD_OS_H_ */
278ab64e1faSKarsten Heimrich
279