ba7a098f59ad61546f66c02af2ed269e054dd5d8
[dpdk.git] / drivers / net / ena / base / ena_plat_dpdk.h
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 <rte_atomic.h>
18 #include <rte_branch_prediction.h>
19 #include <rte_cycles.h>
20 #include <rte_io.h>
21 #include <rte_log.h>
22 #include <rte_malloc.h>
23 #include <rte_memzone.h>
24 #include <rte_prefetch.h>
25 #include <rte_spinlock.h>
26
27 #include <sys/time.h>
28
29 typedef uint64_t u64;
30 typedef uint32_t u32;
31 typedef uint16_t u16;
32 typedef uint8_t u8;
33
34 typedef uint64_t dma_addr_t;
35 #ifndef ETIME
36 #define ETIME ETIMEDOUT
37 #endif
38
39 #define ena_atomic32_t rte_atomic32_t
40 #define ena_mem_handle_t const struct rte_memzone *
41
42 #define SZ_256 (256U)
43 #define SZ_4K (4096U)
44
45 #define ENA_COM_OK      0
46 #define ENA_COM_NO_MEM  -ENOMEM
47 #define ENA_COM_INVAL   -EINVAL
48 #define ENA_COM_NO_SPACE        -ENOSPC
49 #define ENA_COM_NO_DEVICE       -ENODEV
50 #define ENA_COM_TIMER_EXPIRED   -ETIME
51 #define ENA_COM_FAULT   -EFAULT
52 #define ENA_COM_TRY_AGAIN       -EAGAIN
53 #define ENA_COM_UNSUPPORTED    -EOPNOTSUPP
54
55 #define ____cacheline_aligned __rte_cache_aligned
56
57 #define ENA_ABORT() abort()
58
59 #define ENA_MSLEEP(x) rte_delay_ms(x)
60 #define ENA_UDELAY(x) rte_delay_us(x)
61
62 #define ENA_TOUCH(x) ((void)(x))
63 #define memcpy_toio memcpy
64 #define wmb rte_wmb
65 #define rmb rte_rmb
66 #define mb rte_mb
67 #define mmiowb rte_io_wmb
68 #define __iomem
69
70 #define US_PER_S 1000000
71 #define ENA_GET_SYSTEM_USECS()                                          \
72         (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
73
74 extern int ena_logtype_com;
75 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
76 #define ENA_ASSERT(cond, format, arg...)                                \
77         do {                                                            \
78                 if (unlikely(!(cond))) {                                \
79                         rte_log(RTE_LOGTYPE_ERR, ena_logtype_com,       \
80                                 format, ##arg);                         \
81                         rte_panic("line %d\tassert \"" #cond "\""       \
82                                         "failed\n", __LINE__);          \
83                 }                                                       \
84         } while (0)
85 #else
86 #define ENA_ASSERT(cond, format, arg...) do {} while (0)
87 #endif
88
89 #define ENA_MAX_T(type, x, y) RTE_MAX((type)(x), (type)(y))
90 #define ENA_MAX32(x, y) ENA_MAX_T(uint32_t, (x), (y))
91 #define ENA_MAX16(x, y) ENA_MAX_T(uint16_t, (x), (y))
92 #define ENA_MAX8(x, y) ENA_MAX_T(uint8_t, (x), (y))
93 #define ENA_MIN_T(type, x, y) RTE_MIN((type)(x), (type)(y))
94 #define ENA_MIN32(x, y) ENA_MIN_T(uint32_t, (x), (y))
95 #define ENA_MIN16(x, y) ENA_MIN_T(uint16_t, (x), (y))
96 #define ENA_MIN8(x, y) ENA_MIN_T(uint8_t, (x), (y))
97
98 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8)
99 #define U64_C(x) x ## ULL
100 #define BIT(nr)         (1UL << (nr))
101 #define BITS_PER_LONG   (__SIZEOF_LONG__ * 8)
102 #define GENMASK(h, l)   (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
103 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \
104                           (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
105
106 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
107 #define ena_trc_log(level, fmt, arg...) \
108         rte_log(RTE_LOG_ ## level, ena_logtype_com, \
109                 "[ENA_COM: %s]" fmt, __func__, ##arg)
110
111 #define ena_trc_dbg(format, arg...)     ena_trc_log(DEBUG, format, ##arg)
112 #define ena_trc_info(format, arg...)    ena_trc_log(INFO, format, ##arg)
113 #define ena_trc_warn(format, arg...)    ena_trc_log(WARNING, format, ##arg)
114 #define ena_trc_err(format, arg...)     ena_trc_log(ERR, format, ##arg)
115 #else
116 #define ena_trc_dbg(format, arg...) do { } while (0)
117 #define ena_trc_info(format, arg...) do { } while (0)
118 #define ena_trc_warn(format, arg...) do { } while (0)
119 #define ena_trc_err(format, arg...) do { } while (0)
120 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
121
122 #define ENA_WARN(cond, format, arg...)                                 \
123 do {                                                                   \
124        if (unlikely(cond)) {                                           \
125                ena_trc_err(                                            \
126                        "Warn failed on %s:%s:%d:" format,              \
127                        __FILE__, __func__, __LINE__, ##arg);           \
128        }                                                               \
129 } while (0)
130
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)); })
138 #define ENA_SPINLOCK_DESTROY(spinlock) ((void)spinlock)
139
140 #define q_waitqueue_t                   \
141         struct {                        \
142                 pthread_cond_t cond;    \
143                 pthread_mutex_t mutex;  \
144         }
145
146 #define ena_wait_queue_t q_waitqueue_t
147
148 #define ENA_WAIT_EVENT_INIT(waitqueue)                                  \
149         do {                                                            \
150                 pthread_mutex_init(&(waitqueue).mutex, NULL);           \
151                 pthread_cond_init(&(waitqueue).cond, NULL);             \
152         } while (0)
153
154 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout)                         \
155         do {                                                            \
156                 struct timespec wait;                                   \
157                 struct timeval now;                                     \
158                 unsigned long timeout_us;                               \
159                 gettimeofday(&now, NULL);                               \
160                 wait.tv_sec = now.tv_sec + timeout / 1000000UL;         \
161                 timeout_us = timeout % 1000000UL;                       \
162                 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL;     \
163                 pthread_mutex_lock(&waitevent.mutex);                   \
164                 pthread_cond_timedwait(&waitevent.cond,                 \
165                                 &waitevent.mutex, &wait);               \
166                 pthread_mutex_unlock(&waitevent.mutex);                 \
167         } while (0)
168 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
169 /* pthread condition doesn't need to be rearmed after usage */
170 #define ENA_WAIT_EVENT_CLEAR(...)
171 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
172
173 #define ena_wait_event_t ena_wait_queue_t
174 #define ENA_MIGHT_SLEEP()
175
176 #define ena_time_t uint64_t
177 #define ENA_TIME_EXPIRE(timeout)  (timeout < rte_get_timer_cycles())
178 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us)                             \
179        (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
180
181 /*
182  * Each rte_memzone should have unique name.
183  * To satisfy it, count number of allocations and add it to name.
184  */
185 extern rte_atomic32_t ena_alloc_cnt;
186
187 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle)        \
188         do {                                                            \
189                 const struct rte_memzone *mz = NULL;                    \
190                 ENA_TOUCH(dmadev); ENA_TOUCH(handle);                   \
191                 if (size > 0) {                                         \
192                         char z_name[RTE_MEMZONE_NAMESIZE];              \
193                         snprintf(z_name, sizeof(z_name),                \
194                          "ena_alloc_%d",                                \
195                          rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
196                         mz = rte_memzone_reserve(z_name, size,          \
197                                         SOCKET_ID_ANY,                  \
198                                         RTE_MEMZONE_IOVA_CONTIG);       \
199                         handle = mz;                                    \
200                 }                                                       \
201                 if (mz == NULL) {                                       \
202                         virt = NULL;                                    \
203                         phys = 0;                                       \
204                 } else {                                                \
205                         memset(mz->addr, 0, size);                      \
206                         virt = mz->addr;                                \
207                         phys = mz->iova;                                \
208                 }                                                       \
209         } while (0)
210 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle)         \
211                 ({ ENA_TOUCH(size); ENA_TOUCH(phys);                    \
212                    ENA_TOUCH(dmadev);                                   \
213                    rte_memzone_free(handle); })
214
215 #define ENA_MEM_ALLOC_COHERENT_NODE(                                    \
216         dmadev, size, virt, phys, mem_handle, node, dev_node)           \
217         do {                                                            \
218                 const struct rte_memzone *mz = NULL;                    \
219                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
220                 if (size > 0) {                                         \
221                         char z_name[RTE_MEMZONE_NAMESIZE];              \
222                         snprintf(z_name, sizeof(z_name),                \
223                          "ena_alloc_%d",                                \
224                          rte_atomic32_add_return(&ena_alloc_cnt, 1));   \
225                         mz = rte_memzone_reserve(z_name, size, node,    \
226                                 RTE_MEMZONE_IOVA_CONTIG);               \
227                         mem_handle = mz;                                \
228                 }                                                       \
229                 if (mz == NULL) {                                       \
230                         virt = NULL;                                    \
231                         phys = 0;                                       \
232                 } else {                                                \
233                         memset(mz->addr, 0, size);                      \
234                         virt = mz->addr;                                \
235                         phys = mz->iova;                                \
236                 }                                                       \
237         } while (0)
238
239 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
240         do {                                                            \
241                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
242                 virt = rte_zmalloc_socket(NULL, size, 0, node);         \
243         } while (0)
244
245 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
246 #define ENA_MEM_FREE(dmadev, ptr, size)                                 \
247         ({ ENA_TOUCH(dmadev); ENA_TOUCH(size); rte_free(ptr); })
248
249 #define ENA_DB_SYNC(mem_handle) ((void)mem_handle)
250
251 #define ENA_REG_WRITE32(bus, value, reg)                                \
252         ({ (void)(bus); rte_write32((value), (reg)); })
253 #define ENA_REG_WRITE32_RELAXED(bus, value, reg)                        \
254         ({ (void)(bus); rte_write32_relaxed((value), (reg)); })
255 #define ENA_REG_READ32(bus, reg)                                        \
256         ({ (void)(bus); rte_read32_relaxed((reg)); })
257
258 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
259 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
260 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
261 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
262
263 #define msleep(x) rte_delay_us(x * 1000)
264 #define udelay(x) rte_delay_us(x)
265
266 #define dma_rmb() rmb()
267
268 #define MAX_ERRNO       4095
269 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
270 #define ERR_PTR(error) ((void *)(long)error)
271 #define PTR_ERR(error) ((long)(void *)error)
272 #define might_sleep()
273
274 #define prefetch(x) rte_prefetch0(x)
275 #define prefetchw(x) prefetch(x)
276
277 #define lower_32_bits(x) ((uint32_t)(x))
278 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16))
279
280 #define ENA_TIME_EXPIRE(timeout)  (timeout < rte_get_timer_cycles())
281 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us)                              \
282     (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
283 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
284
285 #ifndef READ_ONCE
286 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
287 #endif
288
289 #define READ_ONCE8(var) READ_ONCE(var)
290 #define READ_ONCE16(var) READ_ONCE(var)
291 #define READ_ONCE32(var) READ_ONCE(var)
292
293 /* The size must be 8 byte align */
294 #define ENA_MEMCPY_TO_DEVICE_64(dst, src, size)                         \
295         do {                                                            \
296                 int count, i;                                           \
297                 uint64_t *to = (uint64_t *)(dst);                       \
298                 const uint64_t *from = (const uint64_t *)(src);         \
299                 count = (size) / 8;                                     \
300                 for (i = 0; i < count; i++, from++, to++)               \
301                         rte_write64_relaxed(*from, to);                 \
302         } while(0)
303
304 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
305
306 #define ENA_FFS(x) ffs(x)
307
308 void ena_rss_key_fill(void *key, size_t size);
309
310 #define ENA_RSS_FILL_KEY(key, size) ena_rss_key_fill(key, size)
311
312 #define ENA_INTR_INITIAL_TX_INTERVAL_USECS_PLAT 0
313
314 #define ENA_PRIu64 PRIu64
315
316 #include "ena_includes.h"
317 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */