1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017 Huawei Technologies Co., Ltd 3 */ 4 5 #ifndef _HINIC_COMPAT_H_ 6 #define _HINIC_COMPAT_H_ 7 8 #include <stdint.h> 9 #include <sys/time.h> 10 #include <unistd.h> 11 #include <pthread.h> 12 #include <rte_common.h> 13 #include <rte_bitops.h> 14 #include <rte_byteorder.h> 15 #include <rte_memzone.h> 16 #include <rte_memcpy.h> 17 #include <rte_malloc.h> 18 #include <rte_atomic.h> 19 #include <rte_spinlock.h> 20 #include <rte_cycles.h> 21 #include <rte_log.h> 22 23 typedef uint8_t u8; 24 typedef int8_t s8; 25 typedef uint16_t u16; 26 typedef uint32_t u32; 27 typedef int32_t s32; 28 typedef uint64_t u64; 29 30 #ifndef dma_addr_t 31 typedef uint64_t dma_addr_t; 32 #endif 33 34 #ifndef gfp_t 35 #define gfp_t unsigned 36 #endif 37 38 #ifndef bool 39 #define bool int 40 #endif 41 42 #ifndef FALSE 43 #define FALSE (0) 44 #endif 45 46 #ifndef TRUE 47 #define TRUE (1) 48 #endif 49 50 #ifndef false 51 #define false (0) 52 #endif 53 54 #ifndef true 55 #define true (1) 56 #endif 57 58 #ifndef NULL 59 #define NULL ((void *)0) 60 #endif 61 62 #define HINIC_ERROR (-1) 63 #define HINIC_OK (0) 64 65 #ifndef BIT 66 #define BIT(n) (1 << (n)) 67 #endif 68 69 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 70 #define lower_32_bits(n) ((u32)(n)) 71 72 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */ 73 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y)) 74 75 /* Returns X rounded up to the nearest multiple of Y. */ 76 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y)) 77 78 #undef ALIGN 79 #define ALIGN(x, a) RTE_ALIGN(x, a) 80 81 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 82 83 extern int hinic_logtype; 84 #define RTE_LOGTYPE_NET_HINIC hinic_logtype 85 86 #define PMD_DRV_LOG(level, ...) \ 87 RTE_LOG_LINE(level, NET_HINIC, __VA_ARGS__) 88 89 /* common definition */ 90 #ifndef ETH_ALEN 91 #define ETH_ALEN 6 92 #endif 93 #define ETH_HLEN 14 94 #define ETH_CRC_LEN 4 95 #define VLAN_PRIO_SHIFT 13 96 #define VLAN_N_VID 4096 97 98 /* bit order interface */ 99 #define cpu_to_be16(o) rte_cpu_to_be_16(o) 100 #define cpu_to_be32(o) rte_cpu_to_be_32(o) 101 #define cpu_to_be64(o) rte_cpu_to_be_64(o) 102 #define cpu_to_le32(o) rte_cpu_to_le_32(o) 103 #define be16_to_cpu(o) rte_be_to_cpu_16(o) 104 #define be32_to_cpu(o) rte_be_to_cpu_32(o) 105 #define be64_to_cpu(o) rte_be_to_cpu_64(o) 106 #define le32_to_cpu(o) rte_le_to_cpu_32(o) 107 108 /* virt memory and dma phy memory */ 109 #define __iomem 110 #define GFP_KERNEL RTE_MEMZONE_IOVA_CONTIG 111 #define HINIC_PAGE_SHIFT 12 112 #define HINIC_PAGE_SIZE RTE_PGSIZE_4K 113 #define HINIC_MEM_ALLOC_ALIGN_MIN 8 114 115 #define HINIC_PAGE_SIZE_DPDK 6 116 117 void *dma_zalloc_coherent(void *dev, size_t size, dma_addr_t *dma_handle, 118 unsigned int socket_id); 119 120 void *dma_zalloc_coherent_aligned(void *hwdev, size_t size, 121 dma_addr_t *dma_handle, unsigned int socket_id); 122 123 void *dma_zalloc_coherent_aligned256k(void *hwdev, size_t size, 124 dma_addr_t *dma_handle, unsigned int socket_id); 125 126 void dma_free_coherent(void *dev, size_t size, void *virt, dma_addr_t phys); 127 128 /* dma pool alloc and free */ 129 #define pci_pool dma_pool 130 #define pci_pool_alloc(pool, handle) dma_pool_alloc(pool, handle) 131 #define pci_pool_free(pool, vaddr, addr) dma_pool_free(pool, vaddr, addr) 132 133 struct dma_pool *dma_pool_create(const char *name, void *dev, size_t size, 134 size_t align, size_t boundary); 135 void dma_pool_destroy(struct dma_pool *pool); 136 void *dma_pool_alloc(struct pci_pool *pool, dma_addr_t *dma_addr); 137 void dma_pool_free(struct pci_pool *pool, void *vaddr, dma_addr_t dma); 138 139 #define kzalloc(size, flag) rte_zmalloc(NULL, size, HINIC_MEM_ALLOC_ALIGN_MIN) 140 #define kzalloc_aligned(size, flag) rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE) 141 #define kfree(ptr) rte_free(ptr) 142 143 /* mmio interface */ 144 static inline void writel(u32 value, volatile void *addr) 145 { 146 *(volatile u32 *)addr = value; 147 } 148 149 static inline u32 readl(const volatile void *addr) 150 { 151 return *(const volatile u32 *)addr; 152 } 153 154 #define __raw_writel(value, reg) writel((value), (reg)) 155 #define __raw_readl(reg) readl((reg)) 156 157 /* Spinlock related interface */ 158 #define hinic_spinlock_t rte_spinlock_t 159 160 #define spinlock_t rte_spinlock_t 161 #define spin_lock_init(spinlock_prt) rte_spinlock_init(spinlock_prt) 162 #define spin_lock_deinit(lock) 163 #define spin_lock(spinlock_prt) rte_spinlock_lock(spinlock_prt) 164 #define spin_unlock(spinlock_prt) rte_spinlock_unlock(spinlock_prt) 165 166 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */ 167 #define CLOCK_TYPE CLOCK_MONOTONIC_RAW 168 #else 169 #define CLOCK_TYPE CLOCK_MONOTONIC 170 #endif 171 #define HINIC_MUTEX_TIMEOUT 10 172 173 static inline unsigned long clock_gettime_ms(void) 174 { 175 struct timespec tv; 176 177 (void)clock_gettime(CLOCK_TYPE, &tv); 178 179 return (unsigned long)tv.tv_sec * 1000 + 180 (unsigned long)tv.tv_nsec / 1000000; 181 } 182 183 #define jiffies clock_gettime_ms() 184 #define msecs_to_jiffies(ms) (ms) 185 #define time_before(now, end) ((now) < (end)) 186 187 /* misc kernel utils */ 188 static inline u16 ilog2(u32 n) 189 { 190 u16 res = 0; 191 192 while (n > 1) { 193 n >>= 1; 194 res++; 195 } 196 197 return res; 198 } 199 200 static inline int hinic_mutex_init(pthread_mutex_t *pthreadmutex, 201 const pthread_mutexattr_t *mattr) 202 { 203 return pthread_mutex_init(pthreadmutex, mattr); 204 } 205 206 static inline int hinic_mutex_destroy(pthread_mutex_t *pthreadmutex) 207 { 208 int err; 209 210 err = pthread_mutex_destroy(pthreadmutex); 211 if (unlikely(err)) 212 PMD_DRV_LOG(ERR, "Fail to destroy mutex, error: %d", err); 213 214 return err; 215 } 216 217 static inline int hinic_mutex_lock(pthread_mutex_t *pthreadmutex) 218 { 219 int err; 220 struct timespec tout; 221 222 (void)clock_gettime(CLOCK_TYPE, &tout); 223 224 tout.tv_sec += HINIC_MUTEX_TIMEOUT; 225 err = pthread_mutex_timedlock(pthreadmutex, &tout); 226 if (err) 227 PMD_DRV_LOG(ERR, "Mutex lock failed. (ErrorNo=%d)", err); 228 229 return err; 230 } 231 232 static inline int hinic_mutex_unlock(pthread_mutex_t *pthreadmutex) 233 { 234 return pthread_mutex_unlock(pthreadmutex); 235 } 236 237 #endif /* _HINIC_COMPAT_H_ */ 238