1 // SPDX-License-Identifier: ISC
3 * Copyright (c) 2010 Broadcom Corporation
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/netdevice.h>
9 #include <linux/module.h>
11 #include <brcmu_utils.h>
13 MODULE_AUTHOR("Broadcom Corporation");
14 MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities.");
15 MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards");
16 MODULE_LICENSE("Dual BSD/GPL");
18 struct sk_buff
*brcmu_pkt_buf_get_skb(uint len
)
22 skb
= dev_alloc_skb(len
);
30 EXPORT_SYMBOL(brcmu_pkt_buf_get_skb
);
32 /* Free the driver packet. Free the tag if present */
33 void brcmu_pkt_buf_free_skb(struct sk_buff
*skb
)
39 dev_kfree_skb_any(skb
);
41 EXPORT_SYMBOL(brcmu_pkt_buf_free_skb
);
44 * osl multiple-precedence packet queue
45 * hi_prec is always >= the number of the highest non-empty precedence
47 struct sk_buff
*brcmu_pktq_penq(struct pktq
*pq
, int prec
,
50 struct sk_buff_head
*q
;
52 if (pktq_full(pq
) || pktq_pfull(pq
, prec
))
55 q
= &pq
->q
[prec
].skblist
;
59 if (pq
->hi_prec
< prec
)
60 pq
->hi_prec
= (u8
) prec
;
64 EXPORT_SYMBOL(brcmu_pktq_penq
);
66 struct sk_buff
*brcmu_pktq_penq_head(struct pktq
*pq
, int prec
,
69 struct sk_buff_head
*q
;
71 if (pktq_full(pq
) || pktq_pfull(pq
, prec
))
74 q
= &pq
->q
[prec
].skblist
;
78 if (pq
->hi_prec
< prec
)
79 pq
->hi_prec
= (u8
) prec
;
83 EXPORT_SYMBOL(brcmu_pktq_penq_head
);
85 struct sk_buff
*brcmu_pktq_pdeq(struct pktq
*pq
, int prec
)
87 struct sk_buff_head
*q
;
90 q
= &pq
->q
[prec
].skblist
;
98 EXPORT_SYMBOL(brcmu_pktq_pdeq
);
101 * precedence based dequeue with match function. Passing a NULL pointer
102 * for the match function parameter is considered to be a wildcard so
103 * any packet on the queue is returned. In that case it is no different
104 * from brcmu_pktq_pdeq() above.
106 struct sk_buff
*brcmu_pktq_pdeq_match(struct pktq
*pq
, int prec
,
107 bool (*match_fn
)(struct sk_buff
*skb
,
108 void *arg
), void *arg
)
110 struct sk_buff_head
*q
;
111 struct sk_buff
*p
, *next
;
113 q
= &pq
->q
[prec
].skblist
;
114 skb_queue_walk_safe(q
, p
, next
) {
115 if (match_fn
== NULL
|| match_fn(p
, arg
)) {
123 EXPORT_SYMBOL(brcmu_pktq_pdeq_match
);
125 struct sk_buff
*brcmu_pktq_pdeq_tail(struct pktq
*pq
, int prec
)
127 struct sk_buff_head
*q
;
130 q
= &pq
->q
[prec
].skblist
;
131 p
= skb_dequeue_tail(q
);
138 EXPORT_SYMBOL(brcmu_pktq_pdeq_tail
);
141 brcmu_pktq_pflush(struct pktq
*pq
, int prec
, bool dir
,
142 bool (*fn
)(struct sk_buff
*, void *), void *arg
)
144 struct sk_buff_head
*q
;
145 struct sk_buff
*p
, *next
;
147 q
= &pq
->q
[prec
].skblist
;
148 skb_queue_walk_safe(q
, p
, next
) {
149 if (fn
== NULL
|| (*fn
) (p
, arg
)) {
151 brcmu_pkt_buf_free_skb(p
);
156 EXPORT_SYMBOL(brcmu_pktq_pflush
);
158 void brcmu_pktq_flush(struct pktq
*pq
, bool dir
,
159 bool (*fn
)(struct sk_buff
*, void *), void *arg
)
162 for (prec
= 0; prec
< pq
->num_prec
; prec
++)
163 brcmu_pktq_pflush(pq
, prec
, dir
, fn
, arg
);
165 EXPORT_SYMBOL(brcmu_pktq_flush
);
167 void brcmu_pktq_init(struct pktq
*pq
, int num_prec
, int max_len
)
171 /* pq is variable size; only zero out what's requested */
173 offsetof(struct pktq
, q
) + (sizeof(struct pktq_prec
) * num_prec
));
175 pq
->num_prec
= (u16
) num_prec
;
177 pq
->max
= (u16
) max_len
;
179 for (prec
= 0; prec
< num_prec
; prec
++) {
180 pq
->q
[prec
].max
= pq
->max
;
181 skb_queue_head_init(&pq
->q
[prec
].skblist
);
184 EXPORT_SYMBOL(brcmu_pktq_init
);
186 struct sk_buff
*brcmu_pktq_peek_tail(struct pktq
*pq
, int *prec_out
)
193 for (prec
= 0; prec
< pq
->hi_prec
; prec
++)
194 if (!skb_queue_empty(&pq
->q
[prec
].skblist
))
200 return skb_peek_tail(&pq
->q
[prec
].skblist
);
202 EXPORT_SYMBOL(brcmu_pktq_peek_tail
);
204 /* Return sum of lengths of a specific set of precedences */
205 int brcmu_pktq_mlen(struct pktq
*pq
, uint prec_bmp
)
211 for (prec
= 0; prec
<= pq
->hi_prec
; prec
++)
212 if (prec_bmp
& (1 << prec
))
213 len
+= pq
->q
[prec
].skblist
.qlen
;
217 EXPORT_SYMBOL(brcmu_pktq_mlen
);
219 /* Priority dequeue from a specific set of precedences */
220 struct sk_buff
*brcmu_pktq_mdeq(struct pktq
*pq
, uint prec_bmp
,
223 struct sk_buff_head
*q
;
230 while ((prec
= pq
->hi_prec
) > 0 &&
231 skb_queue_empty(&pq
->q
[prec
].skblist
))
234 while ((prec_bmp
& (1 << prec
)) == 0 ||
235 skb_queue_empty(&pq
->q
[prec
].skblist
))
239 q
= &pq
->q
[prec
].skblist
;
251 EXPORT_SYMBOL(brcmu_pktq_mdeq
);
253 /* Produce a human-readable string for boardrev */
254 char *brcmu_boardrev_str(u32 brev
, char *buf
)
259 snprintf(buf
, BRCMU_BOARDREV_LEN
, "%d.%d",
260 (brev
& 0xf0) >> 4, brev
& 0xf);
262 c
= (brev
& 0xf000) == 0x1000 ? 'P' : 'A';
263 snprintf(buf
, BRCMU_BOARDREV_LEN
, "%c%03x", c
, brev
& 0xfff);
267 EXPORT_SYMBOL(brcmu_boardrev_str
);
269 char *brcmu_dotrev_str(u32 dotrev
, char *buf
)
274 snprintf(buf
, BRCMU_DOTREV_LEN
, "unknown");
277 dotval
[0] = (dotrev
>> 24) & 0xFF;
278 dotval
[1] = (dotrev
>> 16) & 0xFF;
279 dotval
[2] = (dotrev
>> 8) & 0xFF;
280 dotval
[3] = dotrev
& 0xFF;
283 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d.%d.%d", dotval
[0],
284 dotval
[1], dotval
[2], dotval
[3]);
286 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d.%d", dotval
[0],
287 dotval
[1], dotval
[2]);
289 snprintf(buf
, BRCMU_DOTREV_LEN
, "%d.%d", dotval
[0],
294 EXPORT_SYMBOL(brcmu_dotrev_str
);
297 /* pretty hex print a pkt buffer chain */
298 void brcmu_prpkt(const char *msg
, struct sk_buff
*p0
)
302 if (msg
&& (msg
[0] != '\0'))
303 pr_debug("%s:\n", msg
);
305 for (p
= p0
; p
; p
= p
->next
)
306 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, p
->data
, p
->len
);
308 EXPORT_SYMBOL(brcmu_prpkt
);
310 void brcmu_dbg_hex_dump(const void *data
, size_t size
, const char *fmt
, ...)
312 struct va_format vaf
;
320 pr_debug("%pV", &vaf
);
324 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET
, data
, size
);
326 EXPORT_SYMBOL(brcmu_dbg_hex_dump
);
328 #endif /* defined(DEBUG) */