108057504Sxy /*
208057504Sxy  * This file is provided under a CDDLv1 license.  When using or
308057504Sxy  * redistributing this file, you may do so under this license.
408057504Sxy  * In redistributing this file this license must be included
508057504Sxy  * and no other modification of this header file is permitted.
608057504Sxy  *
708057504Sxy  * CDDL LICENSE SUMMARY
808057504Sxy  *
9d5c3073dSchenlu chen - Sun Microsystems - Beijing China  * Copyright(c) 1999 - 2009 Intel Corporation. All rights reserved.
1008057504Sxy  *
1108057504Sxy  * The contents of this file are subject to the terms of Version
1208057504Sxy  * 1.0 of the Common Development and Distribution License (the "License").
1308057504Sxy  *
1408057504Sxy  * You should have received a copy of the License with this software.
1508057504Sxy  * You can obtain a copy of the License at
1608057504Sxy  *	http://www.opensolaris.org/os/licensing.
1708057504Sxy  * See the License for the specific language governing permissions
1808057504Sxy  * and limitations under the License.
1908057504Sxy  */
2008057504Sxy 
2108057504Sxy /*
223f64cd55Sguoqing zhu - Sun Microsystems - Beijing China  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
2308057504Sxy  * Use is subject to license terms of the CDDLv1.
24*49b78600SRobert Mustacchi  *
25*49b78600SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
2608057504Sxy  */
2708057504Sxy 
2808057504Sxy #ifndef _E1000_OSDEP_H
2908057504Sxy #define	_E1000_OSDEP_H
3008057504Sxy 
3108057504Sxy #ifdef __cplusplus
3208057504Sxy extern "C" {
3308057504Sxy #endif
3408057504Sxy 
3508057504Sxy #include <sys/types.h>
3608057504Sxy #include <sys/conf.h>
3708057504Sxy #include <sys/debug.h>
3808057504Sxy #include <sys/stropts.h>
3908057504Sxy #include <sys/stream.h>
4008057504Sxy #include <sys/strlog.h>
4108057504Sxy #include <sys/kmem.h>
4208057504Sxy #include <sys/stat.h>
4308057504Sxy #include <sys/kstat.h>
4408057504Sxy #include <sys/modctl.h>
4508057504Sxy #include <sys/errno.h>
4608057504Sxy #include <sys/ddi.h>
4708057504Sxy #include <sys/sunddi.h>
4808057504Sxy #include <sys/pci.h>
4925f2d433Sxy #include <sys/atomic.h>
50fe62dec3SChen-Liang Xu #include <sys/note.h>
51caf05df5SMiles Xu, Sun Microsystems #include <sys/mutex.h>
5275eba5b6SRobert Mustacchi #include <sys/pci_cap.h>
5325f2d433Sxy #include "e1000g_debug.h"
5408057504Sxy 
5508057504Sxy #define	usec_delay(x)		drv_usecwait(x)
56c124a83eSRobert Mustacchi #define	usec_delay_irq		usec_delay
5708057504Sxy #define	msec_delay(x)		drv_usecwait(x * 1000)
584d737963Sxiangtao you - Sun Microsystems - Beijing China #define	msec_delay_irq		msec_delay
5908057504Sxy 
6025f2d433Sxy #ifdef E1000G_DEBUG
6125f2d433Sxy #define	DEBUGOUT(S)		\
6225f2d433Sxy 	E1000G_DEBUGLOG_0(NULL, E1000G_INFO_LEVEL, S)
6325f2d433Sxy #define	DEBUGOUT1(S, A)		\
6425f2d433Sxy 	E1000G_DEBUGLOG_1(NULL, E1000G_INFO_LEVEL, S, A)
6525f2d433Sxy #define	DEBUGOUT2(S, A, B)	\
6625f2d433Sxy 	E1000G_DEBUGLOG_2(NULL, E1000G_INFO_LEVEL, S, A, B)
6725f2d433Sxy #define	DEBUGOUT3(S, A, B, C)	\
6825f2d433Sxy 	E1000G_DEBUGLOG_3(NULL, E1000G_INFO_LEVEL, S, A, B, C)
6925f2d433Sxy #define	DEBUGFUNC(F)		\
7025f2d433Sxy 	E1000G_DEBUGLOG_0(NULL, E1000G_TRACE_LEVEL, F)
7108057504Sxy #else
7208057504Sxy #define	DEBUGOUT(S)
7308057504Sxy #define	DEBUGOUT1(S, A)
7408057504Sxy #define	DEBUGOUT2(S, A, B)
7508057504Sxy #define	DEBUGOUT3(S, A, B, C)
7625f2d433Sxy #define	DEBUGFUNC(F)
7708057504Sxy #endif
7808057504Sxy 
7925f2d433Sxy #define	OS_DEP(hw)		((struct e1000g_osdep *)((hw)->back))
8008057504Sxy 
814d737963Sxiangtao you - Sun Microsystems - Beijing China #define	false		0
824d737963Sxiangtao you - Sun Microsystems - Beijing China #define	true		1
8375eba5b6SRobert Mustacchi #define	FALSE		false
8475eba5b6SRobert Mustacchi #define	TRUE		true
85caf05df5SMiles Xu, Sun Microsystems 
8608057504Sxy #define	CMD_MEM_WRT_INVALIDATE	0x0010	/* BIT_4 */
8708057504Sxy #define	PCI_COMMAND_REGISTER	0x04
8825f2d433Sxy #define	PCI_EX_CONF_CAP		0xE0
894d737963Sxiangtao you - Sun Microsystems - Beijing China #define	ADAPTER_REG_SET		1 /* solaris mapping of adapter registers */
9025f2d433Sxy #define	ICH_FLASH_REG_SET	2	/* solaris mapping of flash memory */
9125f2d433Sxy 
9225f2d433Sxy #define	RECEIVE_BUFFER_ALIGN_SIZE	256
9325f2d433Sxy #define	E1000_MDALIGN			4096
94ede5269eSchenlu chen - Sun Microsystems - Beijing China #define	E1000_MDALIGN_82546		65536
9525f2d433Sxy #define	E1000_ERT_2048			0x100
9625f2d433Sxy 
9725f2d433Sxy /* PHY Extended Status Register */
9825f2d433Sxy #define	IEEE_ESR_1000T_HD_CAPS	0x1000	/* 1000T HD capable */
9925f2d433Sxy #define	IEEE_ESR_1000T_FD_CAPS	0x2000	/* 1000T FD capable */
10025f2d433Sxy #define	IEEE_ESR_1000X_HD_CAPS	0x4000	/* 1000X HD capable */
10125f2d433Sxy #define	IEEE_ESR_1000X_FD_CAPS	0x8000	/* 1000X FD capable */
10225f2d433Sxy 
103592a4d85Scc /*
104592a4d85Scc  * required by shared code
105592a4d85Scc  */
106fe62dec3SChen-Liang Xu #define	E1000_WRITE_FLUSH(a)	(void)E1000_READ_REG(a, E1000_STATUS)
10725f2d433Sxy 
108*49b78600SRobert Mustacchi /*
109*49b78600SRobert Mustacchi  * Note, for all of the following register defines, it's important that these be
110*49b78600SRobert Mustacchi  * in do {} while loops that only run a single time. Previously they were formed
111*49b78600SRobert Mustacchi  * as normal blocks. Unfortunately this would fail in the following form which
112*49b78600SRobert Mustacchi  * is used in the common code:
113*49b78600SRobert Mustacchi  *
114*49b78600SRobert Mustacchi  * if (cond)
115*49b78600SRobert Mustacchi  * 	E1000_WRITE_REG
116*49b78600SRobert Mustacchi  * else
117*49b78600SRobert Mustacchi  * 	...
118*49b78600SRobert Mustacchi  *
119*49b78600SRobert Mustacchi  * When the E1000_WRITE_REG macros was missing the do keyword, the compiler
120*49b78600SRobert Mustacchi  * would end up associating the outer brace of the block with the if statement
121*49b78600SRobert Mustacchi  * and thus the else clause would get left behind.
122*49b78600SRobert Mustacchi  */
12325f2d433Sxy #define	E1000_WRITE_REG(hw, reg, value)	\
124*49b78600SRobert Mustacchi do { \
12525f2d433Sxy 	if ((hw)->mac.type != e1000_82542) \
12625f2d433Sxy 		ddi_put32((OS_DEP(hw))->reg_handle, \
127fe62dec3SChen-Liang Xu 		    (uint32_t *)((uintptr_t)(hw)->hw_addr + reg), \
12808057504Sxy 		    value); \
12908057504Sxy 	else \
13025f2d433Sxy 		ddi_put32((OS_DEP(hw))->reg_handle, \
131fe62dec3SChen-Liang Xu 		    (uint32_t *)((uintptr_t)(hw)->hw_addr + \
13225f2d433Sxy 		    e1000_translate_register_82542(reg)), \
13308057504Sxy 		    value); \
134*49b78600SRobert Mustacchi } while (0)
13508057504Sxy 
13625f2d433Sxy #define	E1000_READ_REG(hw, reg) (\
13725f2d433Sxy 	((hw)->mac.type != e1000_82542) ? \
13825f2d433Sxy 	    ddi_get32((OS_DEP(hw))->reg_handle, \
139fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->hw_addr + reg)) : \
14025f2d433Sxy 	    ddi_get32((OS_DEP(hw))->reg_handle, \
141fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->hw_addr + \
14225f2d433Sxy 		e1000_translate_register_82542(reg))))
14308057504Sxy 
14425f2d433Sxy #define	E1000_WRITE_REG_ARRAY(hw, reg, offset, value) \
145*49b78600SRobert Mustacchi do {\
14625f2d433Sxy 	if ((hw)->mac.type != e1000_82542) \
14725f2d433Sxy 		ddi_put32((OS_DEP(hw))->reg_handle, \
148fe62dec3SChen-Liang Xu 		    (uint32_t *)((uintptr_t)(hw)->hw_addr + \
149fe62dec3SChen-Liang Xu 		    reg + ((offset) << 2)),\
15008057504Sxy 		    value); \
15108057504Sxy 	else \
15225f2d433Sxy 		ddi_put32((OS_DEP(hw))->reg_handle, \
153fe62dec3SChen-Liang Xu 		    (uint32_t *)((uintptr_t)(hw)->hw_addr + \
15425f2d433Sxy 		    e1000_translate_register_82542(reg) + \
15508057504Sxy 		    ((offset) << 2)), value); \
156*49b78600SRobert Mustacchi } while (0)
15708057504Sxy 
15825f2d433Sxy #define	E1000_READ_REG_ARRAY(hw, reg, offset) (\
15925f2d433Sxy 	((hw)->mac.type != e1000_82542) ? \
16025f2d433Sxy 	    ddi_get32((OS_DEP(hw))->reg_handle, \
161fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->hw_addr + reg + \
162fe62dec3SChen-Liang Xu 		((offset) << 2))) : \
16325f2d433Sxy 	    ddi_get32((OS_DEP(hw))->reg_handle, \
164fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->hw_addr + \
16525f2d433Sxy 		e1000_translate_register_82542(reg) + \
16608057504Sxy 		((offset) << 2))))
16708057504Sxy 
16808057504Sxy 
169fe62dec3SChen-Liang Xu #define	E1000_WRITE_REG_ARRAY_DWORD(a, reg, offset, value)	\
170fe62dec3SChen-Liang Xu 	E1000_WRITE_REG_ARRAY(a, reg, offset, value)
171fe62dec3SChen-Liang Xu #define	E1000_READ_REG_ARRAY_DWORD(a, reg, offset)		\
172fe62dec3SChen-Liang Xu 	E1000_READ_REG_ARRAY(a, reg, offset)
17308057504Sxy 
17408057504Sxy 
17525f2d433Sxy #define	E1000_READ_FLASH_REG(hw, reg)	\
17608057504Sxy 	ddi_get32((OS_DEP(hw))->ich_flash_handle, \
177fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->flash_address + (reg)))
17808057504Sxy 
17925f2d433Sxy #define	E1000_READ_FLASH_REG16(hw, reg)	\
18008057504Sxy 	ddi_get16((OS_DEP(hw))->ich_flash_handle, \
181fe62dec3SChen-Liang Xu 		(uint16_t *)((uintptr_t)(hw)->flash_address + (reg)))
18208057504Sxy 
18325f2d433Sxy #define	E1000_WRITE_FLASH_REG(hw, reg, value)	\
18408057504Sxy 	ddi_put32((OS_DEP(hw))->ich_flash_handle, \
185fe62dec3SChen-Liang Xu 		(uint32_t *)((uintptr_t)(hw)->flash_address + (reg)), (value))
18608057504Sxy 
18725f2d433Sxy #define	E1000_WRITE_FLASH_REG16(hw, reg, value)	\
18808057504Sxy 	ddi_put16((OS_DEP(hw))->ich_flash_handle, \
189fe62dec3SChen-Liang Xu 		(uint16_t *)((uintptr_t)(hw)->flash_address + (reg)), (value))
19008057504Sxy 
191fe62dec3SChen-Liang Xu #define	UNREFERENCED_1PARAMETER(_p)		_NOTE(ARGUNUSED(_p))
192fe62dec3SChen-Liang Xu #define	UNREFERENCED_2PARAMETER(_p, _q)		_NOTE(ARGUNUSED(_p, _q))
193fe62dec3SChen-Liang Xu #define	UNREFERENCED_3PARAMETER(_p, _q, _r)	_NOTE(ARGUNUSED(_p, _q, _r))
194fe62dec3SChen-Liang Xu #define	UNREFERENCED_4PARAMETER(_p, _q, _r, _s)	_NOTE(ARGUNUSED(_p, _q, _r, _s))
195fe62dec3SChen-Liang Xu #define	UNREFERENCED_5PARAMETER(_p, _q, _r, _s, _t)	\
196fe62dec3SChen-Liang Xu 	_NOTE(ARGUNUSED(_p, _q, _r, _s, _t))
197fe62dec3SChen-Liang Xu 
19825f2d433Sxy typedef	int8_t		s8;
19925f2d433Sxy typedef	int16_t		s16;
20025f2d433Sxy typedef	int32_t		s32;
20125f2d433Sxy typedef	int64_t		s64;
20225f2d433Sxy typedef	uint8_t		u8;
20325f2d433Sxy typedef	uint16_t	u16;
20425f2d433Sxy typedef	uint32_t	u32;
20525f2d433Sxy typedef	uint64_t	u64;
2064d737963Sxiangtao you - Sun Microsystems - Beijing China typedef boolean_t	bool;
20708057504Sxy 
208d5c3073dSchenlu chen - Sun Microsystems - Beijing China #define	__le16 u16
209d5c3073dSchenlu chen - Sun Microsystems - Beijing China #define	__le32 u32
210d5c3073dSchenlu chen - Sun Microsystems - Beijing China #define	__le64 u64
211d5c3073dSchenlu chen - Sun Microsystems - Beijing China 
21208057504Sxy struct e1000g_osdep {
21325f2d433Sxy 	ddi_acc_handle_t reg_handle;
21425f2d433Sxy 	ddi_acc_handle_t cfg_handle;
21508057504Sxy 	ddi_acc_handle_t ich_flash_handle;
2163f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	ddi_acc_handle_t io_reg_handle;
21725f2d433Sxy 	struct e1000g *adapter;
21808057504Sxy };
21908057504Sxy 
220caf05df5SMiles Xu, Sun Microsystems /* Shared Code Mutex Defines */
221caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX			kmutex_t
222caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX_INIT(mutex)		mutex_init(mutex, NULL, \
223caf05df5SMiles Xu, Sun Microsystems 	MUTEX_DRIVER, NULL)
224caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX_DESTROY(mutex)	mutex_destroy(mutex)
225caf05df5SMiles Xu, Sun Microsystems 
226caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX_LOCK(mutex)		mutex_enter(mutex)
227caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX_TRYLOCK(mutex)	mutex_tryenter(mutex)
228caf05df5SMiles Xu, Sun Microsystems #define	E1000_MUTEX_UNLOCK(mutex)	mutex_exit(mutex)
229caf05df5SMiles Xu, Sun Microsystems 
23008057504Sxy #ifdef __sparc	/* on SPARC, use only memory-mapped routines */
23108057504Sxy #define	E1000_WRITE_REG_IO	E1000_WRITE_REG
23208057504Sxy #else	/* on x86, use port io routines */
23325f2d433Sxy #define	E1000_WRITE_REG_IO(a, reg, val)	{ \
2343f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	ddi_put32((OS_DEP(a))->io_reg_handle, \
2353f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	    (uint32_t *)(a)->io_base, \
2363f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	    reg); \
2373f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	ddi_put32((OS_DEP(a))->io_reg_handle, \
2383f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	    (uint32_t *)((a)->io_base + 4), \
2393f64cd55Sguoqing zhu - Sun Microsystems - Beijing China 	    val); \
2403f64cd55Sguoqing zhu - Sun Microsystems - Beijing China }
24108057504Sxy #endif	/* __sparc */
24208057504Sxy 
24308057504Sxy #ifdef __cplusplus
24408057504Sxy }
24508057504Sxy #endif
24608057504Sxy 
24708057504Sxy #endif	/* _E1000_OSDEP_H */
248