flowring.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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_flowring_ring *ring;
  202. u16 hash_idx;
  203. struct sk_buff *skb;
  204. ring = flow->rings[flowid];
  205. if (!ring)
  206. return;
  207. brcmf_flowring_block(flow, flowid, false);
  208. hash_idx = ring->hash_id;
  209. flow->hash[hash_idx].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
  210. eth_zero_addr(flow->hash[hash_idx].mac);
  211. flow->rings[flowid] = NULL;
  212. skb = skb_dequeue(&ring->skblist);
  213. while (skb) {
  214. brcmu_pkt_buf_free_skb(skb);
  215. skb = skb_dequeue(&ring->skblist);
  216. }
  217. kfree(ring);
  218. }
  219. u32 brcmf_flowring_enqueue(struct brcmf_flowring *flow, u16 flowid,
  220. struct sk_buff *skb)
  221. {
  222. struct brcmf_flowring_ring *ring;
  223. ring = flow->rings[flowid];
  224. skb_queue_tail(&ring->skblist, skb);
  225. if (!ring->blocked &&
  226. (skb_queue_len(&ring->skblist) > BRCMF_FLOWRING_HIGH)) {
  227. brcmf_flowring_block(flow, flowid, true);
  228. brcmf_dbg(MSGBUF, "Flowcontrol: BLOCK for ring %d\n", flowid);
  229. /* To prevent (work around) possible race condition, check
  230. * queue len again. It is also possible to use locking to
  231. * protect, but that is undesirable for every enqueue and
  232. * dequeue. This simple check will solve a possible race
  233. * condition if it occurs.
  234. */
  235. if (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)
  236. brcmf_flowring_block(flow, flowid, false);
  237. }
  238. return skb_queue_len(&ring->skblist);
  239. }
  240. struct sk_buff *brcmf_flowring_dequeue(struct brcmf_flowring *flow, u16 flowid)
  241. {
  242. struct brcmf_flowring_ring *ring;
  243. struct sk_buff *skb;
  244. ring = flow->rings[flowid];
  245. if (ring->status != RING_OPEN)
  246. return NULL;
  247. skb = skb_dequeue(&ring->skblist);
  248. if (ring->blocked &&
  249. (skb_queue_len(&ring->skblist) < BRCMF_FLOWRING_LOW)) {
  250. brcmf_flowring_block(flow, flowid, false);
  251. brcmf_dbg(MSGBUF, "Flowcontrol: OPEN for ring %d\n", flowid);
  252. }
  253. return skb;
  254. }
  255. void brcmf_flowring_reinsert(struct brcmf_flowring *flow, u16 flowid,
  256. struct sk_buff *skb)
  257. {
  258. struct brcmf_flowring_ring *ring;
  259. ring = flow->rings[flowid];
  260. skb_queue_head(&ring->skblist, skb);
  261. }
  262. u32 brcmf_flowring_qlen(struct brcmf_flowring *flow, u16 flowid)
  263. {
  264. struct brcmf_flowring_ring *ring;
  265. ring = flow->rings[flowid];
  266. if (!ring)
  267. return 0;
  268. if (ring->status != RING_OPEN)
  269. return 0;
  270. return skb_queue_len(&ring->skblist);
  271. }
  272. void brcmf_flowring_open(struct brcmf_flowring *flow, u16 flowid)
  273. {
  274. struct brcmf_flowring_ring *ring;
  275. ring = flow->rings[flowid];
  276. if (!ring) {
  277. brcmf_err("Ring NULL, for flowid %d\n", flowid);
  278. return;
  279. }
  280. ring->status = RING_OPEN;
  281. }
  282. u8 brcmf_flowring_ifidx_get(struct brcmf_flowring *flow, u16 flowid)
  283. {
  284. struct brcmf_flowring_ring *ring;
  285. u16 hash_idx;
  286. ring = flow->rings[flowid];
  287. hash_idx = ring->hash_id;
  288. return flow->hash[hash_idx].ifidx;
  289. }
  290. struct brcmf_flowring *brcmf_flowring_attach(struct device *dev, u16 nrofrings)
  291. {
  292. struct brcmf_flowring *flow;
  293. u32 i;
  294. flow = kzalloc(sizeof(*flow), GFP_KERNEL);
  295. if (flow) {
  296. flow->dev = dev;
  297. flow->nrofrings = nrofrings;
  298. spin_lock_init(&flow->block_lock);
  299. for (i = 0; i < ARRAY_SIZE(flow->addr_mode); i++)
  300. flow->addr_mode[i] = ADDR_INDIRECT;
  301. for (i = 0; i < ARRAY_SIZE(flow->hash); i++)
  302. flow->hash[i].ifidx = BRCMF_FLOWRING_INVALID_IFIDX;
  303. flow->rings = kcalloc(nrofrings, sizeof(*flow->rings),
  304. GFP_KERNEL);
  305. if (!flow->rings) {
  306. kfree(flow);
  307. flow = NULL;
  308. }
  309. }
  310. return flow;
  311. }
  312. void brcmf_flowring_detach(struct brcmf_flowring *flow)
  313. {
  314. struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
  315. struct brcmf_pub *drvr = bus_if->drvr;
  316. struct brcmf_flowring_tdls_entry *search;
  317. struct brcmf_flowring_tdls_entry *remove;
  318. u16 flowid;
  319. for (flowid = 0; flowid < flow->nrofrings; flowid++) {
  320. if (flow->rings[flowid])
  321. brcmf_msgbuf_delete_flowring(drvr, flowid);
  322. }
  323. search = flow->tdls_entry;
  324. while (search) {
  325. remove = search;
  326. search = search->next;
  327. kfree(remove);
  328. }
  329. kfree(flow->rings);
  330. kfree(flow);
  331. }
  332. void brcmf_flowring_configure_addr_mode(struct brcmf_flowring *flow, int ifidx,
  333. enum proto_addr_mode addr_mode)
  334. {
  335. struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
  336. struct brcmf_pub *drvr = bus_if->drvr;
  337. u32 i;
  338. u16 flowid;
  339. if (flow->addr_mode[ifidx] != addr_mode) {
  340. for (i = 0; i < ARRAY_SIZE(flow->hash); i++) {
  341. if (flow->hash[i].ifidx == ifidx) {
  342. flowid = flow->hash[i].flowid;
  343. if (flow->rings[flowid]->status != RING_OPEN)
  344. continue;
  345. flow->rings[flowid]->status = RING_CLOSING;
  346. brcmf_msgbuf_delete_flowring(drvr, flowid);
  347. }
  348. }
  349. flow->addr_mode[ifidx] = addr_mode;
  350. }
  351. }
  352. void brcmf_flowring_delete_peer(struct brcmf_flowring *flow, int ifidx,
  353. u8 peer[ETH_ALEN])
  354. {
  355. struct brcmf_bus *bus_if = dev_get_drvdata(flow->dev);
  356. struct brcmf_pub *drvr = bus_if->drvr;
  357. struct brcmf_flowring_hash *hash;
  358. struct brcmf_flowring_tdls_entry *prev;
  359. struct brcmf_flowring_tdls_entry *search;
  360. u32 i;
  361. u16 flowid;
  362. bool sta;
  363. sta = (flow->addr_mode[ifidx] == ADDR_INDIRECT);
  364. search = flow->tdls_entry;
  365. prev = NULL;
  366. while (search) {
  367. if (memcmp(search->mac, peer, ETH_ALEN) == 0) {
  368. sta = false;
  369. break;
  370. }
  371. prev = search;
  372. search = search->next;
  373. }
  374. hash = flow->hash;
  375. for (i = 0; i < BRCMF_FLOWRING_HASHSIZE; i++) {
  376. if ((sta || (memcmp(hash[i].mac, peer, ETH_ALEN) == 0)) &&
  377. (hash[i].ifidx == ifidx)) {
  378. flowid = flow->hash[i].flowid;
  379. if (flow->rings[flowid]->status == RING_OPEN) {
  380. flow->rings[flowid]->status = RING_CLOSING;
  381. brcmf_msgbuf_delete_flowring(drvr, flowid);
  382. }
  383. }
  384. }
  385. if (search) {
  386. if (prev)
  387. prev->next = search->next;
  388. else
  389. flow->tdls_entry = search->next;
  390. kfree(search);
  391. if (flow->tdls_entry == NULL)
  392. flow->tdls_active = false;
  393. }
  394. }
  395. void brcmf_flowring_add_tdls_peer(struct brcmf_flowring *flow, int ifidx,
  396. u8 peer[ETH_ALEN])
  397. {
  398. struct brcmf_flowring_tdls_entry *tdls_entry;
  399. struct brcmf_flowring_tdls_entry *search;
  400. tdls_entry = kzalloc(sizeof(*tdls_entry), GFP_ATOMIC);
  401. if (tdls_entry == NULL)
  402. return;
  403. memcpy(tdls_entry->mac, peer, ETH_ALEN);
  404. tdls_entry->next = NULL;
  405. if (flow->tdls_entry == NULL) {
  406. flow->tdls_entry = tdls_entry;
  407. } else {
  408. search = flow->tdls_entry;
  409. if (memcmp(search->mac, peer, ETH_ALEN) == 0)
  410. return;
  411. while (search->next) {
  412. search = search->next;
  413. if (memcmp(search->mac, peer, ETH_ALEN) == 0)
  414. return;
  415. }
  416. search->next = tdls_entry;
  417. }
  418. flow->tdls_active = true;
  419. }