i40e_osdep.h revision 270631
1/******************************************************************************
2
3  Copyright (c) 2013-2014, Intel Corporation
4  All rights reserved.
5
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are met:
8
9   1. Redistributions of source code must retain the above copyright notice,
10      this list of conditions and the following disclaimer.
11
12   2. Redistributions in binary form must reproduce the above copyright
13      notice, this list of conditions and the following disclaimer in the
14      documentation and/or other materials provided with the distribution.
15
16   3. Neither the name of the Intel Corporation nor the names of its
17      contributors may be used to endorse or promote products derived from
18      this software without specific prior written permission.
19
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE.
31
32******************************************************************************/
33/*$FreeBSD: stable/10/sys/dev/ixl/i40e_osdep.h 270631 2014-08-25 22:04:29Z jfv $*/
34
35#ifndef _I40E_OSDEP_H_
36#define _I40E_OSDEP_H_
37
38#include <sys/types.h>
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/endian.h>
42#include <sys/mbuf.h>
43#include <sys/protosw.h>
44#include <sys/socket.h>
45#include <sys/malloc.h>
46#include <sys/kernel.h>
47#include <sys/bus.h>
48#include <machine/bus.h>
49#include <sys/rman.h>
50#include <machine/resource.h>
51#include <vm/vm.h>
52#include <vm/pmap.h>
53#include <machine/clock.h>
54#include <dev/pci/pcivar.h>
55#include <dev/pci/pcireg.h>
56
57#define ASSERT(x) if(!(x)) panic("IXL: x")
58
59#define i40e_usec_delay(x) DELAY(x)
60#define i40e_msec_delay(x) DELAY(1000*(x))
61
62#define DBG 0
63#define MSGOUT(S, A, B)     printf(S "\n", A, B)
64#define DEBUGFUNC(F)        DEBUGOUT(F);
65#if DBG
66	#define DEBUGOUT(S)         printf(S "\n")
67	#define DEBUGOUT1(S,A)      printf(S "\n",A)
68	#define DEBUGOUT2(S,A,B)    printf(S "\n",A,B)
69	#define DEBUGOUT3(S,A,B,C)  printf(S "\n",A,B,C)
70	#define DEBUGOUT7(S,A,B,C,D,E,F,G)  printf(S "\n",A,B,C,D,E,F,G)
71#else
72	#define DEBUGOUT(S)
73	#define DEBUGOUT1(S,A)
74	#define DEBUGOUT2(S,A,B)
75	#define DEBUGOUT3(S,A,B,C)
76	#define DEBUGOUT6(S,A,B,C,D,E,F)
77	#define DEBUGOUT7(S,A,B,C,D,E,F,G)
78#endif
79
80#define UNREFERENCED_XPARAMETER
81#define UNREFERENCED_PARAMETER(_p)
82#define UNREFERENCED_1PARAMETER(_p)
83#define UNREFERENCED_2PARAMETER(_p, _q)
84#define UNREFERENCED_3PARAMETER(_p, _q, _r)
85#define UNREFERENCED_4PARAMETER(_p, _q, _r, _s)
86
87#define STATIC	static
88#define INLINE  inline
89
90#define FALSE               0
91#define false               0 /* shared code requires this */
92#define TRUE                1
93#define true                1
94#define CMD_MEM_WRT_INVALIDATE          0x0010  /* BIT_4 */
95#define PCI_COMMAND_REGISTER            PCIR_COMMAND
96#define ARRAY_SIZE(a)		(sizeof(a) / sizeof((a)[0]))
97
98#define i40e_memset(a, b, c, d)  memset((a), (b), (c))
99#define i40e_memcpy(a, b, c, d)  memcpy((a), (b), (c))
100
101#define CPU_TO_LE16(o)	htole16(o)
102#define CPU_TO_LE32(s)	htole32(s)
103#define CPU_TO_LE64(h)	htole64(h)
104#define LE16_TO_CPU(a)	le16toh(a)
105#define LE32_TO_CPU(c)	le32toh(c)
106#define LE64_TO_CPU(k)	le64toh(k)
107
108#define I40E_NTOHS(a)	ntohs(a)
109#define I40E_NTOHL(a)	ntohl(a)
110#define I40E_HTONS(a)	htons(a)
111#define I40E_HTONL(a)	htonl(a)
112
113#define FIELD_SIZEOF(x, y) (sizeof(((x*)0)->y))
114
115typedef uint8_t		u8;
116typedef int8_t		s8;
117typedef uint16_t	u16;
118typedef int16_t		s16;
119typedef uint32_t	u32;
120typedef int32_t		s32;
121typedef uint64_t	u64;
122
123/* long string relief */
124typedef enum i40e_status_code i40e_status;
125
126#define __le16  u16
127#define __le32  u32
128#define __le64  u64
129#define __be16  u16
130#define __be32  u32
131#define __be64  u64
132
133/* SW spinlock */
134struct i40e_spinlock {
135        struct mtx mutex;
136};
137
138#define le16_to_cpu
139
140static __inline
141void prefetch(void *x)
142{
143	__asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
144}
145
146struct i40e_osdep
147{
148	bus_space_tag_t		mem_bus_space_tag;
149	bus_space_handle_t	mem_bus_space_handle;
150	bus_size_t		mem_bus_space_size;
151	struct device		*dev;
152};
153
154struct i40e_dma_mem {
155	void			*va;
156	u64			pa;
157	bus_dma_tag_t		tag;
158	bus_dmamap_t		map;
159	bus_dma_segment_t	seg;
160	bus_size_t              size;
161	int			nseg;
162	int                     flags;
163};
164
165struct i40e_hw; /* forward decl */
166u16	i40e_read_pci_cfg(struct i40e_hw *, u32);
167void	i40e_write_pci_cfg(struct i40e_hw *, u32, u16);
168
169#define i40e_allocate_dma_mem(h, m, unused, s, a) i40e_allocate_dma(h, m, s, a)
170#define i40e_free_dma_mem(h, m) i40e_free_dma(h, m)
171
172#define i40e_debug(h, m, s, ...)  i40e_debug_d(h, m, s, ##__VA_ARGS__)
173extern void i40e_debug_d(void *hw, u32 mask, char *fmt_str, ...);
174
175struct i40e_virt_mem {
176	void *va;
177	u32 size;
178};
179#define i40e_allocate_virt_mem(h, m, s) i40e_allocate_virt(h, m, s)
180#define i40e_free_virt_mem(h, m) i40e_free_virt(h, m)
181
182/*
183** This hardware supports either 16 or 32 byte rx descriptors
184** we default here to the larger size.
185*/
186#define i40e_rx_desc i40e_32byte_rx_desc
187
188static __inline uint32_t
189rd32_osdep(struct i40e_osdep *osdep, uint32_t reg)
190{
191
192	KASSERT(reg < osdep->mem_bus_space_size,
193	    ("ixl: register offset %#jx too large (max is %#jx",
194	    (uintmax_t)a, (uintmax_t)osdep->mem_bus_space_size));
195
196	return (bus_space_read_4(osdep->mem_bus_space_tag,
197	    osdep->mem_bus_space_handle, reg));
198}
199
200static __inline void
201wr32_osdep(struct i40e_osdep *osdep, uint32_t reg, uint32_t value)
202{
203
204	KASSERT(reg < osdep->mem_bus_space_size,
205	    ("ixl: register offset %#jx too large (max is %#jx",
206	    (uintmax_t)a, (uintmax_t)osdep->mem_bus_space_size));
207
208	bus_space_write_4(osdep->mem_bus_space_tag,
209	    osdep->mem_bus_space_handle, reg, value);
210}
211
212#define rd32(a, reg)		rd32_osdep((a)->back, (reg))
213#define wr32(a, reg, value)	wr32_osdep((a)->back, (reg), (value))
214
215#define rd64(a, reg) (\
216   bus_space_read_8( ((struct i40e_osdep *)(a)->back)->mem_bus_space_tag, \
217                     ((struct i40e_osdep *)(a)->back)->mem_bus_space_handle, \
218                     reg))
219
220#define wr64(a, reg, value) (\
221   bus_space_write_8( ((struct i40e_osdep *)(a)->back)->mem_bus_space_tag, \
222                     ((struct i40e_osdep *)(a)->back)->mem_bus_space_handle, \
223                     reg, value))
224
225#define ixl_flush(a) (\
226   bus_space_read_4( ((struct i40e_osdep *)(a)->back)->mem_bus_space_tag, \
227                     ((struct i40e_osdep *)(a)->back)->mem_bus_space_handle, \
228                     I40E_GLGEN_STAT))
229
230#endif /* _I40E_OSDEP_H_ */
231