blob: e9784b50e2af3633d2038c92e79b40a054ebc738 [file] [log] [blame]
Felix Fietkauaee5b8c2018-01-24 16:19:14 +01001/*
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
18#define REORDER_TIMEOUT (HZ / 10)
19
20static void
21mt76_aggr_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames, int idx)
22{
23 struct sk_buff *skb;
24
25 tid->head = ieee80211_sn_inc(tid->head);
26
27 skb = tid->reorder_buf[idx];
28 if (!skb)
29 return;
30
31 tid->reorder_buf[idx] = NULL;
32 tid->nframes--;
33 __skb_queue_tail(frames, skb);
34}
35
36static void
37mt76_rx_aggr_release_frames(struct mt76_rx_tid *tid, struct sk_buff_head *frames,
38 u16 head)
39{
40 int idx;
41
42 while (ieee80211_sn_less(tid->head, head)) {
43 idx = tid->head % tid->size;
44 mt76_aggr_release(tid, frames, idx);
45 }
46}
47
48static void
49mt76_rx_aggr_release_head(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
50{
51 int idx = tid->head % tid->size;
52
53 while (tid->reorder_buf[idx]) {
54 mt76_aggr_release(tid, frames, idx);
55 idx = tid->head % tid->size;
56 }
57}
58
59static void
60mt76_rx_aggr_check_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
61{
62 struct mt76_rx_status *status;
63 struct sk_buff *skb;
64 int start, idx, nframes;
65
66 if (!tid->nframes)
67 return;
68
69 mt76_rx_aggr_release_head(tid, frames);
70
71 start = tid->head % tid->size;
72 nframes = tid->nframes;
73
74 for (idx = (tid->head + 1) % tid->size;
75 idx != start && nframes;
76 idx = (idx + 1) % tid->size) {
77
78 skb = tid->reorder_buf[idx];
79 if (!skb)
80 continue;
81
82 nframes--;
83 status = (struct mt76_rx_status *) skb->cb;
84 if (!time_after(jiffies, status->reorder_time +
85 REORDER_TIMEOUT))
86 continue;
87
88 mt76_rx_aggr_release_frames(tid, frames, status->seqno);
89 }
90
91 mt76_rx_aggr_release_head(tid, frames);
92}
93
94static void
95mt76_rx_aggr_reorder_work(struct work_struct *work)
96{
97 struct mt76_rx_tid *tid = container_of(work, struct mt76_rx_tid,
98 reorder_work.work);
99 struct mt76_dev *dev = tid->dev;
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100100 struct sk_buff_head frames;
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100101
102 __skb_queue_head_init(&frames);
103
104 local_bh_disable();
105
106 spin_lock(&tid->lock);
107 mt76_rx_aggr_check_release(tid, &frames);
108 spin_unlock(&tid->lock);
109
110 ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work, REORDER_TIMEOUT);
Felix Fietkau9d9d7382018-01-24 16:19:15 +0100111 mt76_rx_complete(dev, &frames, -1);
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100112
113 local_bh_enable();
114}
115
Felix Fietkau17cf68b2018-01-27 16:02:04 +0100116static void
117mt76_rx_aggr_check_ctl(struct sk_buff *skb, struct sk_buff_head *frames)
118{
119 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
120 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
121 struct mt76_wcid *wcid = status->wcid;
122 struct mt76_rx_tid *tid;
123 u16 seqno;
124
125 if (!ieee80211_is_ctl(bar->frame_control))
126 return;
127
128 if (!ieee80211_is_back_req(bar->frame_control))
129 return;
130
131 status->tid = le16_to_cpu(bar->control) >> 12;
132 seqno = le16_to_cpu(bar->start_seq_num) >> 4;
133 tid = rcu_dereference(wcid->aggr[status->tid]);
134 if (!tid)
135 return;
136
137 spin_lock_bh(&tid->lock);
138 mt76_rx_aggr_release_frames(tid, frames, seqno);
139 mt76_rx_aggr_release_head(tid, frames);
140 spin_unlock_bh(&tid->lock);
141}
142
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100143void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
144{
145 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
146 struct mt76_wcid *wcid = status->wcid;
147 struct ieee80211_sta *sta;
148 struct mt76_rx_tid *tid;
149 bool sn_less;
150 u16 seqno, head, size;
151 u8 idx;
152
153 __skb_queue_tail(frames, skb);
154
155 sta = wcid_to_sta(wcid);
Felix Fietkau17cf68b2018-01-27 16:02:04 +0100156 if (!sta)
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100157 return;
158
Felix Fietkau17cf68b2018-01-27 16:02:04 +0100159 if (!status->aggr) {
160 mt76_rx_aggr_check_ctl(skb, frames);
161 return;
162 }
163
Felix Fietkauaee5b8c2018-01-24 16:19:14 +0100164 tid = rcu_dereference(wcid->aggr[status->tid]);
165 if (!tid)
166 return;
167
168 spin_lock_bh(&tid->lock);
169
170 if (tid->stopped)
171 goto out;
172
173 head = tid->head;
174 seqno = status->seqno;
175 size = tid->size;
176 sn_less = ieee80211_sn_less(seqno, head);
177
178 if (!tid->started) {
179 if (sn_less)
180 goto out;
181
182 tid->started = true;
183 }
184
185 if (sn_less) {
186 __skb_unlink(skb, frames);
187 dev_kfree_skb(skb);
188 goto out;
189 }
190
191 if (seqno == head) {
192 tid->head = ieee80211_sn_inc(head);
193 if (tid->nframes)
194 mt76_rx_aggr_release_head(tid, frames);
195 goto out;
196 }
197
198 __skb_unlink(skb, frames);
199
200 /*
201 * Frame sequence number exceeds buffering window, free up some space
202 * by releasing previous frames
203 */
204 if (!ieee80211_sn_less(seqno, head + size)) {
205 head = ieee80211_sn_inc(ieee80211_sn_sub(seqno, size));
206 mt76_rx_aggr_release_frames(tid, frames, head);
207 }
208
209 idx = seqno % size;
210
211 /* Discard if the current slot is already in use */
212 if (tid->reorder_buf[idx]) {
213 dev_kfree_skb(skb);
214 goto out;
215 }
216
217 status->reorder_time = jiffies;
218 tid->reorder_buf[idx] = skb;
219 tid->nframes++;
220 mt76_rx_aggr_release_head(tid, frames);
221
222 ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work, REORDER_TIMEOUT);
223
224out:
225 spin_unlock_bh(&tid->lock);
226}
227
228int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
229 u16 ssn, u8 size)
230{
231 struct mt76_rx_tid *tid;
232
233 mt76_rx_aggr_stop(dev, wcid, tidno);
234
235 tid = kzalloc(sizeof(*tid) + size * sizeof(tid->reorder_buf[0]),
236 GFP_KERNEL);
237 if (!tid)
238 return -ENOMEM;
239
240 tid->dev = dev;
241 tid->head = ssn;
242 tid->size = size;
243 INIT_DELAYED_WORK(&tid->reorder_work, mt76_rx_aggr_reorder_work);
244 spin_lock_init(&tid->lock);
245
246 rcu_assign_pointer(wcid->aggr[tidno], tid);
247
248 return 0;
249}
250EXPORT_SYMBOL_GPL(mt76_rx_aggr_start);
251
252static void mt76_rx_aggr_shutdown(struct mt76_dev *dev, struct mt76_rx_tid *tid)
253{
254 u8 size = tid->size;
255 int i;
256
257 spin_lock_bh(&tid->lock);
258
259 tid->stopped = true;
260 for (i = 0; tid->nframes && i < size; i++) {
261 struct sk_buff *skb = tid->reorder_buf[i];
262
263 if (!skb)
264 continue;
265
266 tid->nframes--;
267 dev_kfree_skb(skb);
268 }
269
270 spin_unlock_bh(&tid->lock);
271
272 cancel_delayed_work_sync(&tid->reorder_work);
273}
274
275void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno)
276{
277 struct mt76_rx_tid *tid;
278
279 rcu_read_lock();
280
281 tid = rcu_dereference(wcid->aggr[tidno]);
282 if (tid) {
283 rcu_assign_pointer(wcid->aggr[tidno], NULL);
284 mt76_rx_aggr_shutdown(dev, tid);
285 kfree_rcu(tid, rcu_head);
286 }
287
288 rcu_read_unlock();
289}
290EXPORT_SYMBOL_GPL(mt76_rx_aggr_stop);