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