net/bnxt: update buffer descriptor definitions
[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_log.h>
48 #include <rte_malloc.h>
49 #include <rte_memzone.h>
50 #include <rte_spinlock.h>
51
52 #include <sys/time.h>
53
54 typedef uint64_t u64;
55 typedef uint32_t u32;
56 typedef uint16_t u16;
57 typedef uint8_t u8;
58
59 typedef uint64_t dma_addr_t;
60 #ifndef ETIME
61 #define ETIME ETIMEDOUT
62 #endif
63
64 #define ena_atomic32_t rte_atomic32_t
65 #define ena_mem_handle_t const struct rte_memzone *
66
67 #define SZ_256 (256U)
68 #define SZ_4K (4096U)
69
70 #define ENA_COM_OK      0
71 #define ENA_COM_NO_MEM  -ENOMEM
72 #define ENA_COM_INVAL   -EINVAL
73 #define ENA_COM_NO_SPACE        -ENOSPC
74 #define ENA_COM_NO_DEVICE       -ENODEV
75 #define ENA_COM_PERMISSION      -EPERM
76 #define ENA_COM_TIMER_EXPIRED   -ETIME
77 #define ENA_COM_FAULT   -EFAULT
78 #define ENA_COM_TRY_AGAIN       -EAGAIN
79
80 #define ____cacheline_aligned __rte_cache_aligned
81
82 #define ENA_ABORT() abort()
83
84 #define ENA_MSLEEP(x) rte_delay_ms(x)
85 #define ENA_UDELAY(x) rte_delay_us(x)
86
87 #define ENA_TOUCH(x) ((void)(x))
88 #define memcpy_toio memcpy
89 #define wmb rte_wmb
90 #define rmb rte_wmb
91 #define mb rte_mb
92 #define __iomem
93
94 #define US_PER_S 1000000
95 #define ENA_GET_SYSTEM_USECS()                                          \
96         (rte_get_timer_cycles() * US_PER_S / rte_get_timer_hz())
97
98 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
99 #define ENA_ASSERT(cond, format, arg...)                                \
100         do {                                                            \
101                 if (unlikely(!(cond))) {                                \
102                         RTE_LOG(ERR, PMD, format, ##arg);               \
103                         rte_panic("line %d\tassert \"" #cond "\""       \
104                                         "failed\n", __LINE__);          \
105                 }                                                       \
106         } while (0)
107 #else
108 #define ENA_ASSERT(cond, format, arg...) do {} while (0)
109 #endif
110
111 #define ENA_MAX32(x, y) RTE_MAX((x), (y))
112 #define ENA_MAX16(x, y) RTE_MAX((x), (y))
113 #define ENA_MAX8(x, y) RTE_MAX((x), (y))
114 #define ENA_MIN32(x, y) RTE_MIN((x), (y))
115 #define ENA_MIN16(x, y) RTE_MIN((x), (y))
116 #define ENA_MIN8(x, y) RTE_MIN((x), (y))
117
118 #define U64_C(x) x ## ULL
119 #define BIT(nr)         (1UL << (nr))
120 #define BITS_PER_LONG   (__SIZEOF_LONG__ * 8)
121 #define GENMASK(h, l)   (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h))))
122 #define GENMASK_ULL(h, l) (((U64_C(1) << ((h) - (l) + 1)) - 1) << (l))
123
124 #ifdef RTE_LIBRTE_ENA_COM_DEBUG
125 #define ena_trc_dbg(format, arg...)                                     \
126         RTE_LOG(DEBUG, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
127 #define ena_trc_info(format, arg...)                                    \
128         RTE_LOG(INFO, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
129 #define ena_trc_warn(format, arg...)                                    \
130         RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
131 #define ena_trc_err(format, arg...)                                     \
132         RTE_LOG(ERR, PMD, "[ENA_COM: %s] " format, __func__, ##arg)
133 #else
134 #define ena_trc_dbg(format, arg...) do { } while (0)
135 #define ena_trc_info(format, arg...) do { } while (0)
136 #define ena_trc_warn(format, arg...) do { } while (0)
137 #define ena_trc_err(format, arg...) do { } while (0)
138 #endif /* RTE_LIBRTE_ENA_COM_DEBUG */
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         ({(void)flags; rte_spinlock_lock(&spinlock); })
145 #define ENA_SPINLOCK_UNLOCK(spinlock, flags)                            \
146         ({(void)flags; rte_spinlock_unlock(&(spinlock)); })
147
148 #define q_waitqueue_t                   \
149         struct {                        \
150                 pthread_cond_t cond;    \
151                 pthread_mutex_t mutex;  \
152         }
153
154 #define ena_wait_queue_t q_waitqueue_t
155
156 #define ENA_WAIT_EVENT_INIT(waitqueue)                                  \
157         do {                                                            \
158                 pthread_mutex_init(&(waitqueue).mutex, NULL);           \
159                 pthread_cond_init(&(waitqueue).cond, NULL);             \
160         } while (0)
161
162 #define ENA_WAIT_EVENT_WAIT(waitevent, timeout)                         \
163         do {                                                            \
164                 struct timespec wait;                                   \
165                 struct timeval now;                                     \
166                 unsigned long timeout_us;                               \
167                 gettimeofday(&now, NULL);                               \
168                 wait.tv_sec = now.tv_sec + timeout / 1000000UL;         \
169                 timeout_us = timeout % 1000000UL;                       \
170                 wait.tv_nsec = (now.tv_usec + timeout_us) * 1000UL;     \
171                 pthread_mutex_lock(&waitevent.mutex);                   \
172                 pthread_cond_timedwait(&waitevent.cond,                 \
173                                 &waitevent.mutex, &wait);               \
174                 pthread_mutex_unlock(&waitevent.mutex);                 \
175         } while (0)
176 #define ENA_WAIT_EVENT_SIGNAL(waitevent) pthread_cond_signal(&waitevent.cond)
177 /* pthread condition doesn't need to be rearmed after usage */
178 #define ENA_WAIT_EVENT_CLEAR(...)
179
180 #define ena_wait_event_t ena_wait_queue_t
181 #define ENA_MIGHT_SLEEP()
182
183 #define ENA_MEM_ALLOC_COHERENT(dmadev, size, virt, phys, handle)        \
184         do {                                                            \
185                 const struct rte_memzone *mz;                           \
186                 char z_name[RTE_MEMZONE_NAMESIZE];                      \
187                 ENA_TOUCH(dmadev); ENA_TOUCH(handle);                   \
188                 snprintf(z_name, sizeof(z_name),                        \
189                                 "ena_alloc_%d", ena_alloc_cnt++);       \
190                 mz = rte_memzone_reserve(z_name, size, SOCKET_ID_ANY, 0); \
191                 memset(mz->addr, 0, size);                              \
192                 virt = mz->addr;                                        \
193                 phys = mz->phys_addr;                                   \
194                 handle = mz;                                            \
195         } while (0)
196 #define ENA_MEM_FREE_COHERENT(dmadev, size, virt, phys, handle)         \
197                 ({ ENA_TOUCH(size); ENA_TOUCH(phys);                    \
198                    ENA_TOUCH(dmadev);                                   \
199                    rte_memzone_free(handle); })
200
201 #define ENA_MEM_ALLOC_COHERENT_NODE(dmadev, size, virt, phys, node, dev_node) \
202         do {                                                            \
203                 const struct rte_memzone *mz;                           \
204                 char z_name[RTE_MEMZONE_NAMESIZE];                      \
205                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
206                 snprintf(z_name, sizeof(z_name),                        \
207                                 "ena_alloc_%d", ena_alloc_cnt++);       \
208                 mz = rte_memzone_reserve(z_name, size, node, 0); \
209                 virt = mz->addr;                                        \
210                 phys = mz->phys_addr;                                   \
211         } while (0)
212
213 #define ENA_MEM_ALLOC_NODE(dmadev, size, virt, node, dev_node) \
214         do {                                                            \
215                 const struct rte_memzone *mz;                           \
216                 char z_name[RTE_MEMZONE_NAMESIZE];                      \
217                 ENA_TOUCH(dmadev); ENA_TOUCH(dev_node);                 \
218                 snprintf(z_name, sizeof(z_name),                        \
219                                 "ena_alloc_%d", ena_alloc_cnt++);       \
220                 mz = rte_memzone_reserve(z_name, size, node, 0); \
221                 virt = mz->addr;                                        \
222         } while (0)
223
224 #define ENA_MEM_ALLOC(dmadev, size) rte_zmalloc(NULL, size, 1)
225 #define ENA_MEM_FREE(dmadev, ptr) ({ENA_TOUCH(dmadev); rte_free(ptr); })
226
227 static inline void writel(u32 value, volatile void  *addr)
228 {
229         *(volatile u32 *)addr = value;
230 }
231
232 static inline u32 readl(const volatile void *addr)
233 {
234         return *(const volatile u32 *)addr;
235 }
236
237 #define ENA_REG_WRITE32(value, reg) writel((value), (reg))
238 #define ENA_REG_READ32(reg) readl((reg))
239
240 #define ATOMIC32_INC(i32_ptr) rte_atomic32_inc(i32_ptr)
241 #define ATOMIC32_DEC(i32_ptr) rte_atomic32_dec(i32_ptr)
242 #define ATOMIC32_SET(i32_ptr, val) rte_atomic32_set(i32_ptr, val)
243 #define ATOMIC32_READ(i32_ptr) rte_atomic32_read(i32_ptr)
244
245 #define msleep(x) rte_delay_us(x * 1000)
246 #define udelay(x) rte_delay_us(x)
247
248 #define MAX_ERRNO       4095
249 #define IS_ERR(x) (((unsigned long)x) >= (unsigned long)-MAX_ERRNO)
250 #define ERR_PTR(error) ((void *)(long)error)
251 #define PTR_ERR(error) ((long)(void *)error)
252 #define might_sleep()
253
254 #endif /* DPDK_ENA_COM_ENA_PLAT_DPDK_H_ */