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