Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v6.8
  1// SPDX-License-Identifier: ISC
  2/*
  3 * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
  4 */
  5#include "mt76.h"
  6
  7static unsigned long mt76_aggr_tid_to_timeo(u8 tidno)
  8{
  9	/* Currently voice traffic (AC_VO) always runs without aggregation,
 10	 * no special handling is needed. AC_BE/AC_BK use tids 0-3. Just check
 11	 * for non AC_BK/AC_BE and set smaller timeout for it. */
 12	return HZ / (tidno >= 4 ? 25 : 10);
 13}
 14
 15static void
 16mt76_aggr_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames, int idx)
 17{
 18	struct sk_buff *skb;
 19
 20	tid->head = ieee80211_sn_inc(tid->head);
 21
 22	skb = tid->reorder_buf[idx];
 23	if (!skb)
 24		return;
 25
 26	tid->reorder_buf[idx] = NULL;
 27	tid->nframes--;
 28	__skb_queue_tail(frames, skb);
 29}
 30
 31static void
 32mt76_rx_aggr_release_frames(struct mt76_rx_tid *tid,
 33			    struct sk_buff_head *frames,
 34			    u16 head)
 35{
 36	int idx;
 37
 38	while (ieee80211_sn_less(tid->head, head)) {
 39		idx = tid->head % tid->size;
 40		mt76_aggr_release(tid, frames, idx);
 41	}
 42}
 43
 44static void
 45mt76_rx_aggr_release_head(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
 46{
 47	int idx = tid->head % tid->size;
 48
 49	while (tid->reorder_buf[idx]) {
 50		mt76_aggr_release(tid, frames, idx);
 51		idx = tid->head % tid->size;
 52	}
 53}
 54
 55static void
 56mt76_rx_aggr_check_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
 57{
 58	struct mt76_rx_status *status;
 59	struct sk_buff *skb;
 60	int start, idx, nframes;
 61
 62	if (!tid->nframes)
 63		return;
 64
 65	mt76_rx_aggr_release_head(tid, frames);
 66
 67	start = tid->head % tid->size;
 68	nframes = tid->nframes;
 69
 70	for (idx = (tid->head + 1) % tid->size;
 71	     idx != start && nframes;
 72	     idx = (idx + 1) % tid->size) {
 73		skb = tid->reorder_buf[idx];
 74		if (!skb)
 75			continue;
 76
 77		nframes--;
 78		status = (struct mt76_rx_status *)skb->cb;
 79		if (!time_after32(jiffies,
 80				  status->reorder_time +
 81				  mt76_aggr_tid_to_timeo(tid->num)))
 82			continue;
 83
 84		mt76_rx_aggr_release_frames(tid, frames, status->seqno);
 85	}
 86
 87	mt76_rx_aggr_release_head(tid, frames);
 88}
 89
 90static void
 91mt76_rx_aggr_reorder_work(struct work_struct *work)
 92{
 93	struct mt76_rx_tid *tid = container_of(work, struct mt76_rx_tid,
 94					       reorder_work.work);
 95	struct mt76_dev *dev = tid->dev;
 96	struct sk_buff_head frames;
 97	int nframes;
 98
 99	__skb_queue_head_init(&frames);
100
101	local_bh_disable();
102	rcu_read_lock();
103
104	spin_lock(&tid->lock);
105	mt76_rx_aggr_check_release(tid, &frames);
106	nframes = tid->nframes;
107	spin_unlock(&tid->lock);
108
109	if (nframes)
110		ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
111					     mt76_aggr_tid_to_timeo(tid->num));
112	mt76_rx_complete(dev, &frames, NULL);
113
114	rcu_read_unlock();
115	local_bh_enable();
116}
117
118static void
119mt76_rx_aggr_check_ctl(struct sk_buff *skb, struct sk_buff_head *frames)
120{
121	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
122	struct ieee80211_bar *bar = mt76_skb_get_hdr(skb);
123	struct mt76_wcid *wcid = status->wcid;
124	struct mt76_rx_tid *tid;
125	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
126	u16 seqno;
127
128	if (!ieee80211_is_ctl(bar->frame_control))
129		return;
130
131	if (!ieee80211_is_back_req(bar->frame_control))
132		return;
133
134	status->qos_ctl = tidno = le16_to_cpu(bar->control) >> 12;
135	seqno = IEEE80211_SEQ_TO_SN(le16_to_cpu(bar->start_seq_num));
136	tid = rcu_dereference(wcid->aggr[tidno]);
137	if (!tid)
138		return;
139
140	spin_lock_bh(&tid->lock);
141	if (!tid->stopped) {
142		mt76_rx_aggr_release_frames(tid, frames, seqno);
143		mt76_rx_aggr_release_head(tid, frames);
144	}
145	spin_unlock_bh(&tid->lock);
146}
147
148void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
149{
150	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
 
151	struct mt76_wcid *wcid = status->wcid;
152	struct ieee80211_sta *sta;
153	struct mt76_rx_tid *tid;
154	bool sn_less;
155	u16 seqno, head, size, idx;
156	u8 tidno = status->qos_ctl & IEEE80211_QOS_CTL_TID_MASK;
157	u8 ackp;
158
159	__skb_queue_tail(frames, skb);
160
161	sta = wcid_to_sta(wcid);
162	if (!sta)
163		return;
164
165	if (!status->aggr) {
166		if (!(status->flag & RX_FLAG_8023))
167			mt76_rx_aggr_check_ctl(skb, frames);
168		return;
169	}
170
171	/* not part of a BA session */
172	ackp = status->qos_ctl & IEEE80211_QOS_CTL_ACK_POLICY_MASK;
173	if (ackp == IEEE80211_QOS_CTL_ACK_POLICY_NOACK)
 
174		return;
175
176	tid = rcu_dereference(wcid->aggr[tidno]);
177	if (!tid)
178		return;
179
180	status->flag |= RX_FLAG_DUP_VALIDATED;
181	spin_lock_bh(&tid->lock);
182
183	if (tid->stopped)
184		goto out;
185
186	head = tid->head;
187	seqno = status->seqno;
188	size = tid->size;
189	sn_less = ieee80211_sn_less(seqno, head);
190
191	if (!tid->started) {
192		if (sn_less)
193			goto out;
194
195		tid->started = true;
196	}
197
198	if (sn_less) {
199		__skb_unlink(skb, frames);
200		dev_kfree_skb(skb);
201		goto out;
202	}
203
204	if (seqno == head) {
205		tid->head = ieee80211_sn_inc(head);
206		if (tid->nframes)
207			mt76_rx_aggr_release_head(tid, frames);
208		goto out;
209	}
210
211	__skb_unlink(skb, frames);
212
213	/*
214	 * Frame sequence number exceeds buffering window, free up some space
215	 * by releasing previous frames
216	 */
217	if (!ieee80211_sn_less(seqno, head + size)) {
218		head = ieee80211_sn_inc(ieee80211_sn_sub(seqno, size));
219		mt76_rx_aggr_release_frames(tid, frames, head);
220	}
221
222	idx = seqno % size;
223
224	/* Discard if the current slot is already in use */
225	if (tid->reorder_buf[idx]) {
226		dev_kfree_skb(skb);
227		goto out;
228	}
229
230	status->reorder_time = jiffies;
231	tid->reorder_buf[idx] = skb;
232	tid->nframes++;
233	mt76_rx_aggr_release_head(tid, frames);
234
235	ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
236				     mt76_aggr_tid_to_timeo(tid->num));
237
238out:
239	spin_unlock_bh(&tid->lock);
240}
241
242int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
243		       u16 ssn, u16 size)
244{
245	struct mt76_rx_tid *tid;
246
247	mt76_rx_aggr_stop(dev, wcid, tidno);
248
249	tid = kzalloc(struct_size(tid, reorder_buf, size), GFP_KERNEL);
250	if (!tid)
251		return -ENOMEM;
252
253	tid->dev = dev;
254	tid->head = ssn;
255	tid->size = size;
256	tid->num = tidno;
257	INIT_DELAYED_WORK(&tid->reorder_work, mt76_rx_aggr_reorder_work);
258	spin_lock_init(&tid->lock);
259
260	rcu_assign_pointer(wcid->aggr[tidno], tid);
261
262	return 0;
263}
264EXPORT_SYMBOL_GPL(mt76_rx_aggr_start);
265
266static void mt76_rx_aggr_shutdown(struct mt76_dev *dev, struct mt76_rx_tid *tid)
267{
268	u16 size = tid->size;
269	int i;
270
 
 
271	spin_lock_bh(&tid->lock);
272
273	tid->stopped = true;
274	for (i = 0; tid->nframes && i < size; i++) {
275		struct sk_buff *skb = tid->reorder_buf[i];
276
277		if (!skb)
278			continue;
279
280		tid->reorder_buf[i] = NULL;
281		tid->nframes--;
282		dev_kfree_skb(skb);
283	}
284
285	spin_unlock_bh(&tid->lock);
286
287	cancel_delayed_work_sync(&tid->reorder_work);
288}
289
290void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno)
291{
292	struct mt76_rx_tid *tid = NULL;
 
 
293
294	tid = rcu_replace_pointer(wcid->aggr[tidno], tid,
295				  lockdep_is_held(&dev->mutex));
296	if (tid) {
 
297		mt76_rx_aggr_shutdown(dev, tid);
298		kfree_rcu(tid, rcu_head);
299	}
 
 
300}
301EXPORT_SYMBOL_GPL(mt76_rx_aggr_stop);
v5.4
  1// SPDX-License-Identifier: ISC
  2/*
  3 * Copyright (C) 2018 Felix Fietkau <nbd@nbd.name>
  4 */
  5#include "mt76.h"
  6
  7#define REORDER_TIMEOUT (HZ / 10)
 
 
 
 
 
 
  8
  9static void
 10mt76_aggr_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames, int idx)
 11{
 12	struct sk_buff *skb;
 13
 14	tid->head = ieee80211_sn_inc(tid->head);
 15
 16	skb = tid->reorder_buf[idx];
 17	if (!skb)
 18		return;
 19
 20	tid->reorder_buf[idx] = NULL;
 21	tid->nframes--;
 22	__skb_queue_tail(frames, skb);
 23}
 24
 25static void
 26mt76_rx_aggr_release_frames(struct mt76_rx_tid *tid,
 27			    struct sk_buff_head *frames,
 28			    u16 head)
 29{
 30	int idx;
 31
 32	while (ieee80211_sn_less(tid->head, head)) {
 33		idx = tid->head % tid->size;
 34		mt76_aggr_release(tid, frames, idx);
 35	}
 36}
 37
 38static void
 39mt76_rx_aggr_release_head(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
 40{
 41	int idx = tid->head % tid->size;
 42
 43	while (tid->reorder_buf[idx]) {
 44		mt76_aggr_release(tid, frames, idx);
 45		idx = tid->head % tid->size;
 46	}
 47}
 48
 49static void
 50mt76_rx_aggr_check_release(struct mt76_rx_tid *tid, struct sk_buff_head *frames)
 51{
 52	struct mt76_rx_status *status;
 53	struct sk_buff *skb;
 54	int start, idx, nframes;
 55
 56	if (!tid->nframes)
 57		return;
 58
 59	mt76_rx_aggr_release_head(tid, frames);
 60
 61	start = tid->head % tid->size;
 62	nframes = tid->nframes;
 63
 64	for (idx = (tid->head + 1) % tid->size;
 65	     idx != start && nframes;
 66	     idx = (idx + 1) % tid->size) {
 67		skb = tid->reorder_buf[idx];
 68		if (!skb)
 69			continue;
 70
 71		nframes--;
 72		status = (struct mt76_rx_status *)skb->cb;
 73		if (!time_after(jiffies,
 74				status->reorder_time + REORDER_TIMEOUT))
 
 75			continue;
 76
 77		mt76_rx_aggr_release_frames(tid, frames, status->seqno);
 78	}
 79
 80	mt76_rx_aggr_release_head(tid, frames);
 81}
 82
 83static void
 84mt76_rx_aggr_reorder_work(struct work_struct *work)
 85{
 86	struct mt76_rx_tid *tid = container_of(work, struct mt76_rx_tid,
 87					       reorder_work.work);
 88	struct mt76_dev *dev = tid->dev;
 89	struct sk_buff_head frames;
 90	int nframes;
 91
 92	__skb_queue_head_init(&frames);
 93
 94	local_bh_disable();
 95	rcu_read_lock();
 96
 97	spin_lock(&tid->lock);
 98	mt76_rx_aggr_check_release(tid, &frames);
 99	nframes = tid->nframes;
100	spin_unlock(&tid->lock);
101
102	if (nframes)
103		ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
104					     REORDER_TIMEOUT);
105	mt76_rx_complete(dev, &frames, NULL);
106
107	rcu_read_unlock();
108	local_bh_enable();
109}
110
111static void
112mt76_rx_aggr_check_ctl(struct sk_buff *skb, struct sk_buff_head *frames)
113{
114	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
115	struct ieee80211_bar *bar = (struct ieee80211_bar *)skb->data;
116	struct mt76_wcid *wcid = status->wcid;
117	struct mt76_rx_tid *tid;
 
118	u16 seqno;
119
120	if (!ieee80211_is_ctl(bar->frame_control))
121		return;
122
123	if (!ieee80211_is_back_req(bar->frame_control))
124		return;
125
126	status->tid = le16_to_cpu(bar->control) >> 12;
127	seqno = IEEE80211_SEQ_TO_SN(le16_to_cpu(bar->start_seq_num));
128	tid = rcu_dereference(wcid->aggr[status->tid]);
129	if (!tid)
130		return;
131
132	spin_lock_bh(&tid->lock);
133	mt76_rx_aggr_release_frames(tid, frames, seqno);
134	mt76_rx_aggr_release_head(tid, frames);
 
 
135	spin_unlock_bh(&tid->lock);
136}
137
138void mt76_rx_aggr_reorder(struct sk_buff *skb, struct sk_buff_head *frames)
139{
140	struct mt76_rx_status *status = (struct mt76_rx_status *)skb->cb;
141	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
142	struct mt76_wcid *wcid = status->wcid;
143	struct ieee80211_sta *sta;
144	struct mt76_rx_tid *tid;
145	bool sn_less;
146	u16 seqno, head, size;
147	u8 ackp, idx;
 
148
149	__skb_queue_tail(frames, skb);
150
151	sta = wcid_to_sta(wcid);
152	if (!sta)
153		return;
154
155	if (!status->aggr) {
156		mt76_rx_aggr_check_ctl(skb, frames);
 
157		return;
158	}
159
160	/* not part of a BA session */
161	ackp = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_ACK_POLICY_MASK;
162	if (ackp != IEEE80211_QOS_CTL_ACK_POLICY_BLOCKACK &&
163	    ackp != IEEE80211_QOS_CTL_ACK_POLICY_NORMAL)
164		return;
165
166	tid = rcu_dereference(wcid->aggr[status->tid]);
167	if (!tid)
168		return;
169
170	status->flag |= RX_FLAG_DUP_VALIDATED;
171	spin_lock_bh(&tid->lock);
172
173	if (tid->stopped)
174		goto out;
175
176	head = tid->head;
177	seqno = status->seqno;
178	size = tid->size;
179	sn_less = ieee80211_sn_less(seqno, head);
180
181	if (!tid->started) {
182		if (sn_less)
183			goto out;
184
185		tid->started = true;
186	}
187
188	if (sn_less) {
189		__skb_unlink(skb, frames);
190		dev_kfree_skb(skb);
191		goto out;
192	}
193
194	if (seqno == head) {
195		tid->head = ieee80211_sn_inc(head);
196		if (tid->nframes)
197			mt76_rx_aggr_release_head(tid, frames);
198		goto out;
199	}
200
201	__skb_unlink(skb, frames);
202
203	/*
204	 * Frame sequence number exceeds buffering window, free up some space
205	 * by releasing previous frames
206	 */
207	if (!ieee80211_sn_less(seqno, head + size)) {
208		head = ieee80211_sn_inc(ieee80211_sn_sub(seqno, size));
209		mt76_rx_aggr_release_frames(tid, frames, head);
210	}
211
212	idx = seqno % size;
213
214	/* Discard if the current slot is already in use */
215	if (tid->reorder_buf[idx]) {
216		dev_kfree_skb(skb);
217		goto out;
218	}
219
220	status->reorder_time = jiffies;
221	tid->reorder_buf[idx] = skb;
222	tid->nframes++;
223	mt76_rx_aggr_release_head(tid, frames);
224
225	ieee80211_queue_delayed_work(tid->dev->hw, &tid->reorder_work,
226				     REORDER_TIMEOUT);
227
228out:
229	spin_unlock_bh(&tid->lock);
230}
231
232int mt76_rx_aggr_start(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno,
233		       u16 ssn, u8 size)
234{
235	struct mt76_rx_tid *tid;
236
237	mt76_rx_aggr_stop(dev, wcid, tidno);
238
239	tid = kzalloc(struct_size(tid, reorder_buf, size), GFP_KERNEL);
240	if (!tid)
241		return -ENOMEM;
242
243	tid->dev = dev;
244	tid->head = ssn;
245	tid->size = size;
 
246	INIT_DELAYED_WORK(&tid->reorder_work, mt76_rx_aggr_reorder_work);
247	spin_lock_init(&tid->lock);
248
249	rcu_assign_pointer(wcid->aggr[tidno], tid);
250
251	return 0;
252}
253EXPORT_SYMBOL_GPL(mt76_rx_aggr_start);
254
255static void mt76_rx_aggr_shutdown(struct mt76_dev *dev, struct mt76_rx_tid *tid)
256{
257	u8 size = tid->size;
258	int i;
259
260	cancel_delayed_work(&tid->reorder_work);
261
262	spin_lock_bh(&tid->lock);
263
264	tid->stopped = true;
265	for (i = 0; tid->nframes && i < size; i++) {
266		struct sk_buff *skb = tid->reorder_buf[i];
267
268		if (!skb)
269			continue;
270
 
271		tid->nframes--;
272		dev_kfree_skb(skb);
273	}
274
275	spin_unlock_bh(&tid->lock);
 
 
276}
277
278void mt76_rx_aggr_stop(struct mt76_dev *dev, struct mt76_wcid *wcid, u8 tidno)
279{
280	struct mt76_rx_tid *tid;
281
282	rcu_read_lock();
283
284	tid = rcu_dereference(wcid->aggr[tidno]);
 
285	if (tid) {
286		rcu_assign_pointer(wcid->aggr[tidno], NULL);
287		mt76_rx_aggr_shutdown(dev, tid);
288		kfree_rcu(tid, rcu_head);
289	}
290
291	rcu_read_unlock();
292}
293EXPORT_SYMBOL_GPL(mt76_rx_aggr_stop);