rx_reorder.c 5.7 KB

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