2 * Copyright (c) 2010 Broadcom Corporation
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* Buffer structure for collecting string-formatted data
21 * using bcm_bprintf() API.
22 * Use bcm_binit() to initialize before use
26 char *buf; /* pointer to current position in origbuf */
27 unsigned int size; /* current (residual) size in bytes */
28 char *origbuf; /* unmodified pointer to orignal buffer */
29 unsigned int origsize; /* unmodified orignal buffer size in bytes */
32 /* ** driver-only section ** */
35 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
38 * Spin at most 'us' microseconds while 'exp' is true.
39 * Caller should explicitly test 'exp' when this completes
40 * and take appropriate error action if 'exp' is still true.
42 #define SPINWAIT(exp, us) { \
43 uint countdown = (us) + 9; \
44 while ((exp) && (countdown >= 10)) {\
50 /* osl multi-precedence packet queue */
51 #ifndef PKTQ_LEN_DEFAULT
52 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
55 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
58 typedef struct pktq_prec {
59 void *head; /* first packet to dequeue */
60 void *tail; /* last packet to dequeue */
61 u16 len; /* number of queued packets */
62 u16 max; /* maximum number of queued packets */
65 /* multi-priority pkt queue */
67 u16 num_prec; /* number of precedences in use */
68 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
69 u16 max; /* total max packets */
70 u16 len; /* total number of packets */
71 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
72 struct pktq_prec q[PKTQ_MAX_PREC];
75 /* simple, non-priority pkt queue */
77 u16 num_prec; /* number of precedences in use (always 1) */
78 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
79 u16 max; /* total max packets */
80 u16 len; /* total number of packets */
81 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
82 struct pktq_prec q[1];
85 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
87 /* fn(pkt, arg). return true if pkt belongs to if */
88 typedef bool(*ifpkt_cb_t) (void *, int);
90 /* forward definition of ether_addr structure used by some function prototypes */
94 extern int ether_isbcast(const void *ea);
95 extern int ether_isnulladdr(const void *ea);
97 /* operations on a specific precedence in packet queue */
99 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
100 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
101 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
102 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
103 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
105 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
106 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
108 extern void *pktq_penq(struct pktq *pq, int prec, void *p);
109 extern void *pktq_penq_head(struct pktq *pq, int prec, void *p);
110 extern void *pktq_pdeq(struct pktq *pq, int prec);
111 extern void *pktq_pdeq_tail(struct pktq *pq, int prec);
112 /* Empty the queue at particular precedence level */
114 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec,
117 extern void pktq_pflush(osl_t *osh, struct pktq *pq, int prec,
118 bool dir, ifpkt_cb_t fn, int arg);
119 #endif /* BRCM_FULLMAC */
121 /* operations on a set of precedences in packet queue */
123 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
124 extern void *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
126 /* operations on packet queue as a whole */
128 #define pktq_len(pq) ((int)(pq)->len)
129 #define pktq_max(pq) ((int)(pq)->max)
130 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
131 #define pktq_full(pq) ((pq)->len >= (pq)->max)
132 #define pktq_empty(pq) ((pq)->len == 0)
134 /* operations for single precedence queues */
135 #define pktenq(pq, p) pktq_penq(((struct pktq *)pq), 0, (p))
136 #define pktenq_head(pq, p) pktq_penq_head(((struct pktq *)pq), 0, (p))
137 #define pktdeq(pq) pktq_pdeq(((struct pktq *)pq), 0)
138 #define pktdeq_tail(pq) pktq_pdeq_tail(((struct pktq *)pq), 0)
139 #define pktqinit(pq, len) pktq_init(((struct pktq *)pq), 1, len)
141 extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
142 /* prec_out may be NULL if caller is not interested in return value */
143 extern void *pktq_peek_tail(struct pktq *pq, int *prec_out);
145 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir);
147 extern void pktq_flush(osl_t *osh, struct pktq *pq, bool dir,
148 ifpkt_cb_t fn, int arg);
153 extern uint pktfrombuf(osl_t *osh, void *p, uint offset, int len,
155 extern uint pktsegcnt(osl_t *osh, void *p);
156 extern uint pkttotlen(osl_t *osh, void *p);
158 /* ethernet address */
159 extern int bcm_ether_atoe(char *p, struct ether_addr *ea);
163 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
165 /* variable access */
166 extern char *getvar(char *vars, const char *name);
167 extern int getintvar(char *vars, const char *name);
169 extern void prpkt(const char *msg, osl_t *osh, void *p0);
171 #define bcm_perf_enable()
172 #define bcmstats(fmt)
173 #define bcmlog(fmt, a1, a2)
174 #define bcmdumplog(buf, size) (*buf = '\0')
175 #define bcmdumplogent(buf, idx) -1
177 #define bcmtslog(tstamp, fmt, a1, a2)
178 #define bcmprinttslogs()
179 #define bcmprinttstamp(us)
181 /* Support for sharing code across in-driver iovar implementations.
182 * The intent is that a driver use this structure to map iovar names
183 * to its (private) iovar identifiers, and the lookup function to
184 * find the entry. Macros are provided to map ids and get/set actions
185 * into a single number space for a switch statement.
188 /* iovar structure */
189 typedef struct bcm_iovar {
190 const char *name; /* name for lookup and display */
191 u16 varid; /* id for switch */
192 u16 flags; /* driver-specific flag bits */
193 u16 type; /* base type of argument */
194 u16 minlen; /* min length for buffer vars */
197 /* varid definitions are per-driver, may use these get/set bits */
199 /* IOVar action bits for id mapping */
200 #define IOV_GET 0 /* Get an iovar */
201 #define IOV_SET 1 /* Set an iovar */
203 /* Varid to actionid mapping */
204 #define IOV_GVAL(id) ((id)*2)
205 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
206 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
207 #define IOV_ID(actionid) (actionid >> 1)
209 /* flags are per-driver based on driver attributes */
211 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table,
213 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg,
216 /* Base type definitions */
217 #define IOVT_VOID 0 /* no value (implictly set only) */
218 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
219 #define IOVT_INT8 2 /* integer values are range-checked */
220 #define IOVT_UINT8 3 /* unsigned int 8 bits */
221 #define IOVT_INT16 4 /* int 16 bits */
222 #define IOVT_UINT16 5 /* unsigned int 16 bits */
223 #define IOVT_INT32 6 /* int 32 bits */
224 #define IOVT_UINT32 7 /* unsigned int 32 bits */
225 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
226 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
228 /* Initializer for IOV type strings */
229 #define BCM_IOV_TYPE_INIT { \
241 #define BCM_IOVT_IS_INT(type) (\
242 (type == IOVT_BOOL) || \
243 (type == IOVT_INT8) || \
244 (type == IOVT_UINT8) || \
245 (type == IOVT_INT16) || \
246 (type == IOVT_UINT16) || \
247 (type == IOVT_INT32) || \
248 (type == IOVT_UINT32))
250 /* ** driver/apps-shared section ** */
252 #define BCME_STRLEN 64 /* Max string length for BCM errors */
253 #define VALID_BCMERROR(e) ((e <= 0) && (e >= BCME_LAST))
256 * error codes could be added but the defined ones shouldn't be changed/deleted
257 * these error codes are exposed to the user code
258 * when ever a new error code is added to this list
259 * please update errorstring table with the related error string and
260 * update osl files with os specific errorcode map
263 #define BCME_OK 0 /* Success */
264 #define BCME_ERROR -1 /* Error generic */
265 #define BCME_BADARG -2 /* Bad Argument */
266 #define BCME_BADOPTION -3 /* Bad option */
267 #define BCME_NOTUP -4 /* Not up */
268 #define BCME_NOTDOWN -5 /* Not down */
269 #define BCME_NOTAP -6 /* Not AP */
270 #define BCME_NOTSTA -7 /* Not STA */
271 #define BCME_BADKEYIDX -8 /* BAD Key Index */
272 #define BCME_RADIOOFF -9 /* Radio Off */
273 #define BCME_NOTBANDLOCKED -10 /* Not band locked */
274 #define BCME_NOCLK -11 /* No Clock */
275 #define BCME_BADRATESET -12 /* BAD Rate valueset */
276 #define BCME_BADBAND -13 /* BAD Band */
277 #define BCME_BUFTOOSHORT -14 /* Buffer too short */
278 #define BCME_BUFTOOLONG -15 /* Buffer too long */
279 #define BCME_BUSY -16 /* Busy */
280 #define BCME_NOTASSOCIATED -17 /* Not Associated */
281 #define BCME_BADSSIDLEN -18 /* Bad SSID len */
282 #define BCME_OUTOFRANGECHAN -19 /* Out of Range Channel */
283 #define BCME_BADCHAN -20 /* Bad Channel */
284 #define BCME_BADADDR -21 /* Bad Address */
285 #define BCME_NORESOURCE -22 /* Not Enough Resources */
286 #define BCME_UNSUPPORTED -23 /* Unsupported */
287 #define BCME_BADLEN -24 /* Bad length */
288 #define BCME_NOTREADY -25 /* Not Ready */
289 #define BCME_EPERM -26 /* Not Permitted */
290 #define BCME_NOMEM -27 /* No Memory */
291 #define BCME_ASSOCIATED -28 /* Associated */
292 #define BCME_RANGE -29 /* Not In Range */
293 #define BCME_NOTFOUND -30 /* Not Found */
294 #define BCME_WME_NOT_ENABLED -31 /* WME Not Enabled */
295 #define BCME_TSPEC_NOTFOUND -32 /* TSPEC Not Found */
296 #define BCME_ACM_NOTSUPPORTED -33 /* ACM Not Supported */
297 #define BCME_NOT_WME_ASSOCIATION -34 /* Not WME Association */
298 #define BCME_SDIO_ERROR -35 /* SDIO Bus Error */
299 #define BCME_DONGLE_DOWN -36 /* Dongle Not Accessible */
300 #define BCME_VERSION -37 /* Incorrect version */
301 #define BCME_TXFAIL -38 /* TX failure */
302 #define BCME_RXFAIL -39 /* RX failure */
303 #define BCME_NODEVICE -40 /* Device not present */
304 #define BCME_NMODE_DISABLED -41 /* NMODE disabled */
305 #define BCME_NONRESIDENT -42 /* access to nonresident overlay */
306 #define BCME_LAST BCME_NONRESIDENT
308 /* These are collection of BCME Error strings */
309 #define BCMERRSTRINGTABLE { \
322 "Bad Rate valueset", \
324 "Buffer too short", \
329 "Out of Range Channel", \
332 "Not Enough Resources", \
343 "ACM Not Supported", \
344 "Not WME Association", \
346 "Dongle Not Accessible", \
347 "Incorrect version", \
350 "Device Not Present", \
352 "Nonresident overlay access", \
356 #define ABS(a) (((a) < 0) ? -(a) : (a))
359 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
360 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
362 /* bit map related macros */
364 #ifndef NBBY /* the BSD family defines NBBY */
365 #define NBBY 8 /* 8 bits per byte */
366 #endif /* #ifndef NBBY */
367 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
368 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
369 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
370 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
373 #define NBITS(type) (sizeof(type) * 8)
374 #define NBITVAL(nbits) (1 << (nbits))
375 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
376 #define NBITMASK(nbits) MAXBITVAL(nbits)
377 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
379 /* basic mux operation - can be optimized on several architectures */
380 #define MUX(pred, true, false) ((pred) ? (true) : (false))
382 /* modulo inc/dec - assumes x E [0, bound - 1] */
383 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
384 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
386 /* modulo inc/dec, bound = 2^k */
387 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
388 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
390 /* modulo add/sub - assumes x, y E [0, bound - 1] */
391 #define MODADD(x, y, bound) \
392 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
393 #define MODSUB(x, y, bound) \
394 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
396 /* module add/sub, bound = 2^k */
397 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
398 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
401 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
402 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
403 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
404 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
406 /* bcm_format_flags() bit description structure */
407 typedef struct bcm_bit_desc {
412 /* tag_ID/length/value_buffer tuple */
413 typedef struct bcm_tlv {
419 /* Check that bcm_tlv_t fits into the given buflen */
420 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
422 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
424 /* crypto utility function */
425 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
427 xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
432 (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
434 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
435 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
437 ((const u32 *)src1)[0] ^ ((const u32 *)
440 ((const u32 *)src1)[1] ^ ((const u32 *)
443 ((const u32 *)src1)[2] ^ ((const u32 *)
446 ((const u32 *)src1)[3] ^ ((const u32 *)
449 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
451 for (k = 0; k < 16; k++)
452 dst[k] = src1[k] ^ src2[k];
458 extern u8 hndcrc8(u8 *p, uint nbytes, u8 crc);
459 extern u16 hndcrc16(u8 *p, uint nbytes, u16 crc);
462 extern int bcm_format_flags(const bcm_bit_desc_t *bd, u32 flags,
464 extern int bcm_format_hex(char *str, const void *bytes, int len);
466 extern char *bcm_chipname(uint chipid, char *buf, uint len);
467 extern void prhex(const char *msg, unsigned char *buf, uint len);
469 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen,
472 extern const char *bcmerrorstr(int bcmerror);
474 /* multi-bool data type: set of bools, mbool is true if any is set */
476 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
477 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
478 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* true if one bool is set */
479 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
481 /* power conversion */
482 extern u16 bcm_qdbm_to_mw(u8 qdbm);
483 extern u8 bcm_mw_to_qdbm(u16 mw);
485 /* generic datastruct to help dump routines */
487 const char *nameandfmt;
492 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
493 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
495 typedef u32(*bcmutl_rdreg_rtn) (void *arg0, uint arg1,
498 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf,
500 extern uint bcm_bitcount(u8 *bitmap, uint bytelength);
502 #endif /* _bcmutils_h_ */