1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017 Huawei Technologies Co., Ltd
5 #ifndef _HINIC_COMPAT_H_
6 #define _HINIC_COMPAT_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>
31 typedef uint64_t dma_addr_t;
35 #define gfp_t unsigned
59 #define NULL ((void *)0)
62 #define HINIC_ERROR (-1)
66 #define BIT(n) (1 << (n))
69 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
70 #define lower_32_bits(n) ((u32)(n))
72 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
73 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
75 /* Returns X rounded up to the nearest multiple of Y. */
76 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
79 #define ALIGN(x, a) RTE_ALIGN(x, a)
81 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
83 /* Reported driver name. */
84 #define HINIC_DRIVER_NAME "net_hinic"
86 extern int hinic_logtype;
88 #define PMD_DRV_LOG(level, fmt, args...) \
89 rte_log(RTE_LOG_ ## level, hinic_logtype, \
90 HINIC_DRIVER_NAME": " fmt "\n", ##args)
92 /* common definition */
98 #define VLAN_PRIO_SHIFT 13
99 #define VLAN_N_VID 4096
101 /* bit order interface */
102 #define cpu_to_be16(o) rte_cpu_to_be_16(o)
103 #define cpu_to_be32(o) rte_cpu_to_be_32(o)
104 #define cpu_to_be64(o) rte_cpu_to_be_64(o)
105 #define cpu_to_le32(o) rte_cpu_to_le_32(o)
106 #define be16_to_cpu(o) rte_be_to_cpu_16(o)
107 #define be32_to_cpu(o) rte_be_to_cpu_32(o)
108 #define be64_to_cpu(o) rte_be_to_cpu_64(o)
109 #define le32_to_cpu(o) rte_le_to_cpu_32(o)
111 /* virt memory and dma phy memory */
113 #define GFP_KERNEL RTE_MEMZONE_IOVA_CONTIG
114 #define HINIC_PAGE_SHIFT 12
115 #define HINIC_PAGE_SIZE RTE_PGSIZE_4K
116 #define HINIC_MEM_ALLOC_ALIGN_MIN 8
118 #define HINIC_PAGE_SIZE_DPDK 6
120 void *dma_zalloc_coherent(void *dev, size_t size, dma_addr_t *dma_handle,
121 unsigned int socket_id);
123 void *dma_zalloc_coherent_aligned(void *hwdev, size_t size,
124 dma_addr_t *dma_handle, unsigned int socket_id);
126 void *dma_zalloc_coherent_aligned256k(void *hwdev, size_t size,
127 dma_addr_t *dma_handle, unsigned int socket_id);
129 void dma_free_coherent(void *dev, size_t size, void *virt, dma_addr_t phys);
131 /* dma pool alloc and free */
132 #define pci_pool dma_pool
133 #define pci_pool_alloc(pool, handle) dma_pool_alloc(pool, handle)
134 #define pci_pool_free(pool, vaddr, addr) dma_pool_free(pool, vaddr, addr)
136 struct dma_pool *dma_pool_create(const char *name, void *dev, size_t size,
137 size_t align, size_t boundary);
138 void dma_pool_destroy(struct dma_pool *pool);
139 void *dma_pool_alloc(struct pci_pool *pool, dma_addr_t *dma_addr);
140 void dma_pool_free(struct pci_pool *pool, void *vaddr, dma_addr_t dma);
142 #define kzalloc(size, flag) rte_zmalloc(NULL, size, HINIC_MEM_ALLOC_ALIGN_MIN)
143 #define kzalloc_aligned(size, flag) rte_zmalloc(NULL, size, RTE_CACHE_LINE_SIZE)
144 #define kfree(ptr) rte_free(ptr)
147 static inline void writel(u32 value, volatile void *addr)
149 *(volatile u32 *)addr = value;
152 static inline u32 readl(const volatile void *addr)
154 return *(const volatile u32 *)addr;
157 #define __raw_writel(value, reg) writel((value), (reg))
158 #define __raw_readl(reg) readl((reg))
160 /* Spinlock related interface */
161 #define hinic_spinlock_t rte_spinlock_t
163 #define spinlock_t rte_spinlock_t
164 #define spin_lock_init(spinlock_prt) rte_spinlock_init(spinlock_prt)
165 #define spin_lock_deinit(lock)
166 #define spin_lock(spinlock_prt) rte_spinlock_lock(spinlock_prt)
167 #define spin_unlock(spinlock_prt) rte_spinlock_unlock(spinlock_prt)
169 #ifdef CLOCK_MONOTONIC_RAW /* Defined in glibc bits/time.h */
170 #define CLOCK_TYPE CLOCK_MONOTONIC_RAW
172 #define CLOCK_TYPE CLOCK_MONOTONIC
174 #define HINIC_MUTEX_TIMEOUT 10
176 static inline unsigned long clock_gettime_ms(void)
180 (void)clock_gettime(CLOCK_TYPE, &tv);
182 return (unsigned long)tv.tv_sec * 1000 +
183 (unsigned long)tv.tv_nsec / 1000000;
186 #define jiffies clock_gettime_ms()
187 #define msecs_to_jiffies(ms) (ms)
188 #define time_before(now, end) ((now) < (end))
190 /* misc kernel utils */
191 static inline u16 ilog2(u32 n)
203 static inline int hinic_mutex_init(pthread_mutex_t *pthreadmutex,
204 const pthread_mutexattr_t *mattr)
208 err = pthread_mutex_init(pthreadmutex, mattr);
210 PMD_DRV_LOG(ERR, "Fail to initialize mutex, error: %d", err);
215 static inline int hinic_mutex_destroy(pthread_mutex_t *pthreadmutex)
219 err = pthread_mutex_destroy(pthreadmutex);
221 PMD_DRV_LOG(ERR, "Fail to destroy mutex, error: %d", err);
226 static inline int hinic_mutex_lock(pthread_mutex_t *pthreadmutex)
229 struct timespec tout;
231 (void)clock_gettime(CLOCK_TYPE, &tout);
233 tout.tv_sec += HINIC_MUTEX_TIMEOUT;
234 err = pthread_mutex_timedlock(pthreadmutex, &tout);
236 PMD_DRV_LOG(ERR, "Mutex lock failed. (ErrorNo=%d)", err);
241 static inline int hinic_mutex_unlock(pthread_mutex_t *pthreadmutex)
243 return pthread_mutex_unlock(pthreadmutex);
246 #endif /* _HINIC_COMPAT_H_ */