few updates from RMerlin sources
[tomato.git] / release / src-rt-6.x.4708 / include / bcmutils.h
blobddba2f55fbb223f700302b1bdcc9a1cafefa7786
1 /*
2 * Misc useful os-independent macros and functions.
4 * Copyright (C) 2014, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * $Id: bcmutils.h 422014 2013-09-05 14:46:51Z $
21 #ifndef _bcmutils_h_
22 #define _bcmutils_h_
24 #if defined(UNDER_CE)
25 #include <bcmsafestr.h>
26 #else
27 #define bcm_strcpy_s(dst, noOfElements, src) strcpy((dst), (src))
28 #define bcm_strncpy_s(dst, noOfElements, src, count) strncpy((dst), (src), (count))
29 #define bcm_strcat_s(dst, noOfElements, src) strcat((dst), (src))
30 #endif
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
36 #ifdef PKTQ_LOG
37 #include <wlioctl.h>
38 #endif
40 /* ctype replacement */
41 #define _BCM_U 0x01 /* upper */
42 #define _BCM_L 0x02 /* lower */
43 #define _BCM_D 0x04 /* digit */
44 #define _BCM_C 0x08 /* cntrl */
45 #define _BCM_P 0x10 /* punct */
46 #define _BCM_S 0x20 /* white space (space/lf/tab) */
47 #define _BCM_X 0x40 /* hex digit */
48 #define _BCM_SP 0x80 /* hard space (0x20) */
50 #if defined(BCMROMBUILD)
51 extern const unsigned char BCMROMDATA(bcm_ctype)[];
52 #else
53 extern const unsigned char bcm_ctype[];
54 #endif
55 #define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
57 #define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
58 #define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
59 #define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
60 #define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
61 #define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
62 #define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
63 #define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
64 #define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
65 #define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
66 #define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
67 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
68 #define bcm_tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
69 #define bcm_toupper(c) (bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
71 /* Buffer structure for collecting string-formatted data
72 * using bcm_bprintf() API.
73 * Use bcm_binit() to initialize before use
76 struct bcmstrbuf {
77 char *buf; /* pointer to current position in origbuf */
78 unsigned int size; /* current (residual) size in bytes */
79 char *origbuf; /* unmodified pointer to orignal buffer */
80 unsigned int origsize; /* unmodified orignal buffer size in bytes */
83 /* ** driver-only section ** */
84 #ifdef BCMDRIVER
85 #ifdef EFI
86 /* forward declare structyre type */
87 struct spktq;
88 #endif
89 #include <osl.h>
91 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
94 * Spin at most 'us' microseconds while 'exp' is true.
95 * Caller should explicitly test 'exp' when this completes
96 * and take appropriate error action if 'exp' is still true.
98 #ifdef MACOSX
99 #define SPINWAIT_POLL_PERIOD 20
100 #else
101 #define SPINWAIT_POLL_PERIOD 10
102 #endif
104 #define SPINWAIT(exp, us) { \
105 uint countdown = (us) + (SPINWAIT_POLL_PERIOD - 1); \
106 while ((exp) && (countdown >= SPINWAIT_POLL_PERIOD)) {\
107 OSL_DELAY(SPINWAIT_POLL_PERIOD); \
108 countdown -= SPINWAIT_POLL_PERIOD; \
112 /* osl multi-precedence packet queue */
113 #define PKTQ_LEN_MAX 0xFFFF /* Max uint16 65535 packets */
114 #ifndef PKTQ_LEN_DEFAULT
115 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
116 #endif
117 #ifndef PKTQ_MAX_PREC
118 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
119 #endif
121 typedef struct pktq_prec {
122 void *head; /* first packet to dequeue */
123 void *tail; /* last packet to dequeue */
124 uint16 len; /* number of queued packets */
125 uint16 max; /* maximum number of queued packets */
126 } pktq_prec_t;
128 #ifdef PKTQ_LOG
129 typedef struct {
130 uint32 requested; /* packets requested to be stored */
131 uint32 stored; /* packets stored */
132 uint32 saved; /* packets saved,
133 because a lowest priority queue has given away one packet
135 uint32 selfsaved; /* packets saved,
136 because an older packet from the same queue has been dropped
138 uint32 full_dropped; /* packets dropped,
139 because pktq is full with higher precedence packets
141 uint32 dropped; /* packets dropped because pktq per that precedence is full */
142 uint32 sacrificed; /* packets dropped,
143 in order to save one from a queue of a highest priority
145 uint32 busy; /* packets droped because of hardware/transmission error */
146 uint32 retry; /* packets re-sent because they were not received */
147 uint32 ps_retry; /* packets retried again prior to moving power save mode */
148 uint32 suppress; /* packets which were suppressed and not transmitted */
149 uint32 retry_drop; /* packets finally dropped after retry limit */
150 uint32 max_avail; /* the high-water mark of the queue capacity for packets -
151 goes to zero as queue fills
153 uint32 max_used; /* the high-water mark of the queue utilisation for packets -
154 increases with use ('inverse' of max_avail)
156 uint32 queue_capacity; /* the maximum capacity of the queue */
157 uint32 rtsfail; /* count of rts attempts that failed to receive cts */
158 uint32 acked; /* count of packets sent (acked) successfully */
159 uint32 txrate_succ; /* running total of phy rate of packets sent successfully */
160 uint32 txrate_main; /* running totoal of primary phy rate of all packets */
161 uint32 throughput; /* actual data transferred successfully */
162 uint32 airtime; /* cumulative total medium access delay in useconds */
163 uint32 _logtime; /* timestamp of last counter clear */
164 } pktq_counters_t;
166 typedef struct {
167 uint32 _prec_log;
168 pktq_counters_t* _prec_cnt[PKTQ_MAX_PREC]; /* Counters per queue */
169 #ifdef BCMDBG
170 uint32 pps_time; /* time spent in ps pretend state */
171 #endif
172 } pktq_log_t;
173 #endif /* PKTQ_LOG */
176 #define PKTQ_COMMON \
177 uint16 num_prec; /* number of precedences in use */ \
178 uint16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */ \
179 uint16 max; /* total max packets */ \
180 uint16 len; /* total number of packets */
182 /* multi-priority pkt queue */
183 struct pktq {
184 PKTQ_COMMON
185 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
186 struct pktq_prec q[PKTQ_MAX_PREC];
187 #ifdef PKTQ_LOG
188 pktq_log_t* pktqlog;
189 #endif
192 /* simple, non-priority pkt queue */
193 struct spktq {
194 PKTQ_COMMON
195 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
196 struct pktq_prec q[1];
199 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
201 /* fn(pkt, arg). return true if pkt belongs to if */
202 typedef bool (*ifpkt_cb_t)(void*, int);
204 #ifdef BCMPKTPOOL
205 #define POOL_ENAB(pool) ((pool) && (pool)->inited)
206 #if defined(BCM4329C0)
207 #define SHARED_POOL (pktpool_shared_ptr)
208 #else
209 #define SHARED_POOL (pktpool_shared)
210 #endif /* BCM4329C0 */
211 #else /* BCMPKTPOOL */
212 #define POOL_ENAB(bus) 0
213 #define SHARED_POOL ((struct pktpool *)NULL)
214 #endif /* BCMPKTPOOL */
216 #ifndef PKTPOOL_LEN_MAX
217 #define PKTPOOL_LEN_MAX 40
218 #endif /* PKTPOOL_LEN_MAX */
219 #define PKTPOOL_CB_MAX 3
221 struct pktpool;
222 typedef void (*pktpool_cb_t)(struct pktpool *pool, void *arg);
223 typedef struct {
224 pktpool_cb_t cb;
225 void *arg;
226 } pktpool_cbinfo_t;
228 #ifdef BCMDBG_POOL
229 /* pkt pool debug states */
230 #define POOL_IDLE 0
231 #define POOL_RXFILL 1
232 #define POOL_RXDH 2
233 #define POOL_RXD11 3
234 #define POOL_TXDH 4
235 #define POOL_TXD11 5
236 #define POOL_AMPDU 6
237 #define POOL_TXENQ 7
239 typedef struct {
240 void *p;
241 uint32 cycles;
242 uint32 dur;
243 } pktpool_dbg_t;
245 typedef struct {
246 uint8 txdh; /* tx to host */
247 uint8 txd11; /* tx to d11 */
248 uint8 enq; /* waiting in q */
249 uint8 rxdh; /* rx from host */
250 uint8 rxd11; /* rx from d11 */
251 uint8 rxfill; /* dma_rxfill */
252 uint8 idle; /* avail in pool */
253 } pktpool_stats_t;
254 #endif /* BCMDBG_POOL */
256 typedef struct pktpool {
257 bool inited;
258 uint16 r;
259 uint16 w;
260 uint16 len;
261 uint16 maxlen;
262 uint16 plen;
263 bool istx;
264 bool empty;
265 uint8 cbtoggle;
266 uint8 cbcnt;
267 uint8 ecbcnt;
268 bool emptycb_disable;
269 pktpool_cbinfo_t *availcb_excl;
270 pktpool_cbinfo_t cbs[PKTPOOL_CB_MAX];
271 pktpool_cbinfo_t ecbs[PKTPOOL_CB_MAX];
272 void *q[PKTPOOL_LEN_MAX + 1];
274 #ifdef BCMDBG_POOL
275 uint8 dbg_cbcnt;
276 pktpool_cbinfo_t dbg_cbs[PKTPOOL_CB_MAX];
277 uint16 dbg_qlen;
278 pktpool_dbg_t dbg_q[PKTPOOL_LEN_MAX + 1];
279 #endif
280 } pktpool_t;
282 #if defined(BCM4329C0)
283 extern pktpool_t *pktpool_shared_ptr;
284 #else
285 extern pktpool_t *pktpool_shared;
286 #endif /* BCM4329C0 */
288 extern int pktpool_init(osl_t *osh, pktpool_t *pktp, int *pktplen, int plen, bool istx);
289 extern int pktpool_deinit(osl_t *osh, pktpool_t *pktp);
290 extern int pktpool_fill(osl_t *osh, pktpool_t *pktp, bool minimal);
291 extern void* pktpool_get(pktpool_t *pktp);
292 extern void pktpool_free(pktpool_t *pktp, void *p);
293 extern int pktpool_add(pktpool_t *pktp, void *p);
294 extern uint16 pktpool_avail(pktpool_t *pktp);
295 extern int pktpool_avail_notify_normal(osl_t *osh, pktpool_t *pktp);
296 extern int pktpool_avail_notify_exclusive(osl_t *osh, pktpool_t *pktp, pktpool_cb_t cb);
297 extern int pktpool_avail_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
298 extern int pktpool_empty_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
299 extern int pktpool_setmaxlen(pktpool_t *pktp, uint16 maxlen);
300 extern int pktpool_setmaxlen_strict(osl_t *osh, pktpool_t *pktp, uint16 maxlen);
301 extern void pktpool_emptycb_disable(pktpool_t *pktp, bool disable);
302 extern bool pktpool_emptycb_disabled(pktpool_t *pktp);
304 #define POOLPTR(pp) ((pktpool_t *)(pp))
305 #define pktpool_len(pp) (POOLPTR(pp)->len - 1)
306 #define pktpool_plen(pp) (POOLPTR(pp)->plen)
307 #define pktpool_maxlen(pp) (POOLPTR(pp)->maxlen)
309 #ifdef BCMDBG_POOL
310 extern int pktpool_dbg_register(pktpool_t *pktp, pktpool_cb_t cb, void *arg);
311 extern int pktpool_start_trigger(pktpool_t *pktp, void *p);
312 extern int pktpool_dbg_dump(pktpool_t *pktp);
313 extern int pktpool_dbg_notify(pktpool_t *pktp);
314 extern int pktpool_stats_dump(pktpool_t *pktp, pktpool_stats_t *stats);
315 #endif /* BCMDBG_POOL */
317 /* forward definition of ether_addr structure used by some function prototypes */
319 struct ether_addr;
321 extern int ether_isbcast(const void *ea);
322 extern int ether_isnulladdr(const void *ea);
324 /* operations on a specific precedence in packet queue */
326 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
327 #define pktq_pmax(pq, prec) ((pq)->q[prec].max)
328 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
329 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
330 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
331 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
333 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
334 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
336 extern void *pktq_penq(struct pktq *pq, int prec, void *p);
337 extern void *pktq_penq_head(struct pktq *pq, int prec, void *p);
338 extern void *pktq_pdeq(struct pktq *pq, int prec);
339 extern void *pktq_pdeq_prev(struct pktq *pq, int prec, void *prev_p);
340 extern void *pktq_pdeq_tail(struct pktq *pq, int prec);
341 /* Empty the queue at particular precedence level */
342 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec, bool dir,
343 ifpkt_cb_t fn, int arg);
344 /* Remove a specified packet from its queue */
345 extern bool pktq_pdel(struct pktq *pq, void *p, int prec);
347 /* operations on a set of precedences in packet queue */
349 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
350 extern void *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
351 extern void *pktq_mpeek(struct pktq *pq, uint prec_bmp, int *prec_out);
353 /* operations on packet queue as a whole */
355 #define pktq_len(pq) ((int)(pq)->len)
356 #define pktq_max(pq) ((int)(pq)->max)
357 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
358 #define pktq_full(pq) ((pq)->len >= (pq)->max)
359 #define pktq_empty(pq) ((pq)->len == 0)
361 /* operations for single precedence queues */
362 #define pktenq(pq, p) pktq_penq(((struct pktq *)(void *)pq), 0, (p))
363 #define pktenq_head(pq, p) pktq_penq_head(((struct pktq *)(void *)pq), 0, (p))
364 #define pktdeq(pq) pktq_pdeq(((struct pktq *)(void *)pq), 0)
365 #define pktdeq_tail(pq) pktq_pdeq_tail(((struct pktq *)(void *)pq), 0)
366 #define pktqinit(pq, len) pktq_init(((struct pktq *)(void *)pq), 1, len)
368 extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
369 extern void pktq_set_max_plen(struct pktq *pq, int prec, int max_len);
371 /* prec_out may be NULL if caller is not interested in return value */
372 extern void *pktq_deq(struct pktq *pq, int *prec_out);
373 extern void *pktq_deq_tail(struct pktq *pq, int *prec_out);
374 extern void *pktq_peek(struct pktq *pq, int *prec_out);
375 extern void *pktq_peek_tail(struct pktq *pq, int *prec_out);
376 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir, ifpkt_cb_t fn, int arg);
378 /* externs */
379 /* packet */
380 extern uint pktcopy(osl_t *osh, void *p, uint offset, int len, uchar *buf);
381 extern uint pktfrombuf(osl_t *osh, void *p, uint offset, int len, uchar *buf);
382 extern uint pkttotlen(osl_t *osh, void *p);
383 extern void *pktlast(osl_t *osh, void *p);
384 extern uint pktsegcnt(osl_t *osh, void *p);
385 extern uint pktsegcnt_war(osl_t *osh, void *p);
386 extern uint8 *pktdataoffset(osl_t *osh, void *p, uint offset);
387 extern void *pktoffset(osl_t *osh, void *p, uint offset);
389 /* Get priority from a packet and pass it back in scb (or equiv) */
390 #define PKTPRIO_VDSCP 0x100 /* DSCP prio found after VLAN tag */
391 #define PKTPRIO_VLAN 0x200 /* VLAN prio found */
392 #define PKTPRIO_UPD 0x400 /* DSCP used to update VLAN prio */
393 #define PKTPRIO_DSCP 0x800 /* DSCP prio found */
395 extern uint pktsetprio(void *pkt, bool update_vtag);
397 /* string */
398 extern int BCMROMFN(bcm_atoi)(const char *s);
399 extern ulong BCMROMFN(bcm_strtoul)(const char *cp, char **endp, uint base);
400 extern char *BCMROMFN(bcmstrstr)(const char *haystack, const char *needle);
401 extern char *BCMROMFN(bcmstrcat)(char *dest, const char *src);
402 extern char *BCMROMFN(bcmstrncat)(char *dest, const char *src, uint size);
403 extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
404 char* bcmstrtok(char **string, const char *delimiters, char *tokdelim);
405 int bcmstricmp(const char *s1, const char *s2);
406 int bcmstrnicmp(const char* s1, const char* s2, int cnt);
409 /* ethernet address */
410 extern char *bcm_ether_ntoa(const struct ether_addr *ea, char *buf);
411 extern int BCMROMFN(bcm_ether_atoe)(const char *p, struct ether_addr *ea);
413 /* ip address */
414 struct ipv4_addr;
415 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
416 extern char *bcm_ipv6_ntoa(void *ipv6, char *buf);
418 /* delay */
419 extern void bcm_mdelay(uint ms);
420 /* variable access */
421 #if defined(DONGLEBUILD) && !defined(WLTEST)
422 #ifdef BCMDBG
423 #define NVRAM_RECLAIM_CHECK(name) \
424 if (attach_part_reclaimed == TRUE) { \
425 printf("%s: NVRAM already reclaimed, %s\n", __FUNCTION__, (name)); \
426 *(char*) 0 = 0; /* TRAP */ \
427 return NULL; \
429 #else /* BCMDBG */
430 #define NVRAM_RECLAIM_CHECK(name) \
431 if (attach_part_reclaimed == TRUE) { \
432 *(char*) 0 = 0; /* TRAP */ \
433 return NULL; \
435 #endif /* BCMDBG */
436 #else /* DONGLEBUILD && !WLTEST && !BCMINTERNAL && !BCMDBG_DUMP */
437 #define NVRAM_RECLAIM_CHECK(name)
438 #endif
440 extern char *getvar(char *vars, const char *name);
441 extern int getintvar(char *vars, const char *name);
442 extern int getintvararray(char *vars, const char *name, int index);
443 extern int getintvararraysize(char *vars, const char *name);
444 extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
445 #ifdef BCMDBG
446 extern void prpkt(const char *msg, osl_t *osh, void *p0);
447 #endif /* BCMDBG */
448 #ifdef BCMPERFSTATS
449 extern void bcm_perf_enable(void);
450 extern void bcmstats(char *fmt);
451 extern void bcmlog(char *fmt, uint a1, uint a2);
452 extern void bcmdumplog(char *buf, int size);
453 extern int bcmdumplogent(char *buf, uint idx);
454 #else
455 #define bcm_perf_enable()
456 #define bcmstats(fmt)
457 #define bcmlog(fmt, a1, a2)
458 #define bcmdumplog(buf, size) *buf = '\0'
459 #define bcmdumplogent(buf, idx) -1
460 #endif /* BCMPERFSTATS */
462 #define TSF_TICKS_PER_MS 1024
463 #if defined(BCMTSTAMPEDLOGS)
464 /* Store a TSF timestamp and a log line in the log buffer */
465 extern void bcmtslog(uint32 tstamp, char *fmt, uint a1, uint a2);
466 /* Print out the log buffer with timestamps */
467 extern void bcmprinttslogs(void);
468 /* Print out a microsecond timestamp as "sec.ms.us " */
469 extern void bcmprinttstamp(uint32 us);
470 /* Dump to buffer a microsecond timestamp as "sec.ms.us " */
471 extern void bcmdumptslog(char *buf, int size);
472 #else
473 #define bcmtslog(tstamp, fmt, a1, a2)
474 #define bcmprinttslogs()
475 #define bcmprinttstamp(us)
476 #define bcmdumptslog(buf, size)
477 #endif /* BCMTSTAMPEDLOGS */
479 extern char *bcm_nvram_vars(uint *length);
480 extern int bcm_nvram_cache(void *sih);
482 /* Support for sharing code across in-driver iovar implementations.
483 * The intent is that a driver use this structure to map iovar names
484 * to its (private) iovar identifiers, and the lookup function to
485 * find the entry. Macros are provided to map ids and get/set actions
486 * into a single number space for a switch statement.
489 /* iovar structure */
490 typedef struct bcm_iovar {
491 const char *name; /* name for lookup and display */
492 uint16 varid; /* id for switch */
493 uint16 flags; /* driver-specific flag bits */
494 uint16 type; /* base type of argument */
495 uint16 minlen; /* min length for buffer vars */
496 } bcm_iovar_t;
498 /* varid definitions are per-driver, may use these get/set bits */
500 /* IOVar action bits for id mapping */
501 #define IOV_GET 0 /* Get an iovar */
502 #define IOV_SET 1 /* Set an iovar */
504 /* Varid to actionid mapping */
505 #define IOV_GVAL(id) ((id) * 2)
506 #define IOV_SVAL(id) ((id) * 2 + IOV_SET)
507 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
508 #define IOV_ID(actionid) (actionid >> 1)
510 /* flags are per-driver based on driver attributes */
512 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
513 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, int len, bool set);
514 #if defined(WLTINYDUMP) || defined(BCMDBG) || defined(WLMSG_INFORM) || \
515 defined(WLMSG_ASSOC) || defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
516 extern int bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len);
517 #endif /* WLTINYDUMP || BCMDBG || WLMSG_INFORM || WLMSG_ASSOC || WLMSG_PRPKT */
518 #endif /* BCMDRIVER */
520 /* Base type definitions */
521 #define IOVT_VOID 0 /* no value (implictly set only) */
522 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
523 #define IOVT_INT8 2 /* integer values are range-checked */
524 #define IOVT_UINT8 3 /* unsigned int 8 bits */
525 #define IOVT_INT16 4 /* int 16 bits */
526 #define IOVT_UINT16 5 /* unsigned int 16 bits */
527 #define IOVT_INT32 6 /* int 32 bits */
528 #define IOVT_UINT32 7 /* unsigned int 32 bits */
529 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
530 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
532 /* Initializer for IOV type strings */
533 #define BCM_IOV_TYPE_INIT { \
534 "void", \
535 "bool", \
536 "int8", \
537 "uint8", \
538 "int16", \
539 "uint16", \
540 "int32", \
541 "uint32", \
542 "buffer", \
543 "" }
545 #define BCM_IOVT_IS_INT(type) (\
546 (type == IOVT_BOOL) || \
547 (type == IOVT_INT8) || \
548 (type == IOVT_UINT8) || \
549 (type == IOVT_INT16) || \
550 (type == IOVT_UINT16) || \
551 (type == IOVT_INT32) || \
552 (type == IOVT_UINT32))
554 /* ** driver/apps-shared section ** */
556 #define BCME_STRLEN 64 /* Max string length for BCM errors */
557 #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST))
561 * error codes could be added but the defined ones shouldn't be changed/deleted
562 * these error codes are exposed to the user code
563 * when ever a new error code is added to this list
564 * please update errorstring table with the related error string and
565 * update osl files with os specific errorcode map
568 #define BCME_OK 0 /* Success */
569 #define BCME_ERROR -1 /* Error generic */
570 #define BCME_BADARG -2 /* Bad Argument */
571 #define BCME_BADOPTION -3 /* Bad option */
572 #define BCME_NOTUP -4 /* Not up */
573 #define BCME_NOTDOWN -5 /* Not down */
574 #define BCME_NOTAP -6 /* Not AP */
575 #define BCME_NOTSTA -7 /* Not STA */
576 #define BCME_BADKEYIDX -8 /* BAD Key Index */
577 #define BCME_RADIOOFF -9 /* Radio Off */
578 #define BCME_NOTBANDLOCKED -10 /* Not band locked */
579 #define BCME_NOCLK -11 /* No Clock */
580 #define BCME_BADRATESET -12 /* BAD Rate valueset */
581 #define BCME_BADBAND -13 /* BAD Band */
582 #define BCME_BUFTOOSHORT -14 /* Buffer too short */
583 #define BCME_BUFTOOLONG -15 /* Buffer too long */
584 #define BCME_BUSY -16 /* Busy */
585 #define BCME_NOTASSOCIATED -17 /* Not Associated */
586 #define BCME_BADSSIDLEN -18 /* Bad SSID len */
587 #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
588 #define BCME_BADCHAN -20 /* Bad Channel */
589 #define BCME_BADADDR -21 /* Bad Address */
590 #define BCME_NORESOURCE -22 /* Not Enough Resources */
591 #define BCME_UNSUPPORTED -23 /* Unsupported */
592 #define BCME_BADLEN -24 /* Bad length */
593 #define BCME_NOTREADY -25 /* Not Ready */
594 #define BCME_EPERM -26 /* Not Permitted */
595 #define BCME_NOMEM -27 /* No Memory */
596 #define BCME_ASSOCIATED -28 /* Associated */
597 #define BCME_RANGE -29 /* Not In Range */
598 #define BCME_NOTFOUND -30 /* Not Found */
599 #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
600 #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
601 #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
602 #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
603 #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
604 #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
605 #define BCME_VERSION -37 /* Incorrect version */
606 #define BCME_TXFAIL -38 /* TX failure */
607 #define BCME_RXFAIL -39 /* RX failure */
608 #define BCME_NODEVICE -40 /* Device not present */
609 #define BCME_NMODE_DISABLED -41 /* NMODE disabled */
610 #define BCME_NONRESIDENT -42 /* access to nonresident overlay */
611 #define BCME_SCANREJECT -43 /* reject scan request */
612 #define BCME_USAGE_ERROR -44 /* WLCMD usage error */
613 #define BCME_IOCTL_ERROR -45 /* WLCMD ioctl error */
614 #define BCME_SERIAL_PORT_ERR -46 /* RWL serial port error */
615 #define BCME_DISABLED -47 /* Disabled in this build */
616 #define BCME_DECERR -48 /* Decrypt error */
617 #define BCME_ENCERR -49 /* Encrypt error */
618 #define BCME_MICERR -50 /* Integrity/MIC error */
619 #define BCME_REPLAY -51 /* Replay */
620 #define BCME_IE_NOTFOUND -52 /* IE not found */
621 #define BCME_LAST BCME_IE_NOTFOUND
623 /* These are collection of BCME Error strings */
624 #define BCMERRSTRINGTABLE { \
625 "OK", \
626 "Undefined error", \
627 "Bad Argument", \
628 "Bad Option", \
629 "Not up", \
630 "Not down", \
631 "Not AP", \
632 "Not STA", \
633 "Bad Key Index", \
634 "Radio Off", \
635 "Not band locked", \
636 "No clock", \
637 "Bad Rate valueset", \
638 "Bad Band", \
639 "Buffer too short", \
640 "Buffer too long", \
641 "Busy", \
642 "Not Associated", \
643 "Bad SSID len", \
644 "Out of Range Channel", \
645 "Bad Channel", \
646 "Bad Address", \
647 "Not Enough Resources", \
648 "Unsupported", \
649 "Bad length", \
650 "Not Ready", \
651 "Not Permitted", \
652 "No Memory", \
653 "Associated", \
654 "Not In Range", \
655 "Not Found", \
656 "WME Not Enabled", \
657 "TSPEC Not Found", \
658 "ACM Not Supported", \
659 "Not WME Association", \
660 "SDIO Bus Error", \
661 "Dongle Not Accessible", \
662 "Incorrect version", \
663 "TX Failure", \
664 "RX Failure", \
665 "Device Not Present", \
666 "NMODE Disabled", \
667 "Nonresident overlay access", \
668 "Scan Rejected", \
669 "WLCMD usage error", \
670 "WLCMD ioctl error", \
671 "RWL serial port error", \
672 "Disabled", \
673 "Decrypt error", \
674 "Encrypt error", \
675 "MIC error", \
676 "Replay", \
677 "IE not found", \
680 #ifndef ABS
681 #define ABS(a) (((a) < 0) ? -(a) : (a))
682 #endif /* ABS */
684 #ifndef MIN
685 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
686 #endif /* MIN */
688 #ifndef MAX
689 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
690 #endif /* MAX */
692 /* limit to [min, max] */
693 #ifndef LIMIT_TO_RANGE
694 #define LIMIT_TO_RANGE(x, min, max) \
695 ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
696 #endif /* LIMIT_TO_RANGE */
698 /* limit to max */
699 #ifndef LIMIT_TO_MAX
700 #define LIMIT_TO_MAX(x, max) \
701 (((x) > (max) ? (max) : (x)))
702 #endif /* LIMIT_TO_MAX */
704 /* limit to min */
705 #ifndef LIMIT_TO_MIN
706 #define LIMIT_TO_MIN(x, min) \
707 (((x) < (min) ? (min) : (x)))
708 #endif /* LIMIT_TO_MIN */
710 #define CEIL(x, y) (((x) + ((y) - 1)) / (y))
711 #define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
712 #define ISALIGNED(a, x) (((uintptr)(a) & ((x) - 1)) == 0)
713 #define ALIGN_ADDR(addr, boundary) (void *)(((uintptr)(addr) + (boundary) - 1) \
714 & ~((boundary) - 1))
715 #define ALIGN_SIZE(size, boundary) (((size) + (boundary) - 1) \
716 & ~((boundary) - 1))
717 #define ISPOWEROF2(x) ((((x) - 1) & (x)) == 0)
718 #define VALID_MASK(mask) !((mask) & ((mask) + 1))
720 #ifndef OFFSETOF
721 #ifdef __ARMCC_VERSION
723 * The ARM RVCT compiler complains when using OFFSETOF where a constant
724 * expression is expected, such as an initializer for a static object.
725 * offsetof from the runtime library doesn't have that problem.
727 #include <stddef.h>
728 #define OFFSETOF(type, member) offsetof(type, member)
729 #elif __GNUC__ >= 4
730 /* New versions of GCC are also complaining if the usual macro is used */
731 #define OFFSETOF(type, member) __builtin_offsetof(type, member)
732 #else
733 #define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member)
734 #endif /* __ARMCC_VERSION */
735 #endif /* OFFSETOF */
737 #ifndef ARRAYSIZE
738 #define ARRAYSIZE(a) (sizeof(a) / sizeof(a[0]))
739 #endif
741 /* Reference a function; used to prevent a static function from being optimized out */
742 extern void *_bcmutils_dummy_fn;
743 #define REFERENCE_FUNCTION(f) (_bcmutils_dummy_fn = (void *)(f))
745 #if defined(__NetBSD__)
746 /* use internal
747 * setbit/clrbit since it has a cast and netbsd Xbit funcs dont
748 * and the wl driver doesnt cast. this results in us offsetting
749 * incorrectly and corrupting memory.
751 #ifdef setbit
752 #undef setbit
753 #undef clrbit
754 #undef isset
755 #undef isclr
756 #undef NBBY
757 #endif
758 #endif /* __NetBSD__ */
760 /* bit map related macros */
761 #ifndef setbit
762 #ifndef NBBY /* the BSD family defines NBBY */
763 #define NBBY 8 /* 8 bits per byte */
764 #endif /* #ifndef NBBY */
765 #define setbit(a, i) (((uint8 *)a)[(i) / NBBY] |= 1 << ((i) % NBBY))
766 #define clrbit(a, i) (((uint8 *)a)[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
767 #define isset(a, i) (((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY)))
768 #define isclr(a, i) ((((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
769 #endif /* setbit */
771 #define isbitset(a, i) (((a) & (1 << (i))) != 0)
773 #define NBITS(type) (sizeof(type) * 8)
774 #define NBITVAL(nbits) (1 << (nbits))
775 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
776 #define NBITMASK(nbits) MAXBITVAL(nbits)
777 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
779 /* basic mux operation - can be optimized on several architectures */
780 #define MUX(pred, true, false) ((pred) ? (true) : (false))
782 /* modulo inc/dec - assumes x E [0, bound - 1] */
783 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
784 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
786 /* modulo inc/dec, bound = 2^k */
787 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
788 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
790 /* modulo add/sub - assumes x, y E [0, bound - 1] */
791 #define MODADD(x, y, bound) \
792 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
793 #define MODSUB(x, y, bound) \
794 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
796 /* module add/sub, bound = 2^k */
797 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
798 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
800 /* crc defines */
801 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
802 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
803 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
804 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
805 #define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
806 #define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
808 /* use for direct output of MAC address in printf etc */
809 #define MACF "%02x:%02x:%02x:%02x:%02x:%02x"
810 #define ETHERP_TO_MACF(ea) ((struct ether_addr *) (ea))->octet[0], \
811 ((struct ether_addr *) (ea))->octet[1], \
812 ((struct ether_addr *) (ea))->octet[2], \
813 ((struct ether_addr *) (ea))->octet[3], \
814 ((struct ether_addr *) (ea))->octet[4], \
815 ((struct ether_addr *) (ea))->octet[5]
817 #define ETHER_TO_MACF(ea) (ea).octet[0], \
818 (ea).octet[1], \
819 (ea).octet[2], \
820 (ea).octet[3], \
821 (ea).octet[4], \
822 (ea).octet[5]
824 /* bcm_format_flags() bit description structure */
825 typedef struct bcm_bit_desc {
826 uint32 bit;
827 const char* name;
828 } bcm_bit_desc_t;
830 /* bcm_format_field */
831 typedef struct bcm_bit_desc_ex {
832 uint32 mask;
833 const bcm_bit_desc_t *bitfield;
834 } bcm_bit_desc_ex_t;
837 /* tag_ID/length/value_buffer tuple */
838 typedef struct bcm_tlv {
839 uint8 id;
840 uint8 len;
841 uint8 data[1];
842 } bcm_tlv_t;
844 /* Check that bcm_tlv_t fits into the given buflen */
845 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
847 /* buffer length for ethernet address from bcm_ether_ntoa() */
848 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
850 /* crypto utility function */
851 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
852 static INLINE void
853 xor_128bit_block(const uint8 *src1, const uint8 *src2, uint8 *dst)
855 if (
856 #ifdef __i386__
857 1 ||
858 #endif
859 (((uintptr)src1 | (uintptr)src2 | (uintptr)dst) & 3) == 0) {
860 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
861 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
862 ((uint32 *)dst)[0] = ((const uint32 *)src1)[0] ^ ((const uint32 *)src2)[0];
863 ((uint32 *)dst)[1] = ((const uint32 *)src1)[1] ^ ((const uint32 *)src2)[1];
864 ((uint32 *)dst)[2] = ((const uint32 *)src1)[2] ^ ((const uint32 *)src2)[2];
865 ((uint32 *)dst)[3] = ((const uint32 *)src1)[3] ^ ((const uint32 *)src2)[3];
866 } else {
867 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
868 int k;
869 for (k = 0; k < 16; k++)
870 dst[k] = src1[k] ^ src2[k];
874 /* externs */
875 /* crc */
876 extern uint8 BCMROMFN(hndcrc8)(uint8 *p, uint nbytes, uint8 crc);
877 extern uint16 BCMROMFN(hndcrc16)(uint8 *p, uint nbytes, uint16 crc);
878 extern uint32 BCMROMFN(hndcrc32)(uint8 *p, uint nbytes, uint32 crc);
880 /* format/print */
881 #if defined(BCMDBG) || defined(DHD_DEBUG) || defined(BCMDBG_ERR) || \
882 defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || defined(WLMSG_ASSOC)
883 /* print out the value a field has: fields may have 1-32 bits and may hold any value */
884 extern int bcm_format_field(const bcm_bit_desc_ex_t *bd, uint32 field, char* buf, int len);
885 /* print out which bits in flags are set */
886 extern int bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, int len);
887 #endif
889 #if defined(BCMDBG) || defined(DHD_DEBUG) || defined(BCMDBG_ERR) || \
890 defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || defined(WLMSG_ASSOC) || \
891 defined(WLMEDIA_PEAKRATE)
892 extern int bcm_format_hex(char *str, const void *bytes, int len);
893 #endif
895 #ifdef BCMDBG
896 extern void deadbeef(void *p, uint len);
897 #endif
898 extern const char *bcm_crypto_algo_name(uint algo);
899 extern char *bcm_chipname(uint chipid, char *buf, uint len);
900 extern char *bcm_brev_str(uint32 brev, char *buf);
901 extern void printbig(char *buf);
902 extern void prhex(const char *msg, uchar *buf, uint len);
904 /* IE parsing */
905 extern bcm_tlv_t *BCMROMFN(bcm_next_tlv)(bcm_tlv_t *elt, int *buflen);
906 extern bcm_tlv_t *BCMROMFN(bcm_parse_tlvs)(void *buf, int buflen, uint key);
907 extern bcm_tlv_t *BCMROMFN(bcm_parse_ordered_tlvs)(void *buf, int buflen, uint key);
909 /* bcmerror */
910 extern const char *bcmerrorstr(int bcmerror);
911 extern bcm_tlv_t *BCMROMFN(bcm_parse_tlvs)(void *buf, int buflen, uint key);
913 /* multi-bool data type: set of bools, mbool is true if any is set */
914 typedef uint32 mbool;
915 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
916 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
917 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* TRUE if one bool is set */
918 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
920 /* generic datastruct to help dump routines */
921 struct fielddesc {
922 const char *nameandfmt;
923 uint32 offset;
924 uint32 len;
927 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
928 extern void bcm_bprhex(struct bcmstrbuf *b, const char *msg, bool newline, uint8 *buf, int len);
930 extern void bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount);
931 extern int bcm_cmp_bytes(const uchar *arg1, const uchar *arg2, uint8 nbytes);
932 extern void bcm_print_bytes(const char *name, const uchar *cdata, int len);
934 typedef uint32 (*bcmutl_rdreg_rtn)(void *arg0, uint arg1, uint32 offset);
935 extern uint bcmdumpfields(bcmutl_rdreg_rtn func_ptr, void *arg0, uint arg1, struct fielddesc *str,
936 char *buf, uint32 bufsize);
937 extern uint BCMROMFN(bcm_bitcount)(uint8 *bitmap, uint bytelength);
939 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
941 /* power conversion */
942 extern uint16 BCMROMFN(bcm_qdbm_to_mw)(uint8 qdbm);
943 extern uint8 BCMROMFN(bcm_mw_to_qdbm)(uint16 mw);
944 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf, uint len);
946 unsigned int process_nvram_vars(char *varbuf, unsigned int len);
948 /* calculate a * b + c */
949 extern void bcm_uint64_multiple_add(uint32* r_high, uint32* r_low, uint32 a, uint32 b, uint32 c);
950 /* calculate a / b */
951 extern void bcm_uint64_divide(uint32* r, uint32 a_high, uint32 a_low, uint32 b);
952 /* calculate a >> b */
953 void bcm_uint64_right_shift(uint32* r, uint32 a_high, uint32 a_low, uint32 b);
954 #ifdef __cplusplus
956 #endif
957 #endif /* _bcmutils_h_ */