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