block: fix warning with calling smp_processor_id() in preemptible section
[pandora-kernel.git] / drivers / staging / brcm80211 / include / bcmutils.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 _bcmutils_h_
18 #define _bcmutils_h_
19
20 /* Buffer structure for collecting string-formatted data
21 * using bcm_bprintf() API.
22 * Use bcm_binit() to initialize before use
23 */
24
25         struct bcmstrbuf {
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 *bcm_pktq_penq(struct pktq *pq, int prec,
91                                  struct sk_buff *p);
92 extern struct sk_buff *bcm_pktq_penq_head(struct pktq *pq, int prec,
93                                       struct sk_buff *p);
94 extern struct sk_buff *bcm_pktq_pdeq(struct pktq *pq, int prec);
95 extern struct sk_buff *bcm_pktq_pdeq_tail(struct pktq *pq, int prec);
96
97 /* packet primitives */
98 extern struct sk_buff *bcm_pkt_buf_get_skb(uint len);
99 extern void bcm_pkt_buf_free_skb(struct sk_buff *skb);
100
101 /* Empty the queue at particular precedence level */
102 extern void bcm_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 bcm_pktq_mlen(struct pktq *pq, uint prec_bmp);
108 extern struct sk_buff *bcm_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)           bcm_pktq_penq(((struct pktq *)pq), 0, (p))
121 #define pktenq_head(pq, p)      bcm_pktq_penq_head(((struct pktq *)pq), 0, (p))
122 #define pktdeq(pq)              bcm_pktq_pdeq(((struct pktq *)pq), 0)
123 #define pktdeq_tail(pq)         bcm_pktq_pdeq_tail(((struct pktq *)pq), 0)
124 #define pktqinit(pq, len)       bcm_pktq_init(((struct pktq *)pq), 1, len)
125
126 extern void bcm_pktq_init(struct pktq *pq, int num_prec, int max_len);
127 /* prec_out may be NULL if caller is not interested in return value */
128 extern struct sk_buff *bcm_pktq_peek_tail(struct pktq *pq, int *prec_out);
129 extern void bcm_pktq_flush(struct pktq *pq, bool dir,
130         ifpkt_cb_t fn, void *arg);
131
132 /* externs */
133 /* packet */
134 extern uint bcm_pktfrombuf(struct sk_buff *p,
135         uint offset, int len, unsigned char *buf);
136 extern uint bcm_pkttotlen(struct sk_buff *p);
137
138 /* ethernet address */
139 extern int bcm_ether_atoe(char *p, u8 *ea);
140
141 /* ip address */
142         struct ipv4_addr;
143         extern char *bcm_ip_ntoa(struct ipv4_addr *ia, char *buf);
144
145 #ifdef BCMDBG
146 extern void bcm_prpkt(const char *msg, struct sk_buff *p0);
147 #else
148 #define bcm_prpkt(a, b)
149 #endif                          /* BCMDBG */
150
151 #define bcm_perf_enable()
152 #define bcmlog(fmt, a1, a2)
153 #define bcmdumplog(buf, size)   (*buf = '\0')
154 #define bcmdumplogent(buf, idx) -1
155
156 #define bcmtslog(tstamp, fmt, a1, a2)
157 #define bcmprinttslogs()
158 #define bcmprinttstamp(us)
159
160 /* Support for sharing code across in-driver iovar implementations.
161  * The intent is that a driver use this structure to map iovar names
162  * to its (private) iovar identifiers, and the lookup function to
163  * find the entry.  Macros are provided to map ids and get/set actions
164  * into a single number space for a switch statement.
165  */
166
167 /* iovar structure */
168         typedef struct bcm_iovar {
169                 const char *name;       /* name for lookup and display */
170                 u16 varid;      /* id for switch */
171                 u16 flags;      /* driver-specific flag bits */
172                 u16 type;       /* base type of argument */
173                 u16 minlen;     /* min length for buffer vars */
174         } bcm_iovar_t;
175
176 /* varid definitions are per-driver, may use these get/set bits */
177
178 /* IOVar action bits for id mapping */
179 #define IOV_GET 0               /* Get an iovar */
180 #define IOV_SET 1               /* Set an iovar */
181
182 /* Varid to actionid mapping */
183 #define IOV_GVAL(id)            ((id)*2)
184 #define IOV_SVAL(id)            (((id)*2)+IOV_SET)
185 #define IOV_ISSET(actionid)     ((actionid & IOV_SET) == IOV_SET)
186 #define IOV_ID(actionid)        (actionid >> 1)
187
188 /* flags are per-driver based on driver attributes */
189
190         extern const bcm_iovar_t *bcm_iovar_lookup(const bcm_iovar_t *table,
191                                                    const char *name);
192         extern int bcm_iovar_lencheck(const bcm_iovar_t *table, void *arg,
193                                       int len, bool set);
194
195 /* Base type definitions */
196 #define IOVT_VOID       0       /* no value (implictly set only) */
197 #define IOVT_BOOL       1       /* any value ok (zero/nonzero) */
198 #define IOVT_INT8       2       /* integer values are range-checked */
199 #define IOVT_UINT8      3       /* unsigned int 8 bits */
200 #define IOVT_INT16      4       /* int 16 bits */
201 #define IOVT_UINT16     5       /* unsigned int 16 bits */
202 #define IOVT_INT32      6       /* int 32 bits */
203 #define IOVT_UINT32     7       /* unsigned int 32 bits */
204 #define IOVT_BUFFER     8       /* buffer is size-checked as per minlen */
205 #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER)
206
207 /* Initializer for IOV type strings */
208 #define BCM_IOV_TYPE_INIT { \
209         "void", \
210         "bool", \
211         "s8", \
212         "u8", \
213         "s16", \
214         "u16", \
215         "s32", \
216         "u32", \
217         "buffer", \
218         "" }
219
220 #define BCM_IOVT_IS_INT(type) (\
221         (type == IOVT_BOOL) || \
222         (type == IOVT_INT8) || \
223         (type == IOVT_UINT8) || \
224         (type == IOVT_INT16) || \
225         (type == IOVT_UINT16) || \
226         (type == IOVT_INT32) || \
227         (type == IOVT_UINT32))
228
229 /* ** driver/apps-shared section ** */
230
231 #define BCME_STRLEN             64      /* Max string length for BCM errors */
232
233 #ifndef ABS
234 #define ABS(a)                  (((a) < 0) ? -(a) : (a))
235 #endif                          /* ABS */
236
237 #define CEIL(x, y)              (((x) + ((y)-1)) / (y))
238 #define ISPOWEROF2(x)           ((((x)-1)&(x)) == 0)
239
240 /* map physical to virtual I/O */
241 #if !defined(CONFIG_MMC_MSM7X00A)
242 #define REG_MAP(pa, size)       ioremap_nocache((unsigned long)(pa), \
243                                         (unsigned long)(size))
244 #else
245 #define REG_MAP(pa, size)       (void *)(0)
246 #endif
247
248 /* register access macros */
249 #if defined(BCMSDIO)
250 #ifdef BRCM_FULLMAC
251 #include <bcmsdh.h>
252 #endif
253 #define OSL_WRITE_REG(r, v) \
254                 (bcmsdh_reg_write(NULL, (unsigned long)(r), sizeof(*(r)), (v)))
255 #define OSL_READ_REG(r) \
256                 (bcmsdh_reg_read(NULL, (unsigned long)(r), sizeof(*(r))))
257 #endif
258
259 #if defined(BCMSDIO)
260 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
261 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
262 #else
263 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
264 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
265 #endif
266
267 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
268 #define PKTBUFSZ        2048
269
270 #define OSL_SYSUPTIME()         ((u32)jiffies * (1000 / HZ))
271 #ifdef BRCM_FULLMAC
272 #include <linux/kernel.h>       /* for vsn/printf's */
273 #include <linux/string.h>       /* for mem*, str* */
274 #endif
275 /* bcopy's: Linux kernel doesn't provide these (anymore) */
276 #define bcopy(src, dst, len)    memcpy((dst), (src), (len))
277
278 /* register access macros */
279 #ifndef __BIG_ENDIAN
280 #ifndef __mips__
281 #define R_REG(r) (\
282         SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
283         readb((volatile u8*)(r)) : \
284         sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
285         readl((volatile u32*)(r)), OSL_READ_REG(r)) \
286 )
287 #else                           /* __mips__ */
288 #define R_REG(r) (\
289         SELECT_BUS_READ( \
290                 ({ \
291                         __typeof(*(r)) __osl_v; \
292                         __asm__ __volatile__("sync"); \
293                         switch (sizeof(*(r))) { \
294                         case sizeof(u8): \
295                                 __osl_v = readb((volatile u8*)(r)); \
296                                 break; \
297                         case sizeof(u16): \
298                                 __osl_v = readw((volatile u16*)(r)); \
299                                 break; \
300                         case sizeof(u32): \
301                                 __osl_v = \
302                                 readl((volatile u32*)(r)); \
303                                 break; \
304                         } \
305                         __asm__ __volatile__("sync"); \
306                         __osl_v; \
307                 }), \
308                 ({ \
309                         __typeof(*(r)) __osl_v; \
310                         __asm__ __volatile__("sync"); \
311                         __osl_v = OSL_READ_REG(r); \
312                         __asm__ __volatile__("sync"); \
313                         __osl_v; \
314                 })) \
315 )
316 #endif                          /* __mips__ */
317
318 #define W_REG(r, v) do { \
319         SELECT_BUS_WRITE( \
320                 switch (sizeof(*(r))) { \
321                 case sizeof(u8): \
322                         writeb((u8)(v), (volatile u8*)(r)); break; \
323                 case sizeof(u16): \
324                         writew((u16)(v), (volatile u16*)(r)); break; \
325                 case sizeof(u32): \
326                         writel((u32)(v), (volatile u32*)(r)); break; \
327                 }, \
328                 (OSL_WRITE_REG(r, v))); \
329         } while (0)
330 #else                           /* __BIG_ENDIAN */
331 #define R_REG(r) (\
332         SELECT_BUS_READ( \
333                 ({ \
334                         __typeof(*(r)) __osl_v; \
335                         switch (sizeof(*(r))) { \
336                         case sizeof(u8): \
337                                 __osl_v = \
338                                 readb((volatile u8*)((r)^3)); \
339                                 break; \
340                         case sizeof(u16): \
341                                 __osl_v = \
342                                 readw((volatile u16*)((r)^2)); \
343                                 break; \
344                         case sizeof(u32): \
345                                 __osl_v = readl((volatile u32*)(r)); \
346                                 break; \
347                         } \
348                         __osl_v; \
349                 }), \
350                 OSL_READ_REG(r)) \
351 )
352 #define W_REG(r, v) do { \
353         SELECT_BUS_WRITE( \
354                 switch (sizeof(*(r))) { \
355                 case sizeof(u8):        \
356                         writeb((u8)(v), \
357                         (volatile u8*)((r)^3)); break; \
358                 case sizeof(u16):       \
359                         writew((u16)(v), \
360                         (volatile u16*)((r)^2)); break; \
361                 case sizeof(u32):       \
362                         writel((u32)(v), \
363                         (volatile u32*)(r)); break; \
364                 }, \
365                 (OSL_WRITE_REG(r, v))); \
366         } while (0)
367 #endif                          /* __BIG_ENDIAN */
368
369 #define AND_REG(r, v)   W_REG((r), R_REG(r) & (v))
370 #define OR_REG(r, v)    W_REG((r), R_REG(r) | (v))
371
372 #define SET_REG(r, mask, val) \
373                 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
374
375 #ifndef setbit
376 #ifndef NBBY                    /* the BSD family defines NBBY */
377 #define NBBY    8               /* 8 bits per byte */
378 #endif                          /* #ifndef NBBY */
379 #define setbit(a, i)    (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
380 #define clrbit(a, i)    (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
381 #define isset(a, i)     (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
382 #define isclr(a, i)     ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
383 #endif                          /* setbit */
384
385 #define NBITS(type)     (sizeof(type) * 8)
386 #define NBITVAL(nbits)  (1 << (nbits))
387 #define MAXBITVAL(nbits)        ((1 << (nbits)) - 1)
388 #define NBITMASK(nbits) MAXBITVAL(nbits)
389 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
390
391 /* basic mux operation - can be optimized on several architectures */
392 #define MUX(pred, true, false) ((pred) ? (true) : (false))
393
394 /* modulo inc/dec - assumes x E [0, bound - 1] */
395 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
396 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
397
398 /* modulo inc/dec, bound = 2^k */
399 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
400 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
401
402 /* modulo add/sub - assumes x, y E [0, bound - 1] */
403 #define MODADD(x, y, bound) \
404     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
405 #define MODSUB(x, y, bound) \
406     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
407
408 /* module add/sub, bound = 2^k */
409 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
410 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
411
412 /* crc defines */
413 #define CRC8_INIT_VALUE  0xff   /* Initial CRC8 checksum value */
414 #define CRC8_GOOD_VALUE  0x9f   /* Good final CRC8 checksum value */
415 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
416 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
417
418 /* bcm_format_flags() bit description structure */
419         typedef struct bcm_bit_desc {
420                 u32 bit;
421                 const char *name;
422         } bcm_bit_desc_t;
423
424 /* tag_ID/length/value_buffer tuple */
425         typedef struct bcm_tlv {
426                 u8 id;
427                 u8 len;
428                 u8 data[1];
429         } bcm_tlv_t;
430
431 /* Check that bcm_tlv_t fits into the given buflen */
432 #define bcm_valid_tlv(elt, buflen) ((buflen) >= 2 && (int)(buflen) >= (int)(2 + (elt)->len))
433
434 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
435
436 /* crypto utility function */
437 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
438         static inline void
439          xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
440                 if (
441 #ifdef __i386__
442                            1 ||
443 #endif
444                            (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
445                             3) == 0) {
446                         /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
447                         /* x86 supports unaligned.  This version runs 6x-9x faster on x86. */
448                         ((u32 *) dst)[0] =
449                             ((const u32 *)src1)[0] ^ ((const u32 *)
450                                                          src2)[0];
451                         ((u32 *) dst)[1] =
452                             ((const u32 *)src1)[1] ^ ((const u32 *)
453                                                          src2)[1];
454                         ((u32 *) dst)[2] =
455                             ((const u32 *)src1)[2] ^ ((const u32 *)
456                                                          src2)[2];
457                         ((u32 *) dst)[3] =
458                             ((const u32 *)src1)[3] ^ ((const u32 *)
459                                                          src2)[3];
460                 } else {
461                         /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
462                         int k;
463                         for (k = 0; k < 16; k++)
464                                 dst[k] = src1[k] ^ src2[k];
465                 }
466         }
467
468 /* externs */
469 /* crc */
470 extern u8 bcm_crc8(u8 *p, uint nbytes, u8 crc);
471 /* format/print */
472 #if defined(BCMDBG)
473         extern int bcm_format_flags(const bcm_bit_desc_t *bd, u32 flags,
474                                     char *buf, int len);
475         extern int bcm_format_hex(char *str, const void *bytes, int len);
476 #endif
477         extern char *bcm_chipname(uint chipid, char *buf, uint len);
478
479         extern bcm_tlv_t *bcm_parse_tlvs(void *buf, int buflen,
480                                                     uint key);
481
482 /* multi-bool data type: set of bools, mbool is true if any is set */
483         typedef u32 mbool;
484 #define mboolset(mb, bit)               ((mb) |= (bit)) /* set one bool */
485 #define mboolclr(mb, bit)               ((mb) &= ~(bit))        /* clear one bool */
486 #define mboolisset(mb, bit)             (((mb) & (bit)) != 0)   /* true if one bool is set */
487 #define mboolmaskset(mb, mask, val)     ((mb) = (((mb) & ~(mask)) | (val)))
488
489 /* power conversion */
490         extern u16 bcm_qdbm_to_mw(u8 qdbm);
491         extern u8 bcm_mw_to_qdbm(u16 mw);
492
493         extern void bcm_binit(struct bcmstrbuf *b, char *buf, uint size);
494         extern int bcm_bprintf(struct bcmstrbuf *b, const char *fmt, ...);
495
496         extern uint bcm_mkiovar(char *name, char *data, uint datalen, char *buf,
497                                 uint len);
498         extern uint bcm_bitcount(u8 *bitmap, uint bytelength);
499
500 #endif                          /* _bcmutils_h_ */