3acf8f7c407b762b8820787130aecf807627ebf6
[deb_dpdk.git] / drivers / net / qede / base / bcm_osal.h
1 /*
2  * Copyright (c) 2016 QLogic Corporation.
3  * All rights reserved.
4  * www.qlogic.com
5  *
6  * See LICENSE.qede_pmd for copyright and licensing details.
7  */
8
9 #ifndef __BCM_OSAL_H
10 #define __BCM_OSAL_H
11
12 #include <rte_byteorder.h>
13 #include <rte_spinlock.h>
14 #include <rte_malloc.h>
15 #include <rte_atomic.h>
16 #include <rte_memcpy.h>
17 #include <rte_log.h>
18 #include <rte_cycles.h>
19 #include <rte_debug.h>
20 #include <rte_ether.h>
21 #include <rte_io.h>
22
23 /* Forward declaration */
24 struct ecore_dev;
25 struct ecore_hwfn;
26 struct ecore_vf_acquire_sw_info;
27 struct vf_pf_resc_request;
28 enum ecore_mcp_protocol_type;
29 union ecore_mcp_protocol_stats;
30 enum ecore_hw_err_type;
31
32 void qed_link_update(struct ecore_hwfn *hwfn);
33
34 #if RTE_BYTE_ORDER == RTE_LITTLE_ENDIAN
35 #undef __BIG_ENDIAN
36 #ifndef __LITTLE_ENDIAN
37 #define __LITTLE_ENDIAN
38 #endif
39 #else
40 #undef __LITTLE_ENDIAN
41 #ifndef __BIG_ENDIAN
42 #define __BIG_ENDIAN
43 #endif
44 #endif
45
46 #define OSAL_WARN(arg1, arg2, arg3, ...) (0)
47
48 /* Memory Types */
49 typedef uint8_t u8;
50 typedef uint16_t u16;
51 typedef uint32_t u32;
52 typedef uint64_t u64;
53
54 typedef int16_t s16;
55 typedef int32_t s32;
56
57 typedef u16 __le16;
58 typedef u32 __le32;
59 typedef u32 OSAL_BE32;
60
61 #define osal_uintptr_t uintptr_t
62
63 typedef phys_addr_t dma_addr_t;
64
65 typedef rte_spinlock_t osal_spinlock_t;
66
67 typedef void *osal_dpc_t;
68
69 typedef size_t osal_size_t;
70
71 typedef intptr_t osal_int_ptr_t;
72
73 typedef int bool;
74 #define true 1
75 #define false 0
76
77 #define nothing do {} while (0)
78
79 /* Delays */
80
81 #define DELAY(x) rte_delay_us(x)
82 #define usec_delay(x) DELAY(x)
83 #define msec_delay(x) DELAY(1000 * (x))
84 #define OSAL_UDELAY(time) usec_delay(time)
85 #define OSAL_MSLEEP(time) msec_delay(time)
86
87 /* Memory allocations and deallocations */
88
89 #define OSAL_NULL ((void *)0)
90 #define OSAL_ALLOC(dev, GFP, size) rte_malloc("qede", size, 0)
91 #define OSAL_ZALLOC(dev, GFP, size) rte_zmalloc("qede", size, 0)
92 #define OSAL_CALLOC(dev, GFP, num, size) rte_calloc("qede", num, size, 0)
93 #define OSAL_VZALLOC(dev, size) rte_zmalloc("qede", size, 0)
94 #define OSAL_FREE(dev, memory)            \
95         do {                              \
96                 rte_free((void *)memory); \
97                 memory = OSAL_NULL;       \
98         } while (0)
99 #define OSAL_VFREE(dev, memory) OSAL_FREE(dev, memory)
100 #define OSAL_MEM_ZERO(mem, size) bzero(mem, size)
101 #define OSAL_MEMCPY(dst, src, size) rte_memcpy(dst, src, size)
102 #define OSAL_MEMCMP(s1, s2, size) memcmp(s1, s2, size)
103 #define OSAL_MEMSET(dst, val, length) \
104         memset(dst, val, length)
105
106 void *osal_dma_alloc_coherent(struct ecore_dev *, dma_addr_t *, size_t);
107
108 void *osal_dma_alloc_coherent_aligned(struct ecore_dev *, dma_addr_t *,
109                                       size_t, int);
110
111 void osal_dma_free_mem(struct ecore_dev *edev, dma_addr_t phys);
112
113 #define OSAL_DMA_ALLOC_COHERENT(dev, phys, size) \
114         osal_dma_alloc_coherent(dev, phys, size)
115
116 #define OSAL_DMA_ALLOC_COHERENT_ALIGNED(dev, phys, size, align) \
117         osal_dma_alloc_coherent_aligned(dev, phys, size, align)
118
119 #define OSAL_DMA_FREE_COHERENT(dev, virt, phys, size) \
120         osal_dma_free_mem(dev, phys)
121
122 /* HW reads/writes */
123
124 #define DIRECT_REG_RD(_dev, _reg_addr) rte_read32(_reg_addr)
125
126 #define REG_RD(_p_hwfn, _reg_offset) \
127         DIRECT_REG_RD(_p_hwfn,          \
128                         ((u8 *)(uintptr_t)(_p_hwfn->regview) + (_reg_offset)))
129
130 #define DIRECT_REG_WR16(_reg_addr, _val) rte_write16((_val), (_reg_addr))
131
132 #define DIRECT_REG_WR(_dev, _reg_addr, _val) rte_write32((_val), (_reg_addr))
133
134 #define DIRECT_REG_WR_RELAXED(_dev, _reg_addr, _val) \
135         rte_write32_relaxed((_val), (_reg_addr))
136
137 #define REG_WR(_p_hwfn, _reg_offset, _val) \
138         DIRECT_REG_WR(NULL,  \
139         ((u8 *)((uintptr_t)(_p_hwfn->regview)) + (_reg_offset)), (u32)_val)
140
141 #define REG_WR16(_p_hwfn, _reg_offset, _val) \
142         DIRECT_REG_WR16(((u8 *)(uintptr_t)(_p_hwfn->regview) + \
143                         (_reg_offset)), (u16)_val)
144
145 #define DOORBELL(_p_hwfn, _db_addr, _val)                               \
146         DIRECT_REG_WR_RELAXED((_p_hwfn),                                \
147                               ((u8 *)(uintptr_t)(_p_hwfn->doorbells) +  \
148                               (_db_addr)), (u32)_val)
149
150 /* Mutexes */
151
152 typedef pthread_mutex_t osal_mutex_t;
153 #define OSAL_MUTEX_RELEASE(lock) pthread_mutex_unlock(lock)
154 #define OSAL_MUTEX_INIT(lock) pthread_mutex_init(lock, NULL)
155 #define OSAL_MUTEX_ACQUIRE(lock) pthread_mutex_lock(lock)
156 #define OSAL_MUTEX_ALLOC(hwfn, lock) nothing
157 #define OSAL_MUTEX_DEALLOC(lock) nothing
158
159 /* Spinlocks */
160
161 #define OSAL_SPIN_LOCK_INIT(lock) rte_spinlock_init(lock)
162 #define OSAL_SPIN_LOCK(lock) rte_spinlock_lock(lock)
163 #define OSAL_SPIN_UNLOCK(lock) rte_spinlock_unlock(lock)
164 #define OSAL_SPIN_LOCK_IRQSAVE(lock, flags) nothing
165 #define OSAL_SPIN_UNLOCK_IRQSAVE(lock, flags) nothing
166 #define OSAL_SPIN_LOCK_ALLOC(hwfn, lock) nothing
167 #define OSAL_SPIN_LOCK_DEALLOC(lock) nothing
168
169 /* DPC */
170
171 #define OSAL_DPC_ALLOC(hwfn) OSAL_ALLOC(hwfn, GFP, sizeof(osal_dpc_t))
172 #define OSAL_DPC_INIT(dpc, hwfn) nothing
173 #define OSAL_POLL_MODE_DPC(hwfn) nothing
174 #define OSAL_DPC_SYNC(hwfn) nothing
175
176 /* Lists */
177
178 #define OSAL_LIST_SPLICE_INIT(new_list, list) nothing
179 #define OSAL_LIST_SPLICE_TAIL_INIT(new_list, list) nothing
180
181 typedef struct _osal_list_entry_t {
182         struct _osal_list_entry_t *next, *prev;
183 } osal_list_entry_t;
184
185 typedef struct osal_list_t {
186         osal_list_entry_t *head, *tail;
187         unsigned long cnt;
188 } osal_list_t;
189
190 #define OSAL_LIST_INIT(list) \
191         do {                    \
192                 (list)->head = NULL;  \
193                 (list)->tail = NULL;  \
194                 (list)->cnt  = 0;       \
195         } while (0)
196
197 #define OSAL_LIST_PUSH_HEAD(entry, list)                \
198         do {                                            \
199                 (entry)->prev = (osal_list_entry_t *)0;         \
200                 (entry)->next = (list)->head;                   \
201                 if ((list)->tail == (osal_list_entry_t *)0) {   \
202                         (list)->tail = (entry);                 \
203                 } else {                                        \
204                         (list)->head->prev = (entry);           \
205                 }                                               \
206                 (list)->head = (entry);                         \
207                 (list)->cnt++;                                  \
208         } while (0)
209
210 #define OSAL_LIST_PUSH_TAIL(entry, list)        \
211         do {                                    \
212                 (entry)->next = (osal_list_entry_t *)0; \
213                 (entry)->prev = (list)->tail;           \
214                 if ((list)->tail) {                     \
215                         (list)->tail->next = (entry);   \
216                 } else {                                \
217                         (list)->head = (entry);         \
218                 }                                       \
219                 (list)->tail = (entry);                 \
220                 (list)->cnt++;                          \
221         } while (0)
222
223 #define OSAL_LIST_FIRST_ENTRY(list, type, field) \
224         (type *)((list)->head)
225
226 #define OSAL_LIST_REMOVE_ENTRY(entry, list)                     \
227         do {                                                    \
228                 if ((list)->head == (entry)) {                          \
229                         if ((list)->head) {                             \
230                                 (list)->head = (list)->head->next;      \
231                         if ((list)->head) {                             \
232                                 (list)->head->prev = (osal_list_entry_t *)0;\
233                         } else {                                        \
234                                 (list)->tail = (osal_list_entry_t *)0;  \
235                         }                                               \
236                         (list)->cnt--;                                  \
237                         }                                               \
238                 } else if ((list)->tail == (entry)) {                   \
239                         if ((list)->tail) {                             \
240                                 (list)->tail = (list)->tail->prev;      \
241                         if ((list)->tail) {                             \
242                                 (list)->tail->next = (osal_list_entry_t *)0;\
243                         } else {                                        \
244                                 (list)->head = (osal_list_entry_t *)0;  \
245                         }                                               \
246                         (list)->cnt--;                                  \
247                         }                                               \
248                 } else {                                                \
249                         (entry)->prev->next = (entry)->next;            \
250                         (entry)->next->prev = (entry)->prev;            \
251                         (list)->cnt--;                                  \
252                 }                                                       \
253         } while (0)
254
255 #define OSAL_LIST_IS_EMPTY(list) \
256         ((list)->cnt == 0)
257
258 #define OSAL_LIST_NEXT(entry, field, type) \
259         (type *)((&((entry)->field))->next)
260
261 /* TODO: Check field, type order */
262
263 #define OSAL_LIST_FOR_EACH_ENTRY(entry, list, field, type) \
264         for (entry = OSAL_LIST_FIRST_ENTRY(list, type, field); \
265                 entry;                                          \
266                 entry = OSAL_LIST_NEXT(entry, field, type))
267
268 #define OSAL_LIST_FOR_EACH_ENTRY_SAFE(entry, tmp_entry, list, field, type) \
269          for (entry = OSAL_LIST_FIRST_ENTRY(list, type, field), \
270           tmp_entry = (entry) ? OSAL_LIST_NEXT(entry, field, type) : NULL;    \
271           entry != NULL;                                                \
272           entry = (type *)tmp_entry,                                     \
273           tmp_entry = (entry) ? OSAL_LIST_NEXT(entry, field, type) : NULL)
274
275 /* TODO: OSAL_LIST_INSERT_ENTRY_AFTER */
276 #define OSAL_LIST_INSERT_ENTRY_AFTER(new_entry, entry, list) \
277         OSAL_LIST_PUSH_HEAD(new_entry, list)
278
279 /* PCI config space */
280
281 #define OSAL_PCI_READ_CONFIG_BYTE(dev, address, dst) nothing
282 #define OSAL_PCI_READ_CONFIG_WORD(dev, address, dst) nothing
283 #define OSAL_PCI_READ_CONFIG_DWORD(dev, address, dst) nothing
284 #define OSAL_PCI_FIND_EXT_CAPABILITY(dev, pcie_id) 0
285 #define OSAL_PCI_FIND_CAPABILITY(dev, pcie_id) 0
286 #define OSAL_PCI_WRITE_CONFIG_WORD(dev, address, val) nothing
287 #define OSAL_BAR_SIZE(dev, bar_id) 0
288
289 /* Barriers */
290
291 #define OSAL_MMIOWB(dev)                rte_wmb()
292 #define OSAL_BARRIER(dev)               rte_compiler_barrier()
293 #define OSAL_SMP_RMB(dev)               rte_rmb()
294 #define OSAL_SMP_WMB(dev)               rte_wmb()
295 #define OSAL_RMB(dev)                   rte_rmb()
296 #define OSAL_WMB(dev)                   rte_wmb()
297 #define OSAL_DMA_SYNC(dev, addr, length, is_post) nothing
298
299 #define OSAL_BIT(nr)            (1UL << (nr))
300 #define OSAL_BITS_PER_BYTE      (8)
301 #define OSAL_BITS_PER_UL        (sizeof(unsigned long) * OSAL_BITS_PER_BYTE)
302 #define OSAL_BITS_PER_UL_MASK           (OSAL_BITS_PER_UL - 1)
303
304 /* Bitops */
305 void qede_set_bit(u32, unsigned long *);
306 #define OSAL_SET_BIT(bit, bitmap) \
307         qede_set_bit(bit, bitmap)
308
309 void qede_clr_bit(u32, unsigned long *);
310 #define OSAL_CLEAR_BIT(bit, bitmap) \
311         qede_clr_bit(bit, bitmap)
312
313 bool qede_test_bit(u32, unsigned long *);
314 #define OSAL_TEST_BIT(bit, bitmap) \
315         qede_test_bit(bit, bitmap)
316
317 u32 qede_find_first_bit(unsigned long *, u32);
318 #define OSAL_FIND_FIRST_BIT(bitmap, length) \
319         qede_find_first_bit(bitmap, length)
320
321 u32 qede_find_first_zero_bit(unsigned long *, u32);
322 #define OSAL_FIND_FIRST_ZERO_BIT(bitmap, length) \
323         qede_find_first_zero_bit(bitmap, length)
324
325 #define OSAL_BUILD_BUG_ON(cond)         nothing
326 #define ETH_ALEN                        ETHER_ADDR_LEN
327
328 #define OSAL_BITMAP_WEIGHT(bitmap, count) 0
329
330 #define OSAL_LINK_UPDATE(hwfn) qed_link_update(hwfn)
331 #define OSAL_DCBX_AEN(hwfn, mib_type) nothing
332
333 /* SR-IOV channel */
334
335 #define OSAL_VF_FLR_UPDATE(hwfn) nothing
336 #define OSAL_VF_SEND_MSG2PF(dev, done, msg, reply_addr, msg_size, reply_size) 0
337 #define OSAL_VF_CQE_COMPLETION(_dev_p, _cqe, _protocol) (0)
338 #define OSAL_PF_VF_MSG(hwfn, vfid) 0
339 #define OSAL_PF_VF_MALICIOUS(hwfn, vfid) nothing
340 #define OSAL_IOV_CHK_UCAST(hwfn, vfid, params) 0
341 #define OSAL_IOV_POST_START_VPORT(hwfn, vf, vport_id, opaque_fid) nothing
342 #define OSAL_IOV_VF_ACQUIRE(hwfn, vfid) 0
343 #define OSAL_IOV_VF_CLEANUP(hwfn, vfid) nothing
344 #define OSAL_IOV_VF_VPORT_UPDATE(hwfn, vfid, p_params, p_mask) 0
345 #define OSAL_VF_UPDATE_ACQUIRE_RESC_RESP(_dev_p, _resc_resp) 0
346 #define OSAL_IOV_GET_OS_TYPE() 0
347 #define OSAL_IOV_VF_MSG_TYPE(hwfn, vfid, vf_msg_type) 0
348 #define OSAL_IOV_PF_RESP_TYPE(hwfn, vfid, pf_resp_type) 0
349
350 u32 qede_unzip_data(struct ecore_hwfn *p_hwfn, u32 input_len,
351                    u8 *input_buf, u32 max_size, u8 *unzip_buf);
352 void qede_vf_fill_driver_data(struct ecore_hwfn *, struct vf_pf_resc_request *,
353                               struct ecore_vf_acquire_sw_info *);
354 void qede_hw_err_notify(struct ecore_hwfn *p_hwfn,
355                         enum ecore_hw_err_type err_type);
356 #define OSAL_VF_FILL_ACQUIRE_RESC_REQ(_dev_p, _resc_req, _os_info) \
357         qede_vf_fill_driver_data(_dev_p, _resc_req, _os_info)
358
359 #define OSAL_UNZIP_DATA(p_hwfn, input_len, buf, max_size, unzip_buf) \
360         qede_unzip_data(p_hwfn, input_len, buf, max_size, unzip_buf)
361
362 /* TODO: */
363 #define OSAL_SCHEDULE_RECOVERY_HANDLER(hwfn) nothing
364 #define OSAL_HW_ERROR_OCCURRED(hwfn, err_type) \
365         qede_hw_err_notify(hwfn, err_type)
366
367 #define OSAL_NVM_IS_ACCESS_ENABLED(hwfn) (1)
368 #define OSAL_NUM_ACTIVE_CPU()   0
369
370 /* Utility functions */
371
372 #define RTE_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
373 #define DIV_ROUND_UP(size, to_what) RTE_DIV_ROUND_UP(size, to_what)
374 #define RTE_ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
375 #define ROUNDUP(value, to_what) RTE_ROUNDUP((value), (to_what))
376
377 unsigned long qede_log2_align(unsigned long n);
378 #define OSAL_ROUNDUP_POW_OF_TWO(val) \
379         qede_log2_align(val)
380
381 u32 qede_osal_log2(u32);
382 #define OSAL_LOG2(val) \
383         qede_osal_log2(val)
384
385 #define PRINT(format, ...) printf
386 #define PRINT_ERR(format, ...) PRINT
387
388 #define OFFSETOF(str, field) __builtin_offsetof(str, field)
389 #define OSAL_ASSERT(is_assert) assert(is_assert)
390 #define OSAL_BEFORE_PF_START(file, engine) nothing
391 #define OSAL_AFTER_PF_STOP(file, engine) nothing
392
393 /* Endian macros */
394 #define OSAL_CPU_TO_BE32(val) rte_cpu_to_be_32(val)
395 #define OSAL_BE32_TO_CPU(val) rte_be_to_cpu_32(val)
396 #define OSAL_CPU_TO_LE32(val) rte_cpu_to_le_32(val)
397 #define OSAL_CPU_TO_LE16(val) rte_cpu_to_le_16(val)
398 #define OSAL_LE32_TO_CPU(val) rte_le_to_cpu_32(val)
399 #define OSAL_LE16_TO_CPU(val) rte_le_to_cpu_16(val)
400 #define OSAL_CPU_TO_BE64(val) rte_cpu_to_be_64(val)
401
402 #define OSAL_ARRAY_SIZE(arr) RTE_DIM(arr)
403 #define OSAL_SPRINTF(name, pattern, ...) \
404         sprintf(name, pattern, ##__VA_ARGS__)
405 #define OSAL_SNPRINTF(buf, size, format, ...) \
406         snprintf(buf, size, format, ##__VA_ARGS__)
407 #define OSAL_STRLEN(string) strlen(string)
408 #define OSAL_STRCPY(dst, string) strcpy(dst, string)
409 #define OSAL_STRNCPY(dst, string, len) strncpy(dst, string, len)
410 #define OSAL_STRCMP(str1, str2) strcmp(str1, str2)
411 #define OSAL_STRTOUL(str, base, res) 0
412
413 #define OSAL_INLINE inline
414 #define OSAL_REG_ADDR(_p_hwfn, _offset) \
415                 (void *)((u8 *)(uintptr_t)(_p_hwfn->regview) + (_offset))
416 #define OSAL_PAGE_SIZE 4096
417 #define OSAL_IOMEM volatile
418 #define OSAL_UNLIKELY(x)  __builtin_expect(!!(x), 0)
419 #define OSAL_MIN_T(type, __min1, __min2)        \
420         ((type)(__min1) < (type)(__min2) ? (type)(__min1) : (type)(__min2))
421 #define OSAL_MAX_T(type, __max1, __max2)        \
422         ((type)(__max1) > (type)(__max2) ? (type)(__max1) : (type)(__max2))
423
424 void qede_get_mcp_proto_stats(struct ecore_dev *, enum ecore_mcp_protocol_type,
425                               union ecore_mcp_protocol_stats *);
426 #define OSAL_GET_PROTOCOL_STATS(dev, type, stats) \
427         qede_get_mcp_proto_stats(dev, type, stats)
428
429 #define OSAL_SLOWPATH_IRQ_REQ(p_hwfn) (0)
430 #define OSAL_CRC32(crc, buf, length) 0
431 #define OSAL_CRC8_POPULATE(table, polynomial) nothing
432 #define OSAL_CRC8(table, pdata, nbytes, crc) 0
433 #define OSAL_MFW_TLV_REQ(p_hwfn) (0)
434 #define OSAL_MFW_FILL_TLV_DATA(type, buf, data) (0)
435 #define OSAL_PF_VALIDATE_MODIFY_TUNN_CONFIG(p_hwfn, mask, b_update, tunn) 0
436 #endif /* __BCM_OSAL_H */