4 * Copyright (c) 2015-2016 Amazon.com, Inc. or its affiliates.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_
35 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_
44 #include <rte_atomic.h>
45 #include <rte_branch_prediction.h>
46 #include <rte_cycles.h>
48 #include <rte_malloc.h>
49 #include <rte_memzone.h>
50 #include <rte_spinlock.h>
59 typedef uint64_t dma_addr_t;
61 #define ena_atomic32_t rte_atomic32_t
62 #define ena_mem_handle_t void *
68 #define ENA_COM_NO_MEM -ENOMEM
69 #define ENA_COM_INVAL -EINVAL
70 #define ENA_COM_NO_SPACE -ENOSPC
71 #define ENA_COM_NO_DEVICE -ENODEV
72 #define ENA_COM_PERMISSION -EPERM
73 #define ENA_COM_TIMER_EXPIRED -ETIME
74 #define ENA_COM_FAULT -EFAULT
76 #define ____cacheline_aligned __rte_cache_aligned
78 #define ENA_ABORT() abort()
80 #define ENA_MSLEEP(x) rte_delay_ms(x)
81 #define ENA_UDELAY(x) rte_delay_us(x)
83 #define memcpy_toio memcpy
89 #define US_PER_S 1000000
90 #define ENA_GET_SYSTEM_USECS() \
91 (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
93 #define ENA_ASSERT(cond, format, arg...) \
95 if (unlikely(!(cond))) { \
96 printf("Assertion failed on %s:%s:%d: " format, \
97 __FILE__, __func__, __LINE__, ##arg); \
98 rte_exit(EXIT_FAILURE, "ASSERTION FAILED\n"); \
102 #define ENA_MAX32(x, y) RTE_MAX((x), (y))
103 #define ENA_MAX16(x, y) RTE_MAX((x), (y))
104 #define ENA_MAX8(x, y) RTE_MAX((x), (y))
105 #define ENA_MIN32(x, y) RTE_MIN((x), (y))
106 #define ENA_MIN16(x, y) RTE_MIN((x), (y))
107 #define ENA_MIN8(x, y) RTE_MIN((x), (y))
109 #define U64_C(x) x ## ULL
110 #define BIT(nr) (1UL << (nr))
111 #define BITS_PER_LONG (__SIZEOF_LONG__ * 8)
112 #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
113 #define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
115 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
116 #define ena_trc_dbg(format, arg...) \
117 RTE_LOG(DEBUG, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
118 #define ena_trc_info(format, arg...) \
119 RTE_LOG(INFO, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
120 #define ena_trc_warn(format, arg...) \
121 RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
122 #define ena_trc_err(format, arg...) \
123 RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
125 #define ena_trc_dbg(format, arg...) do { } while (0)
126 #define ena_trc_info(format, arg...) do { } while (0)
127 #define ena_trc_warn(format, arg...) do { } while (0)
128 #define ena_trc_err(format, arg...) do { } while (0)
129 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
131 /* Spinlock related methods */
132 #define ena_spinlock_t rte_spinlock_t
133 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&spinlock)
134 #define ENA_SPINLOCK_LOCK(spinlock, flags) \
135 ({(void)flags; rte_spinlock_lock(&spinlock); })
136 #define ENA_SPINLOCK_UNLOCK(spinlock, flags) \
137 ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
139 #define q_waitqueue_t \
141 pthread_cond_t cond; \
142 pthread_mutex_t mutex; \
145 #define ena_wait_queue_t q_waitqueue_t
147 #define ENA_WAIT_EVENT_INIT(waitqueue) \
149 pthread_mutex_init(&(waitqueue).mutex, NULL); \
150 pthread_cond_init(&(waitqueue).cond, NULL); \
153 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout) \
155 struct timespec wait; \
156 struct timeval now; \
157 unsigned long timeout_us; \
158 gettimeofday(&now, NULL); \
159 wait.tv_sec = now.tv_sec + timeout / 1000000UL; \
160 timeout_us = timeout % 1000000UL; \
161 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL; \
162 pthread_mutex_lock(&waitevent.mutex); \
163 pthread_cond_timedwait(&waitevent.cond, \
164 &waitevent.mutex, &wait); \
165 pthread_mutex_unlock(&waitevent.mutex); \
167 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
168 /* pthread condition doesn't need to be rearmed after usage */
169 #define ENA_WAIT_EVENT_CLEAR(...)
171 #define ena_wait_event_t ena_wait_queue_t
172 #define ENA_MIGHT_SLEEP()
174 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle) \
176 const struct rte_memzone *mz; \
177 char z_name[RTE_MEMZONE_NAMESIZE]; \
178 (void)dmadev; (void)handle; \
179 snprintf(z_name, sizeof(z_name), \
180 "ena_alloc_%d", ena_alloc_cnt++); \
181 mz = rte_memzone_reserve(z_name, size, SOCKET_ID_ANY, 0); \
183 phys = mz->phys_addr; \
185 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle) \
186 ({(void)size; rte_free(virt); })
187 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
188 #define ENA_MEM_FREE(dmadev, ptr) ({(void)dmadev; rte_free(ptr); })
190 static inline void writel(u32 value, volatile void *addr)
192 *(volatile u32 *)addr = value;
195 static inline u32 readl(const volatile void *addr)
197 return *(const volatile u32 *)addr;
200 #define ENA_REG_WRITE32(value, reg) writel((value), (reg))
201 #define ENA_REG_READ32(reg) readl((reg))
203 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
204 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
205 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
206 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
208 #define msleep(x) rte_delay_us(x * 1000)
209 #define udelay(x) rte_delay_us(x)
211 #define MAX_ERRNO 4095
212 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
213 #define ERR_PTR(error) ((void *)(long)error)
214 #define PTR_ERR(error) ((long)(void *)error)
215 #define might_sleep()
217 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */