260d0b6c35189f9ecb8342931cfc5a2daf54dd99
[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 /* register access macros */
239 #if defined(BCMSDIO)
240 #ifdef BRCM_FULLMAC
241 #include <bcmsdh.h>
242 #endif
243 #endif
244
245 #if defined(BCMSDIO)
246 #define SELECT_BUS_WRITE(mmap_op, bus_op) bus_op
247 #define SELECT_BUS_READ(mmap_op, bus_op) bus_op
248 #else
249 #define SELECT_BUS_WRITE(mmap_op, bus_op) mmap_op
250 #define SELECT_BUS_READ(mmap_op, bus_op) mmap_op
251 #endif
252
253 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
254 #define PKTBUFSZ        2048
255
256 #define OSL_SYSUPTIME()         ((u32)jiffies * (1000 / HZ))
257 #ifdef BRCM_FULLMAC
258 #include <linux/kernel.h>       /* for vsn/printf's */
259 #include <linux/string.h>       /* for mem*, str* */
260 #endif
261
262 /* register access macros */
263 #ifndef __BIG_ENDIAN
264 #ifndef __mips__
265 #define R_REG(r) (\
266         SELECT_BUS_READ(sizeof(*(r)) == sizeof(u8) ? \
267         readb((volatile u8*)(r)) : \
268         sizeof(*(r)) == sizeof(u16) ? readw((volatile u16*)(r)) : \
269         readl((volatile u32*)(r)), bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r))) \
270 )
271 #else                           /* __mips__ */
272 #define R_REG(r) (\
273         SELECT_BUS_READ( \
274                 ({ \
275                         __typeof(*(r)) __osl_v; \
276                         __asm__ __volatile__("sync"); \
277                         switch (sizeof(*(r))) { \
278                         case sizeof(u8): \
279                                 __osl_v = readb((volatile u8*)(r)); \
280                                 break; \
281                         case sizeof(u16): \
282                                 __osl_v = readw((volatile u16*)(r)); \
283                                 break; \
284                         case sizeof(u32): \
285                                 __osl_v = \
286                                 readl((volatile u32*)(r)); \
287                                 break; \
288                         } \
289                         __asm__ __volatile__("sync"); \
290                         __osl_v; \
291                 }), \
292                 ({ \
293                         __typeof(*(r)) __osl_v; \
294                         __asm__ __volatile__("sync"); \
295                         __osl_v = bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r)); \
296                         __asm__ __volatile__("sync"); \
297                         __osl_v; \
298                 })) \
299 )
300 #endif                          /* __mips__ */
301
302 #define W_REG(r, v) do { \
303         SELECT_BUS_WRITE( \
304                 switch (sizeof(*(r))) { \
305                 case sizeof(u8): \
306                         writeb((u8)(v), (volatile u8*)(r)); break; \
307                 case sizeof(u16): \
308                         writew((u16)(v), (volatile u16*)(r)); break; \
309                 case sizeof(u32): \
310                         writel((u32)(v), (volatile u32*)(r)); break; \
311                 }, \
312                 bcmsdh_reg_write(NULL, (unsigned long)r, sizeof(*r), (v))); \
313         } while (0)
314 #else                           /* __BIG_ENDIAN */
315 #define R_REG(r) (\
316         SELECT_BUS_READ( \
317                 ({ \
318                         __typeof(*(r)) __osl_v; \
319                         switch (sizeof(*(r))) { \
320                         case sizeof(u8): \
321                                 __osl_v = \
322                                 readb((volatile u8*)((r)^3)); \
323                                 break; \
324                         case sizeof(u16): \
325                                 __osl_v = \
326                                 readw((volatile u16*)((r)^2)); \
327                                 break; \
328                         case sizeof(u32): \
329                                 __osl_v = readl((volatile u32*)(r)); \
330                                 break; \
331                         } \
332                         __osl_v; \
333                 }), \
334                 bcmsdh_reg_read(NULL, (unsigned long)r, sizeof(*r))) \
335 )
336 #define W_REG(r, v) do { \
337         SELECT_BUS_WRITE( \
338                 switch (sizeof(*(r))) { \
339                 case sizeof(u8):        \
340                         writeb((u8)(v), \
341                         (volatile u8*)((r)^3)); break; \
342                 case sizeof(u16):       \
343                         writew((u16)(v), \
344                         (volatile u16*)((r)^2)); break; \
345                 case sizeof(u32):       \
346                         writel((u32)(v), \
347                         (volatile u32*)(r)); break; \
348                 }, \
349                 bcmsdh_reg_write(NULL, (unsigned long)r, sizeof(*r), v)); \
350         } while (0)
351 #endif                          /* __BIG_ENDIAN */
352
353 #ifdef __mips__
354 /*
355  * bcm4716 (which includes 4717 & 4718), plus 4706 on PCIe can reorder
356  * transactions. As a fix, a read after write is performed on certain places
357  * in the code. Older chips and the newer 5357 family don't require this fix.
358  */
359 #define W_REG_FLUSH(r, v)       ({ W_REG((r), (v)); (void)R_REG(r); })
360 #else
361 #define W_REG_FLUSH(r, v)       W_REG((r), (v))
362 #endif                          /* __mips__ */
363
364 #define AND_REG(r, v)   W_REG((r), R_REG(r) & (v))
365 #define OR_REG(r, v)    W_REG((r), R_REG(r) | (v))
366
367 #define SET_REG(r, mask, val) \
368                 W_REG((r), ((R_REG(r) & ~(mask)) | (val)))
369
370 #ifndef setbit
371 #ifndef NBBY                    /* the BSD family defines NBBY */
372 #define NBBY    8               /* 8 bits per byte */
373 #endif                          /* #ifndef NBBY */
374 #define setbit(a, i)    (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
375 #define clrbit(a, i)    (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
376 #define isset(a, i)     (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
377 #define isclr(a, i)     ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
378 #endif                          /* setbit */
379
380 #define NBITS(type)     (sizeof(type) * 8)
381 #define NBITVAL(nbits)  (1 << (nbits))
382 #define MAXBITVAL(nbits)        ((1 << (nbits)) - 1)
383 #define NBITMASK(nbits) MAXBITVAL(nbits)
384 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
385
386 /* basic mux operation - can be optimized on several architectures */
387 #define MUX(pred, true, false) ((pred) ? (true) : (false))
388
389 /* modulo inc/dec - assumes x E [0, bound - 1] */
390 #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1)
391 #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1)
392
393 /* modulo inc/dec, bound = 2^k */
394 #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1))
395 #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1))
396
397 /* modulo add/sub - assumes x, y E [0, bound - 1] */
398 #define MODADD(x, y, bound) \
399     MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y))
400 #define MODSUB(x, y, bound) \
401     MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y))
402
403 /* module add/sub, bound = 2^k */
404 #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1))
405 #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1))
406
407 /* crc defines */
408 #define CRC8_INIT_VALUE  0xff   /* Initial CRC8 checksum value */
409 #define CRC8_GOOD_VALUE  0x9f   /* Good final CRC8 checksum value */
410 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
411 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
412
413 /* brcmu_format_flags() bit description structure */
414 struct brcmu_bit_desc {
415         u32 bit;
416         const char *name;
417 };
418
419 /* tag_ID/length/value_buffer tuple */
420 struct brcmu_tlv {
421         u8 id;
422         u8 len;
423         u8 data[1];
424 };
425
426 #define ETHER_ADDR_STR_LEN      18      /* 18-bytes of Ethernet address buffer length */
427
428 /* crypto utility function */
429 /* 128-bit xor: *dst = *src1 xor *src2. dst1, src1 and src2 may have any alignment */
430         static inline void
431          xor_128bit_block(const u8 *src1, const u8 *src2, u8 *dst) {
432                 if (
433 #ifdef __i386__
434                            1 ||
435 #endif
436                            (((unsigned long) src1 | (unsigned long) src2 | (unsigned long) dst) &
437                             3) == 0) {
438                         /* ARM CM3 rel time: 1229 (727 if alignment check could be omitted) */
439                         /* x86 supports unaligned.  This version runs 6x-9x faster on x86. */
440                         ((u32 *) dst)[0] =
441                             ((const u32 *)src1)[0] ^ ((const u32 *)
442                                                          src2)[0];
443                         ((u32 *) dst)[1] =
444                             ((const u32 *)src1)[1] ^ ((const u32 *)
445                                                          src2)[1];
446                         ((u32 *) dst)[2] =
447                             ((const u32 *)src1)[2] ^ ((const u32 *)
448                                                          src2)[2];
449                         ((u32 *) dst)[3] =
450                             ((const u32 *)src1)[3] ^ ((const u32 *)
451                                                          src2)[3];
452                 } else {
453                         /* ARM CM3 rel time: 4668 (4191 if alignment check could be omitted) */
454                         int k;
455                         for (k = 0; k < 16; k++)
456                                 dst[k] = src1[k] ^ src2[k];
457                 }
458         }
459
460 /* externs */
461 /* crc */
462 extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc);
463
464 /* format/print */
465 #if defined(BCMDBG)
466 extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags,
467                               char *buf, int len);
468 extern int brcmu_format_hex(char *str, const void *bytes, int len);
469 #endif
470
471 extern char *brcmu_chipname(uint chipid, char *buf, uint len);
472
473 extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen,
474                                           uint key);
475
476 /* multi-bool data type: set of bools, mbool is true if any is set */
477         typedef u32 mbool;
478 #define mboolset(mb, bit)               ((mb) |= (bit)) /* set one bool */
479 #define mboolclr(mb, bit)               ((mb) &= ~(bit))        /* clear one bool */
480 #define mboolisset(mb, bit)             (((mb) & (bit)) != 0)   /* true if one bool is set */
481 #define mboolmaskset(mb, mask, val)     ((mb) = (((mb) & ~(mask)) | (val)))
482
483 /* power conversion */
484 extern u16 brcmu_qdbm_to_mw(u8 qdbm);
485 extern u8 brcmu_mw_to_qdbm(u16 mw);
486
487 extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size);
488 extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...);
489
490 extern uint brcmu_mkiovar(char *name, char *data, uint datalen,
491                           char *buf, uint len);
492 extern uint brcmu_bitcount(u8 *bitmap, uint bytelength);
493
494 #endif                          /* _brcmutils_h_ */