blob: b7537f70a79538fab66fd48183179c4b78a2f78a [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#include <linux/netdevice.h>
Stephen Rothwellb7a57e72011-10-12 21:35:07 +020018#include <linux/module.h>
Arend van Spriel20e5ca12011-10-18 14:03:09 +020019
Arend van Spriel5b435de2011-10-05 13:19:03 +020020#include <brcmu_utils.h>
21
22MODULE_AUTHOR("Broadcom Corporation");
23MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities.");
24MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
25MODULE_LICENSE("Dual BSD/GPL");
26
27struct sk_buff *brcmu_pkt_buf_get_skb(uint len)
28{
29 struct sk_buff *skb;
30
31 skb = dev_alloc_skb(len);
32 if (skb) {
33 skb_put(skb, len);
34 skb->priority = 0;
35 }
36
37 return skb;
38}
39EXPORT_SYMBOL(brcmu_pkt_buf_get_skb);
40
41/* Free the driver packet. Free the tag if present */
42void brcmu_pkt_buf_free_skb(struct sk_buff *skb)
43{
Arend van Spriel53ee4bc2011-11-10 20:30:30 +010044 WARN_ON(skb->next);
45 if (skb->destructor)
46 /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if
47 * destructor exists
48 */
49 dev_kfree_skb_any(skb);
50 else
51 /* can free immediately (even in_irq()) if destructor
52 * does not exist
53 */
54 dev_kfree_skb(skb);
Arend van Spriel5b435de2011-10-05 13:19:03 +020055}
56EXPORT_SYMBOL(brcmu_pkt_buf_free_skb);
57
Arend van Spriel5b435de2011-10-05 13:19:03 +020058/*
59 * osl multiple-precedence packet queue
60 * hi_prec is always >= the number of the highest non-empty precedence
61 */
62struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec,
63 struct sk_buff *p)
64{
Arend van Sprielad3b8b32011-11-10 20:30:27 +010065 struct sk_buff_head *q;
Arend van Spriel5b435de2011-10-05 13:19:03 +020066
67 if (pktq_full(pq) || pktq_pfull(pq, prec))
68 return NULL;
69
Arend van Sprielad3b8b32011-11-10 20:30:27 +010070 q = &pq->q[prec].skblist;
71 skb_queue_tail(q, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +020072 pq->len++;
73
74 if (pq->hi_prec < prec)
75 pq->hi_prec = (u8) prec;
76
77 return p;
78}
79EXPORT_SYMBOL(brcmu_pktq_penq);
80
81struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec,
82 struct sk_buff *p)
83{
Arend van Sprielad3b8b32011-11-10 20:30:27 +010084 struct sk_buff_head *q;
Arend van Spriel5b435de2011-10-05 13:19:03 +020085
86 if (pktq_full(pq) || pktq_pfull(pq, prec))
87 return NULL;
88
Arend van Sprielad3b8b32011-11-10 20:30:27 +010089 q = &pq->q[prec].skblist;
90 skb_queue_head(q, p);
Arend van Spriel5b435de2011-10-05 13:19:03 +020091 pq->len++;
92
93 if (pq->hi_prec < prec)
94 pq->hi_prec = (u8) prec;
95
96 return p;
97}
98EXPORT_SYMBOL(brcmu_pktq_penq_head);
99
100struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec)
101{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100102 struct sk_buff_head *q;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200103 struct sk_buff *p;
104
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100105 q = &pq->q[prec].skblist;
106 p = skb_dequeue(q);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200107 if (p == NULL)
108 return NULL;
109
Arend van Spriel5b435de2011-10-05 13:19:03 +0200110 pq->len--;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200111 return p;
112}
113EXPORT_SYMBOL(brcmu_pktq_pdeq);
114
115struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec)
116{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100117 struct sk_buff_head *q;
118 struct sk_buff *p;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200119
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100120 q = &pq->q[prec].skblist;
121 p = skb_dequeue_tail(q);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200122 if (p == NULL)
123 return NULL;
124
Arend van Spriel5b435de2011-10-05 13:19:03 +0200125 pq->len--;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200126 return p;
127}
128EXPORT_SYMBOL(brcmu_pktq_pdeq_tail);
129
130void
131brcmu_pktq_pflush(struct pktq *pq, int prec, bool dir,
132 bool (*fn)(struct sk_buff *, void *), void *arg)
133{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100134 struct sk_buff_head *q;
135 struct sk_buff *p, *next;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200136
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100137 q = &pq->q[prec].skblist;
138 skb_queue_walk_safe(q, p, next) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200139 if (fn == NULL || (*fn) (p, arg)) {
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100140 skb_unlink(p, q);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200141 brcmu_pkt_buf_free_skb(p);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200142 pq->len--;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200143 }
144 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200145}
146EXPORT_SYMBOL(brcmu_pktq_pflush);
147
148void brcmu_pktq_flush(struct pktq *pq, bool dir,
149 bool (*fn)(struct sk_buff *, void *), void *arg)
150{
151 int prec;
152 for (prec = 0; prec < pq->num_prec; prec++)
153 brcmu_pktq_pflush(pq, prec, dir, fn, arg);
154}
155EXPORT_SYMBOL(brcmu_pktq_flush);
156
157void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len)
158{
159 int prec;
160
161 /* pq is variable size; only zero out what's requested */
162 memset(pq, 0,
163 offsetof(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec));
164
165 pq->num_prec = (u16) num_prec;
166
167 pq->max = (u16) max_len;
168
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100169 for (prec = 0; prec < num_prec; prec++) {
Arend van Spriel5b435de2011-10-05 13:19:03 +0200170 pq->q[prec].max = pq->max;
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100171 skb_queue_head_init(&pq->q[prec].skblist);
172 }
Arend van Spriel5b435de2011-10-05 13:19:03 +0200173}
174EXPORT_SYMBOL(brcmu_pktq_init);
175
176struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out)
177{
178 int prec;
179
180 if (pq->len == 0)
181 return NULL;
182
183 for (prec = 0; prec < pq->hi_prec; prec++)
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100184 if (!skb_queue_empty(&pq->q[prec].skblist))
Arend van Spriel5b435de2011-10-05 13:19:03 +0200185 break;
186
187 if (prec_out)
188 *prec_out = prec;
189
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100190 return skb_peek_tail(&pq->q[prec].skblist);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200191}
192EXPORT_SYMBOL(brcmu_pktq_peek_tail);
193
194/* Return sum of lengths of a specific set of precedences */
195int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp)
196{
197 int prec, len;
198
199 len = 0;
200
201 for (prec = 0; prec <= pq->hi_prec; prec++)
202 if (prec_bmp & (1 << prec))
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100203 len += pq->q[prec].skblist.qlen;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200204
205 return len;
206}
207EXPORT_SYMBOL(brcmu_pktq_mlen);
208
209/* Priority dequeue from a specific set of precedences */
210struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp,
211 int *prec_out)
212{
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100213 struct sk_buff_head *q;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200214 struct sk_buff *p;
215 int prec;
216
217 if (pq->len == 0)
218 return NULL;
219
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100220 while ((prec = pq->hi_prec) > 0 &&
221 skb_queue_empty(&pq->q[prec].skblist))
Arend van Spriel5b435de2011-10-05 13:19:03 +0200222 pq->hi_prec--;
223
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100224 while ((prec_bmp & (1 << prec)) == 0 ||
225 skb_queue_empty(&pq->q[prec].skblist))
Arend van Spriel5b435de2011-10-05 13:19:03 +0200226 if (prec-- == 0)
227 return NULL;
228
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100229 q = &pq->q[prec].skblist;
230 p = skb_dequeue(q);
Arend van Spriel5b435de2011-10-05 13:19:03 +0200231 if (p == NULL)
232 return NULL;
233
Arend van Sprielad3b8b32011-11-10 20:30:27 +0100234 pq->len--;
Arend van Spriel5b435de2011-10-05 13:19:03 +0200235
236 if (prec_out)
237 *prec_out = prec;
238
Arend van Spriel5b435de2011-10-05 13:19:03 +0200239 return p;
240}
241EXPORT_SYMBOL(brcmu_pktq_mdeq);
242
243#if defined(BCMDBG)
244/* pretty hex print a pkt buffer chain */
245void brcmu_prpkt(const char *msg, struct sk_buff *p0)
246{
247 struct sk_buff *p;
248
249 if (msg && (msg[0] != '\0'))
250 printk(KERN_DEBUG "%s:\n", msg);
251
252 for (p = p0; p; p = p->next)
253 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, p->data, p->len);
254}
255EXPORT_SYMBOL(brcmu_prpkt);
256#endif /* defined(BCMDBG) */