xref: /linux/drivers/net/wireless/nxp/nxpwifi/11n.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * nxpwifi 802.11n helpers
4  * Copyright 2011-2024 NXP
5  */
6 
7 #include "cfg.h"
8 #include "util.h"
9 #include "fw.h"
10 #include "main.h"
11 #include "cmdevt.h"
12 #include "wmm.h"
13 #include "11n.h"
14 #include "11ax.h"
15 
16 /*
17  * Fills HT capability information field, AMPDU Parameters field, HT extended
18  * capability field, and supported MCS set fields.
19  *
20  * HT capability information field, AMPDU Parameters field, supported MCS set
21  * fields are retrieved from cfg80211 stack
22  *
23  * RD responder bit to set to clear in the extended capability header.
24  */
nxpwifi_fill_cap_info(struct nxpwifi_private * priv,u8 radio_type,struct ieee80211_ht_cap * ht_cap)25 int nxpwifi_fill_cap_info(struct nxpwifi_private *priv, u8 radio_type,
26 			  struct ieee80211_ht_cap *ht_cap)
27 {
28 	u16 ht_cap_info;
29 	u16 bcn_ht_cap = le16_to_cpu(ht_cap->cap_info);
30 	u16 ht_ext_cap = le16_to_cpu(ht_cap->extended_ht_cap_info);
31 	struct ieee80211_supported_band *sband =
32 		priv->wdev.wiphy->bands[radio_type];
33 
34 	if (WARN_ON_ONCE(!sband)) {
35 		nxpwifi_dbg(priv->adapter, ERROR, "Invalid radio type!\n");
36 		return -EINVAL;
37 	}
38 
39 	ht_cap->ampdu_params_info =
40 		(AMPDU_FACTOR_64K & IEEE80211_HT_AMPDU_PARM_FACTOR) |
41 		((priv->adapter->hw_mpdu_density <<
42 		 IEEE80211_HT_AMPDU_PARM_DENSITY_SHIFT) &
43 		 IEEE80211_HT_AMPDU_PARM_DENSITY);
44 
45 	memcpy((u8 *)&ht_cap->mcs, &sband->ht_cap.mcs,
46 	       sizeof(sband->ht_cap.mcs));
47 
48 	if (priv->bss_mode == NL80211_IFTYPE_STATION ||
49 	    (sband->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
50 	     priv->adapter->sec_chan_offset != IEEE80211_HT_PARAM_CHA_SEC_NONE))
51 		/* Set MCS32 for infra mode or ad-hoc mode with 40MHz support */
52 		SETHT_MCS32(ht_cap->mcs.rx_mask);
53 
54 	/* Clear RD responder bit */
55 	ht_ext_cap &= ~IEEE80211_HT_EXT_CAP_RD_RESPONDER;
56 
57 	ht_cap_info = sband->ht_cap.cap;
58 	if (bcn_ht_cap) {
59 		if (!(bcn_ht_cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
60 			ht_cap_info &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
61 		if (!(bcn_ht_cap & IEEE80211_HT_CAP_SGI_40))
62 			ht_cap_info &= ~IEEE80211_HT_CAP_SGI_40;
63 		if (!(bcn_ht_cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT))
64 			ht_cap_info &= ~IEEE80211_HT_CAP_40MHZ_INTOLERANT;
65 	}
66 	ht_cap->cap_info = cpu_to_le16(ht_cap_info);
67 	ht_cap->extended_ht_cap_info = cpu_to_le16(ht_ext_cap);
68 
69 	if (ISSUPP_BEAMFORMING(priv->adapter->hw_dot_11n_dev_cap))
70 		ht_cap->tx_BF_cap_info = cpu_to_le32(NXPWIFI_DEF_11N_TX_BF_CAP);
71 
72 	return 0;
73 }
74 
75 /* Return BA stream entry that matches the requested status. */
76 static struct nxpwifi_tx_ba_stream_tbl *
nxpwifi_get_ba_status(struct nxpwifi_private * priv,int tid,enum nxpwifi_ba_status ba_status)77 nxpwifi_get_ba_status(struct nxpwifi_private *priv, int tid,
78 		      enum nxpwifi_ba_status ba_status)
79 {
80 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tsr_tbl, *found = NULL;
81 
82 	guard(rcu)();
83 	list_for_each_entry_rcu(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr[tid], list) {
84 		if (tx_ba_tsr_tbl->ba_status == ba_status) {
85 			found = tx_ba_tsr_tbl;
86 			break;
87 		}
88 	}
89 	return found;
90 }
91 
92 /* Handle DELBA command response (recreate or continue ADDBA as needed). */
nxpwifi_ret_11n_delba(struct nxpwifi_private * priv,struct host_cmd_ds_command * resp)93 int nxpwifi_ret_11n_delba(struct nxpwifi_private *priv,
94 			  struct host_cmd_ds_command *resp)
95 {
96 	int tid;
97 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tbl;
98 	struct host_cmd_ds_11n_delba *del_ba = &resp->params.del_ba;
99 	u16 del_ba_param_set = le16_to_cpu(del_ba->del_ba_param_set);
100 
101 	tid = del_ba_param_set >> DELBA_TID_POS;
102 	if (del_ba->del_result == BA_RESULT_SUCCESS) {
103 		nxpwifi_del_ba_tbl(priv, tid, del_ba->peer_mac_addr,
104 				   TYPE_DELBA_SENT,
105 				   INITIATOR_BIT(del_ba_param_set));
106 
107 		tx_ba_tbl = nxpwifi_get_ba_status(priv, tid, BA_SETUP_INPROGRESS);
108 		if (tx_ba_tbl)
109 			nxpwifi_send_addba(priv, tx_ba_tbl->tid,
110 					   tx_ba_tbl->ra);
111 	} else {
112 		/*
113 		 * In case of failure, recreate the deleted stream in case
114 		 * we initiated the DELBA
115 		 */
116 		if (!INITIATOR_BIT(del_ba_param_set))
117 			return 0;
118 
119 		nxpwifi_create_ba_tbl(priv, del_ba->peer_mac_addr, tid,
120 				      BA_SETUP_INPROGRESS);
121 
122 		tx_ba_tbl = nxpwifi_get_ba_status(priv, tid, BA_SETUP_INPROGRESS);
123 
124 		if (tx_ba_tbl)
125 			nxpwifi_del_ba_tbl(priv, tx_ba_tbl->tid, tx_ba_tbl->ra,
126 					   TYPE_DELBA_SENT, true);
127 	}
128 
129 	return 0;
130 }
131 
132 /* Handle ADDBA response; delete BA stream on failure. */
nxpwifi_ret_11n_addba_req(struct nxpwifi_private * priv,struct host_cmd_ds_command * resp)133 int nxpwifi_ret_11n_addba_req(struct nxpwifi_private *priv,
134 			      struct host_cmd_ds_command *resp)
135 {
136 	int tid, tid_down;
137 	struct host_cmd_ds_11n_addba_rsp *add_ba_rsp = &resp->params.add_ba_rsp;
138 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tbl;
139 	struct nxpwifi_ra_list_tbl *ra_list;
140 	u16 block_ack_param_set = le16_to_cpu(add_ba_rsp->block_ack_param_set);
141 
142 	add_ba_rsp->ssn = cpu_to_le16((le16_to_cpu(add_ba_rsp->ssn))
143 			& SSN_MASK);
144 
145 	tid = u16_get_bits(block_ack_param_set, IEEE80211_ADDBA_PARAM_TID_MASK);
146 
147 	tid_down = nxpwifi_wmm_downgrade_tid(priv, tid);
148 	ra_list = nxpwifi_wmm_get_ralist_node(priv, tid_down,
149 					      add_ba_rsp->peer_mac_addr);
150 	if (le16_to_cpu(add_ba_rsp->status_code) != BA_RESULT_SUCCESS) {
151 		if (ra_list) {
152 			ra_list->ba_status = BA_SETUP_NONE;
153 			ra_list->amsdu_in_ampdu = false;
154 		}
155 		nxpwifi_del_ba_tbl(priv, tid, add_ba_rsp->peer_mac_addr,
156 				   TYPE_DELBA_SENT, true);
157 		if (add_ba_rsp->add_rsp_result != BA_RESULT_TIMEOUT)
158 			priv->aggr_prio_tbl[tid].ampdu_ap =
159 				BA_STREAM_NOT_ALLOWED;
160 		return 0;
161 	}
162 
163 	guard(rcu)();
164 	tx_ba_tbl = nxpwifi_get_ba_tbl(priv, tid, add_ba_rsp->peer_mac_addr);
165 	if (tx_ba_tbl) {
166 		nxpwifi_dbg(priv->adapter, EVENT, "info: BA stream complete\n");
167 		tx_ba_tbl->ba_status = BA_SETUP_COMPLETE;
168 		if ((block_ack_param_set & IEEE80211_ADDBA_PARAM_AMSDU_MASK) &&
169 		    priv->add_ba_param.tx_amsdu &&
170 		    priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED)
171 			tx_ba_tbl->amsdu = true;
172 		else
173 			tx_ba_tbl->amsdu = false;
174 		if (ra_list) {
175 			ra_list->amsdu_in_ampdu = tx_ba_tbl->amsdu;
176 			ra_list->ba_status = BA_SETUP_COMPLETE;
177 		}
178 	} else {
179 		nxpwifi_dbg(priv->adapter, ERROR, "BA stream not created\n");
180 	}
181 
182 	return 0;
183 }
184 
185 /*
186  * Reconfigure Tx buffer command.
187  * Set command ID/action/size; set Tx buffer size on SET; ensure little-endian.
188  */
nxpwifi_cmd_recfg_tx_buf(struct nxpwifi_private * priv,struct host_cmd_ds_command * cmd,int cmd_action,u16 * buf_size)189 int nxpwifi_cmd_recfg_tx_buf(struct nxpwifi_private *priv,
190 			     struct host_cmd_ds_command *cmd, int cmd_action,
191 			     u16 *buf_size)
192 {
193 	struct host_cmd_ds_txbuf_cfg *tx_buf = &cmd->params.tx_buf;
194 	u16 action = (u16)cmd_action;
195 
196 	cmd->command = cpu_to_le16(HOST_CMD_RECONFIGURE_TX_BUFF);
197 	cmd->size =
198 		cpu_to_le16(sizeof(struct host_cmd_ds_txbuf_cfg) + S_DS_GEN);
199 	tx_buf->action = cpu_to_le16(action);
200 	switch (action) {
201 	case HOST_ACT_GEN_SET:
202 		nxpwifi_dbg(priv->adapter, CMD,
203 			    "cmd: set tx_buf=%d\n", *buf_size);
204 		tx_buf->buff_size = cpu_to_le16(*buf_size);
205 		break;
206 	case HOST_ACT_GEN_GET:
207 	default:
208 		tx_buf->buff_size = 0;
209 		break;
210 	}
211 	return 0;
212 }
213 
214 /*
215  * AMSDU aggregation control command.
216  * Set ID/action/size; set AMSDU params on SET; ensure little-endian.
217  */
nxpwifi_cmd_amsdu_aggr_ctrl(struct host_cmd_ds_command * cmd,int cmd_action,struct nxpwifi_ds_11n_amsdu_aggr_ctrl * aa_ctrl)218 int nxpwifi_cmd_amsdu_aggr_ctrl(struct host_cmd_ds_command *cmd,
219 				int cmd_action,
220 				struct nxpwifi_ds_11n_amsdu_aggr_ctrl *aa_ctrl)
221 {
222 	struct host_cmd_ds_amsdu_aggr_ctrl *amsdu_ctrl =
223 		&cmd->params.amsdu_aggr_ctrl;
224 	u16 action = (u16)cmd_action;
225 
226 	cmd->command = cpu_to_le16(HOST_CMD_AMSDU_AGGR_CTRL);
227 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_amsdu_aggr_ctrl)
228 				+ S_DS_GEN);
229 	amsdu_ctrl->action = cpu_to_le16(action);
230 	switch (action) {
231 	case HOST_ACT_GEN_SET:
232 		amsdu_ctrl->enable = cpu_to_le16(aa_ctrl->enable);
233 		amsdu_ctrl->curr_buf_size = 0;
234 		break;
235 	case HOST_ACT_GEN_GET:
236 	default:
237 		amsdu_ctrl->curr_buf_size = 0;
238 		break;
239 	}
240 	return 0;
241 }
242 
243 /*
244  * 11n configuration command.
245  * Set action, HT Tx capability/info, and misc config when 11ac HW is present.
246  */
nxpwifi_cmd_11n_cfg(struct nxpwifi_private * priv,struct host_cmd_ds_command * cmd,u16 cmd_action,struct nxpwifi_ds_11n_tx_cfg * txcfg)247 int nxpwifi_cmd_11n_cfg(struct nxpwifi_private *priv,
248 			struct host_cmd_ds_command *cmd, u16 cmd_action,
249 			struct nxpwifi_ds_11n_tx_cfg *txcfg)
250 {
251 	struct host_cmd_ds_11n_cfg *htcfg = &cmd->params.htcfg;
252 
253 	cmd->command = cpu_to_le16(HOST_CMD_11N_CFG);
254 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_11n_cfg) + S_DS_GEN);
255 	htcfg->action = cpu_to_le16(cmd_action);
256 	htcfg->ht_tx_cap = cpu_to_le16(txcfg->tx_htcap);
257 	htcfg->ht_tx_info = cpu_to_le16(txcfg->tx_htinfo);
258 
259 	if (priv->adapter->is_hw_11ac_capable)
260 		htcfg->misc_config = cpu_to_le16(txcfg->misc_config);
261 
262 	return 0;
263 }
264 
265 /*
266  * Append 11n TLVs to the caller-owned buffer.
267  * Caller allocates space; no size checks here.
268  * May add: HT Cap, HT Operation + channel list, 20/40 BSS Coexistence,
269  * and Extended Capabilities (HS2/TWT bits when applicable).
270  */
271 int
nxpwifi_cmd_append_11n_tlv(struct nxpwifi_private * priv,struct nxpwifi_bssdescriptor * bss_desc,u8 ** buffer)272 nxpwifi_cmd_append_11n_tlv(struct nxpwifi_private *priv,
273 			   struct nxpwifi_bssdescriptor *bss_desc,
274 			   u8 **buffer)
275 {
276 	struct nxpwifi_ie_types_htcap *ht_cap;
277 	struct nxpwifi_ie_types_chan_list_param_set *chan_list;
278 	struct nxpwifi_chan_scan_param_set *chan_param;
279 	struct nxpwifi_ie_types_2040bssco *bss_co_2040;
280 	struct nxpwifi_ie_types_extcap *ext_cap;
281 	int ret_len = 0;
282 	struct ieee80211_supported_band *sband;
283 	struct element *hdr;
284 	u8 radio_type;
285 
286 	if (!buffer || !*buffer)
287 		return ret_len;
288 
289 	radio_type = nxpwifi_band_to_radio_type((u8)bss_desc->bss_band);
290 	sband = priv->wdev.wiphy->bands[radio_type];
291 
292 	if (bss_desc->bcn_ht_cap) {
293 		ht_cap = (struct nxpwifi_ie_types_htcap *)*buffer;
294 		memset(ht_cap, 0, sizeof(struct nxpwifi_ie_types_htcap));
295 		ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
296 		ht_cap->header.len =
297 			cpu_to_le16(sizeof(struct ieee80211_ht_cap));
298 		memcpy((u8 *)ht_cap + sizeof(struct nxpwifi_ie_types_header),
299 		       (u8 *)bss_desc->bcn_ht_cap,
300 		       le16_to_cpu(ht_cap->header.len));
301 
302 		nxpwifi_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
303 		/* Update HT40 capability from current channel. */
304 		if (bss_desc->bcn_ht_oper) {
305 			u8 ht_param = bss_desc->bcn_ht_oper->ht_param;
306 			u8 radio =
307 				nxpwifi_band_to_radio_type(bss_desc->bss_band);
308 			int freq =
309 				ieee80211_channel_to_frequency(bss_desc->channel,
310 							       radio);
311 			struct ieee80211_channel *chan =
312 				ieee80211_get_channel(priv->adapter->wiphy, freq);
313 
314 			switch (ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET) {
315 			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
316 				if (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) {
317 					ht_cap->ht_cap.cap_info &=
318 					cpu_to_le16
319 					(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
320 					ht_cap->ht_cap.cap_info &=
321 					cpu_to_le16(~IEEE80211_HT_CAP_SGI_40);
322 				}
323 				break;
324 			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
325 				if (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) {
326 					ht_cap->ht_cap.cap_info &=
327 					cpu_to_le16
328 					(~IEEE80211_HT_CAP_SUP_WIDTH_20_40);
329 					ht_cap->ht_cap.cap_info &=
330 					cpu_to_le16(~IEEE80211_HT_CAP_SGI_40);
331 				}
332 				break;
333 			}
334 		}
335 
336 		*buffer += sizeof(struct nxpwifi_ie_types_htcap);
337 		ret_len += sizeof(struct nxpwifi_ie_types_htcap);
338 	}
339 
340 	if (bss_desc->bcn_ht_oper) {
341 		chan_list =
342 			(struct nxpwifi_ie_types_chan_list_param_set *)*buffer;
343 		chan_param = chan_list->chan_scan_param;
344 		memset(chan_list, 0, struct_size(chan_list, chan_scan_param, 1));
345 		chan_list->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
346 		chan_list->header.len = cpu_to_le16(sizeof(*chan_param));
347 		chan_param->chan_number = bss_desc->bcn_ht_oper->primary_chan;
348 		chan_param->band_cfg =
349 			nxpwifi_band_to_radio_type((u8)bss_desc->bss_band);
350 
351 		if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
352 		    bss_desc->bcn_vht_oper &&
353 		    bss_desc->bcn_vht_oper->chan_width ==
354 		    IEEE80211_VHT_CHANWIDTH_80MHZ) {
355 			SET_SECONDARYCHAN(chan_param->band_cfg,
356 					  (bss_desc->bcn_ht_oper->ht_param &
357 					   IEEE80211_HT_PARAM_CHA_SEC_OFFSET));
358 			chan_param->band_cfg |=
359 				((CHAN_BW_80MHZ <<
360 				  BAND_CFG_CHAN_WIDTH_SHIFT_BIT) &
361 				 BAND_CFG_CHAN_WIDTH_MASK);
362 		} else if (sband->ht_cap.cap &
363 			   IEEE80211_HT_CAP_SUP_WIDTH_20_40 &&
364 			   bss_desc->bcn_ht_oper->ht_param &
365 			   IEEE80211_HT_PARAM_CHAN_WIDTH_ANY) {
366 			SET_SECONDARYCHAN(chan_param->band_cfg,
367 					  (bss_desc->bcn_ht_oper->ht_param &
368 					   IEEE80211_HT_PARAM_CHA_SEC_OFFSET));
369 			chan_param->band_cfg |=
370 				((CHAN_BW_40MHZ <<
371 				  BAND_CFG_CHAN_WIDTH_SHIFT_BIT) &
372 				 BAND_CFG_CHAN_WIDTH_MASK);
373 		}
374 
375 		*buffer += struct_size(chan_list, chan_scan_param, 1);
376 		ret_len += struct_size(chan_list, chan_scan_param, 1);
377 	}
378 
379 	if (bss_desc->bcn_bss_co_2040) {
380 		bss_co_2040 = (struct nxpwifi_ie_types_2040bssco *)*buffer;
381 		memset(bss_co_2040, 0,
382 		       sizeof(struct nxpwifi_ie_types_2040bssco));
383 		bss_co_2040->header.type = cpu_to_le16(WLAN_EID_BSS_COEX_2040);
384 		bss_co_2040->header.len =
385 		       cpu_to_le16(sizeof(bss_co_2040->bss_co_2040));
386 
387 		memcpy((u8 *)bss_co_2040 +
388 		       sizeof(struct nxpwifi_ie_types_header),
389 		       bss_desc->bcn_bss_co_2040 +
390 		       sizeof(struct element),
391 		       le16_to_cpu(bss_co_2040->header.len));
392 
393 		*buffer += sizeof(struct nxpwifi_ie_types_2040bssco);
394 		ret_len += sizeof(struct nxpwifi_ie_types_2040bssco);
395 	}
396 
397 	if (bss_desc->bcn_ext_cap) {
398 		u8 *ext_capab;
399 
400 		hdr = (void *)bss_desc->bcn_ext_cap;
401 
402 		ext_capab = (u8 *)cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, priv->gen_ie_buf,
403 				  priv->gen_ie_buf_len);
404 		if (ext_capab) {
405 			ext_capab += 2;
406 		} else {
407 			ext_cap = (struct nxpwifi_ie_types_extcap *)*buffer;
408 			memset(ext_cap, 0, sizeof(struct nxpwifi_ie_types_extcap) + hdr->datalen);
409 			ext_cap->header.type = cpu_to_le16(WLAN_EID_EXT_CAPABILITY);
410 			ext_cap->header.len = cpu_to_le16(hdr->datalen);
411 			ext_capab = ext_cap->ext_capab;
412 			*buffer += sizeof(struct nxpwifi_ie_types_extcap) + hdr->datalen;
413 			ret_len += sizeof(struct nxpwifi_ie_types_extcap) + hdr->datalen;
414 		}
415 
416 		if (hdr->datalen > 3 &&
417 		    ext_capab[3] & WLAN_EXT_CAPA4_INTERWORKING_ENABLED)
418 			priv->hs2_enabled = true;
419 		else
420 			priv->hs2_enabled = false;
421 
422 		if (nxpwifi_is_11ax_twt_supported(priv, bss_desc))
423 			ext_capab[9] |=
424 			WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT;
425 	}
426 	return ret_len;
427 }
428 
429 /* Check if pointer is a valid Tx BA stream entry. */
430 static bool
nxpwifi_is_tx_ba_stream_ptr_valid(struct nxpwifi_private * priv,struct nxpwifi_tx_ba_stream_tbl * tx_tbl_ptr)431 nxpwifi_is_tx_ba_stream_ptr_valid(struct nxpwifi_private *priv,
432 				  struct nxpwifi_tx_ba_stream_tbl *tx_tbl_ptr)
433 {
434 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tsr_tbl;
435 	bool ret = false;
436 	int tid;
437 
438 	tid = tx_tbl_ptr->tid;
439 	guard(rcu)();
440 	list_for_each_entry_rcu(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr[tid], list) {
441 		if (tx_ba_tsr_tbl == tx_tbl_ptr) {
442 			ret = true;
443 			break;
444 		}
445 	}
446 	return ret;
447 }
448 
449 /* Delete a Tx BA stream entry (after validating pointer). */
450 void
nxpwifi_11n_delete_tx_ba_stream_tbl_entry(struct nxpwifi_private * priv,struct nxpwifi_tx_ba_stream_tbl * tbl)451 nxpwifi_11n_delete_tx_ba_stream_tbl_entry(struct nxpwifi_private *priv,
452 					  struct nxpwifi_tx_ba_stream_tbl *tbl)
453 {
454 	if (!tbl || nxpwifi_is_tx_ba_stream_ptr_valid(priv, tbl))
455 		return;
456 
457 	nxpwifi_dbg(priv->adapter, INFO,
458 		    "info: tx_ba_tsr_tbl %p\n", tbl);
459 
460 	list_del_rcu(&tbl->list);
461 	kfree_rcu(tbl, rcu);
462 }
463 
464 /* Delete all entries in Tx BA stream table. */
nxpwifi_11n_delete_all_tx_ba_stream_tbl(struct nxpwifi_private * priv)465 void nxpwifi_11n_delete_all_tx_ba_stream_tbl(struct nxpwifi_private *priv)
466 {
467 	int i;
468 	struct nxpwifi_tx_ba_stream_tbl *del_tbl_ptr, *tmp_node;
469 
470 	for (i = 0; i < MAX_NUM_TID; i++) {
471 		spin_lock_bh(&priv->tx_ba_stream_tbl_lock[i]);
472 		list_for_each_entry_safe(del_tbl_ptr, tmp_node,
473 					 &priv->tx_ba_stream_tbl_ptr[i], list)
474 			nxpwifi_11n_delete_tx_ba_stream_tbl_entry(priv, del_tbl_ptr);
475 		spin_unlock_bh(&priv->tx_ba_stream_tbl_lock[i]);
476 
477 		INIT_LIST_HEAD(&priv->tx_ba_stream_tbl_ptr[i]);
478 
479 		priv->aggr_prio_tbl[i].ampdu_ap =
480 			priv->aggr_prio_tbl[i].ampdu_user;
481 	}
482 }
483 
484 /* Return BA stream entry for given RA/TID. */
485 struct nxpwifi_tx_ba_stream_tbl *
nxpwifi_get_ba_tbl(struct nxpwifi_private * priv,int tid,u8 * ra)486 nxpwifi_get_ba_tbl(struct nxpwifi_private *priv, int tid, u8 *ra)
487 {
488 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tsr_tbl = NULL;
489 
490 	list_for_each_entry_rcu(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr[tid], list) {
491 		if (ether_addr_equal_unaligned(tx_ba_tsr_tbl->ra, ra) &&
492 		    tx_ba_tsr_tbl->tid == tid)
493 			return tx_ba_tsr_tbl;
494 	}
495 	return NULL;
496 }
497 
498 /* Create Tx BA stream entry for given RA/TID. */
nxpwifi_create_ba_tbl(struct nxpwifi_private * priv,u8 * ra,int tid,enum nxpwifi_ba_status ba_status)499 void nxpwifi_create_ba_tbl(struct nxpwifi_private *priv, u8 *ra, int tid,
500 			   enum nxpwifi_ba_status ba_status)
501 {
502 	struct nxpwifi_tx_ba_stream_tbl *new_node;
503 	struct nxpwifi_ra_list_tbl *ra_list;
504 	int tid_down;
505 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tbl;
506 
507 	guard(rcu)();
508 	tx_ba_tbl = nxpwifi_get_ba_tbl(priv, tid, ra);
509 
510 	if (!tx_ba_tbl) {
511 		new_node = kzalloc_obj(*new_node, GFP_ATOMIC);
512 		if (!new_node)
513 			return;
514 
515 		tid_down = nxpwifi_wmm_downgrade_tid(priv, tid);
516 		ra_list = nxpwifi_wmm_get_ralist_node(priv, tid_down, ra);
517 		if (ra_list) {
518 			ra_list->ba_status = ba_status;
519 			ra_list->amsdu_in_ampdu = false;
520 		}
521 		INIT_LIST_HEAD(&new_node->list);
522 
523 		new_node->tid = tid;
524 		new_node->ba_status = ba_status;
525 		memcpy(new_node->ra, ra, ETH_ALEN);
526 
527 		spin_lock_bh(&priv->tx_ba_stream_tbl_lock[tid]);
528 		list_add_tail_rcu(&new_node->list, &priv->tx_ba_stream_tbl_ptr[tid]);
529 		spin_unlock_bh(&priv->tx_ba_stream_tbl_lock[tid]);
530 	}
531 }
532 
533 /* Send ADDBA request to the given TID/RA. */
nxpwifi_send_addba(struct nxpwifi_private * priv,int tid,u8 * peer_mac)534 int nxpwifi_send_addba(struct nxpwifi_private *priv, int tid, u8 *peer_mac)
535 {
536 	struct host_cmd_ds_11n_addba_req add_ba_req;
537 	u32 tx_win_size = priv->add_ba_param.tx_win_size;
538 	static u8 dialog_tok;
539 	u16 block_ack_param_set;
540 
541 	nxpwifi_dbg(priv->adapter, CMD, "cmd: %s: tid %d\n", __func__, tid);
542 
543 	memset(&add_ba_req, 0, sizeof(add_ba_req));
544 
545 	block_ack_param_set = (u16)((tid << BLOCKACKPARAM_TID_POS) |
546 				    tx_win_size << BLOCKACKPARAM_WINSIZE_POS |
547 				    IMMEDIATE_BLOCK_ACK);
548 
549 	/* enable AMSDU inside AMPDU */
550 	if (priv->add_ba_param.tx_amsdu &&
551 	    priv->aggr_prio_tbl[tid].amsdu != BA_STREAM_NOT_ALLOWED)
552 		block_ack_param_set |= IEEE80211_ADDBA_PARAM_AMSDU_MASK;
553 
554 	add_ba_req.block_ack_param_set = cpu_to_le16(block_ack_param_set);
555 	add_ba_req.block_ack_tmo = cpu_to_le16((u16)priv->add_ba_param.timeout);
556 
557 	++dialog_tok;
558 
559 	if (dialog_tok == 0)
560 		dialog_tok = 1;
561 
562 	add_ba_req.dialog_token = dialog_tok;
563 	memcpy(&add_ba_req.peer_mac_addr, peer_mac, ETH_ALEN);
564 
565 	/* We don't wait for the response of this command */
566 	return nxpwifi_send_cmd(priv, HOST_CMD_11N_ADDBA_REQ,
567 			       0, 0, &add_ba_req, false);
568 }
569 
570 /* Send DELBA request to the given TID/RA. */
nxpwifi_send_delba(struct nxpwifi_private * priv,int tid,u8 * peer_mac,int initiator)571 int nxpwifi_send_delba(struct nxpwifi_private *priv, int tid, u8 *peer_mac,
572 		       int initiator)
573 {
574 	struct host_cmd_ds_11n_delba delba;
575 	u16 del_ba_param_set;
576 
577 	memset(&delba, 0, sizeof(delba));
578 
579 	del_ba_param_set = tid << DELBA_TID_POS;
580 
581 	if (initiator)
582 		del_ba_param_set |= IEEE80211_DELBA_PARAM_INITIATOR_MASK;
583 	else
584 		del_ba_param_set &= ~IEEE80211_DELBA_PARAM_INITIATOR_MASK;
585 
586 	delba.del_ba_param_set = cpu_to_le16(del_ba_param_set);
587 	memcpy(&delba.peer_mac_addr, peer_mac, ETH_ALEN);
588 
589 	/* We don't wait for the response of this command */
590 	return nxpwifi_send_cmd(priv, HOST_CMD_11N_DELBA,
591 			       HOST_ACT_GEN_SET, 0, &delba, false);
592 }
593 
594 /* Send DELBA to specific TID. */
nxpwifi_11n_delba(struct nxpwifi_private * priv,int tid)595 void nxpwifi_11n_delba(struct nxpwifi_private *priv, int tid)
596 {
597 	struct nxpwifi_rx_reorder_tbl *rx_reor_tbl_ptr;
598 	u8 ta[ETH_ALEN];
599 	bool found = false;
600 
601 	rcu_read_lock();
602 	list_for_each_entry_rcu(rx_reor_tbl_ptr, &priv->rx_reorder_tbl_ptr[tid], list) {
603 		if (rx_reor_tbl_ptr->tid == tid) {
604 			memcpy(ta, rx_reor_tbl_ptr->ta, ETH_ALEN);
605 			found = true;
606 			break;
607 		}
608 	}
609 	rcu_read_unlock();
610 
611 	if (found) {
612 		nxpwifi_dbg(priv->adapter, INFO,
613 			    "Send delba to tid=%d, %pM\n", tid, ta);
614 		nxpwifi_send_delba(priv, tid, ta, 0);
615 	}
616 }
617 
618 /* Handle DELBA event; remove BA stream. */
nxpwifi_11n_delete_ba_stream(struct nxpwifi_private * priv,u8 * del_ba)619 void nxpwifi_11n_delete_ba_stream(struct nxpwifi_private *priv, u8 *del_ba)
620 {
621 	struct host_cmd_ds_11n_delba *cmd_del_ba =
622 		(struct host_cmd_ds_11n_delba *)del_ba;
623 	u16 del_ba_param_set = le16_to_cpu(cmd_del_ba->del_ba_param_set);
624 	int tid;
625 
626 	tid = del_ba_param_set >> DELBA_TID_POS;
627 
628 	nxpwifi_del_ba_tbl(priv, tid, cmd_del_ba->peer_mac_addr,
629 			   TYPE_DELBA_RECEIVE, INITIATOR_BIT(del_ba_param_set));
630 }
631 
632 /* Retrieve Rx reordering table. */
nxpwifi_get_rx_reorder_tbl(struct nxpwifi_private * priv,struct nxpwifi_ds_rx_reorder_tbl * buf)633 int nxpwifi_get_rx_reorder_tbl(struct nxpwifi_private *priv,
634 			       struct nxpwifi_ds_rx_reorder_tbl *buf)
635 {
636 	int i, j;
637 	struct nxpwifi_ds_rx_reorder_tbl *rx_reo_tbl = buf;
638 	struct nxpwifi_rx_reorder_tbl *rx_reorder_tbl_ptr;
639 	int count = 0;
640 
641 	guard(rcu)();
642 	for (j = 0; j < MAX_NUM_TID; j++) {
643 		list_for_each_entry_rcu(rx_reorder_tbl_ptr,
644 					&priv->rx_reorder_tbl_ptr[j],
645 					list) {
646 			rx_reo_tbl->tid = (u16)rx_reorder_tbl_ptr->tid;
647 			memcpy(rx_reo_tbl->ta, rx_reorder_tbl_ptr->ta, ETH_ALEN);
648 			rx_reo_tbl->start_win = rx_reorder_tbl_ptr->start_win;
649 			rx_reo_tbl->win_size = rx_reorder_tbl_ptr->win_size;
650 			for (i = 0; i < rx_reorder_tbl_ptr->win_size; ++i) {
651 				if (rx_reorder_tbl_ptr->rx_reorder_ptr[i])
652 					rx_reo_tbl->buffer[i] = true;
653 				else
654 					rx_reo_tbl->buffer[i] = false;
655 			}
656 			rx_reo_tbl++;
657 			count++;
658 
659 			if (count >= NXPWIFI_MAX_RX_BASTREAM_SUPPORTED)
660 				return count;
661 		}
662 	}
663 
664 	return count;
665 }
666 
667 /* Retrieve Tx BA stream table. */
nxpwifi_get_tx_ba_stream_tbl(struct nxpwifi_private * priv,struct nxpwifi_ds_tx_ba_stream_tbl * buf)668 int nxpwifi_get_tx_ba_stream_tbl(struct nxpwifi_private *priv,
669 				 struct nxpwifi_ds_tx_ba_stream_tbl *buf)
670 {
671 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_tsr_tbl;
672 	struct nxpwifi_ds_tx_ba_stream_tbl *rx_reo_tbl = buf;
673 	int count = 0;
674 	int i;
675 
676 	guard(rcu)();
677 	for (i = 0; i < MAX_NUM_TID; i++) {
678 		list_for_each_entry_rcu(tx_ba_tsr_tbl, &priv->tx_ba_stream_tbl_ptr[i], list) {
679 			rx_reo_tbl->tid = (u16)tx_ba_tsr_tbl->tid;
680 			nxpwifi_dbg(priv->adapter, DATA, "data: %s tid=%d\n",
681 				    __func__, rx_reo_tbl->tid);
682 			memcpy(rx_reo_tbl->ra, tx_ba_tsr_tbl->ra, ETH_ALEN);
683 			rx_reo_tbl->amsdu = tx_ba_tsr_tbl->amsdu;
684 			rx_reo_tbl++;
685 			count++;
686 			if (count >= NXPWIFI_MAX_TX_BASTREAM_SUPPORTED)
687 				return count;
688 		}
689 	}
690 
691 	return count;
692 }
693 
694 /* Delete Tx BA stream entry by RA. */
nxpwifi_del_tx_ba_stream_tbl_by_ra(struct nxpwifi_private * priv,u8 * ra)695 void nxpwifi_del_tx_ba_stream_tbl_by_ra(struct nxpwifi_private *priv, u8 *ra)
696 {
697 	struct nxpwifi_tx_ba_stream_tbl *tbl, *tmp;
698 	int i;
699 
700 	if (!ra)
701 		return;
702 
703 	for (i = 0; i < MAX_NUM_TID; i++) {
704 		spin_lock_bh(&priv->tx_ba_stream_tbl_lock[i]);
705 		list_for_each_entry_safe(tbl, tmp,
706 					 &priv->tx_ba_stream_tbl_ptr[i], list)
707 			if (!memcmp(tbl->ra, ra, ETH_ALEN))
708 				nxpwifi_11n_delete_tx_ba_stream_tbl_entry(priv, tbl);
709 
710 		spin_unlock_bh(&priv->tx_ba_stream_tbl_lock[i]);
711 	}
712 }
713 
714 /* Initialize BlockAck parameters. */
nxpwifi_set_ba_params(struct nxpwifi_private * priv)715 void nxpwifi_set_ba_params(struct nxpwifi_private *priv)
716 {
717 	priv->add_ba_param.timeout = NXPWIFI_DEFAULT_BLOCK_ACK_TIMEOUT;
718 
719 	if (GET_BSS_ROLE(priv) == NXPWIFI_BSS_ROLE_UAP) {
720 		priv->add_ba_param.tx_win_size =
721 			NXPWIFI_UAP_AMPDU_DEF_TXWINSIZE;
722 		priv->add_ba_param.rx_win_size =
723 			NXPWIFI_UAP_AMPDU_DEF_RXWINSIZE;
724 	} else {
725 		priv->add_ba_param.tx_win_size =
726 			NXPWIFI_STA_AMPDU_DEF_TXWINSIZE;
727 		priv->add_ba_param.rx_win_size =
728 			NXPWIFI_STA_AMPDU_DEF_RXWINSIZE;
729 	}
730 
731 	priv->add_ba_param.tx_amsdu = true;
732 	priv->add_ba_param.rx_amsdu = true;
733 }
734 
nxpwifi_get_sec_chan_offset(int chan)735 u8 nxpwifi_get_sec_chan_offset(int chan)
736 {
737 	u8 sec_offset;
738 
739 	switch (chan) {
740 	case 36:
741 	case 44:
742 	case 52:
743 	case 60:
744 	case 100:
745 	case 108:
746 	case 116:
747 	case 124:
748 	case 132:
749 	case 140:
750 	case 149:
751 	case 157:
752 	case 173:
753 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
754 		break;
755 	case 40:
756 	case 48:
757 	case 56:
758 	case 64:
759 	case 104:
760 	case 112:
761 	case 120:
762 	case 128:
763 	case 136:
764 	case 144:
765 	case 153:
766 	case 161:
767 	case 169:
768 	case 177:
769 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
770 		break;
771 	case 165:
772 	default:
773 		sec_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
774 		break;
775 	}
776 
777 	return sec_offset;
778 }
779 
780 /* Send DELBA to entries in the Tx BA stream table. */
781 static void
nxpwifi_send_delba_txbastream_tbl(struct nxpwifi_private * priv,u8 tid)782 nxpwifi_send_delba_txbastream_tbl(struct nxpwifi_private *priv, u8 tid)
783 {
784 	struct nxpwifi_adapter *adapter = priv->adapter;
785 	struct nxpwifi_tx_ba_stream_tbl *tx_ba_stream_tbl_ptr;
786 
787 	guard(rcu)();
788 	list_for_each_entry_rcu(tx_ba_stream_tbl_ptr,
789 				&priv->tx_ba_stream_tbl_ptr[tid], list) {
790 		if (tx_ba_stream_tbl_ptr->ba_status == BA_SETUP_COMPLETE) {
791 			if (tid == tx_ba_stream_tbl_ptr->tid) {
792 				nxpwifi_dbg(adapter, INFO,
793 					    "Tx:Send delba to tid=%d, %pM\n", tid,
794 					    tx_ba_stream_tbl_ptr->ra);
795 				nxpwifi_send_delba(priv,
796 						   tx_ba_stream_tbl_ptr->tid,
797 						   tx_ba_stream_tbl_ptr->ra, 1);
798 				break;
799 			}
800 		}
801 	}
802 }
803 
804 /*
805  * Update tx_win_size for all interfaces and send DELBA when it changes.
806  */
nxpwifi_update_ampdu_txwinsize(struct nxpwifi_adapter * adapter)807 void nxpwifi_update_ampdu_txwinsize(struct nxpwifi_adapter *adapter)
808 {
809 	u8 i, j;
810 	u32 tx_win_size;
811 	struct nxpwifi_private *priv;
812 
813 	for (i = 0; i < adapter->priv_num; i++) {
814 		priv = adapter->priv[i];
815 		tx_win_size = priv->add_ba_param.tx_win_size;
816 
817 		if (priv->bss_type == NXPWIFI_BSS_TYPE_STA)
818 			priv->add_ba_param.tx_win_size =
819 				NXPWIFI_STA_AMPDU_DEF_TXWINSIZE;
820 
821 		if (priv->bss_type == NXPWIFI_BSS_TYPE_UAP)
822 			priv->add_ba_param.tx_win_size =
823 				NXPWIFI_UAP_AMPDU_DEF_TXWINSIZE;
824 
825 		if (adapter->coex_win_size) {
826 			if (adapter->coex_tx_win_size)
827 				priv->add_ba_param.tx_win_size =
828 					adapter->coex_tx_win_size;
829 		}
830 
831 		if (tx_win_size != priv->add_ba_param.tx_win_size) {
832 			if (!priv->media_connected)
833 				continue;
834 			for (j = 0; j < MAX_NUM_TID; j++)
835 				nxpwifi_send_delba_txbastream_tbl(priv, j);
836 		}
837 	}
838 }
839