agg-rx.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "mt76.h"
  17. #define REORDER_TIMEOUT (HZ / 10)
  18. static void
  19. mt76_aggr_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames, int idx)
  20. {
  21. struct sk_buff *skb;
  22. tid->head = ieee80211_sn_inc(tid->head);
  23. skb = tid->reorder_buf[idx];
  24. if (!skb)
  25. return;
  26. tid->reorder_buf[idx] = NULL;
  27. tid->nframes--;
  28. __skb_queue_tail(frames, skb);
  29. }
  30. static void
  31. mt76_rx_aggr_release_frames(struct mt76_rx_tid *tid, struct sk_buff_head *frames,
  32. u16 head)
  33. {
  34. int idx;
  35. while (ieee80211_sn_less(tid->head, head)) {
  36. idx = tid->head % tid->size;
  37. mt76_aggr_release(tid, frames, idx);
  38. }
  39. }
  40. static void
  41. mt76_rx_aggr_release_head(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
  42. {
  43. int idx = tid->head % tid->size;
  44. while (tid->reorder_buf[idx]) {
  45. mt76_aggr_release(tid, frames, idx);
  46. idx = tid->head % tid->size;
  47. }
  48. }
  49. static void
  50. mt76_rx_aggr_check_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
  51. {
  52. struct mt76_rx_status *status;
  53. struct sk_buff *skb;
  54. int start, idx, nframes;
  55. if (!tid->nframes)
  56. return;
  57. mt76_rx_aggr_release_head(tid, frames);
  58. start = tid->head % tid->size;
  59. nframes = tid->nframes;
  60. for (idx = (tid->head + 1) % tid->size;
  61. idx != start && nframes;
  62. idx = (idx + 1) % tid->size) {
  63. skb = tid->reorder_buf[idx];
  64. if (!skb)
  65. continue;
  66. nframes--;
  67. status = (struct mt76_rx_status *) skb->cb;
  68. if (!time_after(jiffies, status->reorder_time +
  69. REORDER_TIMEOUT))
  70. continue;
  71. mt76_rx_aggr_release_frames(tid, frames, status->seqno);
  72. }
  73. mt76_rx_aggr_release_head(tid, frames);
  74. }
  75. static void
  76. mt76_rx_aggr_reorder_work(struct work_struct *work)
  77. {
  78. struct mt76_rx_tid *tid = container_of(work, struct mt76_rx_tid,
  79. reorder_work.work);
  80. struct mt76_dev *dev = tid->dev;
  81. struct sk_buff_head frames;
  82. int nframes;
  83. __skb_queue_head_init(&frames);
  84. local_bh_disable();
  85. rcu_read_lock();
  86. spin_lock(&tid->lock);
  87. mt76_rx_aggr_check_release(tid, &frames);
  88. nframes = tid->nframes;
  89. spin_unlock(&tid->lock);
  90. if (nframes)
  91. ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
  92. REORDER_TIMEOUT);
  93. mt76_rx_complete(dev, &frames, NULL);
  94. rcu_read_unlock();
  95. local_bh_enable();
  96. }
  97. static void
  98. mt76_rx_aggr_check_ctl(struct sk_buff *skb, struct sk_buff_head *frames)
  99. {
  100. struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
  101. struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
  102. struct mt76_wcid *wcid = status->wcid;
  103. struct mt76_rx_tid *tid;
  104. u16 seqno;
  105. if (!ieee80211_is_ctl(bar->frame_control))
  106. return;
  107. if (!ieee80211_is_back_req(bar->frame_control))
  108. return;
  109. status->tid = le16_to_cpu(bar->control) >> 12;
  110. seqno = le16_to_cpu(bar->start_seq_num) >> 4;
  111. tid = rcu_dereference(wcid->aggr[status->tid]);
  112. if (!tid)
  113. return;
  114. spin_lock_bh(&tid->lock);
  115. mt76_rx_aggr_release_frames(tid, frames, seqno);
  116. mt76_rx_aggr_release_head(tid, frames);
  117. spin_unlock_bh(&tid->lock);
  118. }
  119. void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
  120. {
  121. struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
  122. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  123. struct mt76_wcid *wcid = status->wcid;
  124. struct ieee80211_sta *sta;
  125. struct mt76_rx_tid *tid;
  126. bool sn_less;
  127. u16 seqno, head, size, idx;
  128. u8 ackp;
  129. __skb_queue_tail(frames, skb);
  130. sta = wcid_to_sta(wcid);
  131. if (!sta)
  132. return;
  133. if (!status->aggr) {
  134. mt76_rx_aggr_check_ctl(skb, frames);
  135. return;
  136. }
  137. /* not part of a BA session */
  138. ackp = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_ACK_POLICY_MASK;
  139. if (ackp != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
  140. ackp != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
  141. return;
  142. tid = rcu_dereference(wcid->aggr[status->tid]);
  143. if (!tid)
  144. return;
  145. status->flag |= RX_FLAG_DUP_VALIDATED;
  146. spin_lock_bh(&tid->lock);
  147. if (tid->stopped)
  148. goto out;
  149. head = tid->head;
  150. seqno = status->seqno;
  151. size = tid->size;
  152. sn_less = ieee80211_sn_less(seqno, head);
  153. if (!tid->started) {
  154. if (sn_less)
  155. goto out;
  156. tid->started = true;
  157. }
  158. if (sn_less) {
  159. __skb_unlink(skb, frames);
  160. dev_kfree_skb(skb);
  161. goto out;
  162. }
  163. if (seqno == head) {
  164. tid->head = ieee80211_sn_inc(head);
  165. if (tid->nframes)
  166. mt76_rx_aggr_release_head(tid, frames);
  167. goto out;
  168. }
  169. __skb_unlink(skb, frames);
  170. /*
  171. * Frame sequence number exceeds buffering window, free up some space
  172. * by releasing previous frames
  173. */
  174. if (!ieee80211_sn_less(seqno, head + size)) {
  175. head = ieee80211_sn_inc(ieee80211_sn_sub(seqno, size));
  176. mt76_rx_aggr_release_frames(tid, frames, head);
  177. }
  178. idx = seqno % size;
  179. /* Discard if the current slot is already in use */
  180. if (tid->reorder_buf[idx]) {
  181. dev_kfree_skb(skb);
  182. goto out;
  183. }
  184. status->reorder_time = jiffies;
  185. tid->reorder_buf[idx] = skb;
  186. tid->nframes++;
  187. mt76_rx_aggr_release_head(tid, frames);
  188. ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work, REORDER_TIMEOUT);
  189. out:
  190. spin_unlock_bh(&tid->lock);
  191. }
  192. int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
  193. u16 ssn, u16 size)
  194. {
  195. struct mt76_rx_tid *tid;
  196. mt76_rx_aggr_stop(dev, wcid, tidno);
  197. tid = kzalloc(struct_size(tid, reorder_buf, size), GFP_KERNEL);
  198. if (!tid)
  199. return -ENOMEM;
  200. tid->dev = dev;
  201. tid->head = ssn;
  202. tid->size = size;
  203. INIT_DELAYED_WORK(&tid->reorder_work, mt76_rx_aggr_reorder_work);
  204. spin_lock_init(&tid->lock);
  205. rcu_assign_pointer(wcid->aggr[tidno], tid);
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(mt76_rx_aggr_start);
  209. static void mt76_rx_aggr_shutdown(struct mt76_dev *dev, struct mt76_rx_tid *tid)
  210. {
  211. u16 size = tid->size;
  212. int i;
  213. cancel_delayed_work(&tid->reorder_work);
  214. spin_lock_bh(&tid->lock);
  215. tid->stopped = true;
  216. for (i = 0; tid->nframes && i < size; i++) {
  217. struct sk_buff *skb = tid->reorder_buf[i];
  218. if (!skb)
  219. continue;
  220. tid->reorder_buf[i] = NULL;
  221. tid->nframes--;
  222. dev_kfree_skb(skb);
  223. }
  224. spin_unlock_bh(&tid->lock);
  225. }
  226. void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno)
  227. {
  228. struct mt76_rx_tid *tid;
  229. rcu_read_lock();
  230. tid = rcu_dereference(wcid->aggr[tidno]);
  231. if (tid) {
  232. rcu_assign_pointer(wcid->aggr[tidno], NULL);
  233. mt76_rx_aggr_shutdown(dev, tid);
  234. kfree_rcu(tid, rcu_head);
  235. }
  236. rcu_read_unlock();
  237. }
  238. EXPORT_SYMBOL_GPL(mt76_rx_aggr_stop);