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.
17 #ifndef _BRCMU_UTILS_H_
18 #define _BRCMU_UTILS_H_
20 #include <linux/skbuff.h>
22 /* Buffer structure for collecting string-formatted data
23 * using brcmu_bprintf() API.
24 * Use brcmu_binit() to initialize before use
28 char *buf; /* pointer to current position in origbuf */
29 unsigned int size; /* current (residual) size in bytes */
30 char *origbuf; /* unmodified pointer to orignal buffer */
31 unsigned int origsize; /* unmodified orignal buffer size in bytes */
35 * Spin at most 'us' microseconds while 'exp' is true.
36 * Caller should explicitly test 'exp' when this completes
37 * and take appropriate error action if 'exp' is still true.
39 #define SPINWAIT(exp, us) { \
40 uint countdown = (us) + 9; \
41 while ((exp) && (countdown >= 10)) {\
47 /* osl multi-precedence packet queue */
48 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
49 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
52 struct sk_buff *head; /* first packet to dequeue */
53 struct sk_buff *tail; /* last packet to dequeue */
54 u16 len; /* number of queued packets */
55 u16 max; /* maximum number of queued packets */
58 /* multi-priority pkt queue */
60 u16 num_prec; /* number of precedences in use */
61 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
62 u16 max; /* total max packets */
63 u16 len; /* total number of packets */
65 * q array must be last since # of elements can be either
68 struct pktq_prec q[PKTQ_MAX_PREC];
71 /* operations on a specific precedence in packet queue */
73 #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max))
74 #define pktq_plen(pq, prec) ((pq)->q[prec].len)
75 #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len)
76 #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max)
77 #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0)
79 #define pktq_ppeek(pq, prec) ((pq)->q[prec].head)
80 #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail)
82 extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
84 extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
86 extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
87 extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
89 /* packet primitives */
90 extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
91 extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
93 /* Empty the queue at particular precedence level */
94 /* callback function fn(pkt, arg) returns true if pkt belongs to if */
95 extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
96 bool dir, bool (*fn)(struct sk_buff *, void *), void *arg);
98 /* operations on a set of precedences in packet queue */
100 extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
101 extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
104 /* operations on packet queue as a whole */
106 #define pktq_len(pq) ((int)(pq)->len)
107 #define pktq_max(pq) ((int)(pq)->max)
108 #define pktq_avail(pq) ((int)((pq)->max - (pq)->len))
109 #define pktq_full(pq) ((pq)->len >= (pq)->max)
110 #define pktq_empty(pq) ((pq)->len == 0)
112 /* operations for single precedence queues */
113 #define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p))
114 #define pktenq_head(pq, p)\
115 brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p))
116 #define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0)
117 #define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0)
118 #define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len)
120 extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
121 /* prec_out may be NULL if caller is not interested in return value */
122 extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
123 extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
124 bool (*fn)(struct sk_buff *, void *), void *arg);
128 extern uint brcmu_pktfrombuf(struct sk_buff *p,
129 uint offset, int len, unsigned char *buf);
130 extern uint brcmu_pkttotlen(struct sk_buff *p);
136 extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
138 #define brcmu_prpkt(a, b)
141 /* Support for sharing code across in-driver iovar implementations.
142 * The intent is that a driver use this structure to map iovar names
143 * to its (private) iovar identifiers, and the lookup function to
144 * find the entry. Macros are provided to map ids and get/set actions
145 * into a single number space for a switch statement.
148 /* iovar structure */
150 const char *name; /* name for lookup and display */
151 u16 varid; /* id for switch */
152 u16 flags; /* driver-specific flag bits */
153 u16 type; /* base type of argument */
154 u16 minlen; /* min length for buffer vars */
157 /* varid definitions are per-driver, may use these get/set bits */
159 /* IOVar action bits for id mapping */
160 #define IOV_GET 0 /* Get an iovar */
161 #define IOV_SET 1 /* Set an iovar */
163 /* Varid to actionid mapping */
164 #define IOV_GVAL(id) ((id)*2)
165 #define IOV_SVAL(id) (((id)*2)+IOV_SET)
166 #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET)
167 #define IOV_ID(actionid) (actionid >> 1)
170 brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
172 extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
175 /* Base type definitions */
176 #define IOVT_VOID 0 /* no value (implictly set only) */
177 #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */
178 #define IOVT_INT8 2 /* integer values are range-checked */
179 #define IOVT_UINT8 3 /* unsigned int 8 bits */
180 #define IOVT_INT16 4 /* int 16 bits */
181 #define IOVT_UINT16 5 /* unsigned int 16 bits */
182 #define IOVT_INT32 6 /* int 32 bits */
183 #define IOVT_UINT32 7 /* unsigned int 32 bits */
184 #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */
185 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
187 /* ** driver/apps-shared section ** */
189 #define BCME_STRLEN 64 /* Max string length for BCM errors */
191 #define ABS(a) (((a) < 0) ? -(a) : (a))
193 #define CEIL(x, y) (((x) + ((y)-1)) / (y))
194 #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0)
196 /* map physical to virtual I/O */
197 #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \
198 (unsigned long)(size))
200 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
201 #define PKTBUFSZ 2048
203 #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ))
206 #ifndef NBBY /* the BSD family defines NBBY */
207 #define NBBY 8 /* 8 bits per byte */
208 #endif /* #ifndef NBBY */
209 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
210 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
211 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
212 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
215 #define NBITS(type) (sizeof(type) * 8)
216 #define NBITVAL(nbits) (1 << (nbits))
217 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
218 #define NBITMASK(nbits) MAXBITVAL(nbits)
219 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
221 /* basic mux operation - can be optimized on several architectures */
222 #define MUX(pred, true, false) ((pred) ? (true) : (false))
224 /* modulo inc/dec - assumes x E [0, bound - 1] */
225 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
226 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
228 /* modulo inc/dec, bound = 2^k */
229 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
230 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
232 /* modulo add/sub - assumes x, y E [0, bound - 1] */
233 #define MODADD(x, y, bound) \
234 MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
235 #define MODSUB(x, y, bound) \
236 MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
238 /* module add/sub, bound = 2^k */
239 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
240 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
243 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
244 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
246 /* brcmu_format_flags() bit description structure */
247 struct brcmu_bit_desc {
252 /* tag_ID/length/value_buffer tuple */
259 /* 18-bytes of Ethernet address buffer length */
260 #define ETHER_ADDR_STR_LEN 18
265 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
267 extern int brcmu_format_hex(char *str, const void *bytes, int len);
270 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
272 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
275 /* power conversion */
276 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
277 extern u8 brcmu_mw_to_qdbm(u16 mw);
279 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
280 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
282 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
283 char *buf, uint len);
284 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
286 #endif /* _BRCMU_UTILS_H_ */