staging: brcm80211: moved register read/write macro's
[linux-drm-fsl-dcu.git] / drivers / staging / brcm80211 / include / brcmu_utils.h
1 /*
2  * Copyright (c) 2010 Broadcom Corporation
3  *
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.
7  *
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.
15  */
16
17 #ifndef _brcmutils_h_
18 #define _brcmutils_h_
19
20 /* Buffer structure for collecting string-formatted data
21 * using brcmu_bprintf() API.
22 * Use brcmu_binit() to initialize before use
23 */
24
25         struct brcmu_strbuf {
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 */
30         };
31
32 /* ** driver-only section ** */
33
34 #define GPIO_PIN_NOTDEFINED     0x20    /* Pin not defined */
35
36 /*
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.
40  */
41 #define SPINWAIT(exp, us) { \
42         uint countdown = (us) + 9; \
43         while ((exp) && (countdown >= 10)) {\
44                 udelay(10); \
45                 countdown -= 10; \
46         } \
47 }
48
49 /* osl multi-precedence packet queue */
50 #ifndef PKTQ_LEN_DEFAULT
51 #define PKTQ_LEN_DEFAULT        128     /* Max 128 packets */
52 #endif
53 #ifndef PKTQ_MAX_PREC
54 #define PKTQ_MAX_PREC           16      /* Maximum precedence levels */
55 #endif
56
57         struct pktq_prec {
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 */
62         };
63
64 /* multi-priority pkt queue */
65         struct pktq {
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];
72         };
73
74 #define PKTQ_PREC_ITER(pq, prec)        for (prec = (pq)->num_prec - 1; prec >= 0; prec--)
75
76 /* fn(pkt, arg).  return true if pkt belongs to if */
77 typedef bool(*ifpkt_cb_t) (struct sk_buff *, void *);
78
79 /* operations on a specific precedence in packet queue */
80
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)
86
87 #define pktq_ppeek(pq, prec)            ((pq)->q[prec].head)
88 #define pktq_ppeek_tail(pq, prec)       ((pq)->q[prec].tail)
89
90 extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
91                                  struct sk_buff *p);
92 extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
93                                       struct sk_buff *p);
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);
96
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);
100
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);
104
105 /* operations on a set of precedences in packet queue */
106
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,
109         int *prec_out);
110
111 /* operations on packet queue as a whole */
112
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)
118
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)
126
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);
132
133 /* externs */
134 /* packet */
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);
138
139 /* ethernet address */
140 extern int brcmu_ether_atoe(char *p, u8 *ea);
141
142 /* ip address */
143         struct ipv4_addr;
144
145 #ifdef BCMDBG
146 extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
147 #else
148 #define brcmu_prpkt(a, b)
149 #endif                          /* BCMDBG */
150
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.
156  */
157
158 /* iovar structure */
159 struct brcmu_iovar {
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 */
165 };
166
167 /* varid definitions are per-driver, may use these get/set bits */
168
169 /* IOVar action bits for id mapping */
170 #define IOV_GET 0               /* Get an iovar */
171 #define IOV_SET 1               /* Set an iovar */
172
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)
178
179 extern const struct
180 brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table,
181                                 const char *name);
182 extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg,
183                                 int len, bool set);
184
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)
196
197 /* Initializer for IOV type strings */
198 #define BCM_IOV_TYPE_INIT { \
199         "void", \
200         "bool", \
201         "s8", \
202         "u8", \
203         "s16", \
204         "u16", \
205         "s32", \
206         "u32", \
207         "buffer", \
208         "" }
209
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))
218
219 /* ** driver/apps-shared section ** */
220
221 #define BCME_STRLEN             64      /* Max string length for BCM errors */
222
223 #ifndef ABS
224 #define ABS(a)                  (((a) < 0) ? -(a) : (a))
225 #endif                          /* ABS */
226
227 #define CEIL(x, y)              (((x) + ((y)-1)) / (y))
228 #define ISPOWEROF2(x)           ((((x)-1)&(x)) == 0)
229
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))
234 #else
235 #define REG_MAP(pa, size)       (void *)(0)
236 #endif
237
238 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
239 #define PKTBUFSZ        2048
240
241 #define OSL_SYSUPTIME()         ((u32)jiffies * (1000 / HZ))
242 #ifdef BRCM_FULLMAC
243 #include <linux/kernel.h>       /* for vsn/printf's */
244 #include <linux/string.h>       /* for mem*, str* */
245 #endif
246
247 #ifndef setbit
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)
255 #endif                          /* setbit */
256
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)
262
263 /* basic mux operation - can be optimized on several architectures */
264 #define MUX(pred, true, false) ((pred) ? (true) : (false))
265
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)
269
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))
273
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))
279
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))
283
284 /* crc defines */
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 */
289
290 /* brcmu_format_flags() bit description structure */
291 struct brcmu_bit_desc {
292         u32 bit;
293         const char *name;
294 };
295
296 /* tag_ID/length/value_buffer tuple */
297 struct brcmu_tlv {
298         u8 id;
299         u8 len;
300         u8 data[1];
301 };
302
303 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
304
305 /* crypto utility function */
306 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
307         static inline void
308          xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
309                 if (
310 #ifdef __i386__
311                            1 ||
312 #endif
313                            (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
314                             3) == 0) {
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. */
317                         ((u32 *) dst)[0] =
318                             ((const u32 *)src1)[0] ^ ((const u32 *)
319                                                          src2)[0];
320                         ((u32 *) dst)[1] =
321                             ((const u32 *)src1)[1] ^ ((const u32 *)
322                                                          src2)[1];
323                         ((u32 *) dst)[2] =
324                             ((const u32 *)src1)[2] ^ ((const u32 *)
325                                                          src2)[2];
326                         ((u32 *) dst)[3] =
327                             ((const u32 *)src1)[3] ^ ((const u32 *)
328                                                          src2)[3];
329                 } else {
330                         /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
331                         int k;
332                         for (k = 0; k < 16; k++)
333                                 dst[k] = src1[k] ^ src2[k];
334                 }
335         }
336
337 /* externs */
338 /* crc */
339 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
340
341 /* format/print */
342 #if defined(BCMDBG)
343 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
344                               char *buf, int len);
345 extern int brcmu_format_hex(char *str, const void *bytes, int len);
346 #endif
347
348 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
349
350 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
351                                           uint key);
352
353 /* multi-bool data type: set of bools, mbool is true if any is set */
354         typedef u32 mbool;
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)))
359
360 /* power conversion */
361 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
362 extern u8 brcmu_mw_to_qdbm(u16 mw);
363
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, ...);
366
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);
370
371 #endif                          /* _brcmutils_h_ */