flowring.c 12 KB

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