Commit | Line | Data |
---|---|---|
a9533e7e HP |
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 | ||
f8557965 RV |
17 | #ifndef _BRCMU_UTILS_H_ |
18 | #define _BRCMU_UTILS_H_ | |
a9533e7e | 19 | |
a9533e7e | 20 | /* Buffer structure for collecting string-formatted data |
67ad48bc RV |
21 | * using brcmu_bprintf() API. |
22 | * Use brcmu_binit() to initialize before use | |
a9533e7e HP |
23 | */ |
24 | ||
f99f8a72 RV |
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 | }; | |
a9533e7e HP |
31 | |
32 | /* | |
33 | * Spin at most 'us' microseconds while 'exp' is true. | |
34 | * Caller should explicitly test 'exp' when this completes | |
35 | * and take appropriate error action if 'exp' is still true. | |
36 | */ | |
37 | #define SPINWAIT(exp, us) { \ | |
38 | uint countdown = (us) + 9; \ | |
39 | while ((exp) && (countdown >= 10)) {\ | |
7383141b | 40 | udelay(10); \ |
a9533e7e HP |
41 | countdown -= 10; \ |
42 | } \ | |
43 | } | |
44 | ||
45 | /* osl multi-precedence packet queue */ | |
46 | #ifndef PKTQ_LEN_DEFAULT | |
47 | #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */ | |
48 | #endif | |
49 | #ifndef PKTQ_MAX_PREC | |
50 | #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */ | |
51 | #endif | |
52 | ||
f99f8a72 RV |
53 | struct pktq_prec { |
54 | struct sk_buff *head; /* first packet to dequeue */ | |
55 | struct sk_buff *tail; /* last packet to dequeue */ | |
56 | u16 len; /* number of queued packets */ | |
57 | u16 max; /* maximum number of queued packets */ | |
58 | }; | |
a9533e7e HP |
59 | |
60 | /* multi-priority pkt queue */ | |
f99f8a72 RV |
61 | struct pktq { |
62 | u16 num_prec; /* number of precedences in use */ | |
63 | u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */ | |
64 | u16 max; /* total max packets */ | |
65 | u16 len; /* total number of packets */ | |
66 | /* | |
67 | * q array must be last since # of elements can be either | |
68 | * PKTQ_MAX_PREC or 1 | |
69 | */ | |
70 | struct pktq_prec q[PKTQ_MAX_PREC]; | |
71 | }; | |
a9533e7e HP |
72 | |
73 | /* fn(pkt, arg). return true if pkt belongs to if */ | |
aacd531d | 74 | typedef bool(*ifpkt_cb_t) (struct sk_buff *, void *); |
a9533e7e | 75 | |
a9533e7e HP |
76 | /* operations on a specific precedence in packet queue */ |
77 | ||
78 | #define pktq_psetmax(pq, prec, _max) ((pq)->q[prec].max = (_max)) | |
79 | #define pktq_plen(pq, prec) ((pq)->q[prec].len) | |
80 | #define pktq_pavail(pq, prec) ((pq)->q[prec].max - (pq)->q[prec].len) | |
81 | #define pktq_pfull(pq, prec) ((pq)->q[prec].len >= (pq)->q[prec].max) | |
82 | #define pktq_pempty(pq, prec) ((pq)->q[prec].len == 0) | |
83 | ||
84 | #define pktq_ppeek(pq, prec) ((pq)->q[prec].head) | |
85 | #define pktq_ppeek_tail(pq, prec) ((pq)->q[prec].tail) | |
86 | ||
67ad48bc | 87 | extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec, |
c26b1378 | 88 | struct sk_buff *p); |
67ad48bc | 89 | extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec, |
c26b1378 | 90 | struct sk_buff *p); |
67ad48bc RV |
91 | extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec); |
92 | extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec); | |
c26b1378 | 93 | |
371d72a1 | 94 | /* packet primitives */ |
67ad48bc RV |
95 | extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len); |
96 | extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb); | |
371d72a1 | 97 | |
a9533e7e | 98 | /* Empty the queue at particular precedence level */ |
67ad48bc | 99 | extern void brcmu_pktq_pflush(struct pktq *pq, int prec, |
84067de6 | 100 | bool dir, ifpkt_cb_t fn, void *arg); |
a9533e7e HP |
101 | |
102 | /* operations on a set of precedences in packet queue */ | |
103 | ||
67ad48bc RV |
104 | extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp); |
105 | extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp, | |
b33f0e28 | 106 | int *prec_out); |
a9533e7e HP |
107 | |
108 | /* operations on packet queue as a whole */ | |
109 | ||
110 | #define pktq_len(pq) ((int)(pq)->len) | |
111 | #define pktq_max(pq) ((int)(pq)->max) | |
112 | #define pktq_avail(pq) ((int)((pq)->max - (pq)->len)) | |
113 | #define pktq_full(pq) ((pq)->len >= (pq)->max) | |
114 | #define pktq_empty(pq) ((pq)->len == 0) | |
115 | ||
116 | /* operations for single precedence queues */ | |
67ad48bc RV |
117 | #define pktenq(pq, p) brcmu_pktq_penq(((struct pktq *)pq), 0, (p)) |
118 | #define pktenq_head(pq, p)\ | |
119 | brcmu_pktq_penq_head(((struct pktq *)pq), 0, (p)) | |
120 | #define pktdeq(pq) brcmu_pktq_pdeq(((struct pktq *)pq), 0) | |
121 | #define pktdeq_tail(pq) brcmu_pktq_pdeq_tail(((struct pktq *)pq), 0) | |
122 | #define pktqinit(pq, len) brcmu_pktq_init(((struct pktq *)pq), 1, len) | |
123 | ||
124 | extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len); | |
a9533e7e | 125 | /* prec_out may be NULL if caller is not interested in return value */ |
67ad48bc RV |
126 | extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out); |
127 | extern void brcmu_pktq_flush(struct pktq *pq, bool dir, | |
84067de6 | 128 | ifpkt_cb_t fn, void *arg); |
a9533e7e HP |
129 | |
130 | /* externs */ | |
131 | /* packet */ | |
67ad48bc | 132 | extern uint brcmu_pktfrombuf(struct sk_buff *p, |
b33f0e28 | 133 | uint offset, int len, unsigned char *buf); |
67ad48bc | 134 | extern uint brcmu_pkttotlen(struct sk_buff *p); |
a9533e7e | 135 | |
a9533e7e | 136 | /* ethernet address */ |
67ad48bc | 137 | extern int brcmu_ether_atoe(char *p, u8 *ea); |
a9533e7e HP |
138 | |
139 | /* ip address */ | |
140 | struct ipv4_addr; | |
a9533e7e | 141 | |
a9533e7e | 142 | #ifdef BCMDBG |
67ad48bc | 143 | extern void brcmu_prpkt(const char *msg, struct sk_buff *p0); |
bc042b67 | 144 | #else |
67ad48bc | 145 | #define brcmu_prpkt(a, b) |
a9533e7e | 146 | #endif /* BCMDBG */ |
bc042b67 | 147 | |
a9533e7e HP |
148 | /* Support for sharing code across in-driver iovar implementations. |
149 | * The intent is that a driver use this structure to map iovar names | |
150 | * to its (private) iovar identifiers, and the lookup function to | |
151 | * find the entry. Macros are provided to map ids and get/set actions | |
152 | * into a single number space for a switch statement. | |
153 | */ | |
154 | ||
155 | /* iovar structure */ | |
67ad48bc RV |
156 | struct brcmu_iovar { |
157 | const char *name; /* name for lookup and display */ | |
158 | u16 varid; /* id for switch */ | |
159 | u16 flags; /* driver-specific flag bits */ | |
160 | u16 type; /* base type of argument */ | |
161 | u16 minlen; /* min length for buffer vars */ | |
162 | }; | |
a9533e7e HP |
163 | |
164 | /* varid definitions are per-driver, may use these get/set bits */ | |
165 | ||
166 | /* IOVar action bits for id mapping */ | |
167 | #define IOV_GET 0 /* Get an iovar */ | |
168 | #define IOV_SET 1 /* Set an iovar */ | |
169 | ||
170 | /* Varid to actionid mapping */ | |
171 | #define IOV_GVAL(id) ((id)*2) | |
172 | #define IOV_SVAL(id) (((id)*2)+IOV_SET) | |
173 | #define IOV_ISSET(actionid) ((actionid & IOV_SET) == IOV_SET) | |
174 | #define IOV_ID(actionid) (actionid >> 1) | |
175 | ||
67ad48bc RV |
176 | extern const struct |
177 | brcmu_iovar *brcmu_iovar_lookup(const struct brcmu_iovar *table, | |
178 | const char *name); | |
179 | extern int brcmu_iovar_lencheck(const struct brcmu_iovar *table, void *arg, | |
180 | int len, bool set); | |
a9533e7e HP |
181 | |
182 | /* Base type definitions */ | |
183 | #define IOVT_VOID 0 /* no value (implictly set only) */ | |
184 | #define IOVT_BOOL 1 /* any value ok (zero/nonzero) */ | |
185 | #define IOVT_INT8 2 /* integer values are range-checked */ | |
186 | #define IOVT_UINT8 3 /* unsigned int 8 bits */ | |
187 | #define IOVT_INT16 4 /* int 16 bits */ | |
188 | #define IOVT_UINT16 5 /* unsigned int 16 bits */ | |
189 | #define IOVT_INT32 6 /* int 32 bits */ | |
190 | #define IOVT_UINT32 7 /* unsigned int 32 bits */ | |
191 | #define IOVT_BUFFER 8 /* buffer is size-checked as per minlen */ | |
192 | #define BCM_IOVT_VALID(type) (((unsigned int)(type)) <= IOVT_BUFFER) | |
193 | ||
a9533e7e HP |
194 | /* ** driver/apps-shared section ** */ |
195 | ||
196 | #define BCME_STRLEN 64 /* Max string length for BCM errors */ | |
a9533e7e | 197 | |
a9533e7e | 198 | #ifndef ABS |
5fee2540 | 199 | #define ABS(a) (((a) < 0) ? -(a) : (a)) |
a9533e7e HP |
200 | #endif /* ABS */ |
201 | ||
a9533e7e | 202 | #define CEIL(x, y) (((x) + ((y)-1)) / (y)) |
a9533e7e | 203 | #define ISPOWEROF2(x) ((((x)-1)&(x)) == 0) |
a9533e7e | 204 | |
8968af14 BR |
205 | /* map physical to virtual I/O */ |
206 | #if !defined(CONFIG_MMC_MSM7X00A) | |
207 | #define REG_MAP(pa, size) ioremap_nocache((unsigned long)(pa), \ | |
208 | (unsigned long)(size)) | |
209 | #else | |
210 | #define REG_MAP(pa, size) (void *)(0) | |
211 | #endif | |
212 | ||
278d927d AS |
213 | /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */ |
214 | #define PKTBUFSZ 2048 | |
215 | ||
216 | #define OSL_SYSUPTIME() ((u32)jiffies * (1000 / HZ)) | |
278d927d | 217 | |
a9533e7e HP |
218 | #ifndef setbit |
219 | #ifndef NBBY /* the BSD family defines NBBY */ | |
220 | #define NBBY 8 /* 8 bits per byte */ | |
221 | #endif /* #ifndef NBBY */ | |
de9bca63 GKH |
222 | #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY)) |
223 | #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY))) | |
224 | #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) | |
225 | #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0) | |
a9533e7e HP |
226 | #endif /* setbit */ |
227 | ||
228 | #define NBITS(type) (sizeof(type) * 8) | |
229 | #define NBITVAL(nbits) (1 << (nbits)) | |
230 | #define MAXBITVAL(nbits) ((1 << (nbits)) - 1) | |
231 | #define NBITMASK(nbits) MAXBITVAL(nbits) | |
232 | #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8) | |
233 | ||
234 | /* basic mux operation - can be optimized on several architectures */ | |
235 | #define MUX(pred, true, false) ((pred) ? (true) : (false)) | |
236 | ||
237 | /* modulo inc/dec - assumes x E [0, bound - 1] */ | |
238 | #define MODDEC(x, bound) MUX((x) == 0, (bound) - 1, (x) - 1) | |
239 | #define MODINC(x, bound) MUX((x) == (bound) - 1, 0, (x) + 1) | |
240 | ||
241 | /* modulo inc/dec, bound = 2^k */ | |
242 | #define MODDEC_POW2(x, bound) (((x) - 1) & ((bound) - 1)) | |
243 | #define MODINC_POW2(x, bound) (((x) + 1) & ((bound) - 1)) | |
244 | ||
245 | /* modulo add/sub - assumes x, y E [0, bound - 1] */ | |
246 | #define MODADD(x, y, bound) \ | |
247 | MUX((x) + (y) >= (bound), (x) + (y) - (bound), (x) + (y)) | |
248 | #define MODSUB(x, y, bound) \ | |
249 | MUX(((int)(x)) - ((int)(y)) < 0, (x) - (y) + (bound), (x) - (y)) | |
250 | ||
251 | /* module add/sub, bound = 2^k */ | |
252 | #define MODADD_POW2(x, y, bound) (((x) + (y)) & ((bound) - 1)) | |
253 | #define MODSUB_POW2(x, y, bound) (((x) - (y)) & ((bound) - 1)) | |
254 | ||
255 | /* crc defines */ | |
256 | #define CRC8_INIT_VALUE 0xff /* Initial CRC8 checksum value */ | |
257 | #define CRC8_GOOD_VALUE 0x9f /* Good final CRC8 checksum value */ | |
258 | #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */ | |
259 | #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */ | |
260 | ||
67ad48bc RV |
261 | /* brcmu_format_flags() bit description structure */ |
262 | struct brcmu_bit_desc { | |
263 | u32 bit; | |
264 | const char *name; | |
265 | }; | |
a9533e7e HP |
266 | |
267 | /* tag_ID/length/value_buffer tuple */ | |
67ad48bc RV |
268 | struct brcmu_tlv { |
269 | u8 id; | |
270 | u8 len; | |
271 | u8 data[1]; | |
272 | }; | |
a9533e7e | 273 | |
a9533e7e HP |
274 | #define ETHER_ADDR_STR_LEN 18 /* 18-bytes of Ethernet address buffer length */ |
275 | ||
a9533e7e HP |
276 | /* externs */ |
277 | /* crc */ | |
67ad48bc RV |
278 | extern u8 brcmu_crc8(u8 *p, uint nbytes, u8 crc); |
279 | ||
a9533e7e HP |
280 | /* format/print */ |
281 | #if defined(BCMDBG) | |
67ad48bc RV |
282 | extern int brcmu_format_flags(const struct brcmu_bit_desc *bd, u32 flags, |
283 | char *buf, int len); | |
284 | extern int brcmu_format_hex(char *str, const void *bytes, int len); | |
a9533e7e | 285 | #endif |
a9533e7e | 286 | |
67ad48bc RV |
287 | extern char *brcmu_chipname(uint chipid, char *buf, uint len); |
288 | ||
289 | extern struct brcmu_tlv *brcmu_parse_tlvs(void *buf, int buflen, | |
290 | uint key); | |
a9533e7e | 291 | |
a9533e7e | 292 | /* power conversion */ |
67ad48bc RV |
293 | extern u16 brcmu_qdbm_to_mw(u8 qdbm); |
294 | extern u8 brcmu_mw_to_qdbm(u16 mw); | |
a9533e7e | 295 | |
67ad48bc RV |
296 | extern void brcmu_binit(struct brcmu_strbuf *b, char *buf, uint size); |
297 | extern int brcmu_bprintf(struct brcmu_strbuf *b, const char *fmt, ...); | |
a9533e7e | 298 | |
67ad48bc RV |
299 | extern uint brcmu_mkiovar(char *name, char *data, uint datalen, |
300 | char *buf, uint len); | |
301 | extern uint brcmu_bitcount(u8 *bitmap, uint bytelength); | |
a9533e7e | 302 | |
f8557965 | 303 | #endif /* _BRCMU_UTILS_H_ */ |