1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2015-2020 Amazon.com, Inc. or its affiliates. 3 * All rights reserved. 4 */ 5 6 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_ 7 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_ 8 9 #include <stdbool.h> 10 #include <stdlib.h> 11 #include <pthread.h> 12 #include <stdint.h> 13 #include <inttypes.h> 14 #include <string.h> 15 #include <errno.h> 16 17 #include <ethdev_driver.h> 18 #include <rte_atomic.h> 19 #include <rte_branch_prediction.h> 20 #include <rte_cycles.h> 21 #include <rte_io.h> 22 #include <rte_log.h> 23 #include <rte_malloc.h> 24 #include <rte_memzone.h> 25 #include <rte_prefetch.h> 26 #include <rte_spinlock.h> 27 28 #include <sys/time.h> 29 #include <rte_memcpy.h> 30 31 typedef uint64_t u64; 32 typedef uint32_t u32; 33 typedef uint16_t u16; 34 typedef uint8_t u8; 35 36 typedef struct rte_eth_dev ena_netdev; 37 typedef uint64_t dma_addr_t; 38 39 #ifndef ETIME 40 #define ETIME ETIMEDOUT 41 #endif 42 43 #define ENA_PRIU64 PRIu64 44 #define ena_atomic32_t rte_atomic32_t 45 #define ena_mem_handle_t const struct rte_memzone * 46 47 #define SZ_256 (256U) 48 #define SZ_4K (4096U) 49 50 #define ENA_COM_OK 0 51 #define ENA_COM_NO_MEM -ENOMEM 52 #define ENA_COM_INVAL -EINVAL 53 #define ENA_COM_NO_SPACE -ENOSPC 54 #define ENA_COM_NO_DEVICE -ENODEV 55 #define ENA_COM_TIMER_EXPIRED -ETIME 56 #define ENA_COM_FAULT -EFAULT 57 #define ENA_COM_TRY_AGAIN -EAGAIN 58 #define ENA_COM_UNSUPPORTED -EOPNOTSUPP 59 #define ENA_COM_EIO -EIO 60 #define ENA_COM_DEVICE_BUSY -EBUSY 61 62 #define ____cacheline_aligned __rte_cache_aligned 63 64 #define ENA_CDESC_RING_SIZE_ALIGNMENT (1 << 12) /* 4K */ 65 66 #define ENA_ABORT() abort() 67 68 #define ENA_MSLEEP(x) rte_delay_us_sleep(x * 1000) 69 #define ENA_USLEEP(x) rte_delay_us_sleep(x) 70 #define ENA_UDELAY(x) rte_delay_us_block(x) 71 72 #define ENA_TOUCH(x) ((void)(x)) 73 /* Redefine memcpy with caution: rte_memcpy can be simply aliased to memcpy, so 74 * make the redefinition only if it's safe (and beneficial) to do so. 75 */ 76 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64_MEMCPY) || \ 77 defined(RTE_ARCH_ARM_NEON_MEMCPY) 78 #undef memcpy 79 #define memcpy rte_memcpy 80 #endif 81 #define wmb rte_wmb 82 #define rmb rte_rmb 83 #define mb rte_mb 84 #define mmiowb rte_io_wmb 85 #define __iomem 86 87 #ifndef READ_ONCE 88 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var)))) 89 #endif 90 91 #define READ_ONCE8(var) READ_ONCE(var) 92 #define READ_ONCE16(var) READ_ONCE(var) 93 #define READ_ONCE32(var) READ_ONCE(var) 94 95 #define US_PER_S 1000000 96 #define ENA_GET_SYSTEM_USECS() \ 97 (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz()) 98 99 extern int ena_logtype_com; 100 101 #define ENA_MAX_T(type, x, y) RTE_MAX((type)(x), (type)(y)) 102 #define ENA_MAX32(x, y) ENA_MAX_T(uint32_t, (x), (y)) 103 #define ENA_MAX16(x, y) ENA_MAX_T(uint16_t, (x), (y)) 104 #define ENA_MAX8(x, y) ENA_MAX_T(uint8_t, (x), (y)) 105 #define ENA_MIN_T(type, x, y) RTE_MIN((type)(x), (type)(y)) 106 #define ENA_MIN32(x, y) ENA_MIN_T(uint32_t, (x), (y)) 107 #define ENA_MIN16(x, y) ENA_MIN_T(uint16_t, (x), (y)) 108 #define ENA_MIN8(x, y) ENA_MIN_T(uint8_t, (x), (y)) 109 110 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8) 111 #define U64_C(x) x ## ULL 112 #define BIT(nr) RTE_BIT32(nr) 113 #define BIT64(nr) RTE_BIT64(nr) 114 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8) 115 #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) 116 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \ 117 (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h)))) 118 119 #define ena_trc_log(dev, level, fmt, arg...) \ 120 ( \ 121 ENA_TOUCH(dev), \ 122 rte_log(RTE_LOG_ ## level, ena_logtype_com, \ 123 "[ENA_COM: %s]" fmt, __func__, ##arg) \ 124 ) 125 126 #define ena_trc_dbg(dev, format, arg...) ena_trc_log(dev, DEBUG, format, ##arg) 127 #define ena_trc_info(dev, format, arg...) ena_trc_log(dev, INFO, format, ##arg) 128 #define ena_trc_warn(dev, format, arg...) ena_trc_log(dev, WARNING, format, ##arg) 129 #define ena_trc_err(dev, format, arg...) ena_trc_log(dev, ERR, format, ##arg) 130 131 #define ENA_WARN(cond, dev, format, arg...) \ 132 do { \ 133 if (unlikely(cond)) { \ 134 ena_trc_err(dev, \ 135 "Warn failed on %s:%s:%d:" format, \ 136 __FILE__, __func__, __LINE__, ##arg); \ 137 } \ 138 } while (0) 139 140 /* Spinlock related methods */ 141 #define ena_spinlock_t rte_spinlock_t 142 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&(spinlock)) 143 #define ENA_SPINLOCK_LOCK(spinlock, flags) \ 144 __extension__ ({(void)(flags); rte_spinlock_lock(&(spinlock)); }) 145 #define ENA_SPINLOCK_UNLOCK(spinlock, flags) \ 146 __extension__ ({(void)(flags); rte_spinlock_unlock(&(spinlock)); }) 147 #define ENA_SPINLOCK_DESTROY(spinlock) ((void)(spinlock)) 148 149 typedef struct { 150 pthread_cond_t cond; 151 pthread_mutex_t mutex; 152 uint8_t flag; 153 } ena_wait_event_t; 154 155 #define ENA_WAIT_EVENT_INIT(waitevent) \ 156 do { \ 157 ena_wait_event_t *_we = &(waitevent); \ 158 pthread_mutex_init(&_we->mutex, NULL); \ 159 pthread_cond_init(&_we->cond, NULL); \ 160 _we->flag = 0; \ 161 } while (0) 162 163 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout) \ 164 do { \ 165 ena_wait_event_t *_we = &(waitevent); \ 166 typeof(timeout) _tmo = (timeout); \ 167 int ret = 0; \ 168 struct timespec wait; \ 169 struct timeval now; \ 170 unsigned long timeout_us; \ 171 gettimeofday(&now, NULL); \ 172 wait.tv_sec = now.tv_sec + _tmo / 1000000UL; \ 173 timeout_us = _tmo % 1000000UL; \ 174 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL; \ 175 pthread_mutex_lock(&_we->mutex); \ 176 while (ret == 0 && !_we->flag) { \ 177 ret = pthread_cond_timedwait(&_we->cond, \ 178 &_we->mutex, &wait); \ 179 } \ 180 /* Asserts only if not working on ena_wait_event_t */ \ 181 if (unlikely(ret != 0 && ret != ETIMEDOUT)) \ 182 ena_trc_err(NULL, \ 183 "Invalid wait event. pthread ret: %d\n", ret); \ 184 else if (unlikely(ret == ETIMEDOUT)) \ 185 ena_trc_err(NULL, \ 186 "Timeout waiting for " #waitevent "\n"); \ 187 _we->flag = 0; \ 188 pthread_mutex_unlock(&_we->mutex); \ 189 } while (0) 190 #define ENA_WAIT_EVENT_SIGNAL(waitevent) \ 191 do { \ 192 ena_wait_event_t *_we = &(waitevent); \ 193 pthread_mutex_lock(&_we->mutex); \ 194 _we->flag = 1; \ 195 pthread_cond_signal(&_we->cond); \ 196 pthread_mutex_unlock(&_we->mutex); \ 197 } while (0) 198 /* pthread condition doesn't need to be rearmed after usage */ 199 #define ENA_WAIT_EVENT_CLEAR(...) 200 #define ENA_WAIT_EVENT_DESTROY(waitevent) ((void)(waitevent)) 201 202 #define ENA_MIGHT_SLEEP() 203 204 #define ena_time_t uint64_t 205 #define ena_time_high_res_t uint64_t 206 207 /* Note that high resolution timers are not used by the ENA PMD for now. 208 * Although these macro definitions compile, it shall fail the 209 * compilation in case the unimplemented API is called prematurely. 210 */ 211 #define ENA_TIME_EXPIRE(timeout) ((timeout) < rte_get_timer_cycles()) 212 #define ENA_TIME_EXPIRE_HIGH_RES(timeout) (RTE_SET_USED(timeout), 0) 213 #define ENA_TIME_INIT_HIGH_RES() 0 214 #define ENA_TIME_COMPARE_HIGH_RES(time1, time2) (RTE_SET_USED(time1), RTE_SET_USED(time2), 0) 215 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \ 216 ((timeout_us) * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles()) 217 #define ENA_GET_SYSTEM_TIMEOUT_HIGH_RES(current_time, timeout_us) \ 218 (RTE_SET_USED(current_time), RTE_SET_USED(timeout_us), 0) 219 #define ENA_GET_SYSTEM_TIME_HIGH_RES() 0 220 221 const struct rte_memzone * 222 ena_mem_alloc_coherent(struct rte_eth_dev_data *data, size_t size, 223 int socket_id, unsigned int alignment, void **virt_addr, 224 dma_addr_t *phys_addr); 225 226 #define ENA_MEM_ALLOC_COHERENT_ALIGNED( \ 227 dmadev, size, virt, phys, mem_handle, alignment) \ 228 do { \ 229 void *virt_addr; \ 230 dma_addr_t phys_addr; \ 231 (mem_handle) = ena_mem_alloc_coherent((dmadev), (size), \ 232 SOCKET_ID_ANY, (alignment), &virt_addr, &phys_addr); \ 233 (virt) = virt_addr; \ 234 (phys) = phys_addr; \ 235 } while (0) 236 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, mem_handle) \ 237 ENA_MEM_ALLOC_COHERENT_ALIGNED(dmadev, size, virt, phys, \ 238 mem_handle, RTE_CACHE_LINE_SIZE) 239 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, mem_handle) \ 240 __extension__ ({ ENA_TOUCH(size); ENA_TOUCH(phys); ENA_TOUCH(dmadev); \ 241 rte_memzone_free(mem_handle); }) 242 243 #define ENA_MEM_ALLOC_COHERENT_NODE_ALIGNED( \ 244 dmadev, size, virt, phys, mem_handle, node, dev_node, alignment) \ 245 do { \ 246 void *virt_addr; \ 247 dma_addr_t phys_addr; \ 248 ENA_TOUCH(dev_node); \ 249 (mem_handle) = ena_mem_alloc_coherent((dmadev), (size), \ 250 (node), (alignment), &virt_addr, &phys_addr); \ 251 (virt) = virt_addr; \ 252 (phys) = phys_addr; \ 253 } while (0) 254 #define ENA_MEM_ALLOC_COHERENT_NODE( \ 255 dmadev, size, virt, phys, mem_handle, node, dev_node) \ 256 ENA_MEM_ALLOC_COHERENT_NODE_ALIGNED(dmadev, size, virt, phys, \ 257 mem_handle, node, dev_node, RTE_CACHE_LINE_SIZE) 258 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \ 259 do { \ 260 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node); \ 261 virt = rte_zmalloc_socket(NULL, size, 0, node); \ 262 } while (0) 263 264 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1) 265 #define ENA_MEM_FREE(dmadev, ptr, size) \ 266 __extension__ ({ ENA_TOUCH(dmadev); ENA_TOUCH(size); rte_free(ptr); }) 267 268 #define ENA_DB_SYNC(mem_handle) ((void)mem_handle) 269 270 #define ENA_REG_WRITE32(bus, value, reg) \ 271 __extension__ ({ (void)(bus); rte_write32((value), (reg)); }) 272 #define ENA_REG_WRITE32_RELAXED(bus, value, reg) \ 273 __extension__ ({ (void)(bus); rte_write32_relaxed((value), (reg)); }) 274 #define ENA_REG_READ32(bus, reg) \ 275 __extension__ ({ (void)(bus); rte_read32_relaxed((reg)); }) 276 277 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr) 278 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr) 279 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val) 280 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr) 281 282 #define msleep(x) rte_delay_us(x * 1000) 283 #define udelay(x) rte_delay_us(x) 284 285 #define dma_rmb() rmb() 286 287 #define MAX_ERRNO 4095 288 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO) 289 #define ERR_PTR(error) ((void *)(long)error) 290 #define PTR_ERR(error) ((long)(void *)error) 291 #define might_sleep() 292 293 #define prefetch(x) rte_prefetch0(x) 294 #define prefetchw(x) rte_prefetch0_write(x) 295 296 #define lower_32_bits(x) ((uint32_t)(x)) 297 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16)) 298 299 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us) \ 300 ((timeout_us) * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles()) 301 #define ENA_WAIT_EVENTS_DESTROY(admin_queue) ((void)(admin_queue)) 302 303 /* The size must be 8 byte align */ 304 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size) \ 305 do { \ 306 int count, i; \ 307 uint64_t *to = (uint64_t *)(dst); \ 308 const uint64_t *from = (const uint64_t *)(src); \ 309 count = (size) / 8; \ 310 for (i = 0; i < count; i++, from++, to++) \ 311 rte_write64_relaxed(*from, to); \ 312 } while(0) 313 314 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 315 316 #define ENA_FFS(x) ffs(x) 317 318 void ena_rss_key_fill(void *key, size_t size); 319 320 #define ENA_RSS_FILL_KEY(key, size) ena_rss_key_fill(key, size) 321 322 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS_PLAT 0 323 #define ENA_INTR_INITIAL_RX_INTERVAL_USECS_PLAT 0 324 325 #include "ena_includes.h" 326 327 #define ENA_BITS_PER_U64(bitmap) (ena_bits_per_u64(bitmap)) 328 329 #define ENA_FIELD_GET(value, mask, offset) (((value) & (mask)) >> (offset)) 330 331 static __rte_always_inline int ena_bits_per_u64(uint64_t bitmap) 332 { 333 int count = 0; 334 335 while (bitmap) { 336 bitmap &= (bitmap - 1); 337 count++; 338 } 339 340 return count; 341 } 342 343 344 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */ 345