xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mld-mac80211.c (revision 001821b0e79716c4e17c71d8e053a23599a7a508)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2022-2024 Intel Corporation
4  */
5 #include "mvm.h"
6 
7 static int iwl_mvm_mld_mac_add_interface(struct ieee80211_hw *hw,
8 					 struct ieee80211_vif *vif)
9 {
10 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
11 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
12 	int ret;
13 	int i;
14 
15 	mutex_lock(&mvm->mutex);
16 
17 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
18 
19 	mvmvif->mvm = mvm;
20 
21 	/* Not much to do here. The stack will not allow interface
22 	 * types or combinations that we didn't advertise, so we
23 	 * don't really have to check the types.
24 	 */
25 
26 	/* make sure that beacon statistics don't go backwards with FW reset */
27 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
28 		for_each_mvm_vif_valid_link(mvmvif, i)
29 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
30 				mvmvif->link[i]->beacon_stats.num_beacons;
31 
32 	/* Allocate resources for the MAC context, and add it to the fw  */
33 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
34 	if (ret)
35 		goto out_unlock;
36 
37 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
38 
39 	mvmvif->features |= hw->netdev_features;
40 
41 	/* reset deflink MLO parameters */
42 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
43 	mvmvif->deflink.active = 0;
44 	/* the first link always points to the default one */
45 	mvmvif->link[0] = &mvmvif->deflink;
46 
47 	ret = iwl_mvm_mld_mac_ctxt_add(mvm, vif);
48 	if (ret)
49 		goto out_unlock;
50 
51 	/* beacon filtering */
52 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
53 	if (ret)
54 		goto out_remove_mac;
55 
56 	if (!mvm->bf_allowed_vif &&
57 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
58 		mvm->bf_allowed_vif = mvmvif;
59 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
60 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
61 	}
62 
63 	ret = iwl_mvm_add_link(mvm, vif, &vif->bss_conf);
64 	if (ret)
65 		goto out_free_bf;
66 
67 	/* Save a pointer to p2p device vif, so it can later be used to
68 	 * update the p2p device MAC when a GO is started/stopped
69 	 */
70 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
71 		mvm->p2p_device_vif = vif;
72 
73 	ret = iwl_mvm_power_update_mac(mvm);
74 	if (ret)
75 		goto out_free_bf;
76 
77 	iwl_mvm_tcm_add_vif(mvm, vif);
78 	INIT_DELAYED_WORK(&mvmvif->csa_work,
79 			  iwl_mvm_channel_switch_disconnect_wk);
80 
81 	if (vif->type == NL80211_IFTYPE_MONITOR) {
82 		mvm->monitor_on = true;
83 		ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
84 	}
85 
86 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
87 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
88 
89 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
90 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
91 	    !mvm->csme_vif && mvm->mei_registered) {
92 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
93 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
94 		mvm->csme_vif = vif;
95 	}
96 
97 	if (vif->p2p || iwl_fw_lookup_cmd_ver(mvm->fw, PHY_CONTEXT_CMD, 1) < 5)
98 		vif->driver_flags |= IEEE80211_VIF_IGNORE_OFDMA_WIDER_BW;
99 
100 	goto out_unlock;
101 
102  out_free_bf:
103 	if (mvm->bf_allowed_vif == mvmvif) {
104 		mvm->bf_allowed_vif = NULL;
105 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
106 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
107 	}
108  out_remove_mac:
109 	mvmvif->link[0] = NULL;
110 	iwl_mvm_mld_mac_ctxt_remove(mvm, vif);
111  out_unlock:
112 	mutex_unlock(&mvm->mutex);
113 
114 	return ret;
115 }
116 
117 static void iwl_mvm_mld_mac_remove_interface(struct ieee80211_hw *hw,
118 					     struct ieee80211_vif *vif)
119 {
120 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
121 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
122 	struct iwl_probe_resp_data *probe_data;
123 
124 	iwl_mvm_prepare_mac_removal(mvm, vif);
125 
126 	if (!(vif->type == NL80211_IFTYPE_AP ||
127 	      vif->type == NL80211_IFTYPE_ADHOC))
128 		iwl_mvm_tcm_rm_vif(mvm, vif);
129 
130 	mutex_lock(&mvm->mutex);
131 
132 	if (vif == mvm->csme_vif) {
133 		iwl_mei_set_netdev(NULL);
134 		mvm->csme_vif = NULL;
135 	}
136 
137 	if (mvm->bf_allowed_vif == mvmvif) {
138 		mvm->bf_allowed_vif = NULL;
139 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
140 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
141 	}
142 
143 	if (vif->bss_conf.ftm_responder)
144 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
145 
146 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
147 
148 	/* For AP/GO interface, the tear down of the resources allocated to the
149 	 * interface is be handled as part of the stop_ap flow.
150 	 */
151 	if (vif->type == NL80211_IFTYPE_AP ||
152 	    vif->type == NL80211_IFTYPE_ADHOC) {
153 #ifdef CONFIG_NL80211_TESTMODE
154 		if (vif == mvm->noa_vif) {
155 			mvm->noa_vif = NULL;
156 			mvm->noa_duration = 0;
157 		}
158 #endif
159 	}
160 
161 	iwl_mvm_power_update_mac(mvm);
162 
163 	/* Before the interface removal, mac80211 would cancel the ROC, and the
164 	 * ROC worker would be scheduled if needed. The worker would be flushed
165 	 * in iwl_mvm_prepare_mac_removal() and thus at this point the link is
166 	 * not active. So need only to remove the link.
167 	 */
168 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
169 		if (mvmvif->deflink.phy_ctxt) {
170 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
171 			mvmvif->deflink.phy_ctxt = NULL;
172 		}
173 		mvm->p2p_device_vif = NULL;
174 		iwl_mvm_remove_link(mvm, vif, &vif->bss_conf);
175 	} else {
176 		iwl_mvm_disable_link(mvm, vif, &vif->bss_conf);
177 	}
178 
179 	iwl_mvm_mld_mac_ctxt_remove(mvm, vif);
180 
181 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
182 
183 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
184 					       lockdep_is_held(&mvm->mutex));
185 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
186 	if (probe_data)
187 		kfree_rcu(probe_data, rcu_head);
188 
189 	if (vif->type == NL80211_IFTYPE_MONITOR) {
190 		mvm->monitor_on = false;
191 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
192 	}
193 
194 	mutex_unlock(&mvm->mutex);
195 }
196 
197 static unsigned int iwl_mvm_mld_count_active_links(struct iwl_mvm_vif *mvmvif)
198 {
199 	unsigned int n_active = 0;
200 	int i;
201 
202 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
203 		if (mvmvif->link[i] && mvmvif->link[i]->phy_ctxt)
204 			n_active++;
205 	}
206 
207 	return n_active;
208 }
209 
210 static void iwl_mvm_restart_mpdu_count(struct iwl_mvm *mvm,
211 				       struct iwl_mvm_vif *mvmvif)
212 {
213 	struct ieee80211_sta *ap_sta = mvmvif->ap_sta;
214 	struct iwl_mvm_sta *mvmsta;
215 
216 	lockdep_assert_held(&mvm->mutex);
217 
218 	if (!ap_sta)
219 		return;
220 
221 	mvmsta = iwl_mvm_sta_from_mac80211(ap_sta);
222 	if (!mvmsta->mpdu_counters)
223 		return;
224 
225 	for (int q = 0; q < mvm->trans->num_rx_queues; q++) {
226 		spin_lock_bh(&mvmsta->mpdu_counters[q].lock);
227 		memset(mvmsta->mpdu_counters[q].per_link, 0,
228 		       sizeof(mvmsta->mpdu_counters[q].per_link));
229 		mvmsta->mpdu_counters[q].window_start = jiffies;
230 		spin_unlock_bh(&mvmsta->mpdu_counters[q].lock);
231 	}
232 }
233 
234 static int iwl_mvm_esr_mode_active(struct iwl_mvm *mvm,
235 				   struct ieee80211_vif *vif)
236 {
237 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
238 	int link_id, ret = 0;
239 
240 	mvmvif->esr_active = true;
241 
242 	/* Indicate to mac80211 that EML is enabled */
243 	vif->driver_flags |= IEEE80211_VIF_EML_ACTIVE;
244 
245 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_FW,
246 					    IEEE80211_SMPS_OFF);
247 
248 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
249 		struct iwl_mvm_vif_link_info *link = mvmvif->link[link_id];
250 
251 		if (!link->phy_ctxt)
252 			continue;
253 
254 		ret = iwl_mvm_phy_send_rlc(mvm, link->phy_ctxt, 2, 2);
255 		if (ret)
256 			break;
257 
258 		link->phy_ctxt->rlc_disabled = true;
259 	}
260 
261 	if (vif->active_links == mvmvif->link_selection_res &&
262 	    !WARN_ON(!(vif->active_links & BIT(mvmvif->link_selection_primary))))
263 		mvmvif->primary_link = mvmvif->link_selection_primary;
264 	else
265 		mvmvif->primary_link = __ffs(vif->active_links);
266 
267 	/* Needed for tracking RSSI */
268 	iwl_mvm_request_periodic_system_statistics(mvm, true);
269 
270 	/*
271 	 * Restart the MPDU counters and the counting window, so when the
272 	 * statistics arrive (which is where we look at the counters) we
273 	 * will be at the end of the window.
274 	 */
275 	iwl_mvm_restart_mpdu_count(mvm, mvmvif);
276 
277 	return ret;
278 }
279 
280 static int
281 __iwl_mvm_mld_assign_vif_chanctx(struct iwl_mvm *mvm,
282 				 struct ieee80211_vif *vif,
283 				 struct ieee80211_bss_conf *link_conf,
284 				 struct ieee80211_chanctx_conf *ctx,
285 				 bool switching_chanctx)
286 {
287 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
288 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
289 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
290 	unsigned int n_active = iwl_mvm_mld_count_active_links(mvmvif);
291 	unsigned int link_id = link_conf->link_id;
292 	int ret;
293 
294 	if (WARN_ON_ONCE(!mvmvif->link[link_id]))
295 		return -EINVAL;
296 
297 	/* if the assigned one was not counted yet, count it now */
298 	if (!mvmvif->link[link_id]->phy_ctxt)
299 		n_active++;
300 
301 	/* mac parameters such as HE support can change at this stage
302 	 * For sta, need first to configure correct state from drv_sta_state
303 	 * and only after that update mac config.
304 	 */
305 	if (vif->type == NL80211_IFTYPE_AP) {
306 		ret = iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
307 		if (ret) {
308 			IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
309 			return -EINVAL;
310 		}
311 	}
312 
313 	mvmvif->link[link_id]->phy_ctxt = phy_ctxt;
314 
315 	if (iwl_mvm_is_esr_supported(mvm->fwrt.trans) && n_active > 1) {
316 		mvmvif->link[link_id]->listen_lmac = true;
317 		ret = iwl_mvm_esr_mode_active(mvm, vif);
318 		if (ret) {
319 			IWL_ERR(mvm, "failed to activate ESR mode (%d)\n", ret);
320 			iwl_mvm_request_periodic_system_statistics(mvm, false);
321 			goto out;
322 		}
323 	}
324 
325 	if (switching_chanctx) {
326 		/* reactivate if we turned this off during channel switch */
327 		if (vif->type == NL80211_IFTYPE_AP)
328 			mvmvif->ap_ibss_active = true;
329 	}
330 
331 	/* send it first with phy context ID */
332 	ret = iwl_mvm_link_changed(mvm, vif, link_conf, 0, false);
333 	if (ret)
334 		goto out;
335 
336 	/* Initialize rate control for the AP station, since we might be
337 	 * doing a link switch here - we cannot initialize it before since
338 	 * this needs the phy context assigned (and in FW?), and we cannot
339 	 * do it later because it needs to be initialized as soon as we're
340 	 * able to TX on the link, i.e. when active.
341 	 */
342 	if (mvmvif->ap_sta) {
343 		struct ieee80211_link_sta *link_sta;
344 
345 		rcu_read_lock();
346 		link_sta = rcu_dereference(mvmvif->ap_sta->link[link_id]);
347 
348 		if (!WARN_ON_ONCE(!link_sta))
349 			iwl_mvm_rs_rate_init(mvm, vif, mvmvif->ap_sta,
350 					     link_conf, link_sta,
351 					     phy_ctxt->channel->band);
352 		rcu_read_unlock();
353 	}
354 
355 	/* then activate */
356 	ret = iwl_mvm_link_changed(mvm, vif, link_conf,
357 				   LINK_CONTEXT_MODIFY_ACTIVE |
358 				   LINK_CONTEXT_MODIFY_RATES_INFO,
359 				   true);
360 	if (ret)
361 		goto out;
362 
363 	/*
364 	 * Power state must be updated before quotas,
365 	 * otherwise fw will complain.
366 	 */
367 	iwl_mvm_power_update_mac(mvm);
368 
369 	if (vif->type == NL80211_IFTYPE_MONITOR) {
370 		ret = iwl_mvm_mld_add_snif_sta(mvm, vif, link_conf);
371 		if (ret)
372 			goto deactivate;
373 	}
374 
375 	return 0;
376 
377 deactivate:
378 	iwl_mvm_link_changed(mvm, vif, link_conf, LINK_CONTEXT_MODIFY_ACTIVE,
379 			     false);
380 out:
381 	mvmvif->link[link_id]->phy_ctxt = NULL;
382 	iwl_mvm_power_update_mac(mvm);
383 	return ret;
384 }
385 
386 static int iwl_mvm_mld_assign_vif_chanctx(struct ieee80211_hw *hw,
387 					  struct ieee80211_vif *vif,
388 					  struct ieee80211_bss_conf *link_conf,
389 					  struct ieee80211_chanctx_conf *ctx)
390 {
391 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
392 	int ret;
393 
394 	/* update EMLSR mode */
395 	if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION) {
396 		ret = iwl_mvm_esr_non_bss_link(mvm, vif, link_conf->link_id,
397 					       true);
398 		/*
399 		 * Don't activate this link if failed to exit EMLSR in
400 		 * the BSS interface
401 		 */
402 		if (ret)
403 			return ret;
404 	}
405 
406 	mutex_lock(&mvm->mutex);
407 	ret = __iwl_mvm_mld_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
408 	mutex_unlock(&mvm->mutex);
409 
410 	return ret;
411 }
412 
413 static int iwl_mvm_esr_mode_inactive(struct iwl_mvm *mvm,
414 				     struct ieee80211_vif *vif)
415 {
416 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
417 	struct ieee80211_bss_conf *link_conf;
418 	int link_id, ret = 0;
419 
420 	mvmvif->esr_active = false;
421 
422 	vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
423 
424 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_FW,
425 					    IEEE80211_SMPS_AUTOMATIC);
426 
427 	for_each_vif_active_link(vif, link_conf, link_id) {
428 		struct ieee80211_chanctx_conf *chanctx_conf;
429 		struct iwl_mvm_phy_ctxt *phy_ctxt;
430 		u8 static_chains, dynamic_chains;
431 
432 		mvmvif->link[link_id]->listen_lmac = false;
433 
434 		rcu_read_lock();
435 
436 		chanctx_conf = rcu_dereference(link_conf->chanctx_conf);
437 		phy_ctxt = mvmvif->link[link_id]->phy_ctxt;
438 
439 		if (!chanctx_conf || !phy_ctxt) {
440 			rcu_read_unlock();
441 			continue;
442 		}
443 
444 		phy_ctxt->rlc_disabled = false;
445 		static_chains = chanctx_conf->rx_chains_static;
446 		dynamic_chains = chanctx_conf->rx_chains_dynamic;
447 
448 		rcu_read_unlock();
449 
450 		ret = iwl_mvm_phy_send_rlc(mvm, phy_ctxt, static_chains,
451 					   dynamic_chains);
452 		if (ret)
453 			break;
454 	}
455 
456 	iwl_mvm_request_periodic_system_statistics(mvm, false);
457 
458 	/* Start a new counting window */
459 	iwl_mvm_restart_mpdu_count(mvm, mvmvif);
460 
461 	return ret;
462 }
463 
464 static void
465 __iwl_mvm_mld_unassign_vif_chanctx(struct iwl_mvm *mvm,
466 				   struct ieee80211_vif *vif,
467 				   struct ieee80211_bss_conf *link_conf,
468 				   struct ieee80211_chanctx_conf *ctx,
469 				   bool switching_chanctx)
470 
471 {
472 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
473 	unsigned int n_active = iwl_mvm_mld_count_active_links(mvmvif);
474 	unsigned int link_id = link_conf->link_id;
475 
476 	/* shouldn't happen, but verify link_id is valid before accessing */
477 	if (WARN_ON_ONCE(!mvmvif->link[link_id]))
478 		return;
479 
480 	if (vif->type == NL80211_IFTYPE_AP && switching_chanctx) {
481 		mvmvif->csa_countdown = false;
482 
483 		/* Set CS bit on all the stations */
484 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
485 
486 		/* Save blocked iface, the timeout is set on the next beacon */
487 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
488 
489 		mvmvif->ap_ibss_active = false;
490 	}
491 
492 	iwl_mvm_link_changed(mvm, vif, link_conf,
493 			     LINK_CONTEXT_MODIFY_ACTIVE, false);
494 
495 	if (iwl_mvm_is_esr_supported(mvm->fwrt.trans) && n_active > 1) {
496 		int ret = iwl_mvm_esr_mode_inactive(mvm, vif);
497 
498 		if (ret)
499 			IWL_ERR(mvm, "failed to deactivate ESR mode (%d)\n",
500 				ret);
501 	}
502 
503 	if (vif->type == NL80211_IFTYPE_MONITOR)
504 		iwl_mvm_mld_rm_snif_sta(mvm, vif);
505 
506 	if (switching_chanctx)
507 		return;
508 	mvmvif->link[link_id]->phy_ctxt = NULL;
509 	iwl_mvm_power_update_mac(mvm);
510 }
511 
512 static void iwl_mvm_mld_unassign_vif_chanctx(struct ieee80211_hw *hw,
513 					     struct ieee80211_vif *vif,
514 					     struct ieee80211_bss_conf *link_conf,
515 					     struct ieee80211_chanctx_conf *ctx)
516 {
517 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
518 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
519 
520 	mutex_lock(&mvm->mutex);
521 	__iwl_mvm_mld_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
522 	/* in the non-MLD case, remove/re-add the link to clean up FW state */
523 	if (!ieee80211_vif_is_mld(vif) && !mvmvif->ap_sta &&
524 	    !WARN_ON_ONCE(vif->cfg.assoc)) {
525 		iwl_mvm_remove_link(mvm, vif, link_conf);
526 		iwl_mvm_add_link(mvm, vif, link_conf);
527 	}
528 	mutex_unlock(&mvm->mutex);
529 
530 	/* update EMLSR mode */
531 	if (ieee80211_vif_type_p2p(vif) != NL80211_IFTYPE_STATION)
532 		iwl_mvm_esr_non_bss_link(mvm, vif, link_conf->link_id, false);
533 }
534 
535 static void
536 iwl_mvm_send_ap_tx_power_constraint_cmd(struct iwl_mvm *mvm,
537 					struct ieee80211_vif *vif,
538 					struct ieee80211_bss_conf *bss_conf)
539 {
540 	struct iwl_txpower_constraints_cmd cmd = {};
541 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
542 	struct iwl_mvm_vif_link_info *link_info =
543 			mvmvif->link[bss_conf->link_id];
544 	u32 cmd_id = WIDE_ID(PHY_OPS_GROUP, AP_TX_POWER_CONSTRAINTS_CMD);
545 	u32 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
546 					    IWL_FW_CMD_VER_UNKNOWN);
547 	int ret;
548 
549 	lockdep_assert_held(&mvm->mutex);
550 
551 	if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
552 		return;
553 
554 	if (!link_info->active ||
555 	    link_info->fw_link_id == IWL_MVM_FW_LINK_ID_INVALID)
556 		return;
557 
558 	if (bss_conf->chanreq.oper.chan->band != NL80211_BAND_6GHZ ||
559 	    bss_conf->chanreq.oper.chan->flags &
560 		    IEEE80211_CHAN_NO_6GHZ_VLP_CLIENT)
561 		return;
562 
563 	cmd.link_id = cpu_to_le16(link_info->fw_link_id);
564 	/*
565 	 * Currently supporting VLP Soft AP only.
566 	 */
567 	cmd.ap_type = cpu_to_le16(IWL_6GHZ_AP_TYPE_VLP);
568 	memset(cmd.psd_pwr, DEFAULT_TPE_TX_POWER, sizeof(cmd.psd_pwr));
569 	memset(cmd.eirp_pwr, DEFAULT_TPE_TX_POWER, sizeof(cmd.eirp_pwr));
570 
571 	ret = iwl_mvm_send_cmd_pdu(mvm,
572 				   WIDE_ID(PHY_OPS_GROUP,
573 					   AP_TX_POWER_CONSTRAINTS_CMD),
574 				   0, sizeof(cmd), &cmd);
575 	if (ret)
576 		IWL_ERR(mvm,
577 			"failed to send AP_TX_POWER_CONSTRAINTS_CMD (%d)\n",
578 			ret);
579 }
580 
581 static int iwl_mvm_mld_start_ap_ibss(struct ieee80211_hw *hw,
582 				     struct ieee80211_vif *vif,
583 				     struct ieee80211_bss_conf *link_conf)
584 {
585 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
586 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
587 	int ret;
588 
589 	mutex_lock(&mvm->mutex);
590 
591 	if (vif->type == NL80211_IFTYPE_AP)
592 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif, link_conf);
593 
594 	/* Send the beacon template */
595 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
596 	if (ret)
597 		goto out_unlock;
598 
599 	/* the link should be already activated when assigning chan context */
600 	ret = iwl_mvm_link_changed(mvm, vif, link_conf,
601 				   LINK_CONTEXT_MODIFY_ALL &
602 				   ~LINK_CONTEXT_MODIFY_ACTIVE,
603 				   true);
604 	if (ret)
605 		goto out_unlock;
606 
607 	ret = iwl_mvm_mld_add_mcast_sta(mvm, vif, link_conf);
608 	if (ret)
609 		goto out_unlock;
610 
611 	/* Send the bcast station. At this stage the TBTT and DTIM time
612 	 * events are added and applied to the scheduler
613 	 */
614 	ret = iwl_mvm_mld_add_bcast_sta(mvm, vif, link_conf);
615 	if (ret)
616 		goto out_rm_mcast;
617 
618 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
619 		goto out_failed;
620 
621 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
622 	if (vif->p2p && mvm->p2p_device_vif)
623 		iwl_mvm_mld_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
624 
625 	iwl_mvm_bt_coex_vif_change(mvm);
626 
627 	/* we don't support TDLS during DCM */
628 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
629 		iwl_mvm_teardown_tdls_peers(mvm);
630 
631 	iwl_mvm_ftm_restart_responder(mvm, vif, link_conf);
632 
633 	goto out_unlock;
634 
635 out_failed:
636 	iwl_mvm_power_update_mac(mvm);
637 	mvmvif->ap_ibss_active = false;
638 	iwl_mvm_mld_rm_bcast_sta(mvm, vif, link_conf);
639 out_rm_mcast:
640 	iwl_mvm_mld_rm_mcast_sta(mvm, vif, link_conf);
641 out_unlock:
642 	mutex_unlock(&mvm->mutex);
643 	return ret;
644 }
645 
646 static int iwl_mvm_mld_start_ap(struct ieee80211_hw *hw,
647 				struct ieee80211_vif *vif,
648 				struct ieee80211_bss_conf *link_conf)
649 {
650 	return iwl_mvm_mld_start_ap_ibss(hw, vif, link_conf);
651 }
652 
653 static int iwl_mvm_mld_start_ibss(struct ieee80211_hw *hw,
654 				  struct ieee80211_vif *vif)
655 {
656 	return iwl_mvm_mld_start_ap_ibss(hw, vif, &vif->bss_conf);
657 }
658 
659 static void iwl_mvm_mld_stop_ap_ibss(struct ieee80211_hw *hw,
660 				     struct ieee80211_vif *vif,
661 				     struct ieee80211_bss_conf *link_conf)
662 {
663 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
664 
665 	mutex_lock(&mvm->mutex);
666 
667 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
668 
669 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
670 	if (vif->p2p && mvm->p2p_device_vif)
671 		iwl_mvm_mld_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
672 
673 	iwl_mvm_ftm_responder_clear(mvm, vif);
674 
675 	iwl_mvm_mld_rm_bcast_sta(mvm, vif, link_conf);
676 	iwl_mvm_mld_rm_mcast_sta(mvm, vif, link_conf);
677 
678 	iwl_mvm_power_update_mac(mvm);
679 	mutex_unlock(&mvm->mutex);
680 }
681 
682 static void iwl_mvm_mld_stop_ap(struct ieee80211_hw *hw,
683 				struct ieee80211_vif *vif,
684 				struct ieee80211_bss_conf *link_conf)
685 {
686 	iwl_mvm_mld_stop_ap_ibss(hw, vif, link_conf);
687 }
688 
689 static void iwl_mvm_mld_stop_ibss(struct ieee80211_hw *hw,
690 				  struct ieee80211_vif *vif)
691 {
692 	iwl_mvm_mld_stop_ap_ibss(hw, vif, &vif->bss_conf);
693 }
694 
695 static int iwl_mvm_mld_mac_sta_state(struct ieee80211_hw *hw,
696 				     struct ieee80211_vif *vif,
697 				     struct ieee80211_sta *sta,
698 				     enum ieee80211_sta_state old_state,
699 				     enum ieee80211_sta_state new_state)
700 {
701 	static const struct iwl_mvm_sta_state_ops callbacks = {
702 		.add_sta = iwl_mvm_mld_add_sta,
703 		.update_sta = iwl_mvm_mld_update_sta,
704 		.rm_sta = iwl_mvm_mld_rm_sta,
705 		.mac_ctxt_changed = iwl_mvm_mld_mac_ctxt_changed,
706 	};
707 
708 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
709 					    &callbacks);
710 }
711 
712 static bool iwl_mvm_esr_bw_criteria(struct iwl_mvm *mvm,
713 				    struct ieee80211_vif *vif,
714 				    struct ieee80211_bss_conf *link_conf)
715 {
716 	struct ieee80211_bss_conf *other_link;
717 	int link_id;
718 
719 	/* Exit EMLSR if links don't have equal bandwidths */
720 	for_each_vif_active_link(vif, other_link, link_id) {
721 		if (link_id == link_conf->link_id)
722 			continue;
723 		if (link_conf->chanreq.oper.width ==
724 		    other_link->chanreq.oper.width)
725 			return true;
726 	}
727 
728 	return false;
729 }
730 
731 static void
732 iwl_mvm_mld_link_info_changed_station(struct iwl_mvm *mvm,
733 				      struct ieee80211_vif *vif,
734 				      struct ieee80211_bss_conf *link_conf,
735 				      u64 changes)
736 {
737 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
738 	bool has_he, has_eht;
739 	u32 link_changes = 0;
740 	int ret;
741 
742 	if (WARN_ON_ONCE(!mvmvif->link[link_conf->link_id]))
743 		return;
744 
745 	has_he = link_conf->he_support && !iwlwifi_mod_params.disable_11ax;
746 	has_eht = link_conf->eht_support && !iwlwifi_mod_params.disable_11be;
747 
748 	/* Update EDCA params */
749 	if (changes & BSS_CHANGED_QOS && vif->cfg.assoc && link_conf->qos)
750 		link_changes |= LINK_CONTEXT_MODIFY_QOS_PARAMS;
751 
752 	if (changes & BSS_CHANGED_ERP_SLOT)
753 		link_changes |= LINK_CONTEXT_MODIFY_RATES_INFO;
754 
755 	if (vif->cfg.assoc && (has_he || has_eht)) {
756 		IWL_DEBUG_MAC80211(mvm, "Associated in HE mode\n");
757 		link_changes |= LINK_CONTEXT_MODIFY_HE_PARAMS;
758 	}
759 
760 	if ((changes & BSS_CHANGED_BANDWIDTH) &&
761 	    ieee80211_vif_link_active(vif, link_conf->link_id) &&
762 	    mvmvif->esr_active &&
763 	    !iwl_mvm_esr_bw_criteria(mvm, vif, link_conf))
764 		iwl_mvm_exit_esr(mvm, vif,
765 				 IWL_MVM_ESR_EXIT_BANDWIDTH,
766 				 iwl_mvm_get_primary_link(vif));
767 
768 	/* if associated, maybe puncturing changed - we'll check later */
769 	if (vif->cfg.assoc)
770 		link_changes |= LINK_CONTEXT_MODIFY_EHT_PARAMS;
771 
772 	if (link_changes) {
773 		ret = iwl_mvm_link_changed(mvm, vif, link_conf, link_changes,
774 					   true);
775 		if (ret)
776 			IWL_ERR(mvm, "failed to update link\n");
777 	}
778 
779 	ret = iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
780 	if (ret)
781 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
782 
783 	memcpy(mvmvif->link[link_conf->link_id]->bssid, link_conf->bssid,
784 	       ETH_ALEN);
785 
786 	iwl_mvm_bss_info_changed_station_common(mvm, vif, link_conf, changes);
787 }
788 
789 static bool iwl_mvm_mld_vif_have_valid_ap_sta(struct iwl_mvm_vif *mvmvif)
790 {
791 	int i;
792 
793 	for_each_mvm_vif_valid_link(mvmvif, i) {
794 		if (mvmvif->link[i]->ap_sta_id != IWL_MVM_INVALID_STA)
795 			return true;
796 	}
797 
798 	return false;
799 }
800 
801 static void iwl_mvm_mld_vif_delete_all_stas(struct iwl_mvm *mvm,
802 					    struct ieee80211_vif *vif)
803 {
804 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
805 	int i, ret;
806 
807 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
808 		return;
809 
810 	for_each_mvm_vif_valid_link(mvmvif, i) {
811 		struct iwl_mvm_vif_link_info *link = mvmvif->link[i];
812 
813 		if (!link)
814 			continue;
815 
816 		iwl_mvm_sec_key_remove_ap(mvm, vif, link, i);
817 		ret = iwl_mvm_mld_rm_sta_id(mvm, link->ap_sta_id);
818 		if (ret)
819 			IWL_ERR(mvm, "failed to remove AP station\n");
820 
821 		link->ap_sta_id = IWL_MVM_INVALID_STA;
822 	}
823 }
824 
825 static void iwl_mvm_mld_vif_cfg_changed_station(struct iwl_mvm *mvm,
826 						struct ieee80211_vif *vif,
827 						u64 changes)
828 {
829 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
830 	struct ieee80211_bss_conf *link_conf;
831 	bool protect = false;
832 	unsigned int i;
833 	int ret;
834 
835 	/* This might get called without active links during the
836 	 * chanctx switch, but we don't care about it anyway.
837 	 */
838 	if (changes == BSS_CHANGED_IDLE)
839 		return;
840 
841 	ret = iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
842 	if (ret)
843 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
844 
845 	mvmvif->associated = vif->cfg.assoc;
846 
847 	if (changes & BSS_CHANGED_ASSOC) {
848 		if (vif->cfg.assoc) {
849 			/* clear statistics to get clean beacon counter */
850 			iwl_mvm_request_statistics(mvm, true);
851 			iwl_mvm_sf_update(mvm, vif, false);
852 			iwl_mvm_power_vif_assoc(mvm, vif);
853 
854 			for_each_mvm_vif_valid_link(mvmvif, i) {
855 				memset(&mvmvif->link[i]->beacon_stats, 0,
856 				       sizeof(mvmvif->link[i]->beacon_stats));
857 
858 				if (vif->p2p) {
859 					iwl_mvm_update_smps(mvm, vif,
860 							    IWL_MVM_SMPS_REQ_PROT,
861 							    IEEE80211_SMPS_DYNAMIC, i);
862 				}
863 
864 				rcu_read_lock();
865 				link_conf = rcu_dereference(vif->link_conf[i]);
866 				if (link_conf && !link_conf->dtim_period)
867 					protect = true;
868 				rcu_read_unlock();
869 			}
870 
871 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
872 			    protect) {
873 				/* We are in assoc so only one link is active-
874 				 * The association link
875 				 */
876 				unsigned int link_id =
877 					ffs(vif->active_links) - 1;
878 
879 				/* If we're not restarting and still haven't
880 				 * heard a beacon (dtim period unknown) then
881 				 * make sure we still have enough minimum time
882 				 * remaining in the time event, since the auth
883 				 * might actually have taken quite a while
884 				 * (especially for SAE) and so the remaining
885 				 * time could be small without us having heard
886 				 * a beacon yet.
887 				 */
888 				iwl_mvm_protect_assoc(mvm, vif, 0, link_id);
889 			}
890 
891 			iwl_mvm_sf_update(mvm, vif, false);
892 
893 			/* FIXME: need to decide about misbehaving AP handling */
894 			iwl_mvm_power_vif_assoc(mvm, vif);
895 		} else if (iwl_mvm_mld_vif_have_valid_ap_sta(mvmvif)) {
896 			iwl_mvm_mei_host_disassociated(mvm);
897 
898 			/* If update fails - SF might be running in associated
899 			 * mode while disassociated - which is forbidden.
900 			 */
901 			ret = iwl_mvm_sf_update(mvm, vif, false);
902 			WARN_ONCE(ret &&
903 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
904 					    &mvm->status),
905 				  "Failed to update SF upon disassociation\n");
906 
907 			/* If we get an assert during the connection (after the
908 			 * station has been added, but before the vif is set
909 			 * to associated), mac80211 will re-add the station and
910 			 * then configure the vif. Since the vif is not
911 			 * associated, we would remove the station here and
912 			 * this would fail the recovery.
913 			 */
914 			iwl_mvm_mld_vif_delete_all_stas(mvm, vif);
915 		}
916 
917 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
918 	}
919 
920 	if (changes & BSS_CHANGED_PS) {
921 		ret = iwl_mvm_power_update_mac(mvm);
922 		if (ret)
923 			IWL_ERR(mvm, "failed to update power mode\n");
924 	}
925 
926 	if (changes & (BSS_CHANGED_MLD_VALID_LINKS | BSS_CHANGED_MLD_TTLM) &&
927 	    ieee80211_vif_is_mld(vif) && mvmvif->authorized)
928 		wiphy_delayed_work_queue(mvm->hw->wiphy,
929 					 &mvmvif->mlo_int_scan_wk, 0);
930 }
931 
932 static void
933 iwl_mvm_mld_link_info_changed_ap_ibss(struct iwl_mvm *mvm,
934 				      struct ieee80211_vif *vif,
935 				      struct ieee80211_bss_conf *link_conf,
936 				      u64 changes)
937 {
938 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
939 	u32 link_changes = LINK_CONTEXT_MODIFY_PROTECT_FLAGS |
940 			   LINK_CONTEXT_MODIFY_QOS_PARAMS;
941 
942 	/* Changes will be applied when the AP/IBSS is started */
943 	if (!mvmvif->ap_ibss_active)
944 		return;
945 
946 	if (link_conf->he_support)
947 		link_changes |= LINK_CONTEXT_MODIFY_HE_PARAMS;
948 
949 	if (changes & BSS_CHANGED_ERP_SLOT)
950 		link_changes |= LINK_CONTEXT_MODIFY_RATES_INFO;
951 
952 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_SLOT |
953 		       BSS_CHANGED_HT |
954 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS |
955 		       BSS_CHANGED_HE_BSS_COLOR) &&
956 		       iwl_mvm_link_changed(mvm, vif, link_conf,
957 					    link_changes, true))
958 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
959 
960 	/* Need to send a new beacon template to the FW */
961 	if (changes & BSS_CHANGED_BEACON &&
962 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf))
963 		IWL_WARN(mvm, "Failed updating beacon data\n");
964 
965 	/* FIXME: need to decide if we need FTM responder per link */
966 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
967 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, link_conf);
968 
969 		if (ret)
970 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
971 				 ret);
972 	}
973 }
974 
975 static void iwl_mvm_mld_link_info_changed(struct ieee80211_hw *hw,
976 					  struct ieee80211_vif *vif,
977 					  struct ieee80211_bss_conf *link_conf,
978 					  u64 changes)
979 {
980 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
981 
982 	mutex_lock(&mvm->mutex);
983 
984 	switch (vif->type) {
985 	case NL80211_IFTYPE_STATION:
986 		iwl_mvm_mld_link_info_changed_station(mvm, vif, link_conf,
987 						      changes);
988 		break;
989 	case NL80211_IFTYPE_AP:
990 	case NL80211_IFTYPE_ADHOC:
991 		iwl_mvm_mld_link_info_changed_ap_ibss(mvm, vif, link_conf,
992 						      changes);
993 		break;
994 	case NL80211_IFTYPE_MONITOR:
995 		if (changes & BSS_CHANGED_MU_GROUPS)
996 			iwl_mvm_update_mu_groups(mvm, vif);
997 		break;
998 	default:
999 		/* shouldn't happen */
1000 		WARN_ON_ONCE(1);
1001 	}
1002 
1003 	if (changes & BSS_CHANGED_TXPOWER) {
1004 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
1005 				link_conf->txpower);
1006 		iwl_mvm_set_tx_power(mvm, vif, link_conf->txpower);
1007 	}
1008 
1009 	mutex_unlock(&mvm->mutex);
1010 }
1011 
1012 static void iwl_mvm_mld_vif_cfg_changed(struct ieee80211_hw *hw,
1013 					struct ieee80211_vif *vif,
1014 					u64 changes)
1015 {
1016 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1017 
1018 	mutex_lock(&mvm->mutex);
1019 
1020 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
1021 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1022 
1023 	if (vif->type == NL80211_IFTYPE_STATION)
1024 		iwl_mvm_mld_vif_cfg_changed_station(mvm, vif, changes);
1025 
1026 	mutex_unlock(&mvm->mutex);
1027 }
1028 
1029 static int
1030 iwl_mvm_mld_switch_vif_chanctx(struct ieee80211_hw *hw,
1031 			       struct ieee80211_vif_chanctx_switch *vifs,
1032 			       int n_vifs,
1033 			       enum ieee80211_chanctx_switch_mode mode)
1034 {
1035 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
1036 		.__assign_vif_chanctx = __iwl_mvm_mld_assign_vif_chanctx,
1037 		.__unassign_vif_chanctx = __iwl_mvm_mld_unassign_vif_chanctx,
1038 	};
1039 
1040 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
1041 }
1042 
1043 static void iwl_mvm_mld_config_iface_filter(struct ieee80211_hw *hw,
1044 					    struct ieee80211_vif *vif,
1045 					    unsigned int filter_flags,
1046 					    unsigned int changed_flags)
1047 {
1048 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1049 
1050 	/* We support only filter for probe requests */
1051 	if (!(changed_flags & FIF_PROBE_REQ))
1052 		return;
1053 
1054 	/* Supported only for p2p client interfaces */
1055 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1056 	    !vif->p2p)
1057 		return;
1058 
1059 	mutex_lock(&mvm->mutex);
1060 	iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1061 	mutex_unlock(&mvm->mutex);
1062 }
1063 
1064 static int
1065 iwl_mvm_mld_mac_conf_tx(struct ieee80211_hw *hw,
1066 			struct ieee80211_vif *vif,
1067 			unsigned int link_id, u16 ac,
1068 			const struct ieee80211_tx_queue_params *params)
1069 {
1070 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1071 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1072 	struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
1073 
1074 	if (!mvm_link)
1075 		return -EINVAL;
1076 
1077 	mvm_link->queue_params[ac] = *params;
1078 
1079 	/* No need to update right away, we'll get BSS_CHANGED_QOS
1080 	 * The exception is P2P_DEVICE interface which needs immediate update.
1081 	 */
1082 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1083 		int ret;
1084 
1085 		mutex_lock(&mvm->mutex);
1086 		ret = iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
1087 					   LINK_CONTEXT_MODIFY_QOS_PARAMS,
1088 					   true);
1089 		mutex_unlock(&mvm->mutex);
1090 		return ret;
1091 	}
1092 	return 0;
1093 }
1094 
1095 static int iwl_mvm_mld_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1096 {
1097 	int ret;
1098 
1099 	lockdep_assert_held(&mvm->mutex);
1100 
1101 	/* The PHY context ID might have changed so need to set it */
1102 	ret = iwl_mvm_link_changed(mvm, vif, &vif->bss_conf, 0, false);
1103 	if (WARN(ret, "Failed to set PHY context ID\n"))
1104 		return ret;
1105 
1106 	ret = iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
1107 				   LINK_CONTEXT_MODIFY_ACTIVE |
1108 				   LINK_CONTEXT_MODIFY_RATES_INFO,
1109 				   true);
1110 
1111 	if (WARN(ret, "Failed linking P2P_DEVICE\n"))
1112 		return ret;
1113 
1114 	/* The station and queue allocation must be done only after the linking
1115 	 * is done, as otherwise the FW might incorrectly configure its state.
1116 	 */
1117 	return iwl_mvm_mld_add_bcast_sta(mvm, vif, &vif->bss_conf);
1118 }
1119 
1120 static int iwl_mvm_mld_roc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1121 			   struct ieee80211_channel *channel, int duration,
1122 			   enum ieee80211_roc_type type)
1123 {
1124 	static const struct iwl_mvm_roc_ops ops = {
1125 		.add_aux_sta_for_hs20 = iwl_mvm_mld_add_aux_sta,
1126 		.link = iwl_mvm_mld_roc_link,
1127 	};
1128 
1129 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
1130 }
1131 
1132 static int
1133 iwl_mvm_mld_change_vif_links(struct ieee80211_hw *hw,
1134 			     struct ieee80211_vif *vif,
1135 			     u16 old_links, u16 new_links,
1136 			     struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
1137 {
1138 	struct iwl_mvm_vif_link_info *new_link[IEEE80211_MLD_MAX_NUM_LINKS] = {};
1139 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1140 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1141 	u16 removed = old_links & ~new_links;
1142 	u16 added = new_links & ~old_links;
1143 	int err, i;
1144 
1145 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
1146 		int r;
1147 
1148 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1149 			break;
1150 
1151 		if (!(added & BIT(i)))
1152 			continue;
1153 		new_link[i] = kzalloc(sizeof(*new_link[i]), GFP_KERNEL);
1154 		if (!new_link[i]) {
1155 			err = -ENOMEM;
1156 			goto free;
1157 		}
1158 
1159 		new_link[i]->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
1160 		new_link[i]->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
1161 		new_link[i]->ap_sta_id = IWL_MVM_INVALID_STA;
1162 		new_link[i]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1163 
1164 		for (r = 0; r < NUM_IWL_MVM_SMPS_REQ; r++)
1165 			new_link[i]->smps_requests[r] =
1166 				IEEE80211_SMPS_AUTOMATIC;
1167 	}
1168 
1169 	mutex_lock(&mvm->mutex);
1170 
1171 	if (old_links == 0) {
1172 		err = iwl_mvm_disable_link(mvm, vif, &vif->bss_conf);
1173 		if (err)
1174 			goto out_err;
1175 		mvmvif->link[0] = NULL;
1176 	}
1177 
1178 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
1179 		if (removed & BIT(i)) {
1180 			struct ieee80211_bss_conf *link_conf = old[i];
1181 
1182 			err = iwl_mvm_disable_link(mvm, vif, link_conf);
1183 			if (err)
1184 				goto out_err;
1185 			kfree(mvmvif->link[i]);
1186 			mvmvif->link[i] = NULL;
1187 		} else if (added & BIT(i)) {
1188 			struct ieee80211_bss_conf *link_conf;
1189 
1190 			link_conf = link_conf_dereference_protected(vif, i);
1191 			if (WARN_ON(!link_conf))
1192 				continue;
1193 
1194 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1195 				      &mvm->status))
1196 				mvmvif->link[i] = new_link[i];
1197 			new_link[i] = NULL;
1198 			err = iwl_mvm_add_link(mvm, vif, link_conf);
1199 			if (err)
1200 				goto out_err;
1201 		}
1202 	}
1203 
1204 	err = 0;
1205 	if (new_links == 0) {
1206 		mvmvif->link[0] = &mvmvif->deflink;
1207 		err = iwl_mvm_add_link(mvm, vif, &vif->bss_conf);
1208 		if (err == 0)
1209 			mvmvif->primary_link = 0;
1210 	} else if (!(new_links & BIT(mvmvif->primary_link))) {
1211 		/*
1212 		 * Ensure we always have a valid primary_link, the real
1213 		 * decision happens later when PHY is activated.
1214 		 */
1215 		mvmvif->primary_link = __ffs(new_links);
1216 	}
1217 
1218 out_err:
1219 	/* we really don't have a good way to roll back here ... */
1220 	mutex_unlock(&mvm->mutex);
1221 
1222 free:
1223 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
1224 		kfree(new_link[i]);
1225 	return err;
1226 }
1227 
1228 static int
1229 iwl_mvm_mld_change_sta_links(struct ieee80211_hw *hw,
1230 			     struct ieee80211_vif *vif,
1231 			     struct ieee80211_sta *sta,
1232 			     u16 old_links, u16 new_links)
1233 {
1234 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1235 	int ret;
1236 
1237 	mutex_lock(&mvm->mutex);
1238 	ret = iwl_mvm_mld_update_sta_links(mvm, vif, sta, old_links, new_links);
1239 	mutex_unlock(&mvm->mutex);
1240 
1241 	return ret;
1242 }
1243 
1244 bool iwl_mvm_vif_has_esr_cap(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1245 {
1246 	const struct wiphy_iftype_ext_capab *ext_capa;
1247 
1248 	lockdep_assert_held(&mvm->mutex);
1249 
1250 	if (!ieee80211_vif_is_mld(vif) || !vif->cfg.assoc ||
1251 	    hweight16(ieee80211_vif_usable_links(vif)) == 1)
1252 		return false;
1253 
1254 	if (!(vif->cfg.eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP))
1255 		return false;
1256 
1257 	ext_capa = cfg80211_get_iftype_ext_capa(mvm->hw->wiphy,
1258 						ieee80211_vif_type_p2p(vif));
1259 	return (ext_capa &&
1260 		(ext_capa->eml_capabilities & IEEE80211_EML_CAP_EMLSR_SUPP));
1261 }
1262 
1263 static bool iwl_mvm_mld_can_activate_links(struct ieee80211_hw *hw,
1264 					   struct ieee80211_vif *vif,
1265 					   u16 desired_links)
1266 {
1267 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1268 	int n_links = hweight16(desired_links);
1269 	bool ret = true;
1270 
1271 	if (n_links <= 1)
1272 		return true;
1273 
1274 	mutex_lock(&mvm->mutex);
1275 
1276 	/* Check if HW supports the wanted number of links */
1277 	if (n_links > iwl_mvm_max_active_links(mvm, vif)) {
1278 		ret = false;
1279 		goto unlock;
1280 	}
1281 
1282 	/* If it is an eSR device, check that we can enter eSR */
1283 	ret = iwl_mvm_is_esr_supported(mvm->fwrt.trans) &&
1284 	      iwl_mvm_vif_has_esr_cap(mvm, vif);
1285 
1286 unlock:
1287 	mutex_unlock(&mvm->mutex);
1288 	return ret;
1289 }
1290 
1291 static enum ieee80211_neg_ttlm_res
1292 iwl_mvm_mld_can_neg_ttlm(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1293 			 struct ieee80211_neg_ttlm *neg_ttlm)
1294 {
1295 	u16 map;
1296 	u8 i;
1297 
1298 	/* Verify all TIDs are mapped to the same links set */
1299 	map = neg_ttlm->downlink[0];
1300 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
1301 		if (neg_ttlm->downlink[i] != neg_ttlm->uplink[i] ||
1302 		    neg_ttlm->uplink[i] != map)
1303 			return NEG_TTLM_RES_REJECT;
1304 	}
1305 
1306 	return NEG_TTLM_RES_ACCEPT;
1307 }
1308 
1309 static int
1310 iwl_mvm_mld_mac_pre_channel_switch(struct ieee80211_hw *hw,
1311 				   struct ieee80211_vif *vif,
1312 				   struct ieee80211_channel_switch *chsw)
1313 {
1314 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1315 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1316 	int ret;
1317 
1318 	mutex_lock(&mvm->mutex);
1319 	if (mvmvif->esr_active) {
1320 		u8 primary = iwl_mvm_get_primary_link(vif);
1321 		int selected;
1322 
1323 		/* prefer primary unless quiet CSA on it */
1324 		if (chsw->link_id == primary && chsw->block_tx)
1325 			selected = iwl_mvm_get_other_link(vif, primary);
1326 		else
1327 			selected = primary;
1328 
1329 		iwl_mvm_exit_esr(mvm, vif, IWL_MVM_ESR_EXIT_CSA, selected);
1330 		mutex_unlock(&mvm->mutex);
1331 
1332 		/*
1333 		 * If we've not kept the link active that's doing the CSA
1334 		 * then we don't need to do anything else, just return.
1335 		 */
1336 		if (selected != chsw->link_id)
1337 			return 0;
1338 
1339 		mutex_lock(&mvm->mutex);
1340 	}
1341 
1342 	ret = iwl_mvm_pre_channel_switch(mvm, vif, chsw);
1343 	mutex_unlock(&mvm->mutex);
1344 
1345 	return ret;
1346 }
1347 
1348 const struct ieee80211_ops iwl_mvm_mld_hw_ops = {
1349 	.tx = iwl_mvm_mac_tx,
1350 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
1351 	.ampdu_action = iwl_mvm_mac_ampdu_action,
1352 	.get_antenna = iwl_mvm_op_get_antenna,
1353 	.set_antenna = iwl_mvm_op_set_antenna,
1354 	.start = iwl_mvm_mac_start,
1355 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
1356 	.stop = iwl_mvm_mac_stop,
1357 	.add_interface = iwl_mvm_mld_mac_add_interface,
1358 	.remove_interface = iwl_mvm_mld_mac_remove_interface,
1359 	.config = iwl_mvm_mac_config,
1360 	.prepare_multicast = iwl_mvm_prepare_multicast,
1361 	.configure_filter = iwl_mvm_configure_filter,
1362 	.config_iface_filter = iwl_mvm_mld_config_iface_filter,
1363 	.link_info_changed = iwl_mvm_mld_link_info_changed,
1364 	.vif_cfg_changed = iwl_mvm_mld_vif_cfg_changed,
1365 	.hw_scan = iwl_mvm_mac_hw_scan,
1366 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1367 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
1368 	.sta_state = iwl_mvm_mld_mac_sta_state,
1369 	.sta_notify = iwl_mvm_mac_sta_notify,
1370 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1371 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
1372 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1373 	.sta_rc_update = iwl_mvm_sta_rc_update,
1374 	.conf_tx = iwl_mvm_mld_mac_conf_tx,
1375 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1376 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
1377 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
1378 	.flush = iwl_mvm_mac_flush,
1379 	.flush_sta = iwl_mvm_mac_flush_sta,
1380 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
1381 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
1382 	.set_key = iwl_mvm_mac_set_key,
1383 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
1384 	.remain_on_channel = iwl_mvm_mld_roc,
1385 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
1386 	.add_chanctx = iwl_mvm_add_chanctx,
1387 	.remove_chanctx = iwl_mvm_remove_chanctx,
1388 	.change_chanctx = iwl_mvm_change_chanctx,
1389 	.assign_vif_chanctx = iwl_mvm_mld_assign_vif_chanctx,
1390 	.unassign_vif_chanctx = iwl_mvm_mld_unassign_vif_chanctx,
1391 	.switch_vif_chanctx = iwl_mvm_mld_switch_vif_chanctx,
1392 
1393 	.start_ap = iwl_mvm_mld_start_ap,
1394 	.stop_ap = iwl_mvm_mld_stop_ap,
1395 	.join_ibss = iwl_mvm_mld_start_ibss,
1396 	.leave_ibss = iwl_mvm_mld_stop_ibss,
1397 
1398 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
1399 
1400 	.channel_switch = iwl_mvm_channel_switch,
1401 	.pre_channel_switch = iwl_mvm_mld_mac_pre_channel_switch,
1402 	.post_channel_switch = iwl_mvm_post_channel_switch,
1403 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
1404 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
1405 
1406 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
1407 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
1408 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
1409 
1410 	.event_callback = iwl_mvm_mac_event_callback,
1411 
1412 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
1413 
1414 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
1415 
1416 #ifdef CONFIG_PM_SLEEP
1417 	/* look at d3.c */
1418 	.suspend = iwl_mvm_suspend,
1419 	.resume = iwl_mvm_resume,
1420 	.set_wakeup = iwl_mvm_set_wakeup,
1421 	.set_rekey_data = iwl_mvm_set_rekey_data,
1422 #if IS_ENABLED(CONFIG_IPV6)
1423 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1424 #endif
1425 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1426 #endif
1427 	.get_survey = iwl_mvm_mac_get_survey,
1428 	.sta_statistics = iwl_mvm_mac_sta_statistics,
1429 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
1430 	.start_pmsr = iwl_mvm_start_pmsr,
1431 	.abort_pmsr = iwl_mvm_abort_pmsr,
1432 
1433 #ifdef CONFIG_IWLWIFI_DEBUGFS
1434 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
1435 	.link_add_debugfs = iwl_mvm_link_add_debugfs,
1436 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
1437 #endif
1438 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
1439 
1440 	.change_vif_links = iwl_mvm_mld_change_vif_links,
1441 	.change_sta_links = iwl_mvm_mld_change_sta_links,
1442 	.can_activate_links = iwl_mvm_mld_can_activate_links,
1443 	.can_neg_ttlm = iwl_mvm_mld_can_neg_ttlm,
1444 };
1445