1 /*
2 * Misc useful os-independent macros and functions.
3 *
4 * Copyright (C) 1999-2013, Broadcom Corporation
5 *
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
11 *
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
19 *
20 * Notwithstanding the above, under no circumstances may you combine this
21 * software in any way with any other Broadcom software provided under a license
22 * other than the GPL, without Broadcom's express prior written consent.
23 *
24 * $Id: bcmutils.h 412804 2013-07-16 16:26:39Z $
25 */
26
27 #ifndef _bcmutils_h_
28 #define _bcmutils_h_
29
30 #define bcm_strcpy_s(dst, noOfElements, src) strcpy((dst), (src))
31 #define bcm_strncpy_s(dst, noOfElements, src, count) strncpy((dst), (src), (count))
32 #define bcm_strcat_s(dst, noOfElements, src) strcat((dst), (src))
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #ifdef PKTQ_LOG
39 #include <wlioctl.h>
40 #endif
41
42 /* ctype replacement */
43 #define _BCM_U 0x01 /* upper */
44 #define _BCM_L 0x02 /* lower */
45 #define _BCM_D 0x04 /* digit */
46 #define _BCM_C 0x08 /* cntrl */
47 #define _BCM_P 0x10 /* punct */
48 #define _BCM_S 0x20 /* white space (space/lf/tab) */
49 #define _BCM_X 0x40 /* hex digit */
50 #define _BCM_SP 0x80 /* hard space (0x20) */
51
52 extern const unsigned char bcm_ctype[];
53 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
54
55 #define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
56 #define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
57 #define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
58 #define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
59 #define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
60 #define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
61 #define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
62 #define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
63 #define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
64 #define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
65 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
66 #define bcm_tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
67 #define bcm_toupper(c) (bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
68
69 /* Buffer structure for collecting string-formatted data
70 * using bcm_bprintf() API.
71 * Use bcm_binit() to initialize before use
72 */
73
74 struct bcmstrbuf {
75 char *buf; /* pointer to current position in origbuf */
76 unsigned int size; /* current (residual) size in bytes */
77 char *origbuf; /* unmodified pointer to orignal buffer */
78 unsigned int origsize; /* unmodified orignal buffer size in bytes */
79 };
80
81 /* ** driver-only section ** */
82 #ifdef BCMDRIVER
83 #include <osl.h>
84
85 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
86
87 /*
88 * Spin at most 'us' microseconds while 'exp' is true.
89 * Caller should explicitly test 'exp' when this completes
90 * and take appropriate error action if 'exp' is still true.
91 */
92 #define SPINWAIT(exp, us) { \
93 uint countdown = (us) + 9; \
94 while ((exp) && (countdown >= 10)) {\
95 OSL_DELAY(10); \
96 countdown -= 10; \
97 } \
98 }
99
100 /* osl multi-precedence packet queue */
101 #ifndef PKTQ_LEN_DEFAULT
102 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
103 #endif
104 #ifndef PKTQ_MAX_PREC
105 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
106 #endif
107
108 typedef struct pktq_prec {
109 void *head; /* first packet to dequeue */
110 void *tail; /* last packet to dequeue */
111 uint16 len; /* number of queued packets */
112 uint16 max; /* maximum number of queued packets */
113 } pktq_prec_t;
114
115 #ifdef PKTQ_LOG
116 typedef struct {
117 uint32 requested; /* packets requested to be stored */
118 uint32 stored; /* packets stored */
119 uint32 saved; /* packets saved,
120 because a lowest priority queue has given away one packet
121 */
122 uint32 selfsaved; /* packets saved,
123 because an older packet from the same queue has been dropped
124 */
125 uint32 full_dropped; /* packets dropped,
126 because pktq is full with higher precedence packets
127 */
128 uint32 dropped; /* packets dropped because pktq per that precedence is full */
129 uint32 sacrificed; /* packets dropped,
130 in order to save one from a queue of a highest priority
131 */
132 uint32 busy; /* packets droped because of hardware/transmission error */
133 uint32 retry; /* packets re-sent because they were not received */
134 uint32 ps_retry; /* packets retried again prior to moving power save mode */
135 uint32 retry_drop; /* packets finally dropped after retry limit */
136 uint32 max_avail; /* the high-water mark of the queue capacity for packets -
137 goes to zero as queue fills
138 */
139 uint32 max_used; /* the high-water mark of the queue utilisation for packets -
140 increases with use ('inverse' of max_avail)
141 */
142 uint32 queue_capacity; /* the maximum capacity of the queue */
143 uint32 rtsfail; /* count of rts attempts that failed to receive cts */
144 uint32 acked; /* count of packets sent (acked) successfully */
145 } pktq_counters_t;
146 #endif /* PKTQ_LOG */
147
148
149 #define PKTQ_COMMON \
150 uint16 num_prec; /* number of precedences in use */ \
151 uint16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */ \
152 uint16 max; /* total max packets */ \
153 uint16 len; /* total number of packets */
154
155 /* multi-priority pkt queue */
156 struct pktq {
157 PKTQ_COMMON
158 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
159 struct pktq_prec q[PKTQ_MAX_PREC];
160 #ifdef PKTQ_LOG
161 pktq_counters_t _prec_cnt[PKTQ_MAX_PREC]; /* Counters per queue */
162 pktq_counters_t _prec_bytes[PKTQ_MAX_PREC]; /* Byte count per queue */
163 uint32 _logtime; /* timestamp of last counter clear */
164 #endif
165 };
166
167 /* simple, non-priority pkt queue */
168 struct spktq {
169 PKTQ_COMMON
170 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
171 struct pktq_prec q[1];
172 };
173
174 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
175
176 /* fn(pkt, arg). return true if pkt belongs to if */
177 typedef bool (*ifpkt_cb_t)(void*, int);
178
179 #ifdef BCMPKTPOOL
180 #define POOL_ENAB(pool) ((pool) && (pool)->inited)
181 #define SHARED_POOL (pktpool_shared)
182 #else /* BCMPKTPOOL */
183 #define POOL_ENAB(bus) 0
184 #define SHARED_POOL ((struct pktpool *)NULL)
185 #endif /* BCMPKTPOOL */
186
187 #ifndef PKTPOOL_LEN_MAX
188 #define PKTPOOL_LEN_MAX 40
189 #endif /* PKTPOOL_LEN_MAX */
190 #define PKTPOOL_CB_MAX 3
191
192 struct pktpool;
193 typedef void (*pktpool_cb_t)(struct pktpool *pool, void *arg);
194 typedef struct {
195 pktpool_cb_t cb;
196 void *arg;
197 } pktpool_cbinfo_t;
198
199 #ifdef BCMDBG_POOL
200 /* pkt pool debug states */
201 #define POOL_IDLE 0
202 #define POOL_RXFILL 1
203 #define POOL_RXDH 2
204 #define POOL_RXD11 3
205 #define POOL_TXDH 4
206 #define POOL_TXD11 5
207 #define POOL_AMPDU 6
208 #define POOL_TXENQ 7
209
210 typedef struct {
211 void *p;
212 uint32 cycles;
213 uint32 dur;
214 } pktpool_dbg_t;
215
216 typedef struct {
217 uint8 txdh; /* tx to host */
218 uint8 txd11; /* tx to d11 */
219 uint8 enq; /* waiting in q */
220 uint8 rxdh; /* rx from host */
221 uint8 rxd11; /* rx from d11 */
222 uint8 rxfill; /* dma_rxfill */
223 uint8 idle; /* avail in pool */
224 } pktpool_stats_t;
225 #endif /* BCMDBG_POOL */
226
227 typedef struct pktpool {
228 bool inited;
229 uint16 r;
230 uint16 w;
231 uint16 len;
232 uint16 maxlen;
233 uint16 plen;
234 bool istx;
235 bool empty;
236 uint8 cbtoggle;
237 uint8 cbcnt;
238 uint8 ecbcnt;
239 bool emptycb_disable;
240 pktpool_cbinfo_t *availcb_excl;
241 pktpool_cbinfo_t cbs[PKTPOOL_CB_MAX];
242 pktpool_cbinfo_t ecbs[PKTPOOL_CB_MAX];
243 void *q[PKTPOOL_LEN_MAX + 1];
244
245 #ifdef BCMDBG_POOL
246 uint8 dbg_cbcnt;
247 pktpool_cbinfo_t dbg_cbs[PKTPOOL_CB_MAX];
248 uint16 dbg_qlen;
249 pktpool_dbg_t dbg_q[PKTPOOL_LEN_MAX + 1];
250 #endif
251 } pktpool_t;
252
253 extern pktpool_t *pktpool_shared;
254
255 extern int pktpool_init(osl_t *osh, pktpool_t *pktp, int *pktplen, int plen, bool istx);
256 extern int pktpool_deinit(osl_t *osh, pktpool_t *pktp);
257 extern int pktpool_fill(osl_t *osh, pktpool_t *pktp, bool minimal);
258 extern void* pktpool_get(pktpool_t *pktp);
259 extern void pktpool_free(pktpool_t *pktp, void *p);
260 extern int pktpool_add(pktpool_t *pktp, void *p);
261 extern uint16 pktpool_avail(pktpool_t *pktp);
262 extern int pktpool_avail_notify_normal(osl_t *osh, pktpool_t *pktp);
263 extern int pktpool_avail_notify_exclusive(osl_t *osh, pktpool_t *pktp, pktpool_cb_t cb);
264 extern int pktpool_avail_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
265 extern int pktpool_empty_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
266 extern int pktpool_setmaxlen(pktpool_t *pktp, uint16 maxlen);
267 extern int pktpool_setmaxlen_strict(osl_t *osh, pktpool_t *pktp, uint16 maxlen);
268 extern void pktpool_emptycb_disable(pktpool_t *pktp, bool disable);
269 extern bool pktpool_emptycb_disabled(pktpool_t *pktp);
270
271 #define POOLPTR(pp) ((pktpool_t *)(pp))
272 #define pktpool_len(pp) (POOLPTR(pp)->len - 1)
273 #define pktpool_plen(pp) (POOLPTR(pp)->plen)
274 #define pktpool_maxlen(pp) (POOLPTR(pp)->maxlen)
275
276 #ifdef BCMDBG_POOL
277 extern int pktpool_dbg_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
278 extern int pktpool_start_trigger(pktpool_t *pktp, void *p);
279 extern int pktpool_dbg_dump(pktpool_t *pktp);
280 extern int pktpool_dbg_notify(pktpool_t *pktp);
281 extern int pktpool_stats_dump(pktpool_t *pktp, pktpool_stats_t *stats);
282 #endif /* BCMDBG_POOL */
283
284 /* forward definition of ether_addr structure used by some function prototypes */
285
286 struct ether_addr;
287
288 extern int ether_isbcast(const void *ea);
289 extern int ether_isnulladdr(const void *ea);
290
291 /* operations on a specific precedence in packet queue */
292
293 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
294 #define pktq_pmax(pq, prec) ((pq)->q[prec].max)
295 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
296 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
297 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
298 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
299
300 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
301 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
302
303 extern void *pktq_penq(struct pktq *pq, int prec, void *p);
304 extern void *pktq_penq_head(struct pktq *pq, int prec, void *p);
305 extern void *pktq_pdeq(struct pktq *pq, int prec);
306 extern void *pktq_pdeq_prev(struct pktq *pq, int prec, void *prev_p);
307 extern void *pktq_pdeq_with_fn(struct pktq *pq, int prec, ifpkt_cb_t fn, int arg);
308 extern void *pktq_pdeq_tail(struct pktq *pq, int prec);
309 /* Empty the queue at particular precedence level */
310 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec, bool dir,
311 ifpkt_cb_t fn, int arg);
312 /* Remove a specified packet from its queue */
313 extern bool pktq_pdel(struct pktq *pq, void *p, int prec);
314
315 /* operations on a set of precedences in packet queue */
316
317 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
318 extern void *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
319 extern void *pktq_mpeek(struct pktq *pq, uint prec_bmp, int *prec_out);
320
321 /* operations on packet queue as a whole */
322
323 #define pktq_len(pq) ((int)(pq)->len)
324 #define pktq_max(pq) ((int)(pq)->max)
325 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
326 #define pktq_full(pq) ((pq)->len >= (pq)->max)
327 #define pktq_empty(pq) ((pq)->len == 0)
328
329 /* operations for single precedence queues */
330 #define pktenq(pq, p) pktq_penq(((struct pktq *)(void *)pq), 0, (p))
331 #define pktenq_head(pq, p) pktq_penq_head(((struct pktq *)(void *)pq), 0, (p))
332 #define pktdeq(pq) pktq_pdeq(((struct pktq *)(void *)pq), 0)
333 #define pktdeq_tail(pq) pktq_pdeq_tail(((struct pktq *)(void *)pq), 0)
334 #define pktqinit(pq, len) pktq_init(((struct pktq *)(void *)pq), 1, len)
335
336 extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
337 extern void pktq_set_max_plen(struct pktq *pq, int prec, int max_len);
338
339 /* prec_out may be NULL if caller is not interested in return value */
340 extern void *pktq_deq(struct pktq *pq, int *prec_out);
341 extern void *pktq_deq_tail(struct pktq *pq, int *prec_out);
342 extern void *pktq_peek(struct pktq *pq, int *prec_out);
343 extern void *pktq_peek_tail(struct pktq *pq, int *prec_out);
344 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir, ifpkt_cb_t fn, int arg);
345
346 /* externs */
347 /* packet */
348 extern uint pktcopy(osl_t *osh, void *p, uint offset, int len, uchar *buf);
349 extern uint pktfrombuf(osl_t *osh, void *p, uint offset, int len, uchar *buf);
350 extern uint pkttotlen(osl_t *osh, void *p);
351 extern void *pktlast(osl_t *osh, void *p);
352 extern uint pktsegcnt(osl_t *osh, void *p);
353 extern uint pktsegcnt_war(osl_t *osh, void *p);
354 extern uint8 *pktdataoffset(osl_t *osh, void *p, uint offset);
355 extern void *pktoffset(osl_t *osh, void *p, uint offset);
356
357 /* Get priority from a packet and pass it back in scb (or equiv) */
358 #define PKTPRIO_VDSCP 0x100 /* DSCP prio found after VLAN tag */
359 #define PKTPRIO_VLAN 0x200 /* VLAN prio found */
360 #define PKTPRIO_UPD 0x400 /* DSCP used to update VLAN prio */
361 #define PKTPRIO_DSCP 0x800 /* DSCP prio found */
362
363 /* DSCP type definitions (RFC4594) */
364 /* AF1x: High-Throughput Data (RFC2597) */
365 #define DSCP_AF11 0x0A
366 #define DSCP_AF12 0x0C
367 #define DSCP_AF13 0x0E
368 /* AF2x: Low-Latency Data (RFC2597) */
369 #define DSCP_AF21 0x12
370 #define DSCP_AF22 0x14
371 #define DSCP_AF23 0x16
372 /* AF3x: Multimedia Streaming (RFC2597) */
373 #define DSCP_AF31 0x1A
374 #define DSCP_AF32 0x1C
375 #define DSCP_AF33 0x1E
376 /* EF: Telephony (RFC3246) */
377 #define DSCP_EF 0x2E
378
379 extern uint pktsetprio(void *pkt, bool update_vtag);
380
381 /* string */
382 extern int bcm_atoi(const char *s);
383 extern ulong bcm_strtoul(const char *cp, char **endp, uint base);
384 extern char *bcmstrstr(const char *haystack, const char *needle);
385 extern char *bcmstrcat(char *dest, const char *src);
386 extern char *bcmstrncat(char *dest, const char *src, uint size);
387 extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
388 char* bcmstrtok(char **string, const char *delimiters, char *tokdelim);
389 int bcmstricmp(const char *s1, const char *s2);
390 int bcmstrnicmp(const char* s1, const char* s2, int cnt);
391
392
393 /* ethernet address */
394 extern char *bcm_ether_ntoa(const struct ether_addr *ea, char *buf);
395 extern int bcm_ether_atoe(const char *p, struct ether_addr *ea);
396
397 /* ip address */
398 struct ipv4_addr;
399 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
400
401 /* delay */
402 extern void bcm_mdelay(uint ms);
403 /* variable access */
404 #define NVRAM_RECLAIM_CHECK(name)
405
406 extern char *getvar(char *vars, const char *name);
407 extern int getintvar(char *vars, const char *name);
408 extern int getintvararray(char *vars, const char *name, int index);
409 extern int getintvararraysize(char *vars, const char *name);
410 extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
411 #define bcm_perf_enable()
412 #define bcmstats(fmt)
413 #define bcmlog(fmt, a1, a2)
414 #define bcmdumplog(buf, size) *buf = '\0'
415 #define bcmdumplogent(buf, idx) -1
416
417 #define bcmtslog(tstamp, fmt, a1, a2)
418 #define bcmprinttslogs()
419 #define bcmprinttstamp(us)
420 #define bcmdumptslog(buf, size)
421
422 extern char *bcm_nvram_vars(uint *length);
423 extern int bcm_nvram_cache(void *sih);
424
425 /* Support for sharing code across in-driver iovar implementations.
426 * The intent is that a driver use this structure to map iovar names
427 * to its (private) iovar identifiers, and the lookup function to
428 * find the entry. Macros are provided to map ids and get/set actions
429 * into a single number space for a switch statement.
430 */
431
432 /* iovar structure */
433 typedef struct bcm_iovar {
434 const char *name; /* name for lookup and display */
435 uint16 varid; /* id for switch */
436 uint16 flags; /* driver-specific flag bits */
437 uint16 type; /* base type of argument */
438 uint16 minlen; /* min length for buffer vars */
439 } bcm_iovar_t;
440
441 /* varid definitions are per-driver, may use these get/set bits */
442
443 /* IOVar action bits for id mapping */
444 #define IOV_GET 0 /* Get an iovar */
445 #define IOV_SET 1 /* Set an iovar */
446
447 /* Varid to actionid mapping */
448 #define IOV_GVAL(id) ((id) * 2)
449 #define IOV_SVAL(id) ((id) * 2 + IOV_SET)
450 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
451 #define IOV_ID(actionid) (actionid >> 1)
452
453 /* flags are per-driver based on driver attributes */
454
455 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
456 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, int len, bool set);
457 #if defined(WLTINYDUMP) || defined(WLMSG_INFORM) || defined(WLMSG_ASSOC) || \
458 defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
459 extern int bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len);
460 #endif
461 #endif /* BCMDRIVER */
462
463 /* Base type definitions */
464 #define IOVT_VOID 0 /* no value (implictly set only) */
465 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
466 #define IOVT_INT8 2 /* integer values are range-checked */
467 #define IOVT_UINT8 3 /* unsigned int 8 bits */
468 #define IOVT_INT16 4 /* int 16 bits */
469 #define IOVT_UINT16 5 /* unsigned int 16 bits */
470 #define IOVT_INT32 6 /* int 32 bits */
471 #define IOVT_UINT32 7 /* unsigned int 32 bits */
472 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
473 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
474
475 /* Initializer for IOV type strings */
476 #define BCM_IOV_TYPE_INIT { \
477 "void", \
478 "bool", \
479 "int8", \
480 "uint8", \
481 "int16", \
482 "uint16", \
483 "int32", \
484 "uint32", \
485 "buffer", \
486 "" }
487
488 #define BCM_IOVT_IS_INT(type) (\
489 (type == IOVT_BOOL) || \
490 (type == IOVT_INT8) || \
491 (type == IOVT_UINT8) || \
492 (type == IOVT_INT16) || \
493 (type == IOVT_UINT16) || \
494 (type == IOVT_INT32) || \
495 (type == IOVT_UINT32))
496
497 /* ** driver/apps-shared section ** */
498
499 #define BCME_STRLEN 64 /* Max string length for BCM errors */
500 #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST))
501
502
503 /*
504 * error codes could be added but the defined ones shouldn't be changed/deleted
505 * these error codes are exposed to the user code
506 * when ever a new error code is added to this list
507 * please update errorstring table with the related error string and
508 * update osl files with os specific errorcode map
509 */
510
511 #define BCME_OK 0 /* Success */
512 #define BCME_ERROR -1 /* Error generic */
513 #define BCME_BADARG -2 /* Bad Argument */
514 #define BCME_BADOPTION -3 /* Bad option */
515 #define BCME_NOTUP -4 /* Not up */
516 #define BCME_NOTDOWN -5 /* Not down */
517 #define BCME_NOTAP -6 /* Not AP */
518 #define BCME_NOTSTA -7 /* Not STA */
519 #define BCME_BADKEYIDX -8 /* BAD Key Index */
520 #define BCME_RADIOOFF -9 /* Radio Off */
521 #define BCME_NOTBANDLOCKED -10 /* Not band locked */
522 #define BCME_NOCLK -11 /* No Clock */
523 #define BCME_BADRATESET -12 /* BAD Rate valueset */
524 #define BCME_BADBAND -13 /* BAD Band */
525 #define BCME_BUFTOOSHORT -14 /* Buffer too short */
526 #define BCME_BUFTOOLONG -15 /* Buffer too long */
527 #define BCME_BUSY -16 /* Busy */
528 #define BCME_NOTASSOCIATED -17 /* Not Associated */
529 #define BCME_BADSSIDLEN -18 /* Bad SSID len */
530 #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
531 #define BCME_BADCHAN -20 /* Bad Channel */
532 #define BCME_BADADDR -21 /* Bad Address */
533 #define BCME_NORESOURCE -22 /* Not Enough Resources */
534 #define BCME_UNSUPPORTED -23 /* Unsupported */
535 #define BCME_BADLEN -24 /* Bad length */
536 #define BCME_NOTREADY -25 /* Not Ready */
537 #define BCME_EPERM -26 /* Not Permitted */
538 #define BCME_NOMEM -27 /* No Memory */
539 #define BCME_ASSOCIATED -28 /* Associated */
540 #define BCME_RANGE -29 /* Not In Range */
541 #define BCME_NOTFOUND -30 /* Not Found */
542 #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
543 #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
544 #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
545 #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
546 #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
547 #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
548 #define BCME_VERSION -37 /* Incorrect version */
549 #define BCME_TXFAIL -38 /* TX failure */
550 #define BCME_RXFAIL -39 /* RX failure */
551 #define BCME_NODEVICE -40 /* Device not present */
552 #define BCME_NMODE_DISABLED -41 /* NMODE disabled */
553 #define BCME_NONRESIDENT -42 /* access to nonresident overlay */
554 #define BCME_SCANREJECT -43 /* reject scan request */
555 #define BCME_USAGE_ERROR -44 /* WLCMD usage error */
556 #define BCME_IOCTL_ERROR -45 /* WLCMD ioctl error */
557 #define BCME_SERIAL_PORT_ERR -46 /* RWL serial port error */
558 #define BCME_LAST BCME_SERIAL_PORT_ERR
559
560 /* These are collection of BCME Error strings */
561 #define BCMERRSTRINGTABLE { \
562 "OK", \
563 "Undefined error", \
564 "Bad Argument", \
565 "Bad Option", \
566 "Not up", \
567 "Not down", \
568 "Not AP", \
569 "Not STA", \
570 "Bad Key Index", \
571 "Radio Off", \
572 "Not band locked", \
573 "No clock", \
574 "Bad Rate valueset", \
575 "Bad Band", \
576 "Buffer too short", \
577 "Buffer too long", \
578 "Busy", \
579 "Not Associated", \
580 "Bad SSID len", \
581 "Out of Range Channel", \
582 "Bad Channel", \
583 "Bad Address", \
584 "Not Enough Resources", \
585 "Unsupported", \
586 "Bad length", \
587 "Not Ready", \
588 "Not Permitted", \
589 "No Memory", \
590 "Associated", \
591 "Not In Range", \
592 "Not Found", \
593 "WME Not Enabled", \
594 "TSPEC Not Found", \
595 "ACM Not Supported", \
596 "Not WME Association", \
597 "SDIO Bus Error", \
598 "Dongle Not Accessible", \
599 "Incorrect version", \
600 "TX Failure", \
601 "RX Failure", \
602 "Device Not Present", \
603 "NMODE Disabled", \
604 "Nonresident overlay access", \
605 "Scan Rejected", \
606 "WLCMD usage error", \
607 "WLCMD ioctl error", \
608 "RWL serial port error", \
609 }
610
611 #ifndef ABS
612 #define ABS(a) (((a) < 0) ? -(a) : (a))
613 #endif /* ABS */
614
615 #ifndef MIN
616 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
617 #endif /* MIN */
618
619 #ifndef MAX
620 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
621 #endif /* MAX */
622
623 /* limit to [min, max] */
624 #ifndef LIMIT_TO_RANGE
625 #define LIMIT_TO_RANGE(x, min, max) \
626 ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
627 #endif /* LIMIT_TO_RANGE */
628
629 /* limit to max */
630 #ifndef LIMIT_TO_MAX
631 #define LIMIT_TO_MAX(x, max) \
632 (((x) > (max) ? (max) : (x)))
633 #endif /* LIMIT_TO_MAX */
634
635 /* limit to min */
636 #ifndef LIMIT_TO_MIN
637 #define LIMIT_TO_MIN(x, min) \
638 (((x) < (min) ? (min) : (x)))
639 #endif /* LIMIT_TO_MIN */
640
641 #define CEIL(x, y) (((x) + ((y) - 1)) / (y))
642 #define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
643 #define ISALIGNED(a, x) (((uintptr)(a) & ((x) - 1)) == 0)
644 #define ALIGN_ADDR(addr, boundary) (void *)(((uintptr)(addr) + (boundary) - 1) \
645 & ~((boundary) - 1))
646 #define ALIGN_SIZE(size, boundary) (((size) + (boundary) - 1) \
647 & ~((boundary) - 1))
648 #define ISPOWEROF2(x) ((((x) - 1) & (x)) == 0)
649 #define VALID_MASK(mask) !((mask) & ((mask) + 1))
650
651 #ifndef OFFSETOF
652 #ifdef __ARMCC_VERSION
653 /*
654 * The ARM RVCT compiler complains when using OFFSETOF where a constant
655 * expression is expected, such as an initializer for a static object.
656 * offsetof from the runtime library doesn't have that problem.
657 */
658 #include <stddef.h>
659 #define OFFSETOF(type, member) offsetof(type, member)
660 #else
661 #define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member)
662 #endif /* __ARMCC_VERSION */
663 #endif /* OFFSETOF */
664
665 #ifndef ARRAYSIZE
666 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
667 #endif
668
669 #ifndef ARRAYLAST /* returns pointer to last array element */
670 #define ARRAYLAST(a) (&a[ARRAYSIZE(a)-1])
671 #endif
672
673 /* Reference a function; used to prevent a static function from being optimized out */
674 extern void *_bcmutils_dummy_fn;
675 #define REFERENCE_FUNCTION(f) (_bcmutils_dummy_fn = (void *)(f))
676
677 /* bit map related macros */
678 #ifndef setbit
679 #ifndef NBBY /* the BSD family defines NBBY */
680 #define NBBY 8 /* 8 bits per byte */
681 #endif /* #ifndef NBBY */
682 #ifdef BCMUTILS_BIT_MACROS_USE_FUNCS
683 extern void setbit(void *array, uint bit);
684 extern void clrbit(void *array, uint bit);
685 extern bool isset(const void *array, uint bit);
686 extern bool isclr(const void *array, uint bit);
687 #else
688 #define setbit(a, i) (((uint8 *)a)[(i) / NBBY] |= 1 << ((i) % NBBY))
689 #define clrbit(a, i) (((uint8 *)a)[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
690 #define isset(a, i) (((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY)))
691 #define isclr(a, i) ((((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
692 #endif
693 #endif /* setbit */
694
695 #define isbitset(a, i) (((a) & (1 << (i))) != 0)
696
697 #define NBITS(type) (sizeof(type) * 8)
698 #define NBITVAL(nbits) (1 << (nbits))
699 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
700 #define NBITMASK(nbits) MAXBITVAL(nbits)
701 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
702
703 /* basic mux operation - can be optimized on several architectures */
704 #define MUX(pred, true, false) ((pred) ? (true) : (false))
705
706 /* modulo inc/dec - assumes x E [0, bound - 1] */
707 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
708 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
709
710 /* modulo inc/dec, bound = 2^k */
711 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
712 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
713
714 /* modulo add/sub - assumes x, y E [0, bound - 1] */
715 #define MODADD(x, y, bound) \
716 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
717 #define MODSUB(x, y, bound) \
718 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
719
720 /* module add/sub, bound = 2^k */
721 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
722 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
723
724 /* crc defines */
725 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
726 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
727 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
728 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
729 #define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
730 #define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
731
732 /* use for direct output of MAC address in printf etc */
733 #define MACF "%02x:%02x:%02x:%02x:%02x:%02x"
734 #define ETHERP_TO_MACF(ea) ((struct ether_addr *) (ea))->octet[0], \
735 ((struct ether_addr *) (ea))->octet[1], \
736 ((struct ether_addr *) (ea))->octet[2], \
737 ((struct ether_addr *) (ea))->octet[3], \
738 ((struct ether_addr *) (ea))->octet[4], \
739 ((struct ether_addr *) (ea))->octet[5]
740
741 #define ETHER_TO_MACF(ea) (ea).octet[0], \
742 (ea).octet[1], \
743 (ea).octet[2], \
744 (ea).octet[3], \
745 (ea).octet[4], \
746 (ea).octet[5]
747 #if !defined(SIMPLE_MAC_PRINT)
748 #define MACDBG "%02x:%02x:%02x:%02x:%02x:%02x"
749 #define MAC2STRDBG(ea) (ea)[0], (ea)[1], (ea)[2], (ea)[3], (ea)[4], (ea)[5]
750 #else
751 #define MACDBG "%02x:%02x:%02x"
752 #define MAC2STRDBG(ea) (ea)[0], (ea)[4], (ea)[5]
753 #endif /* SIMPLE_MAC_PRINT */
754
755 /* bcm_format_flags() bit description structure */
756 typedef struct bcm_bit_desc {
757 uint32 bit;
758 const char* name;
759 } bcm_bit_desc_t;
760
761 /* bcm_format_field */
762 typedef struct bcm_bit_desc_ex {
763 uint32 mask;
764 const bcm_bit_desc_t *bitfield;
765 } bcm_bit_desc_ex_t;
766
767
768 /* tag_ID/length/value_buffer tuple */
769 typedef struct bcm_tlv {
770 uint8 id;
771 uint8 len;
772 uint8 data[1];
773 } bcm_tlv_t;
774
775 /* Check that bcm_tlv_t fits into the given buflen */
776 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
777
778 /* buffer length for ethernet address from bcm_ether_ntoa() */
779 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
780
781 /* crypto utility function */
782 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
783 static INLINE void
xor_128bit_block(const uint8 * src1,const uint8 * src2,uint8 * dst)784 xor_128bit_block(const uint8 *src1, const uint8 *src2, uint8 *dst)
785 {
786 if (
787 #ifdef __i386__
788 1 ||
789 #endif
790 (((uintptr)src1 | (uintptr)src2 | (uintptr)dst) & 3) == 0) {
791 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
792 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
793 ((uint32 *)dst)[0] = ((const uint32 *)src1)[0] ^ ((const uint32 *)src2)[0];
794 ((uint32 *)dst)[1] = ((const uint32 *)src1)[1] ^ ((const uint32 *)src2)[1];
795 ((uint32 *)dst)[2] = ((const uint32 *)src1)[2] ^ ((const uint32 *)src2)[2];
796 ((uint32 *)dst)[3] = ((const uint32 *)src1)[3] ^ ((const uint32 *)src2)[3];
797 } else {
798 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
799 int k;
800 for (k = 0; k < 16; k++)
801 dst[k] = src1[k] ^ src2[k];
802 }
803 }
804
805 /* externs */
806 /* crc */
807 extern uint8 hndcrc8(uint8 *p, uint nbytes, uint8 crc);
808 extern uint16 hndcrc16(uint8 *p, uint nbytes, uint16 crc);
809 extern uint32 hndcrc32(uint8 *p, uint nbytes, uint32 crc);
810
811 /* format/print */
812 #if defined(DHD_DEBUG) || defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || \
813 defined(WLMSG_ASSOC)
814 /* print out the value a field has: fields may have 1-32 bits and may hold any value */
815 extern int bcm_format_field(const bcm_bit_desc_ex_t *bd, uint32 field, char* buf, int len);
816 /* print out which bits in flags are set */
817 extern int bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, int len);
818 #endif
819
820 #if defined(DHD_DEBUG) || defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || \
821 defined(WLMSG_ASSOC) || defined(WLMEDIA_PEAKRATE)
822 extern int bcm_format_hex(char *str, const void *bytes, int len);
823 #endif
824
825 extern const char *bcm_crypto_algo_name(uint algo);
826 extern char *bcm_chipname(uint chipid, char *buf, uint len);
827 extern char *bcm_brev_str(uint32 brev, char *buf);
828 extern void printbig(char *buf);
829 extern void prhex(const char *msg, uchar *buf, uint len);
830
831 /* IE parsing */
832 extern bcm_tlv_t *bcm_next_tlv(bcm_tlv_t *elt, int *buflen);
833 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen, uint key);
834 extern bcm_tlv_t *bcm_parse_ordered_tlvs(void *buf, int buflen, uint key);
835
836 /* bcmerror */
837 extern const char *bcmerrorstr(int bcmerror);
838 /* extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen, uint key); */
839
840 /* multi-bool data type: set of bools, mbool is true if any is set */
841 typedef uint32 mbool;
842 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
843 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
844 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* TRUE if one bool is set */
845 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
846
847 /* generic datastruct to help dump routines */
848 struct fielddesc {
849 const char *nameandfmt;
850 uint32 offset;
851 uint32 len;
852 };
853
854 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
855 extern void bcm_bprhex(struct bcmstrbuf *b, const char *msg, bool newline, uint8 *buf, int len);
856
857 extern void bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount);
858 extern int bcm_cmp_bytes(const uchar *arg1, const uchar *arg2, uint8 nbytes);
859 extern void bcm_print_bytes(const char *name, const uchar *cdata, int len);
860
861 typedef uint32 (*bcmutl_rdreg_rtn)(void *arg0, uint arg1, uint32 offset);
862 extern uint bcmdumpfields(bcmutl_rdreg_rtn func_ptr, void *arg0, uint arg1, struct fielddesc *str,
863 char *buf, uint32 bufsize);
864 extern uint bcm_bitcount(uint8 *bitmap, uint bytelength);
865
866 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
867
868 /* power conversion */
869 extern uint16 bcm_qdbm_to_mw(uint8 qdbm);
870 extern uint8 bcm_mw_to_qdbm(uint16 mw);
871 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf, uint len);
872
873 unsigned int process_nvram_vars(char *varbuf, unsigned int len);
874 extern bcm_tlv_t *find_vendor_ie(void *tlvs, int tlvs_len,
875 const char *voui, uint8 *type, int type_len);
876
877 /* calculate a * b + c */
878 extern void bcm_uint64_multiple_add(uint32* r_high, uint32* r_low, uint32 a, uint32 b, uint32 c);
879 /* calculate a / b */
880 extern void bcm_uint64_divide(uint32* r, uint32 a_high, uint32 a_low, uint32 b);
881
882 #ifdef __cplusplus
883 }
884 #endif
885
886 #endif /* _bcmutils_h_ */
887