c57ecded2171a96980e0cc278fc43acba1149c8b
[deb_dpdk.git] / drivers / net / i40e / base / i40e_osdep.h
1 /******************************************************************************
2
3   Copyright (c) 2001-2015, 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 #ifndef _I40E_OSDEP_H_
34 #define _I40E_OSDEP_H_
35
36 #include <string.h>
37 #include <stdint.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40
41 #include <rte_common.h>
42 #include <rte_memcpy.h>
43 #include <rte_byteorder.h>
44 #include <rte_cycles.h>
45 #include <rte_spinlock.h>
46 #include <rte_log.h>
47 #include <rte_io.h>
48
49 #include "../i40e_logs.h"
50
51 #define INLINE inline
52 #define STATIC static
53
54 typedef uint8_t         u8;
55 typedef int8_t          s8;
56 typedef uint16_t        u16;
57 typedef uint32_t        u32;
58 typedef int32_t         s32;
59 typedef uint64_t        u64;
60 typedef int             bool;
61
62 typedef enum i40e_status_code i40e_status;
63 #define __iomem
64 #define hw_dbg(hw, S, A...) do {} while (0)
65 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
66 #define lower_32_bits(n) ((u32)(n))
67 #define low_16_bits(x)   ((x) & 0xFFFF)
68 #define high_16_bits(x)  (((x) & 0xFFFF0000) >> 16)
69
70 #ifndef ETH_ADDR_LEN
71 #define ETH_ADDR_LEN                  6
72 #endif
73
74 #ifndef __le16
75 #define __le16          uint16_t
76 #endif
77 #ifndef __le32
78 #define __le32          uint32_t
79 #endif
80 #ifndef __le64
81 #define __le64          uint64_t
82 #endif
83 #ifndef __be16
84 #define __be16          uint16_t
85 #endif
86 #ifndef __be32
87 #define __be32          uint32_t
88 #endif
89 #ifndef __be64
90 #define __be64          uint64_t
91 #endif
92
93 #define FALSE           0
94 #define TRUE            1
95 #define false           0
96 #define true            1
97
98 #define min(a,b) RTE_MIN(a,b)
99 #define max(a,b) RTE_MAX(a,b)
100
101 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
102 #define ASSERT(x) if(!(x)) rte_panic("IXGBE: x")
103
104 #define DEBUGOUT(S)        PMD_DRV_LOG_RAW(DEBUG, S)
105 #define DEBUGOUT1(S, A...) PMD_DRV_LOG_RAW(DEBUG, S, ##A)
106
107 #define DEBUGFUNC(F) DEBUGOUT(F "\n")
108 #define DEBUGOUT2 DEBUGOUT1
109 #define DEBUGOUT3 DEBUGOUT2
110 #define DEBUGOUT6 DEBUGOUT3
111 #define DEBUGOUT7 DEBUGOUT6
112
113 #define i40e_debug(h, m, s, ...)                                \
114 do {                                                            \
115         if (((m) & (h)->debug_mask))                            \
116                 PMD_DRV_LOG_RAW(DEBUG, "i40e %02x.%x " s,       \
117                         (h)->bus.device, (h)->bus.func,         \
118                                         ##__VA_ARGS__);         \
119 } while (0)
120
121 /* AQ commands based interfaces of i40e_read_rx_ctl() and i40e_write_rx_ctl()
122  * are required for reading/writing below registers, as reading/writing it
123  * directly may not function correctly if the device is under heavy small
124  * packet traffic. Note that those interfaces are available from FVL5 and not
125  * suitable before the AdminQ is ready during initialization.
126  *
127  * I40E_PFQF_CTL_0
128  * I40E_PFQF_HENA
129  * I40E_PFQF_FDALLOC
130  * I40E_PFQF_HREGION
131  * I40E_PFLAN_QALLOC
132  * I40E_VPQF_CTL
133  * I40E_VFQF_HENA
134  * I40E_VFQF_HREGION
135  * I40E_VSIQF_CTL
136  * I40E_VSILAN_QBASE
137  * I40E_VSILAN_QTABLE
138  * I40E_VSIQF_TCREGION
139  * I40E_PFQF_HKEY
140  * I40E_VFQF_HKEY
141  * I40E_PRTQF_CTL_0
142  * I40E_GLFCOE_RCTL
143  * I40E_GLFCOE_RSOF
144  * I40E_GLQF_CTL
145  * I40E_GLQF_SWAP
146  * I40E_GLQF_HASH_MSK
147  * I40E_GLQF_HASH_INSET
148  * I40E_GLQF_HSYM
149  * I40E_GLQF_FC_MSK
150  * I40E_GLQF_FC_INSET
151  * I40E_GLQF_FD_MSK
152  * I40E_PRTQF_FD_INSET
153  * I40E_PRTQF_FD_FLXINSET
154  * I40E_PRTQF_FD_MSK
155  */
156
157 #define I40E_PCI_REG(reg)               rte_read32(reg)
158 #define I40E_PCI_REG_ADDR(a, reg) \
159         ((volatile uint32_t *)((char *)(a)->hw_addr + (reg)))
160 static inline uint32_t i40e_read_addr(volatile void *addr)
161 {
162         return rte_le_to_cpu_32(I40E_PCI_REG(addr));
163 }
164
165 #define I40E_PCI_REG_WRITE(reg, value)          \
166         rte_write32((rte_cpu_to_le_32(value)), reg)
167 #define I40E_PCI_REG_WRITE_RELAXED(reg, value)  \
168         rte_write32_relaxed((rte_cpu_to_le_32(value)), reg)
169
170 #define I40E_WRITE_FLUSH(a) I40E_READ_REG(a, I40E_GLGEN_STAT)
171 #define I40EVF_WRITE_FLUSH(a) I40E_READ_REG(a, I40E_VFGEN_RSTAT)
172
173 #define I40E_READ_REG(hw, reg) i40e_read_addr(I40E_PCI_REG_ADDR((hw), (reg)))
174 #define I40E_WRITE_REG(hw, reg, value) \
175         I40E_PCI_REG_WRITE(I40E_PCI_REG_ADDR((hw), (reg)), (value))
176
177 #define rd32(a, reg) i40e_read_addr(I40E_PCI_REG_ADDR((a), (reg)))
178 #define wr32(a, reg, value) \
179         I40E_PCI_REG_WRITE(I40E_PCI_REG_ADDR((a), (reg)), (value))
180 #define flush(a) i40e_read_addr(I40E_PCI_REG_ADDR((a), (I40E_GLGEN_STAT)))
181
182 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
183
184 /* memory allocation tracking */
185 struct i40e_dma_mem {
186         void *va;
187         u64 pa;
188         u32 size;
189         const void *zone;
190 } __attribute__((packed));
191
192 #define i40e_allocate_dma_mem(h, m, unused, s, a) \
193                         i40e_allocate_dma_mem_d(h, m, s, a)
194 #define i40e_free_dma_mem(h, m) i40e_free_dma_mem_d(h, m)
195
196 struct i40e_virt_mem {
197         void *va;
198         u32 size;
199 } __attribute__((packed));
200
201 #define i40e_allocate_virt_mem(h, m, s) i40e_allocate_virt_mem_d(h, m, s)
202 #define i40e_free_virt_mem(h, m) i40e_free_virt_mem_d(h, m)
203
204 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
205 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
206 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
207 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
208 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
209 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
210
211 #define cpu_to_le16(o) rte_cpu_to_le_16(o)
212 #define cpu_to_le32(s) rte_cpu_to_le_32(s)
213 #define cpu_to_le64(h) rte_cpu_to_le_64(h)
214 #define le16_to_cpu(a) rte_le_to_cpu_16(a)
215 #define le32_to_cpu(c) rte_le_to_cpu_32(c)
216 #define le64_to_cpu(k) rte_le_to_cpu_64(k)
217
218 /* SW spinlock */
219 struct i40e_spinlock {
220         rte_spinlock_t spinlock;
221 };
222
223 #define i40e_init_spinlock(_sp) i40e_init_spinlock_d(_sp)
224 #define i40e_acquire_spinlock(_sp) i40e_acquire_spinlock_d(_sp)
225 #define i40e_release_spinlock(_sp) i40e_release_spinlock_d(_sp)
226 #define i40e_destroy_spinlock(_sp) i40e_destroy_spinlock_d(_sp)
227
228 #define I40E_NTOHS(a) rte_be_to_cpu_16(a)
229 #define I40E_NTOHL(a) rte_be_to_cpu_32(a)
230 #define I40E_HTONS(a) rte_cpu_to_be_16(a)
231 #define I40E_HTONL(a) rte_cpu_to_be_32(a)
232
233 #define i40e_memset(a, b, c, d) memset((a), (b), (c))
234 #define i40e_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
235
236 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
237 #define DELAY(x) rte_delay_us(x)
238 #define i40e_usec_delay(x) rte_delay_us(x)
239 #define i40e_msec_delay(x) rte_delay_us(1000*(x))
240 #define udelay(x) DELAY(x)
241 #define msleep(x) DELAY(1000*(x))
242 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
243
244 #endif /* _I40E_OSDEP_H_ */