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 brcmu_bprintf() API.
22 * Use brcmu_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) (struct sk_buff *, void *);
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 *brcmu_pktq_penq(struct pktq *pq, int prec,
92 extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
94 extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
95 extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
97 /* packet primitives */
98 extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
99 extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
101 /* Empty the queue at particular precedence level */
102 extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
103 bool dir, ifpkt_cb_t fn, void *arg);
105 /* operations on a set of precedences in packet queue */
107 extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
108 extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
111 /* operations on packet queue as a whole */
113 #define pktq_len(pq) ((int)(pq)->len)
114 #define pktq_max(pq) ((int)(pq)->max)
115 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
116 #define pktq_full(pq) ((pq)->len >= (pq)->max)
117 #define pktq_empty(pq) ((pq)->len == 0)
119 /* operations for single precedence queues */
120 #define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p))
121 #define pktenq_head(pq, p)\
122 brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p))
123 #define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0)
124 #define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0)
125 #define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len)
127 extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
128 /* prec_out may be NULL if caller is not interested in return value */
129 extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
130 extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
131 ifpkt_cb_t fn, void *arg);
135 extern uint brcmu_pktfrombuf(struct sk_buff *p,
136 uint offset, int len, unsigned char *buf);
137 extern uint brcmu_pkttotlen(struct sk_buff *p);
139 /* ethernet address */
140 extern int brcmu_ether_atoe(char *p, u8 *ea);
146 extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
148 #define brcmu_prpkt(a, b)
151 /* Support for sharing code across in-driver iovar implementations.
152 * The intent is that a driver use this structure to map iovar names
153 * to its (private) iovar identifiers, and the lookup function to
154 * find the entry. Macros are provided to map ids and get/set actions
155 * into a single number space for a switch statement.
158 /* iovar structure */
160 const char *name; /* name for lookup and display */
161 u16 varid; /* id for switch */
162 u16 flags; /* driver-specific flag bits */
163 u16 type; /* base type of argument */
164 u16 minlen; /* min length for buffer vars */
167 /* varid definitions are per-driver, may use these get/set bits */
169 /* IOVar action bits for id mapping */
170 #define IOV_GET 0 /* Get an iovar */
171 #define IOV_SET 1 /* Set an iovar */
173 /* Varid to actionid mapping */
174 #define IOV_GVAL(id) ((id)*2)
175 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
176 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
177 #define IOV_ID(actionid) (actionid >> 1)
180 brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
182 extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
185 /* Base type definitions */
186 #define IOVT_VOID 0 /* no value (implictly set only) */
187 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
188 #define IOVT_INT8 2 /* integer values are range-checked */
189 #define IOVT_UINT8 3 /* unsigned int 8 bits */
190 #define IOVT_INT16 4 /* int 16 bits */
191 #define IOVT_UINT16 5 /* unsigned int 16 bits */
192 #define IOVT_INT32 6 /* int 32 bits */
193 #define IOVT_UINT32 7 /* unsigned int 32 bits */
194 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
195 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
197 /* Initializer for IOV type strings */
198 #define BCM_IOV_TYPE_INIT { \
210 #define BCM_IOVT_IS_INT(type) (\
211 (type == IOVT_BOOL) || \
212 (type == IOVT_INT8) || \
213 (type == IOVT_UINT8) || \
214 (type == IOVT_INT16) || \
215 (type == IOVT_UINT16) || \
216 (type == IOVT_INT32) || \
217 (type == IOVT_UINT32))
219 /* ** driver/apps-shared section ** */
221 #define BCME_STRLEN 64 /* Max string length for BCM errors */
224 #define ABS(a) (((a) < 0) ? -(a) : (a))
227 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
228 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
230 /* map physical to virtual I/O */
231 #if !defined(CONFIG_MMC_MSM7X00A)
232 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
233 (unsigned long)(size))
235 #define REG_MAP(pa, size) (void *)(0)
238 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
239 #define PKTBUFSZ 2048
241 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
243 #include <linux/kernel.h> /* for vsn/printf's */
244 #include <linux/string.h> /* for mem*, str* */
248 #ifndef NBBY /* the BSD family defines NBBY */
249 #define NBBY 8 /* 8 bits per byte */
250 #endif /* #ifndef NBBY */
251 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
252 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
253 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
254 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
257 #define NBITS(type) (sizeof(type) * 8)
258 #define NBITVAL(nbits) (1 << (nbits))
259 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
260 #define NBITMASK(nbits) MAXBITVAL(nbits)
261 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
263 /* basic mux operation - can be optimized on several architectures */
264 #define MUX(pred, true, false) ((pred) ? (true) : (false))
266 /* modulo inc/dec - assumes x E [0, bound - 1] */
267 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
268 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
270 /* modulo inc/dec, bound = 2^k */
271 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
272 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
274 /* modulo add/sub - assumes x, y E [0, bound - 1] */
275 #define MODADD(x, y, bound) \
276 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
277 #define MODSUB(x, y, bound) \
278 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
280 /* module add/sub, bound = 2^k */
281 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
282 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
285 #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */
286 #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */
287 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
288 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
290 /* brcmu_format_flags() bit description structure */
291 struct brcmu_bit_desc {
296 /* tag_ID/length/value_buffer tuple */
303 #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */
305 /* crypto utility function */
306 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
308 xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
313 (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
315 /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
316 /* x86 supports unaligned. This version runs 6x-9x faster on x86. */
318 ((const u32 *)src1)[0] ^ ((const u32 *)
321 ((const u32 *)src1)[1] ^ ((const u32 *)
324 ((const u32 *)src1)[2] ^ ((const u32 *)
327 ((const u32 *)src1)[3] ^ ((const u32 *)
330 /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
332 for (k = 0; k < 16; k++)
333 dst[k] = src1[k] ^ src2[k];
339 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
343 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
345 extern int brcmu_format_hex(char *str, const void *bytes, int len);
348 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
350 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
353 /* multi-bool data type: set of bools, mbool is true if any is set */
355 #define mboolset(mb, bit) ((mb) |= (bit)) /* set one bool */
356 #define mboolclr(mb, bit) ((mb) &= ~(bit)) /* clear one bool */
357 #define mboolisset(mb, bit) (((mb) & (bit)) != 0) /* true if one bool is set */
358 #define mboolmaskset(mb, mask, val) ((mb) = (((mb) & ~(mask)) | (val)))
360 /* power conversion */
361 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
362 extern u8 brcmu_mw_to_qdbm(u16 mw);
364 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
365 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
367 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
368 char *buf, uint len);
369 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
371 #endif /* _brcmutils_h_ */