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