1 /* Copyright (c) 2014 Broadcom Corporation
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <linux/types.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <brcmu_utils.h>
31 #define BRCMF_FLOWRING_HIGH 1024
32 #define BRCMF_FLOWRING_LOW (BRCMF_FLOWRING_HIGH - 256)
33 #define BRCMF_FLOWRING_INVALID_IFIDX 0xff
35 #define BRCMF_FLOWRING_HASH_AP(da, fifo, ifidx) (da[5] + fifo + ifidx * 16)
36 #define BRCMF_FLOWRING_HASH_STA(fifo, ifidx) (fifo + ifidx * 16)
38 static const u8 brcmf_flowring_prio2fifo[] = {
51 brcmf_flowring_is_tdls_mac(struct brcmf_flowring *flow, u8 mac[ETH_ALEN])
53 struct brcmf_flowring_tdls_entry *search;
55 search = flow->tdls_entry;
58 if (memcmp(search->mac, mac, ETH_ALEN) == 0)
60 search = search->next;
67 u32 brcmf_flowring_lookup(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
70 struct brcmf_flowring_hash *hash;
78 fifo = brcmf_flowring_prio2fifo[prio];
79 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
81 if ((!sta) && (is_multicast_ether_addr(da))) {
85 if ((sta) && (flow->tdls_active) &&
86 (brcmf_flowring_is_tdls_mac(flow, da))) {
89 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
90 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
93 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
94 if ((sta || (memcmp(hash[hash_idx].mac, mac, ETH_ALEN) == 0)) &&
95 (hash[hash_idx].fifo == fifo) &&
96 (hash[hash_idx].ifidx == ifidx)) {
103 return hash[hash_idx].flowid;
105 return BRCMF_FLOWRING_INVALID_ID;
109 u32 brcmf_flowring_create(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
112 struct brcmf_flowring_ring *ring;
113 struct brcmf_flowring_hash *hash;
121 fifo = brcmf_flowring_prio2fifo[prio];
122 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
124 if ((!sta) && (is_multicast_ether_addr(da))) {
125 mac = (u8 *)ALLFFMAC;
128 if ((sta) && (flow->tdls_active) &&
129 (brcmf_flowring_is_tdls_mac(flow, da))) {
132 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
133 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
136 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
137 if ((hash[hash_idx].ifidx == BRCMF_FLOWRING_INVALID_IFIDX) &&
138 (is_zero_ether_addr(hash[hash_idx].mac))) {
145 for (i = 0; i < flow->nrofrings; i++) {
146 if (flow->rings[i] == NULL)
149 if (i == flow->nrofrings)
152 ring = kzalloc(sizeof(*ring), GFP_ATOMIC);
156 memcpy(hash[hash_idx].mac, mac, ETH_ALEN);
157 hash[hash_idx].fifo = fifo;
158 hash[hash_idx].ifidx = ifidx;
159 hash[hash_idx].flowid = i;
161 ring->hash_id = hash_idx;
162 ring->status = RING_CLOSED;
163 skb_queue_head_init(&ring->skblist);
164 flow->rings[i] = ring;
168 return BRCMF_FLOWRING_INVALID_ID;
172 u8 brcmf_flowring_tid(struct brcmf_flowring *flow, u8 flowid)
174 struct brcmf_flowring_ring *ring;
176 ring = flow->rings[flowid];
178 return flow->hash[ring->hash_id].fifo;
182 static void brcmf_flowring_block(struct brcmf_flowring *flow, u8 flowid,
185 struct brcmf_flowring_ring *ring;
186 struct brcmf_bus *bus_if;
187 struct brcmf_pub *drvr;
188 struct brcmf_if *ifp;
189 bool currently_blocked;
194 spin_lock_irqsave(&flow->block_lock, flags);
196 ring = flow->rings[flowid];
197 if (ring->blocked == blocked) {
198 spin_unlock_irqrestore(&flow->block_lock, flags);
201 ifidx = brcmf_flowring_ifidx_get(flow, flowid);
203 currently_blocked = false;
204 for (i = 0; i < flow->nrofrings; i++) {
205 if ((flow->rings[i]) && (i != flowid)) {
206 ring = flow->rings[i];
207 if ((ring->status == RING_OPEN) &&
208 (brcmf_flowring_ifidx_get(flow, i) == ifidx)) {
210 currently_blocked = true;
216 flow->rings[flowid]->blocked = blocked;
217 if (currently_blocked) {
218 spin_unlock_irqrestore(&flow->block_lock, flags);
222 bus_if = dev_get_drvdata(flow->dev);
224 ifp = brcmf_get_ifp(drvr, ifidx);
225 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW, blocked);
227 spin_unlock_irqrestore(&flow->block_lock, flags);
231 void brcmf_flowring_delete(struct brcmf_flowring *flow, u8 flowid)
233 struct brcmf_flowring_ring *ring;
237 ring = flow->rings[flowid];
240 brcmf_flowring_block(flow, flowid, false);
241 hash_idx = ring->hash_id;
242 flow->hash[hash_idx].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
243 eth_zero_addr(flow->hash[hash_idx].mac);
244 flow->rings[flowid] = NULL;
246 skb = skb_dequeue(&ring->skblist);
248 brcmu_pkt_buf_free_skb(skb);
249 skb = skb_dequeue(&ring->skblist);
256 u32 brcmf_flowring_enqueue(struct brcmf_flowring *flow, u8 flowid,
259 struct brcmf_flowring_ring *ring;
261 ring = flow->rings[flowid];
263 skb_queue_tail(&ring->skblist, skb);
265 if (!ring->blocked &&
266 (skb_queue_len(&ring->skblist) > BRCMF_FLOWRING_HIGH)) {
267 brcmf_flowring_block(flow, flowid, true);
268 brcmf_dbg(MSGBUF, "Flowcontrol: BLOCK for ring %d\n", flowid);
269 /* To prevent (work around) possible race condition, check
270 * queue len again. It is also possible to use locking to
271 * protect, but that is undesirable for every enqueue and
272 * dequeue. This simple check will solve a possible race
273 * condition if it occurs.
275 if (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)
276 brcmf_flowring_block(flow, flowid, false);
278 return skb_queue_len(&ring->skblist);
282 struct sk_buff *brcmf_flowring_dequeue(struct brcmf_flowring *flow, u8 flowid)
284 struct brcmf_flowring_ring *ring;
287 ring = flow->rings[flowid];
288 if (ring->status != RING_OPEN)
291 skb = skb_dequeue(&ring->skblist);
294 (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)) {
295 brcmf_flowring_block(flow, flowid, false);
296 brcmf_dbg(MSGBUF, "Flowcontrol: OPEN for ring %d\n", flowid);
303 void brcmf_flowring_reinsert(struct brcmf_flowring *flow, u8 flowid,
306 struct brcmf_flowring_ring *ring;
308 ring = flow->rings[flowid];
310 skb_queue_head(&ring->skblist, skb);
314 u32 brcmf_flowring_qlen(struct brcmf_flowring *flow, u8 flowid)
316 struct brcmf_flowring_ring *ring;
318 ring = flow->rings[flowid];
322 if (ring->status != RING_OPEN)
325 return skb_queue_len(&ring->skblist);
329 void brcmf_flowring_open(struct brcmf_flowring *flow, u8 flowid)
331 struct brcmf_flowring_ring *ring;
333 ring = flow->rings[flowid];
335 brcmf_err("Ring NULL, for flowid %d\n", flowid);
339 ring->status = RING_OPEN;
343 u8 brcmf_flowring_ifidx_get(struct brcmf_flowring *flow, u8 flowid)
345 struct brcmf_flowring_ring *ring;
348 ring = flow->rings[flowid];
349 hash_idx = ring->hash_id;
351 return flow->hash[hash_idx].ifidx;
355 struct brcmf_flowring *brcmf_flowring_attach(struct device *dev, u16 nrofrings)
357 struct brcmf_flowring *flow;
360 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
363 flow->nrofrings = nrofrings;
364 spin_lock_init(&flow->block_lock);
365 for (i = 0; i < ARRAY_SIZE(flow->addr_mode); i++)
366 flow->addr_mode[i] = ADDR_INDIRECT;
367 for (i = 0; i < ARRAY_SIZE(flow->hash); i++)
368 flow->hash[i].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
369 flow->rings = kcalloc(nrofrings, sizeof(*flow->rings),
381 void brcmf_flowring_detach(struct brcmf_flowring *flow)
383 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
384 struct brcmf_pub *drvr = bus_if->drvr;
385 struct brcmf_flowring_tdls_entry *search;
386 struct brcmf_flowring_tdls_entry *remove;
389 for (flowid = 0; flowid < flow->nrofrings; flowid++) {
390 if (flow->rings[flowid])
391 brcmf_msgbuf_delete_flowring(drvr, flowid);
394 search = flow->tdls_entry;
397 search = search->next;
405 void brcmf_flowring_configure_addr_mode(struct brcmf_flowring *flow, int ifidx,
406 enum proto_addr_mode addr_mode)
408 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
409 struct brcmf_pub *drvr = bus_if->drvr;
413 if (flow->addr_mode[ifidx] != addr_mode) {
414 for (i = 0; i < ARRAY_SIZE(flow->hash); i++) {
415 if (flow->hash[i].ifidx == ifidx) {
416 flowid = flow->hash[i].flowid;
417 if (flow->rings[flowid]->status != RING_OPEN)
419 flow->rings[flowid]->status = RING_CLOSING;
420 brcmf_msgbuf_delete_flowring(drvr, flowid);
423 flow->addr_mode[ifidx] = addr_mode;
428 void brcmf_flowring_delete_peer(struct brcmf_flowring *flow, int ifidx,
431 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
432 struct brcmf_pub *drvr = bus_if->drvr;
433 struct brcmf_flowring_hash *hash;
434 struct brcmf_flowring_tdls_entry *prev;
435 struct brcmf_flowring_tdls_entry *search;
440 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
442 search = flow->tdls_entry;
445 if (memcmp(search->mac, peer, ETH_ALEN) == 0) {
450 search = search->next;
454 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
455 if ((sta || (memcmp(hash[i].mac, peer, ETH_ALEN) == 0)) &&
456 (hash[i].ifidx == ifidx)) {
457 flowid = flow->hash[i].flowid;
458 if (flow->rings[flowid]->status == RING_OPEN) {
459 flow->rings[flowid]->status = RING_CLOSING;
460 brcmf_msgbuf_delete_flowring(drvr, flowid);
467 prev->next = search->next;
469 flow->tdls_entry = search->next;
471 if (flow->tdls_entry == NULL)
472 flow->tdls_active = false;
477 void brcmf_flowring_add_tdls_peer(struct brcmf_flowring *flow, int ifidx,
480 struct brcmf_flowring_tdls_entry *tdls_entry;
481 struct brcmf_flowring_tdls_entry *search;
483 tdls_entry = kzalloc(sizeof(*tdls_entry), GFP_ATOMIC);
484 if (tdls_entry == NULL)
487 memcpy(tdls_entry->mac, peer, ETH_ALEN);
488 tdls_entry->next = NULL;
489 if (flow->tdls_entry == NULL) {
490 flow->tdls_entry = tdls_entry;
492 search = flow->tdls_entry;
493 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
495 while (search->next) {
496 search = search->next;
497 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
500 search->next = tdls_entry;
503 flow->tdls_active = true;