1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017-2021 Intel Corporation 3 */ 4 5 #ifndef _IAVF_OSDEP_H_ 6 #define _IAVF_OSDEP_H_ 7 8 #include <string.h> 9 #include <stdint.h> 10 #include <stdbool.h> 11 #include <stdio.h> 12 #include <stdarg.h> 13 14 #include <rte_common.h> 15 #include <rte_memcpy.h> 16 #include <rte_memzone.h> 17 #include <rte_malloc.h> 18 #include <rte_byteorder.h> 19 #include <rte_cycles.h> 20 #include <rte_spinlock.h> 21 #include <rte_log.h> 22 #include <rte_io.h> 23 24 #ifndef __INTEL_NET_BASE_OSDEP__ 25 #define __INTEL_NET_BASE_OSDEP__ 26 27 #define INLINE inline 28 #define STATIC static 29 30 typedef uint8_t u8; 31 typedef int8_t s8; 32 typedef uint16_t u16; 33 typedef int16_t s16; 34 typedef uint32_t u32; 35 typedef int32_t s32; 36 typedef uint64_t u64; 37 typedef uint64_t s64; 38 39 #ifndef __le16 40 #define __le16 uint16_t 41 #endif 42 #ifndef __le32 43 #define __le32 uint32_t 44 #endif 45 #ifndef __le64 46 #define __le64 uint64_t 47 #endif 48 #ifndef __be16 49 #define __be16 uint16_t 50 #endif 51 #ifndef __be32 52 #define __be32 uint32_t 53 #endif 54 #ifndef __be64 55 #define __be64 uint64_t 56 #endif 57 58 /* Avoid macro redefinition warning on Windows */ 59 #ifdef RTE_EXEC_ENV_WINDOWS 60 #ifdef min 61 #undef min 62 #endif 63 #ifdef max 64 #undef max 65 #endif 66 #endif 67 #define min(a, b) RTE_MIN(a, b) 68 #define max(a, b) RTE_MAX(a, b) 69 70 #define FIELD_SIZEOF(t, f) RTE_SIZEOF_FIELD(t, f) 71 #define ARRAY_SIZE(arr) RTE_DIM(arr) 72 73 #define CPU_TO_LE16(o) rte_cpu_to_le_16(o) 74 #define CPU_TO_LE32(s) rte_cpu_to_le_32(s) 75 #define CPU_TO_LE64(h) rte_cpu_to_le_64(h) 76 #define LE16_TO_CPU(a) rte_le_to_cpu_16(a) 77 #define LE32_TO_CPU(c) rte_le_to_cpu_32(c) 78 #define LE64_TO_CPU(k) rte_le_to_cpu_64(k) 79 80 #define CPU_TO_BE16(o) rte_cpu_to_be_16(o) 81 #define CPU_TO_BE32(o) rte_cpu_to_be_32(o) 82 #define CPU_TO_BE64(o) rte_cpu_to_be_64(o) 83 84 #define NTOHS(a) rte_be_to_cpu_16(a) 85 #define NTOHL(a) rte_be_to_cpu_32(a) 86 #define HTONS(a) rte_cpu_to_be_16(a) 87 #define HTONL(a) rte_cpu_to_be_32(a) 88 89 static __rte_always_inline uint32_t 90 readl(volatile void *addr) 91 { 92 return rte_le_to_cpu_32(rte_read32(addr)); 93 } 94 95 static __rte_always_inline void 96 writel(uint32_t value, volatile void *addr) 97 { 98 rte_write32(rte_cpu_to_le_32(value), addr); 99 } 100 101 static __rte_always_inline void 102 writel_relaxed(uint32_t value, volatile void *addr) 103 { 104 rte_write32_relaxed(rte_cpu_to_le_32(value), addr); 105 } 106 107 static __rte_always_inline uint64_t 108 readq(volatile void *addr) 109 { 110 return rte_le_to_cpu_64(rte_read64(addr)); 111 } 112 113 static __rte_always_inline void 114 writeq(uint64_t value, volatile void *addr) 115 { 116 rte_write64(rte_cpu_to_le_64(value), addr); 117 } 118 119 #define wr32(a, reg, value) writel((value), (a)->hw_addr + (reg)) 120 #define rd32(a, reg) readl((a)->hw_addr + (reg)) 121 #define wr64(a, reg, value) writeq((value), (a)->hw_addr + (reg)) 122 #define rd64(a, reg) readq((a)->hw_addr + (reg)) 123 124 #endif /* __INTEL_NET_BASE_OSDEP__ */ 125 126 #define iavf_memset(a, b, c, d) memset((a), (b), (c)) 127 #define iavf_memcpy(a, b, c, d) rte_memcpy((a), (b), (c)) 128 129 #define iavf_usec_delay(x) rte_delay_us_sleep(x) 130 #define iavf_msec_delay(x) iavf_usec_delay(1000 * (x)) 131 132 #define IAVF_PCI_REG_WRITE(reg, value) writel(value, reg) 133 #define IAVF_PCI_REG_WRITE_RELAXED(reg, value) writel_relaxed(value, reg) 134 135 #define IAVF_PCI_REG_WC_WRITE(reg, value) \ 136 rte_write32_wc((rte_cpu_to_le_32(value)), reg) 137 #define IAVF_PCI_REG_WC_WRITE_RELAXED(reg, value) \ 138 rte_write32_wc_relaxed((rte_cpu_to_le_32(value)), reg) 139 140 #define IAVF_READ_REG(hw, reg) rd32(hw, reg) 141 #define IAVF_WRITE_REG(hw, reg, value) wr32(hw, reg, value) 142 143 #define IAVF_WRITE_FLUSH(a) IAVF_READ_REG(a, IAVF_VFGEN_RSTAT) 144 145 extern int iavf_common_logger; 146 147 #define DEBUGOUT(S) rte_log(RTE_LOG_DEBUG, iavf_common_logger, S) 148 #define DEBUGOUT2(S, A...) rte_log(RTE_LOG_DEBUG, iavf_common_logger, S, ##A) 149 #define DEBUGFUNC(F) DEBUGOUT(F "\n") 150 151 #define iavf_debug(h, m, s, ...) \ 152 do { \ 153 if (((m) & (h)->debug_mask)) \ 154 rte_log(RTE_LOG_DEBUG, iavf_common_logger, \ 155 "iavf %02x.%x " s, \ 156 (h)->bus.device, (h)->bus.func, \ 157 ##__VA_ARGS__); \ 158 } while (0) 159 160 /* memory allocation tracking */ 161 struct iavf_dma_mem { 162 void *va; 163 u64 pa; 164 u32 size; 165 const void *zone; 166 } __rte_packed; 167 168 struct iavf_virt_mem { 169 void *va; 170 u32 size; 171 } __rte_packed; 172 173 /* SW spinlock */ 174 struct iavf_spinlock { 175 rte_spinlock_t spinlock; 176 }; 177 178 #define iavf_allocate_dma_mem(h, m, unused, s, a) \ 179 iavf_allocate_dma_mem_d(h, m, s, a) 180 #define iavf_free_dma_mem(h, m) iavf_free_dma_mem_d(h, m) 181 182 #define iavf_allocate_virt_mem(h, m, s) iavf_allocate_virt_mem_d(h, m, s) 183 #define iavf_free_virt_mem(h, m) iavf_free_virt_mem_d(h, m) 184 185 static inline void 186 iavf_init_spinlock_d(struct iavf_spinlock *sp) 187 { 188 rte_spinlock_init(&sp->spinlock); 189 } 190 191 static inline void 192 iavf_acquire_spinlock_d(struct iavf_spinlock *sp) 193 { 194 rte_spinlock_lock(&sp->spinlock); 195 } 196 197 static inline void 198 iavf_release_spinlock_d(struct iavf_spinlock *sp) 199 { 200 rte_spinlock_unlock(&sp->spinlock); 201 } 202 203 static inline void 204 iavf_destroy_spinlock_d(__rte_unused struct iavf_spinlock *sp) 205 { 206 } 207 208 #define iavf_init_spinlock(_sp) iavf_init_spinlock_d(_sp) 209 #define iavf_acquire_spinlock(_sp) iavf_acquire_spinlock_d(_sp) 210 #define iavf_release_spinlock(_sp) iavf_release_spinlock_d(_sp) 211 #define iavf_destroy_spinlock(_sp) iavf_destroy_spinlock_d(_sp) 212 213 #endif /* _IAVF_OSDEP_H_ */ 214