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] * 2 + fifo + ifidx * 16)
36 #define BRCMF_FLOWRING_HASH_STA(fifo, ifidx) (fifo + ifidx * 16)
38 static const u8 brcmf_flowring_prio2fifo[] = {
49 static const u8 ALLFFMAC[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
53 brcmf_flowring_is_tdls_mac(struct brcmf_flowring *flow, u8 mac[ETH_ALEN])
55 struct brcmf_flowring_tdls_entry *search;
57 search = flow->tdls_entry;
60 if (memcmp(search->mac, mac, ETH_ALEN) == 0)
62 search = search->next;
69 u32 brcmf_flowring_lookup(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
72 struct brcmf_flowring_hash *hash;
80 fifo = brcmf_flowring_prio2fifo[prio];
81 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
83 if ((!sta) && (is_multicast_ether_addr(da))) {
87 if ((sta) && (flow->tdls_active) &&
88 (brcmf_flowring_is_tdls_mac(flow, da))) {
91 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
92 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
93 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
96 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
97 if ((sta || (memcmp(hash[hash_idx].mac, mac, ETH_ALEN) == 0)) &&
98 (hash[hash_idx].fifo == fifo) &&
99 (hash[hash_idx].ifidx == ifidx)) {
104 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
107 return hash[hash_idx].flowid;
109 return BRCMF_FLOWRING_INVALID_ID;
113 u32 brcmf_flowring_create(struct brcmf_flowring *flow, u8 da[ETH_ALEN],
116 struct brcmf_flowring_ring *ring;
117 struct brcmf_flowring_hash *hash;
125 fifo = brcmf_flowring_prio2fifo[prio];
126 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
128 if ((!sta) && (is_multicast_ether_addr(da))) {
129 mac = (u8 *)ALLFFMAC;
132 if ((sta) && (flow->tdls_active) &&
133 (brcmf_flowring_is_tdls_mac(flow, da))) {
136 hash_idx = sta ? BRCMF_FLOWRING_HASH_STA(fifo, ifidx) :
137 BRCMF_FLOWRING_HASH_AP(mac, fifo, ifidx);
138 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
141 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
142 if ((hash[hash_idx].ifidx == BRCMF_FLOWRING_INVALID_IFIDX) &&
143 (is_zero_ether_addr(hash[hash_idx].mac))) {
148 hash_idx &= (BRCMF_FLOWRING_HASHSIZE - 1);
151 for (i = 0; i < flow->nrofrings; i++) {
152 if (flow->rings[i] == NULL)
155 if (i == flow->nrofrings)
158 ring = kzalloc(sizeof(*ring), GFP_ATOMIC);
162 memcpy(hash[hash_idx].mac, mac, ETH_ALEN);
163 hash[hash_idx].fifo = fifo;
164 hash[hash_idx].ifidx = ifidx;
165 hash[hash_idx].flowid = i;
167 ring->hash_id = hash_idx;
168 ring->status = RING_CLOSED;
169 skb_queue_head_init(&ring->skblist);
170 flow->rings[i] = ring;
174 return BRCMF_FLOWRING_INVALID_ID;
178 u8 brcmf_flowring_tid(struct brcmf_flowring *flow, u16 flowid)
180 struct brcmf_flowring_ring *ring;
182 ring = flow->rings[flowid];
184 return flow->hash[ring->hash_id].fifo;
188 static void brcmf_flowring_block(struct brcmf_flowring *flow, u16 flowid,
191 struct brcmf_flowring_ring *ring;
192 struct brcmf_bus *bus_if;
193 struct brcmf_pub *drvr;
194 struct brcmf_if *ifp;
195 bool currently_blocked;
200 spin_lock_irqsave(&flow->block_lock, flags);
202 ring = flow->rings[flowid];
203 if (ring->blocked == blocked) {
204 spin_unlock_irqrestore(&flow->block_lock, flags);
207 ifidx = brcmf_flowring_ifidx_get(flow, flowid);
209 currently_blocked = false;
210 for (i = 0; i < flow->nrofrings; i++) {
211 if ((flow->rings[i]) && (i != flowid)) {
212 ring = flow->rings[i];
213 if ((ring->status == RING_OPEN) &&
214 (brcmf_flowring_ifidx_get(flow, i) == ifidx)) {
216 currently_blocked = true;
222 flow->rings[flowid]->blocked = blocked;
223 if (currently_blocked) {
224 spin_unlock_irqrestore(&flow->block_lock, flags);
228 bus_if = dev_get_drvdata(flow->dev);
230 ifp = brcmf_get_ifp(drvr, ifidx);
231 brcmf_txflowblock_if(ifp, BRCMF_NETIF_STOP_REASON_FLOW, blocked);
233 spin_unlock_irqrestore(&flow->block_lock, flags);
237 void brcmf_flowring_delete(struct brcmf_flowring *flow, u16 flowid)
239 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
240 struct brcmf_flowring_ring *ring;
241 struct brcmf_if *ifp;
246 ring = flow->rings[flowid];
250 ifidx = brcmf_flowring_ifidx_get(flow, flowid);
251 ifp = brcmf_get_ifp(bus_if->drvr, ifidx);
253 brcmf_flowring_block(flow, flowid, false);
254 hash_idx = ring->hash_id;
255 flow->hash[hash_idx].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
256 eth_zero_addr(flow->hash[hash_idx].mac);
257 flow->rings[flowid] = NULL;
259 skb = skb_dequeue(&ring->skblist);
261 brcmf_txfinalize(ifp, skb, false);
262 skb = skb_dequeue(&ring->skblist);
269 u32 brcmf_flowring_enqueue(struct brcmf_flowring *flow, u16 flowid,
272 struct brcmf_flowring_ring *ring;
274 ring = flow->rings[flowid];
276 skb_queue_tail(&ring->skblist, skb);
278 if (!ring->blocked &&
279 (skb_queue_len(&ring->skblist) > BRCMF_FLOWRING_HIGH)) {
280 brcmf_flowring_block(flow, flowid, true);
281 brcmf_dbg(MSGBUF, "Flowcontrol: BLOCK for ring %d\n", flowid);
282 /* To prevent (work around) possible race condition, check
283 * queue len again. It is also possible to use locking to
284 * protect, but that is undesirable for every enqueue and
285 * dequeue. This simple check will solve a possible race
286 * condition if it occurs.
288 if (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)
289 brcmf_flowring_block(flow, flowid, false);
291 return skb_queue_len(&ring->skblist);
295 struct sk_buff *brcmf_flowring_dequeue(struct brcmf_flowring *flow, u16 flowid)
297 struct brcmf_flowring_ring *ring;
300 ring = flow->rings[flowid];
301 if (ring->status != RING_OPEN)
304 skb = skb_dequeue(&ring->skblist);
307 (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)) {
308 brcmf_flowring_block(flow, flowid, false);
309 brcmf_dbg(MSGBUF, "Flowcontrol: OPEN for ring %d\n", flowid);
316 void brcmf_flowring_reinsert(struct brcmf_flowring *flow, u16 flowid,
319 struct brcmf_flowring_ring *ring;
321 ring = flow->rings[flowid];
323 skb_queue_head(&ring->skblist, skb);
327 u32 brcmf_flowring_qlen(struct brcmf_flowring *flow, u16 flowid)
329 struct brcmf_flowring_ring *ring;
331 ring = flow->rings[flowid];
335 if (ring->status != RING_OPEN)
338 return skb_queue_len(&ring->skblist);
342 void brcmf_flowring_open(struct brcmf_flowring *flow, u16 flowid)
344 struct brcmf_flowring_ring *ring;
346 ring = flow->rings[flowid];
348 brcmf_err("Ring NULL, for flowid %d\n", flowid);
352 ring->status = RING_OPEN;
356 u8 brcmf_flowring_ifidx_get(struct brcmf_flowring *flow, u16 flowid)
358 struct brcmf_flowring_ring *ring;
361 ring = flow->rings[flowid];
362 hash_idx = ring->hash_id;
364 return flow->hash[hash_idx].ifidx;
368 struct brcmf_flowring *brcmf_flowring_attach(struct device *dev, u16 nrofrings)
370 struct brcmf_flowring *flow;
373 flow = kzalloc(sizeof(*flow), GFP_KERNEL);
376 flow->nrofrings = nrofrings;
377 spin_lock_init(&flow->block_lock);
378 for (i = 0; i < ARRAY_SIZE(flow->addr_mode); i++)
379 flow->addr_mode[i] = ADDR_INDIRECT;
380 for (i = 0; i < ARRAY_SIZE(flow->hash); i++)
381 flow->hash[i].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
382 flow->rings = kcalloc(nrofrings, sizeof(*flow->rings),
394 void brcmf_flowring_detach(struct brcmf_flowring *flow)
396 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
397 struct brcmf_pub *drvr = bus_if->drvr;
398 struct brcmf_flowring_tdls_entry *search;
399 struct brcmf_flowring_tdls_entry *remove;
402 for (flowid = 0; flowid < flow->nrofrings; flowid++) {
403 if (flow->rings[flowid])
404 brcmf_msgbuf_delete_flowring(drvr, flowid);
407 search = flow->tdls_entry;
410 search = search->next;
418 void brcmf_flowring_configure_addr_mode(struct brcmf_flowring *flow, int ifidx,
419 enum proto_addr_mode addr_mode)
421 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
422 struct brcmf_pub *drvr = bus_if->drvr;
426 if (flow->addr_mode[ifidx] != addr_mode) {
427 for (i = 0; i < ARRAY_SIZE(flow->hash); i++) {
428 if (flow->hash[i].ifidx == ifidx) {
429 flowid = flow->hash[i].flowid;
430 if (flow->rings[flowid]->status != RING_OPEN)
432 flow->rings[flowid]->status = RING_CLOSING;
433 brcmf_msgbuf_delete_flowring(drvr, flowid);
436 flow->addr_mode[ifidx] = addr_mode;
441 void brcmf_flowring_delete_peer(struct brcmf_flowring *flow, int ifidx,
444 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
445 struct brcmf_pub *drvr = bus_if->drvr;
446 struct brcmf_flowring_hash *hash;
447 struct brcmf_flowring_tdls_entry *prev;
448 struct brcmf_flowring_tdls_entry *search;
453 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
455 search = flow->tdls_entry;
458 if (memcmp(search->mac, peer, ETH_ALEN) == 0) {
463 search = search->next;
467 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
468 if ((sta || (memcmp(hash[i].mac, peer, ETH_ALEN) == 0)) &&
469 (hash[i].ifidx == ifidx)) {
470 flowid = flow->hash[i].flowid;
471 if (flow->rings[flowid]->status == RING_OPEN) {
472 flow->rings[flowid]->status = RING_CLOSING;
473 brcmf_msgbuf_delete_flowring(drvr, flowid);
480 prev->next = search->next;
482 flow->tdls_entry = search->next;
484 if (flow->tdls_entry == NULL)
485 flow->tdls_active = false;
490 void brcmf_flowring_add_tdls_peer(struct brcmf_flowring *flow, int ifidx,
493 struct brcmf_flowring_tdls_entry *tdls_entry;
494 struct brcmf_flowring_tdls_entry *search;
496 tdls_entry = kzalloc(sizeof(*tdls_entry), GFP_ATOMIC);
497 if (tdls_entry == NULL)
500 memcpy(tdls_entry->mac, peer, ETH_ALEN);
501 tdls_entry->next = NULL;
502 if (flow->tdls_entry == NULL) {
503 flow->tdls_entry = tdls_entry;
505 search = flow->tdls_entry;
506 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
508 while (search->next) {
509 search = search->next;
510 if (memcmp(search->mac, peer, ETH_ALEN) == 0)
513 search->next = tdls_entry;
516 flow->tdls_active = true;