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