1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (c) 2014 Broadcom Corporation
4 */
5
6
7 #include <linux/types.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <brcmu_utils.h>
11
12 #include "core.h"
13 #include "debug.h"
14 #include "bus.h"
15 #include "proto.h"
16 #include "flowring.h"
17 #include "msgbuf.h"
18 #include "common.h"
19
20
21 #define BRCMF_FLOWRING_HIGH 1024
22 #define BRCMF_FLOWRING_LOW (BRCMF_FLOWRING_HIGH - 256)
23 #define BRCMF_FLOWRING_INVALID_IFIDX 0xff
24
25 #define BRCMF_FLOWRING_HASH_AP(da, fifo, ifidx) (da[5] * 2 + fifo + ifidx * 16)
26 #define BRCMF_FLOWRING_HASH_STA(fifo, ifidx) (fifo + ifidx * 16)
27
28 static const u8 brcmf_flowring_prio2fifo[] = {
29 0,
30 1,
31 1,
32 0,
33 2,
34 2,
35 3,
36 3
37 };
38
39 static const u8 ALLFFMAC[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
40
41
42 static bool
brcmf_flowring_is_tdls_mac(struct brcmf_flowring * flow,u8 mac[ETH_ALEN])43 brcmf_flowring_is_tdls_mac(struct brcmf_flowring *flow, u8 mac[ETH_ALEN])
44 {
45 struct brcmf_flowring_tdls_entry *search;
46
47 search = flow->tdls_entry;
48
49 while (search) {
50 if (memcmp(search->mac, mac, ETH_ALEN) == 0)
51 return true;
52 search = search->next;
53 }
54
55 return false;
56 }
57
58
brcmf_flowring_lookup(struct brcmf_flowring * flow,u8 da[ETH_ALEN],u8 prio,u8 ifidx)59 u32 brcmf_flowring_lookup(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
60 u8 prio, u8 ifidx)
61 {
62 struct brcmf_flowring_hash *hash;
63 u16 hash_idx;
64 u32 i;
65 bool found;
66 bool sta;
67 u8 fifo;
68 #if defined(__linux__)
69 u8 *mac;
70 #elif defined(__FreeBSD__)
71 const u8 *mac;
72 #endif
73
74 fifo = brcmf_flowring_prio2fifo[prio];
75 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
76 mac = da;
77 if ((!sta) && (is_multicast_ether_addr(da))) {
78 #if defined(__linux__)
79 mac = (u8 *)ALLFFMAC;
80 #elif defined(__FreeBSD__)
81 mac = ALLFFMAC;
82 #endif
83 fifo = 0;
84 }
85 if ((sta) && (flow->tdls_active) &&
86 (brcmf_flowring_is_tdls_mac(flow, da))) {
87 sta = false;
88 }
89 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
90 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
91 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
92 found = false;
93 hash = flow->hash;
94 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
95 if ((sta || (memcmp(hash[hash_idx].mac, mac, ETH_ALEN) == 0)) &&
96 (hash[hash_idx].fifo == fifo) &&
97 (hash[hash_idx].ifidx == ifidx)) {
98 found = true;
99 break;
100 }
101 hash_idx++;
102 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
103 }
104 if (found)
105 return hash[hash_idx].flowid;
106
107 return BRCMF_FLOWRING_INVALID_ID;
108 }
109
110
brcmf_flowring_create(struct brcmf_flowring * flow,u8 da[ETH_ALEN],u8 prio,u8 ifidx)111 u32 brcmf_flowring_create(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
112 u8 prio, u8 ifidx)
113 {
114 struct brcmf_flowring_ring *ring;
115 struct brcmf_flowring_hash *hash;
116 u16 hash_idx;
117 u32 i;
118 bool found;
119 u8 fifo;
120 bool sta;
121 #if defined(__linux__)
122 u8 *mac;
123 #elif defined(__FreeBSD__)
124 const u8 *mac;
125 #endif
126
127 fifo = brcmf_flowring_prio2fifo[prio];
128 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
129 mac = da;
130 if ((!sta) && (is_multicast_ether_addr(da))) {
131 #if defined(__linux__)
132 mac = (u8 *)ALLFFMAC;
133 #elif defined(__FreeBSD__)
134 mac = ALLFFMAC;
135 #endif
136 fifo = 0;
137 }
138 if ((sta) && (flow->tdls_active) &&
139 (brcmf_flowring_is_tdls_mac(flow, da))) {
140 sta = false;
141 }
142 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
143 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
144 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
145 found = false;
146 hash = flow->hash;
147 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
148 if ((hash[hash_idx].ifidx == BRCMF_FLOWRING_INVALID_IFIDX) &&
149 (is_zero_ether_addr(hash[hash_idx].mac))) {
150 found = true;
151 break;
152 }
153 hash_idx++;
154 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
155 }
156 if (found) {
157 for (i = 0; i < flow->nrofrings; i++) {
158 if (flow->rings[i] == NULL)
159 break;
160 }
161 if (i == flow->nrofrings)
162 return -ENOMEM;
163
164 ring = kzalloc_obj(*ring, GFP_ATOMIC);
165 if (!ring)
166 return -ENOMEM;
167
168 memcpy(hash[hash_idx].mac, mac, ETH_ALEN);
169 hash[hash_idx].fifo = fifo;
170 hash[hash_idx].ifidx = ifidx;
171 hash[hash_idx].flowid = i;
172
173 ring->hash_id = hash_idx;
174 ring->status = RING_CLOSED;
175 skb_queue_head_init(&ring->skblist);
176 flow->rings[i] = ring;
177
178 return i;
179 }
180 return BRCMF_FLOWRING_INVALID_ID;
181 }
182
183
brcmf_flowring_tid(struct brcmf_flowring * flow,u16 flowid)184 u8 brcmf_flowring_tid(struct brcmf_flowring *flow, u16 flowid)
185 {
186 struct brcmf_flowring_ring *ring;
187
188 ring = flow->rings[flowid];
189
190 return flow->hash[ring->hash_id].fifo;
191 }
192
193
brcmf_flowring_block(struct brcmf_flowring * flow,u16 flowid,bool blocked)194 static void brcmf_flowring_block(struct brcmf_flowring *flow, u16 flowid,
195 bool blocked)
196 {
197 struct brcmf_flowring_ring *ring;
198 struct brcmf_bus *bus_if;
199 struct brcmf_pub *drvr;
200 struct brcmf_if *ifp;
201 bool currently_blocked;
202 int i;
203 u8 ifidx;
204 unsigned long flags;
205
206 spin_lock_irqsave(&flow->block_lock, flags);
207
208 ring = flow->rings[flowid];
209 if (ring->blocked == blocked) {
210 spin_unlock_irqrestore(&flow->block_lock, flags);
211 return;
212 }
213 ifidx = brcmf_flowring_ifidx_get(flow, flowid);
214
215 currently_blocked = false;
216 for (i = 0; i < flow->nrofrings; i++) {
217 if ((flow->rings[i]) && (i != flowid)) {
218 ring = flow->rings[i];
219 if ((ring->status == RING_OPEN) &&
220 (brcmf_flowring_ifidx_get(flow, i) == ifidx)) {
221 if (ring->blocked) {
222 currently_blocked = true;
223 break;
224 }
225 }
226 }
227 }
228 flow->rings[flowid]->blocked = blocked;
229 if (currently_blocked) {
230 spin_unlock_irqrestore(&flow->block_lock, flags);
231 return;
232 }
233
234 bus_if = dev_get_drvdata(flow->dev);
235 drvr = bus_if->drvr;
236 ifp = brcmf_get_ifp(drvr, ifidx);
237 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW, blocked);
238
239 spin_unlock_irqrestore(&flow->block_lock, flags);
240 }
241
242
brcmf_flowring_delete(struct brcmf_flowring * flow,u16 flowid)243 void brcmf_flowring_delete(struct brcmf_flowring *flow, u16 flowid)
244 {
245 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
246 struct brcmf_flowring_ring *ring;
247 struct brcmf_if *ifp;
248 u16 hash_idx;
249 u8 ifidx;
250 struct sk_buff *skb;
251
252 ring = flow->rings[flowid];
253 if (!ring)
254 return;
255
256 ifidx = brcmf_flowring_ifidx_get(flow, flowid);
257 ifp = brcmf_get_ifp(bus_if->drvr, ifidx);
258
259 brcmf_flowring_block(flow, flowid, false);
260 hash_idx = ring->hash_id;
261 flow->hash[hash_idx].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
262 eth_zero_addr(flow->hash[hash_idx].mac);
263 flow->rings[flowid] = NULL;
264
265 skb = skb_dequeue(&ring->skblist);
266 while (skb) {
267 brcmf_txfinalize(ifp, skb, false);
268 skb = skb_dequeue(&ring->skblist);
269 }
270
271 kfree(ring);
272 }
273
274
brcmf_flowring_enqueue(struct brcmf_flowring * flow,u16 flowid,struct sk_buff * skb)275 u32 brcmf_flowring_enqueue(struct brcmf_flowring *flow, u16 flowid,
276 struct sk_buff *skb)
277 {
278 struct brcmf_flowring_ring *ring;
279
280 ring = flow->rings[flowid];
281
282 skb_queue_tail(&ring->skblist, skb);
283
284 if (!ring->blocked &&
285 (skb_queue_len(&ring->skblist) > BRCMF_FLOWRING_HIGH)) {
286 brcmf_flowring_block(flow, flowid, true);
287 brcmf_dbg(MSGBUF, "Flowcontrol: BLOCK for ring %d\n", flowid);
288 /* To prevent (work around) possible race condition, check
289 * queue len again. It is also possible to use locking to
290 * protect, but that is undesirable for every enqueue and
291 * dequeue. This simple check will solve a possible race
292 * condition if it occurs.
293 */
294 if (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)
295 brcmf_flowring_block(flow, flowid, false);
296 }
297 return skb_queue_len(&ring->skblist);
298 }
299
300
brcmf_flowring_dequeue(struct brcmf_flowring * flow,u16 flowid)301 struct sk_buff *brcmf_flowring_dequeue(struct brcmf_flowring *flow, u16 flowid)
302 {
303 struct brcmf_flowring_ring *ring;
304 struct sk_buff *skb;
305
306 ring = flow->rings[flowid];
307 if (ring->status != RING_OPEN)
308 return NULL;
309
310 skb = skb_dequeue(&ring->skblist);
311
312 if (ring->blocked &&
313 (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)) {
314 brcmf_flowring_block(flow, flowid, false);
315 brcmf_dbg(MSGBUF, "Flowcontrol: OPEN for ring %d\n", flowid);
316 }
317
318 return skb;
319 }
320
321
brcmf_flowring_reinsert(struct brcmf_flowring * flow,u16 flowid,struct sk_buff * skb)322 void brcmf_flowring_reinsert(struct brcmf_flowring *flow, u16 flowid,
323 struct sk_buff *skb)
324 {
325 struct brcmf_flowring_ring *ring;
326
327 ring = flow->rings[flowid];
328
329 skb_queue_head(&ring->skblist, skb);
330 }
331
332
brcmf_flowring_qlen(struct brcmf_flowring * flow,u16 flowid)333 u32 brcmf_flowring_qlen(struct brcmf_flowring *flow, u16 flowid)
334 {
335 struct brcmf_flowring_ring *ring;
336
337 ring = flow->rings[flowid];
338 if (!ring)
339 return 0;
340
341 if (ring->status != RING_OPEN)
342 return 0;
343
344 return skb_queue_len(&ring->skblist);
345 }
346
347
brcmf_flowring_open(struct brcmf_flowring * flow,u16 flowid)348 void brcmf_flowring_open(struct brcmf_flowring *flow, u16 flowid)
349 {
350 struct brcmf_flowring_ring *ring;
351
352 ring = flow->rings[flowid];
353 if (!ring) {
354 brcmf_err("Ring NULL, for flowid %d\n", flowid);
355 return;
356 }
357
358 ring->status = RING_OPEN;
359 }
360
361
brcmf_flowring_ifidx_get(struct brcmf_flowring * flow,u16 flowid)362 u8 brcmf_flowring_ifidx_get(struct brcmf_flowring *flow, u16 flowid)
363 {
364 struct brcmf_flowring_ring *ring;
365 u16 hash_idx;
366
367 ring = flow->rings[flowid];
368 hash_idx = ring->hash_id;
369
370 return flow->hash[hash_idx].ifidx;
371 }
372
373
brcmf_flowring_attach(struct device * dev,u16 nrofrings)374 struct brcmf_flowring *brcmf_flowring_attach(struct device *dev, u16 nrofrings)
375 {
376 struct brcmf_flowring *flow;
377 u32 i;
378
379 flow = kzalloc_obj(*flow);
380 if (flow) {
381 flow->dev = dev;
382 flow->nrofrings = nrofrings;
383 spin_lock_init(&flow->block_lock);
384 for (i = 0; i < ARRAY_SIZE(flow->addr_mode); i++)
385 flow->addr_mode[i] = ADDR_INDIRECT;
386 for (i = 0; i < ARRAY_SIZE(flow->hash); i++)
387 flow->hash[i].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
388 flow->rings = kzalloc_objs(*flow->rings, nrofrings);
389 if (!flow->rings) {
390 kfree(flow);
391 flow = NULL;
392 }
393 }
394
395 return flow;
396 }
397
398
brcmf_flowring_detach(struct brcmf_flowring * flow)399 void brcmf_flowring_detach(struct brcmf_flowring *flow)
400 {
401 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
402 struct brcmf_pub *drvr = bus_if->drvr;
403 struct brcmf_flowring_tdls_entry *search;
404 struct brcmf_flowring_tdls_entry *remove;
405 u16 flowid;
406
407 for (flowid = 0; flowid < flow->nrofrings; flowid++) {
408 if (flow->rings[flowid])
409 brcmf_msgbuf_delete_flowring(drvr, flowid);
410 }
411
412 search = flow->tdls_entry;
413 while (search) {
414 remove = search;
415 search = search->next;
416 kfree(remove);
417 }
418 kfree(flow->rings);
419 kfree(flow);
420 }
421
422
brcmf_flowring_configure_addr_mode(struct brcmf_flowring * flow,int ifidx,enum proto_addr_mode addr_mode)423 void brcmf_flowring_configure_addr_mode(struct brcmf_flowring *flow, int ifidx,
424 enum proto_addr_mode addr_mode)
425 {
426 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
427 struct brcmf_pub *drvr = bus_if->drvr;
428 u32 i;
429 u16 flowid;
430
431 if (flow->addr_mode[ifidx] != addr_mode) {
432 for (i = 0; i < ARRAY_SIZE(flow->hash); i++) {
433 if (flow->hash[i].ifidx == ifidx) {
434 flowid = flow->hash[i].flowid;
435 if (flow->rings[flowid]->status != RING_OPEN)
436 continue;
437 brcmf_msgbuf_delete_flowring(drvr, flowid);
438 }
439 }
440 flow->addr_mode[ifidx] = addr_mode;
441 }
442 }
443
444
brcmf_flowring_delete_peer(struct brcmf_flowring * flow,int ifidx,u8 peer[ETH_ALEN])445 void brcmf_flowring_delete_peer(struct brcmf_flowring *flow, int ifidx,
446 #if defined(__linux__)
447 u8 peer[ETH_ALEN])
448 #elif defined(__FreeBSD__)
449 const u8 peer[ETH_ALEN])
450 #endif
451 {
452 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
453 struct brcmf_pub *drvr = bus_if->drvr;
454 struct brcmf_flowring_hash *hash;
455 struct brcmf_flowring_tdls_entry *prev;
456 struct brcmf_flowring_tdls_entry *search;
457 u32 i;
458 u16 flowid;
459 bool sta;
460
461 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
462
463 search = flow->tdls_entry;
464 prev = NULL;
465 while (search) {
466 if (memcmp(search->mac, peer, ETH_ALEN) == 0) {
467 sta = false;
468 break;
469 }
470 prev = search;
471 search = search->next;
472 }
473
474 hash = flow->hash;
475 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
476 if ((sta || (memcmp(hash[i].mac, peer, ETH_ALEN) == 0)) &&
477 (hash[i].ifidx == ifidx)) {
478 flowid = flow->hash[i].flowid;
479 if (flow->rings[flowid]->status == RING_OPEN)
480 brcmf_msgbuf_delete_flowring(drvr, flowid);
481 }
482 }
483
484 if (search) {
485 if (prev)
486 prev->next = search->next;
487 else
488 flow->tdls_entry = search->next;
489 kfree(search);
490 if (flow->tdls_entry == NULL)
491 flow->tdls_active = false;
492 }
493 }
494
495
brcmf_flowring_add_tdls_peer(struct brcmf_flowring * flow,int ifidx,u8 peer[ETH_ALEN])496 void brcmf_flowring_add_tdls_peer(struct brcmf_flowring *flow, int ifidx,
497 #if defined(__linux__)
498 u8 peer[ETH_ALEN])
499 #elif defined(__FreeBSD__)
500 const u8 peer[ETH_ALEN])
501 #endif
502 {
503 struct brcmf_flowring_tdls_entry *tdls_entry;
504 struct brcmf_flowring_tdls_entry *search;
505
506 tdls_entry = kzalloc_obj(*tdls_entry, GFP_ATOMIC);
507 if (tdls_entry == NULL)
508 return;
509
510 memcpy(tdls_entry->mac, peer, ETH_ALEN);
511 tdls_entry->next = NULL;
512 if (flow->tdls_entry == NULL) {
513 flow->tdls_entry = tdls_entry;
514 } else {
515 search = flow->tdls_entry;
516 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
517 goto free_entry;
518 while (search->next) {
519 search = search->next;
520 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
521 goto free_entry;
522 }
523 search->next = tdls_entry;
524 }
525
526 flow->tdls_active = true;
527 return;
528
529 free_entry:
530 kfree(tdls_entry);
531 }
532