1179055Sjfv/******************************************************************************
2171384Sjfv
3282289Serj  Copyright (c) 2001-2015, Intel Corporation
4179055Sjfv  All rights reserved.
5179055Sjfv
6179055Sjfv  Redistribution and use in source and binary forms, with or without
7179055Sjfv  modification, are permitted provided that the following conditions are met:
8179055Sjfv
9179055Sjfv   1. Redistributions of source code must retain the above copyright notice,
10179055Sjfv      this list of conditions and the following disclaimer.
11179055Sjfv
12179055Sjfv   2. Redistributions in binary form must reproduce the above copyright
13179055Sjfv      notice, this list of conditions and the following disclaimer in the
14179055Sjfv      documentation and/or other materials provided with the distribution.
15179055Sjfv
16179055Sjfv   3. Neither the name of the Intel Corporation nor the names of its
17179055Sjfv      contributors may be used to endorse or promote products derived from
18179055Sjfv      this software without specific prior written permission.
19179055Sjfv
20179055Sjfv  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21179055Sjfv  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22179055Sjfv  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23179055Sjfv  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24179055Sjfv  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25179055Sjfv  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26179055Sjfv  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27179055Sjfv  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28179055Sjfv  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29179055Sjfv  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30179055Sjfv  POSSIBILITY OF SUCH DAMAGE.
31171384Sjfv
32179055Sjfv******************************************************************************/
33179055Sjfv/*$FreeBSD: releng/11.0/sys/dev/ixgbe/ixgbe_osdep.h 294734 2016-01-25 16:18:53Z smh $*/
34171384Sjfv
35171384Sjfv#ifndef _IXGBE_OS_H_
36171384Sjfv#define _IXGBE_OS_H_
37171384Sjfv
38171384Sjfv#include <sys/types.h>
39171384Sjfv#include <sys/param.h>
40230775Sjfv#include <sys/endian.h>
41171384Sjfv#include <sys/systm.h>
42171384Sjfv#include <sys/mbuf.h>
43171384Sjfv#include <sys/protosw.h>
44171384Sjfv#include <sys/socket.h>
45171384Sjfv#include <sys/malloc.h>
46171384Sjfv#include <sys/kernel.h>
47171384Sjfv#include <sys/bus.h>
48171384Sjfv#include <machine/bus.h>
49171384Sjfv#include <sys/rman.h>
50171384Sjfv#include <machine/resource.h>
51171384Sjfv#include <vm/vm.h>
52171384Sjfv#include <vm/pmap.h>
53171384Sjfv#include <machine/clock.h>
54171384Sjfv#include <dev/pci/pcivar.h>
55171384Sjfv#include <dev/pci/pcireg.h>
56171384Sjfv
57171384Sjfv#define ASSERT(x) if(!(x)) panic("IXGBE: x")
58238149Sjfv#define EWARN(H, W, S) printf(W)
59171384Sjfv
60294734Ssmhenum {
61294734Ssmh	IXGBE_ERROR_SOFTWARE,
62294734Ssmh	IXGBE_ERROR_POLLING,
63294734Ssmh	IXGBE_ERROR_INVALID_STATE,
64294734Ssmh	IXGBE_ERROR_UNSUPPORTED,
65294734Ssmh	IXGBE_ERROR_ARGUMENT,
66294734Ssmh	IXGBE_ERROR_CAUTION,
67294734Ssmh};
68294734Ssmh
69171384Sjfv/* The happy-fun DELAY macro is defined in /usr/src/sys/i386/include/clock.h */
70171384Sjfv#define usec_delay(x) DELAY(x)
71171384Sjfv#define msec_delay(x) DELAY(1000*(x))
72171384Sjfv
73292674Ssbruno#define DBG 0
74171384Sjfv#define MSGOUT(S, A, B)     printf(S "\n", A, B)
75171384Sjfv#define DEBUGFUNC(F)        DEBUGOUT(F);
76171384Sjfv#if DBG
77171384Sjfv	#define DEBUGOUT(S)         printf(S "\n")
78171384Sjfv	#define DEBUGOUT1(S,A)      printf(S "\n",A)
79171384Sjfv	#define DEBUGOUT2(S,A,B)    printf(S "\n",A,B)
80171384Sjfv	#define DEBUGOUT3(S,A,B,C)  printf(S "\n",A,B,C)
81240466Seadler	#define DEBUGOUT4(S,A,B,C,D)  printf(S "\n",A,B,C,D)
82240466Seadler	#define DEBUGOUT5(S,A,B,C,D,E)  printf(S "\n",A,B,C,D,E)
83240466Seadler	#define DEBUGOUT6(S,A,B,C,D,E,F)  printf(S "\n",A,B,C,D,E,F)
84171384Sjfv	#define DEBUGOUT7(S,A,B,C,D,E,F,G)  printf(S "\n",A,B,C,D,E,F,G)
85294734Ssmh	#define ERROR_REPORT1 ERROR_REPORT
86294734Ssmh	#define ERROR_REPORT2 ERROR_REPORT
87294734Ssmh	#define ERROR_REPORT3 ERROR_REPORT
88294734Ssmh	#define ERROR_REPORT(level, format, arg...) do { \
89294734Ssmh		switch (level) { \
90294734Ssmh		case IXGBE_ERROR_SOFTWARE: \
91294734Ssmh		case IXGBE_ERROR_CAUTION: \
92294734Ssmh		case IXGBE_ERROR_POLLING: \
93294734Ssmh		case IXGBE_ERROR_INVALID_STATE: \
94294734Ssmh		case IXGBE_ERROR_UNSUPPORTED: \
95294734Ssmh		case IXGBE_ERROR_ARGUMENT: \
96294734Ssmh			device_printf(ixgbe_dev_from_hw(hw), format, ## arg); \
97294734Ssmh			break; \
98294734Ssmh		default: \
99294734Ssmh			break; \
100294734Ssmh		} \
101294734Ssmh	} while (0)
102171384Sjfv#else
103171384Sjfv	#define DEBUGOUT(S)
104171384Sjfv	#define DEBUGOUT1(S,A)
105171384Sjfv	#define DEBUGOUT2(S,A,B)
106171384Sjfv	#define DEBUGOUT3(S,A,B,C)
107240466Seadler	#define DEBUGOUT4(S,A,B,C,D)
108240466Seadler	#define DEBUGOUT5(S,A,B,C,D,E)
109171384Sjfv	#define DEBUGOUT6(S,A,B,C,D,E,F)
110171384Sjfv	#define DEBUGOUT7(S,A,B,C,D,E,F,G)
111251964Sjfv
112251964Sjfv	#define ERROR_REPORT1(S,A)
113251964Sjfv	#define ERROR_REPORT2(S,A,B)
114251964Sjfv	#define ERROR_REPORT3(S,A,B,C)
115171384Sjfv#endif
116171384Sjfv
117171384Sjfv#define FALSE               0
118185352Sjfv#define false               0 /* shared code requires this */
119171384Sjfv#define TRUE                1
120185352Sjfv#define true                1
121171384Sjfv#define CMD_MEM_WRT_INVALIDATE          0x0010  /* BIT_4 */
122171384Sjfv#define PCI_COMMAND_REGISTER            PCIR_COMMAND
123230775Sjfv
124247822Sjfv/* Shared code dropped this define.. */
125247822Sjfv#define IXGBE_INTEL_VENDOR_ID		0x8086
126247822Sjfv
127230775Sjfv/* Bunch of defines for shared code bogosity */
128194875Sjfv#define UNREFERENCED_PARAMETER(_p)
129230775Sjfv#define UNREFERENCED_1PARAMETER(_p)
130230775Sjfv#define UNREFERENCED_2PARAMETER(_p, _q)
131230775Sjfv#define UNREFERENCED_3PARAMETER(_p, _q, _r)
132230775Sjfv#define UNREFERENCED_4PARAMETER(_p, _q, _r, _s)
133171384Sjfv
134215911Sjfv#define IXGBE_NTOHL(_i)	ntohl(_i)
135215911Sjfv#define IXGBE_NTOHS(_i)	ntohs(_i)
136171384Sjfv
137230775Sjfv/* XXX these need to be revisited */
138282289Serj#define IXGBE_CPU_TO_LE32 htole32
139282289Serj#define IXGBE_LE32_TO_CPUS(x)
140282289Serj#define IXGBE_CPU_TO_BE16 htobe16
141282289Serj#define IXGBE_CPU_TO_BE32 htobe32
142230775Sjfv
143185352Sjfvtypedef uint8_t		u8;
144185352Sjfvtypedef int8_t		s8;
145185352Sjfvtypedef uint16_t	u16;
146251964Sjfvtypedef int16_t		s16;
147185352Sjfvtypedef uint32_t	u32;
148185352Sjfvtypedef int32_t		s32;
149185352Sjfvtypedef uint64_t	u64;
150230790Sjfv#ifndef __bool_true_false_are_defined
151185352Sjfvtypedef boolean_t	bool;
152230790Sjfv#endif
153185352Sjfv
154238149Sjfv/* shared code requires this */
155238149Sjfv#define __le16  u16
156238149Sjfv#define __le32  u32
157238149Sjfv#define __le64  u64
158238149Sjfv#define __be16  u16
159238149Sjfv#define __be32  u32
160238149Sjfv#define __be64  u64
161238149Sjfv
162171384Sjfv#define le16_to_cpu
163171384Sjfv
164190873Sjfv#if __FreeBSD_version < 800000
165179055Sjfv#if defined(__i386__) || defined(__amd64__)
166179055Sjfv#define mb()	__asm volatile("mfence" ::: "memory")
167179055Sjfv#define wmb()	__asm volatile("sfence" ::: "memory")
168179055Sjfv#define rmb()	__asm volatile("lfence" ::: "memory")
169179055Sjfv#else
170179055Sjfv#define mb()
171179055Sjfv#define rmb()
172179055Sjfv#define wmb()
173179055Sjfv#endif
174190873Sjfv#endif
175179055Sjfv
176200239Sjfv#if defined(__i386__) || defined(__amd64__)
177200239Sjfvstatic __inline
178200239Sjfvvoid prefetch(void *x)
179200239Sjfv{
180200239Sjfv	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
181200239Sjfv}
182200239Sjfv#else
183200239Sjfv#define prefetch(x)
184200239Sjfv#endif
185200239Sjfv
186239940Sscottl/*
187239940Sscottl * Optimized bcopy thanks to Luigi Rizzo's investigative work.  Assumes
188239940Sscottl * non-overlapping regions and 32-byte padding on both src and dst.
189239940Sscottl */
190239940Sscottlstatic __inline int
191292674Ssbrunoixgbe_bcopy(void *restrict _src, void *restrict _dst, int l)
192239940Sscottl{
193239940Sscottl	uint64_t *src = _src;
194239940Sscottl	uint64_t *dst = _dst;
195239940Sscottl
196239940Sscottl	for (; l > 0; l -= 32) {
197239940Sscottl		*dst++ = *src++;
198239940Sscottl		*dst++ = *src++;
199239940Sscottl		*dst++ = *src++;
200239940Sscottl		*dst++ = *src++;
201239940Sscottl	}
202239940Sscottl	return (0);
203239940Sscottl}
204239940Sscottl
205171384Sjfvstruct ixgbe_osdep
206171384Sjfv{
207171384Sjfv	bus_space_tag_t    mem_bus_space_tag;
208171384Sjfv	bus_space_handle_t mem_bus_space_handle;
209171384Sjfv};
210171384Sjfv
211292674Ssbruno/* These routines need struct ixgbe_hw declared */
212292674Ssbrunostruct ixgbe_hw;
213292674Ssbrunodevice_t ixgbe_dev_from_hw(struct ixgbe_hw *hw);
214292674Ssbruno
215190873Sjfv/* These routines are needed by the shared code */
216171384Sjfvextern u16 ixgbe_read_pci_cfg(struct ixgbe_hw *, u32);
217171384Sjfv#define IXGBE_READ_PCIE_WORD ixgbe_read_pci_cfg
218171384Sjfv
219190873Sjfvextern void ixgbe_write_pci_cfg(struct ixgbe_hw *, u32, u16);
220190873Sjfv#define IXGBE_WRITE_PCIE_WORD ixgbe_write_pci_cfg
221190873Sjfv
222171384Sjfv#define IXGBE_WRITE_FLUSH(a) IXGBE_READ_REG(a, IXGBE_STATUS)
223171384Sjfv
224292674Ssbrunoextern u32 ixgbe_read_reg(struct ixgbe_hw *, u32);
225292674Ssbruno#define IXGBE_READ_REG(a, reg) ixgbe_read_reg(a, reg)
226171384Sjfv
227292674Ssbrunoextern void ixgbe_write_reg(struct ixgbe_hw *, u32, u32);
228292674Ssbruno#define IXGBE_WRITE_REG(a, reg, val) ixgbe_write_reg(a, reg, val)
229171384Sjfv
230292674Ssbrunoextern u32 ixgbe_read_reg_array(struct ixgbe_hw *, u32, u32);
231292674Ssbruno#define IXGBE_READ_REG_ARRAY(a, reg, offset) \
232292674Ssbruno    ixgbe_read_reg_array(a, reg, offset)
233171384Sjfv
234292674Ssbrunoextern void ixgbe_write_reg_array(struct ixgbe_hw *, u32, u32, u32);
235292674Ssbruno#define IXGBE_WRITE_REG_ARRAY(a, reg, offset, val) \
236292674Ssbruno    ixgbe_write_reg_array(a, reg, offset, val)
237171384Sjfv
238171384Sjfv#endif /* _IXGBE_OS_H_ */
239