New upstream version 18.08
[deb_dpdk.git] / drivers / net / ena / base / ena_plat_dpdk.h
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) 2015-2016 Amazon.com, Inc. or its affiliates.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33
34 #ifndef DPDK_ENA_COM_ENA_PLAT_DPDK_H_
35 #define DPDK_ENA_COM_ENA_PLAT_DPDK_H_
36
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include <pthread.h>
40 #include <stdint.h>
41 #include <string.h>
42 #include <errno.h>
43
44 #include <rte_atomic.h>
45 #include <rte_branch_prediction.h>
46 #include <rte_cycles.h>
47 #include <rte_io.h>
48 #include <rte_log.h>
49 #include <rte_malloc.h>
50 #include <rte_memzone.h>
51 #include <rte_spinlock.h>
52
53 #include <sys/time.h>
54
55 typedef uint64_t u64;
56 typedef uint32_t u32;
57 typedef uint16_t u16;
58 typedef uint8_t u8;
59
60 typedef uint64_t dma_addr_t;
61 #ifndef ETIME
62 #define ETIME ETIMEDOUT
63 #endif
64
65 #define ena_atomic32_t rte_atomic32_t
66 #define ena_mem_handle_t const struct rte_memzone *
67
68 #define SZ_256 (256U)
69 #define SZ_4K (4096U)
70
71 #define ENA_COM_OK      0
72 #define ENA_COM_NO_MEM  -ENOMEM
73 #define ENA_COM_INVAL   -EINVAL
74 #define ENA_COM_NO_SPACE        -ENOSPC
75 #define ENA_COM_NO_DEVICE       -ENODEV
76 #define ENA_COM_TIMER_EXPIRED   -ETIME
77 #define ENA_COM_FAULT   -EFAULT
78 #define ENA_COM_TRY_AGAIN       -EAGAIN
79 #define ENA_COM_UNSUPPORTED    -EOPNOTSUPP
80
81 #define ____cacheline_aligned __rte_cache_aligned
82
83 #define ENA_ABORT() abort()
84
85 #define ENA_MSLEEP(x) rte_delay_ms(x)
86 #define ENA_UDELAY(x) rte_delay_us(x)
87
88 #define ENA_TOUCH(x) ((void)(x))
89 #define memcpy_toio memcpy
90 #define wmb rte_wmb
91 #define rmb rte_wmb
92 #define mb rte_mb
93 #define __iomem
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 #if RTE_LOG_DP_LEVEL >= RTE_LOG_DEBUG
100 #define ENA_ASSERT(cond, format, arg...)                                \
101         do {                                                            \
102                 if (unlikely(!(cond))) {                                \
103                         RTE_LOG(ERR, PMD, format, ##arg);               \
104                         rte_panic("line %d\tassert \"" #cond "\""       \
105                                         "failed\n", __LINE__);          \
106                 }                                                       \
107         } while (0)
108 #else
109 #define ENA_ASSERT(cond, format, arg...) do {} while (0)
110 #endif
111
112 #define ENA_MAX32(x, y) RTE_MAX((x), (y))
113 #define ENA_MAX16(x, y) RTE_MAX((x), (y))
114 #define ENA_MAX8(x, y) RTE_MAX((x), (y))
115 #define ENA_MIN32(x, y) RTE_MIN((x), (y))
116 #define ENA_MIN16(x, y) RTE_MIN((x), (y))
117 #define ENA_MIN8(x, y) RTE_MIN((x), (y))
118
119 #define BITS_PER_LONG_LONG (__SIZEOF_LONG_LONG__ * 8)
120 #define U64_C(x) x ## ULL
121 #define BIT(nr)         (1UL << (nr))
122 #define BITS_PER_LONG   (__SIZEOF_LONG__ * 8)
123 #define GENMASK(h, l)   (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
124 #define GENMASK_ULL(h, l) (((~0ULL) - (1ULL << (l)) + 1) & \
125                           (~0ULL >> (BITS_PER_LONG_LONG - 1 - (h))))
126
127 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
128 #define ena_trc_dbg(format, arg...)                                     \
129         RTE_LOG(DEBUG, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
130 #define ena_trc_info(format, arg...)                                    \
131         RTE_LOG(INFO, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
132 #define ena_trc_warn(format, arg...)                                    \
133         RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
134 #define ena_trc_err(format, arg...)                                     \
135         RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
136 #else
137 #define ena_trc_dbg(format, arg...) do { } while (0)
138 #define ena_trc_info(format, arg...) do { } while (0)
139 #define ena_trc_warn(format, arg...) do { } while (0)
140 #define ena_trc_err(format, arg...) do { } while (0)
141 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
142
143 #define ENA_WARN(cond, format, arg...)                                 \
144 do {                                                                   \
145        if (unlikely(cond)) {                                           \
146                ena_trc_err(                                            \
147                        "Warn failed on %s:%s:%d:" format,              \
148                        __FILE__, __func__, __LINE__, ##arg);           \
149        }                                                               \
150 } while (0)
151
152 /* Spinlock related methods */
153 #define ena_spinlock_t rte_spinlock_t
154 #define ENA_SPINLOCK_INIT(spinlock) rte_spinlock_init(&spinlock)
155 #define ENA_SPINLOCK_LOCK(spinlock, flags)                              \
156         ({(void)flags; rte_spinlock_lock(&spinlock); })
157 #define ENA_SPINLOCK_UNLOCK(spinlock, flags)                            \
158         ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
159
160 #define q_waitqueue_t                   \
161         struct {                        \
162                 pthread_cond_t cond;    \
163                 pthread_mutex_t mutex;  \
164         }
165
166 #define ena_wait_queue_t q_waitqueue_t
167
168 #define ENA_WAIT_EVENT_INIT(waitqueue)                                  \
169         do {                                                            \
170                 pthread_mutex_init(&(waitqueue).mutex, NULL);           \
171                 pthread_cond_init(&(waitqueue).cond, NULL);             \
172         } while (0)
173
174 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout)                         \
175         do {                                                            \
176                 struct timespec wait;                                   \
177                 struct timeval now;                                     \
178                 unsigned long timeout_us;                               \
179                 gettimeofday(&now, NULL);                               \
180                 wait.tv_sec = now.tv_sec + timeout / 1000000UL;         \
181                 timeout_us = timeout % 1000000UL;                       \
182                 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL;     \
183                 pthread_mutex_lock(&waitevent.mutex);                   \
184                 pthread_cond_timedwait(&waitevent.cond,                 \
185                                 &waitevent.mutex, &wait);               \
186                 pthread_mutex_unlock(&waitevent.mutex);                 \
187         } while (0)
188 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
189 /* pthread condition doesn't need to be rearmed after usage */
190 #define ENA_WAIT_EVENT_CLEAR(...)
191 #define ENA_WAIT_EVENT_DESTROY(waitqueue) ((void)(waitqueue))
192
193 #define ena_wait_event_t ena_wait_queue_t
194 #define ENA_MIGHT_SLEEP()
195
196 #define ENA_TIME_EXPIRE(timeout)  (timeout < rte_get_timer_cycles())
197 #define ENA_GET_SYSTEM_TIMEOUT(timeout_us)                             \
198        (timeout_us * rte_get_timer_hz() / 1000000 + rte_get_timer_cycles())
199
200 /*
201  * Each rte_memzone should have unique name.
202  * To satisfy it, count number of allocations and add it to name.
203  */
204 extern uint32_t ena_alloc_cnt;
205
206 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle)        \
207         do {                                                            \
208                 const struct rte_memzone *mz;                           \
209                 char z_name[RTE_MEMZONE_NAMESIZE];                      \
210                 ENA_TOUCH(dmadev); ENA_TOUCH(handle);                   \
211                 snprintf(z_name, sizeof(z_name),                        \
212                                 "ena_alloc_%d", ena_alloc_cnt++);       \
213                 mz = rte_memzone_reserve(z_name, size, SOCKET_ID_ANY,   \
214                                 RTE_MEMZONE_IOVA_CONTIG);               \
215                 handle = mz;                                            \
216                 if (mz == NULL) {                                       \
217                         virt = NULL;                                    \
218                         phys = 0;                                       \
219                 } else {                                                \
220                         memset(mz->addr, 0, size);                      \
221                         virt = mz->addr;                                \
222                         phys = mz->iova;                                \
223                 }                                                       \
224         } while (0)
225 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle)         \
226                 ({ ENA_TOUCH(size); ENA_TOUCH(phys);                    \
227                    ENA_TOUCH(dmadev);                                   \
228                    rte_memzone_free(handle); })
229
230 #define ENA_MEM_ALLOC_COHERENT_NODE(                                    \
231         dmadev, size, virt, phys, mem_handle, node, dev_node)           \
232         do {                                                            \
233                 const struct rte_memzone *mz;                           \
234                 char z_name[RTE_MEMZONE_NAMESIZE];                      \
235                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
236                 snprintf(z_name, sizeof(z_name),                        \
237                                 "ena_alloc_%d", ena_alloc_cnt++);       \
238                 mz = rte_memzone_reserve(z_name, size, node,            \
239                                 RTE_MEMZONE_IOVA_CONTIG);               \
240                 mem_handle = mz;                                        \
241                 if (mz == NULL) {                                       \
242                         virt = NULL;                                    \
243                         phys = 0;                                       \
244                 } else {                                                \
245                         memset(mz->addr, 0, size);                      \
246                         virt = mz->addr;                                \
247                         phys = mz->iova;                                \
248                 }                                                       \
249         } while (0)
250
251 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
252         do {                                                            \
253                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
254                 virt = rte_zmalloc_socket(NULL, size, 0, node);         \
255         } while (0)
256
257 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
258 #define ENA_MEM_FREE(dmadev, ptr) ({ENA_TOUCH(dmadev); rte_free(ptr); })
259
260 #define ENA_REG_WRITE32(bus, value, reg)                                \
261         ({ (void)(bus); rte_write32_relaxed((value), (reg)); })
262 #define ENA_REG_READ32(bus, reg)                                        \
263         ({ (void)(bus); rte_read32_relaxed((reg)); })
264
265 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
266 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
267 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
268 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
269
270 #define msleep(x) rte_delay_us(x * 1000)
271 #define udelay(x) rte_delay_us(x)
272
273 #define MAX_ERRNO       4095
274 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
275 #define ERR_PTR(error) ((void *)(long)error)
276 #define PTR_ERR(error) ((long)(void *)error)
277 #define might_sleep()
278
279 #define lower_32_bits(x) ((uint32_t)(x))
280 #define upper_32_bits(x) ((uint32_t)(((x) >> 16) >> 16))
281
282 #ifndef READ_ONCE
283 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
284 #endif
285
286 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */