flowring.c 11 KB

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