xref: /linux/drivers/net/wireless/nxp/nxpwifi/11n_rxreorder.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * nxpwifi: 802.11n RX Re-ordering
4  *
5  * Copyright 2011-2024 NXP
6  */
7 
8 #include "cfg.h"
9 #include "util.h"
10 #include "fw.h"
11 #include "main.h"
12 #include "cmdevt.h"
13 #include "wmm.h"
14 #include "11n.h"
15 #include "11n_rxreorder.h"
16 /* Dispatch A-MSDU to stack. */
nxpwifi_11n_dispatch_amsdu_pkt(struct nxpwifi_private * priv,struct sk_buff * skb)17 static int nxpwifi_11n_dispatch_amsdu_pkt(struct nxpwifi_private *priv,
18 					  struct sk_buff *skb)
19 {
20 	struct rxpd *local_rx_pd = (struct rxpd *)(skb->data);
21 	int ret;
22 
23 	if (le16_to_cpu(local_rx_pd->rx_pkt_type) == PKT_TYPE_AMSDU) {
24 		struct sk_buff_head list;
25 		struct sk_buff *rx_skb;
26 
27 		__skb_queue_head_init(&list);
28 
29 		skb_pull(skb, le16_to_cpu(local_rx_pd->rx_pkt_offset));
30 		skb_trim(skb, le16_to_cpu(local_rx_pd->rx_pkt_length));
31 
32 		ieee80211_amsdu_to_8023s(skb, &list, priv->curr_addr,
33 					 priv->wdev.iftype, 0, NULL, NULL, false);
34 
35 		while (!skb_queue_empty(&list)) {
36 			rx_skb = __skb_dequeue(&list);
37 
38 			if (priv->bss_role == NXPWIFI_BSS_ROLE_UAP)
39 				ret = nxpwifi_uap_recv_packet(priv, rx_skb);
40 			else
41 				ret = nxpwifi_recv_packet(priv, rx_skb);
42 			if (ret)
43 				nxpwifi_dbg(priv->adapter, ERROR,
44 					    "Rx of A-MSDU failed");
45 		}
46 		return 0;
47 	}
48 
49 	return -EINVAL;
50 }
51 
52 /* Process RX packet and forward to stack. */
nxpwifi_11n_dispatch_pkt(struct nxpwifi_private * priv,struct sk_buff * payload)53 static int nxpwifi_11n_dispatch_pkt(struct nxpwifi_private *priv,
54 				    struct sk_buff *payload)
55 {
56 	int ret;
57 
58 	if (!payload) {
59 		nxpwifi_dbg(priv->adapter, INFO, "info: fw drop data\n");
60 		return 0;
61 	}
62 
63 	ret = nxpwifi_11n_dispatch_amsdu_pkt(priv, payload);
64 	if (!ret)
65 		return 0;
66 
67 	if (priv->bss_role == NXPWIFI_BSS_ROLE_UAP)
68 		return nxpwifi_handle_uap_rx_forward(priv, payload);
69 
70 	return nxpwifi_process_rx_packet(priv, payload);
71 }
72 
73 /* Dispatch packets up to start_win. */
74 static void
nxpwifi_11n_dispatch_pkt_until_start_win(struct nxpwifi_private * priv,struct nxpwifi_rx_reorder_tbl * tbl,int start_win)75 nxpwifi_11n_dispatch_pkt_until_start_win(struct nxpwifi_private *priv,
76 					 struct nxpwifi_rx_reorder_tbl *tbl,
77 					 int start_win)
78 {
79 	struct sk_buff_head list;
80 	struct sk_buff *skb;
81 	int pkt_to_send, i, tid;
82 
83 	tid = tbl->tid;
84 	__skb_queue_head_init(&list);
85 	spin_lock_bh(&priv->rx_reorder_tbl_lock[tid]);
86 
87 	pkt_to_send = (start_win > tbl->start_win) ?
88 		      min((start_win - tbl->start_win), tbl->win_size) :
89 		      tbl->win_size;
90 
91 	for (i = 0; i < pkt_to_send; ++i) {
92 		if (tbl->rx_reorder_ptr[i]) {
93 			skb = tbl->rx_reorder_ptr[i];
94 			__skb_queue_tail(&list, skb);
95 			tbl->rx_reorder_ptr[i] = NULL;
96 		}
97 	}
98 
99 	/* Simulate circular buffer via rotation. */
100 	for (i = 0; i < tbl->win_size - pkt_to_send; ++i) {
101 		tbl->rx_reorder_ptr[i] = tbl->rx_reorder_ptr[pkt_to_send + i];
102 		tbl->rx_reorder_ptr[pkt_to_send + i] = NULL;
103 	}
104 
105 	tbl->start_win = start_win;
106 	spin_unlock_bh(&priv->rx_reorder_tbl_lock[tid]);
107 
108 	while ((skb = __skb_dequeue(&list)))
109 		nxpwifi_11n_dispatch_pkt(priv, skb);
110 }
111 
112 /* Dispatch packets until a hole is found. */
113 static void
nxpwifi_11n_scan_and_dispatch(struct nxpwifi_private * priv,struct nxpwifi_rx_reorder_tbl * tbl)114 nxpwifi_11n_scan_and_dispatch(struct nxpwifi_private *priv,
115 			      struct nxpwifi_rx_reorder_tbl *tbl)
116 {
117 	struct sk_buff_head list;
118 	struct sk_buff *skb;
119 	int i, j, xchg, tid;
120 
121 	tid = tbl->tid;
122 	__skb_queue_head_init(&list);
123 	spin_lock_bh(&priv->rx_reorder_tbl_lock[tid]);
124 
125 	for (i = 0; i < tbl->win_size; ++i) {
126 		if (!tbl->rx_reorder_ptr[i])
127 			break;
128 		skb = tbl->rx_reorder_ptr[i];
129 		__skb_queue_tail(&list, skb);
130 		tbl->rx_reorder_ptr[i] = NULL;
131 	}
132 
133 	/* Simulate circular buffer via rotation. */
134 	if (i > 0) {
135 		xchg = tbl->win_size - i;
136 		for (j = 0; j < xchg; ++j) {
137 			tbl->rx_reorder_ptr[j] = tbl->rx_reorder_ptr[i + j];
138 			tbl->rx_reorder_ptr[i + j] = NULL;
139 		}
140 	}
141 	tbl->start_win = (tbl->start_win + i) & (MAX_TID_VALUE - 1);
142 
143 	spin_unlock_bh(&priv->rx_reorder_tbl_lock[tid]);
144 
145 	while ((skb = __skb_dequeue(&list)))
146 		nxpwifi_11n_dispatch_pkt(priv, skb);
147 }
148 
149 /* Delete RX reorder entry and flush pending packets. */
150 static void
nxpwifi_del_rx_reorder_entry(struct nxpwifi_private * priv,struct nxpwifi_rx_reorder_tbl * tbl)151 nxpwifi_del_rx_reorder_entry(struct nxpwifi_private *priv,
152 			     struct nxpwifi_rx_reorder_tbl *tbl)
153 {
154 	int start_win, tid;
155 
156 	if (!tbl)
157 		return;
158 
159 	tid = tbl->tid;
160 
161 	atomic_set(&priv->adapter->rx_ba_teardown_pending, 1);
162 	flush_workqueue(priv->adapter->rx_workqueue);
163 
164 	start_win = (tbl->start_win + tbl->win_size) & (MAX_TID_VALUE - 1);
165 	nxpwifi_11n_dispatch_pkt_until_start_win(priv, tbl, start_win);
166 
167 	timer_delete_sync(&tbl->timer_context.timer);
168 	tbl->timer_context.timer_is_set = false;
169 
170 	spin_lock_bh(&priv->rx_reorder_tbl_lock[tid]);
171 	list_del_rcu(&tbl->list);
172 	spin_unlock_bh(&priv->rx_reorder_tbl_lock[tid]);
173 
174 	kfree_rcu(tbl, rcu);
175 
176 	atomic_set(&priv->adapter->rx_ba_teardown_pending, 0);
177 }
178 
179 /* Lookup RX reorder entry by TID/TA. */
180 struct nxpwifi_rx_reorder_tbl *
nxpwifi_11n_get_rx_reorder_tbl(struct nxpwifi_private * priv,int tid,u8 * ta)181 nxpwifi_11n_get_rx_reorder_tbl(struct nxpwifi_private *priv, int tid, u8 *ta)
182 {
183 	struct nxpwifi_rx_reorder_tbl *tbl, *found = NULL;
184 
185 	guard(rcu)();
186 
187 	list_for_each_entry_rcu(tbl, &priv->rx_reorder_tbl_ptr[tid], list) {
188 		if (!memcmp(tbl->ta, ta, ETH_ALEN) && tbl->tid == tid) {
189 			found = tbl;
190 			break;
191 		}
192 	}
193 
194 	return found;
195 }
196 
197 /* Delete RX reorder entries by TA. */
nxpwifi_11n_del_rx_reorder_tbl_by_ta(struct nxpwifi_private * priv,u8 * ta)198 void nxpwifi_11n_del_rx_reorder_tbl_by_ta(struct nxpwifi_private *priv, u8 *ta)
199 {
200 	struct nxpwifi_rx_reorder_tbl *tbl, *tmp;
201 	LIST_HEAD(to_delete);
202 	int i;
203 
204 	if (!ta)
205 		return;
206 
207 	for (i = 0; i < MAX_NUM_TID; i++) {
208 		guard(rcu)();
209 		list_for_each_entry_rcu(tbl, &priv->rx_reorder_tbl_ptr[i], list) {
210 			if (!memcmp(tbl->ta, ta, ETH_ALEN)) {
211 				INIT_LIST_HEAD(&tbl->tmp_list);
212 				list_add_tail(&tbl->tmp_list, &to_delete);
213 			}
214 		}
215 
216 		list_for_each_entry_safe(tbl, tmp, &to_delete, tmp_list)
217 			nxpwifi_del_rx_reorder_entry(priv, tbl);
218 
219 		INIT_LIST_HEAD(&to_delete);
220 	}
221 }
222 
223 /* Find last buffered sequence index. */
224 static int
nxpwifi_11n_find_last_seq_num(struct reorder_tmr_cnxt * ctx)225 nxpwifi_11n_find_last_seq_num(struct reorder_tmr_cnxt *ctx)
226 {
227 	struct nxpwifi_rx_reorder_tbl *rx_reorder_tbl_ptr = ctx->ptr;
228 	int i;
229 
230 	guard(rcu)();
231 	for (i = rx_reorder_tbl_ptr->win_size - 1; i >= 0; --i) {
232 		if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
233 			return i;
234 	}
235 
236 	return -EINVAL;
237 }
238 
239 /* Flush and dispatch buffered packets on timer. */
240 static void
nxpwifi_flush_data(struct timer_list * t)241 nxpwifi_flush_data(struct timer_list *t)
242 {
243 	struct reorder_tmr_cnxt *ctx =
244 		timer_container_of(ctx, t, timer);
245 	int start_win, seq_num;
246 
247 	ctx->timer_is_set = false;
248 	seq_num = nxpwifi_11n_find_last_seq_num(ctx);
249 
250 	if (seq_num < 0)
251 		return;
252 
253 	nxpwifi_dbg(ctx->priv->adapter, INFO, "info: flush data %d\n", seq_num);
254 	start_win = (ctx->ptr->start_win + seq_num + 1) & (MAX_TID_VALUE - 1);
255 	nxpwifi_11n_dispatch_pkt_until_start_win(ctx->priv, ctx->ptr,
256 						 start_win);
257 }
258 
259 /* Create RX reorder entry (TID/TA, SSN, winsize, timer). */
260 static void
nxpwifi_11n_create_rx_reorder_tbl(struct nxpwifi_private * priv,u8 * ta,int tid,int win_size,int seq_num)261 nxpwifi_11n_create_rx_reorder_tbl(struct nxpwifi_private *priv, u8 *ta,
262 				  int tid, int win_size, int seq_num)
263 {
264 	struct nxpwifi_rx_reorder_tbl *tbl, *new_node;
265 	u16 last_seq = 0;
266 	struct nxpwifi_sta_node *node;
267 
268 	/* Existing TID/TA: flush and move window to SSN. */
269 	tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid, ta);
270 	if (tbl) {
271 		nxpwifi_11n_dispatch_pkt_until_start_win(priv, tbl, seq_num);
272 		return;
273 	}
274 
275 	if (win_size <= 0)
276 		return;
277 
278 	/* if !tbl then create one */
279 	new_node = kzalloc_flex(*new_node, rx_reorder_ptr, win_size);
280 	if (!new_node)
281 		return;
282 
283 	new_node->win_size = win_size;
284 	INIT_LIST_HEAD(&new_node->list);
285 	new_node->tid = tid;
286 	memcpy(new_node->ta, ta, ETH_ALEN);
287 	new_node->start_win = seq_num;
288 	new_node->init_win = seq_num;
289 	new_node->flags = 0;
290 
291 	if (nxpwifi_queuing_ra_based(priv)) {
292 		if (priv->bss_role == NXPWIFI_BSS_ROLE_UAP) {
293 			guard(rcu)();
294 			node = nxpwifi_get_sta_entry(priv, ta);
295 			if (node)
296 				last_seq = node->rx_seq[tid];
297 		}
298 	} else {
299 		guard(rcu)();
300 		node = nxpwifi_get_sta_entry(priv, ta);
301 		if (node)
302 			last_seq = node->rx_seq[tid];
303 		else
304 			last_seq = priv->rx_seq[tid];
305 	}
306 
307 	nxpwifi_dbg(priv->adapter, INFO,
308 		    "info: last_seq=%d start_win=%d\n",
309 		    last_seq, new_node->start_win);
310 
311 	if (last_seq != NXPWIFI_DEF_11N_RX_SEQ_NUM &&
312 	    last_seq >= new_node->start_win) {
313 		new_node->start_win = last_seq + 1;
314 		new_node->flags |= RXREOR_INIT_WINDOW_SHIFT;
315 	}
316 
317 	new_node->timer_context.ptr = new_node;
318 	new_node->timer_context.priv = priv;
319 	new_node->timer_context.timer_is_set = false;
320 
321 	timer_setup(&new_node->timer_context.timer, nxpwifi_flush_data, 0);
322 
323 	spin_lock_bh(&priv->rx_reorder_tbl_lock[tid]);
324 	list_add_tail_rcu(&new_node->list, &priv->rx_reorder_tbl_ptr[tid]);
325 	spin_unlock_bh(&priv->rx_reorder_tbl_lock[tid]);
326 }
327 
328 static void
nxpwifi_11n_rxreorder_timer_restart(struct nxpwifi_rx_reorder_tbl * tbl)329 nxpwifi_11n_rxreorder_timer_restart(struct nxpwifi_rx_reorder_tbl *tbl)
330 {
331 	u32 min_flush_time;
332 
333 	if (tbl->win_size >= NXPWIFI_BA_WIN_SIZE_32)
334 		min_flush_time = MIN_FLUSH_TIMER_15_MS;
335 	else
336 		min_flush_time = MIN_FLUSH_TIMER_MS;
337 
338 	mod_timer(&tbl->timer_context.timer,
339 		  jiffies + msecs_to_jiffies(min_flush_time * tbl->win_size));
340 
341 	tbl->timer_context.timer_is_set = true;
342 }
343 
344 /* Prepare ADDBA request. */
nxpwifi_cmd_11n_addba_req(struct host_cmd_ds_command * cmd,void * data_buf)345 int nxpwifi_cmd_11n_addba_req(struct host_cmd_ds_command *cmd, void *data_buf)
346 {
347 	struct host_cmd_ds_11n_addba_req *add_ba_req = &cmd->params.add_ba_req;
348 
349 	cmd->command = cpu_to_le16(HOST_CMD_11N_ADDBA_REQ);
350 	cmd->size = cpu_to_le16(sizeof(*add_ba_req) + S_DS_GEN);
351 	memcpy(add_ba_req, data_buf, sizeof(*add_ba_req));
352 
353 	return 0;
354 }
355 
356 /* Prepare ADDBA response and create RX reorder table. */
nxpwifi_cmd_11n_addba_rsp_gen(struct nxpwifi_private * priv,struct host_cmd_ds_command * cmd,struct host_cmd_ds_11n_addba_req * cmd_addba_req)357 int nxpwifi_cmd_11n_addba_rsp_gen(struct nxpwifi_private *priv,
358 				  struct host_cmd_ds_command *cmd,
359 				  struct host_cmd_ds_11n_addba_req
360 				  *cmd_addba_req)
361 {
362 	struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &cmd->params.add_ba_rsp;
363 	u32 rx_win_size = priv->add_ba_param.rx_win_size;
364 	u8 tid;
365 	int win_size;
366 	u16 block_ack_param_set;
367 
368 	cmd->command = cpu_to_le16(HOST_CMD_11N_ADDBA_RSP);
369 	cmd->size = cpu_to_le16(sizeof(*add_ba_rsp) + S_DS_GEN);
370 
371 	memcpy(add_ba_rsp->peer_mac_addr, cmd_addba_req->peer_mac_addr,
372 	       ETH_ALEN);
373 	add_ba_rsp->dialog_token = cmd_addba_req->dialog_token;
374 	add_ba_rsp->block_ack_tmo = cmd_addba_req->block_ack_tmo;
375 	add_ba_rsp->ssn = cmd_addba_req->ssn;
376 
377 	block_ack_param_set = le16_to_cpu(cmd_addba_req->block_ack_param_set);
378 	tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
379 		>> BLOCKACKPARAM_TID_POS;
380 	add_ba_rsp->status_code = cpu_to_le16(ADDBA_RSP_STATUS_ACCEPT);
381 	block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK;
382 
383 	/* If we don't support AMSDU inside AMPDU, reset the bit */
384 	if (!priv->add_ba_param.rx_amsdu ||
385 	    priv->aggr_prio_tbl[tid].amsdu == BA_STREAM_NOT_ALLOWED)
386 		block_ack_param_set &= ~IEEE80211_ADDBA_PARAM_AMSDU_MASK;
387 	block_ack_param_set |= rx_win_size << BLOCKACKPARAM_WINSIZE_POS;
388 	add_ba_rsp->block_ack_param_set = cpu_to_le16(block_ack_param_set);
389 	win_size = (le16_to_cpu(add_ba_rsp->block_ack_param_set)
390 		    & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
391 		   >> BLOCKACKPARAM_WINSIZE_POS;
392 	cmd_addba_req->block_ack_param_set = cpu_to_le16(block_ack_param_set);
393 
394 	nxpwifi_11n_create_rx_reorder_tbl(priv, cmd_addba_req->peer_mac_addr,
395 					  tid, win_size,
396 					  le16_to_cpu(cmd_addba_req->ssn));
397 	return 0;
398 }
399 
400 /* Prepare DELBA command. */
nxpwifi_cmd_11n_delba(struct host_cmd_ds_command * cmd,void * data_buf)401 int nxpwifi_cmd_11n_delba(struct host_cmd_ds_command *cmd, void *data_buf)
402 {
403 	struct host_cmd_ds_11n_delba *del_ba = &cmd->params.del_ba;
404 
405 	cmd->command = cpu_to_le16(HOST_CMD_11N_DELBA);
406 	cmd->size = cpu_to_le16(sizeof(*del_ba) + S_DS_GEN);
407 	memcpy(del_ba, data_buf, sizeof(*del_ba));
408 
409 	return 0;
410 }
411 
412 /* Decide and perform RX reordering for a packet. */
nxpwifi_11n_rx_reorder_pkt(struct nxpwifi_private * priv,u16 seq_num,u16 tid,u8 * ta,u8 pkt_type,void * payload)413 int nxpwifi_11n_rx_reorder_pkt(struct nxpwifi_private *priv,
414 			       u16 seq_num, u16 tid,
415 			       u8 *ta, u8 pkt_type, void *payload)
416 {
417 	struct nxpwifi_rx_reorder_tbl *tbl;
418 	int prev_start_win, start_win, end_win, win_size;
419 	u16 pkt_index;
420 	bool init_window_shift = false;
421 	int ret = 0;
422 
423 	tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid, ta);
424 	if (!tbl) {
425 		if (pkt_type != PKT_TYPE_BAR)
426 			nxpwifi_11n_dispatch_pkt(priv, payload);
427 		return ret;
428 	}
429 
430 	if (pkt_type == PKT_TYPE_AMSDU && !tbl->amsdu) {
431 		nxpwifi_11n_dispatch_pkt(priv, payload);
432 		return ret;
433 	}
434 
435 	start_win = tbl->start_win;
436 	prev_start_win = start_win;
437 	win_size = tbl->win_size;
438 	end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
439 	if (tbl->flags & RXREOR_INIT_WINDOW_SHIFT) {
440 		init_window_shift = true;
441 		tbl->flags &= ~RXREOR_INIT_WINDOW_SHIFT;
442 	}
443 
444 	if (tbl->flags & RXREOR_FORCE_NO_DROP) {
445 		nxpwifi_dbg(priv->adapter, INFO,
446 			    "RXREOR_FORCE_NO_DROP when HS is activated\n");
447 		tbl->flags &= ~RXREOR_FORCE_NO_DROP;
448 	} else if (init_window_shift && seq_num < start_win &&
449 		   seq_num >= tbl->init_win) {
450 		nxpwifi_dbg(priv->adapter, INFO,
451 			    "Sender TID sequence number reset %d->%d for SSN %d\n",
452 			    start_win, seq_num, tbl->init_win);
453 		start_win = seq_num;
454 		tbl->start_win = start_win;
455 		end_win = ((start_win + win_size) - 1) & (MAX_TID_VALUE - 1);
456 	} else {
457 		/* Drop packet if seq_num < start_win. */
458 		if ((start_win + TWOPOW11) > (MAX_TID_VALUE - 1)) {
459 			if (seq_num >= ((start_win + TWOPOW11) &
460 					(MAX_TID_VALUE - 1)) &&
461 			    seq_num < start_win) {
462 				ret = -EINVAL;
463 				goto done;
464 			}
465 		} else if ((seq_num < start_win) ||
466 			   (seq_num >= (start_win + TWOPOW11))) {
467 			ret = -EINVAL;
468 			goto done;
469 		}
470 	}
471 
472 	/* Adjust seq_num for BAR (WinStart = seq_num). */
473 	if (pkt_type == PKT_TYPE_BAR)
474 		seq_num = ((seq_num + win_size) - 1) & (MAX_TID_VALUE - 1);
475 
476 	if ((end_win < start_win &&
477 	     seq_num < start_win && seq_num > end_win) ||
478 	    (end_win > start_win && (seq_num > end_win ||
479 				     seq_num < start_win))) {
480 		end_win = seq_num;
481 		if (((end_win - win_size) + 1) >= 0)
482 			start_win = (end_win - win_size) + 1;
483 		else
484 			start_win = (MAX_TID_VALUE - (win_size - end_win)) + 1;
485 		nxpwifi_11n_dispatch_pkt_until_start_win(priv, tbl, start_win);
486 	}
487 
488 	if (pkt_type != PKT_TYPE_BAR) {
489 		if (seq_num >= start_win)
490 			pkt_index = seq_num - start_win;
491 		else
492 			pkt_index = (seq_num + MAX_TID_VALUE) - start_win;
493 
494 		if (tbl->rx_reorder_ptr[pkt_index]) {
495 			ret = -EINVAL;
496 			goto done;
497 		}
498 
499 		tbl->rx_reorder_ptr[pkt_index] = payload;
500 	}
501 
502 	/* Dispatch sequentially until a hole; update start_win. */
503 	nxpwifi_11n_scan_and_dispatch(priv, tbl);
504 
505 done:
506 	if (!tbl->timer_context.timer_is_set ||
507 	    prev_start_win != tbl->start_win)
508 		nxpwifi_11n_rxreorder_timer_restart(tbl);
509 	return ret;
510 }
511 
512 /* Delete BA entry for TID/TA. */
513 void
nxpwifi_del_ba_tbl(struct nxpwifi_private * priv,int tid,u8 * peer_mac,u8 type,int initiator)514 nxpwifi_del_ba_tbl(struct nxpwifi_private *priv, int tid, u8 *peer_mac,
515 		   u8 type, int initiator)
516 {
517 	struct nxpwifi_rx_reorder_tbl *tbl;
518 	struct nxpwifi_tx_ba_stream_tbl *ptx_tbl;
519 	struct nxpwifi_ra_list_tbl *ra_list;
520 	u8 cleanup_rx_reorder_tbl;
521 	int tid_down;
522 
523 	if (type == TYPE_DELBA_RECEIVE)
524 		cleanup_rx_reorder_tbl = (initiator) ? true : false;
525 	else
526 		cleanup_rx_reorder_tbl = (initiator) ? false : true;
527 
528 	nxpwifi_dbg(priv->adapter, EVENT, "event: DELBA: %pM tid=%d initiator=%d\n",
529 		    peer_mac, tid, initiator);
530 
531 	if (cleanup_rx_reorder_tbl) {
532 		tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid, peer_mac);
533 		if (!tbl) {
534 			nxpwifi_dbg(priv->adapter, EVENT,
535 				    "event: TID, TA not found in table\n");
536 			return;
537 		}
538 		nxpwifi_del_rx_reorder_entry(priv, tbl);
539 	} else {
540 		guard(rcu)();
541 		ptx_tbl = nxpwifi_get_ba_tbl(priv, tid, peer_mac);
542 
543 		if (!ptx_tbl) {
544 			nxpwifi_dbg(priv->adapter, EVENT,
545 				    "event: TID, RA not found in table\n");
546 			return;
547 		}
548 
549 		tid_down = nxpwifi_wmm_downgrade_tid(priv, tid);
550 		ra_list = nxpwifi_wmm_get_ralist_node(priv, tid_down, peer_mac);
551 		if (ra_list) {
552 			ra_list->amsdu_in_ampdu = false;
553 			ra_list->ba_status = BA_SETUP_NONE;
554 		}
555 		spin_lock_bh(&priv->tx_ba_stream_tbl_lock[tid]);
556 		nxpwifi_11n_delete_tx_ba_stream_tbl_entry(priv, ptx_tbl);
557 		spin_unlock_bh(&priv->tx_ba_stream_tbl_lock[tid]);
558 	}
559 }
560 
561 /* Handle ADDBA response. */
nxpwifi_ret_11n_addba_resp(struct nxpwifi_private * priv,struct host_cmd_ds_command * resp)562 int nxpwifi_ret_11n_addba_resp(struct nxpwifi_private *priv,
563 			       struct host_cmd_ds_command *resp)
564 {
565 	struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
566 	int tid, win_size;
567 	struct nxpwifi_rx_reorder_tbl *tbl;
568 	u16 block_ack_param_set;
569 
570 	block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
571 
572 	tid = (block_ack_param_set & IEEE80211_ADDBA_PARAM_TID_MASK)
573 		>> BLOCKACKPARAM_TID_POS;
574 	/* Check if we had rejected the ADDBA, if yes then do not create the stream */
575 	if (le16_to_cpu(add_ba_rsp->status_code) != BA_RESULT_SUCCESS) {
576 		nxpwifi_dbg(priv->adapter, ERROR, "ADDBA RSP: failed %pM tid=%d)\n",
577 			    add_ba_rsp->peer_mac_addr, tid);
578 
579 		tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid,
580 						     add_ba_rsp->peer_mac_addr);
581 		if (tbl)
582 			nxpwifi_del_rx_reorder_entry(priv, tbl);
583 
584 		return 0;
585 	}
586 
587 	win_size = (block_ack_param_set & IEEE80211_ADDBA_PARAM_BUF_SIZE_MASK)
588 		    >> BLOCKACKPARAM_WINSIZE_POS;
589 
590 	tbl = nxpwifi_11n_get_rx_reorder_tbl(priv, tid,
591 					     add_ba_rsp->peer_mac_addr);
592 	if (tbl) {
593 		if ((block_ack_param_set & IEEE80211_ADDBA_PARAM_AMSDU_MASK) &&
594 		    priv->add_ba_param.rx_amsdu &&
595 		    priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED)
596 			tbl->amsdu = true;
597 		else
598 			tbl->amsdu = false;
599 	}
600 
601 	nxpwifi_dbg(priv->adapter, CMD,
602 		    "cmd: ADDBA RSP: %pM tid=%d ssn=%d win_size=%d\n",
603 		    add_ba_rsp->peer_mac_addr, tid, add_ba_rsp->ssn, win_size);
604 
605 	return 0;
606 }
607 
608 /* Handle BA stream timeout: send DELBA. */
nxpwifi_11n_ba_stream_timeout(struct nxpwifi_private * priv,struct host_cmd_ds_11n_batimeout * event)609 void nxpwifi_11n_ba_stream_timeout(struct nxpwifi_private *priv,
610 				   struct host_cmd_ds_11n_batimeout *event)
611 {
612 	struct host_cmd_ds_11n_delba delba;
613 
614 	memset(&delba, 0, sizeof(struct host_cmd_ds_11n_delba));
615 	memcpy(delba.peer_mac_addr, event->peer_mac_addr, ETH_ALEN);
616 
617 	delba.del_ba_param_set |=
618 		cpu_to_le16((u16)event->tid << DELBA_TID_POS);
619 	delba.del_ba_param_set |=
620 		cpu_to_le16((u16)event->origninator << DELBA_INITIATOR_POS);
621 	delba.reason_code = cpu_to_le16(WLAN_REASON_QSTA_TIMEOUT);
622 	nxpwifi_send_cmd(priv, HOST_CMD_11N_DELBA, 0, 0, &delba, false);
623 }
624 
625 /* Cleanup all RX reorder entries. */
nxpwifi_11n_cleanup_reorder_tbl(struct nxpwifi_private * priv)626 void nxpwifi_11n_cleanup_reorder_tbl(struct nxpwifi_private *priv)
627 {
628 	struct nxpwifi_rx_reorder_tbl *del_tbl_ptr, *tmp_node;
629 	LIST_HEAD(to_delete_list);
630 	int i;
631 
632 	for (i = 0; i < MAX_NUM_TID; i++) {
633 		spin_lock_bh(&priv->rx_reorder_tbl_lock[i]);
634 		list_splice_init(&priv->rx_reorder_tbl_ptr[i], &to_delete_list);
635 		spin_unlock_bh(&priv->rx_reorder_tbl_lock[i]);
636 
637 		list_for_each_entry_safe(del_tbl_ptr, tmp_node, &to_delete_list, list)
638 			nxpwifi_del_rx_reorder_entry(priv, del_tbl_ptr);
639 
640 		INIT_LIST_HEAD(&to_delete_list);
641 	}
642 
643 	nxpwifi_reset_11n_rx_seq_num(priv);
644 }
645 
646 /* Update flags for all RX reorder tables. */
nxpwifi_update_rxreor_flags(struct nxpwifi_adapter * adapter,u8 flags)647 void nxpwifi_update_rxreor_flags(struct nxpwifi_adapter *adapter, u8 flags)
648 {
649 	struct nxpwifi_private *priv;
650 	struct nxpwifi_rx_reorder_tbl *tbl;
651 	int i, j;
652 
653 	for (i = 0; i < adapter->priv_num; i++) {
654 		priv = adapter->priv[i];
655 
656 		for (j = 0; j < MAX_NUM_TID; j++) {
657 			spin_lock_bh(&priv->rx_reorder_tbl_lock[j]);
658 			list_for_each_entry_rcu(tbl, &priv->rx_reorder_tbl_ptr[j], list)
659 				tbl->flags = flags;
660 			spin_unlock_bh(&priv->rx_reorder_tbl_lock[j]);
661 		}
662 	}
663 }
664 
665 /* Update RX window size based on coex flag. */
nxpwifi_update_ampdu_rxwinsize(struct nxpwifi_adapter * adapter,bool coex_flag)666 static void nxpwifi_update_ampdu_rxwinsize(struct nxpwifi_adapter *adapter,
667 					   bool coex_flag)
668 {
669 	u8 i, j;
670 	u32 rx_win_size;
671 	struct nxpwifi_private *priv;
672 
673 	nxpwifi_dbg(adapter, INFO, "Update rxwinsize %d\n", coex_flag);
674 
675 	for (i = 0; i < adapter->priv_num; i++) {
676 		priv = adapter->priv[i];
677 		rx_win_size = priv->add_ba_param.rx_win_size;
678 		if (coex_flag) {
679 			if (priv->bss_type == NXPWIFI_BSS_TYPE_STA)
680 				priv->add_ba_param.rx_win_size =
681 					NXPWIFI_STA_COEX_AMPDU_DEF_RXWINSIZE;
682 			if (priv->bss_type == NXPWIFI_BSS_TYPE_UAP)
683 				priv->add_ba_param.rx_win_size =
684 					NXPWIFI_UAP_COEX_AMPDU_DEF_RXWINSIZE;
685 		} else {
686 			if (priv->bss_type == NXPWIFI_BSS_TYPE_STA)
687 				priv->add_ba_param.rx_win_size =
688 					NXPWIFI_STA_AMPDU_DEF_RXWINSIZE;
689 			if (priv->bss_type == NXPWIFI_BSS_TYPE_UAP)
690 				priv->add_ba_param.rx_win_size =
691 					NXPWIFI_UAP_AMPDU_DEF_RXWINSIZE;
692 		}
693 
694 		if (adapter->coex_win_size && adapter->coex_rx_win_size)
695 			priv->add_ba_param.rx_win_size =
696 				adapter->coex_rx_win_size;
697 
698 		if (rx_win_size != priv->add_ba_param.rx_win_size) {
699 			if (!priv->media_connected)
700 				continue;
701 			for (j = 0; j < MAX_NUM_TID; j++)
702 				nxpwifi_11n_delba(priv, j);
703 		}
704 	}
705 }
706 
707 /* Check coex for RX BA. */
nxpwifi_coex_ampdu_rxwinsize(struct nxpwifi_adapter * adapter)708 void nxpwifi_coex_ampdu_rxwinsize(struct nxpwifi_adapter *adapter)
709 {
710 	u8 i;
711 	struct nxpwifi_private *priv;
712 	u8 count = 0;
713 
714 	for (i = 0; i < adapter->priv_num; i++) {
715 		priv = adapter->priv[i];
716 		if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_STA) {
717 			if (priv->media_connected)
718 				count++;
719 		}
720 		if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
721 			if (priv->bss_started)
722 				count++;
723 		}
724 		if (count >= NXPWIFI_BSS_COEX_COUNT)
725 			break;
726 	}
727 	if (count >= NXPWIFI_BSS_COEX_COUNT)
728 		nxpwifi_update_ampdu_rxwinsize(adapter, true);
729 	else
730 		nxpwifi_update_ampdu_rxwinsize(adapter, false);
731 }
732 
733 /* Handle RXBA sync event. */
nxpwifi_11n_rxba_sync_event(struct nxpwifi_private * priv,u8 * event_buf,u16 len)734 void nxpwifi_11n_rxba_sync_event(struct nxpwifi_private *priv,
735 				 u8 *event_buf, u16 len)
736 {
737 	struct nxpwifi_ie_types_rxba_sync *tlv_rxba = (void *)event_buf;
738 	u16 tlv_type, tlv_len;
739 	struct nxpwifi_rx_reorder_tbl *rx_reor_tbl_ptr;
740 	u8 i, j;
741 	u16 seq_num, tlv_seq_num, tlv_bitmap_len;
742 	int tlv_buf_left = len;
743 	int ret;
744 	u8 *tmp;
745 
746 	nxpwifi_dbg_dump(priv->adapter, EVT_D, "RXBA_SYNC event:",
747 			 event_buf, len);
748 	while (tlv_buf_left > sizeof(*tlv_rxba)) {
749 		tlv_type = le16_to_cpu(tlv_rxba->header.type);
750 		tlv_len  = le16_to_cpu(tlv_rxba->header.len);
751 		if (size_add(sizeof(tlv_rxba->header), tlv_len) > tlv_buf_left) {
752 			nxpwifi_dbg(priv->adapter, WARN,
753 				    "TLV size (%zu) overflows event_buf buf_left=%d\n",
754 				    size_add(sizeof(tlv_rxba->header), tlv_len),
755 				    tlv_buf_left);
756 			return;
757 		}
758 
759 		if (tlv_type != TLV_TYPE_RXBA_SYNC) {
760 			nxpwifi_dbg(priv->adapter, ERROR,
761 				    "Wrong TLV id=0x%x\n", tlv_type);
762 			return;
763 		}
764 
765 		tlv_seq_num = le16_to_cpu(tlv_rxba->seq_num);
766 		tlv_bitmap_len = le16_to_cpu(tlv_rxba->bitmap_len);
767 		if (size_add(sizeof(*tlv_rxba), tlv_bitmap_len) > tlv_buf_left) {
768 			nxpwifi_dbg(priv->adapter, WARN,
769 				    "TLV size (%zu) overflows event_buf buf_left=%d\n",
770 				    size_add(sizeof(*tlv_rxba), tlv_bitmap_len),
771 				    tlv_buf_left);
772 			return;
773 		}
774 
775 		nxpwifi_dbg(priv->adapter, INFO,
776 			    "%pM tid=%d seq_num=%d bitmap_len=%d\n",
777 			    tlv_rxba->mac, tlv_rxba->tid, tlv_seq_num,
778 			    tlv_bitmap_len);
779 
780 		rx_reor_tbl_ptr =
781 			nxpwifi_11n_get_rx_reorder_tbl(priv, tlv_rxba->tid,
782 						       tlv_rxba->mac);
783 		if (!rx_reor_tbl_ptr) {
784 			nxpwifi_dbg(priv->adapter, ERROR,
785 				    "Can not find rx_reorder_tbl!");
786 			return;
787 		}
788 
789 		for (i = 0; i < tlv_bitmap_len; i++) {
790 			for (j = 0 ; j < 8; j++) {
791 				if (tlv_rxba->bitmap[i] & (1 << j)) {
792 					seq_num = (MAX_TID_VALUE - 1) &
793 						(tlv_seq_num + i * 8 + j);
794 
795 					nxpwifi_dbg(priv->adapter, ERROR,
796 						    "drop packet,seq=%d\n",
797 						    seq_num);
798 
799 					ret = nxpwifi_11n_rx_reorder_pkt
800 					(priv, seq_num, tlv_rxba->tid,
801 					 tlv_rxba->mac, 0, NULL);
802 
803 					if (ret)
804 						nxpwifi_dbg(priv->adapter,
805 							    ERROR,
806 							    "Fail to drop packet");
807 				}
808 			}
809 		}
810 
811 		tlv_buf_left -= (sizeof(tlv_rxba->header) + tlv_len);
812 		tmp = (u8 *)tlv_rxba + sizeof(tlv_rxba->header) + tlv_len;
813 		tlv_rxba = (struct nxpwifi_ie_types_rxba_sync *)tmp;
814 	}
815 }
816