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