1/*
2 * $FreeBSD$
3 */
4#ifndef	__RDMA_TYPES_H_
5#define	__RDMA_TYPES_H_
6#include <sys/types.h>
7#include <sys/malloc.h>
8
9
10typedef uint8_t 	u8;
11typedef uint16_t 	u16;
12typedef uint32_t 	u32;
13typedef uint64_t 	u64;
14
15typedef uint8_t		__u8;
16typedef uint16_t	__u16;
17typedef uint32_t	__u32;
18typedef uint64_t	__u64;
19typedef uint8_t		__be8;
20typedef uint16_t	__be16;
21typedef uint32_t	__be32;
22typedef uint64_t	__be64;
23
24typedef	int32_t		__s32;
25
26
27#define LINUX_TYPES_DEFINED
28#define ERR_PTR(err) ((void *)((long)(err)))
29#define IS_ERR(ptr)  ((unsigned long)(ptr) > (unsigned long)(-1000))
30#define PTR_ERR(ptr)    ((long)(ptr))
31
32#ifndef PANIC_IF
33#define PANIC_IF(exp) do {                  \
34	if (exp)                            \
35		panic("BUG func %s line %u: %s", __FUNCTION__, __LINE__, #exp);      \
36} while (0)
37#endif
38
39#define container_of(p, stype, field) ((stype *)(((uint8_t *)(p)) - offsetof(stype, field)))
40
41static __inline int
42find_first_zero_bit(volatile void *p, int max)
43{
44        int b;
45        volatile int *ptr = (volatile int *)p;
46
47        for (b = 0; b < max; b += 32) {
48                if (ptr[b >> 5] != ~0) {
49                        for (;;) {
50                                if ((ptr[b >> 5] & (1 << (b & 0x1f))) == 0)
51                                        return (b);
52                                b++;
53                        }
54                }
55        }
56
57        return (max);
58}
59
60struct kvl {
61        struct kvl *next;
62        unsigned int key;
63        void *value;
64};
65
66#define DEFINE_KVL(x) struct kvl x;
67
68static __inline void *
69kvl_lookup(struct kvl *x, uint32_t key)
70{
71        struct kvl *i;
72        for (i=x->next;i;i=i->next) if (i->key==key) return(i->value);
73        return(0);
74}
75
76static __inline int
77kvl_alloc_above(struct kvl *idp, void *ptr, int starting_id, int *id)
78{
79	int newid = starting_id;
80	struct kvl *i;
81
82        for (i=idp->next;i;i=i->next)
83		if (i->key == newid)
84			return -EEXIST;
85
86        i=malloc(sizeof(struct kvl),M_TEMP,M_NOWAIT);
87        i->key=newid;
88        i->value=ptr;
89        i->next=idp->next;
90        idp->next=i;
91	*id = newid;
92        return(0);
93}
94
95static __inline void
96kvl_delete(struct kvl *idp, int id)
97{
98        /* leak */
99        struct kvl *i, *prev=NULL;
100        for (i=idp->next;i;prev=i,i=i->next)
101                if ((i)->key==id) {
102			if (!prev)
103				idp->next = i->next;
104			else
105				prev->next = i->next;
106			free(i, M_TEMP);
107                        return;
108                }
109}
110
111static __inline void
112kvl_free(struct kvl *idp)
113{
114        struct kvl *i, *tmp;
115        for (i=idp->next;i;i=tmp) {
116		tmp=i->next;
117		free(i, M_TEMP);
118	}
119	idp->next = NULL;
120}
121
122
123#endif
124