flowring.c 12 KB

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