flowring.c 11 KB

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