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 ** */
34 #define GPIO_PIN_NOTDEFINED 0x20 /* Pin not defined */
37 * Spin at most 'us' microseconds while 'exp' is true.
38 * Caller should explicitly test 'exp' when this completes
39 * and take appropriate error action if 'exp' is still true.
41 #define SPINWAIT(exp, us) { \
42 uint countdown = (us) + 9; \
43 while ((exp) && (countdown >= 10)) {\
49 /* osl multi-precedence packet queue */
50 #ifndef PKTQ_LEN_DEFAULT
51 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
54 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
58 struct sk_buff *head; /* first packet to dequeue */
59 struct sk_buff *tail; /* last packet to dequeue */
60 u16 len; /* number of queued packets */
61 u16 max; /* maximum number of queued packets */
64 /* multi-priority pkt queue */
66 u16 num_prec; /* number of precedences in use */
67 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
68 u16 max; /* total max packets */
69 u16 len; /* total number of packets */
70 /* q array must be last since # of elements can be either PKTQ_MAX_PREC or 1 */
71 struct pktq_prec q[PKTQ_MAX_PREC];
74 #define PKTQ_PREC_ITER(pq, prec) for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
76 /* fn(pkt, arg). return true if pkt belongs to if */
77 typedef bool(*ifpkt_cb_t) (void *, int);
79 /* operations on a specific precedence in packet queue */
81 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
82 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
83 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
84 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
85 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
87 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
88 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
90 extern struct sk_buff *pktq_penq(struct pktq *pq, int prec,
92 extern struct sk_buff *pktq_penq_head(struct pktq *pq, int prec,
94 extern struct sk_buff *pktq_pdeq(struct pktq *pq, int prec);
95 extern struct sk_buff *pktq_pdeq_tail(struct pktq *pq, int prec);
97 /* packet primitives */
98 extern struct sk_buff *pkt_buf_get_skb(uint len);
99 extern void pkt_buf_free_skb(struct sk_buff *skb);
101 /* Empty the queue at particular precedence level */
103 extern void pktq_pflush(struct pktq *pq, int prec,
106 extern void pktq_pflush(struct pktq *pq, int prec,
107 bool dir, ifpkt_cb_t fn, int arg);
108 #endif /* BRCM_FULLMAC */
110 /* operations on a set of precedences in packet queue */
112 extern int pktq_mlen(struct pktq *pq, uint prec_bmp);
113 extern struct sk_buff *pktq_mdeq(struct pktq *pq, uint prec_bmp, int *prec_out);
115 /* operations on packet queue as a whole */
117 #define pktq_len(pq) ((int)(pq)->len)
118 #define pktq_max(pq) ((int)(pq)->max)
119 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
120 #define pktq_full(pq) ((pq)->len >= (pq)->max)
121 #define pktq_empty(pq) ((pq)->len == 0)
123 /* operations for single precedence queues */
124 #define pktenq(pq, p) pktq_penq(((struct pktq *)pq), 0, (p))
125 #define pktenq_head(pq, p) pktq_penq_head(((struct pktq *)pq), 0, (p))
126 #define pktdeq(pq) pktq_pdeq(((struct pktq *)pq), 0)
127 #define pktdeq_tail(pq) pktq_pdeq_tail(((struct pktq *)pq), 0)
128 #define pktqinit(pq, len) pktq_init(((struct pktq *)pq), 1, len)
130 extern void pktq_init(struct pktq *pq, int num_prec, int max_len);
131 /* prec_out may be NULL if caller is not interested in return value */
132 extern struct sk_buff *pktq_peek_tail(struct pktq *pq, int *prec_out);
134 extern void pktq_flush(struct pktq *pq, bool dir);
136 extern void pktq_flush(struct pktq *pq, bool dir,
137 ifpkt_cb_t fn, int arg);
142 extern uint pktfrombuf(struct sk_buff *p,
143 uint offset, int len, unsigned char *buf);
144 extern uint pkttotlen(struct sk_buff *p);
146 /* ethernet address */
147 extern int bcm_ether_atoe(char *p, u8 *ea);
151 extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
153 /* variable access */
154 extern char *getvar(char *vars, const char *name);
155 extern int getintvar(char *vars, const char *name);
157 extern void prpkt(const char *msg, struct sk_buff *p0);
162 #define bcm_perf_enable()
163 #define bcmstats(fmt)
164 #define bcmlog(fmt, a1, a2)
165 #define bcmdumplog(buf, size) (*buf = '\0')
166 #define bcmdumplogent(buf, idx) -1
168 #define bcmtslog(tstamp, fmt, a1, a2)
169 #define bcmprinttslogs()
170 #define bcmprinttstamp(us)
172 /* Support for sharing code across in-driver iovar implementations.
173 * The intent is that a driver use this structure to map iovar names
174 * to its (private) iovar identifiers, and the lookup function to
175 * find the entry. Macros are provided to map ids and get/set actions
176 * into a single number space for a switch statement.
179 /* iovar structure */
180 typedef struct bcm_iovar {
181 const char *name; /* name for lookup and display */
182 u16 varid; /* id for switch */
183 u16 flags; /* driver-specific flag bits */
184 u16 type; /* base type of argument */
185 u16 minlen; /* min length for buffer vars */
188 /* varid definitions are per-driver, may use these get/set bits */
190 /* IOVar action bits for id mapping */
191 #define IOV_GET 0 /* Get an iovar */
192 #define IOV_SET 1 /* Set an iovar */
194 /* Varid to actionid mapping */
195 #define IOV_GVAL(id) ((id)*2)
196 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
197 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
198 #define IOV_ID(actionid) (actionid >> 1)
200 /* flags are per-driver based on driver attributes */
202 extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table,
204 extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg,
207 /* Base type definitions */
208 #define IOVT_VOID 0 /* no value (implictly set only) */
209 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
210 #define IOVT_INT8 2 /* integer values are range-checked */
211 #define IOVT_UINT8 3 /* unsigned int 8 bits */
212 #define IOVT_INT16 4 /* int 16 bits */
213 #define IOVT_UINT16 5 /* unsigned int 16 bits */
214 #define IOVT_INT32 6 /* int 32 bits */
215 #define IOVT_UINT32 7 /* unsigned int 32 bits */
216 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
217 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
219 /* Initializer for IOV type strings */
220 #define BCM_IOV_TYPE_INIT { \
232 #define BCM_IOVT_IS_INT(type) (\
233 (type == IOVT_BOOL) || \
234 (type == IOVT_INT8) || \
235 (type == IOVT_UINT8) || \
236 (type == IOVT_INT16) || \
237 (type == IOVT_UINT16) || \
238 (type == IOVT_INT32) || \
239 (type == IOVT_UINT32))
241 /* ** driver/apps-shared section ** */
243 #define BCME_STRLEN 64 /* Max string length for BCM errors */
246 #define ABS(a) (((a) < 0) ? -(a) : (a))
249 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
250 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
252 /* map physical to virtual I/O */
253 #if !defined(CONFIG_MMC_MSM7X00A)
254 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
255 (unsigned long)(size))
257 #define REG_MAP(pa, size) (void *)(0)
260 extern u32 g_assert_type;
262 #if defined(BCMDBG_ASSERT)
263 #define ASSERT(exp) \
264 do { if (!(exp)) osl_assert(#exp, __FILE__, __LINE__); } while (0)
265 extern void osl_assert(char *exp, char *file, int line);
267 #define ASSERT(exp) do {} while (0)
268 #endif /* defined(BCMDBG_ASSERT) */
270 /* register access macros */
275 #define OSL_WRITE_REG(r, v) \
276 (bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)))
277 #define OSL_READ_REG(r) \
278 (bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r))))
282 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
283 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
285 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
286 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
289 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
290 #define PKTBUFSZ 2048
292 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
294 #include <linux/kernel.h> /* for vsn/printf's */
295 #include <linux/string.h> /* for mem*, str* */
297 /* bcopy's: Linux kernel doesn't provide these (anymore) */
298 #define bcopy(src, dst, len) memcpy((dst), (src), (len))
300 /* register access macros */
304 SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
305 readb((volatile u8*)(r)) : \
306 sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
307 readl((volatile u32*)(r)), OSL_READ_REG(r)) \
313 __typeof(*(r)) __osl_v; \
314 __asm__ __volatile__("sync"); \
315 switch (sizeof(*(r))) { \
317 __osl_v = readb((volatile u8*)(r)); \
320 __osl_v = readw((volatile u16*)(r)); \
324 readl((volatile u32*)(r)); \
327 __asm__ __volatile__("sync"); \
331 __typeof(*(r)) __osl_v; \
332 __asm__ __volatile__("sync"); \
333 __osl_v = OSL_READ_REG(r); \
334 __asm__ __volatile__("sync"); \
338 #endif /* __mips__ */
340 #define W_REG(r, v) do { \
342 switch (sizeof(*(r))) { \
344 writeb((u8)(v), (volatile u8*)(r)); break; \
346 writew((u16)(v), (volatile u16*)(r)); break; \
348 writel((u32)(v), (volatile u32*)(r)); break; \
350 (OSL_WRITE_REG(r, v))); \
352 #else /* IL_BIGENDIAN */
356 __typeof(*(r)) __osl_v; \
357 switch (sizeof(*(r))) { \
360 readb((volatile u8*)((r)^3)); \
364 readw((volatile u16*)((r)^2)); \
367 __osl_v = readl((volatile u32*)(r)); \
374 #define W_REG(r, v) do { \
376 switch (sizeof(*(r))) { \
379 (volatile u8*)((r)^3)); break; \
382 (volatile u16*)((r)^2)); break; \
385 (volatile u32*)(r)); break; \
387 (OSL_WRITE_REG(r, v))); \
389 #endif /* IL_BIGENDIAN */
391 #define AND_REG(r, v) W_REG((r), R_REG(r) & (v))
392 #define OR_REG(r, v) W_REG((r), R_REG(r) | (v))
394 #define SET_REG(r, mask, val) \
395 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
398 #ifndef NBBY /* the BSD family defines NBBY */
399 #define NBBY 8 /* 8 bits per byte */
400 #endif /* #ifndef NBBY */
401 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
402 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
403 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
404 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
407 #define NBITS(type) (sizeof(type) * 8)
408 #define NBITVAL(nbits) (1 << (nbits))
409 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
410 #define NBITMASK(nbits) MAXBITVAL(nbits)
411 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
413 /* basic mux operation - can be optimized on several architectures */
414 #define MUX(pred, true, false) ((pred) ? (true) : (false))
416 /* modulo inc/dec - assumes x E [0, bound - 1] */
417 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
418 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
420 /* modulo inc/dec, bound = 2^k */
421 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
422 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
424 /* modulo add/sub - assumes x, y E [0, bound - 1] */
425 #define MODADD(x, y, bound) \
426 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
427 #define MODSUB(x, y, bound) \
428 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
430 /* module add/sub, bound = 2^k */
431 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
432 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
435 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
436 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
437 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
438 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
440 /* bcm_format_flags() bit description structure */
441 typedef struct bcm_bit_desc {
446 /* tag_ID/length/value_buffer tuple */
447 typedef struct bcm_tlv {
453 /* Check that bcm_tlv_t fits into the given buflen */
454 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
456 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
458 /* crypto utility function */
459 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
461 xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
466 (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
468 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
469 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
471 ((const u32 *)src1)[0] ^ ((const u32 *)
474 ((const u32 *)src1)[1] ^ ((const u32 *)
477 ((const u32 *)src1)[2] ^ ((const u32 *)
480 ((const u32 *)src1)[3] ^ ((const u32 *)
483 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
485 for (k = 0; k < 16; k++)
486 dst[k] = src1[k] ^ src2[k];
492 extern u8 hndcrc8(u8 *p, uint nbytes, u8 crc);
493 extern u16 hndcrc16(u8 *p, uint nbytes, u16 crc);
496 extern int bcm_format_flags(const bcm_bit_desc_t *bd, u32 flags,
498 extern int bcm_format_hex(char *str, const void *bytes, int len);
500 extern char *bcm_chipname(uint chipid, char *buf, uint len);
501 extern void prhex(const char *msg, unsigned char *buf, uint len);
503 extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen,
506 /* multi-bool data type: set of bools, mbool is true if any is set */
508 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
509 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
510 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* true if one bool is set */
511 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
513 /* power conversion */
514 extern u16 bcm_qdbm_to_mw(u8 qdbm);
515 extern u8 bcm_mw_to_qdbm(u16 mw);
517 extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
518 extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
520 extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf,
522 extern uint bcm_bitcount(u8 *bitmap, uint bytelength);
524 #endif /* _bcmutils_h_ */