2 * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
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.
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.
18 #define REORDER_TIMEOUT (HZ / 10)
21 mt76_aggr_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames, int idx)
25 tid->head = ieee80211_sn_inc(tid->head);
27 skb = tid->reorder_buf[idx];
31 tid->reorder_buf[idx] = NULL;
33 __skb_queue_tail(frames, skb);
37 mt76_rx_aggr_release_frames(struct mt76_rx_tid *tid, struct sk_buff_head *frames,
42 while (ieee80211_sn_less(tid->head, head)) {
43 idx = tid->head % tid->size;
44 mt76_aggr_release(tid, frames, idx);
49 mt76_rx_aggr_release_head(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
51 int idx = tid->head % tid->size;
53 while (tid->reorder_buf[idx]) {
54 mt76_aggr_release(tid, frames, idx);
55 idx = tid->head % tid->size;
60 mt76_rx_aggr_check_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
62 struct mt76_rx_status *status;
64 int start, idx, nframes;
69 mt76_rx_aggr_release_head(tid, frames);
71 start = tid->head % tid->size;
72 nframes = tid->nframes;
74 for (idx = (tid->head + 1) % tid->size;
75 idx != start && nframes;
76 idx = (idx + 1) % tid->size) {
78 skb = tid->reorder_buf[idx];
83 status = (struct mt76_rx_status *) skb->cb;
84 if (!time_after(jiffies, status->reorder_time +
88 mt76_rx_aggr_release_frames(tid, frames, status->seqno);
91 mt76_rx_aggr_release_head(tid, frames);
95 mt76_rx_aggr_reorder_work(struct work_struct *work)
97 struct mt76_rx_tid *tid = container_of(work, struct mt76_rx_tid,
99 struct mt76_dev *dev = tid->dev;
100 struct sk_buff_head frames;
103 __skb_queue_head_init(&frames);
108 spin_lock(&tid->lock);
109 mt76_rx_aggr_check_release(tid, &frames);
110 nframes = tid->nframes;
111 spin_unlock(&tid->lock);
114 ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
116 mt76_rx_complete(dev, &frames, NULL);
123 mt76_rx_aggr_check_ctl(struct sk_buff *skb, struct sk_buff_head *frames)
125 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
126 struct ieee80211_bar *bar = (struct ieee80211_bar *) skb->data;
127 struct mt76_wcid *wcid = status->wcid;
128 struct mt76_rx_tid *tid;
131 if (!ieee80211_is_ctl(bar->frame_control))
134 if (!ieee80211_is_back_req(bar->frame_control))
137 status->tid = le16_to_cpu(bar->control) >> 12;
138 seqno = le16_to_cpu(bar->start_seq_num) >> 4;
139 tid = rcu_dereference(wcid->aggr[status->tid]);
143 spin_lock_bh(&tid->lock);
144 mt76_rx_aggr_release_frames(tid, frames, seqno);
145 mt76_rx_aggr_release_head(tid, frames);
146 spin_unlock_bh(&tid->lock);
149 void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
151 struct mt76_rx_status *status = (struct mt76_rx_status *) skb->cb;
152 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
153 struct mt76_wcid *wcid = status->wcid;
154 struct ieee80211_sta *sta;
155 struct mt76_rx_tid *tid;
157 u16 seqno, head, size, idx;
160 __skb_queue_tail(frames, skb);
162 sta = wcid_to_sta(wcid);
167 mt76_rx_aggr_check_ctl(skb, frames);
171 /* not part of a BA session */
172 ackp = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_ACK_POLICY_MASK;
173 if (ackp != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
174 ackp != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
177 tid = rcu_dereference(wcid->aggr[status->tid]);
181 status->flag |= RX_FLAG_DUP_VALIDATED;
182 spin_lock_bh(&tid->lock);
188 seqno = status->seqno;
190 sn_less = ieee80211_sn_less(seqno, head);
200 __skb_unlink(skb, frames);
206 tid->head = ieee80211_sn_inc(head);
208 mt76_rx_aggr_release_head(tid, frames);
212 __skb_unlink(skb, frames);
215 * Frame sequence number exceeds buffering window, free up some space
216 * by releasing previous frames
218 if (!ieee80211_sn_less(seqno, head + size)) {
219 head = ieee80211_sn_inc(ieee80211_sn_sub(seqno, size));
220 mt76_rx_aggr_release_frames(tid, frames, head);
225 /* Discard if the current slot is already in use */
226 if (tid->reorder_buf[idx]) {
231 status->reorder_time = jiffies;
232 tid->reorder_buf[idx] = skb;
234 mt76_rx_aggr_release_head(tid, frames);
236 ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work, REORDER_TIMEOUT);
239 spin_unlock_bh(&tid->lock);
242 int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
245 struct mt76_rx_tid *tid;
247 mt76_rx_aggr_stop(dev, wcid, tidno);
249 tid = kzalloc(struct_size(tid, reorder_buf, size), GFP_KERNEL);
256 INIT_DELAYED_WORK(&tid->reorder_work, mt76_rx_aggr_reorder_work);
257 spin_lock_init(&tid->lock);
259 rcu_assign_pointer(wcid->aggr[tidno], tid);
263 EXPORT_SYMBOL_GPL(mt76_rx_aggr_start);
265 static void mt76_rx_aggr_shutdown(struct mt76_dev *dev, struct mt76_rx_tid *tid)
267 u16 size = tid->size;
270 cancel_delayed_work(&tid->reorder_work);
272 spin_lock_bh(&tid->lock);
275 for (i = 0; tid->nframes && i < size; i++) {
276 struct sk_buff *skb = tid->reorder_buf[i];
281 tid->reorder_buf[i] = NULL;
286 spin_unlock_bh(&tid->lock);
289 void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno)
291 struct mt76_rx_tid *tid;
295 tid = rcu_dereference(wcid->aggr[tidno]);
297 rcu_assign_pointer(wcid->aggr[tidno], NULL);
298 mt76_rx_aggr_shutdown(dev, tid);
299 kfree_rcu(tid, rcu_head);
304 EXPORT_SYMBOL_GPL(mt76_rx_aggr_stop);