rx_reorder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "wil6210.h"
  2. #include "txrx.h"
  3. #define SEQ_MODULO 0x1000
  4. #define SEQ_MASK 0xfff
  5. static inline int seq_less(u16 sq1, u16 sq2)
  6. {
  7. return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
  8. }
  9. static inline u16 seq_inc(u16 sq)
  10. {
  11. return (sq + 1) & SEQ_MASK;
  12. }
  13. static inline u16 seq_sub(u16 sq1, u16 sq2)
  14. {
  15. return (sq1 - sq2) & SEQ_MASK;
  16. }
  17. static inline int reorder_index(struct wil_tid_ampdu_rx *r, u16 seq)
  18. {
  19. return seq_sub(seq, r->ssn) % r->buf_size;
  20. }
  21. static void wil_release_reorder_frame(struct wil6210_priv *wil,
  22. struct wil_tid_ampdu_rx *r,
  23. int index)
  24. {
  25. struct net_device *ndev = wil_to_ndev(wil);
  26. struct sk_buff *skb = r->reorder_buf[index];
  27. if (!skb)
  28. goto no_frame;
  29. /* release the frame from the reorder ring buffer */
  30. r->stored_mpdu_num--;
  31. r->reorder_buf[index] = NULL;
  32. wil_netif_rx_any(skb, ndev);
  33. no_frame:
  34. r->head_seq_num = seq_inc(r->head_seq_num);
  35. }
  36. static void wil_release_reorder_frames(struct wil6210_priv *wil,
  37. struct wil_tid_ampdu_rx *r,
  38. u16 hseq)
  39. {
  40. int index;
  41. /* note: this function is never called with
  42. * hseq preceding r->head_seq_num, i.e it is always true
  43. * !seq_less(hseq, r->head_seq_num)
  44. * and thus on loop exit it should be
  45. * r->head_seq_num == hseq
  46. */
  47. while (seq_less(r->head_seq_num, hseq) && r->stored_mpdu_num) {
  48. index = reorder_index(r, r->head_seq_num);
  49. wil_release_reorder_frame(wil, r, index);
  50. }
  51. r->head_seq_num = hseq;
  52. }
  53. static void wil_reorder_release(struct wil6210_priv *wil,
  54. struct wil_tid_ampdu_rx *r)
  55. {
  56. int index = reorder_index(r, r->head_seq_num);
  57. while (r->reorder_buf[index]) {
  58. wil_release_reorder_frame(wil, r, index);
  59. index = reorder_index(r, r->head_seq_num);
  60. }
  61. }
  62. void wil_rx_reorder(struct wil6210_priv *wil, struct sk_buff *skb)
  63. {
  64. struct net_device *ndev = wil_to_ndev(wil);
  65. struct vring_rx_desc *d = wil_skb_rxdesc(skb);
  66. int tid = wil_rxdesc_tid(d);
  67. int cid = wil_rxdesc_cid(d);
  68. int mid = wil_rxdesc_mid(d);
  69. u16 seq = wil_rxdesc_seq(d);
  70. struct wil_sta_info *sta = &wil->sta[cid];
  71. struct wil_tid_ampdu_rx *r = sta->tid_rx[tid];
  72. u16 hseq;
  73. int index;
  74. wil_dbg_txrx(wil, "MID %d CID %d TID %d Seq 0x%03x\n",
  75. mid, cid, tid, seq);
  76. if (!r) {
  77. wil_netif_rx_any(skb, ndev);
  78. return;
  79. }
  80. hseq = r->head_seq_num;
  81. spin_lock(&r->reorder_lock);
  82. /** Due to the race between WMI events, where BACK establishment
  83. * reported, and data Rx, few packets may be pass up before reorder
  84. * buffer get allocated. Catch up by pretending SSN is what we
  85. * see in the 1-st Rx packet
  86. */
  87. if (r->first_time) {
  88. r->first_time = false;
  89. if (seq != r->head_seq_num) {
  90. wil_err(wil, "Error: 1-st frame with wrong sequence"
  91. " %d, should be %d. Fixing...\n", seq,
  92. r->head_seq_num);
  93. r->head_seq_num = seq;
  94. r->ssn = seq;
  95. }
  96. }
  97. /* frame with out of date sequence number */
  98. if (seq_less(seq, r->head_seq_num)) {
  99. dev_kfree_skb(skb);
  100. goto out;
  101. }
  102. /*
  103. * If frame the sequence number exceeds our buffering window
  104. * size release some previous frames to make room for this one.
  105. */
  106. if (!seq_less(seq, r->head_seq_num + r->buf_size)) {
  107. hseq = seq_inc(seq_sub(seq, r->buf_size));
  108. /* release stored frames up to new head to stack */
  109. wil_release_reorder_frames(wil, r, hseq);
  110. }
  111. /* Now the new frame is always in the range of the reordering buffer */
  112. index = reorder_index(r, seq);
  113. /* check if we already stored this frame */
  114. if (r->reorder_buf[index]) {
  115. dev_kfree_skb(skb);
  116. goto out;
  117. }
  118. /*
  119. * If the current MPDU is in the right order and nothing else
  120. * is stored we can process it directly, no need to buffer it.
  121. * If it is first but there's something stored, we may be able
  122. * to release frames after this one.
  123. */
  124. if (seq == r->head_seq_num && r->stored_mpdu_num == 0) {
  125. r->head_seq_num = seq_inc(r->head_seq_num);
  126. wil_netif_rx_any(skb, ndev);
  127. goto out;
  128. }
  129. /* put the frame in the reordering buffer */
  130. r->reorder_buf[index] = skb;
  131. r->reorder_time[index] = jiffies;
  132. r->stored_mpdu_num++;
  133. wil_reorder_release(wil, r);
  134. out:
  135. spin_unlock(&r->reorder_lock);
  136. }
  137. struct wil_tid_ampdu_rx *wil_tid_ampdu_rx_alloc(struct wil6210_priv *wil,
  138. int size, u16 ssn)
  139. {
  140. struct wil_tid_ampdu_rx *r = kzalloc(sizeof(*r), GFP_KERNEL);
  141. if (!r)
  142. return NULL;
  143. r->reorder_buf =
  144. kcalloc(size, sizeof(struct sk_buff *), GFP_KERNEL);
  145. r->reorder_time =
  146. kcalloc(size, sizeof(unsigned long), GFP_KERNEL);
  147. if (!r->reorder_buf || !r->reorder_time) {
  148. kfree(r->reorder_buf);
  149. kfree(r->reorder_time);
  150. kfree(r);
  151. return NULL;
  152. }
  153. spin_lock_init(&r->reorder_lock);
  154. r->ssn = ssn;
  155. r->head_seq_num = ssn;
  156. r->buf_size = size;
  157. r->stored_mpdu_num = 0;
  158. r->first_time = true;
  159. return r;
  160. }
  161. void wil_tid_ampdu_rx_free(struct wil6210_priv *wil,
  162. struct wil_tid_ampdu_rx *r)
  163. {
  164. if (!r)
  165. return;
  166. wil_release_reorder_frames(wil, r, r->head_seq_num + r->buf_size);
  167. kfree(r->reorder_buf);
  168. kfree(r->reorder_time);
  169. kfree(r);
  170. }