bcmutils.h 48 KB

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