flowring.c 11 KB

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