blob: 477b92ad3d62c37b9ec99b3e7b48b0b60f388311 [file] [log] [blame]
Arend van Spriel5b435de2011-10-05 13:19:03 +02001/*
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 _BRCMU_UTILS_H_
18#define _BRCMU_UTILS_H_
19
20#include <linux/skbuff.h>
21
22/*
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.
26 */
27#define SPINWAIT(exp, us) { \
28 uint countdown = (us) + 9; \
29 while ((exp) && (countdown >= 10)) {\
30 udelay(10); \
31 countdown -= 10; \
32 } \
33}
34
35/* osl multi-precedence packet queue */
36#define PKTQ_LEN_DEFAULT 128 /* Max 128 packets */
37#define PKTQ_MAX_PREC 16 /* Maximum precedence levels */
38
39#define BCME_STRLEN 64 /* Max string length for BCM errors */
40
41/* the largest reasonable packet buffer driver uses for ethernet MTU in bytes */
42#define PKTBUFSZ 2048
43
44#ifndef setbit
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)
52#endif /* setbit */
53
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)
59
60/* crc defines */
61#define CRC16_INIT_VALUE 0xffff /* Initial CRC16 checksum value */
62#define CRC16_GOOD_VALUE 0xf0b8 /* Good final CRC16 checksum value */
63
64/* 18-bytes of Ethernet address buffer length */
65#define ETHER_ADDR_STR_LEN 18
66
67struct pktq_prec {
Arend van Sprielad3b8b32011-11-10 20:30:27 +010068 struct sk_buff_head skblist;
Arend van Spriel5b435de2011-10-05 13:19:03 +020069 u16 max; /* maximum number of queued packets */
70};
71
72/* multi-priority pkt queue */
73struct pktq {
74 u16 num_prec; /* number of precedences in use */
75 u16 hi_prec; /* rapid dequeue hint (>= highest non-empty prec) */
76 u16 max; /* total max packets */
77 u16 len; /* total number of packets */
78 /*
79 * q array must be last since # of elements can be either
80 * PKTQ_MAX_PREC or 1
81 */
82 struct pktq_prec q[PKTQ_MAX_PREC];
83};
84
85/* operations on a specific precedence in packet queue */
86
87static inline int pktq_plen(struct pktq *pq, int prec)
88{
Arend van Sprielad3b8b32011-11-10 20:30:27 +010089 return pq->q[prec].skblist.qlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +020090}
91
92static inline int pktq_pavail(struct pktq *pq, int prec)
93{
Arend van Sprielad3b8b32011-11-10 20:30:27 +010094 return pq->q[prec].max - pq->q[prec].skblist.qlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +020095}
96
97static inline bool pktq_pfull(struct pktq *pq, int prec)
98{
Arend van Sprielad3b8b32011-11-10 20:30:27 +010099 return pq->q[prec].skblist.qlen >= pq->q[prec].max;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200100}
101
102static inline bool pktq_pempty(struct pktq *pq, int prec)
103{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100104 return skb_queue_empty(&pq->q[prec].skblist);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200105}
106
107static inline struct sk_buff *pktq_ppeek(struct pktq *pq, int prec)
108{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100109 return skb_peek(&pq->q[prec].skblist);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200110}
111
112static inline struct sk_buff *pktq_ppeek_tail(struct pktq *pq, int prec)
113{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100114 return skb_peek_tail(&pq->q[prec].skblist);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200115}
116
117extern struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
118 struct sk_buff *p);
119extern struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
120 struct sk_buff *p);
121extern struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec);
122extern struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec);
123
124/* packet primitives */
125extern struct sk_buff *brcmu_pkt_buf_get_skb(uint len);
126extern void brcmu_pkt_buf_free_skb(struct sk_buff *skb);
127
128/* Empty the queue at particular precedence level */
129/* callback function fn(pkt, arg) returns true if pkt belongs to if */
130extern void brcmu_pktq_pflush(struct pktq *pq, int prec,
131 bool dir, bool (*fn)(struct sk_buff *, void *), void *arg);
132
133/* operations on a set of precedences in packet queue */
134
135extern int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp);
136extern struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
137 int *prec_out);
138
139/* operations on packet queue as a whole */
140
141static inline int pktq_len(struct pktq *pq)
142{
143 return (int)pq->len;
144}
145
146static inline int pktq_max(struct pktq *pq)
147{
148 return (int)pq->max;
149}
150
151static inline int pktq_avail(struct pktq *pq)
152{
153 return (int)(pq->max - pq->len);
154}
155
156static inline bool pktq_full(struct pktq *pq)
157{
158 return pq->len >= pq->max;
159}
160
161static inline bool pktq_empty(struct pktq *pq)
162{
163 return pq->len == 0;
164}
165
166extern void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len);
167/* prec_out may be NULL if caller is not interested in return value */
168extern struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out);
169extern void brcmu_pktq_flush(struct pktq *pq, bool dir,
170 bool (*fn)(struct sk_buff *, void *), void *arg);
171
172/* externs */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200173/* ip address */
174struct ipv4_addr;
175
Arend van Spriel09c7dfa2011-10-18 14:03:10 +0200176
177/* externs */
178/* format/print */
Joe Perches8ae74652012-01-15 00:38:38 -0800179#ifdef DEBUG
Arend van Spriel5b435de2011-10-05 13:19:03 +0200180extern void brcmu_prpkt(const char *msg, struct sk_buff *p0);
181#else
182#define brcmu_prpkt(a, b)
Joe Perches8ae74652012-01-15 00:38:38 -0800183#endif /* DEBUG */
Arend van Spriel5b435de2011-10-05 13:19:03 +0200184
Joe Perches1e023822012-01-15 00:38:40 -0800185#ifdef DEBUG
186extern __printf(3, 4)
187void brcmu_dbg_hex_dump(const void *data, size_t size, const char *fmt, ...);
188#else
189__printf(3, 4)
190static inline
191void brcmu_dbg_hex_dump(const void *data, size_t size, const char *fmt, ...)
192{
193}
194#endif
195
Arend van Spriel5b435de2011-10-05 13:19:03 +0200196#endif /* _BRCMU_UTILS_H_ */