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