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 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 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 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(sizeof(*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 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 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 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 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 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 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 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 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 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 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(sizeof(*flow), GFP_KERNEL); 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 = kcalloc(nrofrings, sizeof(*flow->rings), 389 GFP_KERNEL); 390 if (!flow->rings) { 391 kfree(flow); 392 flow = NULL; 393 } 394 } 395 396 return flow; 397 } 398 399 400 void brcmf_flowring_detach(struct brcmf_flowring *flow) 401 { 402 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev); 403 struct brcmf_pub *drvr = bus_if->drvr; 404 struct brcmf_flowring_tdls_entry *search; 405 struct brcmf_flowring_tdls_entry *remove; 406 u16 flowid; 407 408 for (flowid = 0; flowid < flow->nrofrings; flowid++) { 409 if (flow->rings[flowid]) 410 brcmf_msgbuf_delete_flowring(drvr, flowid); 411 } 412 413 search = flow->tdls_entry; 414 while (search) { 415 remove = search; 416 search = search->next; 417 kfree(remove); 418 } 419 kfree(flow->rings); 420 kfree(flow); 421 } 422 423 424 void brcmf_flowring_configure_addr_mode(struct brcmf_flowring *flow, int ifidx, 425 enum proto_addr_mode addr_mode) 426 { 427 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev); 428 struct brcmf_pub *drvr = bus_if->drvr; 429 u32 i; 430 u16 flowid; 431 432 if (flow->addr_mode[ifidx] != addr_mode) { 433 for (i = 0; i < ARRAY_SIZE(flow->hash); i++) { 434 if (flow->hash[i].ifidx == ifidx) { 435 flowid = flow->hash[i].flowid; 436 if (flow->rings[flowid]->status != RING_OPEN) 437 continue; 438 brcmf_msgbuf_delete_flowring(drvr, flowid); 439 } 440 } 441 flow->addr_mode[ifidx] = addr_mode; 442 } 443 } 444 445 446 void brcmf_flowring_delete_peer(struct brcmf_flowring *flow, int ifidx, 447 #if defined(__linux__) 448 u8 peer[ETH_ALEN]) 449 #elif defined(__FreeBSD__) 450 const u8 peer[ETH_ALEN]) 451 #endif 452 { 453 struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev); 454 struct brcmf_pub *drvr = bus_if->drvr; 455 struct brcmf_flowring_hash *hash; 456 struct brcmf_flowring_tdls_entry *prev; 457 struct brcmf_flowring_tdls_entry *search; 458 u32 i; 459 u16 flowid; 460 bool sta; 461 462 sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT); 463 464 search = flow->tdls_entry; 465 prev = NULL; 466 while (search) { 467 if (memcmp(search->mac, peer, ETH_ALEN) == 0) { 468 sta = false; 469 break; 470 } 471 prev = search; 472 search = search->next; 473 } 474 475 hash = flow->hash; 476 for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) { 477 if ((sta || (memcmp(hash[i].mac, peer, ETH_ALEN) == 0)) && 478 (hash[i].ifidx == ifidx)) { 479 flowid = flow->hash[i].flowid; 480 if (flow->rings[flowid]->status == RING_OPEN) 481 brcmf_msgbuf_delete_flowring(drvr, flowid); 482 } 483 } 484 485 if (search) { 486 if (prev) 487 prev->next = search->next; 488 else 489 flow->tdls_entry = search->next; 490 kfree(search); 491 if (flow->tdls_entry == NULL) 492 flow->tdls_active = false; 493 } 494 } 495 496 497 void brcmf_flowring_add_tdls_peer(struct brcmf_flowring *flow, int ifidx, 498 #if defined(__linux__) 499 u8 peer[ETH_ALEN]) 500 #elif defined(__FreeBSD__) 501 const u8 peer[ETH_ALEN]) 502 #endif 503 { 504 struct brcmf_flowring_tdls_entry *tdls_entry; 505 struct brcmf_flowring_tdls_entry *search; 506 507 tdls_entry = kzalloc(sizeof(*tdls_entry), GFP_ATOMIC); 508 if (tdls_entry == NULL) 509 return; 510 511 memcpy(tdls_entry->mac, peer, ETH_ALEN); 512 tdls_entry->next = NULL; 513 if (flow->tdls_entry == NULL) { 514 flow->tdls_entry = tdls_entry; 515 } else { 516 search = flow->tdls_entry; 517 if (memcmp(search->mac, peer, ETH_ALEN) == 0) 518 goto free_entry; 519 while (search->next) { 520 search = search->next; 521 if (memcmp(search->mac, peer, ETH_ALEN) == 0) 522 goto free_entry; 523 } 524 search->next = tdls_entry; 525 } 526 527 flow->tdls_active = true; 528 return; 529 530 free_entry: 531 kfree(tdls_entry); 532 } 533