1 /*
2 * Misc useful os-independent macros and functions.
3 *
4 * Copyright (C) 2020, Broadcom.
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 *
21 * <<Broadcom-WL-IPTag/Dual:>>
22 */
23
24 #ifndef _bcmutils_h_
25 #define _bcmutils_h_
26
27 #include <bcmtlv.h>
28
29 /* For now, protect the bcmerror.h */
30 #ifdef BCMUTILS_ERR_CODES
31 #include <bcmerror.h>
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #define bcm_strncpy_s(dst, noOfElements, src, count) strncpy((dst), (src), (count))
39 #ifdef FREEBSD
40 #define bcm_strncat_s(dst, noOfElements, src, count) strcat((dst), (src))
41 #else
42 #define bcm_strncat_s(dst, noOfElements, src, count) strncat((dst), (src), (count))
43 #endif /* FREEBSD */
44 #define bcm_snprintf_s snprintf
45 #define bcm_sprintf_s snprintf
46
47 /*
48 * #define bcm_strcpy_s(dst, count, src) strncpy((dst), (src), (count))
49 * Use bcm_strcpy_s instead as it is a safer option
50 * bcm_strcat_s: Use bcm_strncat_s as a safer option
51 *
52 */
53
54 #define BCM_BIT(x) (1u << (x))
55 /* useful to count number of set bit in x */
56 #define BCM_CLR_FISRT_BIT(x) ((x - 1) & x)
57 /* first bit set in x. Useful to iterate through a mask */
58 #define BCM_FIRST_BIT(x) (BCM_CLR_FISRT_BIT(x)^(x))
59
60 /* Macro to iterate through the set bits in mask.
61 * NOTE: the argument "mask" will be cleared after
62 * the iteration.
63 */
64
65 #define FOREACH_BIT(c, mask)\
66 for (c = BCM_FIRST_BIT(mask); mask != 0; \
67 mask = BCM_CLR_FISRT_BIT(mask), c = BCM_FIRST_BIT(mask))
68
69 /* ctype replacement */
70 #define _BCM_U 0x01 /* upper */
71 #define _BCM_L 0x02 /* lower */
72 #define _BCM_D 0x04 /* digit */
73 #define _BCM_C 0x08 /* cntrl */
74 #define _BCM_P 0x10 /* punct */
75 #define _BCM_S 0x20 /* white space (space/lf/tab) */
76 #define _BCM_X 0x40 /* hex digit */
77 #define _BCM_SP 0x80 /* hard space (0x20) */
78
79 extern const unsigned char bcm_ctype[256];
80 #define bcm_ismask(x) (bcm_ctype[(unsigned char)(x)])
81
82 #define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
83 #define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
84 #define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
85 #define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
86 #define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
87 #define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
88 #define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
89 #define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
90 #define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
91 #define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
92 #define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
93 #define bcm_tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
94 #define bcm_toupper(c) (bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
95
96 #define CIRCULAR_ARRAY_FULL(rd_idx, wr_idx, max) ((wr_idx + 1)%max == rd_idx)
97
98 #define KB(bytes) (((bytes) + 1023) / 1024)
99
100 /* Buffer structure for collecting string-formatted data
101 * using bcm_bprintf() API.
102 * Use bcm_binit() to initialize before use
103 */
104
105 struct bcmstrbuf {
106 char *buf; /* pointer to current position in origbuf */
107 unsigned int size; /* current (residual) size in bytes */
108 char *origbuf; /* unmodified pointer to orignal buffer */
109 unsigned int origsize; /* unmodified orignal buffer size in bytes */
110 };
111
112 #define BCMSTRBUF_LEN(b) (b->size)
113 #define BCMSTRBUF_BUF(b) (b->buf)
114
115 struct ether_addr;
116 extern char *bcm_ether_ntoa(const struct ether_addr *ea, char *buf);
117 extern int bcm_ether_atoe(const char *p, struct ether_addr *ea);
118
119 /* ** driver-only section ** */
120 #ifdef BCMDRIVER
121
122 #include <osl.h>
123 #include <hnd_pktq.h>
124 #include <hnd_pktpool.h>
125
126 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
127
128 /*
129 * Spin at most 'us' microseconds while 'exp' is true.
130 * Caller should explicitly test 'exp' when this completes
131 * and take appropriate error action if 'exp' is still true.
132 */
133 #ifndef SPINWAIT_POLL_PERIOD
134 #define SPINWAIT_POLL_PERIOD 10U
135 #endif
136
137 #ifdef BCMFUZZ
138 /* fake spinwait for fuzzing */
139 #define SPINWAIT(exp, us) { \
140 uint countdown = (exp) != 0 ? 1 : 0; \
141 while (countdown > 0) { \
142 countdown--; \
143 } \
144 }
145
146 #elif defined(PHY_REG_TRACE_FRAMEWORK)
147 #include <phy_utils_log_api.h>
148 #define SPINWAIT(exp, us) { \
149 uint countdown = (us) + (SPINWAIT_POLL_PERIOD - 1U); \
150 phy_utils_log_spinwait_start(); \
151 while (((exp) != 0) && (uint)(countdown >= SPINWAIT_POLL_PERIOD)) { \
152 OSL_DELAY(SPINWAIT_POLL_PERIOD); \
153 countdown -= SPINWAIT_POLL_PERIOD; \
154 } \
155 phy_utils_log_spinwait_end(us, countdown); \
156 }
157
158 #else
159 #define SPINWAIT(exp, us) { \
160 uint countdown = (us) + (SPINWAIT_POLL_PERIOD - 1U); \
161 while (((exp) != 0) && (uint)(countdown >= SPINWAIT_POLL_PERIOD)) { \
162 OSL_DELAY(SPINWAIT_POLL_PERIOD); \
163 countdown -= SPINWAIT_POLL_PERIOD; \
164 } \
165 }
166 #endif /* BCMFUZZ */
167
168 /* forward definition of ether_addr structure used by some function prototypes */
169
170 extern int ether_isbcast(const void *ea);
171 extern int ether_isnulladdr(const void *ea);
172
173 #define UP_TABLE_MAX ((IPV4_TOS_DSCP_MASK >> IPV4_TOS_DSCP_SHIFT) + 1) /* 64 max */
174 #define CORE_SLAVE_PORT_0 0
175 #define CORE_SLAVE_PORT_1 1
176 #define CORE_BASE_ADDR_0 0
177 #define CORE_BASE_ADDR_1 1
178
179 #ifdef DONGLEBUILD
180 /* TRIM Tail bytes from lfrag */
181 extern void pktfrag_trim_tailbytes(osl_t * osh, void* p, uint16 len, uint8 type);
182 #define PKTFRAG_TRIM_TAILBYTES(osh, p, len, type) pktfrag_trim_tailbytes(osh, p, len, type)
183 #else
184 #define PKTFRAG_TRIM_TAILBYTES(osh, p, len, type) PKTSETLEN(osh, p, PKTLEN(osh, p) - len)
185 #endif /* DONGLEBUILD */
186
187 /* externs */
188 /* packet */
189 extern uint pktcopy(osl_t *osh, void *p, uint offset, uint len, uchar *buf);
190 extern uint pktfrombuf(osl_t *osh, void *p, uint offset, uint len, uchar *buf);
191 extern uint pkttotlen(osl_t *osh, void *p);
192 extern uint pkttotcnt(osl_t *osh, void *p);
193 extern void *pktlast(osl_t *osh, void *p);
194 extern uint pktsegcnt(osl_t *osh, void *p);
195 extern uint8 *pktdataoffset(osl_t *osh, void *p, uint offset);
196 extern void *pktoffset(osl_t *osh, void *p, uint offset);
197
198 #ifdef WLCSO
199 extern uint pkttotlen_no_sfhtoe_hdr(osl_t *osh, void *p, uint toe_hdr_len);
200 #else
201 #define pkttotlen_no_sfhtoe_hdr(osh, p, hdrlen) pkttotlen(osh, p)
202 #endif /* WLCSO */
203
204 /* Get priority from a packet and pass it back in scb (or equiv) */
205 #define PKTPRIO_VDSCP 0x100u /* DSCP prio found after VLAN tag */
206 #define PKTPRIO_VLAN 0x200u /* VLAN prio found */
207 #define PKTPRIO_UPD 0x400u /* DSCP used to update VLAN prio */
208 #define PKTPRIO_DSCP 0x800u /* DSCP prio found */
209
210 /* DSCP type definitions (RFC4594) */
211 /* AF1x: High-Throughput Data (RFC2597) */
212 #define DSCP_AF11 0x0Au
213 #define DSCP_AF12 0x0Cu
214 #define DSCP_AF13 0x0Eu
215 /* AF2x: Low-Latency Data (RFC2597) */
216 #define DSCP_AF21 0x12u
217 #define DSCP_AF22 0x14u
218 #define DSCP_AF23 0x16u
219 /* CS2: OAM (RFC2474) */
220 #define DSCP_CS2 0x10u
221 /* AF3x: Multimedia Streaming (RFC2597) */
222 #define DSCP_AF31 0x1Au
223 #define DSCP_AF32 0x1Cu
224 #define DSCP_AF33 0x1Eu
225 /* CS3: Broadcast Video (RFC2474) */
226 #define DSCP_CS3 0x18u
227 /* VA: VOCIE-ADMIT (RFC5865) */
228 #define DSCP_VA 0x2Cu
229 /* EF: Telephony (RFC3246) */
230 #define DSCP_EF 0x2Eu
231 /* CS6: Network Control (RFC2474) */
232 #define DSCP_CS6 0x30u
233 /* CS7: Network Control (RFC2474) */
234 #define DSCP_CS7 0x38u
235
236 extern uint pktsetprio(void *pkt, bool update_vtag);
237 extern uint pktsetprio_qms(void *pkt, uint8* up_table, bool update_vtag);
238 extern bool pktgetdscp(uint8 *pktdata, uint pktlen, uint8 *dscp);
239
240 /* ethernet address */
241 extern uint64 bcm_ether_ntou64(const struct ether_addr *ea) BCMCONSTFN;
242 extern int bcm_addrmask_set(int enable);
243 extern int bcm_addrmask_get(int *val);
244
245 /* ip address */
246 struct ipv4_addr;
247 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
248 extern char *bcm_ipv6_ntoa(void *ipv6, char *buf);
249 extern int bcm_atoipv4(const char *p, struct ipv4_addr *ip);
250
251 /* delay */
252 extern void bcm_mdelay(uint ms);
253 /* variable access */
254 #if defined(BCM_RECLAIM)
255 extern bool _nvram_reclaim_enb;
256 #define NVRAM_RECLAIM_ENAB() (_nvram_reclaim_enb)
257 #ifdef BCMDBG
258 #define NVRAM_RECLAIM_CHECK(name) \
259 if (NVRAM_RECLAIM_ENAB() && (bcm_attach_part_reclaimed == TRUE)) { \
260 printf("NVRAM already reclaimed, %s\n", (name)); \
261 GCC_DIAGNOSTIC_PUSH_SUPPRESS_NULL_DEREF(); \
262 *(char*) 0 = 0; /* TRAP */ \
263 GCC_DIAGNOSTIC_POP(); \
264 return NULL; \
265 }
266 #else /* BCMDBG */
267 #define NVRAM_RECLAIM_CHECK(name) \
268 if (NVRAM_RECLAIM_ENAB() && (bcm_attach_part_reclaimed == TRUE)) { \
269 GCC_DIAGNOSTIC_PUSH_SUPPRESS_NULL_DEREF(); \
270 *(char*) 0 = 0; /* TRAP */ \
271 GCC_DIAGNOSTIC_POP(); \
272 return NULL; \
273 }
274 #endif /* BCMDBG */
275 #else /* BCM_RECLAIM */
276 #define NVRAM_RECLAIM_CHECK(name)
277 #endif /* BCM_RECLAIM */
278
279 #ifdef WL_FWSIGN
280 #define getvar(vars, name) (NULL)
281 #define getintvar(vars, name) (0)
282 #define getintvararray(vars, name, index) (0)
283 #define getintvararraysize(vars, name) (0)
284 #else /* WL_FWSIGN */
285 extern char *getvar(char *vars, const char *name);
286 extern int getintvar(char *vars, const char *name);
287 extern int getintvararray(char *vars, const char *name, int index);
288 extern int getintvararraysize(char *vars, const char *name);
289 #endif /* WL_FWSIGN */
290
291 /* Read an array of values from a possibly slice-specific nvram string */
292 extern int get_uint8_vararray_slicespecific(osl_t *osh, char *vars, char *vars_table_accessor,
293 const char* name, uint8* dest_array, uint dest_size);
294 extern int get_int16_vararray_slicespecific(osl_t *osh, char *vars, char *vars_table_accessor,
295 const char* name, int16* dest_array, uint dest_size);
296 /* Prepend a slice-specific accessor to an nvram string name */
297 extern uint get_slicespecific_var_name(osl_t *osh, char *vars_table_accessor,
298 const char *name, char **name_out);
299
300 extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
301 #ifdef BCMDBG
302 extern void prpkt(const char *msg, osl_t *osh, void *p0);
303 #endif /* BCMDBG */
304 #ifdef BCMPERFSTATS
305 extern void bcm_perf_enable(void);
306 extern void bcmstats(char *fmt);
307 extern void bcmlog(char *fmt, uint a1, uint a2);
308 extern void bcmdumplog(char *buf, int size);
309 extern int bcmdumplogent(char *buf, uint idx);
310 #else
311 #define bcm_perf_enable()
312 #define bcmstats(fmt)
313 #define bcmlog(fmt, a1, a2)
314 #define bcmdumplog(buf, size) *buf = '\0'
315 #define bcmdumplogent(buf, idx) -1
316 #endif /* BCMPERFSTATS */
317
318 #define TSF_TICKS_PER_MS 1000
319 #define TS_ENTER 0xdeadbeef /* Timestamp profiling enter */
320 #define TS_EXIT 0xbeefcafe /* Timestamp profiling exit */
321
322 #if defined(BCMTSTAMPEDLOGS)
323 /* Store a TSF timestamp and a log line in the log buffer */
324 extern void bcmtslog(uint32 tstamp, const char *fmt, uint a1, uint a2);
325 /* Print out the log buffer with timestamps */
326 extern void bcmprinttslogs(void);
327 /* Print out a microsecond timestamp as "sec.ms.us " */
328 extern void bcmprinttstamp(uint32 us);
329 /* Dump to buffer a microsecond timestamp as "sec.ms.us " */
330 extern void bcmdumptslog(struct bcmstrbuf *b);
331 #else
332 #define bcmtslog(tstamp, fmt, a1, a2)
333 #define bcmprinttslogs()
334 #define bcmprinttstamp(us)
335 #define bcmdumptslog(b)
336 #endif /* BCMTSTAMPEDLOGS */
337
338 bool bcm_match_buffers(const uint8 *b1, uint b1_len, const uint8 *b2, uint b2_len);
339
340 /* Support for sharing code across in-driver iovar implementations.
341 * The intent is that a driver use this structure to map iovar names
342 * to its (private) iovar identifiers, and the lookup function to
343 * find the entry. Macros are provided to map ids and get/set actions
344 * into a single number space for a switch statement.
345 */
346
347 /* iovar structure */
348 typedef struct bcm_iovar {
349 const char *name; /* name for lookup and display */
350 uint16 varid; /* id for switch */
351 uint16 flags; /* driver-specific flag bits */
352 uint8 flags2; /* driver-specific flag bits */
353 uint8 type; /* base type of argument */
354 uint16 minlen; /* min length for buffer vars */
355 } bcm_iovar_t;
356
357 /* varid definitions are per-driver, may use these get/set bits */
358
359 /* IOVar action bits for id mapping */
360 #define IOV_GET 0 /* Get an iovar */
361 #define IOV_SET 1 /* Set an iovar */
362
363 /* Varid to actionid mapping */
364 #define IOV_GVAL(id) ((id) * 2)
365 #define IOV_SVAL(id) ((id) * 2 + IOV_SET)
366 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
367 #define IOV_ID(actionid) (actionid >> 1)
368
369 /* flags are per-driver based on driver attributes */
370
371 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
372 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, uint len, bool set);
373
374 /* ioctl structure */
375 typedef struct wlc_ioctl_cmd {
376 uint16 cmd; /**< IOCTL command */
377 uint16 flags; /**< IOCTL command flags */
378 uint16 min_len; /**< IOCTL command minimum argument len (in bytes) */
379 } wlc_ioctl_cmd_t;
380
381 #if defined(WLTINYDUMP) || defined(BCMDBG) || defined(WLMSG_INFORM) || \
382 defined(WLMSG_ASSOC) || defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
383 extern int bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len);
384 #endif /* WLTINYDUMP || BCMDBG || WLMSG_INFORM || WLMSG_ASSOC || WLMSG_PRPKT */
385 #endif /* BCMDRIVER */
386
387 /* string */
388 extern int bcm_atoi(const char *s);
389 extern ulong bcm_strtoul(const char *cp, char **endp, uint base);
390 extern uint64 bcm_strtoull(const char *cp, char **endp, uint base);
391 extern char *bcmstrstr(const char *haystack, const char *needle);
392 extern char *bcmstrnstr(const char *s, uint s_len, const char *substr, uint substr_len);
393 extern char *bcmstrcat(char *dest, const char *src);
394 extern char *bcmstrncat(char *dest, const char *src, uint size);
395 extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
396 char* bcmstrtok(char **string, const char *delimiters, char *tokdelim);
397 int bcmstricmp(const char *s1, const char *s2);
398 int bcmstrnicmp(const char* s1, const char* s2, int cnt);
399 uint16 bcmhex2bin(const uint8* hex, uint hex_len, uint8 *buf, uint buf_len);
400
401 /* Base type definitions */
402 #define IOVT_VOID 0 /* no value (implictly set only) */
403 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
404 #define IOVT_INT8 2 /* integer values are range-checked */
405 #define IOVT_UINT8 3 /* unsigned int 8 bits */
406 #define IOVT_INT16 4 /* int 16 bits */
407 #define IOVT_UINT16 5 /* unsigned int 16 bits */
408 #define IOVT_INT32 6 /* int 32 bits */
409 #define IOVT_UINT32 7 /* unsigned int 32 bits */
410 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
411 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
412
413 /* Initializer for IOV type strings */
414 #define BCM_IOV_TYPE_INIT { \
415 "void", \
416 "bool", \
417 "int8", \
418 "uint8", \
419 "int16", \
420 "uint16", \
421 "int32", \
422 "uint32", \
423 "buffer", \
424 "" }
425
426 #define BCM_IOVT_IS_INT(type) (\
427 (type == IOVT_BOOL) || \
428 (type == IOVT_INT8) || \
429 (type == IOVT_UINT8) || \
430 (type == IOVT_INT16) || \
431 (type == IOVT_UINT16) || \
432 (type == IOVT_INT32) || \
433 (type == IOVT_UINT32))
434
435 /* ** driver/apps-shared section ** */
436
437 #define BCME_STRLEN 64 /* Max string length for BCM errors */
438 #define VALID_BCMERROR(e) valid_bcmerror(e)
439
440 #ifdef DBG_BUS
441 /** tracks non typical execution paths, use gdb with arm sim + firmware dump to read counters */
442 #define DBG_BUS_INC(s, cnt) ((s)->dbg_bus->cnt++)
443 #else
444 #define DBG_BUS_INC(s, cnt)
445 #endif /* DBG_BUS */
446
447 /* BCMUTILS_ERR_CODES is defined to use the error codes from bcmerror.h
448 * otherwise use from this file.
449 */
450 #ifndef BCMUTILS_ERR_CODES
451
452 /*
453 * error codes could be added but the defined ones shouldn't be changed/deleted
454 * these error codes are exposed to the user code
455 * when ever a new error code is added to this list
456 * please update errorstring table with the related error string and
457 * update osl files with os specific errorcode map
458 */
459
460 #define BCME_OK 0 /* Success */
461 #define BCME_ERROR -1 /* Error generic */
462 #define BCME_BADARG -2 /* Bad Argument */
463 #define BCME_BADOPTION -3 /* Bad option */
464 #define BCME_NOTUP -4 /* Not up */
465 #define BCME_NOTDOWN -5 /* Not down */
466 #define BCME_NOTAP -6 /* Not AP */
467 #define BCME_NOTSTA -7 /* Not STA */
468 #define BCME_BADKEYIDX -8 /* BAD Key Index */
469 #define BCME_RADIOOFF -9 /* Radio Off */
470 #define BCME_NOTBANDLOCKED -10 /* Not band locked */
471 #define BCME_NOCLK -11 /* No Clock */
472 #define BCME_BADRATESET -12 /* BAD Rate valueset */
473 #define BCME_BADBAND -13 /* BAD Band */
474 #define BCME_BUFTOOSHORT -14 /* Buffer too short */
475 #define BCME_BUFTOOLONG -15 /* Buffer too long */
476 #define BCME_BUSY -16 /* Busy */
477 #define BCME_NOTASSOCIATED -17 /* Not Associated */
478 #define BCME_BADSSIDLEN -18 /* Bad SSID len */
479 #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
480 #define BCME_BADCHAN -20 /* Bad Channel */
481 #define BCME_BADADDR -21 /* Bad Address */
482 #define BCME_NORESOURCE -22 /* Not Enough Resources */
483 #define BCME_UNSUPPORTED -23 /* Unsupported */
484 #define BCME_BADLEN -24 /* Bad length */
485 #define BCME_NOTREADY -25 /* Not Ready */
486 #define BCME_EPERM -26 /* Not Permitted */
487 #define BCME_NOMEM -27 /* No Memory */
488 #define BCME_ASSOCIATED -28 /* Associated */
489 #define BCME_RANGE -29 /* Not In Range */
490 #define BCME_NOTFOUND -30 /* Not Found */
491 #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
492 #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
493 #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
494 #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
495 #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
496 #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
497 #define BCME_VERSION -37 /* Incorrect version */
498 #define BCME_TXFAIL -38 /* TX failure */
499 #define BCME_RXFAIL -39 /* RX failure */
500 #define BCME_NODEVICE -40 /* Device not present */
501 #define BCME_NMODE_DISABLED -41 /* NMODE disabled */
502 #define BCME_MSCH_DUP_REG -42 /* Duplicate slot registration */
503 #define BCME_SCANREJECT -43 /* reject scan request */
504 #define BCME_USAGE_ERROR -44 /* WLCMD usage error */
505 #define BCME_IOCTL_ERROR -45 /* WLCMD ioctl error */
506 #define BCME_SERIAL_PORT_ERR -46 /* RWL serial port error */
507 #define BCME_DISABLED -47 /* Disabled in this build */
508 #define BCME_DECERR -48 /* Decrypt error */
509 #define BCME_ENCERR -49 /* Encrypt error */
510 #define BCME_MICERR -50 /* Integrity/MIC error */
511 #define BCME_REPLAY -51 /* Replay */
512 #define BCME_IE_NOTFOUND -52 /* IE not found */
513 #define BCME_DATA_NOTFOUND -53 /* Complete data not found in buffer */
514 #define BCME_NOT_GC -54 /* expecting a group client */
515 #define BCME_PRS_REQ_FAILED -55 /* GC presence req failed to sent */
516 #define BCME_NO_P2P_SE -56 /* Could not find P2P-Subelement */
517 #define BCME_NOA_PND -57 /* NoA pending, CB shuld be NULL */
518 #define BCME_FRAG_Q_FAILED -58 /* queueing 80211 frag failedi */
519 #define BCME_GET_AF_FAILED -59 /* Get p2p AF pkt failed */
520 #define BCME_MSCH_NOTREADY -60 /* scheduler not ready */
521 #define BCME_IOV_LAST_CMD -61 /* last batched iov sub-command */
522 #define BCME_MINIPMU_CAL_FAIL -62 /* MiniPMU cal failed */
523 #define BCME_RCAL_FAIL -63 /* Rcal failed */
524 #define BCME_LPF_RCCAL_FAIL -64 /* RCCAL failed */
525 #define BCME_DACBUF_RCCAL_FAIL -65 /* RCCAL failed */
526 #define BCME_VCOCAL_FAIL -66 /* VCOCAL failed */
527 #define BCME_BANDLOCKED -67 /* interface is restricted to a band */
528 #define BCME_BAD_IE_DATA -68 /* Recieved ie with invalid/bad data */
529 #define BCME_REG_FAILED -69 /* Generic registration failed */
530 #define BCME_NOCHAN -70 /* Registration with 0 chans in list */
531 #define BCME_PKTTOSS -71 /* Pkt tossed */
532 #define BCME_DNGL_DEVRESET -72 /* dongle re-attach during DEVRESET */
533 #define BCME_ROAM -73 /* Roam related failures */
534 #define BCME_NO_SIG_FILE -74 /* Signature file is missing */
535
536 #define BCME_LAST BCME_NO_SIG_FILE
537
538 #define BCME_NOTENABLED BCME_DISABLED
539
540 /* This error code is *internal* to the driver, and is not propogated to users. It should
541 * only be used by IOCTL patch handlers as an indication that it did not handle the IOCTL.
542 * (Since the error code is internal, an entry in 'BCMERRSTRINGTABLE' is not required,
543 * nor does it need to be part of any OSL driver-to-OS error code mapping).
544 */
545 #define BCME_IOCTL_PATCH_UNSUPPORTED -9999
546 #if (BCME_LAST <= BCME_IOCTL_PATCH_UNSUPPORTED)
547 #error "BCME_LAST <= BCME_IOCTL_PATCH_UNSUPPORTED"
548 #endif
549
550 /* These are collection of BCME Error strings */
551 #define BCMERRSTRINGTABLE { \
552 "OK", \
553 "Undefined error", \
554 "Bad Argument", \
555 "Bad Option", \
556 "Not up", \
557 "Not down", \
558 "Not AP", \
559 "Not STA", \
560 "Bad Key Index", \
561 "Radio Off", \
562 "Not band locked", \
563 "No clock", \
564 "Bad Rate valueset", \
565 "Bad Band", \
566 "Buffer too short", \
567 "Buffer too long", \
568 "Busy", \
569 "Not Associated", \
570 "Bad SSID len", \
571 "Out of Range Channel", \
572 "Bad Channel", \
573 "Bad Address", \
574 "Not Enough Resources", \
575 "Unsupported", \
576 "Bad length", \
577 "Not Ready", \
578 "Not Permitted", \
579 "No Memory", \
580 "Associated", \
581 "Not In Range", \
582 "Not Found", \
583 "WME Not Enabled", \
584 "TSPEC Not Found", \
585 "ACM Not Supported", \
586 "Not WME Association", \
587 "SDIO Bus Error", \
588 "Dongle Not Accessible", \
589 "Incorrect version", \
590 "TX Failure", \
591 "RX Failure", \
592 "Device Not Present", \
593 "NMODE Disabled", \
594 "Host Offload in device", \
595 "Scan Rejected", \
596 "WLCMD usage error", \
597 "WLCMD ioctl error", \
598 "RWL serial port error", \
599 "Disabled", \
600 "Decrypt error", \
601 "Encrypt error", \
602 "MIC error", \
603 "Replay", \
604 "IE not found", \
605 "Data not found", \
606 "NOT GC", \
607 "PRS REQ FAILED", \
608 "NO P2P SubElement", \
609 "NOA Pending", \
610 "FRAG Q FAILED", \
611 "GET ActionFrame failed", \
612 "scheduler not ready", \
613 "Last IOV batched sub-cmd", \
614 "Mini PMU Cal failed", \
615 "R-cal failed", \
616 "LPF RC Cal failed", \
617 "DAC buf RC Cal failed", \
618 "VCO Cal failed", \
619 "band locked", \
620 "Recieved ie with invalid data", \
621 "registration failed", \
622 "Registration with zero channels", \
623 "pkt toss", \
624 "Dongle Devreset", \
625 "Critical roam in progress", \
626 "Signature file is missing", \
627 }
628 #endif /* BCMUTILS_ERR_CODES */
629
630 #ifndef ABS
631 #define ABS(a) (((a) < 0) ? -(a) : (a))
632 #endif /* ABS */
633
634 #ifndef MIN
635 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
636 #endif /* MIN */
637
638 #ifndef MAX
639 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
640 #endif /* MAX */
641
642 /* limit to [min, max] */
643 #ifndef LIMIT_TO_RANGE
644 #define LIMIT_TO_RANGE(x, min, max) \
645 ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
646 #endif /* LIMIT_TO_RANGE */
647
648 /* limit to max */
649 #ifndef LIMIT_TO_MAX
650 #define LIMIT_TO_MAX(x, max) \
651 (((x) > (max) ? (max) : (x)))
652 #endif /* LIMIT_TO_MAX */
653
654 /* limit to min */
655 #ifndef LIMIT_TO_MIN
656 #define LIMIT_TO_MIN(x, min) \
657 (((x) < (min) ? (min) : (x)))
658 #endif /* LIMIT_TO_MIN */
659
660 #define SIZE_BITS(x) (sizeof(x) * NBBY)
661 #define SIZE_BITS32(x) ((uint)sizeof(x) * NBBY)
662
663 #define DELTA(curr, prev) ((curr) > (prev) ? ((curr) - (prev)) : \
664 (0xffffffff - (prev) + (curr) + 1))
665 #define CEIL(x, y) (((x) + ((y) - 1)) / (y))
666 #define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
667 #define ROUNDDN(p, align) ((p) & ~((align) - 1))
668 #define ISALIGNED(a, x) (((uintptr)(a) & ((x) - 1)) == 0)
669 #define ALIGN_ADDR(addr, boundary) (void *)(((uintptr)(addr) + (boundary) - 1) \
670 & ~((uintptr)(boundary) - 1))
671 #define ALIGN_SIZE(size, boundary) (((size) + (boundary) - 1) \
672 & ~((boundary) - 1))
673 #define ISPOWEROF2(x) ((((x) - 1) & (x)) == 0)
674 #define VALID_MASK(mask) !((mask) & ((mask) + 1))
675
676 #ifndef OFFSETOF
677 #if ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 8))
678 /* GCC 4.8+ complains when using our OFFSETOF macro in array length declarations. */
679 #define OFFSETOF(type, member) __builtin_offsetof(type, member)
680 #else
681 #ifdef BCMFUZZ
682 /* use 0x10 offset to avoid undefined behavior error due to NULL access */
683 #define OFFSETOF(type, member) (((uint)(uintptr)&((type *)0x10)->member) - 0x10)
684 #else
685 #define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member)
686 #endif /* BCMFUZZ */
687 #endif /* GCC 4.8 or newer */
688 #endif /* OFFSETOF */
689
690 #ifndef CONTAINEROF
691 #define CONTAINEROF(ptr, type, member) ((type *)((char *)(ptr) - OFFSETOF(type, member)))
692 #endif /* CONTAINEROF */
693
694 /* substruct size up to and including a member of the struct */
695 #ifndef STRUCT_SIZE_THROUGH
696 #define STRUCT_SIZE_THROUGH(sptr, fname) \
697 (((uint8*)&((sptr)->fname) - (uint8*)(sptr)) + sizeof((sptr)->fname))
698 #endif
699
700 /* Extracting the size of element in a structure */
701 #define SIZE_OF(type, field) sizeof(((type *)0)->field)
702
703 /* Extracting the size of pointer element in a structure */
704 #define SIZE_OF_PV(type, pfield) sizeof(*((type *)0)->pfield)
705
706 #ifndef ARRAYSIZE
707 #define ARRAYSIZE(a) (uint32)(sizeof(a) / sizeof(a[0]))
708 #endif
709
710 #ifndef ARRAYLAST /* returns pointer to last array element */
711 #define ARRAYLAST(a) (&a[ARRAYSIZE(a)-1])
712 #endif
713
714 /* Calculates the required pad size. This is mainly used in register structures */
715 #define PADSZ(start, end) ((((end) - (start)) / 4) + 1)
716
717 /* Reference a function; used to prevent a static function from being optimized out */
718 extern void *_bcmutils_dummy_fn;
719 #define REFERENCE_FUNCTION(f) (_bcmutils_dummy_fn = (void *)(f))
720
721 /* bit map related macros */
722 #ifndef setbit
723 #ifndef NBBY /* the BSD family defines NBBY */
724 #define NBBY 8 /* 8 bits per byte */
725 #endif /* #ifndef NBBY */
726 #ifdef BCMUTILS_BIT_MACROS_USE_FUNCS
727 extern void setbit(void *array, uint bit);
728 extern void clrbit(void *array, uint bit);
729 extern bool isset(const void *array, uint bit);
730 extern bool isclr(const void *array, uint bit);
731 #else
732 #define setbit(a, i) (((uint8 *)a)[(i) / NBBY] |= 1 << ((i) % NBBY))
733 #define clrbit(a, i) (((uint8 *)a)[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
734 #define isset(a, i) (((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY)))
735 #define isclr(a, i) ((((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
736 #endif
737 #endif /* setbit */
738
739 /* read/write/clear field in a consecutive bits in an octet array.
740 * 'addr' is the octet array's start byte address
741 * 'size' is the octet array's byte size
742 * 'stbit' is the value's start bit offset
743 * 'nbits' is the value's bit size
744 * This set of utilities are for convenience. Don't use them
745 * in time critical/data path as there's a great overhead in them.
746 */
747 void setbits(uint8 *addr, uint size, uint stbit, uint nbits, uint32 val);
748 uint32 getbits(const uint8 *addr, uint size, uint stbit, uint nbits);
749 #define clrbits(addr, size, stbit, nbits) setbits(addr, size, stbit, nbits, 0)
750
751 extern void set_bitrange(void *array, uint start, uint end, uint maxbit);
752 extern void clr_bitrange(void *array, uint start, uint end, uint maxbit);
753 extern void set_bitrange_u32(void *array, uint start, uint end, uint maxbit);
754 extern void clr_bitrange_u32(void *array, uint start, uint end, uint maxbit);
755
756 extern int bcm_find_fsb(uint32 num);
757
758 #define isbitset(a, i) (((a) & (1 << (i))) != 0)
759
760 #if defined DONGLEBUILD
761 #define NBITS(type) (sizeof(type) * 8)
762 #else
763 #define NBITS(type) ((uint32)(sizeof(type) * 8))
764 #endif /* DONGLEBUILD */
765 #define NBITVAL(nbits) (1 << (nbits))
766 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
767 #define NBITMASK(nbits) MAXBITVAL(nbits)
768 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
769
770 enum {
771 BCM_FMT_BASE32
772 };
773 typedef int bcm_format_t;
774
775 /* encodes using specified format and returns length of output written on success
776 * or a status code BCME_XX on failure. Input and output buffers may overlap.
777 * input will be advanced to the position when function stoped.
778 * out value of in_len will specify the number of processed input bytes.
779 * on input pad_off represents the number of bits (MSBs of the first output byte)
780 * to preserve and on output number of pad bits (LSBs) set to 0 in the output.
781 */
782 int bcm_encode(uint8 **in, uint *in_len, bcm_format_t fmt,
783 uint *pad_off, uint8 *out, uint out_size);
784
785 /* decodes input in specified format, returns length of output written on success
786 * or a status code BCME_XX on failure. Input and output buffers may overlap.
787 * input will be advanced to the position when function stoped.
788 * out value of in_len will specify the number of processed input bytes.
789 * on input pad_off represents the number of bits (MSBs of the first output byte)
790 * to preserve and on output number of pad bits (LSBs) set to 0 in the output.
791 */
792 int bcm_decode(const uint8 **in, uint *in_len, bcm_format_t fmt,
793 uint *pad_off, uint8 *out, uint out_size);
794
795 extern void bcm_bitprint32(const uint32 u32);
796
797 /*
798 * ----------------------------------------------------------------------------
799 * Multiword map of 2bits, nibbles
800 * setbit2 setbit4 (void *ptr, uint32 ix, uint32 val)
801 * getbit2 getbit4 (void *ptr, uint32 ix)
802 * ----------------------------------------------------------------------------
803 */
804
805 #define DECLARE_MAP_API(NB, RSH, LSH, OFF, MSK) \
806 static INLINE void setbit##NB(void *ptr, uint32 ix, uint32 val) \
807 { \
808 uint32 *addr = (uint32 *)ptr; \
809 uint32 *a = addr + (ix >> RSH); /* (ix / 2^RSH) */ \
810 uint32 pos = (ix & OFF) << LSH; /* (ix % 2^RSH) * 2^LSH */ \
811 uint32 mask = (MSK << pos); \
812 uint32 tmp = *a & ~mask; \
813 *a = tmp | (val << pos); \
814 } \
815 static INLINE uint32 getbit##NB(void *ptr, uint32 ix) \
816 { \
817 uint32 *addr = (uint32 *)ptr; \
818 uint32 *a = addr + (ix >> RSH); \
819 uint32 pos = (ix & OFF) << LSH; \
820 return ((*a >> pos) & MSK); \
821 }
822
823 DECLARE_MAP_API(2, 4, 1, 15u, 0x0003u) /* setbit2() and getbit2() */
824 DECLARE_MAP_API(4, 3, 2, 7u, 0x000Fu) /* setbit4() and getbit4() */
825 DECLARE_MAP_API(8, 2, 3, 3u, 0x00FFu) /* setbit8() and getbit8() */
826
827 /* basic mux operation - can be optimized on several architectures */
828 #define MUX(pred, true, false) ((pred) ? (true) : (false))
829
830 /* modulo inc/dec - assumes x E [0, bound - 1] */
831 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
832 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
833
834 /* modulo inc/dec, bound = 2^k */
835 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
836 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
837
838 /* modulo add/sub - assumes x, y E [0, bound - 1] */
839 #define MODADD(x, y, bound) \
840 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
841 #define MODSUB(x, y, bound) \
842 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
843
844 /* module add/sub, bound = 2^k */
845 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
846 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
847
848 /* crc defines */
849 #define CRC8_INIT_VALUE 0xffu /* Initial CRC8 checksum value */
850 #define CRC8_GOOD_VALUE 0x9fu /* Good final CRC8 checksum value */
851 #define CRC16_INIT_VALUE 0xffffu /* Initial CRC16 checksum value */
852 #define CRC16_GOOD_VALUE 0xf0b8u /* Good final CRC16 checksum value */
853 #define CRC32_INIT_VALUE 0xffffffffu /* Initial CRC32 checksum value */
854 #define CRC32_GOOD_VALUE 0xdebb20e3u /* Good final CRC32 checksum value */
855
856 #ifdef DONGLEBUILD
857 #define MACF "MACADDR:%08x%04x"
858 #define ETHERP_TO_MACF(ea) (uint32)bcm_ether_ntou64(ea), \
859 (uint32)(bcm_ether_ntou64(ea) >> 32)
860
861 #define CONST_ETHERP_TO_MACF(ea) ETHERP_TO_MACF(ea)
862
863 #define ETHER_TO_MACF(ea) ETHERP_TO_MACF(&ea)
864
865 #else
866 /* use for direct output of MAC address in printf etc */
867 #define MACF "%02x:%02x:%02x:%02x:%02x:%02x"
868 #define ETHERP_TO_MACF(ea) ((const struct ether_addr *) (ea))->octet[0], \
869 ((const struct ether_addr *) (ea))->octet[1], \
870 ((const struct ether_addr *) (ea))->octet[2], \
871 ((const struct ether_addr *) (ea))->octet[3], \
872 ((const struct ether_addr *) (ea))->octet[4], \
873 ((const struct ether_addr *) (ea))->octet[5]
874
875 #define CONST_ETHERP_TO_MACF(ea) ETHERP_TO_MACF(ea)
876
877 #define ETHER_TO_MACF(ea) (ea).octet[0], \
878 (ea).octet[1], \
879 (ea).octet[2], \
880 (ea).octet[3], \
881 (ea).octet[4], \
882 (ea).octet[5]
883 #endif /* DONGLEBUILD */
884 /* use only for debug, the string length can be changed
885 * If you want to use this macro to the logic,
886 * USE MACF instead
887 */
888 #if !defined(SIMPLE_MAC_PRINT)
889 #define MACDBG "%02x:%02x:%02x:%02x:%02x:%02x"
890 #define MAC2STRDBG(ea) ((const uint8*)(ea))[0], \
891 ((const uint8*)(ea))[1], \
892 ((const uint8*)(ea))[2], \
893 ((const uint8*)(ea))[3], \
894 ((const uint8*)(ea))[4], \
895 ((const uint8*)(ea))[5]
896 #else
897 #define MACDBG "%02x:xx:xx:xx:x%x:%02x"
898 #define MAC2STRDBG(ea) ((const uint8*)(ea))[0], \
899 (((const uint8*)(ea))[4] & 0xf), \
900 ((const uint8*)(ea))[5]
901 #endif /* SIMPLE_MAC_PRINT */
902
903 #define MACOUIDBG "%02x:%x:%02x"
904 #define MACOUI2STRDBG(ea) ((const uint8*)(ea))[0], \
905 ((const uint8*)(ea))[1] & 0xf, \
906 ((const uint8*)(ea))[2]
907
908 #define MACOUI "%02x:%02x:%02x"
909 #define MACOUI2STR(ea) (ea)[0], (ea)[1], (ea)[2]
910
911 /* bcm_format_flags() bit description structure */
912 typedef struct bcm_bit_desc {
913 uint32 bit;
914 const char* name;
915 } bcm_bit_desc_t;
916
917 /* bcm_format_field */
918 typedef struct bcm_bit_desc_ex {
919 uint32 mask;
920 const bcm_bit_desc_t *bitfield;
921 } bcm_bit_desc_ex_t;
922
923 /* buffer length for ethernet address from bcm_ether_ntoa() */
924 #define ETHER_ADDR_STR_LEN 18u /* 18-bytes of Ethernet address buffer length */
925
926 static INLINE uint32 /* 32bit word aligned xor-32 */
bcm_compute_xor32(volatile uint32 * u32_val,int num_u32)927 bcm_compute_xor32(volatile uint32 *u32_val, int num_u32)
928 {
929 int idx;
930 uint32 xor32 = 0;
931 for (idx = 0; idx < num_u32; idx++)
932 xor32 ^= *(u32_val + idx);
933 return xor32;
934 }
935
936 /* crypto utility function */
937 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
938 static INLINE void
xor_128bit_block(const uint8 * src1,const uint8 * src2,uint8 * dst)939 xor_128bit_block(const uint8 *src1, const uint8 *src2, uint8 *dst)
940 {
941 if (
942 #ifdef __i386__
943 1 ||
944 #endif
945 (((uintptr)src1 | (uintptr)src2 | (uintptr)dst) & 3) == 0) {
946 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
947 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
948 ((uint32 *)dst)[0] = ((const uint32 *)src1)[0] ^ ((const uint32 *)src2)[0];
949 ((uint32 *)dst)[1] = ((const uint32 *)src1)[1] ^ ((const uint32 *)src2)[1];
950 ((uint32 *)dst)[2] = ((const uint32 *)src1)[2] ^ ((const uint32 *)src2)[2];
951 ((uint32 *)dst)[3] = ((const uint32 *)src1)[3] ^ ((const uint32 *)src2)[3];
952 } else {
953 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
954 int k;
955 for (k = 0; k < 16; k++)
956 dst[k] = src1[k] ^ src2[k];
957 }
958 }
959
960 /* externs */
961 /* crc */
962 uint8 hndcrc8(const uint8 *p, uint nbytes, uint8 crc);
963 uint16 hndcrc16(const uint8 *p, uint nbytes, uint16 crc);
964 uint32 hndcrc32(const uint8 *p, uint nbytes, uint32 crc);
965
966 /* format/print */
967 /* print out the value a field has: fields may have 1-32 bits and may hold any value */
968 extern uint bcm_format_field(const bcm_bit_desc_ex_t *bd, uint32 field, char* buf, uint len);
969 /* print out which bits in flags are set */
970 extern int bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, uint len);
971 /* print out whcih bits in octet array 'addr' are set. bcm_bit_desc_t:bit is a bit offset. */
972 int bcm_format_octets(const bcm_bit_desc_t *bd, uint bdsz,
973 const uint8 *addr, uint size, char *buf, uint len);
974
975 extern int bcm_format_hex(char *str, const void *bytes, uint len);
976
977 #ifdef BCMDBG
978 extern void deadbeef(void *p, uint len);
979 #endif
980 extern const char *bcm_crypto_algo_name(uint algo);
981 extern char *bcm_chipname(uint chipid, char *buf, uint len);
982 extern char *bcm_brev_str(uint32 brev, char *buf);
983 extern void printbig(char *buf);
984 extern void prhex(const char *msg, const uchar *buf, uint len);
985 extern void prhexstr(const char *prefix, const uint8 *buf, uint len, bool newline);
986
987 /* bcmerror */
988 extern const char *bcmerrorstr(int bcmerror);
989
990 #if defined(BCMDBG) || defined(WLMSG_ASSOC)
991 /* get 802.11 frame name based on frame kind - see frame types FC_.. in 802.11.h */
992 const char *bcm_80211_fk_name(uint fk);
993 #else
994 #define bcm_80211_fk_names(_x) ""
995 #endif
996
997 extern int wl_set_up_table(uint8 *up_table, bcm_tlv_t *qos_map_ie);
998
999 /* multi-bool data type: set of bools, mbool is true if any is set */
1000 typedef uint32 mbool;
1001 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
1002 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
1003 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* TRUE if one bool is set */
1004 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
1005
1006 /* generic datastruct to help dump routines */
1007 struct fielddesc {
1008 const char *nameandfmt;
1009 uint32 offset;
1010 uint32 len;
1011 };
1012
1013 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
1014 #define bcm_bsize(b) ((b)->size)
1015 #define bcm_breset(b) do {bcm_binit(b, (b)->origbuf, (b)->origsize);} while (0)
1016 extern void bcm_bprhex(struct bcmstrbuf *b, const char *msg, bool newline,
1017 const uint8 *buf, uint len);
1018
1019 extern void bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount);
1020 extern int bcm_cmp_bytes(const uchar *arg1, const uchar *arg2, uint8 nbytes);
1021 extern void bcm_print_bytes(const char *name, const uchar *cdata, uint len);
1022
1023 typedef uint32 (*bcmutl_rdreg_rtn)(void *arg0, uint arg1, uint32 offset);
1024 extern uint bcmdumpfields(bcmutl_rdreg_rtn func_ptr, void *arg0, uint arg1, struct fielddesc *str,
1025 char *buf, uint32 bufsize);
1026 extern uint bcm_bitcount(const uint8 *bitmap, uint bytelength);
1027
1028 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
1029
1030 /* power conversion */
1031 extern uint16 bcm_qdbm_to_mw(uint8 qdbm);
1032 extern uint8 bcm_mw_to_qdbm(uint16 mw);
1033 extern uint bcm_mkiovar(const char *name, const char *data, uint datalen, char *buf, uint len);
1034
1035 #ifdef BCMDBG_PKT /* pkt logging for debugging */
1036 #define PKTLIST_SIZE 3000
1037
1038 #ifdef BCMDBG_PTRACE
1039 #define PKTTRACE_MAX_BYTES 12
1040 #define PKTTRACE_MAX_BITS (PKTTRACE_MAX_BYTES * NBBY)
1041
1042 enum pkttrace_info {
1043 PKTLIST_PRECQ, /* Pkt in Prec Q */
1044 PKTLIST_FAIL_PRECQ, /* Pkt failed to Q in PRECQ */
1045 PKTLIST_DMAQ, /* Pkt in DMA Q */
1046 PKTLIST_MI_TFS_RCVD, /* Received TX status */
1047 PKTLIST_TXDONE, /* Pkt TX done */
1048 PKTLIST_TXFAIL, /* Pkt TX failed */
1049 PKTLIST_PKTFREE, /* pkt is freed */
1050 PKTLIST_PRECREQ, /* Pkt requeued in precq */
1051 PKTLIST_TXFIFO /* To trace in wlc_fifo */
1052 };
1053 #endif /* BCMDBG_PTRACE */
1054
1055 typedef struct pkt_dbginfo {
1056 int line;
1057 char *file;
1058 void *pkt;
1059 #ifdef BCMDBG_PTRACE
1060 char pkt_trace[PKTTRACE_MAX_BYTES];
1061 #endif /* BCMDBG_PTRACE */
1062 } pkt_dbginfo_t;
1063
1064 typedef struct {
1065 pkt_dbginfo_t list[PKTLIST_SIZE]; /* List of pointers to packets */
1066 uint16 count; /* Total count of the packets */
1067 } pktlist_info_t;
1068
1069 extern void pktlist_add(pktlist_info_t *pktlist, void *p, int len, char *file);
1070 extern void pktlist_remove(pktlist_info_t *pktlist, void *p);
1071 extern char* pktlist_dump(pktlist_info_t *pktlist, char *buf);
1072 #ifdef BCMDBG_PTRACE
1073 extern void pktlist_trace(pktlist_info_t *pktlist, void *pkt, uint16 bit);
1074 #endif /* BCMDBG_PTRACE */
1075 #endif /* BCMDBG_PKT */
1076 unsigned int process_nvram_vars(char *varbuf, unsigned int len);
1077 bool replace_nvram_variable(char *varbuf, unsigned int buflen, const char *variable,
1078 unsigned int *datalen);
1079
1080 /* trace any object allocation / free, with / without features (flags) set to the object */
1081 #if (defined(DONGLEBUILD) && defined(BCMDBG_MEM) && (!defined(BCM_OBJECT_TRACE)))
1082 #define BCM_OBJECT_TRACE
1083 #endif /* (defined(DONGLEBUILD) && defined(BCMDBG_MEM) && (!defined(BCM_OBJECT_TRACE))) */
1084
1085 #define BCM_OBJDBG_ADD 1
1086 #define BCM_OBJDBG_REMOVE 2
1087 #define BCM_OBJDBG_ADD_PKT 3
1088
1089 /* object feature: set or clear flags */
1090 #define BCM_OBJECT_FEATURE_FLAG 1
1091 #define BCM_OBJECT_FEATURE_PKT_STATE 2
1092 /* object feature: flag bits */
1093 #define BCM_OBJECT_FEATURE_0 (1 << 0)
1094 #define BCM_OBJECT_FEATURE_1 (1 << 1)
1095 #define BCM_OBJECT_FEATURE_2 (1 << 2)
1096 /* object feature: clear flag bits field set with this flag */
1097 #define BCM_OBJECT_FEATURE_CLEAR (1 << 31)
1098 #if defined(BCM_OBJECT_TRACE) && !defined(BINCMP)
1099 #define bcm_pkt_validate_chk(obj, func) do { \
1100 void * pkttag; \
1101 bcm_object_trace_chk(obj, 0, 0, \
1102 func, __LINE__); \
1103 if ((pkttag = PKTTAG(obj))) { \
1104 bcm_object_trace_chk(obj, 1, DHD_PKTTAG_SN(pkttag), \
1105 func, __LINE__); \
1106 } \
1107 } while (0)
1108 extern void bcm_object_trace_opr(void *obj, uint32 opt, const char *caller, int line);
1109 extern void bcm_object_trace_upd(void *obj, void *obj_new);
1110 extern void bcm_object_trace_chk(void *obj, uint32 chksn, uint32 sn,
1111 const char *caller, int line);
1112 extern void bcm_object_feature_set(void *obj, uint32 type, uint32 value);
1113 extern int bcm_object_feature_get(void *obj, uint32 type, uint32 value);
1114 extern void bcm_object_trace_init(void);
1115 extern void bcm_object_trace_deinit(void);
1116 #else
1117 #define bcm_pkt_validate_chk(obj, func)
1118 #define bcm_object_trace_opr(a, b, c, d)
1119 #define bcm_object_trace_upd(a, b)
1120 #define bcm_object_trace_chk(a, b, c, d, e)
1121 #define bcm_object_feature_set(a, b, c)
1122 #define bcm_object_feature_get(a, b, c)
1123 #define bcm_object_trace_init()
1124 #define bcm_object_trace_deinit()
1125 #endif /* BCM_OBJECT_TRACE && !BINCMP */
1126
1127 /* Public domain bit twiddling hacks/utilities: Sean Eron Anderson */
1128
1129 /* Table driven count set bits. */
1130 static const uint8 /* Table only for use by bcm_cntsetbits */
1131 _CSBTBL[256] =
1132 {
1133 #define B2(n) n, n + 1, n + 1, n + 2
1134 #define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2)
1135 #define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2)
1136 B6(0), B6(0 + 1), B6(0 + 1), B6(0 + 2)
1137 };
1138
1139 static INLINE uint32 /* Uses table _CSBTBL for fast counting of 1's in a u32 */
bcm_cntsetbits(const uint32 u32arg)1140 bcm_cntsetbits(const uint32 u32arg)
1141 {
1142 /* function local scope declaration of const _CSBTBL[] */
1143 const uint8 * p = (const uint8 *)&u32arg;
1144 /* uint32 cast to avoid uint8 being promoted to int for arithmetic operation */
1145 return ((uint32)_CSBTBL[p[0]] + _CSBTBL[p[1]] + _CSBTBL[p[2]] + _CSBTBL[p[3]]);
1146 }
1147
1148 static INLINE int /* C equivalent count of leading 0's in a u32 */
C_bcm_count_leading_zeros(uint32 u32arg)1149 C_bcm_count_leading_zeros(uint32 u32arg)
1150 {
1151 int shifts = 0;
1152 while (u32arg) {
1153 shifts++; u32arg >>= 1;
1154 }
1155 return (32 - shifts);
1156 }
1157
1158 typedef struct bcm_rand_metadata {
1159 uint32 count; /* number of random numbers in bytes */
1160 uint32 signature; /* host fills it in, FW verfies before reading rand */
1161 } bcm_rand_metadata_t;
1162
1163 #ifdef BCMDRIVER
1164 /*
1165 * Assembly instructions: Count Leading Zeros
1166 * "clz" : MIPS, ARM
1167 * "cntlzw" : PowerPC
1168 * "BSF" : x86
1169 * "lzcnt" : AMD, SPARC
1170 */
1171
1172 #if defined(__arm__)
1173 #if defined(__ARM_ARCH_7M__) /* Cortex M3 */
1174 #define __USE_ASM_CLZ__
1175 #endif /* __ARM_ARCH_7M__ */
1176 #if defined(__ARM_ARCH_7R__) /* Cortex R4 */
1177 #define __USE_ASM_CLZ__
1178 #endif /* __ARM_ARCH_7R__ */
1179 #endif /* __arm__ */
1180
1181 static INLINE int
bcm_count_leading_zeros(uint32 u32arg)1182 bcm_count_leading_zeros(uint32 u32arg)
1183 {
1184 #if defined(__USE_ASM_CLZ__)
1185 int zeros;
1186 __asm__ volatile("clz %0, %1 \n" : "=r" (zeros) : "r" (u32arg));
1187 return zeros;
1188 #else /* C equivalent */
1189 return C_bcm_count_leading_zeros(u32arg);
1190 #endif /* C equivalent */
1191 }
1192
1193 /*
1194 * Macro to count leading zeroes
1195 *
1196 */
1197 #if defined(__GNUC__)
1198 #define CLZ(x) __builtin_clzl(x)
1199 #elif defined(__arm__)
1200 #define CLZ(x) __clz(x)
1201 #else
1202 #define CLZ(x) bcm_count_leading_zeros(x)
1203 #endif /* __GNUC__ */
1204
1205 /* INTERFACE: Multiword bitmap based small id allocator. */
1206 struct bcm_mwbmap; /* forward declaration for use as an opaque mwbmap handle */
1207
1208 #define BCM_MWBMAP_INVALID_HDL ((struct bcm_mwbmap *)NULL)
1209 #define BCM_MWBMAP_INVALID_IDX ((uint32)(~0U))
1210
1211 /* Incarnate a multiword bitmap based small index allocator */
1212 extern struct bcm_mwbmap * bcm_mwbmap_init(osl_t * osh, uint32 items_max);
1213
1214 /* Free up the multiword bitmap index allocator */
1215 extern void bcm_mwbmap_fini(osl_t * osh, struct bcm_mwbmap * mwbmap_hdl);
1216
1217 /* Allocate a unique small index using a multiword bitmap index allocator */
1218 extern uint32 bcm_mwbmap_alloc(struct bcm_mwbmap * mwbmap_hdl);
1219
1220 /* Force an index at a specified position to be in use */
1221 extern void bcm_mwbmap_force(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1222
1223 /* Free a previously allocated index back into the multiword bitmap allocator */
1224 extern void bcm_mwbmap_free(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1225
1226 /* Fetch the toal number of free indices in the multiword bitmap allocator */
1227 extern uint32 bcm_mwbmap_free_cnt(struct bcm_mwbmap * mwbmap_hdl);
1228
1229 /* Determine whether an index is inuse or free */
1230 extern bool bcm_mwbmap_isfree(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1231
1232 /* Debug dump a multiword bitmap allocator */
1233 extern void bcm_mwbmap_show(struct bcm_mwbmap * mwbmap_hdl);
1234
1235 extern void bcm_mwbmap_audit(struct bcm_mwbmap * mwbmap_hdl);
1236 /* End - Multiword bitmap based small Id allocator. */
1237
1238 /* INTERFACE: Simple unique 16bit Id Allocator using a stack implementation. */
1239
1240 #define ID8_INVALID 0xFFu
1241 #define ID16_INVALID 0xFFFFu
1242 #define ID32_INVALID 0xFFFFFFFFu
1243 #define ID16_UNDEFINED ID16_INVALID
1244
1245 /*
1246 * Construct a 16bit id allocator, managing 16bit ids in the range:
1247 * [start_val16 .. start_val16+total_ids)
1248 * Note: start_val16 is inclusive.
1249 * Returns an opaque handle to the 16bit id allocator.
1250 */
1251 extern void * id16_map_init(osl_t *osh, uint16 total_ids, uint16 start_val16);
1252 extern void * id16_map_fini(osl_t *osh, void * id16_map_hndl);
1253 extern void id16_map_clear(void * id16_map_hndl, uint16 total_ids, uint16 start_val16);
1254
1255 /* Allocate a unique 16bit id */
1256 extern uint16 id16_map_alloc(void * id16_map_hndl);
1257
1258 /* Free a 16bit id value into the id16 allocator */
1259 extern void id16_map_free(void * id16_map_hndl, uint16 val16);
1260
1261 /* Get the number of failures encountered during id allocation. */
1262 extern uint32 id16_map_failures(void * id16_map_hndl);
1263
1264 /* Audit the 16bit id allocator state. */
1265 extern bool id16_map_audit(void * id16_map_hndl);
1266 /* End - Simple 16bit Id Allocator. */
1267 #endif /* BCMDRIVER */
1268
1269 void bcm_add_64(uint32* r_hi, uint32* r_lo, uint32 offset);
1270 void bcm_sub_64(uint32* r_hi, uint32* r_lo, uint32 offset);
1271
1272 #define MASK_32_BITS (~0)
1273 #define MASK_8_BITS ((1 << 8) - 1)
1274
1275 #define EXTRACT_LOW32(num) (uint32)(num & MASK_32_BITS)
1276 #define EXTRACT_HIGH32(num) (uint32)(((uint64)num >> 32) & MASK_32_BITS)
1277
1278 #define MAXIMUM(a, b) ((a > b) ? a : b)
1279 #define MINIMUM(a, b) ((a < b) ? a : b)
1280 #define LIMIT(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
1281
1282 /* calculate checksum for ip header, tcp / udp header / data */
1283 uint16 bcm_ip_cksum(uint8 *buf, uint32 len, uint32 sum);
1284
1285 #ifndef _dll_t_
1286 #define _dll_t_
1287 /*
1288 * -----------------------------------------------------------------------------
1289 * Double Linked List Macros
1290 * -----------------------------------------------------------------------------
1291 *
1292 * All dll operations must be performed on a pre-initialized node.
1293 * Inserting an uninitialized node into a list effectively initialized it.
1294 *
1295 * When a node is deleted from a list, you may initialize it to avoid corruption
1296 * incurred by double deletion. You may skip initialization if the node is
1297 * immediately inserted into another list.
1298 *
1299 * By placing a dll_t element at the start of a struct, you may cast a dll_t *
1300 * to the struct or vice versa.
1301 *
1302 * Example of declaring an initializing someList and inserting nodeA, nodeB
1303 *
1304 * typedef struct item {
1305 * dll_t node;
1306 * int someData;
1307 * } Item_t;
1308 * Item_t nodeA, nodeB, nodeC;
1309 * nodeA.someData = 11111, nodeB.someData = 22222, nodeC.someData = 33333;
1310 *
1311 * dll_t someList;
1312 * dll_init(&someList);
1313 *
1314 * dll_append(&someList, (dll_t *) &nodeA);
1315 * dll_prepend(&someList, &nodeB.node);
1316 * dll_insert((dll_t *)&nodeC, &nodeA.node);
1317 *
1318 * dll_delete((dll_t *) &nodeB);
1319 *
1320 * Example of a for loop to walk someList of node_p
1321 *
1322 * extern void mydisplay(Item_t * item_p);
1323 *
1324 * dll_t * item_p, * next_p;
1325 * for (item_p = dll_head_p(&someList); ! dll_end(&someList, item_p);
1326 * item_p = next_p)
1327 * {
1328 * next_p = dll_next_p(item_p);
1329 * ... use item_p at will, including removing it from list ...
1330 * mydisplay((PItem_t)item_p);
1331 * }
1332 *
1333 * -----------------------------------------------------------------------------
1334 */
1335 typedef struct dll {
1336 struct dll * next_p;
1337 struct dll * prev_p;
1338 } dll_t;
1339
1340 static INLINE void
dll_init(dll_t * node_p)1341 dll_init(dll_t *node_p)
1342 {
1343 node_p->next_p = node_p;
1344 node_p->prev_p = node_p;
1345 }
1346 /* dll macros returing a pointer to dll_t */
1347
1348 static INLINE dll_t *
BCMPOSTTRAPFN(dll_head_p)1349 BCMPOSTTRAPFN(dll_head_p)(dll_t *list_p)
1350 {
1351 return list_p->next_p;
1352 }
1353
1354 static INLINE dll_t *
BCMPOSTTRAPFN(dll_tail_p)1355 BCMPOSTTRAPFN(dll_tail_p)(dll_t *list_p)
1356 {
1357 return (list_p)->prev_p;
1358 }
1359
1360 static INLINE dll_t *
BCMPOSTTRAPFN(dll_next_p)1361 BCMPOSTTRAPFN(dll_next_p)(dll_t *node_p)
1362 {
1363 return (node_p)->next_p;
1364 }
1365
1366 static INLINE dll_t *
BCMPOSTTRAPFN(dll_prev_p)1367 BCMPOSTTRAPFN(dll_prev_p)(dll_t *node_p)
1368 {
1369 return (node_p)->prev_p;
1370 }
1371
1372 static INLINE bool
BCMPOSTTRAPFN(dll_empty)1373 BCMPOSTTRAPFN(dll_empty)(dll_t *list_p)
1374 {
1375 return ((list_p)->next_p == (list_p));
1376 }
1377
1378 static INLINE bool
BCMPOSTTRAPFN(dll_end)1379 BCMPOSTTRAPFN(dll_end)(dll_t *list_p, dll_t * node_p)
1380 {
1381 return (list_p == node_p);
1382 }
1383
1384 /* inserts the node new_p "after" the node at_p */
1385 static INLINE void
BCMPOSTTRAPFN(dll_insert)1386 BCMPOSTTRAPFN(dll_insert)(dll_t *new_p, dll_t * at_p)
1387 {
1388 new_p->next_p = at_p->next_p;
1389 new_p->prev_p = at_p;
1390 at_p->next_p = new_p;
1391 (new_p->next_p)->prev_p = new_p;
1392 }
1393
1394 static INLINE void
BCMPOSTTRAPFN(dll_append)1395 BCMPOSTTRAPFN(dll_append)(dll_t *list_p, dll_t *node_p)
1396 {
1397 dll_insert(node_p, dll_tail_p(list_p));
1398 }
1399
1400 static INLINE void
BCMPOSTTRAPFN(dll_prepend)1401 BCMPOSTTRAPFN(dll_prepend)(dll_t *list_p, dll_t *node_p)
1402 {
1403 dll_insert(node_p, list_p);
1404 }
1405
1406 /* deletes a node from any list that it "may" be in, if at all. */
1407 static INLINE void
BCMPOSTTRAPFN(dll_delete)1408 BCMPOSTTRAPFN(dll_delete)(dll_t *node_p)
1409 {
1410 node_p->prev_p->next_p = node_p->next_p;
1411 node_p->next_p->prev_p = node_p->prev_p;
1412 }
1413 #endif /* ! defined(_dll_t_) */
1414
1415 /* Elements managed in a double linked list */
1416
1417 typedef struct dll_pool {
1418 dll_t free_list;
1419 uint16 free_count;
1420 uint16 elems_max;
1421 uint16 elem_size;
1422 dll_t elements[1];
1423 } dll_pool_t;
1424
1425 dll_pool_t * dll_pool_init(void * osh, uint16 elems_max, uint16 elem_size);
1426 void * dll_pool_alloc(dll_pool_t * dll_pool_p);
1427 void dll_pool_free(dll_pool_t * dll_pool_p, void * elem_p);
1428 void dll_pool_free_tail(dll_pool_t * dll_pool_p, void * elem_p);
1429 typedef void (* dll_elem_dump)(void * elem_p);
1430 #ifdef BCMDBG
1431 void dll_pool_dump(dll_pool_t * dll_pool_p, dll_elem_dump dump);
1432 #endif
1433 void dll_pool_detach(void * osh, dll_pool_t * pool, uint16 elems_max, uint16 elem_size);
1434
1435 int valid_bcmerror(int e);
1436 /* Stringify macro definition */
1437 #define BCM_STRINGIFY(s) #s
1438 /* Used to pass in a macro variable that gets expanded and then stringified */
1439 #define BCM_EXTENDED_STRINGIFY(s) BCM_STRINGIFY(s)
1440
1441 /* calculate IPv4 header checksum
1442 * - input ip points to IP header in network order
1443 * - output cksum is in network order
1444 */
1445 uint16 ipv4_hdr_cksum(uint8 *ip, uint ip_len);
1446
1447 /* calculate IPv4 TCP header checksum
1448 * - input ip and tcp points to IP and TCP header in network order
1449 * - output cksum is in network order
1450 */
1451 uint16 ipv4_tcp_hdr_cksum(uint8 *ip, uint8 *tcp, uint16 tcp_len);
1452
1453 /* calculate IPv6 TCP header checksum
1454 * - input ipv6 and tcp points to IPv6 and TCP header in network order
1455 * - output cksum is in network order
1456 */
1457 uint16 ipv6_tcp_hdr_cksum(uint8 *ipv6, uint8 *tcp, uint16 tcp_len);
1458
1459 #ifdef __cplusplus
1460 }
1461 #endif
1462
1463 /* #define DEBUG_COUNTER */
1464 #ifdef DEBUG_COUNTER
1465 #define CNTR_TBL_MAX 10
1466 typedef struct _counter_tbl_t {
1467 char name[16]; /* name of this counter table */
1468 uint32 prev_log_print; /* Internal use. Timestamp of the previous log print */
1469 uint log_print_interval; /* Desired interval to print logs in ms */
1470 uint needed_cnt; /* How many counters need to be used */
1471 uint32 cnt[CNTR_TBL_MAX]; /* Counting entries to increase at desired places */
1472 bool enabled; /* Whether to enable printing log */
1473 } counter_tbl_t;
1474
1475 /* How to use
1476 Eg.: In dhd_linux.c
1477 cnt[0]: How many times dhd_start_xmit() was called in every 1sec.
1478 cnt[1]: How many bytes were requested to be sent in every 1sec.
1479
1480 ++ static counter_tbl_t xmit_tbl = {"xmit", 0, 1000, 2, {0,}, 1};
1481
1482 int
1483 dhd_start_xmit(struct sk_buff *skb, struct net_device *net)
1484 {
1485 ..........
1486 ++ counter_printlog(&xmit_tbl);
1487 ++ xmit_tbl.cnt[0]++;
1488
1489 ifp = dhd->iflist[ifidx];
1490 datalen = PKTLEN(dhdp->osh, skb);
1491
1492 ++ xmit_tbl.cnt[1] += datalen;
1493 ............
1494
1495 ret = dhd_sendpkt(&dhd->pub, ifidx, pktbuf);
1496 ...........
1497 }
1498 */
1499
1500 void counter_printlog(counter_tbl_t *ctr_tbl);
1501 #endif /* DEBUG_COUNTER */
1502
1503 #if defined(__GNUC__)
1504 #define CALL_SITE __builtin_return_address(0)
1505 #elif defined(_WIN32)
1506 #define CALL_SITE _ReturnAddress()
1507 #else
1508 #define CALL_SITE ((void*) 0)
1509 #endif
1510 #ifdef SHOW_LOGTRACE
1511 #define TRACE_LOG_BUF_MAX_SIZE 1700
1512 #define RTT_LOG_BUF_MAX_SIZE 1700
1513 #define BUF_NOT_AVAILABLE 0
1514 #define NEXT_BUF_NOT_AVAIL 1
1515 #define NEXT_BUF_AVAIL 2
1516
1517 typedef struct trace_buf_info {
1518 int availability;
1519 int size;
1520 char buf[TRACE_LOG_BUF_MAX_SIZE];
1521 } trace_buf_info_t;
1522 #endif /* SHOW_LOGTRACE */
1523
1524 enum dump_dongle_e {
1525 DUMP_DONGLE_COREREG = 0,
1526 DUMP_DONGLE_D11MEM
1527 };
1528
1529 typedef struct {
1530 uint32 type; /**< specifies e.g dump of d11 memory, use enum dump_dongle_e */
1531 uint32 index; /**< iterator1, specifies core index or d11 memory index */
1532 uint32 offset; /**< iterator2, byte offset within register set or memory */
1533 } dump_dongle_in_t;
1534
1535 typedef struct {
1536 uint32 address; /**< e.g. backplane address of register */
1537 uint32 id; /**< id, e.g. core id */
1538 uint32 rev; /**< rev, e.g. core rev */
1539 uint32 n_bytes; /**< nbytes in array val[] */
1540 uint32 val[1]; /**< out: values that were read out of registers or memory */
1541 } dump_dongle_out_t;
1542
1543 extern uint32 sqrt_int(uint32 value);
1544
1545 extern uint8 bcm_get_ceil_pow_2(uint val);
1546
1547 #ifdef BCMDRIVER
1548 /* structures and routines to process variable sized data */
1549 typedef struct var_len_data {
1550 uint32 vlen;
1551 uint8 *vdata;
1552 } var_len_data_t;
1553
1554 int bcm_vdata_alloc(osl_t *osh, var_len_data_t *vld, uint32 size);
1555 int bcm_vdata_free(osl_t *osh, var_len_data_t *vld);
1556 #if defined(PRIVACY_MASK)
1557 void bcm_ether_privacy_mask(struct ether_addr *addr);
1558 #else
1559 #define bcm_ether_privacy_mask(addr)
1560 #endif /* PRIVACY_MASK */
1561 #endif /* BCMDRIVER */
1562
1563 /* Count the number of elements in an array that do not match the given value */
1564 extern int array_value_mismatch_count(uint8 value, uint8 *array, int array_size);
1565 /* Count the number of non-zero elements in an uint8 array */
1566 extern int array_nonzero_count(uint8 *array, int array_size);
1567 /* Count the number of non-zero elements in an int16 array */
1568 extern int array_nonzero_count_int16(int16 *array, int array_size);
1569 /* Count the number of zero elements in an uint8 array */
1570 extern int array_zero_count(uint8 *array, int array_size);
1571 /* Validate a uint8 ordered array. Assert if invalid. */
1572 extern int verify_ordered_array_uint8(uint8 *array, int array_size, uint8 range_lo, uint8 range_hi);
1573 /* Validate a int16 configuration array that need not be zero-terminated. Assert if invalid. */
1574 extern int verify_ordered_array_int16(int16 *array, int array_size, int16 range_lo, int16 range_hi);
1575 /* Validate all values in an array are in range */
1576 extern int verify_array_values(uint8 *array, int array_size,
1577 int range_lo, int range_hi, bool zero_terminated);
1578
1579 /* To unwind from the trap_handler. */
1580 extern void (*const print_btrace_int_fn)(int depth, uint32 pc, uint32 lr, uint32 sp);
1581 extern void (*const print_btrace_fn)(int depth);
1582 #define PRINT_BACKTRACE(depth) if (print_btrace_fn) print_btrace_fn(depth)
1583 #define PRINT_BACKTRACE_INT(depth, pc, lr, sp) \
1584 if (print_btrace_int_fn) print_btrace_int_fn(depth, pc, lr, sp)
1585
1586 /* FW Signing - only in bootloader builds, never in dongle FW builds */
1587 #ifdef WL_FWSIGN
1588 #define FWSIGN_ENAB() (1)
1589 #else
1590 #define FWSIGN_ENAB() (0)
1591 #endif /* WL_FWSIGN */
1592
1593 /* Utilities for reading SROM/SFlash vars */
1594
1595 typedef struct varbuf {
1596 char *base; /* pointer to buffer base */
1597 char *buf; /* pointer to current position */
1598 unsigned int size; /* current (residual) size in bytes */
1599 } varbuf_t;
1600
1601 /** Initialization of varbuf structure */
1602 void varbuf_init(varbuf_t *b, char *buf, uint size);
1603 /** append a null terminated var=value string */
1604 int varbuf_append(varbuf_t *b, const char *fmt, ...);
1605 #if defined(BCMDRIVER)
1606 int initvars_table(osl_t *osh, char *start, char *end, char **vars, uint *count);
1607 #endif
1608
1609 /* Count the number of trailing zeros in uint32 val
1610 * Applying unary minus to unsigned value is intentional,
1611 * and doesn't influence counting of trailing zeros
1612 */
1613 static INLINE uint32
count_trailing_zeros(uint32 val)1614 count_trailing_zeros(uint32 val)
1615 {
1616 #ifdef BCMDRIVER
1617 uint32 c = (uint32)CLZ(val & ((uint32)(-(int)val)));
1618 #else
1619 uint32 c = (uint32)C_bcm_count_leading_zeros(val & ((uint32)(-(int)val)));
1620 #endif /* BCMDRIVER */
1621 return val ? 31u - c : c;
1622 }
1623
1624 /** Size in bytes of data block, defined by struct with last field, declared as
1625 * one/zero element vector - such as wl_uint32_list_t or bcm_xtlv_cbuf_s.
1626 * Arguments:
1627 * list - address of data block (value is ignored, only type is important)
1628 * last_var_len_field - name of last field (usually declared as ...[] or ...[1])
1629 * num_elems - number of elements in data block
1630 * Example:
1631 * wl_uint32_list_t *list;
1632 * WL_VAR_LEN_STRUCT_SIZE(list, element, 10); // Size in bytes of 10-element list
1633 */
1634 #define WL_VAR_LEN_STRUCT_SIZE(list, last_var_len_field, num_elems) \
1635 ((size_t)((const char *)&((list)->last_var_len_field) - (const char *)(list)) + \
1636 (sizeof((list)->last_var_len_field[0]) * (size_t)(num_elems)))
1637
1638 int buf_shift_right(uint8 *buf, uint16 len, uint8 bits);
1639 #endif /* _bcmutils_h_ */
1640