i40e: new poll mode driver
[dpdk.git] / lib / librte_pmd_i40e / i40e / i40e_osdep.h
1 /******************************************************************************
2
3   Copyright (c) 2001-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 #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
48 #include "../i40e_logs.h"
49
50 #define INLINE inline
51 #define STATIC static
52
53 typedef uint8_t         u8;
54 typedef int8_t          s8;
55 typedef uint16_t        u16;
56 typedef uint32_t        u32;
57 typedef int32_t         s32;
58 typedef uint64_t        u64;
59 typedef int             bool;
60
61 typedef enum i40e_status_code i40e_status;
62 #define __iomem
63 #define hw_dbg(hw, S, A...) do {} while (0)
64 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
65 #define lower_32_bits(n) ((u32)(n))
66 #define low_16_bits(x)   ((x) & 0xFFFF)
67 #define high_16_bits(x)  (((x) & 0xFFFF0000) >> 16)
68
69 #ifndef ETH_ADDR_LEN
70 #define ETH_ADDR_LEN                  6
71 #endif
72
73 #ifndef __le16
74 #define __le16          uint16_t
75 #endif
76 #ifndef __le32
77 #define __le32          uint32_t
78 #endif
79 #ifndef __le64
80 #define __le64          uint64_t
81 #endif
82 #ifndef __be16
83 #define __be16          uint16_t
84 #endif
85 #ifndef __be32
86 #define __be32          uint32_t
87 #endif
88 #ifndef __be64
89 #define __be64          uint64_t
90 #endif
91
92 #define FALSE           0
93 #define TRUE            1
94 #define false           0
95 #define true            1
96
97 #define min(a,b) RTE_MIN(a,b)
98 #define max(a,b) RTE_MAX(a,b)
99
100 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
101 #define ASSERT(x) if(!(x)) rte_panic("IXGBE: x")
102
103 #define DEBUGOUT(S)        PMD_DRV_LOG(DEBUG, S)
104 #define DEBUGOUT1(S, A...) PMD_DRV_LOG(DEBUG, S, ##A)
105
106 #define DEBUGFUNC(F) DEBUGOUT(F)
107 #define DEBUGOUT2 DEBUGOUT1
108 #define DEBUGOUT3 DEBUGOUT2
109 #define DEBUGOUT6 DEBUGOUT3
110 #define DEBUGOUT7 DEBUGOUT6
111
112 #define i40e_debug(h, m, s, ...)                                \
113 do {                                                            \
114         if (((m) & (h)->debug_mask))                            \
115                 PMD_DRV_LOG(DEBUG, "i40e %02x.%x " s,           \
116                         (h)->bus.device, (h)->bus.func,         \
117                                         ##__VA_ARGS__);         \
118 } while (0)
119
120 #define I40E_PCI_REG(reg)         (*((volatile uint32_t *)(reg)))
121 #define I40E_PCI_REG_ADDR(a, reg) \
122         ((volatile uint32_t *)((char *)(a)->hw_addr + (reg)))
123 static inline uint32_t i40e_read_addr(volatile void *addr)
124 {
125         return I40E_PCI_REG(addr);
126 }
127 #define I40E_PCI_REG_WRITE(reg, value) \
128         do {I40E_PCI_REG((reg)) = (value);} while(0)
129
130 #define I40E_WRITE_FLUSH(a) I40E_READ_REG(a, I40E_GLGEN_STAT)
131 #define I40EVF_WRITE_FLUSH(a) I40E_READ_REG(a, I40E_VFGEN_RSTAT)
132
133 #define I40E_READ_REG(hw, reg) i40e_read_addr(I40E_PCI_REG_ADDR((hw), (reg)))
134 #define I40E_WRITE_REG(hw, reg, value) \
135         I40E_PCI_REG_WRITE(I40E_PCI_REG_ADDR((hw), (reg)), (value))
136
137 #define rd32(a, reg) i40e_read_addr(I40E_PCI_REG_ADDR((a), (reg)))
138 #define wr32(a, reg, value) \
139         I40E_PCI_REG_WRITE(I40E_PCI_REG_ADDR((a), (reg)), (value))
140 #define flush(a) i40e_read_addr(I40E_PCI_REG_ADDR((a), (I40E_GLGEN_STAT)))
141
142 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(arr[0]))
143
144 /* memory allocation tracking */
145 struct i40e_dma_mem {
146         void *va;
147         u64 pa;
148         u32 size;
149         u64 id;
150 } __attribute__((packed));
151
152 #define i40e_allocate_dma_mem(h, m, unused, s, a) \
153                         i40e_allocate_dma_mem_d(h, m, s, a)
154 #define i40e_free_dma_mem(h, m) i40e_free_dma_mem_d(h, m)
155
156 struct i40e_virt_mem {
157         void *va;
158         u32 size;
159 } __attribute__((packed));
160
161 #define i40e_allocate_virt_mem(h, m, s) i40e_allocate_virt_mem_d(h, m, s)
162 #define i40e_free_virt_mem(h, m) i40e_free_virt_mem_d(h, m)
163
164 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o)
165 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s)
166 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h)
167 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a)
168 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c)
169 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k)
170
171 /* SW spinlock */
172 struct i40e_spinlock {
173         rte_spinlock_t spinlock;
174 };
175
176 #define i40e_init_spinlock(_sp) i40e_init_spinlock_d(_sp)
177 #define i40e_acquire_spinlock(_sp) i40e_acquire_spinlock_d(_sp)
178 #define i40e_release_spinlock(_sp) i40e_release_spinlock_d(_sp)
179 #define i40e_destroy_spinlock(_sp) i40e_destroy_spinlock_d(_sp)
180
181 #define I40E_NTOHS(a) rte_be_to_cpu_16(a)
182 #define I40E_NTOHL(a) rte_be_to_cpu_32(a)
183 #define I40E_HTONS(a) rte_cpu_to_be_16(a)
184 #define I40E_HTONL(a) rte_cpu_to_be_32(a)
185
186 #define i40e_memset(a, b, c, d) memset((a), (b), (c))
187 #define i40e_memcpy(a, b, c, d) rte_memcpy((a), (b), (c))
188
189 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
190 #define DELAY(x) rte_delay_us(x)
191 #define i40e_usec_delay(x) rte_delay_us(x)
192 #define i40e_msec_delay(x) rte_delay_us(1000*(x))
193 #define udelay(x) DELAY(x)
194 #define msleep(x) DELAY(1000*(x))
195 #define usleep_range(min, max) msleep(DIV_ROUND_UP(min, 1000))
196
197 #endif /* _I40E_OSDEP_H_ */