xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/sta.c (revision 6a972f26b7ddb8ddeac3b2f20db1c30d1f8c027b)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024-2026 Intel Corporation
4  */
5 
6 #include <linux/ieee80211.h>
7 #include <kunit/static_stub.h>
8 
9 #include "sta.h"
10 #include "hcmd.h"
11 #include "iface.h"
12 #include "mlo.h"
13 #include "key.h"
14 #include "agg.h"
15 #include "tlc.h"
16 #include "nan.h"
17 #include "fw/api/sta.h"
18 #include "fw/api/mac.h"
19 #include "fw/api/rx.h"
20 
21 int iwl_mld_fw_sta_id_from_link_sta(struct iwl_mld *mld,
22 				    struct ieee80211_link_sta *link_sta)
23 {
24 	struct iwl_mld_link_sta *mld_link_sta;
25 
26 	/* This function should only be used with the wiphy lock held,
27 	 * In other cases, it is not guaranteed that the link_sta will exist
28 	 * in the driver too, and it is checked here.
29 	 */
30 	lockdep_assert_wiphy(mld->wiphy);
31 
32 	/* This is not meant to be called with a NULL pointer */
33 	if (WARN_ON(!link_sta))
34 		return -ENOENT;
35 
36 	mld_link_sta = iwl_mld_link_sta_from_mac80211(link_sta);
37 	if (!mld_link_sta) {
38 		WARN_ON(!iwl_mld_error_before_recovery(mld));
39 		return -ENOENT;
40 	}
41 
42 	return mld_link_sta->fw_id;
43 }
44 
45 static void
46 iwl_mld_fill_ampdu_size_and_dens(struct ieee80211_link_sta *link_sta,
47 				 bool is_6ghz,
48 				 __le32 *tx_ampdu_max_size,
49 				 __le32 *tx_ampdu_spacing)
50 {
51 	u32 agg_size = 0, mpdu_dens = 0;
52 
53 	if (WARN_ON(!link_sta))
54 		return;
55 
56 	/* Note that we always use only legacy & highest supported PPDUs, so
57 	 * of Draft P802.11be D.30 Table 10-12a--Fields used for calculating
58 	 * the maximum A-MPDU size of various PPDU types in different bands,
59 	 * we only need to worry about the highest supported PPDU type here.
60 	 */
61 
62 	if (link_sta->ht_cap.ht_supported) {
63 		agg_size = link_sta->ht_cap.ampdu_factor;
64 		mpdu_dens = link_sta->ht_cap.ampdu_density;
65 	}
66 
67 	if (is_6ghz) {
68 		/* overwrite HT values on 6 GHz */
69 		mpdu_dens =
70 			le16_get_bits(link_sta->he_6ghz_capa.capa,
71 				      IEEE80211_HE_6GHZ_CAP_MIN_MPDU_START);
72 		agg_size =
73 			le16_get_bits(link_sta->he_6ghz_capa.capa,
74 				      IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP);
75 	} else if (link_sta->vht_cap.vht_supported) {
76 		/* if VHT supported overwrite HT value */
77 		agg_size =
78 			u32_get_bits(link_sta->vht_cap.cap,
79 				     IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK);
80 	}
81 
82 	/* D6.0 10.12.2 A-MPDU length limit rules
83 	 * A STA indicates the maximum length of the A-MPDU preEOF padding
84 	 * that it can receive in an HE PPDU in the Maximum A-MPDU Length
85 	 * Exponent field in its HT Capabilities, VHT Capabilities,
86 	 * and HE 6 GHz Band Capabilities elements (if present) and the
87 	 * Maximum AMPDU Length Exponent Extension field in its HE
88 	 * Capabilities element
89 	 */
90 	if (link_sta->he_cap.has_he)
91 		agg_size +=
92 			u8_get_bits(link_sta->he_cap.he_cap_elem.mac_cap_info[3],
93 				    IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
94 
95 	if (link_sta->eht_cap.has_eht)
96 		agg_size +=
97 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.mac_cap_info[1],
98 				    IEEE80211_EHT_MAC_CAP1_MAX_AMPDU_LEN_MASK);
99 
100 	/* Limit to max A-MPDU supported by FW */
101 	agg_size = min_t(u32, agg_size,
102 			 STA_FLG_MAX_AGG_SIZE_4M >> STA_FLG_MAX_AGG_SIZE_SHIFT);
103 
104 	*tx_ampdu_max_size = cpu_to_le32(agg_size);
105 	*tx_ampdu_spacing = cpu_to_le32(mpdu_dens);
106 }
107 
108 static u8 iwl_mld_get_uapsd_acs(struct ieee80211_sta *sta)
109 {
110 	u8 uapsd_acs = 0;
111 
112 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
113 		uapsd_acs |= BIT(AC_BK);
114 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
115 		uapsd_acs |= BIT(AC_BE);
116 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
117 		uapsd_acs |= BIT(AC_VI);
118 	if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
119 		uapsd_acs |= BIT(AC_VO);
120 
121 	return uapsd_acs | uapsd_acs << 4;
122 }
123 
124 static u8 iwl_mld_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
125 {
126 	u8 byte_num = ppe_pos_bit / 8;
127 	u8 bit_num = ppe_pos_bit % 8;
128 	u8 residue_bits;
129 	u8 res;
130 
131 	if (bit_num <= 5)
132 		return (ppe[byte_num] >> bit_num) &
133 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
134 
135 	/* If bit_num > 5, we have to combine bits with next byte.
136 	 * Calculate how many bits we need to take from current byte (called
137 	 * here "residue_bits"), and add them to bits from next byte.
138 	 */
139 
140 	residue_bits = 8 - bit_num;
141 
142 	res = (ppe[byte_num + 1] &
143 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
144 	      residue_bits;
145 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
146 
147 	return res;
148 }
149 
150 static void iwl_mld_parse_ppe(struct iwl_mld *mld,
151 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
152 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
153 			      bool inheritance)
154 {
155 	/* FW currently supports only nss == MAX_HE_SUPP_NSS
156 	 *
157 	 * If nss > MAX: we can ignore values we don't support
158 	 * If nss < MAX: we can set zeros in other streams
159 	 */
160 	if (nss > MAX_HE_SUPP_NSS) {
161 		IWL_DEBUG_INFO(mld, "Got NSS = %d - trimming to %d\n", nss,
162 			       MAX_HE_SUPP_NSS);
163 		nss = MAX_HE_SUPP_NSS;
164 	}
165 
166 	for (int i = 0; i < nss; i++) {
167 		u8 ru_index_tmp = ru_index_bitmap << 1;
168 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
169 
170 		for (u8 bw = 0;
171 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
172 		     bw++) {
173 			ru_index_tmp >>= 1;
174 
175 			/* According to the 11be spec, if for a specific BW the PPE Thresholds
176 			 * isn't present - it should inherit the thresholds from the last
177 			 * BW for which we had PPE Thresholds. In 11ax though, we don't have
178 			 * this inheritance - continue in this case
179 			 */
180 			if (!(ru_index_tmp & 1)) {
181 				if (inheritance)
182 					goto set_thresholds;
183 				else
184 					continue;
185 			}
186 
187 			high_th = iwl_mld_he_get_ppe_val(ppe, ppe_pos_bit);
188 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
189 			low_th = iwl_mld_he_get_ppe_val(ppe, ppe_pos_bit);
190 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
191 
192 set_thresholds:
193 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
194 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
195 		}
196 	}
197 }
198 
199 static void iwl_mld_set_pkt_ext_from_he_ppe(struct iwl_mld *mld,
200 					    struct ieee80211_link_sta *link_sta,
201 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
202 					    bool inheritance)
203 {
204 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
205 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
206 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
207 	u8 ru_index_bitmap =
208 		u8_get_bits(*ppe,
209 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
210 	/* Starting after PPE header */
211 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
212 
213 	iwl_mld_parse_ppe(mld, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
214 			  inheritance);
215 }
216 
217 static int
218 iwl_mld_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
219 					 u8 nominal_padding)
220 {
221 	int low_th = -1;
222 	int high_th = -1;
223 
224 	/* all the macros are the same for EHT and HE */
225 	switch (nominal_padding) {
226 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
227 		low_th = IWL_HE_PKT_EXT_NONE;
228 		high_th = IWL_HE_PKT_EXT_NONE;
229 		break;
230 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
231 		low_th = IWL_HE_PKT_EXT_BPSK;
232 		high_th = IWL_HE_PKT_EXT_NONE;
233 		break;
234 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
235 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
236 		low_th = IWL_HE_PKT_EXT_NONE;
237 		high_th = IWL_HE_PKT_EXT_BPSK;
238 		break;
239 	}
240 
241 	if (low_th < 0 || high_th < 0)
242 		return -EINVAL;
243 
244 	/* Set the PPE thresholds accordingly */
245 	for (int i = 0; i < MAX_HE_SUPP_NSS; i++) {
246 		for (u8 bw = 0;
247 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
248 			bw++) {
249 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
250 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
251 		}
252 	}
253 
254 	return 0;
255 }
256 
257 static void iwl_mld_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
258 					 u8 nominal_padding)
259 {
260 	for (int i = 0; i < MAX_HE_SUPP_NSS; i++) {
261 		for (u8 bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
262 		     bw++) {
263 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
264 
265 			if (nominal_padding >
266 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
267 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
268 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
269 			else if (nominal_padding ==
270 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
271 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
272 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
273 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
274 		}
275 	}
276 }
277 
278 static void iwl_mld_fill_pkt_ext(struct iwl_mld *mld,
279 				 struct ieee80211_link_sta *link_sta,
280 				 struct iwl_he_pkt_ext_v2 *pkt_ext)
281 {
282 	if (WARN_ON(!link_sta))
283 		return;
284 
285 	/* Initialize the PPE thresholds to "None" (7), as described in Table
286 	 * 9-262ac of 80211.ax/D3.0.
287 	 */
288 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE, sizeof(*pkt_ext));
289 
290 	if (link_sta->eht_cap.has_eht) {
291 		u8 nominal_padding =
292 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
293 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
294 
295 		/* If PPE Thresholds exists, parse them into a FW-familiar
296 		 * format.
297 		 */
298 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
299 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
300 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
301 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
302 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
303 			u8 ru_index_bitmap =
304 				u16_get_bits(*ppe,
305 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
306 			 /* Starting after PPE header */
307 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
308 
309 			iwl_mld_parse_ppe(mld, pkt_ext, nss, ru_index_bitmap,
310 					  ppe, ppe_pos_bit, true);
311 		/* EHT PPE Thresholds doesn't exist - set the API according to
312 		 * HE PPE Tresholds
313 		 */
314 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
315 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
316 			/* Even though HE Capabilities IE doesn't contain PPE
317 			 * Thresholds for BW 320Mhz, thresholds for this BW will
318 			 * be filled in with the same values as 160Mhz, due to
319 			 * the inheritance, as required.
320 			 */
321 			iwl_mld_set_pkt_ext_from_he_ppe(mld, link_sta, pkt_ext,
322 							true);
323 
324 			/* According to the requirements, for MCSs 12-13 the
325 			 * maximum value between HE PPE Threshold and Common
326 			 * Nominal Packet Padding needs to be taken
327 			 */
328 			iwl_mld_get_optimal_ppe_info(pkt_ext, nominal_padding);
329 
330 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
331 		 * take the Thresholds from Common Nominal Packet Padding field
332 		 */
333 		} else {
334 			iwl_mld_set_pkt_ext_from_nominal_padding(pkt_ext,
335 								 nominal_padding);
336 		}
337 	} else if (link_sta->he_cap.has_he) {
338 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
339 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
340 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
341 			iwl_mld_set_pkt_ext_from_he_ppe(mld, link_sta, pkt_ext,
342 							false);
343 		/* PPE Thresholds doesn't exist - set the API PPE values
344 		 * according to Common Nominal Packet Padding field.
345 		 */
346 		} else {
347 			u8 nominal_padding =
348 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
349 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
350 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
351 				iwl_mld_set_pkt_ext_from_nominal_padding(pkt_ext,
352 									 nominal_padding);
353 		}
354 	}
355 
356 	for (int i = 0; i < MAX_HE_SUPP_NSS; i++) {
357 		for (int bw = 0;
358 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
359 		     bw++) {
360 			u8 *qam_th = pkt_ext->pkt_ext_qam_th[i][bw];
361 
362 			IWL_DEBUG_HT(mld,
363 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
364 				     i, bw, qam_th[0], qam_th[1]);
365 		}
366 	}
367 }
368 
369 static u32 iwl_mld_get_htc_flags(struct ieee80211_link_sta *link_sta)
370 {
371 	u8 *mac_cap_info =
372 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
373 	u32 htc_flags = 0;
374 
375 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
376 		htc_flags |= IWL_HE_HTC_SUPPORT;
377 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
378 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
379 		u8 link_adap =
380 			((mac_cap_info[2] &
381 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
382 			 (mac_cap_info[1] &
383 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
384 
385 		if (link_adap == 2)
386 			htc_flags |=
387 				IWL_HE_HTC_LINK_ADAP_UNSOLICITED;
388 		else if (link_adap == 3)
389 			htc_flags |= IWL_HE_HTC_LINK_ADAP_BOTH;
390 	}
391 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
392 		htc_flags |= IWL_HE_HTC_BSR_SUPP;
393 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
394 		htc_flags |= IWL_HE_HTC_OMI_SUPP;
395 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
396 		htc_flags |= IWL_HE_HTC_BQR_SUPP;
397 
398 	return htc_flags;
399 }
400 
401 /* Note: modifies the command depending on FW command version */
402 static int iwl_mld_send_sta_cmd(struct iwl_mld *mld,
403 				struct iwl_sta_cfg_cmd *cmd)
404 {
405 	int cmd_id = WIDE_ID(MAC_CONF_GROUP, STA_CONFIG_CMD);
406 	int cmd_ver = iwl_fw_lookup_cmd_ver(mld->fw, cmd_id, 0);
407 	int len = sizeof(*cmd);
408 	int ret;
409 
410 	if (cmd_ver < 2) {
411 		IWL_ERR(mld, "Unsupported STA_CONFIG_CMD version %d\n",
412 			cmd_ver);
413 		return -EINVAL;
414 	} else if (cmd_ver == 2) {
415 		struct iwl_sta_cfg_cmd_v2 *cmd_v2 = (void *)cmd;
416 
417 		if (WARN_ON(cmd->station_type == cpu_to_le32(STATION_TYPE_NAN_PEER_NMI) ||
418 			    cmd->station_type == cpu_to_le32(STATION_TYPE_NAN_PEER_NDI) ||
419 			    hweight32(le32_to_cpu(cmd->link_mask)) != 1))
420 			return -EINVAL;
421 		/*
422 		 * These fields are located in a different place in the struct of v2.
423 		 * The assumption is that UHR won't be used with FW that has v2.
424 		 */
425 		if (WARN_ON(cmd->mic_prep_pad_delay || cmd->mic_compute_pad_delay))
426 			return -EINVAL;
427 
428 		len = sizeof(struct iwl_sta_cfg_cmd_v2);
429 		cmd_v2->link_id = cpu_to_le32(__ffs(le32_to_cpu(cmd->link_mask)));
430 	} else if (cmd->station_type ==
431 		   cpu_to_le32(STATION_TYPE_NAN_MCAST_DATA)) {
432 		if (WARN_ON(!hweight32(le32_to_cpu(cmd->link_mask))))
433 			return -EINVAL;
434 	} else if (WARN_ON(cmd->station_type != cpu_to_le32(STATION_TYPE_NAN_PEER_NMI) &&
435 			   cmd->station_type != cpu_to_le32(STATION_TYPE_NAN_PEER_NDI) &&
436 			   cmd->station_type != cpu_to_le32(STATION_TYPE_NAN_BCAST) &&
437 			   cmd->station_type != cpu_to_le32(STATION_TYPE_NAN_MGMT) &&
438 			   hweight32(le32_to_cpu(cmd->link_mask)) != 1)) {
439 		return -EINVAL;
440 	}
441 
442 	ret = iwl_mld_send_cmd_pdu(mld, cmd_id, cmd, len);
443 	if (ret)
444 		IWL_ERR(mld, "STA_CONFIG_CMD send failed, ret=0x%x\n", ret);
445 	return ret;
446 }
447 
448 static u32 iwl_mld_get_nan_link_mask(struct iwl_mld *mld)
449 {
450 	struct iwl_mld_vif *nan_dev =
451 		iwl_mld_vif_from_mac80211(mld->nan_device_vif);
452 	struct iwl_mld_nan_link *nan_link;
453 	u32 link_mask = 0;
454 
455 	for_each_mld_nan_valid_link(nan_dev, nan_link)
456 		link_mask |= BIT(nan_link->fw_id);
457 
458 	return link_mask;
459 }
460 
461 int iwl_mld_add_modify_sta_cmd(struct iwl_mld *mld,
462 			      struct ieee80211_link_sta *link_sta)
463 {
464 	struct ieee80211_sta *sta = link_sta->sta;
465 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
466 	struct iwl_sta_cfg_cmd cmd = {};
467 	int fw_id = iwl_mld_fw_sta_id_from_link_sta(mld, link_sta);
468 	u32 link_mask;
469 	bool is_6ghz;
470 
471 	lockdep_assert_wiphy(mld->wiphy);
472 
473 	if (WARN_ON(fw_id < 0))
474 		return -EINVAL;
475 
476 	if (mld_sta->sta_type == STATION_TYPE_NAN_PEER_NMI ||
477 	    mld_sta->sta_type == STATION_TYPE_NAN_PEER_NDI) {
478 		if (WARN_ON(!mld->nan_device_vif))
479 			return -EINVAL;
480 
481 		is_6ghz = false;
482 
483 		link_mask = iwl_mld_get_nan_link_mask(mld);
484 	} else {
485 		struct ieee80211_bss_conf *link;
486 		struct iwl_mld_link *mld_link;
487 
488 		link = link_conf_dereference_protected(mld_sta->vif,
489 						       link_sta->link_id);
490 		mld_link = iwl_mld_link_from_mac80211(link);
491 
492 		if (WARN_ON(!link || !mld_link))
493 			return -EINVAL;
494 
495 		link_mask = BIT(mld_link->fw_id);
496 		is_6ghz = link->chanreq.oper.chan->band == NL80211_BAND_6GHZ;
497 	}
498 
499 	cmd.sta_id = cpu_to_le32(fw_id);
500 	cmd.link_mask = cpu_to_le32(link_mask);
501 	cmd.station_type = cpu_to_le32(mld_sta->sta_type);
502 
503 	memcpy(&cmd.peer_mld_address, sta->addr, ETH_ALEN);
504 	memcpy(&cmd.peer_link_address, link_sta->addr, ETH_ALEN);
505 
506 	if (mld_sta->sta_state >= IEEE80211_STA_ASSOC)
507 		cmd.assoc_id = cpu_to_le32(sta->aid);
508 
509 	if (sta->mfp || mld_sta->sta_state < IEEE80211_STA_AUTHORIZED)
510 		cmd.mfp = cpu_to_le32(1);
511 
512 	switch (link_sta->rx_nss) {
513 	case 1:
514 		cmd.mimo = cpu_to_le32(0);
515 		break;
516 	case 2 ... 8:
517 		cmd.mimo = cpu_to_le32(1);
518 		break;
519 	}
520 
521 	switch (link_sta->smps_mode) {
522 	case IEEE80211_SMPS_AUTOMATIC:
523 	case IEEE80211_SMPS_NUM_MODES:
524 		WARN_ON(1);
525 		break;
526 	case IEEE80211_SMPS_STATIC:
527 		/* override NSS */
528 		cmd.mimo = cpu_to_le32(0);
529 		break;
530 	case IEEE80211_SMPS_DYNAMIC:
531 		cmd.mimo_protection = cpu_to_le32(1);
532 		break;
533 	case IEEE80211_SMPS_OFF:
534 		/* nothing */
535 		break;
536 	}
537 
538 	/* In NAN, there is no association request so no initial SMPS info */
539 	if (mld_sta->vif->type == NL80211_IFTYPE_NAN_DATA) {
540 		cmd.mimo = cpu_to_le32(1);
541 		cmd.mimo_protection = cpu_to_le32(0);
542 	}
543 
544 	iwl_mld_fill_ampdu_size_and_dens(link_sta, is_6ghz,
545 					 &cmd.tx_ampdu_max_size,
546 					 &cmd.tx_ampdu_spacing);
547 
548 	if (sta->wme) {
549 		cmd.sp_length =
550 			cpu_to_le32(sta->max_sp ? sta->max_sp * 2 : 128);
551 		cmd.uapsd_acs = cpu_to_le32(iwl_mld_get_uapsd_acs(sta));
552 	}
553 
554 	if (link_sta->he_cap.has_he) {
555 		/* PPE Thresholds */
556 		iwl_mld_fill_pkt_ext(mld, link_sta, &cmd.pkt_ext);
557 
558 		/* HTC flags */
559 		cmd.htc_flags =
560 			cpu_to_le32(iwl_mld_get_htc_flags(link_sta));
561 
562 		if (link_sta->he_cap.he_cap_elem.mac_cap_info[2] &
563 		    IEEE80211_HE_MAC_CAP2_ACK_EN)
564 			cmd.ack_enabled = cpu_to_le32(1);
565 	}
566 
567 	if (mld_sta->sta_type == STATION_TYPE_NAN_PEER_NDI) {
568 		struct ieee80211_sta *nmi_sta =
569 			wiphy_dereference(mld->wiphy, sta->nmi);
570 		int nmi_fw_id;
571 
572 		/* copy the local NDI address */
573 		ether_addr_copy(cmd.ndi_local_addr, mld_sta->vif->addr);
574 
575 		if (WARN_ON(!nmi_sta))
576 			return -EINVAL;
577 
578 		nmi_fw_id = iwl_mld_fw_sta_id_from_link_sta(mld,
579 					&nmi_sta->deflink);
580 		if (nmi_fw_id < 0)
581 			return -EINVAL;
582 
583 		cmd.nmi_sta_id = (u8) nmi_fw_id;
584 	}
585 
586 	return iwl_mld_send_sta_cmd(mld, &cmd);
587 }
588 
589 IWL_MLD_ALLOC_FN_STATIC(link_sta, link_sta)
590 
591 static int
592 iwl_mld_add_link_sta(struct iwl_mld *mld, struct ieee80211_link_sta *link_sta)
593 {
594 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(link_sta->sta);
595 	struct iwl_mld_link_sta *mld_link_sta;
596 	int ret;
597 	u8 fw_id;
598 
599 	lockdep_assert_wiphy(mld->wiphy);
600 
601 	/* We will fail to add it to the FW anyway */
602 	if (iwl_mld_error_before_recovery(mld))
603 		return -ENODEV;
604 
605 	mld_link_sta = iwl_mld_link_sta_from_mac80211(link_sta);
606 
607 	/* We need to preserve the fw sta ids during a restart, since the fw
608 	 * will recover SN/PN for them, this is why the mld_link_sta exists.
609 	 */
610 	if (mld_link_sta) {
611 		/* But if we are not restarting, this is not OK */
612 		WARN_ON(!mld->fw_status.in_hw_restart);
613 
614 		/* Avoid adding a STA that is already in FW to avoid an assert */
615 		if (WARN_ON(mld_link_sta->in_fw))
616 			return -EINVAL;
617 
618 		fw_id = mld_link_sta->fw_id;
619 		goto add_to_fw;
620 	}
621 
622 	/* Allocate a fw id and map it to the link_sta */
623 	ret = iwl_mld_allocate_link_sta_fw_id(mld, &fw_id, link_sta);
624 	if (ret)
625 		return ret;
626 
627 	if (link_sta == &link_sta->sta->deflink) {
628 		mld_link_sta = &mld_sta->deflink;
629 	} else {
630 		mld_link_sta = kzalloc_obj(*mld_link_sta);
631 		if (!mld_link_sta)
632 			return -ENOMEM;
633 	}
634 
635 	mld_link_sta->fw_id = fw_id;
636 	rcu_assign_pointer(mld_sta->link[link_sta->link_id], mld_link_sta);
637 
638 add_to_fw:
639 	ret = iwl_mld_add_modify_sta_cmd(mld, link_sta);
640 	if (ret) {
641 		RCU_INIT_POINTER(mld->fw_id_to_link_sta[fw_id], NULL);
642 		RCU_INIT_POINTER(mld_sta->link[link_sta->link_id], NULL);
643 		if (link_sta != &link_sta->sta->deflink)
644 			kfree(mld_link_sta);
645 		return ret;
646 	}
647 	mld_link_sta->in_fw = true;
648 
649 	return 0;
650 }
651 
652 static int iwl_mld_rm_sta_from_fw(struct iwl_mld *mld, u8 fw_sta_id)
653 {
654 	struct iwl_remove_sta_cmd cmd = {
655 		.sta_id = cpu_to_le32(fw_sta_id),
656 	};
657 	int ret;
658 
659 	ret = iwl_mld_send_cmd_pdu(mld,
660 				   WIDE_ID(MAC_CONF_GROUP, STA_REMOVE_CMD),
661 				   &cmd);
662 	if (ret)
663 		IWL_ERR(mld, "Failed to remove station. Id=%d\n", fw_sta_id);
664 
665 	return ret;
666 }
667 
668 static void
669 iwl_mld_remove_link_sta(struct iwl_mld *mld,
670 			struct ieee80211_link_sta *link_sta)
671 {
672 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(link_sta->sta);
673 	struct iwl_mld_link_sta *mld_link_sta =
674 		iwl_mld_link_sta_from_mac80211(link_sta);
675 
676 	if (WARN_ON(!mld_link_sta))
677 		return;
678 
679 	iwl_mld_rm_sta_from_fw(mld, mld_link_sta->fw_id);
680 	mld_link_sta->in_fw = false;
681 
682 	/* Now that the STA doesn't exist in FW, we don't expect any new
683 	 * notifications for it. Cancel the ones that are already pending
684 	 */
685 	iwl_mld_cancel_notifications_of_object(mld, IWL_MLD_OBJECT_TYPE_STA,
686 					       mld_link_sta->fw_id);
687 
688 	/* This will not be done upon reconfig, so do it also when
689 	 * failed to remove from fw
690 	 */
691 	RCU_INIT_POINTER(mld->fw_id_to_link_sta[mld_link_sta->fw_id], NULL);
692 	RCU_INIT_POINTER(mld_sta->link[link_sta->link_id], NULL);
693 	if (mld_link_sta != &mld_sta->deflink)
694 		kfree_rcu(mld_link_sta, rcu_head);
695 }
696 
697 static void iwl_mld_set_max_amsdu_len(struct iwl_mld *mld,
698 				      struct ieee80211_link_sta *link_sta)
699 {
700 	const struct ieee80211_sta_ht_cap *ht_cap = &link_sta->ht_cap;
701 
702 	/* For EHT, HE and VHT we can use the value as it was calculated by
703 	 * mac80211. For HT, mac80211 doesn't enforce to 4095, so force it
704 	 * here
705 	 */
706 	if (link_sta->eht_cap.has_eht || link_sta->he_cap.has_he ||
707 	    link_sta->vht_cap.vht_supported ||
708 	    !ht_cap->ht_supported ||
709 	    !(ht_cap->cap & IEEE80211_HT_CAP_MAX_AMSDU))
710 		return;
711 
712 	link_sta->agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
713 	ieee80211_sta_recalc_aggregates(link_sta->sta);
714 }
715 
716 int iwl_mld_update_all_link_stations(struct iwl_mld *mld,
717 				     struct ieee80211_sta *sta)
718 {
719 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
720 	struct ieee80211_link_sta *link_sta;
721 	int link_id;
722 
723 	for_each_sta_active_link(mld_sta->vif, sta, link_sta, link_id) {
724 		int ret = iwl_mld_add_modify_sta_cmd(mld, link_sta);
725 
726 		if (ret)
727 			return ret;
728 
729 		if (mld_sta->sta_state == IEEE80211_STA_ASSOC)
730 			iwl_mld_set_max_amsdu_len(mld, link_sta);
731 	}
732 	return 0;
733 }
734 
735 static void iwl_mld_destroy_sta(struct ieee80211_sta *sta)
736 {
737 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
738 
739 	kfree(mld_sta->dup_data);
740 	kfree(mld_sta->mpdu_counters);
741 }
742 
743 static int
744 iwl_mld_alloc_dup_data(struct iwl_mld *mld, struct iwl_mld_sta *mld_sta)
745 {
746 	struct iwl_mld_rxq_dup_data *dup_data;
747 
748 	if (mld->fw_status.in_hw_restart)
749 		return 0;
750 
751 	dup_data = kzalloc_objs(*dup_data, mld->trans->info.num_rxqs);
752 	if (!dup_data)
753 		return -ENOMEM;
754 
755 	/* Initialize all the last_seq values to 0xffff which can never
756 	 * compare equal to the frame's seq_ctrl in the check in
757 	 * iwl_mld_is_dup() since the lower 4 bits are the fragment
758 	 * number and fragmented packets don't reach that function.
759 	 *
760 	 * This thus allows receiving a packet with seqno 0 and the
761 	 * retry bit set as the very first packet on a new TID.
762 	 */
763 	for (int q = 0; q < mld->trans->info.num_rxqs; q++)
764 		memset(dup_data[q].last_seq, 0xff,
765 		       sizeof(dup_data[q].last_seq));
766 	mld_sta->dup_data = dup_data;
767 
768 	return 0;
769 }
770 
771 static void iwl_mld_alloc_mpdu_counters(struct iwl_mld *mld,
772 					struct ieee80211_sta *sta)
773 {
774 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
775 	struct ieee80211_vif *vif = mld_sta->vif;
776 
777 	if (mld->fw_status.in_hw_restart)
778 		return;
779 
780 	/* MPDUs are counted only when EMLSR is possible */
781 	if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION ||
782 	    sta->tdls || !ieee80211_vif_is_mld(vif))
783 		return;
784 
785 	mld_sta->mpdu_counters = kzalloc_objs(*mld_sta->mpdu_counters,
786 				              mld->trans->info.num_rxqs);
787 	if (!mld_sta->mpdu_counters)
788 		return;
789 
790 	for (int q = 0; q < mld->trans->info.num_rxqs; q++)
791 		spin_lock_init(&mld_sta->mpdu_counters[q].lock);
792 }
793 
794 static int
795 iwl_mld_init_sta(struct iwl_mld *mld, struct ieee80211_sta *sta,
796 		 struct ieee80211_vif *vif, enum iwl_fw_sta_type type)
797 {
798 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
799 
800 	mld_sta->vif = vif;
801 	mld_sta->sta_type = type;
802 	mld_sta->mld = mld;
803 
804 	if (!mld->fw_status.in_hw_restart)
805 		for (int i = 0; i < ARRAY_SIZE(sta->txq); i++)
806 			iwl_mld_init_txq(iwl_mld_txq_from_mac80211(sta->txq[i]));
807 
808 	iwl_mld_alloc_mpdu_counters(mld, sta);
809 
810 	iwl_mld_toggle_tx_ant(mld, &mld_sta->data_tx_ant);
811 
812 	return iwl_mld_alloc_dup_data(mld, mld_sta);
813 }
814 
815 int iwl_mld_add_sta(struct iwl_mld *mld, struct ieee80211_sta *sta,
816 		    struct ieee80211_vif *vif)
817 {
818 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
819 	struct ieee80211_link_sta *link_sta;
820 	enum iwl_fw_sta_type type;
821 	int link_id;
822 	int ret;
823 
824 	switch (vif->type) {
825 	case NL80211_IFTYPE_NAN:
826 		type = STATION_TYPE_NAN_PEER_NMI;
827 		break;
828 	case NL80211_IFTYPE_NAN_DATA:
829 		type = STATION_TYPE_NAN_PEER_NDI;
830 		break;
831 	default:
832 		type = STATION_TYPE_PEER;
833 		break;
834 	}
835 
836 	ret = iwl_mld_init_sta(mld, sta, vif, type);
837 	if (ret)
838 		return ret;
839 
840 	/* We could have add only the deflink link_sta, but it will not work
841 	 * in the restart case if the single link that is active during
842 	 * reconfig is not the deflink one.
843 	 */
844 	for_each_sta_active_link(mld_sta->vif, sta, link_sta, link_id) {
845 		ret = iwl_mld_add_link_sta(mld, link_sta);
846 		if (ret)
847 			goto destroy_sta;
848 	}
849 
850 	return 0;
851 
852 destroy_sta:
853 	iwl_mld_destroy_sta(sta);
854 
855 	return ret;
856 }
857 
858 void iwl_mld_flush_sta_txqs(struct iwl_mld *mld, struct ieee80211_sta *sta)
859 {
860 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
861 	struct ieee80211_link_sta *link_sta;
862 	int link_id;
863 
864 	for_each_sta_active_link(mld_sta->vif, sta, link_sta, link_id) {
865 		int fw_sta_id = iwl_mld_fw_sta_id_from_link_sta(mld, link_sta);
866 
867 		if (fw_sta_id < 0)
868 			continue;
869 
870 		iwl_mld_flush_link_sta_txqs(mld, fw_sta_id);
871 	}
872 }
873 
874 void iwl_mld_wait_sta_txqs_empty(struct iwl_mld *mld, struct ieee80211_sta *sta)
875 {
876 	/* Avoid a warning in iwl_trans_wait_txq_empty if are anyway on the way
877 	 * to a restart.
878 	 */
879 	if (iwl_mld_error_before_recovery(mld))
880 		return;
881 
882 	for (int i = 0; i < ARRAY_SIZE(sta->txq); i++) {
883 		struct iwl_mld_txq *mld_txq =
884 			iwl_mld_txq_from_mac80211(sta->txq[i]);
885 
886 		if (!mld_txq->status.allocated)
887 			continue;
888 
889 		iwl_trans_wait_txq_empty(mld->trans, mld_txq->fw_id);
890 	}
891 }
892 
893 void iwl_mld_remove_sta(struct iwl_mld *mld, struct ieee80211_sta *sta)
894 {
895 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
896 	struct ieee80211_vif *vif = mld_sta->vif;
897 	struct ieee80211_link_sta *link_sta;
898 	u8 link_id;
899 
900 	lockdep_assert_wiphy(mld->wiphy);
901 
902 	/* Tell the HW to flush the queues */
903 	iwl_mld_flush_sta_txqs(mld, sta);
904 
905 	/* Wait for trans to empty its queues */
906 	iwl_mld_wait_sta_txqs_empty(mld, sta);
907 
908 	/* Now we can remove the queues */
909 	for (int i = 0; i < ARRAY_SIZE(sta->txq); i++)
910 		iwl_mld_remove_txq(mld, sta->txq[i]);
911 
912 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
913 		/* Mac8011 will remove the groupwise keys after the sta is
914 		 * removed, but FW expects all the keys to be removed before
915 		 * the STA is, so remove them all here.
916 		 */
917 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
918 			iwl_mld_remove_ap_keys(mld, vif, sta, link_id);
919 
920 		/* Remove the link_sta */
921 		iwl_mld_remove_link_sta(mld, link_sta);
922 	}
923 
924 	iwl_mld_destroy_sta(sta);
925 }
926 
927 u32 iwl_mld_fw_sta_id_mask(struct iwl_mld *mld, struct ieee80211_sta *sta)
928 {
929 	struct ieee80211_vif *vif = iwl_mld_sta_from_mac80211(sta)->vif;
930 	struct ieee80211_link_sta *link_sta;
931 	unsigned int link_id;
932 	u32 result = 0;
933 
934 	KUNIT_STATIC_STUB_REDIRECT(iwl_mld_fw_sta_id_mask, mld, sta);
935 
936 	/* This function should only be used with the wiphy lock held,
937 	 * In other cases, it is not guaranteed that the link_sta will exist
938 	 * in the driver too, and it is checked in
939 	 * iwl_mld_fw_sta_id_from_link_sta.
940 	 */
941 	lockdep_assert_wiphy(mld->wiphy);
942 
943 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
944 		int fw_id = iwl_mld_fw_sta_id_from_link_sta(mld, link_sta);
945 
946 		if (!(fw_id < 0))
947 			result |= BIT(fw_id);
948 	}
949 
950 	return result;
951 }
952 EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mld_fw_sta_id_mask);
953 
954 static void iwl_mld_count_mpdu(struct ieee80211_link_sta *link_sta, int queue,
955 			       u32 count, bool tx)
956 {
957 	struct iwl_mld_per_q_mpdu_counter *queue_counter;
958 	struct iwl_mld_per_link_mpdu_counter *link_counter;
959 	struct iwl_mld_vif *mld_vif;
960 	struct iwl_mld_sta *mld_sta;
961 	struct iwl_mld_link *mld_link;
962 	struct iwl_mld *mld;
963 	int total_mpdus = 0;
964 
965 	if (WARN_ON(!link_sta))
966 		return;
967 
968 	mld_sta = iwl_mld_sta_from_mac80211(link_sta->sta);
969 	if (!mld_sta->mpdu_counters)
970 		return;
971 
972 	mld_vif = iwl_mld_vif_from_mac80211(mld_sta->vif);
973 	mld_link = iwl_mld_link_dereference_check(mld_vif, link_sta->link_id);
974 
975 	if (WARN_ON_ONCE(!mld_link))
976 		return;
977 
978 	queue_counter = &mld_sta->mpdu_counters[queue];
979 
980 	mld = mld_vif->mld;
981 
982 	/* If it the window is over, first clear the counters.
983 	 * When we are not blocked by TPT, the window is managed by check_tpt_wk
984 	 */
985 	if ((mld_vif->emlsr.blocked_reasons & IWL_MLD_EMLSR_BLOCKED_TPT) &&
986 	    time_is_before_jiffies(queue_counter->window_start_time +
987 					IWL_MLD_TPT_COUNT_WINDOW)) {
988 		memset(queue_counter->per_link, 0,
989 		       sizeof(queue_counter->per_link));
990 		queue_counter->window_start_time = jiffies;
991 
992 		IWL_DEBUG_EHT(mld, "MPDU counters are cleared\n");
993 	}
994 
995 	link_counter = &queue_counter->per_link[mld_link->fw_id];
996 
997 	spin_lock_bh(&queue_counter->lock);
998 
999 	/* Update the statistics for this TPT measurement window */
1000 	if (tx)
1001 		link_counter->tx += count;
1002 	else
1003 		link_counter->rx += count;
1004 
1005 	/*
1006 	 * Next, evaluate whether we should queue an unblock,
1007 	 * skip this if we are not blocked due to low throughput.
1008 	 */
1009 	if (!(mld_vif->emlsr.blocked_reasons & IWL_MLD_EMLSR_BLOCKED_TPT))
1010 		goto unlock;
1011 
1012 	for (int i = 0; i < IWL_FW_MAX_LINKS; i++)
1013 		total_mpdus += tx ? queue_counter->per_link[i].tx :
1014 				    queue_counter->per_link[i].rx;
1015 
1016 	/* Unblock is already queued if the threshold was reached before */
1017 	if (total_mpdus - count >= IWL_MLD_ENTER_EMLSR_TPT_THRESH)
1018 		goto unlock;
1019 
1020 	if (total_mpdus >= IWL_MLD_ENTER_EMLSR_TPT_THRESH)
1021 		wiphy_work_queue(mld->wiphy, &mld_vif->emlsr.unblock_tpt_wk);
1022 
1023 unlock:
1024 	spin_unlock_bh(&queue_counter->lock);
1025 }
1026 
1027 /* must be called under rcu_read_lock() */
1028 void iwl_mld_count_mpdu_rx(struct ieee80211_link_sta *link_sta, int queue,
1029 			   u32 count)
1030 {
1031 	iwl_mld_count_mpdu(link_sta, queue, count, false);
1032 }
1033 
1034 /* must be called under rcu_read_lock() */
1035 void iwl_mld_count_mpdu_tx(struct ieee80211_link_sta *link_sta, u32 count)
1036 {
1037 	/* use queue 0 for all TX */
1038 	iwl_mld_count_mpdu(link_sta, 0, count, true);
1039 }
1040 
1041 static int iwl_mld_allocate_internal_txq(struct iwl_mld *mld,
1042 					 struct iwl_mld_int_sta *internal_sta,
1043 					 u8 tid)
1044 {
1045 	u32 sta_mask = BIT(internal_sta->sta_id);
1046 	int queue, size;
1047 
1048 	size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
1049 		     mld->trans->mac_cfg->base->min_txq_size);
1050 
1051 	queue = iwl_trans_txq_alloc(mld->trans, 0, sta_mask, tid, size,
1052 				    IWL_WATCHDOG_DISABLED);
1053 
1054 	if (queue >= 0)
1055 		IWL_DEBUG_TX_QUEUES(mld,
1056 				    "Enabling TXQ #%d for sta mask 0x%x tid %d\n",
1057 				    queue, sta_mask, tid);
1058 	return queue;
1059 }
1060 
1061 static int iwl_mld_send_aux_sta_cmd(struct iwl_mld *mld,
1062 				    const struct iwl_mld_int_sta *internal_sta)
1063 {
1064 	struct iwl_aux_sta_cmd cmd = {
1065 		.sta_id = cpu_to_le32(internal_sta->sta_id),
1066 		/* TODO: CDB - properly set the lmac_id */
1067 		.lmac_id = cpu_to_le32(IWL_LMAC_24G_INDEX),
1068 	};
1069 
1070 	return iwl_mld_send_cmd_pdu(mld, WIDE_ID(MAC_CONF_GROUP, AUX_STA_CMD),
1071 				    &cmd);
1072 }
1073 
1074 static int
1075 iwl_mld_set_internal_sta_to_fw(struct iwl_mld *mld,
1076 			       const struct iwl_mld_int_sta *internal_sta,
1077 			       u32 link_mask, const u8 *addr)
1078 {
1079 	struct iwl_sta_cfg_cmd cmd = {};
1080 
1081 	if (internal_sta->sta_type == STATION_TYPE_AUX)
1082 		return iwl_mld_send_aux_sta_cmd(mld, internal_sta);
1083 
1084 	cmd.sta_id = cpu_to_le32((u8)internal_sta->sta_id);
1085 	cmd.link_mask = cpu_to_le32(link_mask);
1086 	cmd.station_type = cpu_to_le32(internal_sta->sta_type);
1087 
1088 	/* FW doesn't allow to add a IGTK/BIGTK if the sta isn't marked as MFP.
1089 	 * On the other hand, FW will never check this flag during RX since
1090 	 * an AP/GO doesn't receive protected broadcast management frames.
1091 	 * So, we can set it unconditionally.
1092 	 *
1093 	 * For NAN stations associated with a NAN Device, the MFP bit must be
1094 	 * set to 1, as otherwise the FW will assert when a key associated with
1095 	 * these stations would be added.
1096 	 */
1097 	if (internal_sta->sta_type == STATION_TYPE_BCAST_MGMT ||
1098 	    internal_sta->sta_type == STATION_TYPE_NAN_BCAST ||
1099 	    internal_sta->sta_type == STATION_TYPE_NAN_MGMT)
1100 		cmd.mfp = cpu_to_le32(1);
1101 
1102 	if (addr) {
1103 		if (internal_sta->sta_type == STATION_TYPE_NAN_MCAST_DATA) {
1104 			ether_addr_copy(cmd.ndi_local_addr, addr);
1105 		} else {
1106 			memcpy(cmd.peer_mld_address, addr, ETH_ALEN);
1107 			memcpy(cmd.peer_link_address, addr, ETH_ALEN);
1108 		}
1109 	}
1110 
1111 	return iwl_mld_send_sta_cmd(mld, &cmd);
1112 }
1113 
1114 static int iwl_mld_add_internal_sta(struct iwl_mld *mld,
1115 				    struct iwl_mld_int_sta *internal_sta,
1116 				    enum iwl_fw_sta_type sta_type,
1117 				    u32 link_mask, const u8 *addr,
1118 				    u8 tid, bool add_txq)
1119 {
1120 	int ret, queue_id;
1121 
1122 	ret = iwl_mld_allocate_link_sta_fw_id(mld,
1123 					      &internal_sta->sta_id,
1124 					      ERR_PTR(-EINVAL));
1125 	if (ret)
1126 		return ret;
1127 
1128 	internal_sta->sta_type = sta_type;
1129 
1130 	ret = iwl_mld_set_internal_sta_to_fw(mld, internal_sta, link_mask,
1131 					     addr);
1132 	if (ret)
1133 		goto err;
1134 
1135 	if (!add_txq)
1136 		return 0;
1137 
1138 	queue_id = iwl_mld_allocate_internal_txq(mld, internal_sta, tid);
1139 	if (queue_id < 0) {
1140 		iwl_mld_rm_sta_from_fw(mld, internal_sta->sta_id);
1141 		ret = queue_id;
1142 		goto err;
1143 	}
1144 
1145 	internal_sta->queue_id = queue_id;
1146 
1147 	return 0;
1148 err:
1149 	iwl_mld_free_internal_sta(mld, internal_sta);
1150 	return ret;
1151 }
1152 
1153 int iwl_mld_add_bcast_sta(struct iwl_mld *mld,
1154 			  struct ieee80211_vif *vif,
1155 			  struct ieee80211_bss_conf *link)
1156 {
1157 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1158 	const u8 bcast_addr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1159 	const u8 *addr;
1160 
1161 	if (WARN_ON(!mld_link))
1162 		return -EINVAL;
1163 
1164 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1165 		    vif->type != NL80211_IFTYPE_ADHOC))
1166 		return -EINVAL;
1167 
1168 	addr = vif->type == NL80211_IFTYPE_ADHOC ? link->bssid : bcast_addr;
1169 
1170 	return iwl_mld_add_internal_sta(mld, &mld_link->bcast_sta,
1171 					STATION_TYPE_BCAST_MGMT,
1172 					BIT(mld_link->fw_id), addr,
1173 					IWL_MGMT_TID, true);
1174 }
1175 
1176 int iwl_mld_add_mcast_sta(struct iwl_mld *mld,
1177 			  struct ieee80211_vif *vif,
1178 			  struct ieee80211_bss_conf *link)
1179 {
1180 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1181 	const u8 mcast_addr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
1182 
1183 	if (WARN_ON(!mld_link))
1184 		return -EINVAL;
1185 
1186 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1187 		    vif->type != NL80211_IFTYPE_ADHOC))
1188 		return -EINVAL;
1189 
1190 	return iwl_mld_add_internal_sta(mld, &mld_link->mcast_sta,
1191 					STATION_TYPE_MCAST,
1192 					BIT(mld_link->fw_id), mcast_addr,
1193 					0, true);
1194 }
1195 
1196 int iwl_mld_add_aux_sta(struct iwl_mld *mld,
1197 			struct iwl_mld_int_sta *internal_sta)
1198 {
1199 	return iwl_mld_add_internal_sta(mld, internal_sta, STATION_TYPE_AUX,
1200 					0, NULL, IWL_MAX_TID_COUNT,
1201 					true);
1202 }
1203 
1204 int iwl_mld_add_mon_sta(struct iwl_mld *mld,
1205 			struct ieee80211_vif *vif,
1206 			struct ieee80211_bss_conf *link)
1207 {
1208 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1209 
1210 	if (WARN_ON(!mld_link))
1211 		return -EINVAL;
1212 
1213 	if (WARN_ON(vif->type != NL80211_IFTYPE_MONITOR))
1214 		return -EINVAL;
1215 
1216 	return iwl_mld_add_internal_sta(mld, &mld_link->mon_sta,
1217 					STATION_TYPE_BCAST_MGMT,
1218 					BIT(mld_link->fw_id), NULL,
1219 					IWL_MAX_TID_COUNT,
1220 					true);
1221 }
1222 
1223 static void iwl_mld_remove_internal_sta(struct iwl_mld *mld,
1224 					struct iwl_mld_int_sta *internal_sta,
1225 					bool flush, u8 tid)
1226 {
1227 	if (WARN_ON_ONCE(internal_sta->sta_id == IWL_INVALID_STA))
1228 		return;
1229 
1230 	if (flush && !WARN_ON_ONCE(internal_sta->queue_id ==
1231 				   IWL_MLD_INVALID_QUEUE))
1232 		iwl_mld_flush_link_sta_txqs(mld, internal_sta->sta_id);
1233 
1234 	if (internal_sta->queue_id != IWL_MLD_INVALID_QUEUE)
1235 		iwl_mld_free_txq(mld, BIT(internal_sta->sta_id),
1236 				 tid, internal_sta->queue_id);
1237 
1238 	iwl_mld_rm_sta_from_fw(mld, internal_sta->sta_id);
1239 
1240 	iwl_mld_free_internal_sta(mld, internal_sta);
1241 }
1242 
1243 void iwl_mld_remove_bcast_sta(struct iwl_mld *mld,
1244 			      struct ieee80211_vif *vif,
1245 			      struct ieee80211_bss_conf *link)
1246 {
1247 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1248 
1249 	if (WARN_ON(!mld_link))
1250 		return;
1251 
1252 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1253 		    vif->type != NL80211_IFTYPE_ADHOC))
1254 		return;
1255 
1256 	iwl_mld_remove_internal_sta(mld, &mld_link->bcast_sta, true,
1257 				    IWL_MGMT_TID);
1258 }
1259 
1260 void iwl_mld_remove_mcast_sta(struct iwl_mld *mld,
1261 			      struct ieee80211_vif *vif,
1262 			      struct ieee80211_bss_conf *link)
1263 {
1264 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1265 
1266 	if (WARN_ON(!mld_link))
1267 		return;
1268 
1269 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1270 		    vif->type != NL80211_IFTYPE_ADHOC))
1271 		return;
1272 
1273 	iwl_mld_remove_internal_sta(mld, &mld_link->mcast_sta, true, 0);
1274 }
1275 
1276 void iwl_mld_remove_aux_sta(struct iwl_mld *mld,
1277 			    struct ieee80211_vif *vif)
1278 {
1279 	struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
1280 
1281 	if (WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE &&
1282 		    vif->type != NL80211_IFTYPE_STATION &&
1283 		    vif->type != NL80211_IFTYPE_NAN))
1284 		return;
1285 
1286 	iwl_mld_remove_internal_sta(mld, &mld_vif->aux_sta, false,
1287 				    IWL_MAX_TID_COUNT);
1288 }
1289 
1290 void iwl_mld_remove_mon_sta(struct iwl_mld *mld,
1291 			    struct ieee80211_vif *vif,
1292 			    struct ieee80211_bss_conf *link)
1293 {
1294 	struct iwl_mld_link *mld_link = iwl_mld_link_from_mac80211(link);
1295 
1296 	if (WARN_ON(!mld_link))
1297 		return;
1298 
1299 	if (WARN_ON(vif->type != NL80211_IFTYPE_MONITOR))
1300 		return;
1301 
1302 	iwl_mld_remove_internal_sta(mld, &mld_link->mon_sta, false,
1303 				    IWL_MAX_TID_COUNT);
1304 }
1305 
1306 static int iwl_mld_update_sta_resources(struct iwl_mld *mld,
1307 					struct ieee80211_vif *vif,
1308 					struct ieee80211_sta *sta,
1309 					u32 old_sta_mask,
1310 					u32 new_sta_mask)
1311 {
1312 	int ret;
1313 
1314 	ret = iwl_mld_update_sta_txqs(mld, sta, old_sta_mask, new_sta_mask);
1315 	if (ret)
1316 		return ret;
1317 
1318 	ret = iwl_mld_update_sta_keys(mld, vif, sta, old_sta_mask, new_sta_mask);
1319 	if (ret)
1320 		return ret;
1321 
1322 	return iwl_mld_update_sta_baids(mld, old_sta_mask, new_sta_mask);
1323 }
1324 
1325 int iwl_mld_update_link_stas(struct iwl_mld *mld,
1326 			     struct ieee80211_vif *vif,
1327 			     struct ieee80211_sta *sta,
1328 			     u16 old_links, u16 new_links)
1329 {
1330 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
1331 	struct iwl_mld_link_sta *mld_link_sta;
1332 	unsigned long links_to_add = ~old_links & new_links;
1333 	unsigned long links_to_rem = old_links & ~new_links;
1334 	unsigned long old_links_long = old_links;
1335 	unsigned long sta_mask_added = 0;
1336 	u32 current_sta_mask = 0, sta_mask_to_rem = 0;
1337 	unsigned int link_id, sta_id;
1338 	int ret;
1339 
1340 	lockdep_assert_wiphy(mld->wiphy);
1341 
1342 	for_each_set_bit(link_id, &old_links_long,
1343 			 IEEE80211_MLD_MAX_NUM_LINKS) {
1344 		mld_link_sta =
1345 			iwl_mld_link_sta_dereference_check(mld_sta, link_id);
1346 
1347 		if (WARN_ON(!mld_link_sta))
1348 			return -EINVAL;
1349 
1350 		current_sta_mask |= BIT(mld_link_sta->fw_id);
1351 		if (links_to_rem & BIT(link_id))
1352 			sta_mask_to_rem |= BIT(mld_link_sta->fw_id);
1353 	}
1354 
1355 	if (sta_mask_to_rem) {
1356 		ret = iwl_mld_update_sta_resources(mld, vif, sta,
1357 						   current_sta_mask,
1358 						   current_sta_mask &
1359 							~sta_mask_to_rem);
1360 		if (ret)
1361 			return ret;
1362 
1363 		current_sta_mask &= ~sta_mask_to_rem;
1364 	}
1365 
1366 	for_each_set_bit(link_id, &links_to_rem, IEEE80211_MLD_MAX_NUM_LINKS) {
1367 		struct ieee80211_link_sta *link_sta =
1368 			link_sta_dereference_protected(sta, link_id);
1369 
1370 		if (WARN_ON(!link_sta))
1371 			return -EINVAL;
1372 
1373 		iwl_mld_remove_link_sta(mld, link_sta);
1374 	}
1375 
1376 	for_each_set_bit(link_id, &links_to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
1377 		struct ieee80211_link_sta *link_sta =
1378 			link_sta_dereference_protected(sta, link_id);
1379 		struct ieee80211_bss_conf *link;
1380 
1381 		if (WARN_ON(!link_sta))
1382 			return -EINVAL;
1383 
1384 		ret = iwl_mld_add_link_sta(mld, link_sta);
1385 		if (ret)
1386 			goto remove_added_link_stas;
1387 
1388 		mld_link_sta =
1389 			iwl_mld_link_sta_dereference_check(mld_sta,
1390 							   link_id);
1391 
1392 		link = link_conf_dereference_protected(mld_sta->vif,
1393 						       link_sta->link_id);
1394 
1395 		iwl_mld_set_max_amsdu_len(mld, link_sta);
1396 		iwl_mld_config_tlc_link(mld, vif, link, link_sta);
1397 
1398 		sta_mask_added |= BIT(mld_link_sta->fw_id);
1399 	}
1400 
1401 	if (sta_mask_added) {
1402 		ret = iwl_mld_update_sta_resources(mld, vif, sta,
1403 						   current_sta_mask,
1404 						   current_sta_mask |
1405 							sta_mask_added);
1406 		if (ret)
1407 			goto remove_added_link_stas;
1408 	}
1409 
1410 	/* We couldn't activate the links before it has a STA. Now we can */
1411 	for_each_set_bit(link_id, &links_to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
1412 		struct ieee80211_bss_conf *link =
1413 			link_conf_dereference_protected(mld_sta->vif, link_id);
1414 
1415 		if (WARN_ON(!link))
1416 			continue;
1417 
1418 		iwl_mld_activate_link(mld, link);
1419 	}
1420 
1421 	return 0;
1422 
1423 remove_added_link_stas:
1424 	for_each_set_bit(sta_id, &sta_mask_added, mld->fw->ucode_capa.num_stations) {
1425 		struct ieee80211_link_sta *link_sta =
1426 			wiphy_dereference(mld->wiphy,
1427 					  mld->fw_id_to_link_sta[sta_id]);
1428 
1429 		if (WARN_ON(!link_sta))
1430 			continue;
1431 
1432 		iwl_mld_remove_link_sta(mld, link_sta);
1433 	}
1434 
1435 	return ret;
1436 }
1437 
1438 int iwl_mld_add_nan_bcast_sta(struct iwl_mld *mld,
1439 			      struct iwl_mld_int_sta *sta)
1440 {
1441 	const u8 bcast_addr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1442 
1443 	return iwl_mld_add_internal_sta(mld, sta, STATION_TYPE_NAN_BCAST,
1444 					0, bcast_addr, 0, false);
1445 }
1446 
1447 int iwl_mld_add_nan_mgmt_sta(struct iwl_mld *mld,
1448 			     struct iwl_mld_int_sta *sta)
1449 {
1450 	return iwl_mld_add_internal_sta(mld, sta, STATION_TYPE_NAN_MGMT,
1451 					0, NULL, IWL_MAX_TID_COUNT, true);
1452 }
1453 
1454 int iwl_mld_add_nan_mcast_data_sta(struct iwl_mld *mld,
1455 				   const u8 *ndi_addr,
1456 				   struct iwl_mld_int_sta *sta)
1457 {
1458 	u32 link_mask = iwl_mld_get_nan_link_mask(mld);
1459 
1460 	/* In case that there are no NAN links, nothing to do */
1461 	if (!link_mask)
1462 		return 0;
1463 
1464 	return iwl_mld_add_internal_sta(mld, sta,
1465 					STATION_TYPE_NAN_MCAST_DATA,
1466 					link_mask, ndi_addr,
1467 					0, true);
1468 }
1469 
1470 int iwl_mld_update_nan_mcast_data_sta(struct iwl_mld *mld,
1471 				      const u8 *ndi_addr,
1472 				      struct iwl_mld_int_sta *sta)
1473 {
1474 	u32 link_mask;
1475 
1476 	if (WARN_ON(!mld->nan_device_vif))
1477 		return -EINVAL;
1478 
1479 	/* If the sta doesn't exist, add it */
1480 	if (sta->sta_id == IWL_INVALID_STA)
1481 		return iwl_mld_add_nan_mcast_data_sta(mld, ndi_addr, sta);
1482 
1483 	/* The station was already added */
1484 	if (WARN_ON(sta->sta_type != STATION_TYPE_NAN_MCAST_DATA))
1485 		return -EINVAL;
1486 
1487 	link_mask = iwl_mld_get_nan_link_mask(mld);
1488 	if (link_mask)
1489 		return iwl_mld_set_internal_sta_to_fw(mld,
1490 						      sta, link_mask,
1491 						      ndi_addr);
1492 
1493 	/* If no links are associated with NAN, remove the station */
1494 	iwl_mld_remove_nan_mcast_data_sta(mld, sta);
1495 
1496 	return 0;
1497 }
1498 
1499 void iwl_mld_remove_nan_bcast_sta(struct iwl_mld *mld,
1500 				  struct iwl_mld_int_sta *sta)
1501 {
1502 	iwl_mld_remove_internal_sta(mld, sta, false, 0);
1503 }
1504 
1505 void iwl_mld_remove_nan_mgmt_sta(struct iwl_mld *mld,
1506 				 struct iwl_mld_int_sta *sta)
1507 {
1508 	iwl_mld_remove_internal_sta(mld, sta, true, IWL_MAX_TID_COUNT);
1509 }
1510 
1511 void iwl_mld_remove_nan_mcast_data_sta(struct iwl_mld *mld,
1512 				       struct iwl_mld_int_sta *sta)
1513 {
1514 	iwl_mld_remove_internal_sta(mld, sta, true, 0);
1515 }
1516