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>
23 * Spin at most 'us' microseconds while 'exp' is true.
24 * Caller should explicitly test 'exp' when this completes
25 * and take appropriate error action if 'exp' is still true.
27 #define SPINWAIT(exp, us) { \
28 uint countdown = (us) + 9; \
29 while ((exp) && (countdown >= 10)) {\
35 /* osl multi-precedence packet queue */
36 #define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
37 #define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
39 #define BCME_STRLEN 64 /* Max string length for BCM errors */
41 /* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
45 #ifndef NBBY /* the BSD family defines NBBY */
46 #define NBBY 8 /* 8 bits per byte */
47 #endif /* #ifndef NBBY */
48 #define setbit(a, i) (((u8 *)a)[(i)/NBBY] |= 1<<((i)%NBBY))
49 #define clrbit(a, i) (((u8 *)a)[(i)/NBBY] &= ~(1<<((i)%NBBY)))
50 #define isset(a, i) (((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY)))
51 #define isclr(a, i) ((((const u8 *)a)[(i)/NBBY] & (1<<((i)%NBBY))) == 0)
54 #define NBITS(type) (sizeof(type) * 8)
55 #define NBITVAL(nbits) (1 << (nbits))
56 #define MAXBITVAL(nbits) ((1 << (nbits)) - 1)
57 #define NBITMASK(nbits) MAXBITVAL(nbits)
58 #define MAXNBVAL(nbyte) MAXBITVAL((nbyte) * 8)
61 #define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
62 #define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
64 /* 18-bytes of Ethernet address buffer length */
65 #define ETHER_ADDR_STR_LEN 18
68 struct sk_buff
*head
; /* first packet to dequeue */
69 struct sk_buff
*tail
; /* last packet to dequeue */
70 u16 len
; /* number of queued packets */
71 u16 max
; /* maximum number of queued packets */
74 /* multi-priority pkt queue */
76 u16 num_prec
; /* number of precedences in use */
77 u16 hi_prec
; /* rapid dequeue hint (>= highest non-empty prec) */
78 u16 max
; /* total max packets */
79 u16 len
; /* total number of packets */
81 * q array must be last since # of elements can be either
84 struct pktq_prec q
[PKTQ_MAX_PREC
];
87 /* operations on a specific precedence in packet queue */
89 static inline int pktq_plen(struct pktq
*pq
, int prec
)
91 return pq
->q
[prec
].len
;
94 static inline int pktq_pavail(struct pktq
*pq
, int prec
)
96 return pq
->q
[prec
].max
- pq
->q
[prec
].len
;
99 static inline bool pktq_pfull(struct pktq
*pq
, int prec
)
101 return pq
->q
[prec
].len
>= pq
->q
[prec
].max
;
104 static inline bool pktq_pempty(struct pktq
*pq
, int prec
)
106 return pq
->q
[prec
].len
== 0;
109 static inline struct sk_buff
*pktq_ppeek(struct pktq
*pq
, int prec
)
111 return pq
->q
[prec
].head
;
114 static inline struct sk_buff
*pktq_ppeek_tail(struct pktq
*pq
, int prec
)
116 return pq
->q
[prec
].tail
;
119 extern struct sk_buff
*brcmu_pktq_penq(struct pktq
*pq
, int prec
,
121 extern struct sk_buff
*brcmu_pktq_penq_head(struct pktq
*pq
, int prec
,
123 extern struct sk_buff
*brcmu_pktq_pdeq(struct pktq
*pq
, int prec
);
124 extern struct sk_buff
*brcmu_pktq_pdeq_tail(struct pktq
*pq
, int prec
);
126 /* packet primitives */
127 extern struct sk_buff
*brcmu_pkt_buf_get_skb(uint len
);
128 extern void brcmu_pkt_buf_free_skb(struct sk_buff
*skb
);
130 /* Empty the queue at particular precedence level */
131 /* callback function fn(pkt, arg) returns true if pkt belongs to if */
132 extern void brcmu_pktq_pflush(struct pktq
*pq
, int prec
,
133 bool dir
, bool (*fn
)(struct sk_buff
*, void *), void *arg
);
135 /* operations on a set of precedences in packet queue */
137 extern int brcmu_pktq_mlen(struct pktq
*pq
, uint prec_bmp
);
138 extern struct sk_buff
*brcmu_pktq_mdeq(struct pktq
*pq
, uint prec_bmp
,
141 /* operations on packet queue as a whole */
143 static inline int pktq_len(struct pktq
*pq
)
148 static inline int pktq_max(struct pktq
*pq
)
153 static inline int pktq_avail(struct pktq
*pq
)
155 return (int)(pq
->max
- pq
->len
);
158 static inline bool pktq_full(struct pktq
*pq
)
160 return pq
->len
>= pq
->max
;
163 static inline bool pktq_empty(struct pktq
*pq
)
168 extern void brcmu_pktq_init(struct pktq
*pq
, int num_prec
, int max_len
);
169 /* prec_out may be NULL if caller is not interested in return value */
170 extern struct sk_buff
*brcmu_pktq_peek_tail(struct pktq
*pq
, int *prec_out
);
171 extern void brcmu_pktq_flush(struct pktq
*pq
, bool dir
,
172 bool (*fn
)(struct sk_buff
*, void *), void *arg
);
176 extern uint
brcmu_pktfrombuf(struct sk_buff
*p
,
177 uint offset
, int len
, unsigned char *buf
);
178 extern uint
brcmu_pkttotlen(struct sk_buff
*p
);
184 extern void brcmu_prpkt(const char *msg
, struct sk_buff
*p0
);
186 #define brcmu_prpkt(a, b)
192 extern int brcmu_format_hex(char *str
, const void *bytes
, int len
);
195 #endif /* _BRCMU_UTILS_H_ */