source: G950FXXS5DSI1
[GitHub/exynos8895/android_kernel_samsung_universal8895.git] / drivers / net / wireless / bcmdhd4361 / include / bcmutils.h
CommitLineData
1cac41cb
MB
1/*
2 * Misc useful os-independent macros and functions.
3 *
4 * Copyright (C) 1999-2019, 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 * Notwithstanding the above, under no circumstances may you combine this
21 * software in any way with any other Broadcom software provided under a license
22 * other than the GPL, without Broadcom's express prior written consent.
23 *
24 *
25 * <<Broadcom-WL-IPTag/Open:>>
26 *
5a068558 27 * $Id: bcmutils.h 813756 2019-04-08 05:18:14Z $
1cac41cb
MB
28 */
29
30#ifndef _bcmutils_h_
31#define _bcmutils_h_
32
33#include <bcmtlv.h>
34
35#ifdef __cplusplus
36extern "C" {
37#endif // endif
38
39#define bcm_strncpy_s(dst, noOfElements, src, count) strncpy((dst), (src), (count))
40#ifdef FREEBSD
41#define bcm_strncat_s(dst, noOfElements, src, count) strcat((dst), (src))
42#else
43#define bcm_strncat_s(dst, noOfElements, src, count) strncat((dst), (src), (count))
44#endif /* FREEBSD */
45#define bcm_snprintf_s snprintf
46#define bcm_sprintf_s snprintf
47
48/*
49 * #define bcm_strcpy_s(dst, count, src) strncpy((dst), (src), (count))
50 * Use bcm_strcpy_s instead as it is a safer option
51 * bcm_strcat_s: Use bcm_strncat_s as a safer option
52 *
53 */
54
55#define BCM_BIT(x) (1 << (x))
56
57/* ctype replacement */
58#define _BCM_U 0x01 /* upper */
59#define _BCM_L 0x02 /* lower */
60#define _BCM_D 0x04 /* digit */
61#define _BCM_C 0x08 /* cntrl */
62#define _BCM_P 0x10 /* punct */
63#define _BCM_S 0x20 /* white space (space/lf/tab) */
64#define _BCM_X 0x40 /* hex digit */
65#define _BCM_SP 0x80 /* hard space (0x20) */
66
67extern const unsigned char bcm_ctype[];
68#define bcm_ismask(x) (bcm_ctype[(int)(unsigned char)(x)])
69
70#define bcm_isalnum(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L|_BCM_D)) != 0)
71#define bcm_isalpha(c) ((bcm_ismask(c)&(_BCM_U|_BCM_L)) != 0)
72#define bcm_iscntrl(c) ((bcm_ismask(c)&(_BCM_C)) != 0)
73#define bcm_isdigit(c) ((bcm_ismask(c)&(_BCM_D)) != 0)
74#define bcm_isgraph(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D)) != 0)
75#define bcm_islower(c) ((bcm_ismask(c)&(_BCM_L)) != 0)
76#define bcm_isprint(c) ((bcm_ismask(c)&(_BCM_P|_BCM_U|_BCM_L|_BCM_D|_BCM_SP)) != 0)
77#define bcm_ispunct(c) ((bcm_ismask(c)&(_BCM_P)) != 0)
78#define bcm_isspace(c) ((bcm_ismask(c)&(_BCM_S)) != 0)
79#define bcm_isupper(c) ((bcm_ismask(c)&(_BCM_U)) != 0)
80#define bcm_isxdigit(c) ((bcm_ismask(c)&(_BCM_D|_BCM_X)) != 0)
81#define bcm_tolower(c) (bcm_isupper((c)) ? ((c) + 'a' - 'A') : (c))
82#define bcm_toupper(c) (bcm_islower((c)) ? ((c) + 'A' - 'a') : (c))
83
84#define CIRCULAR_ARRAY_FULL(rd_idx, wr_idx, max) ((wr_idx + 1)%max == rd_idx)
85
86#define KB(bytes) (((bytes) + 1023) / 1024)
87
88/* Buffer structure for collecting string-formatted data
89* using bcm_bprintf() API.
90* Use bcm_binit() to initialize before use
91*/
92
93struct bcmstrbuf {
94 char *buf; /* pointer to current position in origbuf */
95 unsigned int size; /* current (residual) size in bytes */
96 char *origbuf; /* unmodified pointer to orignal buffer */
97 unsigned int origsize; /* unmodified orignal buffer size in bytes */
98};
99
100#define BCMSTRBUF_LEN(b) (b->size)
101#define BCMSTRBUF_BUF(b) (b->buf)
102
103/* ** driver-only section ** */
104#ifdef BCMDRIVER
105#include <osl.h>
106#include <hnd_pktq.h>
107#include <hnd_pktpool.h>
108
109#define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
110
111/*
112 * Spin at most 'us' microseconds while 'exp' is true.
113 * Caller should explicitly test 'exp' when this completes
114 * and take appropriate error action if 'exp' is still true.
115 */
116#ifndef SPINWAIT_POLL_PERIOD
117#define SPINWAIT_POLL_PERIOD 10U
118#endif // endif
119
120#define SPINWAIT(exp, us) { \
121 uint countdown = (us) + (SPINWAIT_POLL_PERIOD - 1U); \
122 while (((exp) != 0) && (uint)(countdown >= SPINWAIT_POLL_PERIOD)) { \
123 OSL_DELAY(SPINWAIT_POLL_PERIOD); \
124 countdown -= SPINWAIT_POLL_PERIOD; \
125 } \
126}
127
128/* forward definition of ether_addr structure used by some function prototypes */
129
130struct ether_addr;
131
132extern int ether_isbcast(const void *ea);
133extern int ether_isnulladdr(const void *ea);
134
135#define UP_TABLE_MAX ((IPV4_TOS_DSCP_MASK >> IPV4_TOS_DSCP_SHIFT) + 1) /* 64 max */
136#define CORE_SLAVE_PORT_0 0
137#define CORE_SLAVE_PORT_1 1
138#define CORE_BASE_ADDR_0 0
139#define CORE_BASE_ADDR_1 1
140
141/* externs */
142/* packet */
143extern uint pktcopy(osl_t *osh, void *p, uint offset, int len, uchar *buf);
144extern uint pktfrombuf(osl_t *osh, void *p, uint offset, int len, uchar *buf);
145extern uint pkttotlen(osl_t *osh, void *p);
146extern void *pktlast(osl_t *osh, void *p);
147extern uint pktsegcnt(osl_t *osh, void *p);
148extern uint8 *pktdataoffset(osl_t *osh, void *p, uint offset);
149extern void *pktoffset(osl_t *osh, void *p, uint offset);
150
151/* Get priority from a packet and pass it back in scb (or equiv) */
152#define PKTPRIO_VDSCP 0x100 /* DSCP prio found after VLAN tag */
153#define PKTPRIO_VLAN 0x200 /* VLAN prio found */
154#define PKTPRIO_UPD 0x400 /* DSCP used to update VLAN prio */
155#define PKTPRIO_DSCP 0x800 /* DSCP prio found */
156
157/* DSCP type definitions (RFC4594) */
158/* AF1x: High-Throughput Data (RFC2597) */
159#define DSCP_AF11 0x0A
160#define DSCP_AF12 0x0C
161#define DSCP_AF13 0x0E
162/* AF2x: Low-Latency Data (RFC2597) */
163#define DSCP_AF21 0x12
164#define DSCP_AF22 0x14
165#define DSCP_AF23 0x16
5a068558
MB
166/* CS2: OAM (RFC2474) */
167#define DSCP_CS2 0x10
1cac41cb
MB
168/* AF3x: Multimedia Streaming (RFC2597) */
169#define DSCP_AF31 0x1A
170#define DSCP_AF32 0x1C
171#define DSCP_AF33 0x1E
5a068558
MB
172/* CS3: Broadcast Video (RFC2474) */
173#define DSCP_CS3 0x18
174/* VA: VOCIE-ADMIT (RFC5865) */
175#define DSCP_VA 0x2C
1cac41cb
MB
176/* EF: Telephony (RFC3246) */
177#define DSCP_EF 0x2E
5a068558
MB
178/* CS6: Network Control (RFC2474) */
179#define DSCP_CS6 0x30
1cac41cb
MB
180
181extern uint pktsetprio(void *pkt, bool update_vtag);
182extern uint pktsetprio_qms(void *pkt, uint8* up_table, bool update_vtag);
183extern bool pktgetdscp(uint8 *pktdata, uint pktlen, uint8 *dscp);
184
185/* ethernet address */
186extern char *bcm_ether_ntoa(const struct ether_addr *ea, char *buf);
187extern int bcm_ether_atoe(const char *p, struct ether_addr *ea);
188
189/* ip address */
190struct ipv4_addr;
191extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
192extern char *bcm_ipv6_ntoa(void *ipv6, char *buf);
193extern int bcm_atoipv4(const char *p, struct ipv4_addr *ip);
194
195/* delay */
196extern void bcm_mdelay(uint ms);
197/* variable access */
198#if defined(BCM_RECLAIM)
199extern bool _nvram_reclaim_enb;
200#define NVRAM_RECLAIM_ENAB() (_nvram_reclaim_enb)
201#define NVRAM_RECLAIM_CHECK(name) \
202 if (NVRAM_RECLAIM_ENAB() && (bcm_attach_part_reclaimed == TRUE)) { \
203 *(char*) 0 = 0; /* TRAP */ \
204 return NULL; \
205 }
206#else /* BCM_RECLAIM */
207#define NVRAM_RECLAIM_CHECK(name)
208#endif /* BCM_RECLAIM */
209
210extern char *getvar(char *vars, const char *name);
211extern int getintvar(char *vars, const char *name);
212extern int getintvararray(char *vars, const char *name, int index);
213extern int getintvararraysize(char *vars, const char *name);
214
215/* Read an array of values from a possibly slice-specific nvram string */
216extern int get_uint8_vararray_slicespecific(osl_t *osh, char *vars, char *vars_table_accessor,
217 const char* name, uint8* dest_array, uint dest_size);
218extern int get_int16_vararray_slicespecific(osl_t *osh, char *vars, char *vars_table_accessor,
219 const char* name, int16* dest_array, uint dest_size);
220/* Prepend a slice-specific accessor to an nvram string name */
221extern int get_slicespecific_var_name(osl_t *osh, char *vars_table_accessor,
222 const char *name, char **name_out);
223
224extern uint getgpiopin(char *vars, char *pin_name, uint def_pin);
225#define bcm_perf_enable()
226#define bcmstats(fmt)
227#define bcmlog(fmt, a1, a2)
228#define bcmdumplog(buf, size) *buf = '\0'
229#define bcmdumplogent(buf, idx) -1
230
231#define TSF_TICKS_PER_MS 1000
232#define TS_ENTER 0xdeadbeef /* Timestamp profiling enter */
233#define TS_EXIT 0xbeefcafe /* Timestamp profiling exit */
234
235#define bcmtslog(tstamp, fmt, a1, a2)
236#define bcmprinttslogs()
237#define bcmprinttstamp(us)
238#define bcmdumptslog(b)
239
240extern char *bcm_nvram_vars(uint *length);
241extern int bcm_nvram_cache(void *sih);
242
243/* Support for sharing code across in-driver iovar implementations.
244 * The intent is that a driver use this structure to map iovar names
245 * to its (private) iovar identifiers, and the lookup function to
246 * find the entry. Macros are provided to map ids and get/set actions
247 * into a single number space for a switch statement.
248 */
249
250/* iovar structure */
251typedef struct bcm_iovar {
252 const char *name; /* name for lookup and display */
253 uint16 varid; /* id for switch */
254 uint16 flags; /* driver-specific flag bits */
255 uint8 flags2; /* driver-specific flag bits */
256 uint8 type; /* base type of argument */
257 uint16 minlen; /* min length for buffer vars */
258} bcm_iovar_t;
259
260/* varid definitions are per-driver, may use these get/set bits */
261
262/* IOVar action bits for id mapping */
263#define IOV_GET 0 /* Get an iovar */
264#define IOV_SET 1 /* Set an iovar */
265
266/* Varid to actionid mapping */
267#define IOV_GVAL(id) ((id) * 2)
268#define IOV_SVAL(id) ((id) * 2 + IOV_SET)
269#define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
270#define IOV_ID(actionid) (actionid >> 1)
271
272/* flags are per-driver based on driver attributes */
273
274extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table, const char *name);
275extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg, int len, bool set);
276
277/* ioctl structure */
278typedef struct wlc_ioctl_cmd {
279 uint16 cmd; /**< IOCTL command */
280 uint16 flags; /**< IOCTL command flags */
281 int16 min_len; /**< IOCTL command minimum argument len (in bytes) */
282} wlc_ioctl_cmd_t;
283
284#if defined(WLTINYDUMP) || defined(WLMSG_INFORM) || defined(WLMSG_ASSOC) || \
285 defined(WLMSG_PRPKT) || defined(WLMSG_WSEC)
286extern int bcm_format_ssid(char* buf, const uchar ssid[], uint ssid_len);
287#endif // endif
288#endif /* BCMDRIVER */
289
290/* string */
291extern int bcm_atoi(const char *s);
292extern ulong bcm_strtoul(const char *cp, char **endp, uint base);
293extern uint64 bcm_strtoull(const char *cp, char **endp, uint base);
294extern char *bcmstrstr(const char *haystack, const char *needle);
295extern char *bcmstrnstr(const char *s, uint s_len, const char *substr, uint substr_len);
296extern char *bcmstrcat(char *dest, const char *src);
297extern char *bcmstrncat(char *dest, const char *src, uint size);
298extern ulong wchar2ascii(char *abuf, ushort *wbuf, ushort wbuflen, ulong abuflen);
299char* bcmstrtok(char **string, const char *delimiters, char *tokdelim);
300int bcmstricmp(const char *s1, const char *s2);
301int bcmstrnicmp(const char* s1, const char* s2, int cnt);
302
303/* Base type definitions */
304#define IOVT_VOID 0 /* no value (implictly set only) */
305#define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
306#define IOVT_INT8 2 /* integer values are range-checked */
307#define IOVT_UINT8 3 /* unsigned int 8 bits */
308#define IOVT_INT16 4 /* int 16 bits */
309#define IOVT_UINT16 5 /* unsigned int 16 bits */
310#define IOVT_INT32 6 /* int 32 bits */
311#define IOVT_UINT32 7 /* unsigned int 32 bits */
312#define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
313#define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
314
315/* Initializer for IOV type strings */
316#define BCM_IOV_TYPE_INIT { \
317 "void", \
318 "bool", \
319 "int8", \
320 "uint8", \
321 "int16", \
322 "uint16", \
323 "int32", \
324 "uint32", \
325 "buffer", \
326 "" }
327
328#define BCM_IOVT_IS_INT(type) (\
329 (type == IOVT_BOOL) || \
330 (type == IOVT_INT8) || \
331 (type == IOVT_UINT8) || \
332 (type == IOVT_INT16) || \
333 (type == IOVT_UINT16) || \
334 (type == IOVT_INT32) || \
335 (type == IOVT_UINT32))
336
337/* ** driver/apps-shared section ** */
338
339#define BCME_STRLEN 64 /* Max string length for BCM errors */
340#define VALID_BCMERROR(e) valid_bcmerror(e)
341
342#ifdef DBG_BUS
343/** tracks non typical execution paths, use gdb with arm sim + firmware dump to read counters */
344#define DBG_BUS_INC(s, cnt) ((s)->dbg_bus->cnt++)
345#else
346#define DBG_BUS_INC(s, cnt)
347#endif /* DBG_BUS */
348
349/*
350 * error codes could be added but the defined ones shouldn't be changed/deleted
351 * these error codes are exposed to the user code
352 * when ever a new error code is added to this list
353 * please update errorstring table with the related error string and
354 * update osl files with os specific errorcode map
355*/
356
357#define BCME_OK 0 /* Success */
358#define BCME_ERROR -1 /* Error generic */
359#define BCME_BADARG -2 /* Bad Argument */
360#define BCME_BADOPTION -3 /* Bad option */
361#define BCME_NOTUP -4 /* Not up */
362#define BCME_NOTDOWN -5 /* Not down */
363#define BCME_NOTAP -6 /* Not AP */
364#define BCME_NOTSTA -7 /* Not STA */
365#define BCME_BADKEYIDX -8 /* BAD Key Index */
366#define BCME_RADIOOFF -9 /* Radio Off */
367#define BCME_NOTBANDLOCKED -10 /* Not band locked */
368#define BCME_NOCLK -11 /* No Clock */
369#define BCME_BADRATESET -12 /* BAD Rate valueset */
370#define BCME_BADBAND -13 /* BAD Band */
371#define BCME_BUFTOOSHORT -14 /* Buffer too short */
372#define BCME_BUFTOOLONG -15 /* Buffer too long */
373#define BCME_BUSY -16 /* Busy */
374#define BCME_NOTASSOCIATED -17 /* Not Associated */
375#define BCME_BADSSIDLEN -18 /* Bad SSID len */
376#define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
377#define BCME_BADCHAN -20 /* Bad Channel */
378#define BCME_BADADDR -21 /* Bad Address */
379#define BCME_NORESOURCE -22 /* Not Enough Resources */
380#define BCME_UNSUPPORTED -23 /* Unsupported */
381#define BCME_BADLEN -24 /* Bad length */
382#define BCME_NOTREADY -25 /* Not Ready */
383#define BCME_EPERM -26 /* Not Permitted */
384#define BCME_NOMEM -27 /* No Memory */
385#define BCME_ASSOCIATED -28 /* Associated */
386#define BCME_RANGE -29 /* Not In Range */
387#define BCME_NOTFOUND -30 /* Not Found */
388#define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
389#define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
390#define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
391#define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
392#define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
393#define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
394#define BCME_VERSION -37 /* Incorrect version */
395#define BCME_TXFAIL -38 /* TX failure */
396#define BCME_RXFAIL -39 /* RX failure */
397#define BCME_NODEVICE -40 /* Device not present */
398#define BCME_NMODE_DISABLED -41 /* NMODE disabled */
399#define BCME_HOFFLOAD_RESIDENT -42 /* offload resident */
400#define BCME_SCANREJECT -43 /* reject scan request */
401#define BCME_USAGE_ERROR -44 /* WLCMD usage error */
402#define BCME_IOCTL_ERROR -45 /* WLCMD ioctl error */
403#define BCME_SERIAL_PORT_ERR -46 /* RWL serial port error */
404#define BCME_DISABLED -47 /* Disabled in this build */
405#define BCME_DECERR -48 /* Decrypt error */
406#define BCME_ENCERR -49 /* Encrypt error */
407#define BCME_MICERR -50 /* Integrity/MIC error */
408#define BCME_REPLAY -51 /* Replay */
409#define BCME_IE_NOTFOUND -52 /* IE not found */
410#define BCME_DATA_NOTFOUND -53 /* Complete data not found in buffer */
411#define BCME_NOT_GC -54 /* expecting a group client */
412#define BCME_PRS_REQ_FAILED -55 /* GC presence req failed to sent */
413#define BCME_NO_P2P_SE -56 /* Could not find P2P-Subelement */
414#define BCME_NOA_PND -57 /* NoA pending, CB shuld be NULL */
415#define BCME_FRAG_Q_FAILED -58 /* queueing 80211 frag failedi */
416#define BCME_GET_AF_FAILED -59 /* Get p2p AF pkt failed */
417#define BCME_MSCH_NOTREADY -60 /* scheduler not ready */
418#define BCME_IOV_LAST_CMD -61 /* last batched iov sub-command */
419#define BCME_MINIPMU_CAL_FAIL -62 /* MiniPMU cal failed */
420#define BCME_RCAL_FAIL -63 /* Rcal failed */
421#define BCME_LPF_RCCAL_FAIL -64 /* RCCAL failed */
422#define BCME_DACBUF_RCCAL_FAIL -65 /* RCCAL failed */
423#define BCME_VCOCAL_FAIL -66 /* VCOCAL failed */
424#define BCME_BANDLOCKED -67 /* interface is restricted to a band */
425#define BCME_DNGL_DEVRESET -68 /* dongle re-attach during DEVRESET */
426#define BCME_LAST BCME_DNGL_DEVRESET
427
428#define BCME_NOTENABLED BCME_DISABLED
429
430/* This error code is *internal* to the driver, and is not propogated to users. It should
431 * only be used by IOCTL patch handlers as an indication that it did not handle the IOCTL.
432 * (Since the error code is internal, an entry in 'BCMERRSTRINGTABLE' is not required,
433 * nor does it need to be part of any OSL driver-to-OS error code mapping).
434 */
435#define BCME_IOCTL_PATCH_UNSUPPORTED -9999
436#if (BCME_LAST <= BCME_IOCTL_PATCH_UNSUPPORTED)
437 #error "BCME_LAST <= BCME_IOCTL_PATCH_UNSUPPORTED"
438#endif // endif
439
440/* These are collection of BCME Error strings */
441#define BCMERRSTRINGTABLE { \
442 "OK", \
443 "Undefined error", \
444 "Bad Argument", \
445 "Bad Option", \
446 "Not up", \
447 "Not down", \
448 "Not AP", \
449 "Not STA", \
450 "Bad Key Index", \
451 "Radio Off", \
452 "Not band locked", \
453 "No clock", \
454 "Bad Rate valueset", \
455 "Bad Band", \
456 "Buffer too short", \
457 "Buffer too long", \
458 "Busy", \
459 "Not Associated", \
460 "Bad SSID len", \
461 "Out of Range Channel", \
462 "Bad Channel", \
463 "Bad Address", \
464 "Not Enough Resources", \
465 "Unsupported", \
466 "Bad length", \
467 "Not Ready", \
468 "Not Permitted", \
469 "No Memory", \
470 "Associated", \
471 "Not In Range", \
472 "Not Found", \
473 "WME Not Enabled", \
474 "TSPEC Not Found", \
475 "ACM Not Supported", \
476 "Not WME Association", \
477 "SDIO Bus Error", \
478 "Dongle Not Accessible", \
479 "Incorrect version", \
480 "TX Failure", \
481 "RX Failure", \
482 "Device Not Present", \
483 "NMODE Disabled", \
484 "Host Offload in device", \
485 "Scan Rejected", \
486 "WLCMD usage error", \
487 "WLCMD ioctl error", \
488 "RWL serial port error", \
489 "Disabled", \
490 "Decrypt error", \
491 "Encrypt error", \
492 "MIC error", \
493 "Replay", \
494 "IE not found", \
495 "Data not found", \
496 "NOT GC", \
497 "PRS REQ FAILED", \
498 "NO P2P SubElement", \
499 "NOA Pending", \
500 "FRAG Q FAILED", \
501 "GET ActionFrame failed", \
502 "scheduler not ready", \
503 "Last IOV batched sub-cmd", \
504 "Mini PMU Cal failed", \
505 "R-cal failed", \
506 "LPF RC Cal failed", \
507 "DAC buf RC Cal failed", \
508 "VCO Cal failed", \
509 "band locked", \
510 "Dongle Devreset", \
511}
512
513#ifndef ABS
514#define ABS(a) (((a) < 0) ? -(a) : (a))
515#endif /* ABS */
516
517#ifndef MIN
518#define MIN(a, b) (((a) < (b)) ? (a) : (b))
519#endif /* MIN */
520
521#ifndef MAX
522#define MAX(a, b) (((a) > (b)) ? (a) : (b))
523#endif /* MAX */
524
525/* limit to [min, max] */
526#ifndef LIMIT_TO_RANGE
527#define LIMIT_TO_RANGE(x, min, max) \
528 ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
529#endif /* LIMIT_TO_RANGE */
530
531/* limit to max */
532#ifndef LIMIT_TO_MAX
533#define LIMIT_TO_MAX(x, max) \
534 (((x) > (max) ? (max) : (x)))
535#endif /* LIMIT_TO_MAX */
536
537/* limit to min */
538#ifndef LIMIT_TO_MIN
539#define LIMIT_TO_MIN(x, min) \
540 (((x) < (min) ? (min) : (x)))
541#endif /* LIMIT_TO_MIN */
542
543#define DELTA(curr, prev) ((curr) > (prev) ? ((curr) - (prev)) : \
544 (0xffffffff - (prev) + (curr) + 1))
545#define CEIL(x, y) (((x) + ((y) - 1)) / (y))
546#define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
547#define ROUNDDN(p, align) ((p) & ~((align) - 1))
548#define ISALIGNED(a, x) (((uintptr)(a) & ((x) - 1)) == 0)
549#define ALIGN_ADDR(addr, boundary) (void *)(((uintptr)(addr) + (boundary) - 1) \
550 & ~((boundary) - 1))
551#define ALIGN_SIZE(size, boundary) (((size) + (boundary) - 1) \
552 & ~((boundary) - 1))
553#define ISPOWEROF2(x) ((((x) - 1) & (x)) == 0)
554#define VALID_MASK(mask) !((mask) & ((mask) + 1))
555
556#ifndef OFFSETOF
557#ifdef __ARMCC_VERSION
558/*
559 * The ARM RVCT compiler complains when using OFFSETOF where a constant
560 * expression is expected, such as an initializer for a static object.
561 * offsetof from the runtime library doesn't have that problem.
562 */
563#include <stddef.h>
564#define OFFSETOF(type, member) offsetof(type, member)
565#else
566# if ((__GNUC__ >= 4) && (__GNUC_MINOR__ >= 8))
567/* GCC 4.8+ complains when using our OFFSETOF macro in array length declarations. */
568# define OFFSETOF(type, member) __builtin_offsetof(type, member)
569# else
570# define OFFSETOF(type, member) ((uint)(uintptr)&((type *)0)->member)
571# endif /* GCC 4.8 or newer */
572#endif /* __ARMCC_VERSION */
573#endif /* OFFSETOF */
574
575#ifndef CONTAINEROF
576#define CONTAINEROF(ptr, type, member) ((type *)((char *)(ptr) - OFFSETOF(type, member)))
577#endif /* CONTAINEROF */
578
579/* substruct size up to and including a member of the struct */
580#ifndef STRUCT_SIZE_THROUGH
581#define STRUCT_SIZE_THROUGH(sptr, fname) \
582 (((uint8*)&((sptr)->fname) - (uint8*)(sptr)) + sizeof((sptr)->fname))
583#endif // endif
584
585/* Extracting the size of element in a structure */
586#define SIZE_OF(type, field) sizeof(((type *)0)->field)
587
588#ifndef ARRAYSIZE
589#define ARRAYSIZE(a) (uint32)(sizeof(a) / sizeof(a[0]))
590#endif // endif
591
592#ifndef ARRAYLAST /* returns pointer to last array element */
593#define ARRAYLAST(a) (&a[ARRAYSIZE(a)-1])
594#endif // endif
595
596/* Calculates the required pad size. This is mainly used in register structures */
597#define PADSZ(start, end) ((((end) - (start)) / 4) + 1)
598
599/* Reference a function; used to prevent a static function from being optimized out */
600extern void *_bcmutils_dummy_fn;
601#define REFERENCE_FUNCTION(f) (_bcmutils_dummy_fn = (void *)(f))
602
603/* bit map related macros */
604#ifndef setbit
605#ifndef NBBY /* the BSD family defines NBBY */
606#define NBBY 8 /* 8 bits per byte */
607#endif /* #ifndef NBBY */
608#ifdef BCMUTILS_BIT_MACROS_USE_FUNCS
609extern void setbit(void *array, uint bit);
610extern void clrbit(void *array, uint bit);
611extern bool isset(const void *array, uint bit);
612extern bool isclr(const void *array, uint bit);
613#else
614#define setbit(a, i) (((uint8 *)a)[(i) / NBBY] |= 1 << ((i) % NBBY))
615#define clrbit(a, i) (((uint8 *)a)[(i) / NBBY] &= ~(1 << ((i) % NBBY)))
616#define isset(a, i) (((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY)))
617#define isclr(a, i) ((((const uint8 *)a)[(i) / NBBY] & (1 << ((i) % NBBY))) == 0)
618#endif // endif
619#endif /* setbit */
620
621/* read/write/clear field in a consecutive bits in an octet array.
622 * 'addr' is the octet array's start byte address
623 * 'size' is the octet array's byte size
624 * 'stbit' is the value's start bit offset
625 * 'nbits' is the value's bit size
626 * This set of utilities are for convenience. Don't use them
627 * in time critical/data path as there's a great overhead in them.
628 */
629void setbits(uint8 *addr, uint size, uint stbit, uint nbits, uint32 val);
630uint32 getbits(const uint8 *addr, uint size, uint stbit, uint nbits);
631#define clrbits(addr, size, stbit, nbits) setbits(addr, size, stbit, nbits, 0)
632
633extern void set_bitrange(void *array, uint start, uint end, uint maxbit);
634extern int bcm_find_fsb(uint32 num);
635
636#define isbitset(a, i) (((a) & (1 << (i))) != 0)
637
638#define NBITS(type) (sizeof(type) * 8)
639#define NBITVAL(nbits) (1 << (nbits))
640#define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
641#define NBITMASK(nbits) MAXBITVAL(nbits)
642#define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
643
644extern void bcm_bitprint32(const uint32 u32);
645
646/*
647 * ----------------------------------------------------------------------------
648 * Multiword map of 2bits, nibbles
649 * setbit2 setbit4 (void *ptr, uint32 ix, uint32 val)
650 * getbit2 getbit4 (void *ptr, uint32 ix)
651 * ----------------------------------------------------------------------------
652 */
653
654#define DECLARE_MAP_API(NB, RSH, LSH, OFF, MSK) \
655static INLINE void setbit##NB(void *ptr, uint32 ix, uint32 val) \
656{ \
657 uint32 *addr = (uint32 *)ptr; \
658 uint32 *a = addr + (ix >> RSH); /* (ix / 2^RSH) */ \
659 uint32 pos = (ix & OFF) << LSH; /* (ix % 2^RSH) * 2^LSH */ \
660 uint32 mask = (MSK << pos); \
661 uint32 tmp = *a & ~mask; \
662 *a = tmp | (val << pos); \
663} \
664static INLINE uint32 getbit##NB(void *ptr, uint32 ix) \
665{ \
666 uint32 *addr = (uint32 *)ptr; \
667 uint32 *a = addr + (ix >> RSH); \
668 uint32 pos = (ix & OFF) << LSH; \
669 return ((*a >> pos) & MSK); \
670}
671
672DECLARE_MAP_API(2, 4, 1, 15U, 0x0003) /* setbit2() and getbit2() */
673DECLARE_MAP_API(4, 3, 2, 7U, 0x000F) /* setbit4() and getbit4() */
674DECLARE_MAP_API(8, 2, 3, 3U, 0x00FF) /* setbit8() and getbit8() */
675
676/* basic mux operation - can be optimized on several architectures */
677#define MUX(pred, true, false) ((pred) ? (true) : (false))
678
679/* modulo inc/dec - assumes x E [0, bound - 1] */
680#define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
681#define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
682
683/* modulo inc/dec, bound = 2^k */
684#define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
685#define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
686
687/* modulo add/sub - assumes x, y E [0, bound - 1] */
688#define MODADD(x, y, bound) \
689 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
690#define MODSUB(x, y, bound) \
691 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
692
693/* module add/sub, bound = 2^k */
694#define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
695#define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
696
697/* crc defines */
698#define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
699#define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
700#define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
701#define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
702#define CRC32_INIT_VALUE 0xffffffff /* Initial CRC32 checksum value */
703#define CRC32_GOOD_VALUE 0xdebb20e3 /* Good final CRC32 checksum value */
704
705/* use for direct output of MAC address in printf etc */
706#define MACF "%02x:%02x:%02x:%02x:%02x:%02x"
707#define ETHERP_TO_MACF(ea) ((struct ether_addr *) (ea))->octet[0], \
708 ((struct ether_addr *) (ea))->octet[1], \
709 ((struct ether_addr *) (ea))->octet[2], \
710 ((struct ether_addr *) (ea))->octet[3], \
711 ((struct ether_addr *) (ea))->octet[4], \
712 ((struct ether_addr *) (ea))->octet[5]
713
714#define CONST_ETHERP_TO_MACF(ea) ((const struct ether_addr *) (ea))->octet[0], \
715 ((const struct ether_addr *) (ea))->octet[1], \
716 ((const struct ether_addr *) (ea))->octet[2], \
717 ((const struct ether_addr *) (ea))->octet[3], \
718 ((const struct ether_addr *) (ea))->octet[4], \
719 ((const struct ether_addr *) (ea))->octet[5]
720#define ETHER_TO_MACF(ea) (ea).octet[0], \
721 (ea).octet[1], \
722 (ea).octet[2], \
723 (ea).octet[3], \
724 (ea).octet[4], \
725 (ea).octet[5]
726#if !defined(SIMPLE_MAC_PRINT)
727#define MACDBG "%02x:%02x:%02x:%02x:%02x:%02x"
728#define MAC2STRDBG(ea) CONST_ETHERP_TO_MACF(ea)
729#else
730#define MACDBG "%02x:xx:xx:xx:x%x:%02x"
731#define MAC2STRDBG(ea) ((uint8*)(ea))[0], (((uint8*)(ea))[4] & 0xf), ((uint8*)(ea))[5]
732#endif /* SIMPLE_MAC_PRINT */
733
734#define MACOUIDBG "%02x:%x:%02x"
735#define MACOUI2STRDBG(ea) ((uint8*)(ea))[0], ((uint8*)(ea))[1] & 0xf, ((uint8*)(ea))[2]
736
737#define MACOUI "%02x:%02x:%02x"
738#define MACOUI2STR(ea) ((uint8*)(ea))[0], ((uint8*)(ea))[1], ((uint8*)(ea))[2]
739
740/* bcm_format_flags() bit description structure */
741typedef struct bcm_bit_desc {
742 uint32 bit;
743 const char* name;
744} bcm_bit_desc_t;
745
746/* bcm_format_field */
747typedef struct bcm_bit_desc_ex {
748 uint32 mask;
749 const bcm_bit_desc_t *bitfield;
750} bcm_bit_desc_ex_t;
751
752/* buffer length for ethernet address from bcm_ether_ntoa() */
753#define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
754
755static INLINE uint32 /* 32bit word aligned xor-32 */
756bcm_compute_xor32(volatile uint32 *u32_val, int num_u32)
757{
758 int idx;
759 uint32 xor32 = 0;
760 for (idx = 0; idx < num_u32; idx++)
761 xor32 ^= *(u32_val + idx);
762 return xor32;
763}
764
765/* crypto utility function */
766/* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
767static INLINE void
768xor_128bit_block(const uint8 *src1, const uint8 *src2, uint8 *dst)
769{
770 if (
771#ifdef __i386__
772 1 ||
773#endif // endif
774 (((uintptr)src1 | (uintptr)src2 | (uintptr)dst) & 3) == 0) {
775 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
776 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
777 ((uint32 *)dst)[0] = ((const uint32 *)src1)[0] ^ ((const uint32 *)src2)[0];
778 ((uint32 *)dst)[1] = ((const uint32 *)src1)[1] ^ ((const uint32 *)src2)[1];
779 ((uint32 *)dst)[2] = ((const uint32 *)src1)[2] ^ ((const uint32 *)src2)[2];
780 ((uint32 *)dst)[3] = ((const uint32 *)src1)[3] ^ ((const uint32 *)src2)[3];
781 } else {
782 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
783 int k;
784 for (k = 0; k < 16; k++)
785 dst[k] = src1[k] ^ src2[k];
786 }
787}
788
789/* externs */
790/* crc */
791uint8 hndcrc8(const uint8 *p, uint nbytes, uint8 crc);
792uint16 hndcrc16(const uint8 *p, uint nbytes, uint16 crc);
793uint32 hndcrc32(const uint8 *p, uint nbytes, uint32 crc);
794
795/* format/print */
796#if defined(DHD_DEBUG) || defined(WLMSG_PRHDRS) || defined(WLMSG_PRPKT) || \
797 defined(WLMSG_ASSOC)
798/* print out the value a field has: fields may have 1-32 bits and may hold any value */
799extern int bcm_format_field(const bcm_bit_desc_ex_t *bd, uint32 field, char* buf, int len);
800/* print out which bits in flags are set */
801extern int bcm_format_flags(const bcm_bit_desc_t *bd, uint32 flags, char* buf, int len);
802/* print out whcih bits in octet array 'addr' are set. bcm_bit_desc_t:bit is a bit offset. */
803int bcm_format_octets(const bcm_bit_desc_t *bd, uint bdsz,
804 const uint8 *addr, uint size, char *buf, int len);
805#endif // endif
806
807extern int bcm_format_hex(char *str, const void *bytes, int len);
808
809extern const char *bcm_crypto_algo_name(uint algo);
810extern char *bcm_chipname(uint chipid, char *buf, uint len);
811extern char *bcm_brev_str(uint32 brev, char *buf);
812extern void printbig(char *buf);
813extern void prhex(const char *msg, const uchar *buf, uint len);
814
815/* bcmerror */
816extern const char *bcmerrorstr(int bcmerror);
817
818extern int wl_set_up_table(uint8 *up_table, bcm_tlv_t *qos_map_ie);
819
820/* multi-bool data type: set of bools, mbool is true if any is set */
821typedef uint32 mbool;
822#define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
823#define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
824#define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* TRUE if one bool is set */
825#define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
826
827/* generic datastruct to help dump routines */
828struct fielddesc {
829 const char *nameandfmt;
830 uint32 offset;
831 uint32 len;
832};
833
834extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
835extern void bcm_bprhex(struct bcmstrbuf *b, const char *msg, bool newline,
836 const uint8 *buf, int len);
837
838extern void bcm_inc_bytes(uchar *num, int num_bytes, uint8 amount);
839extern int bcm_cmp_bytes(const uchar *arg1, const uchar *arg2, uint8 nbytes);
840extern void bcm_print_bytes(const char *name, const uchar *cdata, int len);
841
842typedef uint32 (*bcmutl_rdreg_rtn)(void *arg0, uint arg1, uint32 offset);
843extern uint bcmdumpfields(bcmutl_rdreg_rtn func_ptr, void *arg0, uint arg1, struct fielddesc *str,
844 char *buf, uint32 bufsize);
845extern uint bcm_bitcount(uint8 *bitmap, uint bytelength);
846
847extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
848
849/* power conversion */
850extern uint16 bcm_qdbm_to_mw(uint8 qdbm);
851extern uint8 bcm_mw_to_qdbm(uint16 mw);
852extern uint bcm_mkiovar(const char *name, const char *data, uint datalen, char *buf, uint len);
853
854unsigned int process_nvram_vars(char *varbuf, unsigned int len);
855
856/* trace any object allocation / free, with / without features (flags) set to the object */
857
858#define BCM_OBJDBG_ADD 1
859#define BCM_OBJDBG_REMOVE 2
860#define BCM_OBJDBG_ADD_PKT 3
861
862/* object feature: set or clear flags */
863#define BCM_OBJECT_FEATURE_FLAG 1
864#define BCM_OBJECT_FEATURE_PKT_STATE 2
865/* object feature: flag bits */
866#define BCM_OBJECT_FEATURE_0 (1 << 0)
867#define BCM_OBJECT_FEATURE_1 (1 << 1)
868#define BCM_OBJECT_FEATURE_2 (1 << 2)
869/* object feature: clear flag bits field set with this flag */
870#define BCM_OBJECT_FEATURE_CLEAR (1 << 31)
871#ifdef BCM_OBJECT_TRACE
872#define bcm_pkt_validate_chk(obj) do { \
873 void * pkttag; \
874 bcm_object_trace_chk(obj, 0, 0, \
875 __FUNCTION__, __LINE__); \
876 if ((pkttag = PKTTAG(obj))) { \
877 bcm_object_trace_chk(obj, 1, DHD_PKTTAG_SN(pkttag), \
878 __FUNCTION__, __LINE__); \
879 } \
880} while (0)
881extern void bcm_object_trace_opr(void *obj, uint32 opt, const char *caller, int line);
882extern void bcm_object_trace_upd(void *obj, void *obj_new);
883extern void bcm_object_trace_chk(void *obj, uint32 chksn, uint32 sn,
884 const char *caller, int line);
885extern void bcm_object_feature_set(void *obj, uint32 type, uint32 value);
886extern int bcm_object_feature_get(void *obj, uint32 type, uint32 value);
887extern void bcm_object_trace_init(void);
888extern void bcm_object_trace_deinit(void);
889#else
890#define bcm_pkt_validate_chk(obj)
891#define bcm_object_trace_opr(a, b, c, d)
892#define bcm_object_trace_upd(a, b)
893#define bcm_object_trace_chk(a, b, c, d, e)
894#define bcm_object_feature_set(a, b, c)
895#define bcm_object_feature_get(a, b, c)
896#define bcm_object_trace_init()
897#define bcm_object_trace_deinit()
898#endif /* BCM_OBJECT_TRACE */
899
900/* Public domain bit twiddling hacks/utilities: Sean Eron Anderson */
901
902/* Table driven count set bits. */
903static const uint8 /* Table only for use by bcm_cntsetbits */
904_CSBTBL[256] =
905{
906# define B2(n) n, n + 1, n + 1, n + 2
907# define B4(n) B2(n), B2(n + 1), B2(n + 1), B2(n + 2)
908# define B6(n) B4(n), B4(n + 1), B4(n + 1), B4(n + 2)
909 B6(0), B6(0 + 1), B6(0 + 1), B6(0 + 2)
910};
911
912static INLINE uint32 /* Uses table _CSBTBL for fast counting of 1's in a u32 */
913bcm_cntsetbits(const uint32 u32arg)
914{
915 /* function local scope declaration of const _CSBTBL[] */
916 const uint8 * p = (const uint8 *)&u32arg;
917 return (_CSBTBL[p[0]] + _CSBTBL[p[1]] + _CSBTBL[p[2]] + _CSBTBL[p[3]]);
918}
919
920static INLINE int /* C equivalent count of leading 0's in a u32 */
921C_bcm_count_leading_zeros(uint32 u32arg)
922{
923 int shifts = 0;
924 while (u32arg) {
925 shifts++; u32arg >>= 1;
926 }
927 return (32U - shifts);
928}
929
930/* the format of current TCM layout during boot
931 *
932 * Code Unused memory Random numbers Random number Magic number NVRAM NVRAM
933 * byte Count 0xFEEDC0DE Size
934 * |<-----Variable---->|<---Variable--->|<-----4 bytes-->|<---4 bytes---->|<---V--->|<--4B--->|
935 * |<------------- BCM_ENTROPY_HOST_MAXSIZE --------->|
936 */
937
938/* The HOST need to provided 64 bytes (512 bits) entropy for the bcm SW RNG */
939#define BCM_ENTROPY_MAGIC_SIZE 4u
940#define BCM_ENTROPY_COUNT_SIZE 4u
941#define BCM_ENTROPY_MIN_NBYTES 64u
942#define BCM_ENTROPY_MAX_NBYTES 512u
943#define BCM_ENTROPY_HOST_NBYTES 128u
944#define BCM_ENTROPY_HOST_MAXSIZE \
945 (BCM_ENTROPY_MAGIC_SIZE + BCM_ENTROPY_COUNT_SIZE + BCM_ENTROPY_MAX_NBYTES)
946
947/* Keep BCM MAX_RAND NUMBERS definition for the current dongle image. It will be
948 * removed after the dongle image is updated to use the bcm RNG.
949 */
950#define BCM_MAX_RAND_NUMBERS 2u
951
952/* Constant for calculate the location of host entropy input */
953#define BCM_NVRAM_OFFSET_TCM 4u
954#define BCM_NVRAM_IMG_COMPRS_FACTOR 4u
955#define BCM_NVRAM_RNG_SIGNATURE 0xFEEDC0DEu
956
957typedef struct bcm_rand_metadata {
958 uint32 count; /* number of random numbers in bytes */
959 uint32 signature; /* host fills it in, FW verfies before reading rand */
960} bcm_rand_metadata_t;
961
962typedef struct bcm_host_whitelist_metadata {
963 uint32 signature; /* host fills it in, FW verfies before reading Whitelist region */
964 uint32 count; /* size of whitelist region in bytes */
965} bcm_host_whitelist_metadata_t;
966
967#ifdef BCMDRIVER
968/*
969 * Assembly instructions: Count Leading Zeros
970 * "clz" : MIPS, ARM
971 * "cntlzw" : PowerPC
972 * "BSF" : x86
973 * "lzcnt" : AMD, SPARC
974 */
975
976#if defined(__arm__)
977#if defined(__ARM_ARCH_7M__) /* Cortex M3 */
978#define __USE_ASM_CLZ__
979#endif /* __ARM_ARCH_7M__ */
980#if defined(__ARM_ARCH_7R__) /* Cortex R4 */
981#define __USE_ASM_CLZ__
982#endif /* __ARM_ARCH_7R__ */
983#endif /* __arm__ */
984
985static INLINE int
986bcm_count_leading_zeros(uint32 u32arg)
987{
988#if defined(__USE_ASM_CLZ__)
989 int zeros;
990 __asm__ volatile("clz %0, %1 \n" : "=r" (zeros) : "r" (u32arg));
991 return zeros;
992#else /* C equivalent */
993 return C_bcm_count_leading_zeros(u32arg);
994#endif /* C equivalent */
995}
996
997/*
998 * Macro to count leading zeroes
999 *
1000 */
1001#if defined(__GNUC__)
1002#define CLZ(x) __builtin_clzl(x)
1003#elif defined(__arm__)
1004#define CLZ(x) __clz(x)
1005#else
1006#define CLZ(x) bcm_count_leading_zeros(x)
1007#endif /* __GNUC__ */
1008
1009/* INTERFACE: Multiword bitmap based small id allocator. */
1010struct bcm_mwbmap; /* forward declaration for use as an opaque mwbmap handle */
1011
1012#define BCM_MWBMAP_INVALID_HDL ((struct bcm_mwbmap *)NULL)
1013#define BCM_MWBMAP_INVALID_IDX ((uint32)(~0U))
1014
1015/* Incarnate a multiword bitmap based small index allocator */
1016extern struct bcm_mwbmap * bcm_mwbmap_init(osl_t * osh, uint32 items_max);
1017
1018/* Free up the multiword bitmap index allocator */
1019extern void bcm_mwbmap_fini(osl_t * osh, struct bcm_mwbmap * mwbmap_hdl);
1020
1021/* Allocate a unique small index using a multiword bitmap index allocator */
1022extern uint32 bcm_mwbmap_alloc(struct bcm_mwbmap * mwbmap_hdl);
1023
1024/* Force an index at a specified position to be in use */
1025extern void bcm_mwbmap_force(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1026
1027/* Free a previously allocated index back into the multiword bitmap allocator */
1028extern void bcm_mwbmap_free(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1029
1030/* Fetch the toal number of free indices in the multiword bitmap allocator */
1031extern uint32 bcm_mwbmap_free_cnt(struct bcm_mwbmap * mwbmap_hdl);
1032
1033/* Determine whether an index is inuse or free */
1034extern bool bcm_mwbmap_isfree(struct bcm_mwbmap * mwbmap_hdl, uint32 bitix);
1035
1036/* Debug dump a multiword bitmap allocator */
1037extern void bcm_mwbmap_show(struct bcm_mwbmap * mwbmap_hdl);
1038
1039extern void bcm_mwbmap_audit(struct bcm_mwbmap * mwbmap_hdl);
1040/* End - Multiword bitmap based small Id allocator. */
1041
1042/* INTERFACE: Simple unique 16bit Id Allocator using a stack implementation. */
1043
1044#define ID8_INVALID 0xFFu
1045#define ID16_INVALID 0xFFFFu
1046#define ID32_INVALID 0xFFFFFFFFu
1047#define ID16_UNDEFINED ID16_INVALID
1048
1049/*
1050 * Construct a 16bit id allocator, managing 16bit ids in the range:
1051 * [start_val16 .. start_val16+total_ids)
1052 * Note: start_val16 is inclusive.
1053 * Returns an opaque handle to the 16bit id allocator.
1054 */
1055extern void * id16_map_init(osl_t *osh, uint16 total_ids, uint16 start_val16);
1056extern void * id16_map_fini(osl_t *osh, void * id16_map_hndl);
1057extern void id16_map_clear(void * id16_map_hndl, uint16 total_ids, uint16 start_val16);
1058
1059/* Allocate a unique 16bit id */
1060extern uint16 id16_map_alloc(void * id16_map_hndl);
1061
1062/* Free a 16bit id value into the id16 allocator */
1063extern void id16_map_free(void * id16_map_hndl, uint16 val16);
1064
1065/* Get the number of failures encountered during id allocation. */
1066extern uint32 id16_map_failures(void * id16_map_hndl);
1067
1068/* Audit the 16bit id allocator state. */
1069extern bool id16_map_audit(void * id16_map_hndl);
1070/* End - Simple 16bit Id Allocator. */
1071#endif /* BCMDRIVER */
1072
1073#define MASK_32_BITS (~0)
1074#define MASK_8_BITS ((1 << 8) - 1)
1075
1076#define EXTRACT_LOW32(num) (uint32)(num & MASK_32_BITS)
1077#define EXTRACT_HIGH32(num) (uint32)(((uint64)num >> 32) & MASK_32_BITS)
1078
1079#define MAXIMUM(a, b) ((a > b) ? a : b)
1080#define MINIMUM(a, b) ((a < b) ? a : b)
1081#define LIMIT(x, min, max) ((x) < (min) ? (min) : ((x) > (max) ? (max) : (x)))
1082
1083/* calculate checksum for ip header, tcp / udp header / data */
1084uint16 bcm_ip_cksum(uint8 *buf, uint32 len, uint32 sum);
1085
1086#ifndef _dll_t_
1087#define _dll_t_
1088/*
1089 * -----------------------------------------------------------------------------
1090 * Double Linked List Macros
1091 * -----------------------------------------------------------------------------
1092 *
1093 * All dll operations must be performed on a pre-initialized node.
1094 * Inserting an uninitialized node into a list effectively initialized it.
1095 *
1096 * When a node is deleted from a list, you may initialize it to avoid corruption
1097 * incurred by double deletion. You may skip initialization if the node is
1098 * immediately inserted into another list.
1099 *
1100 * By placing a dll_t element at the start of a struct, you may cast a dll_t *
1101 * to the struct or vice versa.
1102 *
1103 * Example of declaring an initializing someList and inserting nodeA, nodeB
1104 *
1105 * typedef struct item {
1106 * dll_t node;
1107 * int someData;
1108 * } Item_t;
1109 * Item_t nodeA, nodeB, nodeC;
1110 * nodeA.someData = 11111, nodeB.someData = 22222, nodeC.someData = 33333;
1111 *
1112 * dll_t someList;
1113 * dll_init(&someList);
1114 *
1115 * dll_append(&someList, (dll_t *) &nodeA);
1116 * dll_prepend(&someList, &nodeB.node);
1117 * dll_insert((dll_t *)&nodeC, &nodeA.node);
1118 *
1119 * dll_delete((dll_t *) &nodeB);
1120 *
1121 * Example of a for loop to walk someList of node_p
1122 *
1123 * extern void mydisplay(Item_t * item_p);
1124 *
1125 * dll_t * item_p, * next_p;
1126 * for (item_p = dll_head_p(&someList); ! dll_end(&someList, item_p);
1127 * item_p = next_p)
1128 * {
1129 * next_p = dll_next_p(item_p);
1130 * ... use item_p at will, including removing it from list ...
1131 * mydisplay((PItem_t)item_p);
1132 * }
1133 *
1134 * -----------------------------------------------------------------------------
1135 */
1136typedef struct dll {
1137 struct dll * next_p;
1138 struct dll * prev_p;
1139} dll_t;
1140
1141static INLINE void
1142dll_init(dll_t *node_p)
1143{
1144 node_p->next_p = node_p;
1145 node_p->prev_p = node_p;
1146}
1147/* dll macros returing a pointer to dll_t */
1148
1149static INLINE dll_t *
1150dll_head_p(dll_t *list_p)
1151{
1152 return list_p->next_p;
1153}
1154
1155static INLINE dll_t *
1156dll_tail_p(dll_t *list_p)
1157{
1158 return (list_p)->prev_p;
1159}
1160
1161static INLINE dll_t *
1162dll_next_p(dll_t *node_p)
1163{
1164 return (node_p)->next_p;
1165}
1166
1167static INLINE dll_t *
1168dll_prev_p(dll_t *node_p)
1169{
1170 return (node_p)->prev_p;
1171}
1172
1173static INLINE bool
1174dll_empty(dll_t *list_p)
1175{
1176 return ((list_p)->next_p == (list_p));
1177}
1178
1179static INLINE bool
1180dll_end(dll_t *list_p, dll_t * node_p)
1181{
1182 return (list_p == node_p);
1183}
1184
1185/* inserts the node new_p "after" the node at_p */
1186static INLINE void
1187dll_insert(dll_t *new_p, dll_t * at_p)
1188{
1189 new_p->next_p = at_p->next_p;
1190 new_p->prev_p = at_p;
1191 at_p->next_p = new_p;
1192 (new_p->next_p)->prev_p = new_p;
1193}
1194
1195static INLINE void
1196dll_append(dll_t *list_p, dll_t *node_p)
1197{
1198 dll_insert(node_p, dll_tail_p(list_p));
1199}
1200
1201static INLINE void
1202dll_prepend(dll_t *list_p, dll_t *node_p)
1203{
1204 dll_insert(node_p, list_p);
1205}
1206
1207/* deletes a node from any list that it "may" be in, if at all. */
1208static INLINE void
1209dll_delete(dll_t *node_p)
1210{
1211 node_p->prev_p->next_p = node_p->next_p;
1212 node_p->next_p->prev_p = node_p->prev_p;
1213}
1214#endif /* ! defined(_dll_t_) */
1215
1216/* Elements managed in a double linked list */
1217
1218typedef struct dll_pool {
1219 dll_t free_list;
1220 uint16 free_count;
1221 uint16 elems_max;
1222 uint16 elem_size;
1223 dll_t elements[1];
1224} dll_pool_t;
1225
1226dll_pool_t * dll_pool_init(void * osh, uint16 elems_max, uint16 elem_size);
1227void * dll_pool_alloc(dll_pool_t * dll_pool_p);
1228void dll_pool_free(dll_pool_t * dll_pool_p, void * elem_p);
1229void dll_pool_free_tail(dll_pool_t * dll_pool_p, void * elem_p);
1230typedef void (* dll_elem_dump)(void * elem_p);
1231void dll_pool_detach(void * osh, dll_pool_t * pool, uint16 elems_max, uint16 elem_size);
1232
1233int valid_bcmerror(int e);
1234
1235/* calculate IPv4 header checksum
1236 * - input ip points to IP header in network order
1237 * - output cksum is in network order
1238 */
1239uint16 ipv4_hdr_cksum(uint8 *ip, int ip_len);
1240
1241/* calculate IPv4 TCP header checksum
1242 * - input ip and tcp points to IP and TCP header in network order
1243 * - output cksum is in network order
1244 */
1245uint16 ipv4_tcp_hdr_cksum(uint8 *ip, uint8 *tcp, uint16 tcp_len);
1246
1247/* calculate IPv6 TCP header checksum
1248 * - input ipv6 and tcp points to IPv6 and TCP header in network order
1249 * - output cksum is in network order
1250 */
1251uint16 ipv6_tcp_hdr_cksum(uint8 *ipv6, uint8 *tcp, uint16 tcp_len);
1252
1253#ifdef __cplusplus
1254 }
1255#endif // endif
1256
1257/* #define DEBUG_COUNTER */
1258#ifdef DEBUG_COUNTER
1259#define CNTR_TBL_MAX 10
1260typedef struct _counter_tbl_t {
1261 char name[16]; /* name of this counter table */
1262 uint32 prev_log_print; /* Internal use. Timestamp of the previous log print */
1263 uint log_print_interval; /* Desired interval to print logs in ms */
1264 uint needed_cnt; /* How many counters need to be used */
1265 uint32 cnt[CNTR_TBL_MAX]; /* Counting entries to increase at desired places */
1266 bool enabled; /* Whether to enable printing log */
1267} counter_tbl_t;
1268
1269void counter_printlog(counter_tbl_t *ctr_tbl);
1270#endif /* DEBUG_COUNTER */
1271
1272#if defined(__GNUC__)
1273#define CALL_SITE __builtin_return_address(0)
1274#else
1275#define CALL_SITE ((void*) 0)
1276#endif // endif
1277#ifdef SHOW_LOGTRACE
1278#define TRACE_LOG_BUF_MAX_SIZE 1700
1279#define BUF_NOT_AVAILABLE 0
1280#define NEXT_BUF_NOT_AVAIL 1
1281#define NEXT_BUF_AVAIL 2
1282
1283typedef struct trace_buf_info {
1284 int availability;
1285 int size;
1286 char buf[TRACE_LOG_BUF_MAX_SIZE];
1287} trace_buf_info_t;
1288#endif /* SHOW_LOGTRACE */
1289
1290enum dump_dongle_e {
1291 DUMP_DONGLE_COREREG = 0,
1292 DUMP_DONGLE_D11MEM
1293};
1294
1295typedef struct {
1296 uint32 type; /**< specifies e.g dump of d11 memory, use enum dump_dongle_e */
1297 uint32 index; /**< iterator1, specifies core index or d11 memory index */
1298 uint32 offset; /**< iterator2, byte offset within register set or memory */
1299} dump_dongle_in_t;
1300
1301typedef struct {
1302 uint32 address; /**< e.g. backplane address of register */
1303 uint32 id; /**< id, e.g. core id */
1304 uint32 rev; /**< rev, e.g. core rev */
1305 uint32 n_bytes; /**< nbytes in array val[] */
1306 uint32 val[1]; /**< out: values that were read out of registers or memory */
1307} dump_dongle_out_t;
1308
1309extern uint32 sqrt_int(uint32 value);
1310
1311#ifdef BCMDRIVER
1312/* structures and routines to process variable sized data */
1313typedef struct var_len_data {
1314 uint32 vlen;
1315 uint8 *vdata;
1316} var_len_data_t;
1317
1318int bcm_vdata_alloc(osl_t *osh, var_len_data_t *vld, uint32 size);
1319int bcm_vdata_free(osl_t *osh, var_len_data_t *vld);
1320#endif /* BCMDRIVER */
1321
1322/* Count the number of elements in an array that do not match the given value */
1323extern int array_value_mismatch_count(uint8 value, uint8 *array, int array_size);
1324/* Count the number of non-zero elements in an uint8 array */
1325extern int array_nonzero_count(uint8 *array, int array_size);
1326/* Count the number of non-zero elements in an int16 array */
1327extern int array_nonzero_count_int16(int16 *array, int array_size);
1328/* Count the number of zero elements in an uint8 array */
1329extern int array_zero_count(uint8 *array, int array_size);
1330/* Validate a uint8 ordered array. Assert if invalid. */
1331extern int verify_ordered_array_uint8(uint8 *array, int array_size, uint8 range_lo, uint8 range_hi);
1332/* Validate a int16 configuration array that need not be zero-terminated. Assert if invalid. */
1333extern int verify_ordered_array_int16(int16 *array, int array_size, int16 range_lo, int16 range_hi);
1334/* Validate all values in an array are in range */
1335extern int verify_array_values(uint8 *array, int array_size,
1336 int range_lo, int range_hi, bool zero_terminated);
1337
1338#endif /* _bcmutils_h_ */