xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mld-mac80211.c (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
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 			mvmvif->session_prot_connection_loss = false;
877 
878 			/* clear statistics to get clean beacon counter */
879 			iwl_mvm_request_statistics(mvm, true);
880 			iwl_mvm_sf_update(mvm, vif, false);
881 			iwl_mvm_power_vif_assoc(mvm, vif);
882 
883 			for_each_mvm_vif_valid_link(mvmvif, i) {
884 				memset(&mvmvif->link[i]->beacon_stats, 0,
885 				       sizeof(mvmvif->link[i]->beacon_stats));
886 
887 				if (vif->p2p) {
888 					iwl_mvm_update_smps(mvm, vif,
889 							    IWL_MVM_SMPS_REQ_PROT,
890 							    IEEE80211_SMPS_DYNAMIC, i);
891 				}
892 
893 				rcu_read_lock();
894 				link_conf = rcu_dereference(vif->link_conf[i]);
895 				if (link_conf && !link_conf->dtim_period)
896 					protect = true;
897 				rcu_read_unlock();
898 			}
899 
900 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
901 			    protect) {
902 				/* We are in assoc so only one link is active-
903 				 * The association link
904 				 */
905 				unsigned int link_id =
906 					ffs(vif->active_links) - 1;
907 
908 				/* If we're not restarting and still haven't
909 				 * heard a beacon (dtim period unknown) then
910 				 * make sure we still have enough minimum time
911 				 * remaining in the time event, since the auth
912 				 * might actually have taken quite a while
913 				 * (especially for SAE) and so the remaining
914 				 * time could be small without us having heard
915 				 * a beacon yet.
916 				 */
917 				iwl_mvm_protect_assoc(mvm, vif, 0, link_id);
918 			}
919 
920 			iwl_mvm_sf_update(mvm, vif, false);
921 
922 			/* FIXME: need to decide about misbehaving AP handling */
923 			iwl_mvm_power_vif_assoc(mvm, vif);
924 		} else if (iwl_mvm_mld_vif_have_valid_ap_sta(mvmvif)) {
925 			iwl_mvm_mei_host_disassociated(mvm);
926 
927 			/* If update fails - SF might be running in associated
928 			 * mode while disassociated - which is forbidden.
929 			 */
930 			ret = iwl_mvm_sf_update(mvm, vif, false);
931 			WARN_ONCE(ret &&
932 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
933 					    &mvm->status),
934 				  "Failed to update SF upon disassociation\n");
935 
936 			/* If we get an assert during the connection (after the
937 			 * station has been added, but before the vif is set
938 			 * to associated), mac80211 will re-add the station and
939 			 * then configure the vif. Since the vif is not
940 			 * associated, we would remove the station here and
941 			 * this would fail the recovery.
942 			 */
943 			iwl_mvm_mld_vif_delete_all_stas(mvm, vif);
944 		}
945 
946 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
947 	}
948 
949 	if (changes & BSS_CHANGED_PS) {
950 		ret = iwl_mvm_power_update_mac(mvm);
951 		if (ret)
952 			IWL_ERR(mvm, "failed to update power mode\n");
953 	}
954 
955 	if (changes & (BSS_CHANGED_MLD_VALID_LINKS | BSS_CHANGED_MLD_TTLM) &&
956 	    ieee80211_vif_is_mld(vif) && mvmvif->authorized)
957 		wiphy_delayed_work_queue(mvm->hw->wiphy,
958 					 &mvmvif->mlo_int_scan_wk, 0);
959 }
960 
961 static void
962 iwl_mvm_mld_link_info_changed_ap_ibss(struct iwl_mvm *mvm,
963 				      struct ieee80211_vif *vif,
964 				      struct ieee80211_bss_conf *link_conf,
965 				      u64 changes)
966 {
967 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
968 	u32 link_changes = LINK_CONTEXT_MODIFY_PROTECT_FLAGS |
969 			   LINK_CONTEXT_MODIFY_QOS_PARAMS;
970 
971 	/* Changes will be applied when the AP/IBSS is started */
972 	if (!mvmvif->ap_ibss_active)
973 		return;
974 
975 	if (link_conf->he_support)
976 		link_changes |= LINK_CONTEXT_MODIFY_HE_PARAMS;
977 
978 	if (changes & BSS_CHANGED_ERP_SLOT)
979 		link_changes |= LINK_CONTEXT_MODIFY_RATES_INFO;
980 
981 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_ERP_SLOT |
982 		       BSS_CHANGED_HT |
983 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS |
984 		       BSS_CHANGED_HE_BSS_COLOR) &&
985 		       iwl_mvm_link_changed(mvm, vif, link_conf,
986 					    link_changes, true))
987 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
988 
989 	/* Need to send a new beacon template to the FW */
990 	if (changes & BSS_CHANGED_BEACON &&
991 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf))
992 		IWL_WARN(mvm, "Failed updating beacon data\n");
993 
994 	/* FIXME: need to decide if we need FTM responder per link */
995 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
996 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, link_conf);
997 
998 		if (ret)
999 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
1000 				 ret);
1001 	}
1002 }
1003 
1004 static void iwl_mvm_mld_link_info_changed(struct ieee80211_hw *hw,
1005 					  struct ieee80211_vif *vif,
1006 					  struct ieee80211_bss_conf *link_conf,
1007 					  u64 changes)
1008 {
1009 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1010 
1011 	guard(mvm)(mvm);
1012 
1013 	switch (vif->type) {
1014 	case NL80211_IFTYPE_STATION:
1015 		iwl_mvm_mld_link_info_changed_station(mvm, vif, link_conf,
1016 						      changes);
1017 		break;
1018 	case NL80211_IFTYPE_AP:
1019 	case NL80211_IFTYPE_ADHOC:
1020 		iwl_mvm_mld_link_info_changed_ap_ibss(mvm, vif, link_conf,
1021 						      changes);
1022 		break;
1023 	case NL80211_IFTYPE_MONITOR:
1024 		if (changes & BSS_CHANGED_MU_GROUPS)
1025 			iwl_mvm_update_mu_groups(mvm, vif);
1026 		break;
1027 	default:
1028 		/* shouldn't happen */
1029 		WARN_ON_ONCE(1);
1030 	}
1031 
1032 	if (changes & BSS_CHANGED_TXPOWER) {
1033 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
1034 				link_conf->txpower);
1035 		iwl_mvm_set_tx_power(mvm, vif, link_conf->txpower);
1036 	}
1037 }
1038 
1039 static void iwl_mvm_mld_vif_cfg_changed(struct ieee80211_hw *hw,
1040 					struct ieee80211_vif *vif,
1041 					u64 changes)
1042 {
1043 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1044 
1045 	guard(mvm)(mvm);
1046 
1047 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
1048 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1049 
1050 	if (vif->type == NL80211_IFTYPE_STATION)
1051 		iwl_mvm_mld_vif_cfg_changed_station(mvm, vif, changes);
1052 }
1053 
1054 static int
1055 iwl_mvm_mld_switch_vif_chanctx(struct ieee80211_hw *hw,
1056 			       struct ieee80211_vif_chanctx_switch *vifs,
1057 			       int n_vifs,
1058 			       enum ieee80211_chanctx_switch_mode mode)
1059 {
1060 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
1061 		.__assign_vif_chanctx = __iwl_mvm_mld_assign_vif_chanctx,
1062 		.__unassign_vif_chanctx = __iwl_mvm_mld_unassign_vif_chanctx,
1063 	};
1064 
1065 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
1066 }
1067 
1068 static void iwl_mvm_mld_config_iface_filter(struct ieee80211_hw *hw,
1069 					    struct ieee80211_vif *vif,
1070 					    unsigned int filter_flags,
1071 					    unsigned int changed_flags)
1072 {
1073 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1074 
1075 	/* We support only filter for probe requests */
1076 	if (!(changed_flags & FIF_PROBE_REQ))
1077 		return;
1078 
1079 	/* Supported only for p2p client interfaces */
1080 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
1081 	    !vif->p2p)
1082 		return;
1083 
1084 	guard(mvm)(mvm);
1085 	iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1086 }
1087 
1088 static int
1089 iwl_mvm_mld_mac_conf_tx(struct ieee80211_hw *hw,
1090 			struct ieee80211_vif *vif,
1091 			unsigned int link_id, u16 ac,
1092 			const struct ieee80211_tx_queue_params *params)
1093 {
1094 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1095 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1096 	struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
1097 
1098 	if (!mvm_link)
1099 		return -EINVAL;
1100 
1101 	mvm_link->queue_params[ac] = *params;
1102 
1103 	/* No need to update right away, we'll get BSS_CHANGED_QOS
1104 	 * The exception is P2P_DEVICE interface which needs immediate update.
1105 	 */
1106 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1107 		guard(mvm)(mvm);
1108 		return iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
1109 					    LINK_CONTEXT_MODIFY_QOS_PARAMS,
1110 					    true);
1111 	}
1112 	return 0;
1113 }
1114 
1115 static int iwl_mvm_mld_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1116 {
1117 	int ret;
1118 
1119 	lockdep_assert_held(&mvm->mutex);
1120 
1121 	/* The PHY context ID might have changed so need to set it */
1122 	ret = iwl_mvm_link_changed(mvm, vif, &vif->bss_conf, 0, false);
1123 	if (WARN(ret, "Failed to set PHY context ID\n"))
1124 		return ret;
1125 
1126 	ret = iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
1127 				   LINK_CONTEXT_MODIFY_ACTIVE |
1128 				   LINK_CONTEXT_MODIFY_RATES_INFO,
1129 				   true);
1130 
1131 	if (WARN(ret, "Failed linking P2P_DEVICE\n"))
1132 		return ret;
1133 
1134 	/* The station and queue allocation must be done only after the linking
1135 	 * is done, as otherwise the FW might incorrectly configure its state.
1136 	 */
1137 	return iwl_mvm_mld_add_bcast_sta(mvm, vif, &vif->bss_conf);
1138 }
1139 
1140 static int iwl_mvm_mld_roc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1141 			   struct ieee80211_channel *channel, int duration,
1142 			   enum ieee80211_roc_type type)
1143 {
1144 	static const struct iwl_mvm_roc_ops ops = {
1145 		.add_aux_sta_for_hs20 = iwl_mvm_mld_add_aux_sta,
1146 		.link = iwl_mvm_mld_roc_link,
1147 	};
1148 
1149 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
1150 }
1151 
1152 static int
1153 iwl_mvm_mld_change_vif_links(struct ieee80211_hw *hw,
1154 			     struct ieee80211_vif *vif,
1155 			     u16 old_links, u16 new_links,
1156 			     struct ieee80211_bss_conf *old[IEEE80211_MLD_MAX_NUM_LINKS])
1157 {
1158 	struct iwl_mvm_vif_link_info *new_link[IEEE80211_MLD_MAX_NUM_LINKS] = {};
1159 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1160 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1161 	u16 removed = old_links & ~new_links;
1162 	u16 added = new_links & ~old_links;
1163 	int err, i;
1164 
1165 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
1166 		int r;
1167 
1168 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1169 			break;
1170 
1171 		if (!(added & BIT(i)))
1172 			continue;
1173 		new_link[i] = kzalloc(sizeof(*new_link[i]), GFP_KERNEL);
1174 		if (!new_link[i]) {
1175 			err = -ENOMEM;
1176 			goto free;
1177 		}
1178 
1179 		new_link[i]->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
1180 		new_link[i]->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
1181 		new_link[i]->ap_sta_id = IWL_MVM_INVALID_STA;
1182 		new_link[i]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1183 
1184 		for (r = 0; r < NUM_IWL_MVM_SMPS_REQ; r++)
1185 			new_link[i]->smps_requests[r] =
1186 				IEEE80211_SMPS_AUTOMATIC;
1187 	}
1188 
1189 	mutex_lock(&mvm->mutex);
1190 
1191 	if (old_links == 0) {
1192 		err = iwl_mvm_disable_link(mvm, vif, &vif->bss_conf);
1193 		if (err)
1194 			goto out_err;
1195 		mvmvif->link[0] = NULL;
1196 	}
1197 
1198 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++) {
1199 		if (removed & BIT(i)) {
1200 			struct ieee80211_bss_conf *link_conf = old[i];
1201 
1202 			err = iwl_mvm_disable_link(mvm, vif, link_conf);
1203 			if (err)
1204 				goto out_err;
1205 			kfree(mvmvif->link[i]);
1206 			mvmvif->link[i] = NULL;
1207 		} else if (added & BIT(i)) {
1208 			struct ieee80211_bss_conf *link_conf;
1209 
1210 			link_conf = link_conf_dereference_protected(vif, i);
1211 			if (WARN_ON(!link_conf))
1212 				continue;
1213 
1214 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1215 				      &mvm->status))
1216 				mvmvif->link[i] = new_link[i];
1217 			new_link[i] = NULL;
1218 			err = iwl_mvm_add_link(mvm, vif, link_conf);
1219 			if (err)
1220 				goto out_err;
1221 		}
1222 	}
1223 
1224 	err = 0;
1225 	if (new_links == 0) {
1226 		mvmvif->link[0] = &mvmvif->deflink;
1227 		err = iwl_mvm_add_link(mvm, vif, &vif->bss_conf);
1228 		if (err == 0)
1229 			mvmvif->primary_link = 0;
1230 	} else if (!(new_links & BIT(mvmvif->primary_link))) {
1231 		/*
1232 		 * Ensure we always have a valid primary_link, the real
1233 		 * decision happens later when PHY is activated.
1234 		 */
1235 		mvmvif->primary_link = __ffs(new_links);
1236 	}
1237 
1238 out_err:
1239 	/* we really don't have a good way to roll back here ... */
1240 	mutex_unlock(&mvm->mutex);
1241 
1242 free:
1243 	for (i = 0; i < IEEE80211_MLD_MAX_NUM_LINKS; i++)
1244 		kfree(new_link[i]);
1245 	return err;
1246 }
1247 
1248 static int
1249 iwl_mvm_mld_change_sta_links(struct ieee80211_hw *hw,
1250 			     struct ieee80211_vif *vif,
1251 			     struct ieee80211_sta *sta,
1252 			     u16 old_links, u16 new_links)
1253 {
1254 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1255 
1256 	guard(mvm)(mvm);
1257 	return iwl_mvm_mld_update_sta_links(mvm, vif, sta, old_links, new_links);
1258 }
1259 
1260 bool iwl_mvm_vif_has_esr_cap(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1261 {
1262 	const struct wiphy_iftype_ext_capab *ext_capa;
1263 
1264 	lockdep_assert_held(&mvm->mutex);
1265 
1266 	if (!ieee80211_vif_is_mld(vif) || !vif->cfg.assoc ||
1267 	    hweight16(ieee80211_vif_usable_links(vif)) == 1)
1268 		return false;
1269 
1270 	if (!(vif->cfg.eml_cap & IEEE80211_EML_CAP_EMLSR_SUPP))
1271 		return false;
1272 
1273 	ext_capa = cfg80211_get_iftype_ext_capa(mvm->hw->wiphy,
1274 						ieee80211_vif_type_p2p(vif));
1275 	return (ext_capa &&
1276 		(ext_capa->eml_capabilities & IEEE80211_EML_CAP_EMLSR_SUPP));
1277 }
1278 
1279 static bool iwl_mvm_mld_can_activate_links(struct ieee80211_hw *hw,
1280 					   struct ieee80211_vif *vif,
1281 					   u16 desired_links)
1282 {
1283 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1284 	int n_links = hweight16(desired_links);
1285 
1286 	if (n_links <= 1)
1287 		return true;
1288 
1289 	guard(mvm)(mvm);
1290 
1291 	/* Check if HW supports the wanted number of links */
1292 	if (n_links > iwl_mvm_max_active_links(mvm, vif))
1293 		return false;
1294 
1295 	/* If it is an eSR device, check that we can enter eSR */
1296 	return iwl_mvm_is_esr_supported(mvm->fwrt.trans) &&
1297 	       iwl_mvm_vif_has_esr_cap(mvm, vif);
1298 }
1299 
1300 static enum ieee80211_neg_ttlm_res
1301 iwl_mvm_mld_can_neg_ttlm(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1302 			 struct ieee80211_neg_ttlm *neg_ttlm)
1303 {
1304 	u16 map;
1305 	u8 i;
1306 
1307 	/* Verify all TIDs are mapped to the same links set */
1308 	map = neg_ttlm->downlink[0];
1309 	for (i = 0; i < IEEE80211_TTLM_NUM_TIDS; i++) {
1310 		if (neg_ttlm->downlink[i] != neg_ttlm->uplink[i] ||
1311 		    neg_ttlm->uplink[i] != map)
1312 			return NEG_TTLM_RES_REJECT;
1313 	}
1314 
1315 	return NEG_TTLM_RES_ACCEPT;
1316 }
1317 
1318 static int
1319 iwl_mvm_mld_mac_pre_channel_switch(struct ieee80211_hw *hw,
1320 				   struct ieee80211_vif *vif,
1321 				   struct ieee80211_channel_switch *chsw)
1322 {
1323 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1324 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1325 	int ret;
1326 
1327 	mutex_lock(&mvm->mutex);
1328 	if (mvmvif->esr_active) {
1329 		u8 primary = iwl_mvm_get_primary_link(vif);
1330 		int selected;
1331 
1332 		/* prefer primary unless quiet CSA on it */
1333 		if (chsw->link_id == primary && chsw->block_tx)
1334 			selected = iwl_mvm_get_other_link(vif, primary);
1335 		else
1336 			selected = primary;
1337 
1338 		iwl_mvm_exit_esr(mvm, vif, IWL_MVM_ESR_EXIT_CSA, selected);
1339 		mutex_unlock(&mvm->mutex);
1340 
1341 		/*
1342 		 * If we've not kept the link active that's doing the CSA
1343 		 * then we don't need to do anything else, just return.
1344 		 */
1345 		if (selected != chsw->link_id)
1346 			return 0;
1347 
1348 		mutex_lock(&mvm->mutex);
1349 	}
1350 
1351 	ret = iwl_mvm_pre_channel_switch(mvm, vif, chsw);
1352 	mutex_unlock(&mvm->mutex);
1353 
1354 	return ret;
1355 }
1356 
1357 const struct ieee80211_ops iwl_mvm_mld_hw_ops = {
1358 	.tx = iwl_mvm_mac_tx,
1359 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
1360 	.ampdu_action = iwl_mvm_mac_ampdu_action,
1361 	.get_antenna = iwl_mvm_op_get_antenna,
1362 	.set_antenna = iwl_mvm_op_set_antenna,
1363 	.start = iwl_mvm_mac_start,
1364 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
1365 	.stop = iwl_mvm_mac_stop,
1366 	.add_interface = iwl_mvm_mld_mac_add_interface,
1367 	.remove_interface = iwl_mvm_mld_mac_remove_interface,
1368 	.config = iwl_mvm_mac_config,
1369 	.prepare_multicast = iwl_mvm_prepare_multicast,
1370 	.configure_filter = iwl_mvm_configure_filter,
1371 	.config_iface_filter = iwl_mvm_mld_config_iface_filter,
1372 	.link_info_changed = iwl_mvm_mld_link_info_changed,
1373 	.vif_cfg_changed = iwl_mvm_mld_vif_cfg_changed,
1374 	.hw_scan = iwl_mvm_mac_hw_scan,
1375 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1376 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
1377 	.sta_state = iwl_mvm_mld_mac_sta_state,
1378 	.sta_notify = iwl_mvm_mac_sta_notify,
1379 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
1380 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
1381 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1382 	.sta_rc_update = iwl_mvm_sta_rc_update,
1383 	.conf_tx = iwl_mvm_mld_mac_conf_tx,
1384 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
1385 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
1386 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
1387 	.flush = iwl_mvm_mac_flush,
1388 	.flush_sta = iwl_mvm_mac_flush_sta,
1389 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
1390 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
1391 	.set_key = iwl_mvm_mac_set_key,
1392 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
1393 	.remain_on_channel = iwl_mvm_mld_roc,
1394 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
1395 	.add_chanctx = iwl_mvm_add_chanctx,
1396 	.remove_chanctx = iwl_mvm_remove_chanctx,
1397 	.change_chanctx = iwl_mvm_change_chanctx,
1398 	.assign_vif_chanctx = iwl_mvm_mld_assign_vif_chanctx,
1399 	.unassign_vif_chanctx = iwl_mvm_mld_unassign_vif_chanctx,
1400 	.switch_vif_chanctx = iwl_mvm_mld_switch_vif_chanctx,
1401 
1402 	.start_ap = iwl_mvm_mld_start_ap,
1403 	.stop_ap = iwl_mvm_mld_stop_ap,
1404 	.join_ibss = iwl_mvm_mld_start_ibss,
1405 	.leave_ibss = iwl_mvm_mld_stop_ibss,
1406 
1407 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
1408 
1409 	.channel_switch = iwl_mvm_channel_switch,
1410 	.pre_channel_switch = iwl_mvm_mld_mac_pre_channel_switch,
1411 	.post_channel_switch = iwl_mvm_post_channel_switch,
1412 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
1413 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
1414 
1415 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
1416 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
1417 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
1418 
1419 	.event_callback = iwl_mvm_mac_event_callback,
1420 
1421 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
1422 
1423 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
1424 
1425 #ifdef CONFIG_PM_SLEEP
1426 	/* look at d3.c */
1427 	.suspend = iwl_mvm_suspend,
1428 	.resume = iwl_mvm_resume,
1429 	.set_wakeup = iwl_mvm_set_wakeup,
1430 	.set_rekey_data = iwl_mvm_set_rekey_data,
1431 #if IS_ENABLED(CONFIG_IPV6)
1432 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
1433 #endif
1434 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
1435 #endif
1436 	.get_survey = iwl_mvm_mac_get_survey,
1437 	.sta_statistics = iwl_mvm_mac_sta_statistics,
1438 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
1439 	.start_pmsr = iwl_mvm_start_pmsr,
1440 	.abort_pmsr = iwl_mvm_abort_pmsr,
1441 
1442 #ifdef CONFIG_IWLWIFI_DEBUGFS
1443 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
1444 	.link_add_debugfs = iwl_mvm_link_add_debugfs,
1445 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
1446 #endif
1447 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
1448 
1449 	.change_vif_links = iwl_mvm_mld_change_vif_links,
1450 	.change_sta_links = iwl_mvm_mld_change_sta_links,
1451 	.can_activate_links = iwl_mvm_mld_can_activate_links,
1452 	.can_neg_ttlm = iwl_mvm_mld_can_neg_ttlm,
1453 };
1454