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