Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 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 | |
| 17 | #include <linux/netdevice.h> |
Stephen Rothwell | b7a57e7 | 2011-10-12 21:35:07 +0200 | [diff] [blame] | 18 | #include <linux/module.h> |
Arend van Spriel | 20e5ca1 | 2011-10-18 14:03:09 +0200 | [diff] [blame] | 19 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 20 | #include <brcmu_utils.h> |
| 21 | |
| 22 | MODULE_AUTHOR("Broadcom Corporation"); |
| 23 | MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN driver utilities."); |
| 24 | MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN cards"); |
| 25 | MODULE_LICENSE("Dual BSD/GPL"); |
| 26 | |
| 27 | struct 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 | } |
| 39 | EXPORT_SYMBOL(brcmu_pkt_buf_get_skb); |
| 40 | |
| 41 | /* Free the driver packet. Free the tag if present */ |
| 42 | void brcmu_pkt_buf_free_skb(struct sk_buff *skb) |
| 43 | { |
| 44 | struct sk_buff *nskb; |
| 45 | int nest = 0; |
| 46 | |
| 47 | /* perversion: we use skb->next to chain multi-skb packets */ |
| 48 | while (skb) { |
| 49 | nskb = skb->next; |
| 50 | skb->next = NULL; |
| 51 | |
| 52 | if (skb->destructor) |
| 53 | /* cannot kfree_skb() on hard IRQ (net/core/skbuff.c) if |
| 54 | * destructor exists |
| 55 | */ |
| 56 | dev_kfree_skb_any(skb); |
| 57 | else |
| 58 | /* can free immediately (even in_irq()) if destructor |
| 59 | * does not exist |
| 60 | */ |
| 61 | dev_kfree_skb(skb); |
| 62 | |
| 63 | nest++; |
| 64 | skb = nskb; |
| 65 | } |
| 66 | } |
| 67 | EXPORT_SYMBOL(brcmu_pkt_buf_free_skb); |
| 68 | |
| 69 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 70 | /* return total length of buffer chain */ |
| 71 | uint brcmu_pkttotlen(struct sk_buff *p) |
| 72 | { |
| 73 | uint total; |
| 74 | |
| 75 | total = 0; |
| 76 | for (; p; p = p->next) |
| 77 | total += p->len; |
| 78 | return total; |
| 79 | } |
| 80 | EXPORT_SYMBOL(brcmu_pkttotlen); |
| 81 | |
| 82 | /* |
| 83 | * osl multiple-precedence packet queue |
| 84 | * hi_prec is always >= the number of the highest non-empty precedence |
| 85 | */ |
| 86 | struct sk_buff *brcmu_pktq_penq(struct pktq *pq, int prec, |
| 87 | struct sk_buff *p) |
| 88 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 89 | struct sk_buff_head *q; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 90 | |
| 91 | if (pktq_full(pq) || pktq_pfull(pq, prec)) |
| 92 | return NULL; |
| 93 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 94 | q = &pq->q[prec].skblist; |
| 95 | skb_queue_tail(q, p); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 96 | pq->len++; |
| 97 | |
| 98 | if (pq->hi_prec < prec) |
| 99 | pq->hi_prec = (u8) prec; |
| 100 | |
| 101 | return p; |
| 102 | } |
| 103 | EXPORT_SYMBOL(brcmu_pktq_penq); |
| 104 | |
| 105 | struct sk_buff *brcmu_pktq_penq_head(struct pktq *pq, int prec, |
| 106 | struct sk_buff *p) |
| 107 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 108 | struct sk_buff_head *q; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 109 | |
| 110 | if (pktq_full(pq) || pktq_pfull(pq, prec)) |
| 111 | return NULL; |
| 112 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 113 | q = &pq->q[prec].skblist; |
| 114 | skb_queue_head(q, p); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 115 | pq->len++; |
| 116 | |
| 117 | if (pq->hi_prec < prec) |
| 118 | pq->hi_prec = (u8) prec; |
| 119 | |
| 120 | return p; |
| 121 | } |
| 122 | EXPORT_SYMBOL(brcmu_pktq_penq_head); |
| 123 | |
| 124 | struct sk_buff *brcmu_pktq_pdeq(struct pktq *pq, int prec) |
| 125 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 126 | struct sk_buff_head *q; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 127 | struct sk_buff *p; |
| 128 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 129 | q = &pq->q[prec].skblist; |
| 130 | p = skb_dequeue(q); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 131 | if (p == NULL) |
| 132 | return NULL; |
| 133 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 134 | pq->len--; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 135 | return p; |
| 136 | } |
| 137 | EXPORT_SYMBOL(brcmu_pktq_pdeq); |
| 138 | |
| 139 | struct sk_buff *brcmu_pktq_pdeq_tail(struct pktq *pq, int prec) |
| 140 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 141 | struct sk_buff_head *q; |
| 142 | struct sk_buff *p; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 143 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 144 | q = &pq->q[prec].skblist; |
| 145 | p = skb_dequeue_tail(q); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 146 | if (p == NULL) |
| 147 | return NULL; |
| 148 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 149 | pq->len--; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 150 | return p; |
| 151 | } |
| 152 | EXPORT_SYMBOL(brcmu_pktq_pdeq_tail); |
| 153 | |
| 154 | void |
| 155 | brcmu_pktq_pflush(struct pktq *pq, int prec, bool dir, |
| 156 | bool (*fn)(struct sk_buff *, void *), void *arg) |
| 157 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 158 | struct sk_buff_head *q; |
| 159 | struct sk_buff *p, *next; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 160 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 161 | q = &pq->q[prec].skblist; |
| 162 | skb_queue_walk_safe(q, p, next) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 163 | if (fn == NULL || (*fn) (p, arg)) { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 164 | skb_unlink(p, q); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 165 | brcmu_pkt_buf_free_skb(p); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 166 | pq->len--; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 169 | } |
| 170 | EXPORT_SYMBOL(brcmu_pktq_pflush); |
| 171 | |
| 172 | void brcmu_pktq_flush(struct pktq *pq, bool dir, |
| 173 | bool (*fn)(struct sk_buff *, void *), void *arg) |
| 174 | { |
| 175 | int prec; |
| 176 | for (prec = 0; prec < pq->num_prec; prec++) |
| 177 | brcmu_pktq_pflush(pq, prec, dir, fn, arg); |
| 178 | } |
| 179 | EXPORT_SYMBOL(brcmu_pktq_flush); |
| 180 | |
| 181 | void brcmu_pktq_init(struct pktq *pq, int num_prec, int max_len) |
| 182 | { |
| 183 | int prec; |
| 184 | |
| 185 | /* pq is variable size; only zero out what's requested */ |
| 186 | memset(pq, 0, |
| 187 | offsetof(struct pktq, q) + (sizeof(struct pktq_prec) * num_prec)); |
| 188 | |
| 189 | pq->num_prec = (u16) num_prec; |
| 190 | |
| 191 | pq->max = (u16) max_len; |
| 192 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 193 | for (prec = 0; prec < num_prec; prec++) { |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 194 | pq->q[prec].max = pq->max; |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 195 | skb_queue_head_init(&pq->q[prec].skblist); |
| 196 | } |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 197 | } |
| 198 | EXPORT_SYMBOL(brcmu_pktq_init); |
| 199 | |
| 200 | struct sk_buff *brcmu_pktq_peek_tail(struct pktq *pq, int *prec_out) |
| 201 | { |
| 202 | int prec; |
| 203 | |
| 204 | if (pq->len == 0) |
| 205 | return NULL; |
| 206 | |
| 207 | for (prec = 0; prec < pq->hi_prec; prec++) |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 208 | if (!skb_queue_empty(&pq->q[prec].skblist)) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 209 | break; |
| 210 | |
| 211 | if (prec_out) |
| 212 | *prec_out = prec; |
| 213 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 214 | return skb_peek_tail(&pq->q[prec].skblist); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 215 | } |
| 216 | EXPORT_SYMBOL(brcmu_pktq_peek_tail); |
| 217 | |
| 218 | /* Return sum of lengths of a specific set of precedences */ |
| 219 | int brcmu_pktq_mlen(struct pktq *pq, uint prec_bmp) |
| 220 | { |
| 221 | int prec, len; |
| 222 | |
| 223 | len = 0; |
| 224 | |
| 225 | for (prec = 0; prec <= pq->hi_prec; prec++) |
| 226 | if (prec_bmp & (1 << prec)) |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 227 | len += pq->q[prec].skblist.qlen; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 228 | |
| 229 | return len; |
| 230 | } |
| 231 | EXPORT_SYMBOL(brcmu_pktq_mlen); |
| 232 | |
| 233 | /* Priority dequeue from a specific set of precedences */ |
| 234 | struct sk_buff *brcmu_pktq_mdeq(struct pktq *pq, uint prec_bmp, |
| 235 | int *prec_out) |
| 236 | { |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 237 | struct sk_buff_head *q; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 238 | struct sk_buff *p; |
| 239 | int prec; |
| 240 | |
| 241 | if (pq->len == 0) |
| 242 | return NULL; |
| 243 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 244 | while ((prec = pq->hi_prec) > 0 && |
| 245 | skb_queue_empty(&pq->q[prec].skblist)) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 246 | pq->hi_prec--; |
| 247 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 248 | while ((prec_bmp & (1 << prec)) == 0 || |
| 249 | skb_queue_empty(&pq->q[prec].skblist)) |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 250 | if (prec-- == 0) |
| 251 | return NULL; |
| 252 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 253 | q = &pq->q[prec].skblist; |
| 254 | p = skb_dequeue(q); |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 255 | if (p == NULL) |
| 256 | return NULL; |
| 257 | |
Arend van Spriel | ad3b8b3 | 2011-11-10 20:30:27 +0100 | [diff] [blame^] | 258 | pq->len--; |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 259 | |
| 260 | if (prec_out) |
| 261 | *prec_out = prec; |
| 262 | |
Arend van Spriel | 5b435de | 2011-10-05 13:19:03 +0200 | [diff] [blame] | 263 | return p; |
| 264 | } |
| 265 | EXPORT_SYMBOL(brcmu_pktq_mdeq); |
| 266 | |
| 267 | #if defined(BCMDBG) |
| 268 | /* pretty hex print a pkt buffer chain */ |
| 269 | void brcmu_prpkt(const char *msg, struct sk_buff *p0) |
| 270 | { |
| 271 | struct sk_buff *p; |
| 272 | |
| 273 | if (msg && (msg[0] != '\0')) |
| 274 | printk(KERN_DEBUG "%s:\n", msg); |
| 275 | |
| 276 | for (p = p0; p; p = p->next) |
| 277 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, p->data, p->len); |
| 278 | } |
| 279 | EXPORT_SYMBOL(brcmu_prpkt); |
| 280 | #endif /* defined(BCMDBG) */ |