xref: /freebsd/sys/dev/e1000/e1000_osdep.h (revision 7282444b103e0b3e7c8cd053413ff6c068831cc6)
18cfa0ad2SJack F Vogel /******************************************************************************
2*7282444bSPedro F. Giffuni   SPDX-License-Identifier: BSD-3-Clause
38cfa0ad2SJack F Vogel 
47c669ab6SSean Bruno   Copyright (c) 2001-2015, 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 /*$FreeBSD$*/
358cfa0ad2SJack F Vogel 
368cfa0ad2SJack F Vogel 
378cfa0ad2SJack F Vogel #ifndef _FREEBSD_OS_H_
388cfa0ad2SJack F Vogel #define _FREEBSD_OS_H_
398cfa0ad2SJack F Vogel 
408cfa0ad2SJack F Vogel #include <sys/types.h>
418cfa0ad2SJack F Vogel #include <sys/param.h>
428cfa0ad2SJack F Vogel #include <sys/systm.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>
518cfa0ad2SJack F Vogel #include <machine/bus.h>
528cfa0ad2SJack F Vogel #include <sys/rman.h>
538cfa0ad2SJack F Vogel #include <machine/resource.h>
548cfa0ad2SJack F Vogel #include <vm/vm.h>
558cfa0ad2SJack F Vogel #include <vm/pmap.h>
568cfa0ad2SJack F Vogel #include <machine/clock.h>
578cfa0ad2SJack F Vogel #include <dev/pci/pcivar.h>
588cfa0ad2SJack F Vogel #include <dev/pci/pcireg.h>
598cfa0ad2SJack F Vogel 
608cfa0ad2SJack F Vogel 
618cfa0ad2SJack F Vogel #define ASSERT(x) if(!(x)) panic("EM: x")
628cfa0ad2SJack F Vogel 
63ab2e3f79SStephen Hurd #define usec_delay(x) DELAY(x)
64c80429ceSEric Joyner #define usec_delay_irq(x) usec_delay(x)
65ab2e3f79SStephen Hurd #define msec_delay(x) DELAY(1000*(x))
66ab2e3f79SStephen Hurd #define msec_delay_irq(x) DELAY(1000*(x))
678cfa0ad2SJack F Vogel 
68c80429ceSEric Joyner /* Enable/disable debugging statements in shared code */
69c80429ceSEric Joyner #define DBG		0
70c80429ceSEric Joyner 
71c80429ceSEric Joyner #define DEBUGOUT(...) \
72c80429ceSEric Joyner     do { if (DBG) printf(__VA_ARGS__); } while (0)
73c80429ceSEric Joyner #define DEBUGOUT1(...)			DEBUGOUT(__VA_ARGS__)
74c80429ceSEric Joyner #define DEBUGOUT2(...)			DEBUGOUT(__VA_ARGS__)
75c80429ceSEric Joyner #define DEBUGOUT3(...)			DEBUGOUT(__VA_ARGS__)
76c80429ceSEric Joyner #define DEBUGOUT7(...)			DEBUGOUT(__VA_ARGS__)
77c80429ceSEric Joyner #define DEBUGFUNC(F)			DEBUGOUT(F "\n")
788cfa0ad2SJack F Vogel 
798cfa0ad2SJack F Vogel #define STATIC			static
808cfa0ad2SJack F Vogel #define FALSE			0
818cfa0ad2SJack F Vogel #define TRUE			1
828cfa0ad2SJack F Vogel #define CMD_MEM_WRT_INVALIDATE	0x0010  /* BIT_4 */
838cfa0ad2SJack F Vogel #define PCI_COMMAND_REGISTER	PCIR_COMMAND
848cfa0ad2SJack F Vogel 
85ab2e3f79SStephen Hurd /* Mutex used in the shared code */
86ab2e3f79SStephen Hurd #define E1000_MUTEX                     struct mtx
87ab2e3f79SStephen Hurd #define E1000_MUTEX_INIT(mutex)         mtx_init((mutex), #mutex, \
88ab2e3f79SStephen Hurd                                             MTX_NETWORK_LOCK, \
89ab2e3f79SStephen Hurd 					    MTX_DEF | MTX_DUPOK)
90ab2e3f79SStephen Hurd #define E1000_MUTEX_DESTROY(mutex)      mtx_destroy(mutex)
91ab2e3f79SStephen Hurd #define E1000_MUTEX_LOCK(mutex)         mtx_lock(mutex)
92ab2e3f79SStephen Hurd #define E1000_MUTEX_TRYLOCK(mutex)      mtx_trylock(mutex)
93ab2e3f79SStephen Hurd #define E1000_MUTEX_UNLOCK(mutex)       mtx_unlock(mutex)
94ab2e3f79SStephen Hurd 
958cfa0ad2SJack F Vogel typedef uint64_t	u64;
968cfa0ad2SJack F Vogel typedef uint32_t	u32;
978cfa0ad2SJack F Vogel typedef uint16_t	u16;
988cfa0ad2SJack F Vogel typedef uint8_t		u8;
998cfa0ad2SJack F Vogel typedef int64_t		s64;
1008cfa0ad2SJack F Vogel typedef int32_t		s32;
1018cfa0ad2SJack F Vogel typedef int16_t		s16;
1028cfa0ad2SJack F Vogel typedef int8_t		s8;
1038cfa0ad2SJack F Vogel 
104daf9197cSJack F Vogel #define __le16		u16
105daf9197cSJack F Vogel #define __le32		u32
106daf9197cSJack F Vogel #define __le64		u64
107daf9197cSJack F Vogel 
1088cc64f1eSJack F Vogel #if __FreeBSD_version < 800000
1094edd8523SJack F Vogel #if defined(__i386__) || defined(__amd64__)
1104edd8523SJack F Vogel #define mb()	__asm volatile("mfence" ::: "memory")
1114edd8523SJack F Vogel #define wmb()	__asm volatile("sfence" ::: "memory")
1124edd8523SJack F Vogel #define rmb()	__asm volatile("lfence" ::: "memory")
1134edd8523SJack F Vogel #else
1144edd8523SJack F Vogel #define mb()
1154edd8523SJack F Vogel #define rmb()
1164edd8523SJack F Vogel #define wmb()
1174edd8523SJack F Vogel #endif
1184edd8523SJack F Vogel #endif /*__FreeBSD_version < 800000 */
1194edd8523SJack F Vogel 
1204edd8523SJack F Vogel #if defined(__i386__) || defined(__amd64__)
1214edd8523SJack F Vogel static __inline
1224edd8523SJack F Vogel void prefetch(void *x)
1234edd8523SJack F Vogel {
1244edd8523SJack F Vogel 	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
1254edd8523SJack F Vogel }
1264edd8523SJack F Vogel #else
1274edd8523SJack F Vogel #define prefetch(x)
1284edd8523SJack F Vogel #endif
1294edd8523SJack F Vogel 
1308cfa0ad2SJack F Vogel struct e1000_osdep
1318cfa0ad2SJack F Vogel {
1328cfa0ad2SJack F Vogel 	bus_space_tag_t    mem_bus_space_tag;
1338cfa0ad2SJack F Vogel 	bus_space_handle_t mem_bus_space_handle;
1348cfa0ad2SJack F Vogel 	bus_space_tag_t    io_bus_space_tag;
1358cfa0ad2SJack F Vogel 	bus_space_handle_t io_bus_space_handle;
1368cfa0ad2SJack F Vogel 	bus_space_tag_t    flash_bus_space_tag;
1378cfa0ad2SJack F Vogel 	bus_space_handle_t flash_bus_space_handle;
138bd937497SJean-Sébastien Pédron 	device_t	   dev;
1398cfa0ad2SJack F Vogel };
1408cfa0ad2SJack F Vogel 
1418cfa0ad2SJack F Vogel #define E1000_REGISTER(hw, reg) (((hw)->mac.type >= e1000_82543) \
1428cfa0ad2SJack F Vogel     ? reg : e1000_translate_register_82542(reg))
1438cfa0ad2SJack F Vogel 
1448cfa0ad2SJack F Vogel #define E1000_WRITE_FLUSH(a) E1000_READ_REG(a, E1000_STATUS)
1458cfa0ad2SJack F Vogel 
1468cfa0ad2SJack F Vogel /* Read from an absolute offset in the adapter's memory space */
1478cfa0ad2SJack F Vogel #define E1000_READ_OFFSET(hw, offset) \
1488cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1498cfa0ad2SJack F Vogel     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset)
1508cfa0ad2SJack F Vogel 
1518cfa0ad2SJack F Vogel /* Write to an absolute offset in the adapter's memory space */
1528cfa0ad2SJack F Vogel #define E1000_WRITE_OFFSET(hw, offset, value) \
1538cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1548cfa0ad2SJack F Vogel     ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, offset, value)
1558cfa0ad2SJack F Vogel 
1568cfa0ad2SJack F Vogel /* Register READ/WRITE macros */
1578cfa0ad2SJack F Vogel 
1588cfa0ad2SJack F Vogel #define E1000_READ_REG(hw, reg) \
1598cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1608cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1618cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg))
1628cfa0ad2SJack F Vogel 
1638cfa0ad2SJack F Vogel #define E1000_WRITE_REG(hw, reg, value) \
1648cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1658cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1668cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg), value)
1678cfa0ad2SJack F Vogel 
1688cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY(hw, reg, index) \
1698cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1708cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1718cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + ((index)<< 2))
1728cfa0ad2SJack F Vogel 
1738cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY(hw, reg, index, value) \
1748cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1758cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1768cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + ((index)<< 2), value)
1778cfa0ad2SJack F Vogel 
1788cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY_DWORD E1000_READ_REG_ARRAY
1798cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_DWORD E1000_WRITE_REG_ARRAY
1808cfa0ad2SJack F Vogel 
1818cfa0ad2SJack F Vogel #define E1000_READ_REG_ARRAY_BYTE(hw, reg, index) \
1828cfa0ad2SJack F Vogel     bus_space_read_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1838cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1848cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + index)
1858cfa0ad2SJack F Vogel 
1868cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_BYTE(hw, reg, index, value) \
1878cfa0ad2SJack F Vogel     bus_space_write_1(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1888cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1898cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + index, value)
1908cfa0ad2SJack F Vogel 
1918cfa0ad2SJack F Vogel #define E1000_WRITE_REG_ARRAY_WORD(hw, reg, index, value) \
1928cfa0ad2SJack F Vogel     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->mem_bus_space_tag, \
1938cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->mem_bus_space_handle, \
1948cfa0ad2SJack F Vogel         E1000_REGISTER(hw, reg) + (index << 1), value)
1958cfa0ad2SJack F Vogel 
1968cfa0ad2SJack F Vogel #define E1000_WRITE_REG_IO(hw, reg, value) do {\
1978cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
1988cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
1998cfa0ad2SJack F Vogel         (hw)->io_base, reg); \
2008cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->io_bus_space_tag, \
2018cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->io_bus_space_handle, \
2028cfa0ad2SJack F Vogel         (hw)->io_base + 4, value); } while (0)
2038cfa0ad2SJack F Vogel 
2048cfa0ad2SJack F Vogel #define E1000_READ_FLASH_REG(hw, reg) \
2058cfa0ad2SJack F Vogel     bus_space_read_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2068cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
2078cfa0ad2SJack F Vogel 
2088cfa0ad2SJack F Vogel #define E1000_READ_FLASH_REG16(hw, reg) \
2098cfa0ad2SJack F Vogel     bus_space_read_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2108cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg)
2118cfa0ad2SJack F Vogel 
2128cfa0ad2SJack F Vogel #define E1000_WRITE_FLASH_REG(hw, reg, value) \
2138cfa0ad2SJack F Vogel     bus_space_write_4(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2148cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
2158cfa0ad2SJack F Vogel 
2168cfa0ad2SJack F Vogel #define E1000_WRITE_FLASH_REG16(hw, reg, value) \
2178cfa0ad2SJack F Vogel     bus_space_write_2(((struct e1000_osdep *)(hw)->back)->flash_bus_space_tag, \
2188cfa0ad2SJack F Vogel         ((struct e1000_osdep *)(hw)->back)->flash_bus_space_handle, reg, value)
2198cfa0ad2SJack F Vogel 
2208cfa0ad2SJack F Vogel #endif  /* _FREEBSD_OS_H_ */
2218cfa0ad2SJack F Vogel 
222