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