1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#ifndef _BRCMU_UTILS_H_
18#define _BRCMU_UTILS_H_
19
20#include <linux/skbuff.h>
21
22
23
24
25
26
27struct brcmu_strbuf {
28 char *buf;
29 unsigned int size;
30 char *origbuf;
31 unsigned int origsize;
32};
33
34
35
36
37
38
39#define SPINWAIT(exp, us) { \
40 uint countdown = (us) + 9; \
41 while ((exp) && (countdown >= 10)) {\
42 udelay(10); \
43 countdown -= 10; \
44 } \
45}
46
47
48#ifndef PKTQ_LEN_DEFAULT
49#define PKTQ_LEN_DEFAULT 128
50#endif
51#ifndef PKTQ_MAX_PREC
52#define PKTQ_MAX_PREC 16
53#endif
54
55struct pktq_prec {
56 struct sk_buff *head;
57 struct sk_buff *tail;
58 u16 len;
59 u16 max;
60};
61
62
63struct pktq {
64 u16 num_prec;
65 u16 hi_prec;
66 u16 max;
67 u16 len;
68
69
70
71
72 struct pktq_prec q[PKTQ_MAX_PREC];
73};
74
75
76typedef bool(*ifpkt_cb_t) (struct sk_buff *, void *);
77
78
79
80#define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
81#define pktq_plen(pq, prec) ((pq)->q[prec].len)
82#define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
83#define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
84#define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
85
86#define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
87#define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
88
89extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
90 struct sk_buff *p);
91extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
92 struct sk_buff *p);
93extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
94extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
95
96
97extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
98extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
99
100
101extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
102 bool dir, ifpkt_cb_t fn, void *arg);
103
104
105
106extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
107extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
108 int *prec_out);
109
110
111
112#define pktq_len(pq) ((int)(pq)->len)
113#define pktq_max(pq) ((int)(pq)->max)
114#define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
115#define pktq_full(pq) ((pq)->len >= (pq)->max)
116#define pktq_empty(pq) ((pq)->len == 0)
117
118
119#define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p))
120#define pktenq_head(pq, p)\
121 brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p))
122#define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0)
123#define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0)
124#define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len)
125
126extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
127
128extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
129extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
130 ifpkt_cb_t fn, void *arg);
131
132
133
134extern uint brcmu_pktfrombuf(struct sk_buff *p,
135 uint offset, int len, unsigned char *buf);
136extern uint brcmu_pkttotlen(struct sk_buff *p);
137
138
139extern int brcmu_ether_atoe(char *p, u8 *ea);
140
141
142struct ipv4_addr;
143
144#ifdef BCMDBG
145extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
146#else
147#define brcmu_prpkt(a, b)
148#endif
149
150
151
152
153
154
155
156
157
158struct brcmu_iovar {
159 const char *name;
160 u16 varid;
161 u16 flags;
162 u16 type;
163 u16 minlen;
164};
165
166
167
168
169#define IOV_GET 0
170#define IOV_SET 1
171
172
173#define IOV_GVAL(id) ((id)*2)
174#define IOV_SVAL(id) (((id)*2)+IOV_SET)
175#define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
176#define IOV_ID(actionid) (actionid >> 1)
177
178extern const struct
179brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
180 const char *name);
181extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
182 int len, bool set);
183
184
185#define IOVT_VOID 0
186#define IOVT_BOOL 1
187#define IOVT_INT8 2
188#define IOVT_UINT8 3
189#define IOVT_INT16 4
190#define IOVT_UINT16 5
191#define IOVT_INT32 6
192#define IOVT_UINT32 7
193#define IOVT_BUFFER 8
194#define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
195
196
197
198#define BCME_STRLEN 64
199
200#ifndef ABS
201#define ABS(a) (((a) < 0) ? -(a) : (a))
202#endif
203
204#define CEIL(x, y) (((x) + ((y)-1)) / (y))
205#define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
206
207
208#define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
209 (unsigned long)(size))
210
211
212#define PKTBUFSZ 2048
213
214#define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
215
216#ifndef setbit
217#ifndef NBBY
218#define NBBY 8
219#endif
220#define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
221#define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
222#define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
223#define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
224#endif
225
226#define NBITS(type) (sizeof(type) * 8)
227#define NBITVAL(nbits) (1 << (nbits))
228#define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
229#define NBITMASK(nbits) MAXBITVAL(nbits)
230#define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
231
232
233#define MUX(pred, true, false) ((pred) ? (true) : (false))
234
235
236#define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
237#define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
238
239
240#define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
241#define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
242
243
244#define MODADD(x, y, bound) \
245 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
246#define MODSUB(x, y, bound) \
247 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
248
249
250#define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
251#define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
252
253
254#define CRC8_INIT_VALUE 0xff
255#define CRC8_GOOD_VALUE 0x9f
256#define CRC16_INIT_VALUE 0xffff
257#define CRC16_GOOD_VALUE 0xf0b8
258
259
260struct brcmu_bit_desc {
261 u32 bit;
262 const char *name;
263};
264
265
266struct brcmu_tlv {
267 u8 id;
268 u8 len;
269 u8 data[1];
270};
271
272#define ETHER_ADDR_STR_LEN 18
273
274
275
276extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
277
278
279#if defined(BCMDBG)
280extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
281 char *buf, int len);
282extern int brcmu_format_hex(char *str, const void *bytes, int len);
283#endif
284
285extern char *brcmu_chipname(uint chipid, char *buf, uint len);
286
287extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
288 uint key);
289
290
291extern u16 brcmu_qdbm_to_mw(u8 qdbm);
292extern u8 brcmu_mw_to_qdbm(u16 mw);
293
294extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
295extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
296
297extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
298 char *buf, uint len);
299extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
300
301#endif
302