xref: /openbmc/linux/drivers/net/wireless/intel/iwlwifi/mvm/mld-sta.c (revision d5a05299306227d73b0febba9cecedf88931c507)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2022 Intel Corporation
4  */
5 #include "mvm.h"
6 #include "time-sync.h"
7 #include "sta.h"
8 
9 u32 iwl_mvm_sta_fw_id_mask(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
10 			   int filter_link_id)
11 {
12 	struct iwl_mvm_sta *mvmsta;
13 	unsigned int link_id;
14 	u32 result = 0;
15 
16 	if (!sta)
17 		return 0;
18 
19 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
20 
21 	/* it's easy when the STA is not an MLD */
22 	if (!sta->valid_links)
23 		return BIT(mvmsta->deflink.sta_id);
24 
25 	/* but if it is an MLD, get the mask of all the FW STAs it has ... */
26 	for (link_id = 0; link_id < ARRAY_SIZE(mvmsta->link); link_id++) {
27 		struct iwl_mvm_link_sta *link_sta;
28 
29 		/* unless we have a specific link in mind */
30 		if (filter_link_id >= 0 && link_id != filter_link_id)
31 			continue;
32 
33 		link_sta =
34 			rcu_dereference_check(mvmsta->link[link_id],
35 					      lockdep_is_held(&mvm->mutex));
36 		if (!link_sta)
37 			continue;
38 
39 		result |= BIT(link_sta->sta_id);
40 	}
41 
42 	return result;
43 }
44 
45 static int iwl_mvm_mld_send_sta_cmd(struct iwl_mvm *mvm,
46 				    struct iwl_mvm_sta_cfg_cmd *cmd)
47 {
48 	int ret = iwl_mvm_send_cmd_pdu(mvm,
49 				       WIDE_ID(MAC_CONF_GROUP, STA_CONFIG_CMD),
50 				       0, sizeof(*cmd), cmd);
51 	if (ret)
52 		IWL_ERR(mvm, "STA_CONFIG_CMD send failed, ret=0x%x\n", ret);
53 	return ret;
54 }
55 
56 /*
57  * Add an internal station to the FW table
58  */
59 static int iwl_mvm_mld_add_int_sta_to_fw(struct iwl_mvm *mvm,
60 					 struct iwl_mvm_int_sta *sta,
61 					 const u8 *addr, int link_id)
62 {
63 	struct iwl_mvm_sta_cfg_cmd cmd;
64 
65 	lockdep_assert_held(&mvm->mutex);
66 
67 	memset(&cmd, 0, sizeof(cmd));
68 	cmd.sta_id = cpu_to_le32((u8)sta->sta_id);
69 
70 	cmd.link_id = cpu_to_le32(link_id);
71 
72 	cmd.station_type = cpu_to_le32(sta->type);
73 
74 	if (fw_has_capa(&mvm->fw->ucode_capa,
75 			IWL_UCODE_TLV_CAPA_STA_EXP_MFP_SUPPORT) &&
76 	    sta->type == STATION_TYPE_BCAST_MGMT)
77 		cmd.mfp = cpu_to_le32(1);
78 
79 	if (addr) {
80 		memcpy(cmd.peer_mld_address, addr, ETH_ALEN);
81 		memcpy(cmd.peer_link_address, addr, ETH_ALEN);
82 	}
83 
84 	return iwl_mvm_mld_send_sta_cmd(mvm, &cmd);
85 }
86 
87 /*
88  * Remove a station from the FW table. Before sending the command to remove
89  * the station validate that the station is indeed known to the driver (sanity
90  * only).
91  */
92 static int iwl_mvm_mld_rm_sta_from_fw(struct iwl_mvm *mvm, u32 sta_id)
93 {
94 	struct iwl_mvm_remove_sta_cmd rm_sta_cmd = {
95 		.sta_id = cpu_to_le32(sta_id),
96 	};
97 	int ret;
98 
99 	/* Note: internal stations are marked as error values */
100 	if (!rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
101 		IWL_ERR(mvm, "Invalid station id %d\n", sta_id);
102 		return -EINVAL;
103 	}
104 
105 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, STA_REMOVE_CMD),
106 				   0, sizeof(rm_sta_cmd), &rm_sta_cmd);
107 	if (ret) {
108 		IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
115 static int iwl_mvm_add_aux_sta_to_fw(struct iwl_mvm *mvm,
116 				     struct iwl_mvm_int_sta *sta,
117 				     u32 lmac_id)
118 {
119 	int ret;
120 
121 	struct iwl_mvm_aux_sta_cmd cmd = {
122 		.sta_id = cpu_to_le32(sta->sta_id),
123 		.lmac_id = cpu_to_le32(lmac_id),
124 	};
125 
126 	ret = iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, AUX_STA_CMD),
127 				   0, sizeof(cmd), &cmd);
128 	if (ret)
129 		IWL_ERR(mvm, "Failed to send AUX_STA_CMD\n");
130 	return ret;
131 }
132 
133 /*
134  * Adds an internal sta to the FW table with its queues
135  */
136 int iwl_mvm_mld_add_int_sta_with_queue(struct iwl_mvm *mvm,
137 				       struct iwl_mvm_int_sta *sta,
138 				       const u8 *addr, int link_id,
139 				       u16 *queue, u8 tid,
140 				       unsigned int *_wdg_timeout)
141 {
142 	int ret, txq;
143 	unsigned int wdg_timeout = _wdg_timeout ? *_wdg_timeout :
144 		mvm->trans->trans_cfg->base_params->wd_timeout;
145 
146 	if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
147 		return -ENOSPC;
148 
149 	if (sta->type == STATION_TYPE_AUX)
150 		ret = iwl_mvm_add_aux_sta_to_fw(mvm, sta, link_id);
151 	else
152 		ret = iwl_mvm_mld_add_int_sta_to_fw(mvm, sta, addr, link_id);
153 	if (ret)
154 		return ret;
155 
156 	/*
157 	 * For 22000 firmware and on we cannot add queue to a station unknown
158 	 * to firmware so enable queue here - after the station was added
159 	 */
160 	txq = iwl_mvm_tvqm_enable_txq(mvm, NULL, sta->sta_id, tid,
161 				      wdg_timeout);
162 	if (txq < 0) {
163 		iwl_mvm_mld_rm_sta_from_fw(mvm, sta->sta_id);
164 		return txq;
165 	}
166 	*queue = txq;
167 
168 	return 0;
169 }
170 
171 /*
172  * Adds a new int sta: allocate it in the driver, add it to the FW table,
173  * and add its queues.
174  */
175 static int iwl_mvm_mld_add_int_sta(struct iwl_mvm *mvm,
176 				   struct iwl_mvm_int_sta *int_sta, u16 *queue,
177 				   enum nl80211_iftype iftype,
178 				   enum iwl_fw_sta_type sta_type,
179 				   int link_id, const u8 *addr, u8 tid,
180 				   unsigned int *wdg_timeout)
181 {
182 	int ret;
183 
184 	lockdep_assert_held(&mvm->mutex);
185 
186 	/* qmask argument is not used in the new tx api, send a don't care */
187 	ret = iwl_mvm_allocate_int_sta(mvm, int_sta, 0, iftype,
188 				       sta_type);
189 	if (ret)
190 		return ret;
191 
192 	ret = iwl_mvm_mld_add_int_sta_with_queue(mvm, int_sta, addr, link_id,
193 						 queue, tid, wdg_timeout);
194 	if (ret) {
195 		iwl_mvm_dealloc_int_sta(mvm, int_sta);
196 		return ret;
197 	}
198 
199 	return 0;
200 }
201 
202 /* Allocate a new station entry for the broadcast station to the given vif,
203  * and send it to the FW.
204  * Note that each P2P mac should have its own broadcast station.
205  */
206 int iwl_mvm_mld_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
207 			      struct ieee80211_bss_conf *link_conf)
208 {
209 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
210 	struct iwl_mvm_vif_link_info *mvm_link =
211 		mvmvif->link[link_conf->link_id];
212 	struct iwl_mvm_int_sta *bsta = &mvm_link->bcast_sta;
213 	static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
214 	const u8 *baddr = _baddr;
215 	unsigned int wdg_timeout =
216 		iwl_mvm_get_wd_timeout(mvm, vif, false, false);
217 	u16 *queue;
218 
219 	lockdep_assert_held(&mvm->mutex);
220 
221 	if (vif->type == NL80211_IFTYPE_ADHOC)
222 		baddr = link_conf->bssid;
223 
224 	if (vif->type == NL80211_IFTYPE_AP ||
225 	    vif->type == NL80211_IFTYPE_ADHOC) {
226 		queue = &mvm_link->mgmt_queue;
227 	} else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
228 		queue = &mvm->p2p_dev_queue;
229 	} else {
230 		WARN(1, "Missing required TXQ for adding bcast STA\n");
231 		return -EINVAL;
232 	}
233 
234 	return iwl_mvm_mld_add_int_sta(mvm, bsta, queue,
235 				       ieee80211_vif_type_p2p(vif),
236 				       STATION_TYPE_BCAST_MGMT,
237 				       mvm_link->fw_link_id, baddr,
238 				       IWL_MAX_TID_COUNT, &wdg_timeout);
239 }
240 
241 /* Allocate a new station entry for the broadcast station to the given vif,
242  * and send it to the FW.
243  * Note that each AP/GO mac should have its own multicast station.
244  */
245 int iwl_mvm_mld_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
246 			      struct ieee80211_bss_conf *link_conf)
247 {
248 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
249 	struct iwl_mvm_vif_link_info *mvm_link =
250 		mvmvif->link[link_conf->link_id];
251 	struct iwl_mvm_int_sta *msta = &mvm_link->mcast_sta;
252 	static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
253 	const u8 *maddr = _maddr;
254 	unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
255 
256 	lockdep_assert_held(&mvm->mutex);
257 
258 	if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
259 		    vif->type != NL80211_IFTYPE_ADHOC))
260 		return -EOPNOTSUPP;
261 
262 	/* In IBSS, ieee80211_check_queues() sets the cab_queue to be
263 	 * invalid, so make sure we use the queue we want.
264 	 * Note that this is done here as we want to avoid making DQA
265 	 * changes in mac80211 layer.
266 	 */
267 	if (vif->type == NL80211_IFTYPE_ADHOC)
268 		mvm_link->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
269 
270 	return iwl_mvm_mld_add_int_sta(mvm, msta, &mvm_link->cab_queue,
271 				       vif->type, STATION_TYPE_MCAST,
272 				       mvm_link->fw_link_id, maddr, 0,
273 				       &timeout);
274 }
275 
276 /* Allocate a new station entry for the sniffer station to the given vif,
277  * and send it to the FW.
278  */
279 int iwl_mvm_mld_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
280 			     struct ieee80211_bss_conf *link_conf)
281 {
282 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
283 	struct iwl_mvm_vif_link_info *mvm_link =
284 		mvmvif->link[link_conf->link_id];
285 
286 	lockdep_assert_held(&mvm->mutex);
287 
288 	return iwl_mvm_mld_add_int_sta(mvm, &mvm->snif_sta, &mvm->snif_queue,
289 				       vif->type, STATION_TYPE_BCAST_MGMT,
290 				       mvm_link->fw_link_id, NULL,
291 				       IWL_MAX_TID_COUNT, NULL);
292 }
293 
294 int iwl_mvm_mld_add_aux_sta(struct iwl_mvm *mvm, u32 lmac_id)
295 {
296 	lockdep_assert_held(&mvm->mutex);
297 
298 	/* In CDB NICs we need to specify which lmac to use for aux activity;
299 	 * use the link_id argument place to send lmac_id to the function.
300 	 */
301 	return iwl_mvm_mld_add_int_sta(mvm, &mvm->aux_sta, &mvm->aux_queue,
302 				       NL80211_IFTYPE_UNSPECIFIED,
303 				       STATION_TYPE_AUX, lmac_id, NULL,
304 				       IWL_MAX_TID_COUNT, NULL);
305 }
306 
307 static int iwl_mvm_mld_disable_txq(struct iwl_mvm *mvm, u32 sta_mask,
308 				   u16 *queueptr, u8 tid)
309 {
310 	int queue = *queueptr;
311 	int ret = 0;
312 
313 	if (tid == IWL_MAX_TID_COUNT)
314 		tid = IWL_MGMT_TID;
315 
316 	if (mvm->sta_remove_requires_queue_remove) {
317 		u32 cmd_id = WIDE_ID(DATA_PATH_GROUP,
318 				     SCD_QUEUE_CONFIG_CMD);
319 		struct iwl_scd_queue_cfg_cmd remove_cmd = {
320 			.operation = cpu_to_le32(IWL_SCD_QUEUE_REMOVE),
321 			.u.remove.tid = cpu_to_le32(tid),
322 			.u.remove.sta_mask = cpu_to_le32(sta_mask),
323 		};
324 
325 		ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0,
326 					   sizeof(remove_cmd),
327 					   &remove_cmd);
328 	}
329 
330 	iwl_trans_txq_free(mvm->trans, queue);
331 	*queueptr = IWL_MVM_INVALID_QUEUE;
332 
333 	return ret;
334 }
335 
336 /* Removes a sta from the FW table, disable its queues, and dealloc it
337  */
338 static int iwl_mvm_mld_rm_int_sta(struct iwl_mvm *mvm,
339 				  struct iwl_mvm_int_sta *int_sta,
340 				  bool flush, u8 tid, u16 *queuptr)
341 {
342 	int ret;
343 
344 	lockdep_assert_held(&mvm->mutex);
345 
346 	if (WARN_ON_ONCE(int_sta->sta_id == IWL_MVM_INVALID_STA))
347 		return -EINVAL;
348 
349 	if (flush)
350 		iwl_mvm_flush_sta(mvm, int_sta, true);
351 
352 	iwl_mvm_mld_disable_txq(mvm, BIT(int_sta->sta_id), queuptr, tid);
353 
354 	ret = iwl_mvm_mld_rm_sta_from_fw(mvm, int_sta->sta_id);
355 	if (ret)
356 		IWL_WARN(mvm, "Failed sending remove station\n");
357 
358 	iwl_mvm_dealloc_int_sta(mvm, int_sta);
359 
360 	return ret;
361 }
362 
363 int iwl_mvm_mld_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
364 			     struct ieee80211_bss_conf *link_conf)
365 {
366 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
367 	struct iwl_mvm_vif_link_info *link = mvmvif->link[link_conf->link_id];
368 	u16 *queueptr;
369 
370 	lockdep_assert_held(&mvm->mutex);
371 
372 	switch (vif->type) {
373 	case NL80211_IFTYPE_AP:
374 	case NL80211_IFTYPE_ADHOC:
375 		queueptr = &link->mgmt_queue;
376 		break;
377 	case NL80211_IFTYPE_P2P_DEVICE:
378 		queueptr = &mvm->p2p_dev_queue;
379 		break;
380 	default:
381 		WARN(1, "Can't free bcast queue on vif type %d\n",
382 		     vif->type);
383 		return -EINVAL;
384 	}
385 
386 	return iwl_mvm_mld_rm_int_sta(mvm, &link->bcast_sta,
387 				      true, IWL_MAX_TID_COUNT, queueptr);
388 }
389 
390 /* Send the FW a request to remove the station from it's internal data
391  * structures, and in addition remove it from the local data structure.
392  */
393 int iwl_mvm_mld_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
394 			     struct ieee80211_bss_conf *link_conf)
395 {
396 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
397 	struct iwl_mvm_vif_link_info *link = mvmvif->link[link_conf->link_id];
398 
399 	lockdep_assert_held(&mvm->mutex);
400 
401 	return iwl_mvm_mld_rm_int_sta(mvm, &link->mcast_sta, true, 0,
402 				      &link->cab_queue);
403 }
404 
405 int iwl_mvm_mld_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
406 {
407 	lockdep_assert_held(&mvm->mutex);
408 
409 	return iwl_mvm_mld_rm_int_sta(mvm, &mvm->snif_sta, false,
410 				      IWL_MAX_TID_COUNT, &mvm->snif_queue);
411 }
412 
413 int iwl_mvm_mld_rm_aux_sta(struct iwl_mvm *mvm)
414 {
415 	lockdep_assert_held(&mvm->mutex);
416 
417 	return iwl_mvm_mld_rm_int_sta(mvm, &mvm->aux_sta, false,
418 				      IWL_MAX_TID_COUNT, &mvm->aux_queue);
419 }
420 
421 /* send a cfg sta command to add/update a sta in firmware */
422 static int iwl_mvm_mld_cfg_sta(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
423 			       struct ieee80211_vif *vif,
424 			       struct ieee80211_link_sta *link_sta,
425 			       struct ieee80211_bss_conf *link_conf,
426 			       struct iwl_mvm_link_sta *mvm_link_sta)
427 {
428 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
429 	struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);
430 	struct iwl_mvm_vif_link_info *link_info =
431 					mvm_vif->link[link_conf->link_id];
432 	struct iwl_mvm_sta_cfg_cmd cmd = {
433 		.sta_id = cpu_to_le32(mvm_link_sta->sta_id),
434 		.station_type = cpu_to_le32(mvm_sta->sta_type),
435 	};
436 	u32 agg_size = 0, mpdu_dens = 0;
437 
438 	/* when adding sta, link should exist in FW */
439 	if (WARN_ON(link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID))
440 		return -EINVAL;
441 
442 	cmd.link_id = cpu_to_le32(link_info->fw_link_id);
443 
444 	memcpy(&cmd.peer_mld_address, sta->addr, ETH_ALEN);
445 	memcpy(&cmd.peer_link_address, link_sta->addr, ETH_ALEN);
446 
447 	if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
448 		cmd.assoc_id = cpu_to_le32(sta->aid);
449 
450 	if (fw_has_capa(&mvm->fw->ucode_capa,
451 			IWL_UCODE_TLV_CAPA_STA_EXP_MFP_SUPPORT) &&
452 	    (sta->mfp || mvm_sta->sta_state < IEEE80211_STA_AUTHORIZED))
453 		cmd.mfp = cpu_to_le32(1);
454 
455 	switch (link_sta->rx_nss) {
456 	case 1:
457 		cmd.mimo = cpu_to_le32(0);
458 		break;
459 	case 2 ... 8:
460 		cmd.mimo = cpu_to_le32(1);
461 		break;
462 	}
463 
464 	switch (sta->deflink.smps_mode) {
465 	case IEEE80211_SMPS_AUTOMATIC:
466 	case IEEE80211_SMPS_NUM_MODES:
467 		WARN_ON(1);
468 		break;
469 	case IEEE80211_SMPS_STATIC:
470 		/* override NSS */
471 		cmd.mimo = cpu_to_le32(0);
472 		break;
473 	case IEEE80211_SMPS_DYNAMIC:
474 		cmd.mimo_protection = cpu_to_le32(1);
475 		break;
476 	case IEEE80211_SMPS_OFF:
477 		/* nothing */
478 		break;
479 	}
480 
481 	mpdu_dens = iwl_mvm_get_sta_ampdu_dens(link_sta, link_conf, &agg_size);
482 	cmd.tx_ampdu_spacing = cpu_to_le32(mpdu_dens);
483 	cmd.tx_ampdu_max_size = cpu_to_le32(agg_size);
484 
485 	if (sta->wme) {
486 		cmd.sp_length =
487 			cpu_to_le32(sta->max_sp ? sta->max_sp * 2 : 128);
488 		cmd.uapsd_acs = cpu_to_le32(iwl_mvm_get_sta_uapsd_acs(sta));
489 	}
490 
491 	if (link_sta->he_cap.has_he) {
492 		cmd.trig_rnd_alloc =
493 			cpu_to_le32(link_conf->uora_exists ? 1 : 0);
494 
495 		/* PPE Thresholds */
496 		iwl_mvm_set_sta_pkt_ext(mvm, link_sta, &cmd.pkt_ext);
497 
498 		/* HTC flags */
499 		cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, link_sta);
500 
501 		if (link_sta->he_cap.he_cap_elem.mac_cap_info[2] &
502 		    IEEE80211_HE_MAC_CAP2_ACK_EN)
503 			cmd.ack_enabled = cpu_to_le32(1);
504 	}
505 
506 	return iwl_mvm_mld_send_sta_cmd(mvm, &cmd);
507 }
508 
509 static void iwl_mvm_mld_free_sta_link(struct iwl_mvm *mvm,
510 				      struct iwl_mvm_sta *mvm_sta,
511 				      struct iwl_mvm_link_sta *mvm_sta_link,
512 				      unsigned int link_id,
513 				      bool is_in_fw)
514 {
515 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta_link->sta_id],
516 			 is_in_fw ? ERR_PTR(-EINVAL) : NULL);
517 	RCU_INIT_POINTER(mvm->fw_id_to_link_sta[mvm_sta_link->sta_id], NULL);
518 	RCU_INIT_POINTER(mvm_sta->link[link_id], NULL);
519 
520 	if (mvm_sta_link != &mvm_sta->deflink)
521 		kfree_rcu(mvm_sta_link, rcu_head);
522 }
523 
524 static void iwl_mvm_mld_sta_rm_all_sta_links(struct iwl_mvm *mvm,
525 					     struct iwl_mvm_sta *mvm_sta)
526 {
527 	unsigned int link_id;
528 
529 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
530 		struct iwl_mvm_link_sta *link =
531 			rcu_dereference_protected(mvm_sta->link[link_id],
532 						  lockdep_is_held(&mvm->mutex));
533 
534 		if (!link)
535 			continue;
536 
537 		iwl_mvm_mld_free_sta_link(mvm, mvm_sta, link, link_id, false);
538 	}
539 }
540 
541 static int iwl_mvm_mld_alloc_sta_link(struct iwl_mvm *mvm,
542 				      struct ieee80211_vif *vif,
543 				      struct ieee80211_sta *sta,
544 				      unsigned int link_id)
545 {
546 	struct ieee80211_link_sta *link_sta =
547 		link_sta_dereference_protected(sta, link_id);
548 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
549 	struct iwl_mvm_link_sta *link;
550 	u32 sta_id = iwl_mvm_find_free_sta_id(mvm,
551 					  ieee80211_vif_type_p2p(vif));
552 
553 	if (sta_id == IWL_MVM_INVALID_STA)
554 		return -ENOSPC;
555 
556 	if (rcu_access_pointer(sta->link[link_id]) == &sta->deflink) {
557 		link = &mvm_sta->deflink;
558 	} else {
559 		link = kzalloc(sizeof(*link), GFP_KERNEL);
560 		if (!link)
561 			return -ENOMEM;
562 	}
563 
564 	link->sta_id = sta_id;
565 	rcu_assign_pointer(mvm_sta->link[link_id], link);
566 	rcu_assign_pointer(mvm->fw_id_to_mac_id[link->sta_id], sta);
567 	rcu_assign_pointer(mvm->fw_id_to_link_sta[link->sta_id],
568 			   link_sta);
569 
570 	return 0;
571 }
572 
573 /* allocate all the links of a sta, called when the station is first added */
574 static int iwl_mvm_mld_alloc_sta_links(struct iwl_mvm *mvm,
575 				       struct ieee80211_vif *vif,
576 				       struct ieee80211_sta *sta)
577 {
578 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
579 	unsigned int link_id;
580 	int ret;
581 
582 	lockdep_assert_held(&mvm->mutex);
583 
584 	for (link_id = 0; link_id < ARRAY_SIZE(sta->link); link_id++) {
585 		if (!rcu_access_pointer(sta->link[link_id]) ||
586 		    mvm_sta->link[link_id])
587 			continue;
588 
589 		ret = iwl_mvm_mld_alloc_sta_link(mvm, vif, sta, link_id);
590 		if (ret)
591 			goto err;
592 	}
593 
594 	return 0;
595 
596 err:
597 	iwl_mvm_mld_sta_rm_all_sta_links(mvm, mvm_sta);
598 	return ret;
599 }
600 
601 static void iwl_mvm_mld_set_ap_sta_id(struct ieee80211_sta *sta,
602 				      struct iwl_mvm_vif_link_info *vif_link,
603 				      struct iwl_mvm_link_sta *sta_link)
604 {
605 	if (!sta->tdls) {
606 		WARN_ON(vif_link->ap_sta_id != IWL_MVM_INVALID_STA);
607 		vif_link->ap_sta_id = sta_link->sta_id;
608 	} else {
609 		WARN_ON(vif_link->ap_sta_id == IWL_MVM_INVALID_STA);
610 	}
611 }
612 
613 /* FIXME: consider waiting for mac80211 to add the STA instead of allocating
614  * queues here
615  */
616 static int iwl_mvm_alloc_sta_after_restart(struct iwl_mvm *mvm,
617 					   struct ieee80211_vif *vif,
618 					   struct ieee80211_sta *sta)
619 {
620 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
621 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
622 	struct ieee80211_link_sta *link_sta;
623 	unsigned int link_id;
624 	/* no active link found */
625 	int ret = -EINVAL;
626 	int sta_id;
627 
628 	/* First add an empty station since allocating a queue requires
629 	 * a valid station. Since we need a link_id to allocate a station,
630 	 * pick up the first valid one.
631 	 */
632 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
633 		struct iwl_mvm_vif_link_info *mvm_link;
634 		struct ieee80211_bss_conf *link_conf =
635 			link_conf_dereference_protected(vif, link_id);
636 		struct iwl_mvm_link_sta *mvm_link_sta =
637 			rcu_dereference_protected(mvm_sta->link[link_id],
638 						  lockdep_is_held(&mvm->mutex));
639 
640 		if (!link_conf)
641 			continue;
642 
643 		mvm_link = mvmvif->link[link_conf->link_id];
644 
645 		if (!mvm_link || !mvm_link_sta)
646 			continue;
647 
648 		sta_id = mvm_link_sta->sta_id;
649 		ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta,
650 					  link_conf, mvm_link_sta);
651 		if (ret)
652 			return ret;
653 
654 		rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
655 		rcu_assign_pointer(mvm->fw_id_to_link_sta[sta_id], link_sta);
656 		ret = 0;
657 	}
658 
659 	iwl_mvm_realloc_queues_after_restart(mvm, sta);
660 
661 	return ret;
662 }
663 
664 int iwl_mvm_mld_add_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
665 			struct ieee80211_sta *sta)
666 {
667 	struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);
668 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
669 	unsigned long link_sta_added_to_fw = 0;
670 	struct ieee80211_link_sta *link_sta;
671 	int ret = 0;
672 	unsigned int link_id;
673 
674 	lockdep_assert_held(&mvm->mutex);
675 
676 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
677 		ret = iwl_mvm_mld_alloc_sta_links(mvm, vif, sta);
678 		if (ret)
679 			return ret;
680 
681 		spin_lock_init(&mvm_sta->lock);
682 
683 		ret = iwl_mvm_sta_init(mvm, vif, sta, IWL_MVM_INVALID_STA,
684 				       STATION_TYPE_PEER);
685 	} else {
686 		ret = iwl_mvm_alloc_sta_after_restart(mvm, vif, sta);
687 	}
688 
689 	if (ret)
690 		goto err;
691 
692 	/* at this stage sta link pointers are already allocated */
693 	ret = iwl_mvm_mld_update_sta(mvm, vif, sta);
694 
695 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
696 		struct ieee80211_bss_conf *link_conf =
697 			link_conf_dereference_protected(vif, link_id);
698 		struct iwl_mvm_link_sta *mvm_link_sta =
699 			rcu_dereference_protected(mvm_sta->link[link_id],
700 						  lockdep_is_held(&mvm->mutex));
701 
702 		if (WARN_ON(!link_conf || !mvm_link_sta))
703 			goto err;
704 
705 		ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,
706 					  mvm_link_sta);
707 		if (ret)
708 			goto err;
709 
710 		link_sta_added_to_fw |= BIT(link_id);
711 
712 		if (vif->type == NL80211_IFTYPE_STATION)
713 			iwl_mvm_mld_set_ap_sta_id(sta, mvm_vif->link[link_id],
714 						  mvm_link_sta);
715 	}
716 
717 	return 0;
718 
719 err:
720 	/* remove all already allocated stations in FW */
721 	for_each_set_bit(link_id, &link_sta_added_to_fw,
722 			 IEEE80211_MLD_MAX_NUM_LINKS) {
723 		struct iwl_mvm_link_sta *mvm_link_sta =
724 			rcu_dereference_protected(mvm_sta->link[link_id],
725 						  lockdep_is_held(&mvm->mutex));
726 
727 		iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_link_sta->sta_id);
728 	}
729 
730 	/* free all sta resources in the driver */
731 	iwl_mvm_mld_sta_rm_all_sta_links(mvm, mvm_sta);
732 	return ret;
733 }
734 
735 int iwl_mvm_mld_update_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
736 			   struct ieee80211_sta *sta)
737 {
738 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
739 	struct ieee80211_link_sta *link_sta;
740 	unsigned int link_id;
741 	int ret = -EINVAL;
742 
743 	lockdep_assert_held(&mvm->mutex);
744 
745 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
746 		struct ieee80211_bss_conf *link_conf =
747 			link_conf_dereference_protected(vif, link_id);
748 		struct iwl_mvm_link_sta *mvm_link_sta =
749 			rcu_dereference_protected(mvm_sta->link[link_id],
750 						  lockdep_is_held(&mvm->mutex));
751 
752 		if (WARN_ON(!link_conf || !mvm_link_sta))
753 			return -EINVAL;
754 
755 		ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,
756 					  mvm_link_sta);
757 
758 		if (ret) {
759 			IWL_ERR(mvm, "Failed to update sta link %d\n", link_id);
760 			break;
761 		}
762 	}
763 
764 	return ret;
765 }
766 
767 static void iwl_mvm_mld_disable_sta_queues(struct iwl_mvm *mvm,
768 					   struct ieee80211_vif *vif,
769 					   struct ieee80211_sta *sta)
770 {
771 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
772 	u32 sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);
773 	int i;
774 
775 	lockdep_assert_held(&mvm->mutex);
776 
777 	for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
778 		if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
779 			continue;
780 
781 		iwl_mvm_mld_disable_txq(mvm, sta_mask,
782 					&mvm_sta->tid_data[i].txq_id, i);
783 		mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
784 	}
785 
786 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
787 		struct iwl_mvm_txq *mvmtxq =
788 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
789 
790 		mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
791 	}
792 }
793 
794 int iwl_mvm_mld_rm_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
795 		       struct ieee80211_sta *sta)
796 {
797 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
798 	struct ieee80211_link_sta *link_sta;
799 	unsigned int link_id;
800 	int ret;
801 
802 	lockdep_assert_held(&mvm->mutex);
803 
804 	/* flush its queues here since we are freeing mvm_sta */
805 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
806 		struct iwl_mvm_link_sta *mvm_link_sta =
807 			rcu_dereference_protected(mvm_sta->link[link_id],
808 						  lockdep_is_held(&mvm->mutex));
809 
810 		if (WARN_ON(!mvm_link_sta))
811 			return -EINVAL;
812 
813 		ret = iwl_mvm_flush_sta_tids(mvm, mvm_link_sta->sta_id,
814 					     0xffff);
815 		if (ret)
816 			return ret;
817 	}
818 
819 	ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
820 	if (ret)
821 		return ret;
822 
823 	iwl_mvm_mld_disable_sta_queues(mvm, vif, sta);
824 
825 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
826 		struct iwl_mvm_link_sta *mvm_link_sta =
827 			rcu_dereference_protected(mvm_sta->link[link_id],
828 						  lockdep_is_held(&mvm->mutex));
829 		bool stay_in_fw;
830 
831 		stay_in_fw = iwl_mvm_sta_del(mvm, vif, sta, link_sta, &ret);
832 		if (ret)
833 			break;
834 
835 		if (!stay_in_fw)
836 			ret = iwl_mvm_mld_rm_sta_from_fw(mvm,
837 							 mvm_link_sta->sta_id);
838 
839 		iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
840 					  link_id, stay_in_fw);
841 	}
842 
843 	return ret;
844 }
845 
846 int iwl_mvm_mld_rm_sta_id(struct iwl_mvm *mvm, u8 sta_id)
847 {
848 	int ret = iwl_mvm_mld_rm_sta_from_fw(mvm, sta_id);
849 
850 	lockdep_assert_held(&mvm->mutex);
851 
852 	RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
853 	RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
854 	return ret;
855 }
856 
857 void iwl_mvm_mld_sta_modify_disable_tx(struct iwl_mvm *mvm,
858 				       struct iwl_mvm_sta *mvmsta,
859 				       bool disable)
860 {
861 	struct iwl_mvm_sta_disable_tx_cmd cmd;
862 	int ret;
863 
864 	cmd.sta_id = cpu_to_le32(mvmsta->deflink.sta_id);
865 	cmd.disable = cpu_to_le32(disable);
866 
867 	ret = iwl_mvm_send_cmd_pdu(mvm,
868 				   WIDE_ID(MAC_CONF_GROUP, STA_DISABLE_TX_CMD),
869 				   CMD_ASYNC, sizeof(cmd), &cmd);
870 	if (ret)
871 		IWL_ERR(mvm,
872 			"Failed to send STA_DISABLE_TX_CMD command (%d)\n",
873 			ret);
874 }
875 
876 void iwl_mvm_mld_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
877 					  struct ieee80211_sta *sta,
878 					  bool disable)
879 {
880 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
881 
882 	spin_lock_bh(&mvm_sta->lock);
883 
884 	if (mvm_sta->disable_tx == disable) {
885 		spin_unlock_bh(&mvm_sta->lock);
886 		return;
887 	}
888 
889 	iwl_mvm_mld_sta_modify_disable_tx(mvm, mvm_sta, disable);
890 
891 	spin_unlock_bh(&mvm_sta->lock);
892 }
893 
894 void iwl_mvm_mld_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
895 					   struct iwl_mvm_vif *mvmvif,
896 					   bool disable)
897 {
898 	struct ieee80211_sta *sta;
899 	struct iwl_mvm_sta *mvm_sta;
900 	int i;
901 
902 	rcu_read_lock();
903 
904 	/* Block/unblock all the stations of the given mvmvif */
905 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
906 		sta = rcu_dereference(mvm->fw_id_to_mac_id[i]);
907 		if (IS_ERR_OR_NULL(sta))
908 			continue;
909 
910 		mvm_sta = iwl_mvm_sta_from_mac80211(sta);
911 		if (mvm_sta->mac_id_n_color !=
912 		    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
913 			continue;
914 
915 		iwl_mvm_mld_sta_modify_disable_tx(mvm, mvm_sta, disable);
916 	}
917 
918 	rcu_read_unlock();
919 }
920 
921 static int iwl_mvm_mld_update_sta_queues(struct iwl_mvm *mvm,
922 					 struct ieee80211_sta *sta,
923 					 u32 old_sta_mask,
924 					 u32 new_sta_mask)
925 {
926 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
927 	struct iwl_scd_queue_cfg_cmd cmd = {
928 		.operation = cpu_to_le32(IWL_SCD_QUEUE_MODIFY),
929 		.u.modify.old_sta_mask = cpu_to_le32(old_sta_mask),
930 		.u.modify.new_sta_mask = cpu_to_le32(new_sta_mask),
931 	};
932 	struct iwl_host_cmd hcmd = {
933 		.id = WIDE_ID(DATA_PATH_GROUP, SCD_QUEUE_CONFIG_CMD),
934 		.len[0] = sizeof(cmd),
935 		.data[0] = &cmd
936 	};
937 	int tid;
938 	int ret;
939 
940 	lockdep_assert_held(&mvm->mutex);
941 
942 	for (tid = 0; tid <= IWL_MAX_TID_COUNT; tid++) {
943 		struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[tid];
944 		int txq_id = tid_data->txq_id;
945 
946 		if (txq_id == IWL_MVM_INVALID_QUEUE)
947 			continue;
948 
949 		if (tid == IWL_MAX_TID_COUNT)
950 			cmd.u.modify.tid = cpu_to_le32(IWL_MGMT_TID);
951 		else
952 			cmd.u.modify.tid = cpu_to_le32(tid);
953 
954 		ret = iwl_mvm_send_cmd(mvm, &hcmd);
955 		if (ret)
956 			return ret;
957 	}
958 
959 	return 0;
960 }
961 
962 static int iwl_mvm_mld_update_sta_baids(struct iwl_mvm *mvm,
963 					u32 old_sta_mask,
964 					u32 new_sta_mask)
965 {
966 	struct iwl_rx_baid_cfg_cmd cmd = {
967 		.action = cpu_to_le32(IWL_RX_BAID_ACTION_MODIFY),
968 		.modify.old_sta_id_mask = cpu_to_le32(old_sta_mask),
969 		.modify.new_sta_id_mask = cpu_to_le32(new_sta_mask),
970 	};
971 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
972 	int baid;
973 
974 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
975 
976 	for (baid = 0; baid < ARRAY_SIZE(mvm->baid_map); baid++) {
977 		struct iwl_mvm_baid_data *data;
978 		int ret;
979 
980 		data = rcu_dereference_protected(mvm->baid_map[baid],
981 						 lockdep_is_held(&mvm->mutex));
982 		if (!data)
983 			continue;
984 
985 		if (!(data->sta_mask & old_sta_mask))
986 			continue;
987 
988 		WARN_ONCE(data->sta_mask != old_sta_mask,
989 			  "BAID data for %d corrupted - expected 0x%x found 0x%x\n",
990 			  baid, old_sta_mask, data->sta_mask);
991 
992 		cmd.modify.tid = cpu_to_le32(data->tid);
993 
994 		ret = iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, sizeof(cmd), &cmd);
995 		data->sta_mask = new_sta_mask;
996 		if (ret)
997 			return ret;
998 	}
999 
1000 	return 0;
1001 }
1002 
1003 static int iwl_mvm_mld_update_sta_resources(struct iwl_mvm *mvm,
1004 					    struct ieee80211_vif *vif,
1005 					    struct ieee80211_sta *sta,
1006 					    u32 old_sta_mask,
1007 					    u32 new_sta_mask)
1008 {
1009 	int ret;
1010 
1011 	ret = iwl_mvm_mld_update_sta_queues(mvm, sta,
1012 					    old_sta_mask,
1013 					    new_sta_mask);
1014 	if (ret)
1015 		return ret;
1016 
1017 	ret = iwl_mvm_mld_update_sta_keys(mvm, vif, sta,
1018 					  old_sta_mask,
1019 					  new_sta_mask);
1020 	if (ret)
1021 		return ret;
1022 
1023 	return iwl_mvm_mld_update_sta_baids(mvm, old_sta_mask, new_sta_mask);
1024 }
1025 
1026 int iwl_mvm_mld_update_sta_links(struct iwl_mvm *mvm,
1027 				 struct ieee80211_vif *vif,
1028 				 struct ieee80211_sta *sta,
1029 				 u16 old_links, u16 new_links)
1030 {
1031 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1032 	struct iwl_mvm_vif *mvm_vif = iwl_mvm_vif_from_mac80211(vif);
1033 	struct iwl_mvm_link_sta *mvm_sta_link;
1034 	struct iwl_mvm_vif_link_info *mvm_vif_link;
1035 	unsigned long links_to_add = ~old_links & new_links;
1036 	unsigned long links_to_rem = old_links & ~new_links;
1037 	unsigned long old_links_long = old_links;
1038 	u32 current_sta_mask = 0, sta_mask_added = 0, sta_mask_to_rem = 0;
1039 	unsigned long link_sta_added_to_fw = 0, link_sta_allocated = 0;
1040 	unsigned int link_id;
1041 	int ret;
1042 
1043 	lockdep_assert_held(&mvm->mutex);
1044 
1045 	for_each_set_bit(link_id, &old_links_long,
1046 			 IEEE80211_MLD_MAX_NUM_LINKS) {
1047 		mvm_sta_link =
1048 			rcu_dereference_protected(mvm_sta->link[link_id],
1049 						  lockdep_is_held(&mvm->mutex));
1050 
1051 		if (WARN_ON(!mvm_sta_link)) {
1052 			ret = -EINVAL;
1053 			goto err;
1054 		}
1055 
1056 		current_sta_mask |= BIT(mvm_sta_link->sta_id);
1057 		if (links_to_rem & BIT(link_id))
1058 			sta_mask_to_rem |= BIT(mvm_sta_link->sta_id);
1059 	}
1060 
1061 	if (sta_mask_to_rem) {
1062 		ret = iwl_mvm_mld_update_sta_resources(mvm, vif, sta,
1063 						       current_sta_mask,
1064 						       current_sta_mask &
1065 							~sta_mask_to_rem);
1066 		if (WARN_ON(ret))
1067 			goto err;
1068 
1069 		current_sta_mask &= ~sta_mask_to_rem;
1070 	}
1071 
1072 	for_each_set_bit(link_id, &links_to_rem, IEEE80211_MLD_MAX_NUM_LINKS) {
1073 		mvm_sta_link =
1074 			rcu_dereference_protected(mvm_sta->link[link_id],
1075 						  lockdep_is_held(&mvm->mutex));
1076 		mvm_vif_link = mvm_vif->link[link_id];
1077 
1078 		if (WARN_ON(!mvm_sta_link || !mvm_vif_link)) {
1079 			ret = -EINVAL;
1080 			goto err;
1081 		}
1082 
1083 		ret = iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_sta_link->sta_id);
1084 		if (WARN_ON(ret))
1085 			goto err;
1086 
1087 		if (vif->type == NL80211_IFTYPE_STATION)
1088 			mvm_vif_link->ap_sta_id = IWL_MVM_INVALID_STA;
1089 
1090 		iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_sta_link, link_id,
1091 					  false);
1092 	}
1093 
1094 	for_each_set_bit(link_id, &links_to_add, IEEE80211_MLD_MAX_NUM_LINKS) {
1095 		struct ieee80211_bss_conf *link_conf =
1096 			link_conf_dereference_protected(vif, link_id);
1097 		struct ieee80211_link_sta *link_sta =
1098 			link_sta_dereference_protected(sta, link_id);
1099 		mvm_vif_link = mvm_vif->link[link_id];
1100 
1101 		if (WARN_ON(!mvm_vif_link || !link_conf || !link_sta ||
1102 			    mvm_sta->link[link_id])) {
1103 			ret = -EINVAL;
1104 			goto err;
1105 		}
1106 
1107 		ret = iwl_mvm_mld_alloc_sta_link(mvm, vif, sta, link_id);
1108 		if (WARN_ON(ret))
1109 			goto err;
1110 
1111 		link_sta->agg.max_rc_amsdu_len = 1;
1112 		ieee80211_sta_recalc_aggregates(sta);
1113 
1114 		mvm_sta_link =
1115 			rcu_dereference_protected(mvm_sta->link[link_id],
1116 						  lockdep_is_held(&mvm->mutex));
1117 
1118 		if (WARN_ON(!mvm_sta_link)) {
1119 			ret = -EINVAL;
1120 			goto err;
1121 		}
1122 
1123 		if (vif->type == NL80211_IFTYPE_STATION)
1124 			iwl_mvm_mld_set_ap_sta_id(sta, mvm_vif_link,
1125 						  mvm_sta_link);
1126 
1127 		link_sta_allocated |= BIT(link_id);
1128 
1129 		sta_mask_added |= BIT(mvm_sta_link->sta_id);
1130 
1131 		ret = iwl_mvm_mld_cfg_sta(mvm, sta, vif, link_sta, link_conf,
1132 					  mvm_sta_link);
1133 		if (WARN_ON(ret))
1134 			goto err;
1135 
1136 		link_sta_added_to_fw |= BIT(link_id);
1137 
1138 		iwl_mvm_rs_add_sta_link(mvm, mvm_sta_link);
1139 	}
1140 
1141 	if (sta_mask_added) {
1142 		ret = iwl_mvm_mld_update_sta_resources(mvm, vif, sta,
1143 						       current_sta_mask,
1144 						       current_sta_mask |
1145 							sta_mask_added);
1146 		if (WARN_ON(ret))
1147 			goto err;
1148 	}
1149 
1150 	return 0;
1151 
1152 err:
1153 	/* remove all already allocated stations in FW */
1154 	for_each_set_bit(link_id, &link_sta_added_to_fw,
1155 			 IEEE80211_MLD_MAX_NUM_LINKS) {
1156 		mvm_sta_link =
1157 			rcu_dereference_protected(mvm_sta->link[link_id],
1158 						  lockdep_is_held(&mvm->mutex));
1159 
1160 		iwl_mvm_mld_rm_sta_from_fw(mvm, mvm_sta_link->sta_id);
1161 	}
1162 
1163 	/* remove all already allocated station links in driver */
1164 	for_each_set_bit(link_id, &link_sta_allocated,
1165 			 IEEE80211_MLD_MAX_NUM_LINKS) {
1166 		mvm_sta_link =
1167 			rcu_dereference_protected(mvm_sta->link[link_id],
1168 						  lockdep_is_held(&mvm->mutex));
1169 
1170 		iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_sta_link, link_id,
1171 					  false);
1172 	}
1173 
1174 	return ret;
1175 }
1176