xref: /freebsd/sys/dev/e1000/e1000_osdep.h (revision 930a1e6f3d2dd629774f1b48b1acf7ba482ab659)
18cfa0ad2SJack F Vogel /******************************************************************************
27282444bSPedro F. Giffuni   SPDX-License-Identifier: BSD-3-Clause
38cfa0ad2SJack F Vogel 
4702cac6cSKevin Bowling   Copyright (c) 2001-2020, Intel Corporation
58cfa0ad2SJack F Vogel   All rights reserved.
68cfa0ad2SJack F Vogel 
78cfa0ad2SJack F Vogel   Redistribution and use in source and binary forms, with or without
88cfa0ad2SJack F Vogel   modification, are permitted provided that the following conditions are met:
98cfa0ad2SJack F Vogel 
108cfa0ad2SJack F Vogel    1. Redistributions of source code must retain the above copyright notice,
118cfa0ad2SJack F Vogel       this list of conditions and the following disclaimer.
128cfa0ad2SJack F Vogel 
138cfa0ad2SJack F Vogel    2. Redistributions in binary form must reproduce the above copyright
148cfa0ad2SJack F Vogel       notice, this list of conditions and the following disclaimer in the
158cfa0ad2SJack F Vogel       documentation and/or other materials provided with the distribution.
168cfa0ad2SJack F Vogel 
178cfa0ad2SJack F Vogel    3. Neither the name of the Intel Corporation nor the names of its
188cfa0ad2SJack F Vogel       contributors may be used to endorse or promote products derived from
198cfa0ad2SJack F Vogel       this software without specific prior written permission.
208cfa0ad2SJack F Vogel 
218cfa0ad2SJack F Vogel   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
228cfa0ad2SJack F Vogel   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
238cfa0ad2SJack F Vogel   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
248cfa0ad2SJack F Vogel   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
258cfa0ad2SJack F Vogel   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
268cfa0ad2SJack F Vogel   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
278cfa0ad2SJack F Vogel   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
288cfa0ad2SJack F Vogel   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
298cfa0ad2SJack F Vogel   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
308cfa0ad2SJack F Vogel   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
318cfa0ad2SJack F Vogel   POSSIBILITY OF SUCH DAMAGE.
328cfa0ad2SJack F Vogel 
338cfa0ad2SJack F Vogel ******************************************************************************/
348cfa0ad2SJack F Vogel 
358cfa0ad2SJack F Vogel 
368cfa0ad2SJack F Vogel #ifndef _FREEBSD_OS_H_
378cfa0ad2SJack F Vogel #define _FREEBSD_OS_H_
388cfa0ad2SJack F Vogel 
398cfa0ad2SJack F Vogel #include <sys/types.h>
408cfa0ad2SJack F Vogel #include <sys/param.h>
418cfa0ad2SJack F Vogel #include <sys/systm.h>
42d5210708SMatt Macy #include <sys/proc.h>
43afb98829SJack F Vogel #include <sys/lock.h>
44afb98829SJack F Vogel #include <sys/mutex.h>
458cfa0ad2SJack F Vogel #include <sys/mbuf.h>
468cfa0ad2SJack F Vogel #include <sys/protosw.h>
478cfa0ad2SJack F Vogel #include <sys/socket.h>
488cfa0ad2SJack F Vogel #include <sys/malloc.h>
498cfa0ad2SJack F Vogel #include <sys/kernel.h>
508cfa0ad2SJack F Vogel #include <sys/bus.h>
51d5210708SMatt Macy 
52d5210708SMatt Macy #include <net/ethernet.h>
53d5210708SMatt Macy #include <net/if.h>
54d5210708SMatt Macy #include <net/if_var.h>
55d5210708SMatt Macy #include <net/iflib.h>
56d5210708SMatt Macy 
57d5210708SMatt Macy 
58d5210708SMatt Macy 
598cfa0ad2SJack F Vogel #include <machine/bus.h>
608cfa0ad2SJack F Vogel #include <sys/rman.h>
618cfa0ad2SJack F Vogel #include <machine/resource.h>
628cfa0ad2SJack F Vogel #include <vm/vm.h>
638cfa0ad2SJack F Vogel #include <vm/pmap.h>
648cfa0ad2SJack F Vogel #include <machine/clock.h>
658cfa0ad2SJack F Vogel #include <dev/pci/pcivar.h>
668cfa0ad2SJack F Vogel #include <dev/pci/pcireg.h>
678cfa0ad2SJack F Vogel 
688cfa0ad2SJack F Vogel 
698cfa0ad2SJack F Vogel #define ASSERT(x) if(!(x)) panic("EM: x")
70d5210708SMatt Macy #define us_scale(x)  max(1, (x/(1000000/hz)))
71d5210708SMatt Macy static inline int
ms_scale(int x)72d5210708SMatt Macy ms_scale(int x) {
73d5210708SMatt Macy 	if (hz == 1000) {
74d5210708SMatt Macy 		return (x);
75d5210708SMatt Macy 	} else if (hz > 1000) {
76d5210708SMatt Macy 		return (x*(hz/1000));
77d5210708SMatt Macy 	} else {
78d5210708SMatt Macy 		return (max(1, x/(1000/hz)));
79d5210708SMatt Macy 	}
80d5210708SMatt Macy }
818cfa0ad2SJack F Vogel 
82*930a1e6fSJoyu Liao extern int e1000_use_pause_delay;
83*930a1e6fSJoyu Liao 
84d5210708SMatt Macy static inline void
safe_pause_us(int x)85d5210708SMatt Macy safe_pause_us(int x) {
86*930a1e6fSJoyu Liao 	if (!e1000_use_pause_delay) {
87d5210708SMatt Macy 		DELAY(x);
88d5210708SMatt Macy 	} else {
89d5210708SMatt Macy 		pause("e1000_delay", max(1,  x/(1000000/hz)));
90d5210708SMatt Macy 	}
91d5210708SMatt Macy }
92d5210708SMatt Macy 
93d5210708SMatt Macy static inline void
safe_pause_ms(int x)94d5210708SMatt Macy safe_pause_ms(int x) {
95*930a1e6fSJoyu Liao 	if (!e1000_use_pause_delay) {
96d5210708SMatt Macy 		DELAY(x*1000);
97d5210708SMatt Macy 	} else {
98d5210708SMatt Macy 		pause("e1000_delay", ms_scale(x));
99d5210708SMatt Macy 	}
100d5210708SMatt Macy }
101d5210708SMatt Macy 
102d5210708SMatt Macy #define usec_delay(x) safe_pause_us(x)
103c80429ceSEric Joyner #define usec_delay_irq(x) usec_delay(x)
104d5210708SMatt Macy #define msec_delay(x) safe_pause_ms(x)
105d5210708SMatt Macy #define msec_delay_irq(x) msec_delay(x)
1068cfa0ad2SJack F Vogel 
107c80429ceSEric Joyner /* Enable/disable debugging statements in shared code */
108c80429ceSEric Joyner #define DBG		0
109c80429ceSEric Joyner 
110c80429ceSEric Joyner #define DEBUGOUT(...) \
111c80429ceSEric Joyner     do { if (DBG) printf(__VA_ARGS__); } while (0)
112c80429ceSEric Joyner #define DEBUGOUT1(...)			DEBUGOUT(__VA_ARGS__)
113c80429ceSEric Joyner #define DEBUGOUT2(...)			DEBUGOUT(__VA_ARGS__)
114c80429ceSEric Joyner #define DEBUGOUT3(...)			DEBUGOUT(__VA_ARGS__)
115c80429ceSEric Joyner #define DEBUGOUT7(...)			DEBUGOUT(__VA_ARGS__)
116c80429ceSEric Joyner #define DEBUGFUNC(F)			DEBUGOUT(F "\n")
1178cfa0ad2SJack F Vogel 
1188cfa0ad2SJack F Vogel #define STATIC			static
1198cfa0ad2SJack F Vogel #define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
1208cfa0ad2SJack F Vogel #define PCI_COMMAND_REGISTER	PCIR_COMMAND
1218cfa0ad2SJack F Vogel 
1228cfa0ad2SJack F Vogel typedef uint64_t	u64;
1238cfa0ad2SJack F Vogel typedef uint32_t	u32;
1248cfa0ad2SJack F Vogel typedef uint16_t	u16;
1258cfa0ad2SJack F Vogel typedef uint8_t		u8;
1268cfa0ad2SJack F Vogel typedef int64_t		s64;
1278cfa0ad2SJack F Vogel typedef int32_t		s32;
1288cfa0ad2SJack F Vogel typedef int16_t		s16;
1298cfa0ad2SJack F Vogel typedef int8_t		s8;
1308cfa0ad2SJack F Vogel 
131daf9197cSJack F Vogel #define __le16		u16
132daf9197cSJack F Vogel #define __le32		u32
133daf9197cSJack F Vogel #define __le64		u64
134daf9197cSJack F Vogel 
135d5210708SMatt Macy #ifdef INVARIANTS
136d5210708SMatt Macy #define ASSERT_CTX_LOCK_HELD(hw) (sx_assert(iflib_ctx_lock_get(((struct e1000_osdep *)hw->back)->ctx), SX_XLOCKED))
137d5210708SMatt Macy #else
138d5210708SMatt Macy #define ASSERT_CTX_LOCK_HELD(hw)
139d5210708SMatt Macy #endif
140d5210708SMatt Macy 
1414edd8523SJack F Vogel #if defined(__i386__) || defined(__amd64__)
1424edd8523SJack F Vogel static __inline
prefetch(void * x)1434edd8523SJack F Vogel void prefetch(void *x)
1444edd8523SJack F Vogel {
1454edd8523SJack F Vogel 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1464edd8523SJack F Vogel }
1474edd8523SJack F Vogel #else
1484edd8523SJack F Vogel #define prefetch(x)
1494edd8523SJack F Vogel #endif
1504edd8523SJack F Vogel 
1518cfa0ad2SJack F Vogel struct e1000_osdep
1528cfa0ad2SJack F Vogel {
1538cfa0ad2SJack F Vogel 	bus_space_tag_t    mem_bus_space_tag;
1548cfa0ad2SJack F Vogel 	bus_space_handle_t mem_bus_space_handle;
1558cfa0ad2SJack F Vogel 	bus_space_tag_t    io_bus_space_tag;
1568cfa0ad2SJack F Vogel 	bus_space_handle_t io_bus_space_handle;
1578cfa0ad2SJack F Vogel 	bus_space_tag_t    flash_bus_space_tag;
1588cfa0ad2SJack F Vogel 	bus_space_handle_t flash_bus_space_handle;
159bd937497SJean-Sébastien Pédron 	device_t	   dev;
160d5210708SMatt Macy 	if_ctx_t	   ctx;
1618cfa0ad2SJack F Vogel };
1628cfa0ad2SJack F Vogel 
1638cfa0ad2SJack F Vogel #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
1648cfa0ad2SJack F Vogel     ? reg : e1000_translate_register_82542(reg))
1658cfa0ad2SJack F Vogel 
1668cfa0ad2SJack F Vogel #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
1678cfa0ad2SJack F Vogel 
1688cfa0ad2SJack F Vogel /* Read from an absolute offset in the adapter's memory space */
1698cfa0ad2SJack F Vogel #define E1000_READ_OFFSET(hw, offset) \
1708cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1718cfa0ad2SJack F Vogel     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
1728cfa0ad2SJack F Vogel 
1738cfa0ad2SJack F Vogel /* Write to an absolute offset in the adapter's memory space */
1748cfa0ad2SJack F Vogel #define E1000_WRITE_OFFSET(hw, offset, value) \
1758cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1768cfa0ad2SJack F Vogel     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
1778cfa0ad2SJack F Vogel 
1788cfa0ad2SJack F Vogel /* Register READ/WRITE macros */
1798cfa0ad2SJack F Vogel 
1808cfa0ad2SJack F Vogel #define E1000_READ_REG(hw, reg) \
1818cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1828cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1838cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg))
1848cfa0ad2SJack F Vogel 
1858cfa0ad2SJack F Vogel #define E1000_WRITE_REG(hw, reg, value) \
1868cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1878cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1888cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg), value)
1898cfa0ad2SJack F Vogel 
1908cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY(hw, reg, index) \
1918cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1928cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1938cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + ((index)<< 2))
1948cfa0ad2SJack F Vogel 
1958cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
1968cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1978cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1988cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + ((index)<< 2), value)
1998cfa0ad2SJack F Vogel 
2008cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
2018cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
2028cfa0ad2SJack F Vogel 
2038cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
2048cfa0ad2SJack F Vogel     bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
2058cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
2068cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + index)
2078cfa0ad2SJack F Vogel 
2088cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
2098cfa0ad2SJack F Vogel     bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
2108cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
2118cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + index, value)
2128cfa0ad2SJack F Vogel 
2138cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
2148cfa0ad2SJack F Vogel     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
2158cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
2168cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + (index << 1), value)
2178cfa0ad2SJack F Vogel 
2188cfa0ad2SJack F Vogel #define E1000_WRITE_REG_IO(hw, reg, value) do {\
2198cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
2208cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
2218cfa0ad2SJack F Vogel         (hw)->io_base, reg); \
2228cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
2238cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
2248cfa0ad2SJack F Vogel         (hw)->io_base + 4, value); } while (0)
2258cfa0ad2SJack F Vogel 
2268cfa0ad2SJack F Vogel #define E1000_READ_FLASH_REG(hw, reg) \
2278cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2288cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
2298cfa0ad2SJack F Vogel 
2308cfa0ad2SJack F Vogel #define E1000_READ_FLASH_REG16(hw, reg) \
2318cfa0ad2SJack F Vogel     bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2328cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
2338cfa0ad2SJack F Vogel 
2348cfa0ad2SJack F Vogel #define E1000_WRITE_FLASH_REG(hw, reg, value) \
2358cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2368cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
2378cfa0ad2SJack F Vogel 
2388cfa0ad2SJack F Vogel #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
2398cfa0ad2SJack F Vogel     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2408cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
2418cfa0ad2SJack F Vogel 
242d5210708SMatt Macy 
243d5210708SMatt Macy #if defined(INVARIANTS)
244d5210708SMatt Macy #include <sys/proc.h>
245d5210708SMatt Macy 
246d5210708SMatt Macy #define ASSERT_NO_LOCKS()				\
247d5210708SMatt Macy 	do {						\
248d5210708SMatt Macy 	     int unknown_locks = curthread->td_locks - mtx_owned(&Giant);	\
249d5210708SMatt Macy 	     if (unknown_locks > 0) {					\
250d5210708SMatt Macy 		     WITNESS_WARN(WARN_GIANTOK|WARN_SLEEPOK|WARN_PANIC, NULL, "unexpected non-sleepable lock"); \
251d5210708SMatt Macy 	     }								\
252d5210708SMatt Macy 	     MPASS(curthread->td_rw_rlocks == 0);			\
253d5210708SMatt Macy 	     MPASS(curthread->td_lk_slocks == 0);			\
254d5210708SMatt Macy 	} while (0)
255d5210708SMatt Macy #else
256d5210708SMatt Macy #define ASSERT_NO_LOCKS()
257d5210708SMatt Macy #endif
258d5210708SMatt Macy 
2598cfa0ad2SJack F Vogel #endif  /* _FREEBSD_OS_H_ */
2608cfa0ad2SJack F Vogel 
261