xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/mac80211.c (revision 6b627f88584ce13118e0a24951b503c0b1f2d5a7)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2025 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/kernel.h>
8 #include <linux/fips.h>
9 #include <linux/slab.h>
10 #include <linux/skbuff.h>
11 #include <linux/netdevice.h>
12 #include <linux/etherdevice.h>
13 #include <linux/ip.h>
14 #include <linux/if_arp.h>
15 #include <linux/time.h>
16 #if defined(__FreeBSD__)
17 #include <linux/math64.h>
18 #endif
19 #include <net/mac80211.h>
20 #include <net/ieee80211_radiotap.h>
21 #include <net/tcp.h>
22 #if defined(__FreeBSD__)
23 #include <linux/udp.h>
24 #endif
25 
26 #include "iwl-drv.h"
27 #include "iwl-op-mode.h"
28 #include "iwl-io.h"
29 #include "mvm.h"
30 #include "sta.h"
31 #include "time-event.h"
32 #include "iwl-nvm-utils.h"
33 #include "iwl-phy-db.h"
34 #ifdef CONFIG_NL80211_TESTMODE
35 #include "testmode.h"
36 #endif
37 #include "fw/error-dump.h"
38 #include "iwl-prph.h"
39 #include "iwl-nvm-parse.h"
40 #include "time-sync.h"
41 
42 #define IWL_MVM_LIMITS(ap)					\
43 	{							\
44 		.max = 1,					\
45 		.types = BIT(NL80211_IFTYPE_STATION),		\
46 	},							\
47 	{							\
48 		.max = 1,					\
49 		.types = ap |					\
50 			 BIT(NL80211_IFTYPE_P2P_CLIENT) |	\
51 			 BIT(NL80211_IFTYPE_P2P_GO),		\
52 	},							\
53 	{							\
54 		.max = 1,					\
55 		.types = BIT(NL80211_IFTYPE_P2P_DEVICE),	\
56 	}
57 
58 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
59 	IWL_MVM_LIMITS(0)
60 };
61 
62 static const struct ieee80211_iface_limit iwl_mvm_limits_ap[] = {
63 	IWL_MVM_LIMITS(BIT(NL80211_IFTYPE_AP))
64 };
65 
66 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
67 	{
68 		.num_different_channels = 2,
69 		.max_interfaces = 3,
70 		.limits = iwl_mvm_limits,
71 		.n_limits = ARRAY_SIZE(iwl_mvm_limits),
72 	},
73 	{
74 		.num_different_channels = 1,
75 		.max_interfaces = 3,
76 		.limits = iwl_mvm_limits_ap,
77 		.n_limits = ARRAY_SIZE(iwl_mvm_limits_ap),
78 	},
79 };
80 
81 static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
82 	.max_peers = IWL_TOF_MAX_APS,
83 	.report_ap_tsf = 1,
84 	.randomize_mac_addr = 1,
85 
86 	.ftm = {
87 		.supported = 1,
88 		.asap = 1,
89 		.non_asap = 1,
90 		.request_lci = 1,
91 		.request_civicloc = 1,
92 		.trigger_based = 1,
93 		.non_trigger_based = 1,
94 		.max_bursts_exponent = -1, /* all supported */
95 		.max_ftms_per_burst = 0, /* no limits */
96 		.bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
97 			      BIT(NL80211_CHAN_WIDTH_20) |
98 			      BIT(NL80211_CHAN_WIDTH_40) |
99 			      BIT(NL80211_CHAN_WIDTH_80) |
100 			      BIT(NL80211_CHAN_WIDTH_160),
101 		.preambles = BIT(NL80211_PREAMBLE_LEGACY) |
102 			     BIT(NL80211_PREAMBLE_HT) |
103 			     BIT(NL80211_PREAMBLE_VHT) |
104 			     BIT(NL80211_PREAMBLE_HE),
105 	},
106 };
107 
108 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
109 				 enum set_key_cmd cmd,
110 				 struct ieee80211_vif *vif,
111 				 struct ieee80211_sta *sta,
112 				 struct ieee80211_key_conf *key);
113 
iwl_mvm_reset_phy_ctxts(struct iwl_mvm * mvm)114 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
115 {
116 	int i;
117 
118 	memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
119 	for (i = 0; i < NUM_PHY_CTX; i++) {
120 		mvm->phy_ctxts[i].id = i;
121 		mvm->phy_ctxts[i].ref = 0;
122 	}
123 }
124 
iwl_mvm_get_regdomain(struct wiphy * wiphy,const char * alpha2,enum iwl_mcc_source src_id,bool * changed)125 struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
126 						  const char *alpha2,
127 						  enum iwl_mcc_source src_id,
128 						  bool *changed)
129 {
130 	struct ieee80211_regdomain *regd = NULL;
131 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
132 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
133 	struct iwl_mcc_update_resp_v8 *resp;
134 	u8 resp_ver;
135 
136 	IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
137 
138 	lockdep_assert_held(&mvm->mutex);
139 
140 	resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
141 	if (IS_ERR_OR_NULL(resp)) {
142 		IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
143 			      PTR_ERR_OR_ZERO(resp));
144 		resp = NULL;
145 		goto out;
146 	}
147 
148 	if (changed) {
149 		u32 status = le32_to_cpu(resp->status);
150 
151 		*changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
152 			    status == MCC_RESP_ILLEGAL);
153 	}
154 	resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
155 					   MCC_UPDATE_CMD, 0);
156 	IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
157 
158 	regd = iwl_parse_nvm_mcc_info(mvm->trans,
159 				      __le32_to_cpu(resp->n_channels),
160 				      resp->channels,
161 				      __le16_to_cpu(resp->mcc),
162 				      __le16_to_cpu(resp->geo_info),
163 				      le32_to_cpu(resp->cap), resp_ver);
164 	/* Store the return source id */
165 	src_id = resp->source_id;
166 	if (IS_ERR_OR_NULL(regd)) {
167 		IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
168 			      PTR_ERR_OR_ZERO(regd));
169 		goto out;
170 	}
171 
172 	IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
173 		      regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
174 	mvm->lar_regdom_set = true;
175 	mvm->mcc_src = src_id;
176 
177 	iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
178 
179 out:
180 	kfree(resp);
181 	return regd;
182 }
183 
iwl_mvm_update_changed_regdom(struct iwl_mvm * mvm)184 void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
185 {
186 	bool changed;
187 	struct ieee80211_regdomain *regd;
188 
189 	if (!iwl_mvm_is_lar_supported(mvm))
190 		return;
191 
192 	regd = iwl_mvm_get_current_regdomain(mvm, &changed);
193 	if (!IS_ERR_OR_NULL(regd)) {
194 		/* only update the regulatory core if changed */
195 		if (changed)
196 			regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
197 
198 		kfree(regd);
199 	}
200 }
201 
iwl_mvm_get_current_regdomain(struct iwl_mvm * mvm,bool * changed)202 struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
203 							  bool *changed)
204 {
205 	return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
206 				     iwl_mvm_is_wifi_mcc_supported(mvm) ?
207 				     MCC_SOURCE_GET_CURRENT :
208 				     MCC_SOURCE_OLD_FW, changed);
209 }
210 
iwl_mvm_init_fw_regd(struct iwl_mvm * mvm,bool force_regd_sync)211 int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm, bool force_regd_sync)
212 {
213 	enum iwl_mcc_source used_src;
214 	struct ieee80211_regdomain *regd;
215 	int ret;
216 	bool changed;
217 	const struct ieee80211_regdomain *r =
218 			wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
219 
220 	if (!r)
221 		return -ENOENT;
222 
223 	/* save the last source in case we overwrite it below */
224 	used_src = mvm->mcc_src;
225 	if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
226 		/* Notify the firmware we support wifi location updates */
227 		regd = iwl_mvm_get_current_regdomain(mvm, NULL);
228 		if (!IS_ERR_OR_NULL(regd))
229 			kfree(regd);
230 	}
231 
232 	/* Now set our last stored MCC and source */
233 	regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
234 				     &changed);
235 	if (IS_ERR_OR_NULL(regd))
236 		return -EIO;
237 
238 	/* update cfg80211 if the regdomain was changed or the caller explicitly
239 	 * asked to update regdomain
240 	 */
241 	if (changed || force_regd_sync)
242 		ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
243 	else
244 		ret = 0;
245 
246 	kfree(regd);
247 	return ret;
248 }
249 
250 /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
251 static const u8 he_if_types_ext_capa_sta[] = {
252 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
253 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
254 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
255 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
256 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
257 };
258 
259 static const u8 tm_if_types_ext_capa_sta[] = {
260 	 [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
261 	 [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
262 	       WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
263 	 [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
264 	       WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
265 	 [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
266 	 [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
267 };
268 
269 /* Additional interface types for which extended capabilities are
270  * specified separately
271  */
272 
273 #define IWL_MVM_EMLSR_CAPA	(IEEE80211_EML_CAP_EMLSR_SUPP | \
274 				 IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
275 					__bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
276 				 IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
277 					__bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
278 #define IWL_MVM_MLD_CAPA_OPS (FIELD_PREP_CONST( \
279 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
280 			IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME) | \
281 			IEEE80211_MLD_CAP_OP_LINK_RECONF_SUPPORT)
282 
283 static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
284 	{
285 		.iftype = NL80211_IFTYPE_STATION,
286 		.extended_capabilities = he_if_types_ext_capa_sta,
287 		.extended_capabilities_mask = he_if_types_ext_capa_sta,
288 		.extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
289 		/* relevant only if EHT is supported */
290 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
291 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
292 	},
293 	{
294 		.iftype = NL80211_IFTYPE_STATION,
295 		.extended_capabilities = tm_if_types_ext_capa_sta,
296 		.extended_capabilities_mask = tm_if_types_ext_capa_sta,
297 		.extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
298 		/* relevant only if EHT is supported */
299 		.eml_capabilities = IWL_MVM_EMLSR_CAPA,
300 		.mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
301 	},
302 };
303 
iwl_mvm_op_get_antenna(struct ieee80211_hw * hw,int radio_idx,u32 * tx_ant,u32 * rx_ant)304 int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, int radio_idx,
305 			   u32 *tx_ant, u32 *rx_ant)
306 {
307 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
308 	*tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
309 	*rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
310 	return 0;
311 }
312 
iwl_mvm_op_set_antenna(struct ieee80211_hw * hw,int radio_idx,u32 tx_ant,u32 rx_ant)313 int iwl_mvm_op_set_antenna(struct ieee80211_hw *hw, int radio_idx, u32 tx_ant,
314 			   u32 rx_ant)
315 {
316 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
317 
318 	/* This has been tested on those devices only */
319 	if (mvm->trans->mac_cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
320 	    mvm->trans->mac_cfg->device_family != IWL_DEVICE_FAMILY_22000 &&
321 	    mvm->trans->mac_cfg->device_family != IWL_DEVICE_FAMILY_AX210)
322 		return -EOPNOTSUPP;
323 
324 	if (!mvm->nvm_data)
325 		return -EBUSY;
326 
327 	/* mac80211 ensures the device is not started,
328 	 * so the firmware cannot be running
329 	 */
330 
331 	mvm->set_tx_ant = tx_ant;
332 	mvm->set_rx_ant = rx_ant;
333 
334 	iwl_reinit_cab(mvm->trans, mvm->nvm_data, tx_ant, rx_ant, mvm->fw);
335 
336 	return 0;
337 }
338 
iwl_mvm_mac_setup_register(struct iwl_mvm * mvm)339 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
340 {
341 	struct ieee80211_hw *hw = mvm->hw;
342 	int num_mac, ret, i;
343 	static const u32 mvm_ciphers[] = {
344 		WLAN_CIPHER_SUITE_WEP40,
345 		WLAN_CIPHER_SUITE_WEP104,
346 		WLAN_CIPHER_SUITE_TKIP,
347 		WLAN_CIPHER_SUITE_CCMP,
348 	};
349 #ifdef CONFIG_PM_SLEEP
350 	bool unified = fw_has_capa(&mvm->fw->ucode_capa,
351 				   IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
352 #endif
353 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
354 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
355 
356 	/* Tell mac80211 our characteristics */
357 	ieee80211_hw_set(hw, SIGNAL_DBM);
358 	ieee80211_hw_set(hw, SPECTRUM_MGMT);
359 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
360 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
361 	ieee80211_hw_set(hw, SUPPORTS_PS);
362 	ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
363 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
364 	ieee80211_hw_set(hw, CONNECTION_MONITOR);
365 	ieee80211_hw_set(hw, CHANCTX_STA_CSA);
366 	ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
367 	ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
368 	ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
369 	ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
370 	ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
371 	ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
372 	ieee80211_hw_set(hw, STA_MMPDU_TXQ);
373 
374 	/* Set this early since we need to have it for the check below */
375 	if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
376 	    !iwlwifi_mod_params.disable_11ax &&
377 	    !iwlwifi_mod_params.disable_11be) {
378 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
379 		/* we handle this already earlier, but need it for MLO */
380 		ieee80211_hw_set(hw, HANDLES_QUIET_CSA);
381 	}
382 
383 	/* With MLD FW API, it tracks timing by itself,
384 	 * no need for any timing from the host
385 	 */
386 	if (!mvm->mld_api_is_used)
387 		ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
388 
389 	/*
390 	 * On older devices, enabling TX A-MSDU occasionally leads to
391 	 * something getting messed up, the command read from the FIFO
392 	 * gets out of sync and isn't a TX command, so that we have an
393 	 * assert EDC.
394 	 *
395 	 * It's not clear where the bug is, but since we didn't used to
396 	 * support A-MSDU until moving the mac80211 iTXQs, just leave it
397 	 * for older devices. We also don't see this issue on any newer
398 	 * devices.
399 	 */
400 	if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
401 		ieee80211_hw_set(hw, TX_AMSDU);
402 	ieee80211_hw_set(hw, TX_FRAG_LIST);
403 
404 	if (iwl_mvm_has_tlc_offload(mvm)) {
405 		ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
406 		ieee80211_hw_set(hw, HAS_RATE_CONTROL);
407 	}
408 
409 	/* We want to use the mac80211's reorder buffer for 9000 */
410 	if (iwl_mvm_has_new_rx_api(mvm) &&
411 	    mvm->trans->mac_cfg->device_family > IWL_DEVICE_FAMILY_9000)
412 		ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
413 
414 	if (fw_has_capa(&mvm->fw->ucode_capa,
415 			IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
416 		ieee80211_hw_set(hw, AP_LINK_PS);
417 	} else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
418 		/*
419 		 * we absolutely need this for the new TX API since that comes
420 		 * with many more queues than the current code can deal with
421 		 * for station powersave
422 		 */
423 		return -EINVAL;
424 	}
425 
426 	if (mvm->trans->info.num_rxqs > 1)
427 		ieee80211_hw_set(hw, USES_RSS);
428 
429 	if (mvm->trans->info.max_skb_frags)
430 		hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
431 
432 	hw->queues = IEEE80211_NUM_ACS;
433 	hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
434 	hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
435 				    IEEE80211_RADIOTAP_MCS_HAVE_STBC;
436 	hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
437 		IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
438 
439 	hw->radiotap_timestamp.units_pos =
440 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
441 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
442 	/* this is the case for CCK frames, it's better (only 8) for OFDM */
443 	hw->radiotap_timestamp.accuracy = 22;
444 
445 	if (!iwl_mvm_has_tlc_offload(mvm))
446 		hw->rate_control_algorithm = RS_NAME;
447 
448 	hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
449 	hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
450 	hw->max_tx_fragments = mvm->trans->info.max_skb_frags;
451 
452 	BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
453 	memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
454 	hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
455 	hw->wiphy->cipher_suites = mvm->ciphers;
456 
457 	if (iwl_mvm_has_new_rx_api(mvm)) {
458 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
459 			WLAN_CIPHER_SUITE_GCMP;
460 		hw->wiphy->n_cipher_suites++;
461 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
462 			WLAN_CIPHER_SUITE_GCMP_256;
463 		hw->wiphy->n_cipher_suites++;
464 	}
465 
466 	if (iwlwifi_mod_params.swcrypto)
467 		IWL_ERR(mvm,
468 			"iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
469 	if (!iwlwifi_mod_params.bt_coex_active)
470 		IWL_ERR(mvm,
471 			"iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
472 
473 	if (!fips_enabled)
474 		ieee80211_hw_set(hw, MFP_CAPABLE);
475 
476 	mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
477 	hw->wiphy->n_cipher_suites++;
478 	if (iwl_mvm_has_new_rx_api(mvm)) {
479 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
480 			WLAN_CIPHER_SUITE_BIP_GMAC_128;
481 		hw->wiphy->n_cipher_suites++;
482 		mvm->ciphers[hw->wiphy->n_cipher_suites] =
483 			WLAN_CIPHER_SUITE_BIP_GMAC_256;
484 		hw->wiphy->n_cipher_suites++;
485 	}
486 
487 	wiphy_ext_feature_set(hw->wiphy,
488 			      NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
489 	wiphy_ext_feature_set(hw->wiphy,
490 			      NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
491 
492 	if (fw_has_capa(&mvm->fw->ucode_capa,
493 			IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
494 		wiphy_ext_feature_set(hw->wiphy,
495 				      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
496 		hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
497 	}
498 
499 	/*
500 	 * beacon protection must be handled by firmware,
501 	 * so cannot be done with fips_enabled
502 	 */
503 	if (!fips_enabled && sec_key_ver &&
504 	    fw_has_capa(&mvm->fw->ucode_capa,
505 			IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
506 		wiphy_ext_feature_set(hw->wiphy,
507 				      NL80211_EXT_FEATURE_BEACON_PROTECTION);
508 	else if (!fips_enabled &&
509 		 fw_has_capa(&mvm->fw->ucode_capa,
510 			     IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
511 		wiphy_ext_feature_set(hw->wiphy,
512 				      NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
513 
514 	if (fw_has_capa(&mvm->fw->ucode_capa,
515 			IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
516 		hw->wiphy->hw_timestamp_max_peers = 1;
517 
518 	if (fw_has_capa(&mvm->fw->ucode_capa,
519 			IWL_UCODE_TLV_CAPA_SPP_AMSDU_SUPPORT))
520 		wiphy_ext_feature_set(hw->wiphy,
521 				      NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
522 
523 	ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
524 	hw->wiphy->features |=
525 		NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
526 		NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
527 		NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
528 
529 	hw->sta_data_size = sizeof(struct iwl_mvm_sta);
530 	hw->vif_data_size = sizeof(struct iwl_mvm_vif);
531 	hw->chanctx_data_size = sizeof(u16);
532 	hw->txq_data_size = sizeof(struct iwl_mvm_txq);
533 
534 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
535 		BIT(NL80211_IFTYPE_P2P_CLIENT) |
536 		BIT(NL80211_IFTYPE_AP) |
537 		BIT(NL80211_IFTYPE_P2P_GO) |
538 		BIT(NL80211_IFTYPE_P2P_DEVICE) |
539 		BIT(NL80211_IFTYPE_ADHOC);
540 
541 	hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
542 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
543 
544 	/* The new Tx API does not allow to pass the key or keyid of a MPDU to
545 	 * the hw, preventing us to control which key(id) to use per MPDU.
546 	 * Till that's fixed we can't use Extended Key ID for the newer cards.
547 	 */
548 	if (!iwl_mvm_has_new_tx_api(mvm))
549 		wiphy_ext_feature_set(hw->wiphy,
550 				      NL80211_EXT_FEATURE_EXT_KEY_ID);
551 	hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
552 
553 	hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
554 	if (iwl_mvm_is_lar_supported(mvm))
555 		hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
556 	else
557 		hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
558 					       REGULATORY_DISABLE_BEACON_HINTS;
559 
560 	if (mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
561 		wiphy_ext_feature_set(hw->wiphy,
562 				      NL80211_EXT_FEATURE_DFS_CONCURRENT);
563 
564 	hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
565 	hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
566 	hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
567 
568 	hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
569 	hw->wiphy->n_iface_combinations =
570 		ARRAY_SIZE(iwl_mvm_iface_combinations);
571 
572 	hw->wiphy->max_remain_on_channel_duration = 10000;
573 	hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
574 
575 	/* Extract MAC address */
576 	memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
577 	hw->wiphy->addresses = mvm->addresses;
578 	hw->wiphy->n_addresses = 1;
579 
580 	/* Extract additional MAC addresses if available */
581 	num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
582 		min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
583 
584 	for (i = 1; i < num_mac; i++) {
585 		memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
586 		       ETH_ALEN);
587 		mvm->addresses[i].addr[5]++;
588 		hw->wiphy->n_addresses++;
589 	}
590 
591 	iwl_mvm_reset_phy_ctxts(mvm);
592 
593 	hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
594 
595 	hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
596 
597 	BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
598 	BUILD_BUG_ON(IWL_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
599 		     IWL_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
600 
601 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
602 		mvm->max_scans = IWL_MAX_UMAC_SCANS;
603 	else
604 		mvm->max_scans = IWL_MAX_LMAC_SCANS;
605 
606 	if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
607 		hw->wiphy->bands[NL80211_BAND_2GHZ] =
608 			&mvm->nvm_data->bands[NL80211_BAND_2GHZ];
609 	if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
610 		hw->wiphy->bands[NL80211_BAND_5GHZ] =
611 			&mvm->nvm_data->bands[NL80211_BAND_5GHZ];
612 
613 		if (fw_has_capa(&mvm->fw->ucode_capa,
614 				IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
615 		    fw_has_api(&mvm->fw->ucode_capa,
616 			       IWL_UCODE_TLV_API_LQ_SS_PARAMS))
617 			hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
618 				IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
619 	}
620 	if (fw_has_capa(&mvm->fw->ucode_capa,
621 			IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
622 	    mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
623 		hw->wiphy->bands[NL80211_BAND_6GHZ] =
624 			&mvm->nvm_data->bands[NL80211_BAND_6GHZ];
625 
626 	hw->wiphy->hw_version = mvm->trans->info.hw_id;
627 
628 	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
629 		hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
630 	else
631 		hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
632 
633 	hw->wiphy->max_sched_scan_reqs = 1;
634 	hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
635 	hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
636 	/* we create the 802.11 header and zero length SSID IE. */
637 	hw->wiphy->max_sched_scan_ie_len =
638 		SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
639 	hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
640 	hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
641 
642 	/*
643 	 * the firmware uses u8 for num of iterations, but 0xff is saved for
644 	 * infinite loop, so the maximum number of iterations is actually 254.
645 	 */
646 	hw->wiphy->max_sched_scan_plan_iterations = 254;
647 
648 	hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
649 			       NL80211_FEATURE_LOW_PRIORITY_SCAN |
650 			       NL80211_FEATURE_P2P_GO_OPPPS |
651 			       NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
652 			       NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
653 
654 	/* when firmware supports RLC/SMPS offload, do not set these
655 	 * driver features, since it's no longer supported by driver.
656 	 */
657 	if (!iwl_mvm_has_rlc_offload(mvm))
658 		hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS |
659 				       NL80211_FEATURE_DYNAMIC_SMPS;
660 
661 	if (fw_has_capa(&mvm->fw->ucode_capa,
662 			IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
663 		hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
664 	if (fw_has_capa(&mvm->fw->ucode_capa,
665 			IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
666 		hw->wiphy->features |= NL80211_FEATURE_QUIET;
667 
668 	if (fw_has_capa(&mvm->fw->ucode_capa,
669 			IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
670 		hw->wiphy->features |=
671 			NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
672 
673 	if (fw_has_capa(&mvm->fw->ucode_capa,
674 			IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
675 		hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
676 
677 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
678 				  IWL_FW_CMD_VER_UNKNOWN) >= 3)
679 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
680 
681 	if (fw_has_api(&mvm->fw->ucode_capa,
682 		       IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
683 		wiphy_ext_feature_set(hw->wiphy,
684 				      NL80211_EXT_FEATURE_SCAN_START_TIME);
685 		wiphy_ext_feature_set(hw->wiphy,
686 				      NL80211_EXT_FEATURE_BSS_PARENT_TSF);
687 	}
688 
689 	if (iwl_mvm_is_oce_supported(mvm)) {
690 		u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
691 
692 		wiphy_ext_feature_set(hw->wiphy,
693 			NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
694 		wiphy_ext_feature_set(hw->wiphy,
695 			NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
696 		wiphy_ext_feature_set(hw->wiphy,
697 			NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
698 
699 		/* Old firmware also supports probe deferral and suppression */
700 		if (scan_ver < 15)
701 			wiphy_ext_feature_set(hw->wiphy,
702 					      NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
703 	}
704 
705 	hw->wiphy->iftype_ext_capab = NULL;
706 	hw->wiphy->num_iftype_ext_capab = 0;
707 
708 	if (mvm->nvm_data->sku_cap_11ax_enable &&
709 	    !iwlwifi_mod_params.disable_11ax) {
710 		hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
711 		hw->wiphy->num_iftype_ext_capab =
712 			ARRAY_SIZE(add_iftypes_ext_capa) - 1;
713 
714 		ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
715 		ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
716 	}
717 
718 	if (iwl_fw_lookup_cmd_ver(mvm->fw,
719 				  WIDE_ID(DATA_PATH_GROUP,
720 					  WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
721 				  IWL_FW_CMD_VER_UNKNOWN) >= 1) {
722 		IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
723 
724 		if (!hw->wiphy->iftype_ext_capab) {
725 			hw->wiphy->num_iftype_ext_capab = 1;
726 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
727 				ARRAY_SIZE(add_iftypes_ext_capa) - 1;
728 		} else {
729 			hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
730 		}
731 	}
732 
733 	if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(LOCATION_GROUP,
734 						   TOF_RANGE_REQ_CMD),
735 				  IWL_FW_CMD_VER_UNKNOWN) >= 11) {
736 		wiphy_ext_feature_set(hw->wiphy,
737 				      NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE);
738 
739 		if (fw_has_capa(&mvm->fw->ucode_capa,
740 				IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT))
741 			wiphy_ext_feature_set(hw->wiphy,
742 					      NL80211_EXT_FEATURE_SECURE_LTF);
743 	}
744 
745 	mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
746 
747 #ifdef CONFIG_PM_SLEEP
748 	if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
749 	    device_can_wakeup(mvm->trans->dev) && !fips_enabled) {
750 		mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
751 				     WIPHY_WOWLAN_DISCONNECT |
752 				     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
753 				     WIPHY_WOWLAN_RFKILL_RELEASE |
754 				     WIPHY_WOWLAN_NET_DETECT;
755 		mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
756 				     WIPHY_WOWLAN_GTK_REKEY_FAILURE |
757 				     WIPHY_WOWLAN_4WAY_HANDSHAKE;
758 
759 		mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
760 		mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
761 		mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
762 		mvm->wowlan.max_nd_match_sets =
763 			iwl_umac_scan_get_max_profiles(mvm->fw);
764 		hw->wiphy->wowlan = &mvm->wowlan;
765 	}
766 #endif
767 
768 	ret = iwl_mvm_leds_init(mvm);
769 	if (ret)
770 		return ret;
771 
772 	if (fw_has_capa(&mvm->fw->ucode_capa,
773 			IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
774 		IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
775 		hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
776 		ieee80211_hw_set(hw, TDLS_WIDER_BW);
777 	}
778 
779 	if (fw_has_capa(&mvm->fw->ucode_capa,
780 			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
781 		IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
782 		hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
783 	}
784 
785 	hw->netdev_features |= mvm->trans->mac_cfg->base->features;
786 	if (!iwl_mvm_is_csum_supported(mvm))
787 		hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
788 
789 	if (mvm->cfg->vht_mu_mimo_supported)
790 		wiphy_ext_feature_set(hw->wiphy,
791 				      NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
792 
793 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
794 		wiphy_ext_feature_set(hw->wiphy,
795 				      NL80211_EXT_FEATURE_PROTECTED_TWT);
796 
797 	iwl_mvm_vendor_cmds_register(mvm);
798 
799 	hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
800 	hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
801 
802 	ret = ieee80211_register_hw(mvm->hw);
803 	if (ret) {
804 		iwl_mvm_leds_exit(mvm);
805 	}
806 
807 	return ret;
808 }
809 
iwl_mvm_tx_skb(struct iwl_mvm * mvm,struct sk_buff * skb,struct ieee80211_sta * sta)810 static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
811 			   struct ieee80211_sta *sta)
812 {
813 	if (likely(sta)) {
814 		if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
815 			return;
816 	} else {
817 		if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
818 			return;
819 	}
820 
821 	ieee80211_free_txskb(mvm->hw, skb);
822 }
823 
iwl_mvm_mac_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)824 void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
825 		    struct ieee80211_tx_control *control, struct sk_buff *skb)
826 {
827 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
828 	struct ieee80211_sta *sta = control->sta;
829 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
830 	struct ieee80211_hdr *hdr = (void *)skb->data;
831 	bool offchannel = IEEE80211_SKB_CB(skb)->flags &
832 		IEEE80211_TX_CTL_TX_OFFCHAN;
833 	u32 link_id = u32_get_bits(info->control.flags,
834 				   IEEE80211_TX_CTRL_MLO_LINK);
835 	struct ieee80211_sta *tmp_sta = sta;
836 
837 	if (iwl_mvm_is_radio_killed(mvm)) {
838 		IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
839 		goto drop;
840 	}
841 
842 	if (offchannel &&
843 	    !test_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status) &&
844 	    !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
845 		goto drop;
846 
847 	/*
848 	 * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
849 	 * so we treat the others as broadcast
850 	 */
851 	if (ieee80211_is_mgmt(hdr->frame_control))
852 		sta = NULL;
853 
854 	/* this shouldn't even happen: just drop */
855 	if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
856 	    !offchannel)
857 		goto drop;
858 
859 	if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
860 	    !ieee80211_is_probe_resp(hdr->frame_control)) {
861 		/* translate MLD addresses to LINK addresses */
862 		struct ieee80211_link_sta *link_sta =
863 			rcu_dereference(tmp_sta->link[link_id]);
864 		struct ieee80211_bss_conf *link_conf =
865 			rcu_dereference(info->control.vif->link_conf[link_id]);
866 		struct ieee80211_mgmt *mgmt;
867 
868 		if (WARN_ON(!link_sta || !link_conf))
869 			goto drop;
870 
871 		/* if sta is NULL, the frame is a management frame */
872 		mgmt = (void *)hdr;
873 		memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
874 		memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
875 		memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
876 	}
877 
878 	iwl_mvm_tx_skb(mvm, skb, sta);
879 	return;
880  drop:
881 	ieee80211_free_txskb(hw, skb);
882 }
883 
iwl_mvm_mac_itxq_xmit(struct ieee80211_hw * hw,struct ieee80211_txq * txq)884 void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
885 {
886 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
887 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
888 	struct sk_buff *skb = NULL;
889 
890 	/*
891 	 * No need for threads to be pending here, they can leave the first
892 	 * taker all the work.
893 	 *
894 	 * mvmtxq->tx_request logic:
895 	 *
896 	 * If 0, no one is currently TXing, set to 1 to indicate current thread
897 	 * will now start TX and other threads should quit.
898 	 *
899 	 * If 1, another thread is currently TXing, set to 2 to indicate to
900 	 * that thread that there was another request. Since that request may
901 	 * have raced with the check whether the queue is empty, the TXing
902 	 * thread should check the queue's status one more time before leaving.
903 	 * This check is done in order to not leave any TX hanging in the queue
904 	 * until the next TX invocation (which may not even happen).
905 	 *
906 	 * If 2, another thread is currently TXing, and it will already double
907 	 * check the queue, so do nothing.
908 	 */
909 	if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
910 		return;
911 
912 	rcu_read_lock();
913 	do {
914 		while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
915 					&mvmtxq->state) &&
916 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
917 					&mvmtxq->state) &&
918 			      !test_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA,
919 					&mvmtxq->state) &&
920 			      !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
921 			skb = ieee80211_tx_dequeue(hw, txq);
922 
923 			if (!skb) {
924 				if (txq->sta)
925 					IWL_DEBUG_TX(mvm,
926 						     "TXQ of sta %pM tid %d is now empty\n",
927 						     txq->sta->addr,
928 						     txq->tid);
929 				break;
930 			}
931 
932 			iwl_mvm_tx_skb(mvm, skb, txq->sta);
933 		}
934 	} while (atomic_dec_return(&mvmtxq->tx_request));
935 	rcu_read_unlock();
936 }
937 
iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)938 void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
939 			       struct ieee80211_txq *txq)
940 {
941 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
942 	struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
943 
944 	if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
945 	    !txq->sta) {
946 		iwl_mvm_mac_itxq_xmit(hw, txq);
947 		return;
948 	}
949 
950 	/* iwl_mvm_mac_itxq_xmit() will later be called by the worker
951 	 * to handle any packets we leave on the txq now
952 	 */
953 
954 	spin_lock_bh(&mvm->add_stream_lock);
955 	/* The list is being deleted only after the queue is fully allocated. */
956 	if (list_empty(&mvmtxq->list) &&
957 	    /* recheck under lock */
958 	    !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
959 		list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
960 		schedule_work(&mvm->add_stream_wk);
961 	}
962 	spin_unlock_bh(&mvm->add_stream_lock);
963 }
964 
965 #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)		\
966 	do {								\
967 		if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))		\
968 			break;						\
969 		iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);	\
970 	} while (0)
971 
972 static void
iwl_mvm_ampdu_check_trigger(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,u16 tid,u16 rx_ba_ssn,enum ieee80211_ampdu_mlme_action action)973 iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
974 			    struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
975 			    enum ieee80211_ampdu_mlme_action action)
976 {
977 	struct iwl_fw_dbg_trigger_tlv *trig;
978 	struct iwl_fw_dbg_trigger_ba *ba_trig;
979 
980 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
981 				     FW_DBG_TRIGGER_BA);
982 	if (!trig)
983 		return;
984 
985 	ba_trig = (void *)trig->data;
986 
987 	switch (action) {
988 	case IEEE80211_AMPDU_TX_OPERATIONAL: {
989 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
990 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
991 
992 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
993 				 "TX AGG START: MAC %pM tid %d ssn %d\n",
994 				 sta->addr, tid, tid_data->ssn);
995 		break;
996 		}
997 	case IEEE80211_AMPDU_TX_STOP_CONT:
998 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
999 				 "TX AGG STOP: MAC %pM tid %d\n",
1000 				 sta->addr, tid);
1001 		break;
1002 	case IEEE80211_AMPDU_RX_START:
1003 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
1004 				 "RX AGG START: MAC %pM tid %d ssn %d\n",
1005 				 sta->addr, tid, rx_ba_ssn);
1006 		break;
1007 	case IEEE80211_AMPDU_RX_STOP:
1008 		CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
1009 				 "RX AGG STOP: MAC %pM tid %d\n",
1010 				 sta->addr, tid);
1011 		break;
1012 	default:
1013 		break;
1014 	}
1015 }
1016 
iwl_mvm_mac_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)1017 int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1018 			     struct ieee80211_vif *vif,
1019 			     struct ieee80211_ampdu_params *params)
1020 {
1021 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1022 	int ret;
1023 	struct ieee80211_sta *sta = params->sta;
1024 	enum ieee80211_ampdu_mlme_action action = params->action;
1025 	u16 tid = params->tid;
1026 	u16 *ssn = &params->ssn;
1027 	u16 buf_size = params->buf_size;
1028 	bool amsdu = params->amsdu;
1029 	u16 timeout = params->timeout;
1030 
1031 	IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1032 		     sta->addr, tid, action);
1033 
1034 	if (!(mvm->nvm_data->sku_cap_11n_enable))
1035 		return -EACCES;
1036 
1037 	mutex_lock(&mvm->mutex);
1038 
1039 	switch (action) {
1040 	case IEEE80211_AMPDU_RX_START:
1041 		if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
1042 		    iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
1043 			struct iwl_mvm_vif *mvmvif;
1044 			u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1045 			struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1046 
1047 			mdata->opened_rx_ba_sessions = true;
1048 			mvmvif = iwl_mvm_vif_from_mac80211(vif);
1049 			cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1050 		}
1051 		if (!iwl_enable_rx_ampdu()) {
1052 			ret = -EINVAL;
1053 			break;
1054 		}
1055 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
1056 					 timeout);
1057 		break;
1058 	case IEEE80211_AMPDU_RX_STOP:
1059 		ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
1060 					 timeout);
1061 		break;
1062 	case IEEE80211_AMPDU_TX_START:
1063 		if (!iwl_enable_tx_ampdu()) {
1064 			ret = -EINVAL;
1065 			break;
1066 		}
1067 		ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1068 		break;
1069 	case IEEE80211_AMPDU_TX_STOP_CONT:
1070 		ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1071 		break;
1072 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1073 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1074 		ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1075 		break;
1076 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1077 		ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1078 					      buf_size, amsdu);
1079 		break;
1080 	default:
1081 		WARN_ON_ONCE(1);
1082 		ret = -EINVAL;
1083 		break;
1084 	}
1085 
1086 	if (!ret) {
1087 		u16 rx_ba_ssn = 0;
1088 
1089 		if (action == IEEE80211_AMPDU_RX_START)
1090 			rx_ba_ssn = *ssn;
1091 
1092 		iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1093 					    rx_ba_ssn, action);
1094 	}
1095 	mutex_unlock(&mvm->mutex);
1096 
1097 	return ret;
1098 }
1099 
iwl_mvm_cleanup_iterator(void * data,u8 * mac,struct ieee80211_vif * vif)1100 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1101 				     struct ieee80211_vif *vif)
1102 {
1103 	struct iwl_mvm *mvm = data;
1104 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1105 	struct iwl_probe_resp_data *probe_data;
1106 	unsigned int link_id;
1107 
1108 	mvmvif->uploaded = false;
1109 
1110 	spin_lock_bh(&mvm->time_event_lock);
1111 	iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1112 	spin_unlock_bh(&mvm->time_event_lock);
1113 
1114 	mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
1115 
1116 	mvmvif->bf_enabled = false;
1117 	mvmvif->ba_enabled = false;
1118 	mvmvif->ap_sta = NULL;
1119 
1120 	mvmvif->esr_active = false;
1121 	vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
1122 
1123 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
1124 		mvmvif->link[link_id]->ap_sta_id = IWL_INVALID_STA;
1125 		mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1126 		mvmvif->link[link_id]->phy_ctxt = NULL;
1127 		mvmvif->link[link_id]->active = 0;
1128 		mvmvif->link[link_id]->igtk = NULL;
1129 		memset(&mvmvif->link[link_id]->bf_data, 0,
1130 		       sizeof(mvmvif->link[link_id]->bf_data));
1131 	}
1132 
1133 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1134 					       lockdep_is_held(&mvm->mutex));
1135 	if (probe_data)
1136 		kfree_rcu(probe_data, rcu_head);
1137 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1138 }
1139 
iwl_mvm_cleanup_sta_iterator(void * data,struct ieee80211_sta * sta)1140 static void iwl_mvm_cleanup_sta_iterator(void *data, struct ieee80211_sta *sta)
1141 {
1142 	struct iwl_mvm *mvm = data;
1143 	struct iwl_mvm_sta *mvm_sta;
1144 	struct ieee80211_vif *vif;
1145 	int link_id;
1146 
1147 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1148 	vif = mvm_sta->vif;
1149 
1150 	if (!sta->valid_links)
1151 		return;
1152 
1153 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
1154 		struct iwl_mvm_link_sta *mvm_link_sta;
1155 
1156 		mvm_link_sta =
1157 			rcu_dereference_check(mvm_sta->link[link_id],
1158 					      lockdep_is_held(&mvm->mutex));
1159 		if (mvm_link_sta && !(vif->active_links & BIT(link_id))) {
1160 			/*
1161 			 * We have a link STA but the link is inactive in
1162 			 * mac80211. This will happen if we failed to
1163 			 * deactivate the link but mac80211 roll back the
1164 			 * deactivation of the link.
1165 			 * Delete the stale data to avoid issues later on.
1166 			 */
1167 			iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
1168 						  link_id);
1169 		}
1170 	}
1171 }
1172 
iwl_mvm_restart_cleanup(struct iwl_mvm * mvm)1173 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1174 {
1175 	iwl_mvm_stop_device(mvm);
1176 
1177 	mvm->cur_aid = 0;
1178 
1179 	mvm->scan_status = 0;
1180 	mvm->ps_disabled = false;
1181 	mvm->rfkill_safe_init_done = false;
1182 
1183 	/* just in case one was running */
1184 	iwl_mvm_cleanup_roc_te(mvm);
1185 	ieee80211_remain_on_channel_expired(mvm->hw);
1186 
1187 	iwl_mvm_ftm_restart(mvm);
1188 
1189 	/*
1190 	 * cleanup all interfaces, even inactive ones, as some might have
1191 	 * gone down during the HW restart
1192 	 */
1193 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1194 
1195 	/* cleanup stations as links may be gone after restart */
1196 	ieee80211_iterate_stations_atomic(mvm->hw,
1197 					  iwl_mvm_cleanup_sta_iterator, mvm);
1198 
1199 	mvm->p2p_device_vif = NULL;
1200 
1201 	iwl_mvm_reset_phy_ctxts(mvm);
1202 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1203 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1204 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1205 
1206 	ieee80211_wake_queues(mvm->hw);
1207 
1208 	mvm->rx_ba_sessions = 0;
1209 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1210 	mvm->monitor_on = false;
1211 #ifdef CONFIG_IWLWIFI_DEBUGFS
1212 	mvm->beacon_inject_active = false;
1213 #endif
1214 
1215 	/* keep statistics ticking */
1216 	iwl_mvm_accu_radio_stats(mvm);
1217 }
1218 
__iwl_mvm_mac_start(struct iwl_mvm * mvm)1219 int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1220 {
1221 	bool fast_resume = false;
1222 	int ret;
1223 
1224 	lockdep_assert_held(&mvm->mutex);
1225 
1226 	ret = iwl_mvm_mei_get_ownership(mvm);
1227 	if (ret)
1228 		return ret;
1229 
1230 	if (mvm->mei_nvm_data) {
1231 		/* We got the NIC, we can now free the MEI NVM data */
1232 		kfree(mvm->mei_nvm_data);
1233 		mvm->mei_nvm_data = NULL;
1234 
1235 		/*
1236 		 * We can't free the nvm_data we allocated based on the SAP
1237 		 * data because we registered to cfg80211 with the channels
1238 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
1239 		 * just in order to be able free it later.
1240 		 * NULLify nvm_data so that we will read the NVM from the
1241 		 * firmware this time.
1242 		 */
1243 		mvm->temp_nvm_data = mvm->nvm_data;
1244 		mvm->nvm_data = NULL;
1245 	}
1246 
1247 #ifdef CONFIG_PM_SLEEP
1248 	/* fast_resume will be cleared by iwl_mvm_fast_resume */
1249 	fast_resume = mvm->fast_resume;
1250 
1251 	if (fast_resume) {
1252 		iwl_mvm_mei_device_state(mvm, true);
1253 		ret = iwl_mvm_fast_resume(mvm);
1254 		if (ret) {
1255 			iwl_mvm_stop_device(mvm);
1256 			/* iwl_mvm_up() will be called further down */
1257 		} else {
1258 			/*
1259 			 * We clear IWL_MVM_STATUS_FIRMWARE_RUNNING upon
1260 			 * mac_down() so that debugfs will stop honoring
1261 			 * requests after we flush all the workers.
1262 			 * Set the IWL_MVM_STATUS_FIRMWARE_RUNNING bit again
1263 			 * now that we are back. This is a bit abusing the
1264 			 * flag since the firmware wasn't really ever stopped,
1265 			 * but this still serves the purpose.
1266 			 */
1267 			set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1268 		}
1269 	}
1270 #endif /* CONFIG_PM_SLEEP */
1271 
1272 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1273 		/*
1274 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1275 		 * so later code will - from now on - see that we're doing it.
1276 		 */
1277 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1278 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1279 		/* Clean up some internal and mac80211 state on restart */
1280 		iwl_mvm_restart_cleanup(mvm);
1281 	}
1282 
1283 	/* we also want to load the firmware if fast_resume failed */
1284 	if (!fast_resume || ret)
1285 		ret = iwl_mvm_up(mvm);
1286 
1287 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1288 			       NULL);
1289 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1290 			       NULL);
1291 
1292 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1293 
1294 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1295 		/* Something went wrong - we need to finish some cleanup
1296 		 * that normally iwl_mvm_mac_restart_complete() below
1297 		 * would do.
1298 		 */
1299 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1300 	}
1301 
1302 	return ret;
1303 }
1304 
iwl_mvm_mac_start(struct ieee80211_hw * hw)1305 int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1306 {
1307 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1308 	int ret;
1309 	int retry, max_retry = 0;
1310 
1311 	mutex_lock(&mvm->mutex);
1312 
1313 	/* we are starting the mac not in error flow, and restart is enabled */
1314 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
1315 	    iwlwifi_mod_params.fw_restart)
1316 		max_retry = IWL_MAX_INIT_RETRY;
1317 
1318 	for (retry = 0; retry <= max_retry; retry++) {
1319 		ret = __iwl_mvm_mac_start(mvm);
1320 		if (ret != -ETIMEDOUT)
1321 			break;
1322 
1323 		IWL_ERR(mvm, "mac start retry %d\n", retry);
1324 	}
1325 
1326 	mutex_unlock(&mvm->mutex);
1327 
1328 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1329 
1330 	return ret;
1331 }
1332 
iwl_mvm_restart_complete(struct iwl_mvm * mvm)1333 static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1334 {
1335 	int ret;
1336 
1337 	guard(mvm)(mvm);
1338 
1339 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1340 
1341 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1342 	if (ret)
1343 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1344 			ret);
1345 
1346 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1347 
1348 	/*
1349 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1350 	 * of packets the FW sent out, so we must reconnect.
1351 	 */
1352 	iwl_mvm_teardown_tdls_peers(mvm);
1353 
1354 	IWL_INFO(mvm, "restart completed\n");
1355 	iwl_trans_finish_sw_reset(mvm->trans);
1356 
1357 	/* no need to lock, adding in parallel would schedule too */
1358 	if (!list_empty(&mvm->add_stream_txqs))
1359 		schedule_work(&mvm->add_stream_wk);
1360 }
1361 
iwl_mvm_mac_reconfig_complete(struct ieee80211_hw * hw,enum ieee80211_reconfig_type reconfig_type)1362 void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1363 				   enum ieee80211_reconfig_type reconfig_type)
1364 {
1365 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1366 
1367 	switch (reconfig_type) {
1368 	case IEEE80211_RECONFIG_TYPE_RESTART:
1369 		iwl_mvm_restart_complete(mvm);
1370 		break;
1371 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1372 		break;
1373 	}
1374 }
1375 
__iwl_mvm_mac_stop(struct iwl_mvm * mvm,bool suspend)1376 void __iwl_mvm_mac_stop(struct iwl_mvm *mvm, bool suspend)
1377 {
1378 	lockdep_assert_held(&mvm->mutex);
1379 
1380 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1381 
1382 	/* firmware counters are obviously reset now, but we shouldn't
1383 	 * partially track so also clear the fw_reset_accu counters.
1384 	 */
1385 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1386 
1387 	/* async_handlers_wk is now blocked */
1388 
1389 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1390 		iwl_mvm_rm_aux_sta(mvm);
1391 
1392 	if (suspend &&
1393 	    mvm->trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
1394 		iwl_mvm_fast_suspend(mvm);
1395 		/* From this point on, we won't touch the device */
1396 		iwl_mvm_mei_device_state(mvm, false);
1397 	} else {
1398 		iwl_mvm_stop_device(mvm);
1399 	}
1400 
1401 	iwl_mvm_async_handlers_purge(mvm);
1402 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1403 
1404 	/*
1405 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1406 	 * hw (as restart_complete() won't be called in this case) and mac80211
1407 	 * won't execute the restart.
1408 	 * But make sure to cleanup interfaces that have gone down before/during
1409 	 * HW restart was requested.
1410 	 */
1411 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1412 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1413 			       &mvm->status))
1414 		ieee80211_iterate_interfaces(mvm->hw, 0,
1415 					     iwl_mvm_cleanup_iterator, mvm);
1416 
1417 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1418 	 * make sure there's nothing left there and warn if any is found.
1419 	 */
1420 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1421 		int i;
1422 
1423 		for (i = 0; i < mvm->max_scans; i++) {
1424 			if (WARN_ONCE(mvm->scan_uid_status[i],
1425 				      "UMAC scan UID %d status was not cleaned\n",
1426 				      i))
1427 				mvm->scan_uid_status[i] = 0;
1428 		}
1429 	}
1430 }
1431 
iwl_mvm_mac_stop(struct ieee80211_hw * hw,bool suspend)1432 void iwl_mvm_mac_stop(struct ieee80211_hw *hw, bool suspend)
1433 {
1434 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1435 
1436 	/* Stop internal MLO scan, if running */
1437 	mutex_lock(&mvm->mutex);
1438 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
1439 	mutex_unlock(&mvm->mutex);
1440 
1441 	wiphy_work_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
1442 	wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1443 	flush_work(&mvm->async_handlers_wk);
1444 	flush_work(&mvm->add_stream_wk);
1445 
1446 	/*
1447 	 * Lock and clear the firmware running bit here already, so that
1448 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1449 	 * be able to proceed. This is important here because one of those
1450 	 * debugfs files causes the firmware dump to be triggered, and if we
1451 	 * don't stop debugfs accesses before canceling that it could be
1452 	 * retriggered after we flush it but before we've cleared the bit.
1453 	 */
1454 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1455 
1456 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1457 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1458 
1459 	/*
1460 	 * The work item could be running or queued if the
1461 	 * ROC time event stops just as we get here.
1462 	 */
1463 	flush_work(&mvm->roc_done_wk);
1464 
1465 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1466 
1467 	mutex_lock(&mvm->mutex);
1468 	__iwl_mvm_mac_stop(mvm, suspend);
1469 	mutex_unlock(&mvm->mutex);
1470 
1471 	/*
1472 	 * The worker might have been waiting for the mutex, let it run and
1473 	 * discover that its list is now empty.
1474 	 */
1475 	cancel_work_sync(&mvm->async_handlers_wk);
1476 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1477 }
1478 
iwl_mvm_get_free_phy_ctxt(struct iwl_mvm * mvm)1479 struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1480 {
1481 	u16 i;
1482 
1483 	lockdep_assert_held(&mvm->mutex);
1484 
1485 	for (i = 0; i < NUM_PHY_CTX; i++)
1486 		if (!mvm->phy_ctxts[i].ref)
1487 			return &mvm->phy_ctxts[i];
1488 
1489 	IWL_ERR(mvm, "No available PHY context\n");
1490 	return NULL;
1491 }
1492 
iwl_mvm_set_tx_power(struct iwl_mvm * mvm,struct ieee80211_bss_conf * link_conf,s16 tx_power)1493 int iwl_mvm_set_tx_power(struct iwl_mvm *mvm,
1494 			 struct ieee80211_bss_conf *link_conf,
1495 			 s16 tx_power)
1496 {
1497 	u32 cmd_id = REDUCE_TX_POWER_CMD;
1498 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(link_conf->vif);
1499 	u32 mac_id = mvmvif->id;
1500 	int len;
1501 	struct iwl_dev_tx_power_cmd_v3_v8 cmd = {
1502 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_LINK),
1503 		.common.link_id = cpu_to_le32(mac_id),
1504 	};
1505 	struct iwl_dev_tx_power_cmd cmd_v9_v10;
1506 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 3);
1507 	u16 u_tx_power = tx_power == IWL_DEFAULT_MAX_TX_POWER ?
1508 		IWL_DEV_MAX_TX_POWER : 8 * tx_power;
1509 	void *cmd_data = &cmd;
1510 
1511 	cmd.common.pwr_restriction = cpu_to_le16(u_tx_power);
1512 
1513 	if (cmd_ver > 8) {
1514 		u32 link_id;
1515 
1516 		if (WARN_ON(!mvmvif->link[link_conf->link_id]))
1517 			return -ENODEV;
1518 
1519 		link_id = mvmvif->link[link_conf->link_id]->fw_link_id;
1520 
1521 		/* Those fields sit on the same place for v9 and v10 */
1522 		cmd_v9_v10.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_LINK);
1523 		cmd_v9_v10.common.link_id = cpu_to_le32(link_id);
1524 		cmd_v9_v10.common.pwr_restriction = cpu_to_le16(u_tx_power);
1525 		cmd_data = &cmd_v9_v10;
1526 	}
1527 
1528 	if (cmd_ver == 10)
1529 		len = sizeof(cmd_v9_v10.v10);
1530 	else if (cmd_ver == 9)
1531 		len = sizeof(cmd_v9_v10.v9);
1532 	else if (cmd_ver == 8)
1533 		len = sizeof(cmd.v8);
1534 	else if (fw_has_api(&mvm->fw->ucode_capa,
1535 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1536 		len = sizeof(cmd.v5);
1537 	else if (fw_has_capa(&mvm->fw->ucode_capa,
1538 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1539 		len = sizeof(cmd.v4);
1540 	else
1541 		len = sizeof(cmd.v3);
1542 
1543 	/* all structs have the same common part, add its length */
1544 	len += sizeof(cmd.common);
1545 
1546 	if (cmd_ver < 9)
1547 		len += sizeof(cmd.per_band);
1548 
1549 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, cmd_data);
1550 
1551 }
1552 
iwl_mvm_post_csa_tx(void * data,struct ieee80211_sta * sta)1553 static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1554 {
1555 	struct ieee80211_hw *hw = data;
1556 	int i;
1557 
1558 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1559 		struct iwl_mvm_txq *mvmtxq =
1560 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1561 
1562 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1563 		iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1564 	}
1565 }
1566 
iwl_mvm_post_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1567 int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1568 				struct ieee80211_vif *vif,
1569 				struct ieee80211_bss_conf *link_conf)
1570 {
1571 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1572 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1573 	int ret;
1574 
1575 	mutex_lock(&mvm->mutex);
1576 
1577 	if (vif->type == NL80211_IFTYPE_STATION) {
1578 		struct iwl_mvm_sta *mvmsta;
1579 		unsigned int link_id = link_conf->link_id;
1580 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1581 
1582 		mvmvif->csa_bcn_pending = false;
1583 		mvmvif->csa_blocks_tx = false;
1584 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1585 
1586 		if (WARN_ON(!mvmsta)) {
1587 			ret = -EIO;
1588 			goto out_unlock;
1589 		}
1590 
1591 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1592 		if (mvm->mld_api_is_used)
1593 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1594 		else
1595 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1596 
1597 		if (!fw_has_capa(&mvm->fw->ucode_capa,
1598 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1599 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1600 			if (ret)
1601 				goto out_unlock;
1602 
1603 			iwl_mvm_stop_session_protection(mvm, vif);
1604 		}
1605 	} else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1606 		struct iwl_mvm_txq *mvmtxq =
1607 			iwl_mvm_txq_from_mac80211(vif->txq);
1608 
1609 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1610 
1611 		local_bh_disable();
1612 		iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1613 		ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1614 		local_bh_enable();
1615 
1616 		mvmvif->csa_blocks_tx = false;
1617 	}
1618 
1619 	mvmvif->ps_disabled = false;
1620 
1621 	ret = iwl_mvm_power_update_ps(mvm);
1622 
1623 out_unlock:
1624 	if (mvmvif->csa_failed)
1625 		ret = -EIO;
1626 	mutex_unlock(&mvm->mutex);
1627 
1628 	return ret;
1629 }
1630 
iwl_mvm_abort_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)1631 void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1632 				  struct ieee80211_vif *vif,
1633 				  struct ieee80211_bss_conf *link_conf)
1634 {
1635 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1636 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1637 	struct iwl_chan_switch_te_cmd cmd = {
1638 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1639 							  mvmvif->color)),
1640 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1641 	};
1642 
1643 	/*
1644 	 * In the new flow since FW is in charge of the timing,
1645 	 * if driver has canceled the channel switch he will receive the
1646 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1647 	 */
1648 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1649 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1650 		return;
1651 
1652 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1653 
1654 	mutex_lock(&mvm->mutex);
1655 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1656 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1657 		iwl_mvm_remove_csa_period(mvm, vif);
1658 	else
1659 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1660 					     WIDE_ID(MAC_CONF_GROUP,
1661 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1662 					     0, sizeof(cmd), &cmd));
1663 	mvmvif->csa_failed = true;
1664 	mutex_unlock(&mvm->mutex);
1665 
1666 	/* If we're here, we can't support MLD */
1667 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1668 }
1669 
iwl_mvm_channel_switch_disconnect_wk(struct work_struct * wk)1670 void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1671 {
1672 	struct iwl_mvm_vif *mvmvif;
1673 	struct ieee80211_vif *vif;
1674 
1675 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1676 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1677 
1678 	/* Trigger disconnect (should clear the CSA state) */
1679 	ieee80211_chswitch_done(vif, false, 0);
1680 }
1681 
1682 static u8
iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def * chandef)1683 iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1684 {
1685 	int data_start;
1686 	int control_start;
1687 	int bw;
1688 
1689 	if (chandef->width == NL80211_CHAN_WIDTH_320)
1690 		bw = 320;
1691 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
1692 		bw = 160;
1693 	else
1694 		return 0;
1695 
1696 	/* data is bw wide so the start is half the width */
1697 	data_start = chandef->center_freq1 - bw / 2;
1698 	/* control is 20Mhz width */
1699 	control_start = chandef->chan->center_freq - 10;
1700 
1701 	return (control_start - data_start) / 80;
1702 }
1703 
iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1704 static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
1705 					 struct ieee80211_vif *vif)
1706 {
1707 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1708 	int ret;
1709 
1710 	lockdep_assert_held(&mvm->mutex);
1711 
1712 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1713 	if (ret) {
1714 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1715 		return ret;
1716 	}
1717 
1718 	/* Only queue for this station is the mcast queue,
1719 	 * which shouldn't be in TFD mask anyway
1720 	 */
1721 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
1722 					vif->type,
1723 					IWL_STA_MULTICAST);
1724 }
1725 
iwl_mvm_prevent_esr_done_wk(struct wiphy * wiphy,struct wiphy_work * wk)1726 static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1727 					struct wiphy_work *wk)
1728 {
1729 	struct iwl_mvm_vif *mvmvif =
1730 		container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1731 	struct iwl_mvm *mvm = mvmvif->mvm;
1732 	struct ieee80211_vif *vif =
1733 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1734 
1735 	guard(mvm)(mvm);
1736 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1737 }
1738 
iwl_mvm_mlo_int_scan_wk(struct wiphy * wiphy,struct wiphy_work * wk)1739 static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
1740 {
1741 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
1742 						  mlo_int_scan_wk.work);
1743 	struct ieee80211_vif *vif =
1744 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1745 
1746 	guard(mvm)(mvmvif->mvm);
1747 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
1748 }
1749 
iwl_mvm_unblock_esr_tpt(struct wiphy * wiphy,struct wiphy_work * wk)1750 static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1751 {
1752 	struct iwl_mvm_vif *mvmvif =
1753 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1754 	struct iwl_mvm *mvm = mvmvif->mvm;
1755 	struct ieee80211_vif *vif =
1756 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1757 
1758 	guard(mvm)(mvm);
1759 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1760 }
1761 
iwl_mvm_unblock_esr_tmp_non_bss(struct wiphy * wiphy,struct wiphy_work * wk)1762 static void iwl_mvm_unblock_esr_tmp_non_bss(struct wiphy *wiphy,
1763 					    struct wiphy_work *wk)
1764 {
1765 	struct iwl_mvm_vif *mvmvif =
1766 		container_of(wk, struct iwl_mvm_vif,
1767 			     unblock_esr_tmp_non_bss_wk.work);
1768 	struct iwl_mvm *mvm = mvmvif->mvm;
1769 	struct ieee80211_vif *vif =
1770 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1771 
1772 	mutex_lock(&mvm->mutex);
1773 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TMP_NON_BSS);
1774 	mutex_unlock(&mvm->mutex);
1775 }
1776 
iwl_mvm_mac_init_mvmvif(struct iwl_mvm * mvm,struct iwl_mvm_vif * mvmvif)1777 void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
1778 {
1779 	lockdep_assert_held(&mvm->mutex);
1780 
1781 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1782 		return;
1783 
1784 	mvmvif->deflink.average_beacon_energy = 0;
1785 
1786 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1787 			  iwl_mvm_channel_switch_disconnect_wk);
1788 
1789 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1790 				iwl_mvm_prevent_esr_done_wk);
1791 
1792 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
1793 				iwl_mvm_mlo_int_scan_wk);
1794 
1795 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1796 			iwl_mvm_unblock_esr_tpt);
1797 
1798 	wiphy_delayed_work_init(&mvmvif->unblock_esr_tmp_non_bss_wk,
1799 				iwl_mvm_unblock_esr_tmp_non_bss);
1800 }
1801 
iwl_mvm_mac_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1802 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1803 				     struct ieee80211_vif *vif)
1804 {
1805 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1806 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1807 	int ret;
1808 	int i;
1809 
1810 	mutex_lock(&mvm->mutex);
1811 
1812 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
1813 
1814 	mvmvif->mvm = mvm;
1815 
1816 	/* the first link always points to the default one */
1817 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1818 	mvmvif->deflink.active = 0;
1819 	mvmvif->link[0] = &mvmvif->deflink;
1820 
1821 	vif->driver_flags = IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC;
1822 
1823 	iwl_mvm_set_link_fw_id(mvm, vif, &vif->bss_conf);
1824 
1825 	/*
1826 	 * Not much to do here. The stack will not allow interface
1827 	 * types or combinations that we didn't advertise, so we
1828 	 * don't really have to check the types.
1829 	 */
1830 
1831 	/* make sure that beacon statistics don't go backwards with FW reset */
1832 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1833 		for_each_mvm_vif_valid_link(mvmvif, i)
1834 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1835 				mvmvif->link[i]->beacon_stats.num_beacons;
1836 
1837 	/* Allocate resources for the MAC context, and add it to the fw  */
1838 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1839 	if (ret)
1840 		goto out;
1841 
1842 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1843 
1844 	/*
1845 	 * The AP binding flow can be done only after the beacon
1846 	 * template is configured (which happens only in the mac80211
1847 	 * start_ap() flow), and adding the broadcast station can happen
1848 	 * only after the binding.
1849 	 * In addition, since modifying the MAC before adding a bcast
1850 	 * station is not allowed by the FW, delay the adding of MAC context to
1851 	 * the point where we can also add the bcast station.
1852 	 * In short: there's not much we can do at this point, other than
1853 	 * allocating resources :)
1854 	 */
1855 	if (vif->type == NL80211_IFTYPE_AP ||
1856 	    vif->type == NL80211_IFTYPE_ADHOC) {
1857 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1858 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1859 		ret = 0;
1860 		goto out;
1861 	}
1862 
1863 	mvmvif->features |= hw->netdev_features;
1864 
1865 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1866 	if (ret)
1867 		goto out_unlock;
1868 
1869 	ret = iwl_mvm_power_update_mac(mvm);
1870 	if (ret)
1871 		goto out_remove_mac;
1872 
1873 	/* beacon filtering */
1874 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1875 	if (ret)
1876 		goto out_remove_mac;
1877 
1878 	if (!mvm->bf_allowed_vif &&
1879 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1880 		mvm->bf_allowed_vif = mvmvif;
1881 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1882 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1883 	}
1884 
1885 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1886 		mvm->p2p_device_vif = vif;
1887 
1888 	iwl_mvm_tcm_add_vif(mvm, vif);
1889 
1890 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1891 		mvm->monitor_on = true;
1892 		mvm->monitor_p80 =
1893 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1894 	}
1895 
1896 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1897 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1898 
1899 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1900 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1901 	    !mvm->csme_vif && mvm->mei_registered) {
1902 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1903 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1904 		mvm->csme_vif = vif;
1905 	}
1906 
1907 out:
1908 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1909 		     vif->type == NL80211_IFTYPE_ADHOC))
1910 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1911 
1912 	goto out_unlock;
1913 
1914  out_remove_mac:
1915 	mvmvif->deflink.phy_ctxt = NULL;
1916 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1917  out_unlock:
1918 	mutex_unlock(&mvm->mutex);
1919 
1920 	return ret;
1921 }
1922 
iwl_mvm_prepare_mac_removal(struct iwl_mvm * mvm,struct ieee80211_vif * vif)1923 void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1924 				 struct ieee80211_vif *vif)
1925 {
1926 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1927 
1928 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1929 		/*
1930 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1931 		 * We assume here that all the packets sent to the OFFCHANNEL
1932 		 * queue are sent in ROC session.
1933 		 */
1934 		flush_work(&mvm->roc_done_wk);
1935 	}
1936 
1937 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1938 				  &mvmvif->prevent_esr_done_wk);
1939 
1940 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1941 				  &mvmvif->mlo_int_scan_wk);
1942 
1943 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1944 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1945 				  &mvmvif->unblock_esr_tmp_non_bss_wk);
1946 
1947 	cancel_delayed_work_sync(&mvmvif->csa_work);
1948 }
1949 
iwl_mvm_mac_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1950 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1951 					 struct ieee80211_vif *vif)
1952 {
1953 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1954 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1955 	struct iwl_probe_resp_data *probe_data;
1956 
1957 	iwl_mvm_prepare_mac_removal(mvm, vif);
1958 
1959 	if (!(vif->type == NL80211_IFTYPE_AP ||
1960 	      vif->type == NL80211_IFTYPE_ADHOC))
1961 		iwl_mvm_tcm_rm_vif(mvm, vif);
1962 
1963 	mutex_lock(&mvm->mutex);
1964 
1965 	if (vif == mvm->csme_vif) {
1966 		iwl_mei_set_netdev(NULL);
1967 		mvm->csme_vif = NULL;
1968 	}
1969 
1970 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1971 					       lockdep_is_held(&mvm->mutex));
1972 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1973 	if (probe_data)
1974 		kfree_rcu(probe_data, rcu_head);
1975 
1976 	if (mvm->bf_allowed_vif == mvmvif) {
1977 		mvm->bf_allowed_vif = NULL;
1978 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1979 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1980 	}
1981 
1982 	if (vif->bss_conf.ftm_responder)
1983 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1984 
1985 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1986 
1987 	/*
1988 	 * For AP/GO interface, the tear down of the resources allocated to the
1989 	 * interface is be handled as part of the stop_ap flow.
1990 	 */
1991 	if (vif->type == NL80211_IFTYPE_AP ||
1992 	    vif->type == NL80211_IFTYPE_ADHOC)
1993 		goto out;
1994 
1995 	iwl_mvm_power_update_mac(mvm);
1996 
1997 	/* Before the interface removal, mac80211 would cancel the ROC, and the
1998 	 * ROC worker would be scheduled if needed. The worker would be flushed
1999 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
2000 	 * binding etc. so nothing needs to be done here.
2001 	 */
2002 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2003 		if (mvmvif->deflink.phy_ctxt) {
2004 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
2005 			mvmvif->deflink.phy_ctxt = NULL;
2006 		}
2007 		mvm->p2p_device_vif = NULL;
2008 	}
2009 
2010 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2011 
2012 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
2013 
2014 	if (vif->type == NL80211_IFTYPE_MONITOR)
2015 		mvm->monitor_on = false;
2016 
2017 out:
2018 	if (vif->type == NL80211_IFTYPE_AP ||
2019 	    vif->type == NL80211_IFTYPE_ADHOC) {
2020 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
2021 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
2022 	}
2023 
2024 	mutex_unlock(&mvm->mutex);
2025 }
2026 
2027 struct iwl_mvm_mc_iter_data {
2028 	struct iwl_mvm *mvm;
2029 	int port_id;
2030 };
2031 
iwl_mvm_mc_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)2032 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
2033 				      struct ieee80211_vif *vif)
2034 {
2035 	struct iwl_mvm_mc_iter_data *data = _data;
2036 	struct iwl_mvm *mvm = data->mvm;
2037 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
2038 	struct iwl_host_cmd hcmd = {
2039 		.id = MCAST_FILTER_CMD,
2040 		.flags = CMD_ASYNC,
2041 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
2042 	};
2043 	int ret, len;
2044 
2045 	/* if we don't have free ports, mcast frames will be dropped */
2046 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
2047 		return;
2048 
2049 	if (vif->type != NL80211_IFTYPE_STATION ||
2050 	    !vif->cfg.assoc)
2051 		return;
2052 
2053 	cmd->port_id = data->port_id++;
2054 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
2055 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
2056 
2057 	hcmd.len[0] = len;
2058 	hcmd.data[0] = cmd;
2059 
2060 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2061 	if (ret)
2062 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
2063 }
2064 
iwl_mvm_recalc_multicast(struct iwl_mvm * mvm)2065 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
2066 {
2067 	struct iwl_mvm_mc_iter_data iter_data = {
2068 		.mvm = mvm,
2069 	};
2070 	int ret;
2071 
2072 	lockdep_assert_held(&mvm->mutex);
2073 
2074 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2075 		return;
2076 
2077 	ieee80211_iterate_active_interfaces_atomic(
2078 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2079 		iwl_mvm_mc_iface_iterator, &iter_data);
2080 
2081 	/*
2082 	 * Send a (synchronous) ech command so that we wait for the
2083 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
2084 	 * the interface iterator. Otherwise, we might get here over
2085 	 * and over again (by userspace just sending a lot of these)
2086 	 * and the CPU can send them faster than the firmware can
2087 	 * process them.
2088 	 * Note that the CPU is still faster - but with this we'll
2089 	 * actually send fewer commands overall because the CPU will
2090 	 * not schedule the work in mac80211 as frequently if it's
2091 	 * still running when rescheduled (possibly multiple times).
2092 	 */
2093 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2094 	if (ret)
2095 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2096 }
2097 
iwl_mvm_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)2098 u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2099 			      struct netdev_hw_addr_list *mc_list)
2100 {
2101 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2102 	struct iwl_mcast_filter_cmd *cmd;
2103 	struct netdev_hw_addr *addr;
2104 	int addr_count;
2105 	bool pass_all;
2106 	int len;
2107 
2108 	addr_count = netdev_hw_addr_list_count(mc_list);
2109 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2110 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2111 	if (pass_all)
2112 		addr_count = 0;
2113 
2114 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2115 	cmd = kzalloc(len, GFP_ATOMIC);
2116 	if (!cmd)
2117 		return 0;
2118 
2119 	if (pass_all) {
2120 		cmd->pass_all = 1;
2121 #if defined(__linux__)
2122 		return (u64)(unsigned long)cmd;
2123 #elif defined(__FreeBSD__)
2124 		return (u64)(uintptr_t)cmd;
2125 #endif
2126 	}
2127 
2128 	netdev_hw_addr_list_for_each(addr, mc_list) {
2129 #if defined(__linux__)
2130 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2131 				   cmd->count, addr->addr);
2132 #elif defined(__FreeBSD__)
2133 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %6D\n",
2134 				   cmd->count, addr->addr, ":");
2135 #endif
2136 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2137 		       addr->addr, ETH_ALEN);
2138 		cmd->count++;
2139 	}
2140 
2141 #if defined(__linux__)
2142 	return (u64)(unsigned long)cmd;
2143 #elif defined(__FreeBSD__)
2144 	return (u64)(uintptr_t)cmd;
2145 #endif
2146 }
2147 
iwl_mvm_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)2148 void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2149 			      unsigned int changed_flags,
2150 			      unsigned int *total_flags, u64 multicast)
2151 {
2152 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2153 #if defined(__linux__)
2154 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2155 #elif defined(__FreeBSD__)
2156 	struct iwl_mcast_filter_cmd *cmd = (void *)(uintptr_t)multicast;
2157 #endif
2158 
2159 	guard(mvm)(mvm);
2160 
2161 	/* replace previous configuration */
2162 	kfree(mvm->mcast_filter_cmd);
2163 	mvm->mcast_filter_cmd = cmd;
2164 
2165 	if (!cmd)
2166 		goto out;
2167 
2168 	if (changed_flags & FIF_ALLMULTI)
2169 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
2170 
2171 	if (cmd->pass_all)
2172 		cmd->count = 0;
2173 
2174 	iwl_mvm_recalc_multicast(mvm);
2175 out:
2176 	*total_flags = 0;
2177 }
2178 
iwl_mvm_config_iface_filter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int filter_flags,unsigned int changed_flags)2179 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2180 					struct ieee80211_vif *vif,
2181 					unsigned int filter_flags,
2182 					unsigned int changed_flags)
2183 {
2184 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2185 
2186 	/* We support only filter for probe requests */
2187 	if (!(changed_flags & FIF_PROBE_REQ))
2188 		return;
2189 
2190 	/* Supported only for p2p client interfaces */
2191 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2192 	    !vif->p2p)
2193 		return;
2194 
2195 	guard(mvm)(mvm);
2196 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2197 }
2198 
iwl_mvm_update_mu_groups(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2199 int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2200 {
2201 	struct iwl_mu_group_mgmt_cmd cmd = {};
2202 
2203 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2204 	       WLAN_MEMBERSHIP_LEN);
2205 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2206 	       WLAN_USER_POSITION_LEN);
2207 
2208 	return iwl_mvm_send_cmd_pdu(mvm,
2209 				    WIDE_ID(DATA_PATH_GROUP,
2210 					    UPDATE_MU_GROUPS_CMD),
2211 				    0, sizeof(cmd), &cmd);
2212 }
2213 
iwl_mvm_mu_mimo_iface_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)2214 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2215 					   struct ieee80211_vif *vif)
2216 {
2217 	if (vif->bss_conf.mu_mimo_owner) {
2218 		struct iwl_mu_group_mgmt_notif *notif = _data;
2219 
2220 		/*
2221 		 * MU-MIMO Group Id action frame is little endian. We treat
2222 		 * the data received from firmware as if it came from the
2223 		 * action frame, so no conversion is needed.
2224 		 */
2225 		ieee80211_update_mu_groups(vif, 0,
2226 					   (u8 *)&notif->membership_status,
2227 					   (u8 *)&notif->user_position);
2228 	}
2229 }
2230 
iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)2231 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2232 			       struct iwl_rx_cmd_buffer *rxb)
2233 {
2234 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2235 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2236 
2237 	ieee80211_iterate_active_interfaces_atomic(
2238 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2239 			iwl_mvm_mu_mimo_iface_iterator, notif);
2240 }
2241 
iwl_mvm_he_get_ppe_val(u8 * ppe,u8 ppe_pos_bit)2242 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2243 {
2244 	u8 byte_num = ppe_pos_bit / 8;
2245 	u8 bit_num = ppe_pos_bit % 8;
2246 	u8 residue_bits;
2247 	u8 res;
2248 
2249 	if (bit_num <= 5)
2250 		return (ppe[byte_num] >> bit_num) &
2251 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2252 
2253 	/*
2254 	 * If bit_num > 5, we have to combine bits with next byte.
2255 	 * Calculate how many bits we need to take from current byte (called
2256 	 * here "residue_bits"), and add them to bits from next byte.
2257 	 */
2258 
2259 	residue_bits = 8 - bit_num;
2260 
2261 	res = (ppe[byte_num + 1] &
2262 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2263 	      residue_bits;
2264 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2265 
2266 	return res;
2267 }
2268 
iwl_mvm_parse_ppe(struct iwl_mvm * mvm,struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nss,u8 ru_index_bitmap,u8 * ppe,u8 ppe_pos_bit,bool inheritance)2269 static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2270 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2271 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2272 			      bool inheritance)
2273 {
2274 	int i;
2275 
2276 	/*
2277 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2278 	*
2279 	* If nss > MAX: we can ignore values we don't support
2280 	* If nss < MAX: we can set zeros in other streams
2281 	*/
2282 	if (nss > MAX_HE_SUPP_NSS) {
2283 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2284 			       MAX_HE_SUPP_NSS);
2285 		nss = MAX_HE_SUPP_NSS;
2286 	}
2287 
2288 	for (i = 0; i < nss; i++) {
2289 		u8 ru_index_tmp = ru_index_bitmap << 1;
2290 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2291 		u8 bw;
2292 
2293 		for (bw = 0;
2294 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2295 		     bw++) {
2296 			ru_index_tmp >>= 1;
2297 
2298 			/*
2299 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2300 			* isn't present - it should inherit the thresholds from the last
2301 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2302 			* this inheritance - continue in this case
2303 			*/
2304 			if (!(ru_index_tmp & 1)) {
2305 				if (inheritance)
2306 					goto set_thresholds;
2307 				else
2308 					continue;
2309 			}
2310 
2311 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2312 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2313 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2314 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2315 
2316 set_thresholds:
2317 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2318 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2319 		}
2320 	}
2321 }
2322 
iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm * mvm,struct ieee80211_link_sta * link_sta,struct iwl_he_pkt_ext_v2 * pkt_ext,bool inheritance)2323 static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2324 					    struct ieee80211_link_sta *link_sta,
2325 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2326 					    bool inheritance)
2327 {
2328 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
2329 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2330 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2331 	u8 ru_index_bitmap =
2332 		u8_get_bits(*ppe,
2333 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2334 	/* Starting after PPE header */
2335 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2336 
2337 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2338 			  inheritance);
2339 }
2340 
2341 static int
iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nominal_padding)2342 iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2343 					 u8 nominal_padding)
2344 {
2345 	int low_th = -1;
2346 	int high_th = -1;
2347 	int i;
2348 
2349 	/* all the macros are the same for EHT and HE */
2350 	switch (nominal_padding) {
2351 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2352 		low_th = IWL_HE_PKT_EXT_NONE;
2353 		high_th = IWL_HE_PKT_EXT_NONE;
2354 		break;
2355 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2356 		low_th = IWL_HE_PKT_EXT_BPSK;
2357 		high_th = IWL_HE_PKT_EXT_NONE;
2358 		break;
2359 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2360 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2361 		low_th = IWL_HE_PKT_EXT_NONE;
2362 		high_th = IWL_HE_PKT_EXT_BPSK;
2363 		break;
2364 	}
2365 
2366 	if (low_th < 0 || high_th < 0)
2367 		return -EINVAL;
2368 
2369 	/* Set the PPE thresholds accordingly */
2370 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2371 		u8 bw;
2372 
2373 		for (bw = 0;
2374 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2375 			bw++) {
2376 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2377 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2378 		}
2379 	}
2380 
2381 	return 0;
2382 }
2383 
iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 * pkt_ext,u8 nominal_padding)2384 static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2385 					 u8 nominal_padding)
2386 {
2387 	int i;
2388 
2389 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2390 		u8 bw;
2391 
2392 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2393 		     bw++) {
2394 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2395 
2396 			if (nominal_padding >
2397 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2398 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2399 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2400 			else if (nominal_padding ==
2401 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2402 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2403 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2404 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2405 		}
2406 	}
2407 }
2408 
2409 /* Set the pkt_ext field according to PPE Thresholds element */
iwl_mvm_set_sta_pkt_ext(struct iwl_mvm * mvm,struct ieee80211_link_sta * link_sta,struct iwl_he_pkt_ext_v2 * pkt_ext)2410 int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2411 			    struct ieee80211_link_sta *link_sta,
2412 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
2413 {
2414 	u8 nominal_padding;
2415 	int i, ret = 0;
2416 
2417 	if (WARN_ON(!link_sta))
2418 		return -EINVAL;
2419 
2420 	/* Initialize the PPE thresholds to "None" (7), as described in Table
2421 	 * 9-262ac of 80211.ax/D3.0.
2422 	 */
2423 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2424 	       sizeof(struct iwl_he_pkt_ext_v2));
2425 
2426 	if (link_sta->eht_cap.has_eht) {
2427 		nominal_padding =
2428 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2429 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2430 
2431 		/* If PPE Thresholds exists, parse them into a FW-familiar
2432 		 * format.
2433 		 */
2434 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2435 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2436 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2437 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2438 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2439 			u8 ru_index_bitmap =
2440 				u16_get_bits(*ppe,
2441 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2442 			 /* Starting after PPE header */
2443 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2444 
2445 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2446 					  ppe, ppe_pos_bit, true);
2447 		/* EHT PPE Thresholds doesn't exist - set the API according to
2448 		 * HE PPE Tresholds
2449 		 */
2450 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2451 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2452 			/* Even though HE Capabilities IE doesn't contain PPE
2453 			 * Thresholds for BW 320Mhz, thresholds for this BW will
2454 			 * be filled in with the same values as 160Mhz, due to
2455 			 * the inheritance, as required.
2456 			 */
2457 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2458 							true);
2459 
2460 			/* According to the requirements, for MCSs 12-13 the
2461 			 * maximum value between HE PPE Threshold and Common
2462 			 * Nominal Packet Padding needs to be taken
2463 			 */
2464 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2465 
2466 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2467 		 * take the Thresholds from Common Nominal Packet Padding field
2468 		 */
2469 		} else {
2470 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2471 								       nominal_padding);
2472 		}
2473 	} else if (link_sta->he_cap.has_he) {
2474 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2475 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2476 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2477 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2478 							false);
2479 		/* PPE Thresholds doesn't exist - set the API PPE values
2480 		 * according to Common Nominal Packet Padding field.
2481 		 */
2482 		} else {
2483 			nominal_padding =
2484 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2485 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2486 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2487 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2488 									       nominal_padding);
2489 		}
2490 	}
2491 
2492 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2493 		int bw;
2494 
2495 		for (bw = 0;
2496 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2497 		     bw++) {
2498 			u8 *qam_th =
2499 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
2500 
2501 			IWL_DEBUG_HT(mvm,
2502 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2503 				     i, bw, qam_th[0], qam_th[1]);
2504 		}
2505 	}
2506 	return ret;
2507 }
2508 
2509 /*
2510  * This function sets the MU EDCA parameters ans returns whether MU EDCA
2511  * is enabled or not
2512  */
iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm * mvm,const struct iwl_mvm_vif_link_info * link_info,struct iwl_he_backoff_conf * trig_based_txf)2513 bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2514 				   const struct iwl_mvm_vif_link_info *link_info,
2515 				   struct iwl_he_backoff_conf *trig_based_txf)
2516 {
2517 	int i;
2518 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2519 	bool mu_edca_enabled = true;
2520 
2521 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2522 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2523 			&link_info->queue_params[i].mu_edca_param_rec;
2524 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2525 
2526 		if (!link_info->queue_params[i].mu_edca) {
2527 			mu_edca_enabled = false;
2528 			break;
2529 		}
2530 
2531 		trig_based_txf[ac].cwmin =
2532 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2533 		trig_based_txf[ac].cwmax =
2534 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2535 		trig_based_txf[ac].aifsn =
2536 			cpu_to_le16(mu_edca->aifsn & 0xf);
2537 		trig_based_txf[ac].mu_time =
2538 			cpu_to_le16(mu_edca->mu_edca_timer);
2539 	}
2540 
2541 	return mu_edca_enabled;
2542 }
2543 
iwl_mvm_is_nic_ack_enabled(struct iwl_mvm * mvm,struct ieee80211_vif * vif)2544 bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2545 {
2546 	const struct ieee80211_supported_band *sband;
2547 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2548 
2549 	/* This capability is the same for all bands,
2550 	 * so take it from one of them.
2551 	 */
2552 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2553 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2554 
2555 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2556 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
2557 }
2558 
iwl_mvm_get_sta_htc_flags(struct ieee80211_sta * sta,struct ieee80211_link_sta * link_sta)2559 __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2560 				 struct ieee80211_link_sta *link_sta)
2561 {
2562 	u8 *mac_cap_info =
2563 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2564 	__le32 htc_flags = 0;
2565 
2566 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2567 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2568 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2569 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2570 		u8 link_adap =
2571 			((mac_cap_info[2] &
2572 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2573 			 (mac_cap_info[1] &
2574 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2575 
2576 		if (link_adap == 2)
2577 			htc_flags |=
2578 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2579 		else if (link_adap == 3)
2580 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2581 	}
2582 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2583 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2584 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2585 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2586 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2587 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2588 
2589 	return htc_flags;
2590 }
2591 
iwl_mvm_cfg_he_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u8 sta_id)2592 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2593 			       struct ieee80211_vif *vif, u8 sta_id)
2594 {
2595 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2596 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2597 		.sta_id = sta_id,
2598 		.tid_limit = IWL_MAX_TID_COUNT,
2599 		.bss_color = vif->bss_conf.he_bss_color.color,
2600 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2601 		.frame_time_rts_th =
2602 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2603 	};
2604 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2605 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2606 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2607 	int size;
2608 	struct ieee80211_sta *sta;
2609 	u32 flags;
2610 	int i;
2611 	void *cmd;
2612 
2613 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2614 		ver = 1;
2615 
2616 	switch (ver) {
2617 	case 1:
2618 		/* same layout as v2 except some data at the end */
2619 		cmd = &sta_ctxt_cmd_v2;
2620 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
2621 		break;
2622 	case 2:
2623 		cmd = &sta_ctxt_cmd_v2;
2624 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
2625 		break;
2626 	case 3:
2627 		cmd = &sta_ctxt_cmd;
2628 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
2629 		break;
2630 	default:
2631 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2632 		return;
2633 	}
2634 
2635 	rcu_read_lock();
2636 
2637 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2638 	if (IS_ERR_OR_NULL(sta)) {
2639 		rcu_read_unlock();
2640 		WARN(1, "Can't find STA to configure HE\n");
2641 		return;
2642 	}
2643 
2644 	if (!sta->deflink.he_cap.has_he) {
2645 		rcu_read_unlock();
2646 		return;
2647 	}
2648 
2649 	flags = 0;
2650 
2651 	/* Block 26-tone RU OFDMA transmissions */
2652 	if (mvmvif->deflink.he_ru_2mhz_block)
2653 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2654 
2655 	/* HTC flags */
2656 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2657 
2658 	/* PPE Thresholds */
2659 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2660 		flags |= STA_CTXT_HE_PACKET_EXT;
2661 
2662 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2663 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2664 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2665 
2666 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2667 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2668 		flags |= STA_CTXT_HE_ACK_ENABLED;
2669 
2670 	rcu_read_unlock();
2671 
2672 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2673 					  &sta_ctxt_cmd.trig_based_txf[0]))
2674 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2675 
2676 	if (vif->bss_conf.uora_exists) {
2677 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2678 
2679 		sta_ctxt_cmd.rand_alloc_ecwmin =
2680 			vif->bss_conf.uora_ocw_range & 0x7;
2681 		sta_ctxt_cmd.rand_alloc_ecwmax =
2682 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2683 	}
2684 
2685 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2686 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2687 
2688 	if (vif->bss_conf.nontransmitted) {
2689 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2690 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2691 				vif->bss_conf.transmitter_bssid);
2692 		sta_ctxt_cmd.max_bssid_indicator =
2693 			vif->bss_conf.bssid_indicator;
2694 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2695 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2696 		sta_ctxt_cmd.profile_periodicity =
2697 			vif->bss_conf.profile_periodicity;
2698 	}
2699 
2700 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2701 
2702 	if (ver < 3) {
2703 		/* fields before pkt_ext */
2704 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2705 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2706 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2707 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2708 
2709 		/* pkt_ext */
2710 		for (i = 0;
2711 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2712 		     i++) {
2713 			u8 bw;
2714 
2715 			for (bw = 0;
2716 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2717 			     bw++) {
2718 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2719 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2720 
2721 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2722 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2723 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2724 			}
2725 		}
2726 
2727 		/* fields after pkt_ext */
2728 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2729 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2730 			     sizeof(sta_ctxt_cmd_v2) -
2731 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2732 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
2733 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2734 		       (u8 *)&sta_ctxt_cmd +
2735 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2736 		       sizeof(sta_ctxt_cmd) -
2737 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2738 		sta_ctxt_cmd_v2.reserved3 = 0;
2739 	}
2740 
2741 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2742 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2743 }
2744 
iwl_mvm_protect_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u32 duration_override,unsigned int link_id)2745 void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2746 			   u32 duration_override, unsigned int link_id)
2747 {
2748 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2749 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2750 
2751 	if (duration_override > duration)
2752 		duration = duration_override;
2753 
2754 	/* Try really hard to protect the session and hear a beacon
2755 	 * The new session protection command allows us to protect the
2756 	 * session for a much longer time since the firmware will internally
2757 	 * create two events: a 300TU one with a very high priority that
2758 	 * won't be fragmented which should be enough for 99% of the cases,
2759 	 * and another one (which we configure here to be 900TU long) which
2760 	 * will have a slightly lower priority, but more importantly, can be
2761 	 * fragmented so that it'll allow other activities to run.
2762 	 */
2763 	if (fw_has_capa(&mvm->fw->ucode_capa,
2764 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2765 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2766 						    min_duration, false,
2767 						    link_id);
2768 	else
2769 		iwl_mvm_protect_session(mvm, vif, duration,
2770 					min_duration, 500, false);
2771 }
2772 
2773 /* Handle association common part to MLD and non-MLD modes */
iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u64 changes)2774 void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2775 					    struct ieee80211_vif *vif,
2776 					    u64 changes)
2777 {
2778 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2779 	int ret;
2780 	int link_id;
2781 
2782 	/* The firmware tracks the MU-MIMO group on its own.
2783 	 * However, on HW restart we should restore this data.
2784 	 */
2785 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2786 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2787 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2788 		if (ret)
2789 			IWL_ERR(mvm,
2790 				"failed to update VHT MU_MIMO groups\n");
2791 	}
2792 
2793 	iwl_mvm_recalc_multicast(mvm);
2794 
2795 	/* reset rssi values */
2796 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2797 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2798 
2799 	iwl_mvm_bt_coex_vif_change(mvm);
2800 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2801 					    IEEE80211_SMPS_AUTOMATIC);
2802 	if (fw_has_capa(&mvm->fw->ucode_capa,
2803 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2804 		iwl_mvm_config_scan(mvm);
2805 }
2806 
2807 /* Execute the common part for MLD and non-MLD modes */
2808 void
iwl_mvm_bss_info_changed_station_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,u64 changes)2809 iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2810 					struct ieee80211_vif *vif,
2811 					struct ieee80211_bss_conf *link_conf,
2812 					u64 changes)
2813 {
2814 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2815 	int ret;
2816 
2817 	if (changes & BSS_CHANGED_BEACON_INFO) {
2818 		/* We received a beacon from the associated AP so
2819 		 * remove the session protection.
2820 		 */
2821 		iwl_mvm_stop_session_protection(mvm, vif);
2822 
2823 		iwl_mvm_sf_update(mvm, vif, false);
2824 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2825 	}
2826 
2827 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2828 		       /* Send power command on every beacon change,
2829 			* because we may have not enabled beacon abort yet.
2830 			*/
2831 		       BSS_CHANGED_BEACON_INFO)) {
2832 		ret = iwl_mvm_power_update_mac(mvm);
2833 		if (ret)
2834 			IWL_ERR(mvm, "failed to update power mode\n");
2835 	}
2836 
2837 	if (changes & BSS_CHANGED_CQM) {
2838 		struct iwl_mvm_vif_link_info *link_info =
2839 			mvmvif->link[link_conf->link_id];
2840 
2841 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2842 		if (link_info)
2843 			link_info->bf_data.last_cqm_event = 0;
2844 
2845 		if (mvmvif->bf_enabled) {
2846 			/* FIXME: need to update per link when FW API will
2847 			 * support it
2848 			 */
2849 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2850 			if (ret)
2851 				IWL_ERR(mvm,
2852 					"failed to update CQM thresholds\n");
2853 		}
2854 	}
2855 
2856 	if (changes & BSS_CHANGED_BANDWIDTH)
2857 		iwl_mvm_update_link_smps(vif, link_conf);
2858 
2859 	if (changes & BSS_CHANGED_TPE) {
2860 		IWL_DEBUG_CALIB(mvm, "Changing TPE\n");
2861 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
2862 							link_conf,
2863 							false);
2864 	}
2865 }
2866 
iwl_mvm_bss_info_changed_station(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)2867 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2868 					     struct ieee80211_vif *vif,
2869 					     struct ieee80211_bss_conf *bss_conf,
2870 					     u64 changes)
2871 {
2872 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2873 	int ret;
2874 	int i;
2875 
2876 	/*
2877 	 * Re-calculate the tsf id, as the leader-follower relations depend
2878 	 * on the beacon interval, which was not known when the station
2879 	 * interface was added.
2880 	 */
2881 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2882 		if ((vif->bss_conf.he_support &&
2883 		     !iwlwifi_mod_params.disable_11ax))
2884 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2885 
2886 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2887 	}
2888 
2889 	/* Update MU EDCA params */
2890 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2891 	    vif->cfg.assoc &&
2892 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2893 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2894 
2895 	/*
2896 	 * If we're not associated yet, take the (new) BSSID before associating
2897 	 * so the firmware knows. If we're already associated, then use the old
2898 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2899 	 * branch for disassociation below.
2900 	 */
2901 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2902 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2903 
2904 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2905 	if (ret)
2906 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2907 
2908 	/* after sending it once, adopt mac80211 data */
2909 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2910 	mvmvif->associated = vif->cfg.assoc;
2911 
2912 	if (changes & BSS_CHANGED_ASSOC) {
2913 		if (vif->cfg.assoc) {
2914 			mvmvif->session_prot_connection_loss = false;
2915 
2916 			/* clear statistics to get clean beacon counter */
2917 			iwl_mvm_request_statistics(mvm, true);
2918 			for_each_mvm_vif_valid_link(mvmvif, i)
2919 				memset(&mvmvif->link[i]->beacon_stats, 0,
2920 				       sizeof(mvmvif->link[i]->beacon_stats));
2921 
2922 			/* add quota for this interface */
2923 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2924 			if (ret) {
2925 				IWL_ERR(mvm, "failed to update quotas\n");
2926 				return;
2927 			}
2928 
2929 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2930 				     &mvm->status) &&
2931 			    !fw_has_capa(&mvm->fw->ucode_capa,
2932 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2933 				/*
2934 				 * If we're restarting then the firmware will
2935 				 * obviously have lost synchronisation with
2936 				 * the AP. It will attempt to synchronise by
2937 				 * itself, but we can make it more reliable by
2938 				 * scheduling a session protection time event.
2939 				 *
2940 				 * The firmware needs to receive a beacon to
2941 				 * catch up with synchronisation, use 110% of
2942 				 * the beacon interval.
2943 				 *
2944 				 * Set a large maximum delay to allow for more
2945 				 * than a single interface.
2946 				 *
2947 				 * For new firmware versions, rely on the
2948 				 * firmware. This is relevant for DCM scenarios
2949 				 * only anyway.
2950 				 */
2951 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2952 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2953 							5 * dur, false);
2954 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2955 					     &mvm->status) &&
2956 				   !vif->bss_conf.dtim_period) {
2957 				/*
2958 				 * If we're not restarting and still haven't
2959 				 * heard a beacon (dtim period unknown) then
2960 				 * make sure we still have enough minimum time
2961 				 * remaining in the time event, since the auth
2962 				 * might actually have taken quite a while
2963 				 * (especially for SAE) and so the remaining
2964 				 * time could be small without us having heard
2965 				 * a beacon yet.
2966 				 */
2967 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2968 			}
2969 
2970 			iwl_mvm_sf_update(mvm, vif, false);
2971 			iwl_mvm_power_vif_assoc(mvm, vif);
2972 			if (vif->p2p) {
2973 				iwl_mvm_update_smps(mvm, vif,
2974 						    IWL_MVM_SMPS_REQ_PROT,
2975 						    IEEE80211_SMPS_DYNAMIC, 0);
2976 			}
2977 		} else if (mvmvif->deflink.ap_sta_id != IWL_INVALID_STA) {
2978 			iwl_mvm_mei_host_disassociated(mvm);
2979 			/*
2980 			 * If update fails - SF might be running in associated
2981 			 * mode while disassociated - which is forbidden.
2982 			 */
2983 			ret = iwl_mvm_sf_update(mvm, vif, false);
2984 			WARN_ONCE(ret &&
2985 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2986 					    &mvm->status),
2987 				  "Failed to update SF upon disassociation\n");
2988 
2989 			/* remove quota for this interface */
2990 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2991 			if (ret)
2992 				IWL_ERR(mvm, "failed to update quotas\n");
2993 
2994 			/* this will take the cleared BSSID from bss_conf */
2995 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2996 			if (ret)
2997 				IWL_ERR(mvm,
2998 					"failed to update MAC %pM (clear after unassoc)\n",
2999 					vif->addr);
3000 		}
3001 
3002 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
3003 	}
3004 
3005 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
3006 						changes);
3007 }
3008 
iwl_mvm_start_ap_ibss_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int * ret)3009 bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
3010 				  struct ieee80211_vif *vif,
3011 				  int *ret)
3012 {
3013 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3014 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3015 	int i;
3016 
3017 	lockdep_assert_held(&mvm->mutex);
3018 
3019 	mvmvif->ap_assoc_sta_count = 0;
3020 
3021 	/* must be set before quota calculations */
3022 	mvmvif->ap_ibss_active = true;
3023 
3024 	/* send all the early keys to the device now */
3025 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3026 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
3027 
3028 		if (!key)
3029 			continue;
3030 
3031 		mvmvif->ap_early_keys[i] = NULL;
3032 
3033 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
3034 		if (*ret)
3035 			return true;
3036 	}
3037 
3038 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3039 		iwl_mvm_vif_set_low_latency(mvmvif, true,
3040 					    LOW_LATENCY_VIF_TYPE);
3041 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
3042 	}
3043 
3044 	/* power updated needs to be done before quotas */
3045 	iwl_mvm_power_update_mac(mvm);
3046 
3047 	return false;
3048 }
3049 
iwl_mvm_start_ap_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3050 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
3051 				 struct ieee80211_vif *vif,
3052 				 struct ieee80211_bss_conf *link_conf)
3053 {
3054 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3055 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3056 	int ret;
3057 
3058 	mutex_lock(&mvm->mutex);
3059 
3060 	/*
3061 	 * Re-calculate the tsf id, as the leader-follower relations depend on
3062 	 * the beacon interval, which was not known when the AP interface
3063 	 * was added.
3064 	 */
3065 	if (vif->type == NL80211_IFTYPE_AP)
3066 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3067 
3068 	/* For older devices need to send beacon template before adding mac
3069 	 * context. For the newer, the beacon is a resource that belongs to a
3070 	 * MAC, so need to send beacon template after adding the mac.
3071 	 */
3072 	if (mvm->trans->mac_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3073 		/* Add the mac context */
3074 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3075 		if (ret)
3076 			goto out_unlock;
3077 
3078 		/* Send the beacon template */
3079 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3080 		if (ret)
3081 			goto out_unlock;
3082 	} else {
3083 		/* Send the beacon template */
3084 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3085 		if (ret)
3086 			goto out_unlock;
3087 
3088 		/* Add the mac context */
3089 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3090 		if (ret)
3091 			goto out_unlock;
3092 	}
3093 
3094 	/* Perform the binding */
3095 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3096 	if (ret)
3097 		goto out_remove;
3098 
3099 	/*
3100 	 * This is not very nice, but the simplest:
3101 	 * For older FWs adding the mcast sta before the bcast station may
3102 	 * cause assert 0x2b00.
3103 	 * This is fixed in later FW so make the order of removal depend on
3104 	 * the TLV
3105 	 */
3106 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3107 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3108 		if (ret)
3109 			goto out_unbind;
3110 		/*
3111 		 * Send the bcast station. At this stage the TBTT and DTIM time
3112 		 * events are added and applied to the scheduler
3113 		 */
3114 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3115 		if (ret) {
3116 			iwl_mvm_rm_mcast_sta(mvm, vif);
3117 			goto out_unbind;
3118 		}
3119 	} else {
3120 		/*
3121 		 * Send the bcast station. At this stage the TBTT and DTIM time
3122 		 * events are added and applied to the scheduler
3123 		 */
3124 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3125 		if (ret)
3126 			goto out_unbind;
3127 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3128 		if (ret) {
3129 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
3130 			goto out_unbind;
3131 		}
3132 	}
3133 
3134 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3135 		goto out_failed;
3136 
3137 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3138 	if (ret)
3139 		goto out_failed;
3140 
3141 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3142 	if (vif->p2p && mvm->p2p_device_vif)
3143 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3144 
3145 	iwl_mvm_bt_coex_vif_change(mvm);
3146 
3147 	/* we don't support TDLS during DCM */
3148 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3149 		iwl_mvm_teardown_tdls_peers(mvm);
3150 
3151 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3152 
3153 	goto out_unlock;
3154 
3155 out_failed:
3156 	iwl_mvm_power_update_mac(mvm);
3157 	mvmvif->ap_ibss_active = false;
3158 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3159 	iwl_mvm_rm_mcast_sta(mvm, vif);
3160 out_unbind:
3161 	iwl_mvm_binding_remove_vif(mvm, vif);
3162 out_remove:
3163 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3164 out_unlock:
3165 	mutex_unlock(&mvm->mutex);
3166 	return ret;
3167 }
3168 
iwl_mvm_start_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3169 static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3170 			    struct ieee80211_vif *vif,
3171 			    struct ieee80211_bss_conf *link_conf)
3172 {
3173 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3174 }
3175 
iwl_mvm_start_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3176 static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3177 			      struct ieee80211_vif *vif)
3178 {
3179 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3180 }
3181 
3182 /* Common part for MLD and non-MLD ops */
iwl_mvm_stop_ap_ibss_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3183 void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3184 				 struct ieee80211_vif *vif)
3185 {
3186 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3187 
3188 	lockdep_assert_held(&mvm->mutex);
3189 
3190 	iwl_mvm_prepare_mac_removal(mvm, vif);
3191 
3192 	/* Handle AP stop while in CSA */
3193 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3194 		iwl_mvm_remove_time_event(mvm, mvmvif,
3195 					  &mvmvif->time_event_data);
3196 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3197 		mvmvif->csa_countdown = false;
3198 	}
3199 
3200 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3201 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3202 		mvm->csa_tx_block_bcn_timeout = 0;
3203 	}
3204 
3205 	mvmvif->ap_ibss_active = false;
3206 	mvm->ap_last_beacon_gp2 = 0;
3207 
3208 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3209 		iwl_mvm_vif_set_low_latency(mvmvif, false,
3210 					    LOW_LATENCY_VIF_TYPE);
3211 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
3212 	}
3213 
3214 	iwl_mvm_bt_coex_vif_change(mvm);
3215 }
3216 
iwl_mvm_stop_ap_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3217 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3218 				 struct ieee80211_vif *vif,
3219 				 struct ieee80211_bss_conf *link_conf)
3220 {
3221 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3222 
3223 	guard(mvm)(mvm);
3224 
3225 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3226 
3227 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3228 	if (vif->p2p && mvm->p2p_device_vif)
3229 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3230 
3231 	iwl_mvm_update_quotas(mvm, false, NULL);
3232 
3233 	iwl_mvm_ftm_responder_clear(mvm, vif);
3234 
3235 	/*
3236 	 * This is not very nice, but the simplest:
3237 	 * For older FWs removing the mcast sta before the bcast station may
3238 	 * cause assert 0x2b00.
3239 	 * This is fixed in later FW (which will stop beaconing when removing
3240 	 * bcast station).
3241 	 * So make the order of removal depend on the TLV
3242 	 */
3243 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3244 		iwl_mvm_rm_mcast_sta(mvm, vif);
3245 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3246 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3247 		iwl_mvm_rm_mcast_sta(mvm, vif);
3248 	iwl_mvm_binding_remove_vif(mvm, vif);
3249 
3250 	iwl_mvm_power_update_mac(mvm);
3251 
3252 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3253 }
3254 
iwl_mvm_stop_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf)3255 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3256 			    struct ieee80211_vif *vif,
3257 			    struct ieee80211_bss_conf *link_conf)
3258 {
3259 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3260 }
3261 
iwl_mvm_stop_ibss(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3262 static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3263 			      struct ieee80211_vif *vif)
3264 {
3265 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3266 }
3267 
3268 static void
iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)3269 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3270 				 struct ieee80211_vif *vif,
3271 				 struct ieee80211_bss_conf *bss_conf,
3272 				 u64 changes)
3273 {
3274 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3275 
3276 	/* Changes will be applied when the AP/IBSS is started */
3277 	if (!mvmvif->ap_ibss_active)
3278 		return;
3279 
3280 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3281 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3282 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3283 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3284 
3285 	/* Need to send a new beacon template to the FW */
3286 	if (changes & BSS_CHANGED_BEACON &&
3287 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3288 		IWL_WARN(mvm, "Failed updating beacon data\n");
3289 
3290 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3291 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3292 
3293 		if (ret)
3294 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3295 				 ret);
3296 	}
3297 
3298 }
3299 
iwl_mvm_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changes)3300 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3301 				     struct ieee80211_vif *vif,
3302 				     struct ieee80211_bss_conf *bss_conf,
3303 				     u64 changes)
3304 {
3305 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3306 
3307 	guard(mvm)(mvm);
3308 
3309 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3310 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3311 
3312 	switch (vif->type) {
3313 	case NL80211_IFTYPE_STATION:
3314 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3315 		break;
3316 	case NL80211_IFTYPE_AP:
3317 	case NL80211_IFTYPE_ADHOC:
3318 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3319 		break;
3320 	case NL80211_IFTYPE_MONITOR:
3321 		if (changes & BSS_CHANGED_MU_GROUPS)
3322 			iwl_mvm_update_mu_groups(mvm, vif);
3323 		break;
3324 	default:
3325 		/* shouldn't happen */
3326 		WARN_ON_ONCE(1);
3327 	}
3328 
3329 	if (changes & BSS_CHANGED_TXPOWER) {
3330 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3331 				bss_conf->txpower);
3332 		iwl_mvm_set_tx_power(mvm, bss_conf, bss_conf->txpower);
3333 	}
3334 }
3335 
iwl_mvm_mac_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_scan_request * hw_req)3336 int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3337 			struct ieee80211_scan_request *hw_req)
3338 {
3339 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3340 
3341 	if (hw_req->req.n_channels == 0 ||
3342 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3343 		return -EINVAL;
3344 
3345 	guard(mvm)(mvm);
3346 	return iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3347 }
3348 
iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw * hw,struct ieee80211_vif * vif)3349 void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3350 				struct ieee80211_vif *vif)
3351 {
3352 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3353 
3354 	guard(mvm)(mvm);
3355 
3356 	/* Due to a race condition, it's possible that mac80211 asks
3357 	 * us to stop a hw_scan when it's already stopped.  This can
3358 	 * happen, for instance, if we stopped the scan ourselves,
3359 	 * called ieee80211_scan_completed() and the userspace called
3360 	 * cancel scan before ieee80211_scan_work() could run.
3361 	 * To handle that, simply return if the scan is not running.
3362 	*/
3363 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3364 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3365 }
3366 
3367 void
iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int num_frames,enum ieee80211_frame_release_type reason,bool more_data)3368 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3369 				  struct ieee80211_sta *sta, u16 tids,
3370 				  int num_frames,
3371 				  enum ieee80211_frame_release_type reason,
3372 				  bool more_data)
3373 {
3374 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3375 
3376 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3377 
3378 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3379 					  tids, more_data, false);
3380 }
3381 
3382 void
iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw * hw,struct ieee80211_sta * sta,u16 tids,int num_frames,enum ieee80211_frame_release_type reason,bool more_data)3383 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3384 				    struct ieee80211_sta *sta, u16 tids,
3385 				    int num_frames,
3386 				    enum ieee80211_frame_release_type reason,
3387 				    bool more_data)
3388 {
3389 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3390 
3391 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3392 
3393 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3394 					  tids, more_data, true);
3395 }
3396 
__iwl_mvm_mac_sta_notify(struct ieee80211_hw * hw,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)3397 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3398 				     enum sta_notify_cmd cmd,
3399 				     struct ieee80211_sta *sta)
3400 {
3401 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3402 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3403 	unsigned long txqs = 0, tids = 0;
3404 	int tid;
3405 
3406 	/*
3407 	 * If we have TVQM then we get too high queue numbers - luckily
3408 	 * we really shouldn't get here with that because such hardware
3409 	 * should have firmware supporting buffer station offload.
3410 	 */
3411 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3412 		return;
3413 
3414 	spin_lock_bh(&mvmsta->lock);
3415 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3416 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3417 
3418 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3419 			continue;
3420 
3421 		__set_bit(tid_data->txq_id, &txqs);
3422 
3423 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3424 			continue;
3425 
3426 		__set_bit(tid, &tids);
3427 	}
3428 
3429 	switch (cmd) {
3430 	case STA_NOTIFY_SLEEP:
3431 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3432 			ieee80211_sta_set_buffered(sta, tid, true);
3433 
3434 		if (txqs)
3435 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3436 		/*
3437 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3438 		 * queues to this station will not be transmitted. The fw will
3439 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3440 		 */
3441 		break;
3442 	case STA_NOTIFY_AWAKE:
3443 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_INVALID_STA))
3444 			break;
3445 
3446 		if (txqs)
3447 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3448 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3449 		break;
3450 	default:
3451 		break;
3452 	}
3453 	spin_unlock_bh(&mvmsta->lock);
3454 }
3455 
iwl_mvm_mac_sta_notify(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)3456 void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3457 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3458 {
3459 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
3460 }
3461 
iwl_mvm_sta_pm_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)3462 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3463 {
3464 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3465 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3466 	struct ieee80211_sta *sta;
3467 	struct iwl_mvm_sta *mvmsta;
3468 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3469 
3470 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3471 		return;
3472 
3473 	rcu_read_lock();
3474 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3475 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3476 		rcu_read_unlock();
3477 		return;
3478 	}
3479 
3480 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3481 
3482 	if (!mvmsta->vif ||
3483 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
3484 		rcu_read_unlock();
3485 		return;
3486 	}
3487 
3488 	if (mvmsta->sleeping != sleeping) {
3489 		mvmsta->sleeping = sleeping;
3490 		__iwl_mvm_mac_sta_notify(mvm->hw,
3491 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3492 			sta);
3493 		ieee80211_sta_ps_transition(sta, sleeping);
3494 	}
3495 
3496 	if (sleeping) {
3497 		switch (notif->type) {
3498 		case IWL_MVM_PM_EVENT_AWAKE:
3499 		case IWL_MVM_PM_EVENT_ASLEEP:
3500 			break;
3501 		case IWL_MVM_PM_EVENT_UAPSD:
3502 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3503 			break;
3504 		case IWL_MVM_PM_EVENT_PS_POLL:
3505 			ieee80211_sta_pspoll(sta);
3506 			break;
3507 		default:
3508 			break;
3509 		}
3510 	}
3511 
3512 	rcu_read_unlock();
3513 }
3514 
iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3515 void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3516 				struct ieee80211_vif *vif,
3517 				struct ieee80211_sta *sta)
3518 {
3519 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3520 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3521 	unsigned int link_id;
3522 
3523 	lockdep_assert_wiphy(mvm->hw->wiphy);
3524 
3525 	/*
3526 	 * This is called before mac80211 does RCU synchronisation,
3527 	 * so here we already invalidate our internal RCU-protected
3528 	 * station pointer. The rest of the code will thus no longer
3529 	 * be able to find the station this way, and we don't rely
3530 	 * on further RCU synchronisation after the sta_state()
3531 	 * callback deleted the station.
3532 	 * Since there's mvm->mutex here, no need to have RCU lock for
3533 	 * mvm_sta->link access.
3534 	 */
3535 	guard(mvm)(mvm);
3536 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3537 		struct iwl_mvm_link_sta *link_sta;
3538 		u32 sta_id;
3539 
3540 		if (!mvm_sta->link[link_id])
3541 			continue;
3542 
3543 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3544 						     lockdep_is_held(&mvm->mutex));
3545 		sta_id = link_sta->sta_id;
3546 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3547 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3548 					 ERR_PTR(-ENOENT));
3549 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3550 		}
3551 	}
3552 }
3553 
iwl_mvm_check_uapsd(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const u8 * bssid)3554 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3555 				const u8 *bssid)
3556 {
3557 	int i;
3558 
3559 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3560 		struct iwl_mvm_tcm_mac *mdata;
3561 
3562 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3563 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3564 		mdata->opened_rx_ba_sessions = false;
3565 	}
3566 
3567 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3568 		return;
3569 
3570 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3571 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3572 		return;
3573 	}
3574 
3575 	if (!vif->p2p &&
3576 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3577 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3578 		return;
3579 	}
3580 
3581 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3582 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3583 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3584 			return;
3585 		}
3586 	}
3587 
3588 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3589 }
3590 
3591 static void
iwl_mvm_tdls_check_trigger(struct iwl_mvm * mvm,struct ieee80211_vif * vif,u8 * peer_addr,enum nl80211_tdls_operation action)3592 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3593 			   struct ieee80211_vif *vif, u8 *peer_addr,
3594 			   enum nl80211_tdls_operation action)
3595 {
3596 	struct iwl_fw_dbg_trigger_tlv *trig;
3597 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3598 
3599 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3600 				     FW_DBG_TRIGGER_TDLS);
3601 	if (!trig)
3602 		return;
3603 
3604 	tdls_trig = (void *)trig->data;
3605 
3606 	if (!(tdls_trig->action_bitmap & BIT(action)))
3607 		return;
3608 
3609 	if (tdls_trig->peer_mode &&
3610 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3611 		return;
3612 
3613 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3614 				"TDLS event occurred, peer %pM, action %d",
3615 				peer_addr, action);
3616 }
3617 
3618 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3619 	bool tolerated;
3620 };
3621 
iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy * wiphy,struct cfg80211_bss * bss,void * _data)3622 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3623 						    struct cfg80211_bss *bss,
3624 						    void *_data)
3625 {
3626 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3627 	const struct cfg80211_bss_ies *ies;
3628 	const struct element *elem;
3629 
3630 	rcu_read_lock();
3631 	ies = rcu_dereference(bss->ies);
3632 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3633 				  ies->len);
3634 
3635 	if (!elem || elem->datalen < 10 ||
3636 	    !(elem->data[10] &
3637 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3638 		data->tolerated = false;
3639 	}
3640 	rcu_read_unlock();
3641 }
3642 
3643 static void
iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,struct ieee80211_bss_conf * link_conf)3644 iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3645 				   struct ieee80211_vif *vif,
3646 				   unsigned int link_id,
3647 				   struct ieee80211_bss_conf *link_conf)
3648 {
3649 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3650 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3651 		.tolerated = true,
3652 	};
3653 
3654 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
3655 			 !mvmvif->link[link_id]))
3656 		return;
3657 
3658 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
3659 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
3660 		return;
3661 	}
3662 
3663 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
3664 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3665 			  &iter_data);
3666 
3667 	/*
3668 	 * If there is at least one AP on radar channel that cannot
3669 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3670 	 */
3671 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3672 }
3673 
iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3674 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3675 					       struct ieee80211_vif *vif)
3676 {
3677 	struct ieee80211_supported_band *sband;
3678 	const struct ieee80211_sta_he_cap *he_cap;
3679 
3680 	if (vif->type != NL80211_IFTYPE_STATION)
3681 		return;
3682 
3683 	if (!mvm->cca_40mhz_workaround)
3684 		return;
3685 
3686 	/* decrement and check that we reached zero */
3687 	mvm->cca_40mhz_workaround--;
3688 	if (mvm->cca_40mhz_workaround)
3689 		return;
3690 
3691 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3692 
3693 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3694 
3695 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3696 
3697 	if (he_cap) {
3698 		/* we know that ours is writable */
3699 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3700 
3701 		he->he_cap_elem.phy_cap_info[0] |=
3702 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3703 	}
3704 }
3705 
iwl_mvm_mei_host_associated(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_sta * mvm_sta)3706 static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3707 					struct ieee80211_vif *vif,
3708 					struct iwl_mvm_sta *mvm_sta)
3709 {
3710 #if IS_ENABLED(CONFIG_IWLMEI)
3711 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3712 	struct iwl_mei_conn_info conn_info = {
3713 		.ssid_len = vif->cfg.ssid_len,
3714 	};
3715 
3716 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3717 		return;
3718 
3719 	if (!mvm->mei_registered)
3720 		return;
3721 
3722 	/* FIXME: MEI needs to be updated for MLO */
3723 	if (!vif->bss_conf.chanreq.oper.chan)
3724 		return;
3725 
3726 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
3727 
3728 	switch (mvm_sta->pairwise_cipher) {
3729 	case WLAN_CIPHER_SUITE_TKIP:
3730 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3731 		break;
3732 	case WLAN_CIPHER_SUITE_CCMP:
3733 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3734 		break;
3735 	case WLAN_CIPHER_SUITE_GCMP:
3736 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3737 		break;
3738 	case WLAN_CIPHER_SUITE_GCMP_256:
3739 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3740 		break;
3741 	case 0:
3742 		/* open profile */
3743 		break;
3744 	default:
3745 		/* cipher not supported, don't send anything to iwlmei */
3746 		return;
3747 	}
3748 
3749 	switch (mvmvif->rekey_data.akm) {
3750 	case WLAN_AKM_SUITE_SAE & 0xff:
3751 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3752 		break;
3753 	case WLAN_AKM_SUITE_PSK & 0xff:
3754 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3755 		break;
3756 	case WLAN_AKM_SUITE_8021X & 0xff:
3757 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3758 		break;
3759 	case 0:
3760 		/* open profile */
3761 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3762 		break;
3763 	default:
3764 		/* auth method / AKM not supported */
3765 		/* TODO: All the FT vesions of these? */
3766 		return;
3767 	}
3768 
3769 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3770 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3771 
3772 	/* TODO: add support for collocated AP data */
3773 	iwl_mei_host_associated(&conn_info, NULL);
3774 #endif
3775 }
3776 
iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool force_assoc_off)3777 static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3778 					    struct ieee80211_vif *vif,
3779 					    bool force_assoc_off)
3780 {
3781 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3782 }
3783 
iwl_mvm_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)3784 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3785 				 struct ieee80211_vif *vif,
3786 				 struct ieee80211_sta *sta,
3787 				 enum ieee80211_sta_state old_state,
3788 				 enum ieee80211_sta_state new_state)
3789 {
3790 	static const struct iwl_mvm_sta_state_ops callbacks = {
3791 		.add_sta = iwl_mvm_add_sta,
3792 		.update_sta = iwl_mvm_update_sta,
3793 		.rm_sta = iwl_mvm_rm_sta,
3794 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3795 	};
3796 
3797 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3798 					    &callbacks);
3799 }
3800 
3801 /* FIXME: temporary making two assumptions in all sta handling functions:
3802  *	(1) when setting sta state, the link exists and protected
3803  *	(2) if a link is valid in sta then it's valid in vif (can
3804  *	use same index in the link array)
3805  */
iwl_mvm_rs_rate_init_all_links(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3806 static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3807 					   struct ieee80211_vif *vif,
3808 					   struct ieee80211_sta *sta)
3809 {
3810 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3811 	unsigned int link_id;
3812 
3813 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3814 		struct ieee80211_bss_conf *conf =
3815 			link_conf_dereference_check(vif, link_id);
3816 		struct ieee80211_link_sta *link_sta =
3817 			link_sta_dereference_check(sta, link_id);
3818 
3819 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3820 			continue;
3821 
3822 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3823 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3824 	}
3825 }
3826 
iwl_mvm_vif_conf_from_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta)3827 static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3828 				      struct ieee80211_vif *vif,
3829 				      struct ieee80211_sta *sta)
3830 {
3831 	struct ieee80211_link_sta *link_sta;
3832 	unsigned int link_id;
3833 
3834 	/* Beacon interval check - firmware will crash if the beacon
3835 	 * interval is less than 16. We can't avoid connecting at all,
3836 	 * so refuse the station state change, this will cause mac80211
3837 	 * to abandon attempts to connect to this AP, and eventually
3838 	 * wpa_s will blocklist the AP...
3839 	 */
3840 
3841 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3842 		struct ieee80211_bss_conf *link_conf =
3843 			link_conf_dereference_protected(vif, link_id);
3844 
3845 		if (!link_conf)
3846 			continue;
3847 
3848 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3849 			IWL_ERR(mvm,
3850 				"Beacon interval %d for AP %pM is too small\n",
3851 				link_conf->beacon_int, link_sta->addr);
3852 			return false;
3853 		}
3854 
3855 		link_conf->he_support = link_sta->he_cap.has_he;
3856 	}
3857 
3858 	return true;
3859 }
3860 
iwl_mvm_vif_set_he_support(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,bool is_sta)3861 static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3862 				       struct ieee80211_vif *vif,
3863 				       struct ieee80211_sta *sta,
3864 				       bool is_sta)
3865 {
3866 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3867 	struct ieee80211_link_sta *link_sta;
3868 	unsigned int link_id;
3869 
3870 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3871 		struct ieee80211_bss_conf *link_conf =
3872 			link_conf_dereference_protected(vif, link_id);
3873 
3874 		if (!link_conf || !mvmvif->link[link_id])
3875 			continue;
3876 
3877 		link_conf->he_support = link_sta->he_cap.has_he;
3878 
3879 		if (is_sta) {
3880 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
3881 			if (link_sta->he_cap.has_he)
3882 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3883 								   link_id,
3884 								   link_conf);
3885 		}
3886 	}
3887 }
3888 
3889 static int
iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)3890 iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3891 				   struct ieee80211_vif *vif,
3892 				   struct ieee80211_sta *sta,
3893 				   const struct iwl_mvm_sta_state_ops *callbacks)
3894 {
3895 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3896 	struct ieee80211_link_sta *link_sta;
3897 	unsigned int i;
3898 	int ret;
3899 
3900 	lockdep_assert_held(&mvm->mutex);
3901 
3902 	if (vif->type == NL80211_IFTYPE_STATION &&
3903 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3904 		return -EINVAL;
3905 
3906 	if (sta->tdls &&
3907 	    (vif->p2p ||
3908 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_TDLS_STA_COUNT ||
3909 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3910 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3911 		return -EBUSY;
3912 	}
3913 
3914 	ret = callbacks->add_sta(mvm, vif, sta);
3915 	if (sta->tdls && ret == 0) {
3916 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
3917 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3918 					   NL80211_TDLS_SETUP);
3919 	}
3920 
3921 	if (ret)
3922 		return ret;
3923 
3924 	for_each_sta_active_link(vif, sta, link_sta, i)
3925 		link_sta->agg.max_rc_amsdu_len = 1;
3926 
3927 	ieee80211_sta_recalc_aggregates(sta);
3928 
3929 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3930 		mvmvif->ap_sta = sta;
3931 
3932 	/*
3933 	 * Initialize the rates here already - this really tells
3934 	 * the firmware only what the supported legacy rates are
3935 	 * (may be) since it's initialized already from what the
3936 	 * AP advertised in the beacon/probe response. This will
3937 	 * allow the firmware to send auth/assoc frames with one
3938 	 * of the supported rates already, rather than having to
3939 	 * use a mandatory rate.
3940 	 * If we're the AP, we'll just assume mandatory rates at
3941 	 * this point, but we know nothing about the STA anyway.
3942 	 */
3943 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3944 
3945 	return 0;
3946 }
3947 
3948 static int
iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw * hw,struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)3949 iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3950 				struct iwl_mvm *mvm,
3951 				struct ieee80211_vif *vif,
3952 				struct ieee80211_sta *sta,
3953 				const struct iwl_mvm_sta_state_ops *callbacks)
3954 {
3955 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3956 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3957 	struct ieee80211_link_sta *link_sta;
3958 	unsigned int link_id;
3959 
3960 	lockdep_assert_held(&mvm->mutex);
3961 
3962 	if (vif->type == NL80211_IFTYPE_AP) {
3963 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3964 		mvmvif->ap_assoc_sta_count++;
3965 		callbacks->mac_ctxt_changed(mvm, vif, false);
3966 
3967 		/* since the below is not for MLD API, it's ok to use
3968 		 * the default bss_conf
3969 		 */
3970 		if (!mvm->mld_api_is_used &&
3971 		    (vif->bss_conf.he_support &&
3972 		     !iwlwifi_mod_params.disable_11ax))
3973 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3974 	} else if (vif->type == NL80211_IFTYPE_STATION) {
3975 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3976 
3977 		callbacks->mac_ctxt_changed(mvm, vif, false);
3978 
3979 		if (!mvm->mld_api_is_used)
3980 			goto out;
3981 
3982 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
3983 			struct ieee80211_bss_conf *link_conf =
3984 				link_conf_dereference_protected(vif, link_id);
3985 
3986 			if (WARN_ON(!link_conf))
3987 				return -EINVAL;
3988 			if (!mvmvif->link[link_id])
3989 				continue;
3990 
3991 			iwl_mvm_link_changed(mvm, vif, link_conf,
3992 					     LINK_CONTEXT_MODIFY_ALL &
3993 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
3994 					     true);
3995 		}
3996 	}
3997 
3998 out:
3999 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4000 
4001 	return callbacks->update_sta(mvm, vif, sta);
4002 }
4003 
4004 static int
iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)4005 iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
4006 				      struct ieee80211_vif *vif,
4007 				      struct ieee80211_sta *sta,
4008 				      const struct iwl_mvm_sta_state_ops *callbacks)
4009 {
4010 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4011 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4012 
4013 	lockdep_assert_held(&mvm->mutex);
4014 
4015 	/* we don't support TDLS during DCM */
4016 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4017 		iwl_mvm_teardown_tdls_peers(mvm);
4018 
4019 	if (sta->tdls) {
4020 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4021 					   NL80211_TDLS_ENABLE_LINK);
4022 	} else {
4023 		/* enable beacon filtering */
4024 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
4025 
4026 		mvmvif->authorized = 1;
4027 
4028 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
4029 			mvmvif->link_selection_res = vif->active_links;
4030 			mvmvif->link_selection_primary =
4031 				vif->active_links ? __ffs(vif->active_links) : 0;
4032 		}
4033 
4034 		callbacks->mac_ctxt_changed(mvm, vif, false);
4035 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
4036 
4037 		memset(&mvmvif->last_esr_exit, 0,
4038 		       sizeof(mvmvif->last_esr_exit));
4039 
4040 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
4041 
4042 		/* Block until FW notif will arrive */
4043 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
4044 
4045 		/* when client is authorized (AP station marked as such),
4046 		 * try to enable the best link(s).
4047 		 */
4048 		if (vif->type == NL80211_IFTYPE_STATION &&
4049 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4050 			iwl_mvm_select_links(mvm, vif);
4051 	}
4052 
4053 	mvm_sta->authorized = true;
4054 
4055 	/* MFP is set by default before the station is authorized.
4056 	 * Clear it here in case it's not used.
4057 	 */
4058 	if (!sta->mfp) {
4059 		int ret = callbacks->update_sta(mvm, vif, sta);
4060 
4061 		if (ret)
4062 			return ret;
4063 	}
4064 
4065 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4066 
4067 	return 0;
4068 }
4069 
4070 static int
iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * sta,const struct iwl_mvm_sta_state_ops * callbacks)4071 iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
4072 				      struct ieee80211_vif *vif,
4073 				      struct ieee80211_sta *sta,
4074 				      const struct iwl_mvm_sta_state_ops *callbacks)
4075 {
4076 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4077 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4078 
4079 	lockdep_assert_held(&mvm->mutex);
4080 
4081 	mvmsta->authorized = false;
4082 
4083 	/* once we move into assoc state, need to update rate scale to
4084 	 * disable using wide bandwidth
4085 	 */
4086 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4087 
4088 	if (!sta->tdls) {
4089 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
4090 		 * yet to avoid sending high prio again for a little
4091 		 * time.
4092 		 */
4093 		mvmvif->authorized = 0;
4094 		mvmvif->link_selection_res = 0;
4095 
4096 		/* disable beacon filtering */
4097 		iwl_mvm_disable_beacon_filter(mvm, vif);
4098 
4099 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4100 					  &mvmvif->prevent_esr_done_wk);
4101 
4102 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4103 					  &mvmvif->mlo_int_scan_wk);
4104 
4105 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4106 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4107 					  &mvmvif->unblock_esr_tmp_non_bss_wk);
4108 	}
4109 
4110 	return 0;
4111 }
4112 
iwl_mvm_smps_workaround(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool update)4113 void iwl_mvm_smps_workaround(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
4114 			     bool update)
4115 {
4116 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4117 
4118 	if (!iwl_mvm_has_rlc_offload(mvm) ||
4119 	    iwl_fw_lookup_cmd_ver(mvm->fw, MAC_PM_POWER_TABLE, 0) >= 2)
4120 		return;
4121 
4122 	mvmvif->ps_disabled = !vif->cfg.ps;
4123 
4124 	if (update)
4125 		iwl_mvm_power_update_mac(mvm);
4126 }
4127 
4128 /* Common part for MLD and non-MLD modes */
iwl_mvm_mac_sta_state_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,enum ieee80211_sta_state old_state,enum ieee80211_sta_state new_state,const struct iwl_mvm_sta_state_ops * callbacks)4129 int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
4130 				 struct ieee80211_vif *vif,
4131 				 struct ieee80211_sta *sta,
4132 				 enum ieee80211_sta_state old_state,
4133 				 enum ieee80211_sta_state new_state,
4134 				 const struct iwl_mvm_sta_state_ops *callbacks)
4135 {
4136 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4137 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4138 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4139 	struct ieee80211_link_sta *link_sta;
4140 	unsigned int link_id;
4141 	int ret;
4142 
4143 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4144 			   sta->addr, old_state, new_state);
4145 
4146 	/*
4147 	 * If we are in a STA removal flow and in DQA mode:
4148 	 *
4149 	 * This is after the sync_rcu part, so the queues have already been
4150 	 * flushed. No more TXs on their way in mac80211's path, and no more in
4151 	 * the queues.
4152 	 * Also, we won't be getting any new TX frames for this station.
4153 	 * What we might have are deferred TX frames that need to be taken care
4154 	 * of.
4155 	 *
4156 	 * Drop any still-queued deferred-frame before removing the STA, and
4157 	 * make sure the worker is no longer handling frames for this STA.
4158 	 */
4159 	if (old_state == IEEE80211_STA_NONE &&
4160 	    new_state == IEEE80211_STA_NOTEXIST) {
4161 		flush_work(&mvm->add_stream_wk);
4162 
4163 		/*
4164 		 * No need to make sure deferred TX indication is off since the
4165 		 * worker will already remove it if it was on
4166 		 */
4167 
4168 		/*
4169 		 * Additionally, reset the 40 MHz capability if we disconnected
4170 		 * from the AP now.
4171 		 */
4172 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4173 
4174 		/* Also free dup data just in case any assertions below fail */
4175 		kfree(mvm_sta->dup_data);
4176 	}
4177 
4178 	mutex_lock(&mvm->mutex);
4179 
4180 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4181 	 * firmware crash while we were activating a link, in which case it is
4182 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4183 	 * recovery flow since we spit tons of error messages anyway.
4184 	 */
4185 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
4186 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
4187 				 !mvmvif->link[link_id]->phy_ctxt)) {
4188 			mutex_unlock(&mvm->mutex);
4189 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4190 					&mvm->status) ? 0 : -EINVAL;
4191 		}
4192 	}
4193 
4194 	/* track whether or not the station is associated */
4195 	mvm_sta->sta_state = new_state;
4196 
4197 	if (old_state == IEEE80211_STA_NOTEXIST &&
4198 	    new_state == IEEE80211_STA_NONE) {
4199 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
4200 							 callbacks);
4201 		if (ret < 0)
4202 			goto out_unlock;
4203 	} else if (old_state == IEEE80211_STA_NONE &&
4204 		   new_state == IEEE80211_STA_AUTH) {
4205 		/*
4206 		 * EBS may be disabled due to previous failures reported by FW.
4207 		 * Reset EBS status here assuming environment has been changed.
4208 		 */
4209 		mvm->last_ebs_successful = true;
4210 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4211 		ret = 0;
4212 	} else if (old_state == IEEE80211_STA_AUTH &&
4213 		   new_state == IEEE80211_STA_ASSOC) {
4214 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
4215 						      callbacks);
4216 	} else if (old_state == IEEE80211_STA_ASSOC &&
4217 		   new_state == IEEE80211_STA_AUTHORIZED) {
4218 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
4219 							    callbacks);
4220 		iwl_mvm_smps_workaround(mvm, vif, true);
4221 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4222 		   new_state == IEEE80211_STA_ASSOC) {
4223 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
4224 							    callbacks);
4225 	} else if (old_state == IEEE80211_STA_ASSOC &&
4226 		   new_state == IEEE80211_STA_AUTH) {
4227 		if (vif->type == NL80211_IFTYPE_AP) {
4228 			mvmvif->ap_assoc_sta_count--;
4229 			callbacks->mac_ctxt_changed(mvm, vif, false);
4230 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4231 			iwl_mvm_stop_session_protection(mvm, vif);
4232 		ret = 0;
4233 	} else if (old_state == IEEE80211_STA_AUTH &&
4234 		   new_state == IEEE80211_STA_NONE) {
4235 		ret = 0;
4236 	} else if (old_state == IEEE80211_STA_NONE &&
4237 		   new_state == IEEE80211_STA_NOTEXIST) {
4238 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4239 			iwl_mvm_stop_session_protection(mvm, vif);
4240 			mvmvif->ap_sta = NULL;
4241 		}
4242 		ret = callbacks->rm_sta(mvm, vif, sta);
4243 		if (sta->tdls) {
4244 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
4245 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4246 						   NL80211_TDLS_DISABLE_LINK);
4247 		}
4248 
4249 		if (unlikely(ret &&
4250 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4251 				      &mvm->status)))
4252 			ret = 0;
4253 	} else {
4254 		ret = -EIO;
4255 	}
4256  out_unlock:
4257 	mutex_unlock(&mvm->mutex);
4258 
4259 	if (sta->tdls && ret == 0) {
4260 		if (old_state == IEEE80211_STA_NOTEXIST &&
4261 		    new_state == IEEE80211_STA_NONE)
4262 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4263 		else if (old_state == IEEE80211_STA_NONE &&
4264 			 new_state == IEEE80211_STA_NOTEXIST)
4265 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4266 	}
4267 
4268 	return ret;
4269 }
4270 
iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)4271 int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
4272 				  u32 value)
4273 {
4274 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4275 
4276 	mvm->rts_threshold = value;
4277 
4278 	return 0;
4279 }
4280 
iwl_mvm_sta_rc_update(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_link_sta * link_sta,u32 changed)4281 void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4282 			   struct ieee80211_link_sta *link_sta, u32 changed)
4283 {
4284 	struct ieee80211_sta *sta = link_sta->sta;
4285 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4286 
4287 	if (changed & (IEEE80211_RC_BW_CHANGED |
4288 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4289 		       IEEE80211_RC_NSS_CHANGED))
4290 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4291 
4292 	if (vif->type == NL80211_IFTYPE_STATION &&
4293 	    changed & IEEE80211_RC_NSS_CHANGED)
4294 		iwl_mvm_sf_update(mvm, vif, false);
4295 }
4296 
iwl_mvm_mac_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 ac,const struct ieee80211_tx_queue_params * params)4297 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4298 			       struct ieee80211_vif *vif,
4299 			       unsigned int link_id, u16 ac,
4300 			       const struct ieee80211_tx_queue_params *params)
4301 {
4302 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4303 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4304 
4305 	mvmvif->deflink.queue_params[ac] = *params;
4306 
4307 	/*
4308 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4309 	 * The exception is P2P_DEVICE interface which needs immediate update.
4310 	 */
4311 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4312 		guard(mvm)(mvm);
4313 		return iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4314 	}
4315 	return 0;
4316 }
4317 
iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)4318 void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4319 				struct ieee80211_vif *vif,
4320 				struct ieee80211_prep_tx_info *info)
4321 {
4322 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4323 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4324 
4325 	if (info->was_assoc && !mvmvif->session_prot_connection_loss)
4326 		return;
4327 
4328 	guard(mvm)(mvm);
4329 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4330 }
4331 
iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_prep_tx_info * info)4332 void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4333 				 struct ieee80211_vif *vif,
4334 				 struct ieee80211_prep_tx_info *info)
4335 {
4336 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4337 
4338 	/* for successful cases (auth/assoc), don't cancel session protection */
4339 	if (info->success)
4340 		return;
4341 
4342 	guard(mvm)(mvm);
4343 	iwl_mvm_stop_session_protection(mvm, vif);
4344 }
4345 
iwl_mvm_mac_sched_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_sched_scan_request * req,struct ieee80211_scan_ies * ies)4346 int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4347 				 struct ieee80211_vif *vif,
4348 				 struct cfg80211_sched_scan_request *req,
4349 				 struct ieee80211_scan_ies *ies)
4350 {
4351 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4352 
4353 	guard(mvm)(mvm);
4354 
4355 	if (!vif->cfg.idle)
4356 		return -EBUSY;
4357 
4358 	return iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4359 }
4360 
iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4361 int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4362 				struct ieee80211_vif *vif)
4363 {
4364 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4365 	int ret;
4366 
4367 	mutex_lock(&mvm->mutex);
4368 
4369 	/* Due to a race condition, it's possible that mac80211 asks
4370 	 * us to stop a sched_scan when it's already stopped.  This
4371 	 * can happen, for instance, if we stopped the scan ourselves,
4372 	 * called ieee80211_sched_scan_stopped() and the userspace called
4373 	 * stop sched scan before ieee80211_sched_scan_stopped_work()
4374 	 * could run.  To handle this, simply return if the scan is
4375 	 * not running.
4376 	*/
4377 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4378 		mutex_unlock(&mvm->mutex);
4379 		return 0;
4380 	}
4381 
4382 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4383 	mutex_unlock(&mvm->mutex);
4384 	iwl_mvm_wait_for_async_handlers(mvm);
4385 
4386 	return ret;
4387 }
4388 
__iwl_mvm_mac_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)4389 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4390 				 enum set_key_cmd cmd,
4391 				 struct ieee80211_vif *vif,
4392 				 struct ieee80211_sta *sta,
4393 				 struct ieee80211_key_conf *key)
4394 {
4395 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4396 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4397 	struct iwl_mvm_sta *mvmsta = NULL;
4398 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4399 	int keyidx = key->keyidx;
4400 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4401 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4402 	int ret, i;
4403 	u8 key_offset;
4404 
4405 	if (sta)
4406 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4407 
4408 	switch (key->cipher) {
4409 	case WLAN_CIPHER_SUITE_TKIP:
4410 		if (!mvm->trans->mac_cfg->gen2) {
4411 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4412 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4413 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4414 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4415 		} else {
4416 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4417 			return -EOPNOTSUPP;
4418 		}
4419 		break;
4420 	case WLAN_CIPHER_SUITE_CCMP:
4421 	case WLAN_CIPHER_SUITE_GCMP:
4422 	case WLAN_CIPHER_SUITE_GCMP_256:
4423 		if (!iwl_mvm_has_new_tx_api(mvm))
4424 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4425 		break;
4426 	case WLAN_CIPHER_SUITE_AES_CMAC:
4427 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4428 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4429 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4430 		break;
4431 	case WLAN_CIPHER_SUITE_WEP40:
4432 	case WLAN_CIPHER_SUITE_WEP104:
4433 		if (vif->type == NL80211_IFTYPE_STATION)
4434 			break;
4435 		if (iwl_mvm_has_new_tx_api(mvm))
4436 			return -EOPNOTSUPP;
4437 		/* support HW crypto on TX */
4438 		return 0;
4439 	default:
4440 		return -EOPNOTSUPP;
4441 	}
4442 
4443 	switch (cmd) {
4444 	case SET_KEY:
4445 		if (vif->type == NL80211_IFTYPE_STATION &&
4446 		    (keyidx == 6 || keyidx == 7))
4447 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4448 					   key);
4449 
4450 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4451 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4452 			/*
4453 			 * GTK on AP interface is a TX-only key, return 0;
4454 			 * on IBSS they're per-station and because we're lazy
4455 			 * we don't support them for RX, so do the same.
4456 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4457 			 * on older NICs.
4458 			 *
4459 			 * Except, of course, beacon protection - it must be
4460 			 * offloaded since we just set a beacon template, and
4461 			 * then we must also offload the IGTK (not just BIGTK)
4462 			 * for firmware reasons.
4463 			 *
4464 			 * So just check for beacon protection - if we don't
4465 			 * have it we cannot get here with keyidx >= 6, and
4466 			 * if we do have it we need to send the key to FW in
4467 			 * all cases (CMAC/GMAC).
4468 			 */
4469 			if (!wiphy_ext_feature_isset(hw->wiphy,
4470 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4471 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4472 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4473 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4474 				ret = -EOPNOTSUPP;
4475 				break;
4476 			}
4477 
4478 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4479 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4480 			    !iwl_mvm_has_new_tx_api(mvm)) {
4481 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4482 				ret = 0;
4483 				break;
4484 			}
4485 
4486 			if (!mvmvif->ap_ibss_active) {
4487 				for (i = 0;
4488 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4489 				     i++) {
4490 					if (!mvmvif->ap_early_keys[i]) {
4491 						mvmvif->ap_early_keys[i] = key;
4492 						break;
4493 					}
4494 				}
4495 
4496 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4497 					ret = -ENOSPC;
4498 				else
4499 					ret = 0;
4500 
4501 				break;
4502 			}
4503 		}
4504 
4505 		/* During FW restart, in order to restore the state as it was,
4506 		 * don't try to reprogram keys we previously failed for.
4507 		 */
4508 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4509 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4510 			IWL_DEBUG_MAC80211(mvm,
4511 					   "skip invalid idx key programming during restart\n");
4512 			ret = 0;
4513 			break;
4514 		}
4515 
4516 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4517 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4518 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4519 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4520 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4521 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4522 			struct ieee80211_key_seq seq;
4523 			int tid, q;
4524 
4525 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4526 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4527 						     mvm->trans->info.num_rxqs),
4528 					 GFP_KERNEL);
4529 			if (!ptk_pn) {
4530 				ret = -ENOMEM;
4531 				break;
4532 			}
4533 
4534 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4535 				ieee80211_get_key_rx_seq(key, tid, &seq);
4536 				for (q = 0; q < mvm->trans->info.num_rxqs; q++)
4537 					memcpy(ptk_pn->q[q].pn[tid],
4538 					       seq.ccmp.pn,
4539 					       IEEE80211_CCMP_PN_LEN);
4540 			}
4541 
4542 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4543 		}
4544 
4545 		/* in HW restart reuse the index, otherwise request a new one */
4546 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4547 			key_offset = key->hw_key_idx;
4548 		else
4549 			key_offset = STA_KEY_IDX_INVALID;
4550 
4551 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4552 			mvmsta->pairwise_cipher = key->cipher;
4553 
4554 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4555 				   sta ? sta->addr : NULL, key->keyidx);
4556 
4557 		if (sec_key_ver)
4558 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4559 		else
4560 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4561 
4562 		if (ret) {
4563 			IWL_WARN(mvm, "set key failed\n");
4564 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4565 			if (ptk_pn) {
4566 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4567 				kfree(ptk_pn);
4568 			}
4569 			/*
4570 			 * can't add key for RX, but we don't need it
4571 			 * in the device for TX so still return 0,
4572 			 * unless we have new TX API where we cannot
4573 			 * put key material into the TX_CMD
4574 			 */
4575 			if (iwl_mvm_has_new_tx_api(mvm))
4576 				ret = -EOPNOTSUPP;
4577 			else
4578 				ret = 0;
4579 		}
4580 
4581 		break;
4582 	case DISABLE_KEY:
4583 		if (vif->type == NL80211_IFTYPE_STATION &&
4584 		    (keyidx == 6 || keyidx == 7))
4585 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4586 					 NULL);
4587 
4588 		ret = -ENOENT;
4589 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4590 			if (mvmvif->ap_early_keys[i] == key) {
4591 				mvmvif->ap_early_keys[i] = NULL;
4592 				ret = 0;
4593 			}
4594 		}
4595 
4596 		/* found in pending list - don't do anything else */
4597 		if (ret == 0)
4598 			break;
4599 
4600 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4601 			ret = 0;
4602 			break;
4603 		}
4604 
4605 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4606 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4607 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4608 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4609 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4610 			ptk_pn = rcu_dereference_protected(
4611 						mvmsta->ptk_pn[keyidx],
4612 						lockdep_is_held(&mvm->mutex));
4613 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4614 			if (ptk_pn)
4615 				kfree_rcu(ptk_pn, rcu_head);
4616 		}
4617 
4618 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4619 		if (sec_key_ver)
4620 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4621 		else
4622 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4623 		break;
4624 	default:
4625 		ret = -EINVAL;
4626 	}
4627 
4628 	return ret;
4629 }
4630 
iwl_mvm_mac_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)4631 int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4632 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4633 			struct ieee80211_key_conf *key)
4634 {
4635 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4636 
4637 	/* When resuming from wowlan, FW already knows about the newest keys */
4638 	if (test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))
4639 		return 0;
4640 
4641 	guard(mvm)(mvm);
4642 	return __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4643 }
4644 
iwl_mvm_mac_update_tkip_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_key_conf * keyconf,struct ieee80211_sta * sta,u32 iv32,u16 * phase1key)4645 void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4646 				 struct ieee80211_vif *vif,
4647 				 struct ieee80211_key_conf *keyconf,
4648 				 struct ieee80211_sta *sta,
4649 				 u32 iv32, u16 *phase1key)
4650 {
4651 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4652 
4653 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4654 		return;
4655 
4656 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4657 }
4658 
4659 
iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)4660 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4661 			       struct iwl_rx_packet *pkt, void *data)
4662 {
4663 	struct iwl_mvm *mvm =
4664 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4665 	struct iwl_hs20_roc_res *resp;
4666 	int resp_len = iwl_rx_packet_payload_len(pkt);
4667 	struct iwl_mvm_time_event_data *te_data = data;
4668 
4669 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4670 		return true;
4671 
4672 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4673 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4674 		return true;
4675 	}
4676 
4677 	resp = (void *)pkt->data;
4678 
4679 	IWL_DEBUG_TE(mvm,
4680 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4681 		     resp->status, resp->event_unique_id);
4682 
4683 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4684 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4685 		     te_data->uid);
4686 
4687 	spin_lock_bh(&mvm->time_event_lock);
4688 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4689 	spin_unlock_bh(&mvm->time_event_lock);
4690 
4691 	return true;
4692 }
4693 
iwl_mvm_send_aux_roc_cmd(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration)4694 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4695 				    struct ieee80211_channel *channel,
4696 				    struct ieee80211_vif *vif,
4697 				    int duration)
4698 {
4699 	int res;
4700 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4701 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4702 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4703 	struct iwl_notification_wait wait_time_event;
4704 	u32 req_dur, delay;
4705 	struct iwl_hs20_roc_req aux_roc_req = {
4706 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4707 		.id_and_color =
4708 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4709 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4710 	};
4711 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4712 		&aux_roc_req.channel_info);
4713 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4714 
4715 	/* Set the channel info data */
4716 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4717 			      iwl_mvm_phy_band_from_nl80211(channel->band),
4718 			      IWL_PHY_CHANNEL_MODE20,
4719 			      0);
4720 
4721 	/* Set the time and duration */
4722 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4723 
4724 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
4725 	tail->duration = cpu_to_le32(req_dur);
4726 	tail->apply_time_max_delay = cpu_to_le32(delay);
4727 
4728 	IWL_DEBUG_TE(mvm,
4729 		     "ROC: Requesting to remain on channel %u for %ums\n",
4730 		     channel->hw_value, req_dur);
4731 	IWL_DEBUG_TE(mvm,
4732 		     "\t(requested = %ums, max_delay = %ums)\n",
4733 		     duration, delay);
4734 
4735 	/* Set the node address */
4736 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4737 
4738 	lockdep_assert_held(&mvm->mutex);
4739 
4740 	spin_lock_bh(&mvm->time_event_lock);
4741 
4742 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4743 		spin_unlock_bh(&mvm->time_event_lock);
4744 		return -EIO;
4745 	}
4746 
4747 	te_data->vif = vif;
4748 	te_data->duration = duration;
4749 	te_data->id = HOT_SPOT_CMD;
4750 
4751 	spin_unlock_bh(&mvm->time_event_lock);
4752 
4753 	/*
4754 	 * Use a notification wait, which really just processes the
4755 	 * command response and doesn't wait for anything, in order
4756 	 * to be able to process the response and get the UID inside
4757 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4758 	 * stores the buffer and then wakes up this thread, by which
4759 	 * time another notification (that the time event started)
4760 	 * might already be processed unsuccessfully.
4761 	 */
4762 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4763 				   time_event_response,
4764 				   ARRAY_SIZE(time_event_response),
4765 				   iwl_mvm_rx_aux_roc, te_data);
4766 
4767 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4768 				   &aux_roc_req);
4769 
4770 	if (res) {
4771 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4772 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4773 		goto out_clear_te;
4774 	}
4775 
4776 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4777 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4778 	/* should never fail */
4779 	WARN_ON_ONCE(res);
4780 
4781 	if (res) {
4782  out_clear_te:
4783 		spin_lock_bh(&mvm->time_event_lock);
4784 		iwl_mvm_te_clear_data(mvm, te_data);
4785 		spin_unlock_bh(&mvm->time_event_lock);
4786 	}
4787 
4788 	return res;
4789 }
4790 
iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm * mvm,u32 lmac_id)4791 static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4792 {
4793 	int ret = 0;
4794 
4795 	lockdep_assert_held(&mvm->mutex);
4796 
4797 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4798 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4799 		IWL_ERR(mvm, "hotspot not supported\n");
4800 		return -EINVAL;
4801 	}
4802 
4803 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4804 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4805 		WARN(ret, "Failed to allocate aux station");
4806 	}
4807 
4808 	return ret;
4809 }
4810 
iwl_mvm_roc_link(struct iwl_mvm * mvm,struct ieee80211_vif * vif)4811 static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4812 {
4813 	int ret;
4814 
4815 	lockdep_assert_held(&mvm->mutex);
4816 
4817 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4818 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4819 		return ret;
4820 
4821 	/* The station and queue allocation must be done only after the binding
4822 	 * is done, as otherwise the FW might incorrectly configure its state.
4823 	 */
4824 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4825 }
4826 
iwl_mvm_roc(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * channel,int duration,enum ieee80211_roc_type type)4827 static int iwl_mvm_roc(struct ieee80211_hw *hw,
4828 		       struct ieee80211_vif *vif,
4829 		       struct ieee80211_channel *channel,
4830 		       int duration,
4831 		       enum ieee80211_roc_type type)
4832 {
4833 	static const struct iwl_mvm_roc_ops ops = {
4834 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4835 		.link = iwl_mvm_roc_link,
4836 	};
4837 
4838 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4839 }
4840 
iwl_mvm_roc_station(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration)4841 static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
4842 			       struct ieee80211_channel *channel,
4843 			       struct ieee80211_vif *vif,
4844 			       int duration)
4845 {
4846 	int ret;
4847 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
4848 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
4849 					  IWL_FW_CMD_VER_UNKNOWN);
4850 
4851 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
4852 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
4853 	} else if (fw_ver >= 3) {
4854 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
4855 					  ROC_ACTIVITY_HOTSPOT);
4856 	} else {
4857 		ret = -EOPNOTSUPP;
4858 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
4859 	}
4860 
4861 	return ret;
4862 }
4863 
iwl_mvm_roc_p2p(struct iwl_mvm * mvm,struct ieee80211_channel * channel,struct ieee80211_vif * vif,int duration,enum ieee80211_roc_type type)4864 static int iwl_mvm_roc_p2p(struct iwl_mvm *mvm,
4865 			   struct ieee80211_channel *channel,
4866 			   struct ieee80211_vif *vif,
4867 			   int duration,
4868 			   enum ieee80211_roc_type type)
4869 {
4870 	enum iwl_roc_activity activity;
4871 	int ret;
4872 
4873 	lockdep_assert_held(&mvm->mutex);
4874 
4875 	switch (type) {
4876 	case IEEE80211_ROC_TYPE_NORMAL:
4877 		activity = ROC_ACTIVITY_P2P_DISC;
4878 		break;
4879 	case IEEE80211_ROC_TYPE_MGMT_TX:
4880 		activity = ROC_ACTIVITY_P2P_NEG;
4881 		break;
4882 	default:
4883 		WARN_ONCE(1, "Got an invalid P2P ROC type\n");
4884 		return -EINVAL;
4885 	}
4886 
4887 	ret = iwl_mvm_mld_add_aux_sta(mvm,
4888 				      iwl_mvm_get_lmac_id(mvm, channel->band));
4889 	if (ret)
4890 		return ret;
4891 
4892 	return iwl_mvm_roc_add_cmd(mvm, channel, vif, duration, activity);
4893 }
4894 
iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel * channel)4895 static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
4896 				     struct ieee80211_vif *vif,
4897 				     struct ieee80211_channel *channel)
4898 {
4899 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4900 	struct cfg80211_chan_def chandef;
4901 	int i;
4902 
4903 	lockdep_assert_held(&mvm->mutex);
4904 
4905 	if (mvmvif->deflink.phy_ctxt &&
4906 	    channel == mvmvif->deflink.phy_ctxt->channel)
4907 		return 0;
4908 
4909 	/* Try using a PHY context that is already in use */
4910 	for (i = 0; i < NUM_PHY_CTX; i++) {
4911 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
4912 
4913 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4914 			continue;
4915 
4916 		if (channel == phy_ctxt->channel) {
4917 			if (mvmvif->deflink.phy_ctxt)
4918 				iwl_mvm_phy_ctxt_unref(mvm,
4919 						       mvmvif->deflink.phy_ctxt);
4920 
4921 			mvmvif->deflink.phy_ctxt = phy_ctxt;
4922 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4923 			return 0;
4924 		}
4925 	}
4926 
4927 	/* We already have a phy_ctxt, but it's not on the right channel */
4928 	if (mvmvif->deflink.phy_ctxt)
4929 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4930 
4931 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4932 	if (!mvmvif->deflink.phy_ctxt)
4933 		return -ENOSPC;
4934 
4935 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4936 
4937 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
4938 				    &chandef, NULL, 1, 1);
4939 }
4940 
4941 /* Execute the common part for MLD and non-MLD modes */
iwl_mvm_roc_common(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel * channel,int duration,enum ieee80211_roc_type type,const struct iwl_mvm_roc_ops * ops)4942 int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4943 		       struct ieee80211_channel *channel, int duration,
4944 		       enum ieee80211_roc_type type,
4945 		       const struct iwl_mvm_roc_ops *ops)
4946 {
4947 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4948 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
4949 	u32 lmac_id;
4950 	int ret;
4951 
4952 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4953 			   duration, type);
4954 
4955 	/*
4956 	 * Flush the done work, just in case it's still pending, so that
4957 	 * the work it does can complete and we can accept new frames.
4958 	 */
4959 	flush_work(&mvm->roc_done_wk);
4960 
4961 	if (!IS_ERR_OR_NULL(bss_vif)) {
4962 		ret = iwl_mvm_block_esr_sync(mvm, bss_vif,
4963 					     IWL_MVM_ESR_BLOCKED_ROC);
4964 		if (ret)
4965 			return ret;
4966 	}
4967 
4968 	guard(mvm)(mvm);
4969 
4970 	switch (vif->type) {
4971 	case NL80211_IFTYPE_STATION:
4972 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4973 
4974 		/* Use aux roc framework (HS20) */
4975 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4976 		if (!ret)
4977 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4978 		return ret;
4979 	case NL80211_IFTYPE_P2P_DEVICE:
4980 		/* handle below */
4981 		break;
4982 	default:
4983 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4984 		return -EINVAL;
4985 	}
4986 
4987 	if (iwl_mvm_has_p2p_over_aux(mvm)) {
4988 		ret = iwl_mvm_roc_p2p(mvm, channel, vif, duration, type);
4989 		return ret;
4990 	}
4991 
4992 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
4993 	if (ret)
4994 		return ret;
4995 
4996 	ret = ops->link(mvm, vif);
4997 	if (ret)
4998 		return ret;
4999 
5000 	return iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
5001 }
5002 
iwl_mvm_cancel_roc(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5003 int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
5004 		       struct ieee80211_vif *vif)
5005 {
5006 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5007 
5008 	IWL_DEBUG_MAC80211(mvm, "enter\n");
5009 
5010 	iwl_mvm_stop_roc(mvm, vif);
5011 
5012 	IWL_DEBUG_MAC80211(mvm, "leave\n");
5013 	return 0;
5014 }
5015 
5016 struct iwl_mvm_chanctx_usage_data {
5017 	struct iwl_mvm *mvm;
5018 	struct ieee80211_chanctx_conf *ctx;
5019 	bool use_def;
5020 };
5021 
iwl_mvm_chanctx_usage_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)5022 static void iwl_mvm_chanctx_usage_iter(void *_data, u8 *mac,
5023 				       struct ieee80211_vif *vif)
5024 {
5025 	struct iwl_mvm_chanctx_usage_data *data = _data;
5026 	struct ieee80211_bss_conf *link_conf;
5027 	int link_id;
5028 
5029 	for_each_vif_active_link(vif, link_conf, link_id) {
5030 		if (rcu_access_pointer(link_conf->chanctx_conf) != data->ctx)
5031 			continue;
5032 
5033 		if (iwl_mvm_enable_fils(data->mvm, vif, data->ctx))
5034 			data->use_def = true;
5035 
5036 		if (vif->type == NL80211_IFTYPE_AP && link_conf->ftmr_params)
5037 			data->use_def = true;
5038 	}
5039 }
5040 
5041 struct cfg80211_chan_def *
iwl_mvm_chanctx_def(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)5042 iwl_mvm_chanctx_def(struct iwl_mvm *mvm, struct ieee80211_chanctx_conf *ctx)
5043 {
5044 	struct iwl_mvm_chanctx_usage_data data = {
5045 		.mvm = mvm,
5046 		.ctx = ctx,
5047 		.use_def = false,
5048 	};
5049 
5050 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
5051 						   IEEE80211_IFACE_ITER_NORMAL,
5052 						   iwl_mvm_chanctx_usage_iter,
5053 						   &data);
5054 
5055 	return data.use_def ? &ctx->def : &ctx->min_def;
5056 }
5057 
__iwl_mvm_add_chanctx(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)5058 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
5059 				 struct ieee80211_chanctx_conf *ctx)
5060 {
5061 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5062 	struct iwl_mvm_phy_ctxt *phy_ctxt;
5063 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5064 	int ret;
5065 
5066 	lockdep_assert_held(&mvm->mutex);
5067 
5068 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
5069 
5070 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
5071 	if (!phy_ctxt) {
5072 		ret = -ENOSPC;
5073 		goto out;
5074 	}
5075 
5076 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
5077 				   ctx->rx_chains_static,
5078 				   ctx->rx_chains_dynamic);
5079 	if (ret) {
5080 		IWL_ERR(mvm, "Failed to add PHY context\n");
5081 		goto out;
5082 	}
5083 
5084 	*phy_ctxt_id = phy_ctxt->id;
5085 out:
5086 	return ret;
5087 }
5088 
iwl_mvm_add_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5089 int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
5090 			struct ieee80211_chanctx_conf *ctx)
5091 {
5092 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5093 
5094 	guard(mvm)(mvm);
5095 	return __iwl_mvm_add_chanctx(mvm, ctx);
5096 }
5097 
__iwl_mvm_remove_chanctx(struct iwl_mvm * mvm,struct ieee80211_chanctx_conf * ctx)5098 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
5099 				     struct ieee80211_chanctx_conf *ctx)
5100 {
5101 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5102 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5103 
5104 	lockdep_assert_held(&mvm->mutex);
5105 
5106 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
5107 }
5108 
iwl_mvm_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx)5109 void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
5110 			    struct ieee80211_chanctx_conf *ctx)
5111 {
5112 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5113 
5114 	guard(mvm)(mvm);
5115 	__iwl_mvm_remove_chanctx(mvm, ctx);
5116 }
5117 
iwl_mvm_change_chanctx(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * ctx,u32 changed)5118 void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
5119 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
5120 {
5121 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5122 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5123 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5124 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5125 
5126 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
5127 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
5128 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
5129 				   IEEE80211_CHANCTX_CHANGE_RADAR |
5130 				   IEEE80211_CHANCTX_CHANGE_MIN_DEF)),
5131 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
5132 		      phy_ctxt->ref, changed))
5133 		return;
5134 
5135 	guard(mvm)(mvm);
5136 
5137 	/* we are only changing the min_width, may be a noop */
5138 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_DEF) {
5139 		if (phy_ctxt->width == def->width)
5140 			return;
5141 
5142 		/* we are just toggling between 20_NOHT and 20 */
5143 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5144 		    def->width <= NL80211_CHAN_WIDTH_20)
5145 			return;
5146 	}
5147 
5148 	iwl_mvm_bt_coex_vif_change(mvm);
5149 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5150 				 ctx->rx_chains_static,
5151 				 ctx->rx_chains_dynamic);
5152 }
5153 
5154 /*
5155  * This function executes the common part for MLD and non-MLD modes.
5156  *
5157  * Returns true if we're done assigning the chanctx
5158  * (either on failure or success)
5159  */
5160 static bool
__iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * ctx,bool switching_chanctx,int * ret)5161 __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5162 				    struct ieee80211_vif *vif,
5163 				    struct ieee80211_chanctx_conf *ctx,
5164 				    bool switching_chanctx, int *ret)
5165 {
5166 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5167 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5168 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5169 
5170 	lockdep_assert_held(&mvm->mutex);
5171 
5172 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5173 
5174 	switch (vif->type) {
5175 	case NL80211_IFTYPE_AP:
5176 		/* only needed if we're switching chanctx (i.e. during CSA) */
5177 		if (switching_chanctx) {
5178 			mvmvif->ap_ibss_active = true;
5179 			break;
5180 		}
5181 		fallthrough;
5182 	case NL80211_IFTYPE_ADHOC:
5183 		/*
5184 		 * The AP binding flow is handled as part of the start_ap flow
5185 		 * (in bss_info_changed), similarly for IBSS.
5186 		 */
5187 		*ret = 0;
5188 		return true;
5189 	case NL80211_IFTYPE_STATION:
5190 		break;
5191 	case NL80211_IFTYPE_MONITOR:
5192 		/* always disable PS when a monitor interface is active */
5193 		mvmvif->ps_disabled = true;
5194 		break;
5195 	default:
5196 		*ret = -EINVAL;
5197 		return true;
5198 	}
5199 	return false;
5200 }
5201 
__iwl_mvm_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)5202 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
5203 					struct ieee80211_vif *vif,
5204 					struct ieee80211_bss_conf *link_conf,
5205 					struct ieee80211_chanctx_conf *ctx,
5206 					bool switching_chanctx)
5207 {
5208 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5209 	int ret;
5210 
5211 	if (WARN_ON(!link_conf))
5212 		return -EINVAL;
5213 
5214 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
5215 						switching_chanctx, &ret))
5216 		goto out;
5217 
5218 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5219 	if (ret)
5220 		goto out;
5221 
5222 	/*
5223 	 * Power state must be updated before quotas,
5224 	 * otherwise fw will complain.
5225 	 */
5226 	iwl_mvm_power_update_mac(mvm);
5227 
5228 	/* Setting the quota at this stage is only required for monitor
5229 	 * interfaces. For the other types, the bss_info changed flow
5230 	 * will handle quota settings.
5231 	 */
5232 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5233 		mvmvif->monitor_active = true;
5234 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5235 		if (ret)
5236 			goto out_remove_binding;
5237 
5238 		ret = iwl_mvm_add_snif_sta(mvm, vif);
5239 		if (ret)
5240 			goto out_remove_binding;
5241 
5242 	}
5243 
5244 	/* Handle binding during CSA */
5245 	if (vif->type == NL80211_IFTYPE_AP) {
5246 		iwl_mvm_update_quotas(mvm, false, NULL);
5247 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5248 	}
5249 
5250 	if (vif->type == NL80211_IFTYPE_STATION) {
5251 		if (!switching_chanctx) {
5252 			mvmvif->csa_bcn_pending = false;
5253 			goto out;
5254 		}
5255 
5256 		mvmvif->csa_bcn_pending = true;
5257 
5258 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5259 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5260 			u32 duration = 5 * vif->bss_conf.beacon_int;
5261 
5262 			/* Protect the session to make sure we hear the first
5263 			 * beacon on the new channel.
5264 			 */
5265 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5266 						vif->bss_conf.beacon_int / 2,
5267 						true);
5268 		}
5269 
5270 		iwl_mvm_update_quotas(mvm, false, NULL);
5271 
5272 		iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
5273 							link_conf,
5274 							false);
5275 	}
5276 
5277 	goto out;
5278 
5279 out_remove_binding:
5280 	iwl_mvm_binding_remove_vif(mvm, vif);
5281 	iwl_mvm_power_update_mac(mvm);
5282 out:
5283 	if (ret)
5284 		mvmvif->deflink.phy_ctxt = NULL;
5285 	return ret;
5286 }
5287 
iwl_mvm_assign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)5288 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5289 				      struct ieee80211_vif *vif,
5290 				      struct ieee80211_bss_conf *link_conf,
5291 				      struct ieee80211_chanctx_conf *ctx)
5292 {
5293 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5294 
5295 	guard(mvm)(mvm);
5296 	return __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5297 }
5298 
5299 /*
5300  * This function executes the common part for MLD and non-MLD modes.
5301  *
5302  * Returns if chanctx unassign chanctx is done
5303  * (either on failure or success)
5304  */
__iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm * mvm,struct ieee80211_vif * vif,bool switching_chanctx)5305 static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5306 						  struct ieee80211_vif *vif,
5307 						  bool switching_chanctx)
5308 {
5309 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5310 
5311 	lockdep_assert_held(&mvm->mutex);
5312 	iwl_mvm_remove_time_event(mvm, mvmvif,
5313 				  &mvmvif->time_event_data);
5314 
5315 	switch (vif->type) {
5316 	case NL80211_IFTYPE_ADHOC:
5317 		return true;
5318 	case NL80211_IFTYPE_MONITOR:
5319 		mvmvif->monitor_active = false;
5320 		mvmvif->ps_disabled = false;
5321 		break;
5322 	case NL80211_IFTYPE_AP:
5323 		/* This part is triggered only during CSA */
5324 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5325 			return true;
5326 
5327 		mvmvif->csa_countdown = false;
5328 
5329 		/* Set CS bit on all the stations */
5330 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5331 
5332 		/* Save blocked iface, the timeout is set on the next beacon */
5333 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5334 
5335 		mvmvif->ap_ibss_active = false;
5336 		break;
5337 	default:
5338 		break;
5339 	}
5340 	return false;
5341 }
5342 
__iwl_mvm_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)5343 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5344 					   struct ieee80211_vif *vif,
5345 					   struct ieee80211_bss_conf *link_conf,
5346 					   struct ieee80211_chanctx_conf *ctx,
5347 					   bool switching_chanctx)
5348 {
5349 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5350 	struct ieee80211_vif *disabled_vif = NULL;
5351 
5352 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5353 		goto out;
5354 
5355 	if (vif->type == NL80211_IFTYPE_MONITOR)
5356 		iwl_mvm_rm_snif_sta(mvm, vif);
5357 
5358 
5359 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5360 		disabled_vif = vif;
5361 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5362 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5363 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5364 	}
5365 
5366 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5367 	iwl_mvm_binding_remove_vif(mvm, vif);
5368 
5369 out:
5370 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5371 	    switching_chanctx)
5372 		return;
5373 	mvmvif->deflink.phy_ctxt = NULL;
5374 	iwl_mvm_power_update_mac(mvm);
5375 }
5376 
iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * link_conf,struct ieee80211_chanctx_conf * ctx)5377 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5378 					 struct ieee80211_vif *vif,
5379 					 struct ieee80211_bss_conf *link_conf,
5380 					 struct ieee80211_chanctx_conf *ctx)
5381 {
5382 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5383 
5384 	guard(mvm)(mvm);
5385 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5386 }
5387 
5388 static int
iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm * mvm,struct ieee80211_vif_chanctx_switch * vifs,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5389 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5390 				struct ieee80211_vif_chanctx_switch *vifs,
5391 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5392 {
5393 	int ret;
5394 
5395 	guard(mvm)(mvm);
5396 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5397 				    vifs[0].old_ctx, true);
5398 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5399 
5400 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5401 	if (ret) {
5402 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5403 		goto out_reassign;
5404 	}
5405 
5406 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5407 					vifs[0].new_ctx, true);
5408 	if (ret) {
5409 		IWL_ERR(mvm,
5410 			"failed to assign new_ctx during channel switch\n");
5411 		goto out_remove;
5412 	}
5413 
5414 	/* we don't support TDLS during DCM - can be caused by channel switch */
5415 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5416 		iwl_mvm_teardown_tdls_peers(mvm);
5417 
5418 	return 0;
5419 
5420 out_remove:
5421 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5422 
5423 out_reassign:
5424 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5425 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5426 		goto out_restart;
5427 	}
5428 
5429 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5430 				      vifs[0].old_ctx, true)) {
5431 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5432 		goto out_restart;
5433 	}
5434 
5435 	return ret;
5436 
5437 out_restart:
5438 	/* things keep failing, better restart the hw */
5439 	iwl_force_nmi(mvm->trans);
5440 	return ret;
5441 }
5442 
5443 static int
iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm * mvm,struct ieee80211_vif_chanctx_switch * vifs,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5444 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5445 				    struct ieee80211_vif_chanctx_switch *vifs,
5446 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5447 {
5448 	int ret;
5449 
5450 	guard(mvm)(mvm);
5451 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5452 				    vifs[0].old_ctx, true);
5453 
5454 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5455 					vifs[0].new_ctx, true);
5456 	if (ret) {
5457 		IWL_ERR(mvm,
5458 			"failed to assign new_ctx during channel switch\n");
5459 		goto out_reassign;
5460 	}
5461 
5462 	return 0;
5463 
5464 out_reassign:
5465 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5466 				      vifs[0].old_ctx, true)) {
5467 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5468 		goto out_restart;
5469 	}
5470 
5471 	return ret;
5472 
5473 out_restart:
5474 	/* things keep failing, better restart the hw */
5475 	iwl_force_nmi(mvm->trans);
5476 	return ret;
5477 }
5478 
5479 /* Execute the common part for both MLD and non-MLD modes */
5480 int
iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode,const struct iwl_mvm_switch_vif_chanctx_ops * ops)5481 iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5482 				  struct ieee80211_vif_chanctx_switch *vifs,
5483 				  int n_vifs,
5484 				  enum ieee80211_chanctx_switch_mode mode,
5485 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5486 {
5487 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5488 	int ret;
5489 
5490 	/* we only support a single-vif right now */
5491 	if (n_vifs > 1)
5492 		return -EOPNOTSUPP;
5493 
5494 	switch (mode) {
5495 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5496 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5497 		break;
5498 	case CHANCTX_SWMODE_REASSIGN_VIF:
5499 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5500 		break;
5501 	default:
5502 		ret = -EOPNOTSUPP;
5503 		break;
5504 	}
5505 
5506 	return ret;
5507 }
5508 
iwl_mvm_switch_vif_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif_chanctx_switch * vifs,int n_vifs,enum ieee80211_chanctx_switch_mode mode)5509 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5510 				      struct ieee80211_vif_chanctx_switch *vifs,
5511 				      int n_vifs,
5512 				      enum ieee80211_chanctx_switch_mode mode)
5513 {
5514 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5515 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5516 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5517 	};
5518 
5519 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5520 }
5521 
iwl_mvm_tx_last_beacon(struct ieee80211_hw * hw)5522 int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5523 {
5524 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5525 
5526 	return mvm->ibss_manager;
5527 }
5528 
iwl_mvm_set_tim(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool set)5529 static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5530 			   bool set)
5531 {
5532 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5533 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5534 
5535 	if (!mvm_sta || !mvm_sta->vif) {
5536 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5537 		return -EINVAL;
5538 	}
5539 
5540 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5541 					       &mvm_sta->vif->bss_conf);
5542 }
5543 
iwl_mvm_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5544 void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5545 			    struct ieee80211_channel_switch *chsw)
5546 {
5547 	/* By implementing this operation, we prevent mac80211 from
5548 	 * starting its own channel switch timer, so that we can call
5549 	 * ieee80211_chswitch_done() ourselves at the right time
5550 	 * (which is when the absence time event starts).
5551 	 */
5552 
5553 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5554 			   "dummy channel switch op\n");
5555 }
5556 
iwl_mvm_schedule_client_csa(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5557 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5558 				       struct ieee80211_vif *vif,
5559 				       struct ieee80211_channel_switch *chsw)
5560 {
5561 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5562 	struct iwl_chan_switch_te_cmd cmd = {
5563 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5564 							  mvmvif->color)),
5565 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5566 		.tsf = cpu_to_le32(chsw->timestamp),
5567 		.cs_count = chsw->count,
5568 		.cs_mode = chsw->block_tx,
5569 	};
5570 
5571 	lockdep_assert_held(&mvm->mutex);
5572 
5573 	if (chsw->delay)
5574 		cmd.cs_delayed_bcn_count =
5575 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5576 
5577 	return iwl_mvm_send_cmd_pdu(mvm,
5578 				    WIDE_ID(MAC_CONF_GROUP,
5579 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
5580 				    0, sizeof(cmd), &cmd);
5581 }
5582 
iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5583 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5584 				       struct ieee80211_vif *vif,
5585 				       struct ieee80211_channel_switch *chsw)
5586 {
5587 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5588 	u32 apply_time;
5589 
5590 	/* Schedule the time event to a bit before beacon 1,
5591 	 * to make sure we're in the new channel when the
5592 	 * GO/AP arrives. In case count <= 1 immediately schedule the
5593 	 * TE (this might result with some packet loss or connection
5594 	 * loss).
5595 	 */
5596 	if (chsw->count <= 1)
5597 		apply_time = 0;
5598 	else
5599 		apply_time = chsw->device_timestamp +
5600 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
5601 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5602 
5603 	if (chsw->block_tx)
5604 		iwl_mvm_csa_client_absent(mvm, vif);
5605 
5606 	if (mvmvif->bf_enabled) {
5607 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
5608 
5609 		if (ret)
5610 			return ret;
5611 	}
5612 
5613 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5614 				    apply_time);
5615 
5616 	return 0;
5617 }
5618 
iwl_mvm_csa_block_txqs(void * data,struct ieee80211_sta * sta)5619 static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5620 {
5621 	int i;
5622 
5623 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5624 		struct iwl_mvm_txq *mvmtxq =
5625 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5626 
5627 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5628 	}
5629 }
5630 
5631 #define IWL_MAX_CSA_BLOCK_TX 1500
iwl_mvm_pre_channel_switch(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5632 int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5633 			       struct ieee80211_vif *vif,
5634 			       struct ieee80211_channel_switch *chsw)
5635 {
5636 	struct ieee80211_vif *csa_vif;
5637 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5638 	struct iwl_mvm_txq *mvmtxq;
5639 	int ret;
5640 
5641 	lockdep_assert_held(&mvm->mutex);
5642 
5643 	mvmvif->csa_failed = false;
5644 	mvmvif->csa_blocks_tx = false;
5645 
5646 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5647 			   chsw->chandef.center_freq1);
5648 
5649 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5650 				       ieee80211_vif_to_wdev(vif),
5651 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5652 
5653 	switch (vif->type) {
5654 	case NL80211_IFTYPE_AP:
5655 		csa_vif =
5656 			rcu_dereference_protected(mvm->csa_vif,
5657 						  lockdep_is_held(&mvm->mutex));
5658 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5659 			      "Another CSA is already in progress"))
5660 			return -EBUSY;
5661 
5662 		/* we still didn't unblock tx. prevent new CS meanwhile */
5663 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5664 					      lockdep_is_held(&mvm->mutex)))
5665 			return -EBUSY;
5666 
5667 		rcu_assign_pointer(mvm->csa_vif, vif);
5668 
5669 		if (WARN_ONCE(mvmvif->csa_countdown,
5670 			      "Previous CSA countdown didn't complete"))
5671 			return -EBUSY;
5672 
5673 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5674 
5675 		if (!chsw->block_tx)
5676 			break;
5677 		/* don't need blocking in driver otherwise - mac80211 will do */
5678 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5679 			break;
5680 
5681 		mvmvif->csa_blocks_tx = true;
5682 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5683 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5684 		ieee80211_iterate_stations_atomic(mvm->hw,
5685 						  iwl_mvm_csa_block_txqs,
5686 						  NULL);
5687 		break;
5688 	case NL80211_IFTYPE_STATION:
5689 		mvmvif->csa_blocks_tx = chsw->block_tx;
5690 
5691 		/*
5692 		 * In the new flow FW is in charge of timing the switch so there
5693 		 * is no need for all of this
5694 		 */
5695 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5696 					    CHANNEL_SWITCH_ERROR_NOTIF,
5697 					    0))
5698 			break;
5699 
5700 		/*
5701 		 * We haven't configured the firmware to be associated yet since
5702 		 * we don't know the dtim period. In this case, the firmware can't
5703 		 * track the beacons.
5704 		 */
5705 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
5706 			return -EBUSY;
5707 
5708 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5709 		    hweight16(vif->valid_links) <= 1)
5710 			schedule_delayed_work(&mvmvif->csa_work, 0);
5711 
5712 		if (chsw->block_tx) {
5713 			/*
5714 			 * In case of undetermined / long time with immediate
5715 			 * quiet monitor status to gracefully disconnect
5716 			 */
5717 			if (!chsw->count ||
5718 			    chsw->count * vif->bss_conf.beacon_int >
5719 			    IWL_MAX_CSA_BLOCK_TX)
5720 				schedule_delayed_work(&mvmvif->csa_work,
5721 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5722 		}
5723 
5724 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5725 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5726 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5727 			if (ret)
5728 				return ret;
5729 		} else {
5730 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5731 		}
5732 
5733 		mvmvif->csa_count = chsw->count;
5734 		mvmvif->csa_misbehave = false;
5735 		break;
5736 	default:
5737 		break;
5738 	}
5739 
5740 	mvmvif->ps_disabled = true;
5741 
5742 	ret = iwl_mvm_power_update_ps(mvm);
5743 	if (ret)
5744 		return ret;
5745 
5746 	/* we won't be on this channel any longer */
5747 	iwl_mvm_teardown_tdls_peers(mvm);
5748 
5749 	return ret;
5750 }
5751 
iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5752 static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
5753 					  struct ieee80211_vif *vif,
5754 					  struct ieee80211_channel_switch *chsw)
5755 {
5756 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5757 
5758 	guard(mvm)(mvm);
5759 	return iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5760 }
5761 
iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_channel_switch * chsw)5762 void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5763 				      struct ieee80211_vif *vif,
5764 				      struct ieee80211_channel_switch *chsw)
5765 {
5766 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5767 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5768 	struct iwl_chan_switch_te_cmd cmd = {
5769 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5770 							  mvmvif->color)),
5771 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5772 		.tsf = cpu_to_le32(chsw->timestamp),
5773 		.cs_count = chsw->count,
5774 		.cs_mode = chsw->block_tx,
5775 	};
5776 
5777 	/*
5778 	 * In the new flow FW is in charge of timing the switch so there is no
5779 	 * need for all of this
5780 	 */
5781 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5782 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5783 		return;
5784 
5785 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5786 		return;
5787 
5788 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5789 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5790 
5791 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5792 		if (mvmvif->csa_misbehave) {
5793 			struct ieee80211_bss_conf *link_conf;
5794 
5795 			/* Second time, give up on this AP*/
5796 
5797 			link_conf = wiphy_dereference(hw->wiphy,
5798 						      vif->link_conf[chsw->link_id]);
5799 			if (WARN_ON(!link_conf))
5800 				return;
5801 
5802 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5803 			ieee80211_chswitch_done(vif, false, 0);
5804 			mvmvif->csa_misbehave = false;
5805 			return;
5806 		}
5807 		mvmvif->csa_misbehave = true;
5808 	}
5809 	mvmvif->csa_count = chsw->count;
5810 
5811 	guard(mvm)(mvm);
5812 	if (mvmvif->csa_failed)
5813 		return;
5814 
5815 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5816 				     WIDE_ID(MAC_CONF_GROUP,
5817 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5818 				     0, sizeof(cmd), &cmd));
5819 }
5820 
iwl_mvm_flush_no_vif(struct iwl_mvm * mvm,u32 queues,bool drop)5821 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5822 {
5823 	int i;
5824 
5825 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5826 		/* we can't ask the firmware anything if it is dead */
5827 		if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
5828 			     &mvm->status))
5829 			return;
5830 		if (drop) {
5831 			guard(mvm)(mvm);
5832 			iwl_mvm_flush_tx_path(mvm,
5833 				iwl_mvm_flushable_queues(mvm) & queues);
5834 		} else {
5835 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5836 		}
5837 		return;
5838 	}
5839 
5840 	guard(mvm)(mvm);
5841 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5842 		struct ieee80211_sta *sta;
5843 
5844 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5845 						lockdep_is_held(&mvm->mutex));
5846 		if (IS_ERR_OR_NULL(sta))
5847 			continue;
5848 
5849 		if (drop)
5850 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5851 		else
5852 			iwl_mvm_wait_sta_queues_empty(mvm,
5853 					iwl_mvm_sta_from_mac80211(sta));
5854 	}
5855 }
5856 
iwl_mvm_mac_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)5857 void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5858 		       u32 queues, bool drop)
5859 {
5860 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5861 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5862 	struct iwl_mvm_sta *mvmsta;
5863 	struct ieee80211_sta *sta;
5864 	bool ap_sta_done = false;
5865 	int i;
5866 	u32 msk = 0;
5867 
5868 	if (!vif) {
5869 		iwl_mvm_flush_no_vif(mvm, queues, drop);
5870 		return;
5871 	}
5872 
5873 	if (!drop && hweight16(vif->active_links) <= 1) {
5874 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
5875 		struct ieee80211_bss_conf *link_conf;
5876 
5877 		link_conf = wiphy_dereference(hw->wiphy,
5878 					      vif->link_conf[link_id]);
5879 		if (WARN_ON(!link_conf))
5880 			return;
5881 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
5882 			drop = true;
5883 	}
5884 
5885 	/* Make sure we're done with the deferred traffic before flushing */
5886 	flush_work(&mvm->add_stream_wk);
5887 
5888 	mutex_lock(&mvm->mutex);
5889 
5890 	/* flush the AP-station and all TDLS peers */
5891 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5892 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5893 						lockdep_is_held(&mvm->mutex));
5894 		if (IS_ERR_OR_NULL(sta))
5895 			continue;
5896 
5897 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5898 		if (mvmsta->vif != vif)
5899 			continue;
5900 
5901 		if (sta == mvmvif->ap_sta) {
5902 			if (ap_sta_done)
5903 				continue;
5904 			ap_sta_done = true;
5905 		}
5906 
5907 		if (drop) {
5908 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5909 					      mvmsta->tfd_queue_msk))
5910 				IWL_ERR(mvm, "flush request fail\n");
5911 		} else {
5912 			if (iwl_mvm_has_new_tx_api(mvm))
5913 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5914 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5915 				msk |= mvmsta->tfd_queue_msk;
5916 		}
5917 	}
5918 
5919 	mutex_unlock(&mvm->mutex);
5920 
5921 	/* this can take a while, and we may need/want other operations
5922 	 * to succeed while doing this, so do it without the mutex held
5923 	 * If the firmware is dead, this can't work...
5924 	 */
5925 	if (!drop && !iwl_mvm_has_new_tx_api(mvm) &&
5926 	    !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
5927 		      &mvm->status))
5928 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5929 }
5930 
iwl_mvm_mac_flush_sta(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)5931 void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5932 			   struct ieee80211_sta *sta)
5933 {
5934 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5935 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5936 	struct iwl_mvm_link_sta *mvm_link_sta;
5937 	struct ieee80211_link_sta *link_sta;
5938 	int link_id;
5939 
5940 	guard(mvm)(mvm);
5941 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
5942 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5943 							 lockdep_is_held(&mvm->mutex));
5944 		if (!mvm_link_sta)
5945 			continue;
5946 
5947 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5948 				      mvmsta->tfd_queue_msk))
5949 			IWL_ERR(mvm, "flush request fail\n");
5950 	}
5951 }
5952 
iwl_mvm_mac_get_acs_survey(struct iwl_mvm * mvm,int idx,struct survey_info * survey)5953 static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5954 				      struct survey_info *survey)
5955 {
5956 	int chan_idx;
5957 	enum nl80211_band band;
5958 	int ret;
5959 
5960 	mutex_lock(&mvm->mutex);
5961 
5962 	if (!mvm->acs_survey) {
5963 		ret = -ENOENT;
5964 		goto out;
5965 	}
5966 
5967 	/* Find and return the next entry that has a non-zero active time */
5968 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5969 		struct ieee80211_supported_band *sband =
5970 			mvm->hw->wiphy->bands[band];
5971 
5972 		if (!sband)
5973 			continue;
5974 
5975 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5976 			struct iwl_mvm_acs_survey_channel *info =
5977 				&mvm->acs_survey->bands[band][chan_idx];
5978 
5979 			if (!info->time)
5980 				continue;
5981 
5982 			/* Found (the next) channel to report */
5983 			survey->channel = &sband->channels[chan_idx];
5984 			survey->filled = SURVEY_INFO_TIME |
5985 					 SURVEY_INFO_TIME_BUSY |
5986 					 SURVEY_INFO_TIME_RX |
5987 					 SURVEY_INFO_TIME_TX;
5988 			survey->time = info->time;
5989 			survey->time_busy = info->time_busy;
5990 			survey->time_rx = info->time_rx;
5991 			survey->time_tx = info->time_tx;
5992 			survey->noise = info->noise;
5993 			if (survey->noise < 0)
5994 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5995 
5996 			/* Clear time so that channel is only reported once */
5997 			info->time = 0;
5998 
5999 			ret = 0;
6000 			goto out;
6001 		}
6002 	}
6003 
6004 	ret = -ENOENT;
6005 
6006 out:
6007 	mutex_unlock(&mvm->mutex);
6008 
6009 	return ret;
6010 }
6011 
iwl_mvm_mac_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)6012 int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
6013 			   struct survey_info *survey)
6014 {
6015 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6016 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
6017 					   WIDE_ID(SYSTEM_GROUP,
6018 						   SYSTEM_STATISTICS_CMD),
6019 					   IWL_FW_CMD_VER_UNKNOWN);
6020 
6021 	memset(survey, 0, sizeof(*survey));
6022 
6023 	if (!fw_has_capa(&mvm->fw->ucode_capa,
6024 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
6025 		return -ENOENT;
6026 
6027 	/*
6028 	 * Return the beacon stats at index zero and pass on following indices
6029 	 * to the function returning the full survey, most likely for ACS
6030 	 * (Automatic Channel Selection).
6031 	 */
6032 	if (idx > 0)
6033 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
6034 
6035 	guard(mvm)(mvm);
6036 
6037 	if (iwl_mvm_firmware_running(mvm)) {
6038 		int ret = iwl_mvm_request_statistics(mvm, false);
6039 
6040 		if (ret)
6041 			return ret;
6042 	}
6043 
6044 	survey->filled = SURVEY_INFO_TIME_RX |
6045 			 SURVEY_INFO_TIME_TX;
6046 
6047 	survey->time_rx = mvm->accu_radio_stats.rx_time +
6048 			  mvm->radio_stats.rx_time;
6049 	do_div(survey->time_rx, USEC_PER_MSEC);
6050 
6051 	survey->time_tx = mvm->accu_radio_stats.tx_time +
6052 			  mvm->radio_stats.tx_time;
6053 	do_div(survey->time_tx, USEC_PER_MSEC);
6054 
6055 	/* the new fw api doesn't support the following fields */
6056 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
6057 		return 0;
6058 
6059 	survey->filled |= SURVEY_INFO_TIME |
6060 			  SURVEY_INFO_TIME_SCAN;
6061 	survey->time = mvm->accu_radio_stats.on_time_rf +
6062 		       mvm->radio_stats.on_time_rf;
6063 	do_div(survey->time, USEC_PER_MSEC);
6064 
6065 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6066 			    mvm->radio_stats.on_time_scan;
6067 	do_div(survey->time_scan, USEC_PER_MSEC);
6068 
6069 	return 0;
6070 }
6071 
iwl_mvm_set_sta_rate(u32 rate_n_flags,struct rate_info * rinfo)6072 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6073 {
6074 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
6075 	u32 gi_ltf;
6076 
6077 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6078 	case RATE_MCS_CHAN_WIDTH_20:
6079 		rinfo->bw = RATE_INFO_BW_20;
6080 		break;
6081 	case RATE_MCS_CHAN_WIDTH_40:
6082 		rinfo->bw = RATE_INFO_BW_40;
6083 		break;
6084 	case RATE_MCS_CHAN_WIDTH_80:
6085 		rinfo->bw = RATE_INFO_BW_80;
6086 		break;
6087 	case RATE_MCS_CHAN_WIDTH_160:
6088 		rinfo->bw = RATE_INFO_BW_160;
6089 		break;
6090 	case RATE_MCS_CHAN_WIDTH_320:
6091 		rinfo->bw = RATE_INFO_BW_320;
6092 		break;
6093 	}
6094 
6095 	if (format == RATE_MCS_MOD_TYPE_CCK ||
6096 	    format == RATE_MCS_MOD_TYPE_LEGACY_OFDM) {
6097 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
6098 
6099 		/* add the offset needed to get to the legacy ofdm indices */
6100 		if (format == RATE_MCS_MOD_TYPE_LEGACY_OFDM)
6101 			rate += IWL_FIRST_OFDM_RATE;
6102 
6103 		switch (rate) {
6104 		case IWL_RATE_1M_INDEX:
6105 			rinfo->legacy = 10;
6106 			break;
6107 		case IWL_RATE_2M_INDEX:
6108 			rinfo->legacy = 20;
6109 			break;
6110 		case IWL_RATE_5M_INDEX:
6111 			rinfo->legacy = 55;
6112 			break;
6113 		case IWL_RATE_11M_INDEX:
6114 			rinfo->legacy = 110;
6115 			break;
6116 		case IWL_RATE_6M_INDEX:
6117 			rinfo->legacy = 60;
6118 			break;
6119 		case IWL_RATE_9M_INDEX:
6120 			rinfo->legacy = 90;
6121 			break;
6122 		case IWL_RATE_12M_INDEX:
6123 			rinfo->legacy = 120;
6124 			break;
6125 		case IWL_RATE_18M_INDEX:
6126 			rinfo->legacy = 180;
6127 			break;
6128 		case IWL_RATE_24M_INDEX:
6129 			rinfo->legacy = 240;
6130 			break;
6131 		case IWL_RATE_36M_INDEX:
6132 			rinfo->legacy = 360;
6133 			break;
6134 		case IWL_RATE_48M_INDEX:
6135 			rinfo->legacy = 480;
6136 			break;
6137 		case IWL_RATE_54M_INDEX:
6138 			rinfo->legacy = 540;
6139 		}
6140 		return;
6141 	}
6142 
6143 	rinfo->nss = u32_get_bits(rate_n_flags,
6144 				  RATE_MCS_NSS_MSK) + 1;
6145 	rinfo->mcs = format == RATE_MCS_MOD_TYPE_HT ?
6146 		RATE_HT_MCS_INDEX(rate_n_flags) :
6147 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
6148 
6149 	if (rate_n_flags & RATE_MCS_SGI_MSK)
6150 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6151 
6152 	switch (format) {
6153 	case RATE_MCS_MOD_TYPE_EHT:
6154 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
6155 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
6156 		break;
6157 	case RATE_MCS_MOD_TYPE_HE:
6158 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6159 
6160 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6161 
6162 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6163 			rinfo->bw = RATE_INFO_BW_HE_RU;
6164 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6165 		}
6166 
6167 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
6168 		case RATE_MCS_HE_TYPE_SU:
6169 		case RATE_MCS_HE_TYPE_EXT_SU:
6170 			if (gi_ltf == 0 || gi_ltf == 1)
6171 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6172 			else if (gi_ltf == 2)
6173 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6174 			else if (gi_ltf == 3)
6175 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6176 			else
6177 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6178 			break;
6179 		case RATE_MCS_HE_TYPE_MU:
6180 			if (gi_ltf == 0 || gi_ltf == 1)
6181 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6182 			else if (gi_ltf == 2)
6183 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6184 			else
6185 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6186 			break;
6187 		case RATE_MCS_HE_TYPE_TRIG:
6188 			if (gi_ltf == 0 || gi_ltf == 1)
6189 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6190 			else
6191 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6192 			break;
6193 		}
6194 
6195 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6196 			rinfo->he_dcm = 1;
6197 		break;
6198 	case RATE_MCS_MOD_TYPE_HT:
6199 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
6200 		break;
6201 	case RATE_MCS_MOD_TYPE_VHT:
6202 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
6203 		break;
6204 	}
6205 }
6206 
iwl_mvm_mac_sta_statistics(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct station_info * sinfo)6207 void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6208 				struct ieee80211_vif *vif,
6209 				struct ieee80211_sta *sta,
6210 				struct station_info *sinfo)
6211 {
6212 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6213 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6214 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6215 	int i;
6216 
6217 	if (mvmsta->deflink.avg_energy) {
6218 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
6219 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6220 	}
6221 
6222 	if (iwl_mvm_has_tlc_offload(mvm)) {
6223 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6224 
6225 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6226 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6227 	}
6228 
6229 	/* if beacon filtering isn't on mac80211 does it anyway */
6230 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6231 		return;
6232 
6233 	if (!vif->cfg.assoc)
6234 		return;
6235 
6236 	guard(mvm)(mvm);
6237 
6238 	if (sta != mvmvif->ap_sta)
6239 		return;
6240 
6241 	if (iwl_mvm_request_statistics(mvm, false))
6242 		return;
6243 
6244 	sinfo->rx_beacon = 0;
6245 	for_each_mvm_vif_valid_link(mvmvif, i)
6246 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6247 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6248 
6249 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6250 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6251 		/* firmware only reports a value after RXing a few beacons */
6252 		sinfo->rx_beacon_signal_avg =
6253 			mvmvif->deflink.beacon_stats.avg_signal;
6254 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6255 	}
6256 }
6257 
iwl_mvm_event_mlme_callback_ini(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_mlme_event * mlme)6258 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6259 					    struct ieee80211_vif *vif,
6260 					    const  struct ieee80211_mlme_event *mlme)
6261 {
6262 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
6263 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6264 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6265 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6266 				       NULL);
6267 		return;
6268 	}
6269 
6270 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6271 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6272 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6273 				       NULL);
6274 		return;
6275 	}
6276 }
6277 
iwl_mvm_event_mlme_callback(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_event * event)6278 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6279 					struct ieee80211_vif *vif,
6280 					const struct ieee80211_event *event)
6281 {
6282 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6283 	do {								\
6284 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6285 			break;						\
6286 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6287 	} while (0)
6288 
6289 	struct iwl_fw_dbg_trigger_tlv *trig;
6290 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6291 
6292 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6293 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6294 		return;
6295 	}
6296 
6297 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6298 				     FW_DBG_TRIGGER_MLME);
6299 	if (!trig)
6300 		return;
6301 
6302 	trig_mlme = (void *)trig->data;
6303 
6304 	if (event->u.mlme.data == ASSOC_EVENT) {
6305 		if (event->u.mlme.status == MLME_DENIED)
6306 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6307 					   "DENIED ASSOC: reason %d",
6308 					    event->u.mlme.reason);
6309 		else if (event->u.mlme.status == MLME_TIMEOUT)
6310 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6311 					   "ASSOC TIMEOUT");
6312 	} else if (event->u.mlme.data == AUTH_EVENT) {
6313 		if (event->u.mlme.status == MLME_DENIED)
6314 			CHECK_MLME_TRIGGER(stop_auth_denied,
6315 					   "DENIED AUTH: reason %d",
6316 					   event->u.mlme.reason);
6317 		else if (event->u.mlme.status == MLME_TIMEOUT)
6318 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6319 					   "AUTH TIMEOUT");
6320 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
6321 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6322 				   "DEAUTH RX %d", event->u.mlme.reason);
6323 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
6324 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6325 				   "DEAUTH TX %d", event->u.mlme.reason);
6326 	}
6327 #undef CHECK_MLME_TRIGGER
6328 }
6329 
iwl_mvm_event_bar_rx_callback(struct iwl_mvm * mvm,struct ieee80211_vif * vif,const struct ieee80211_event * event)6330 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6331 					  struct ieee80211_vif *vif,
6332 					  const struct ieee80211_event *event)
6333 {
6334 	struct iwl_fw_dbg_trigger_tlv *trig;
6335 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6336 
6337 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6338 				     FW_DBG_TRIGGER_BA);
6339 	if (!trig)
6340 		return;
6341 
6342 	ba_trig = (void *)trig->data;
6343 
6344 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6345 		return;
6346 
6347 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6348 				"BAR received from %pM, tid %d, ssn %d",
6349 				event->u.ba.sta->addr, event->u.ba.tid,
6350 				event->u.ba.ssn);
6351 }
6352 
iwl_mvm_mac_event_callback(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const struct ieee80211_event * event)6353 void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6354 				struct ieee80211_vif *vif,
6355 				const struct ieee80211_event *event)
6356 {
6357 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6358 
6359 	switch (event->type) {
6360 	case MLME_EVENT:
6361 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6362 		break;
6363 	case BAR_RX_EVENT:
6364 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6365 		break;
6366 	case BA_FRAME_TIMEOUT:
6367 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6368 						     event->u.ba.tid);
6369 		break;
6370 	default:
6371 		break;
6372 	}
6373 }
6374 
6375 #define SYNC_RX_QUEUE_TIMEOUT (HZ)
iwl_mvm_sync_rx_queues_internal(struct iwl_mvm * mvm,enum iwl_mvm_rxq_notif_type type,bool sync,const void * data,u32 size)6376 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
6377 				     enum iwl_mvm_rxq_notif_type type,
6378 				     bool sync,
6379 				     const void *data, u32 size)
6380 {
6381 	DEFINE_RAW_FLEX(struct iwl_rxq_sync_cmd, cmd, payload,
6382 			sizeof(struct iwl_mvm_internal_rxq_notif));
6383 	struct iwl_mvm_internal_rxq_notif *notif =
6384 			(struct iwl_mvm_internal_rxq_notif *)cmd->payload;
6385 	struct iwl_host_cmd hcmd = {
6386 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6387 		.data[0] = cmd,
6388 		.len[0] = __struct_size(cmd),
6389 		.data[1] = data,
6390 		.len[1] = size,
6391 		.flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
6392 	};
6393 	int ret;
6394 
6395 	cmd->rxq_mask = cpu_to_le32(BIT(mvm->trans->info.num_rxqs) - 1);
6396 	cmd->count = cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6397 				 size);
6398 	notif->type = type;
6399 	notif->sync = sync;
6400 
6401 	/* size must be a multiple of DWORD */
6402 	if (WARN_ON(cmd->count & cpu_to_le32(3)))
6403 		return;
6404 
6405 	if (!iwl_mvm_has_new_rx_api(mvm))
6406 		return;
6407 
6408 	if (sync) {
6409 		notif->cookie = mvm->queue_sync_cookie;
6410 		mvm->queue_sync_state = (1 << mvm->trans->info.num_rxqs) - 1;
6411 	}
6412 
6413 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
6414 	if (ret) {
6415 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6416 		goto out;
6417 	}
6418 
6419 	if (sync) {
6420 		lockdep_assert_held(&mvm->mutex);
6421 		ret = wait_event_timeout(mvm->rx_sync_waitq,
6422 					 READ_ONCE(mvm->queue_sync_state) == 0,
6423 					 SYNC_RX_QUEUE_TIMEOUT);
6424 		WARN_ONCE(!ret, "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6425 			  mvm->queue_sync_state,
6426 			  mvm->queue_sync_cookie);
6427 	}
6428 
6429 out:
6430 	if (sync) {
6431 		mvm->queue_sync_state = 0;
6432 		mvm->queue_sync_cookie++;
6433 	}
6434 }
6435 
iwl_mvm_sync_rx_queues(struct ieee80211_hw * hw)6436 void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
6437 {
6438 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6439 
6440 	guard(mvm)(mvm);
6441 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
6442 }
6443 
6444 int
iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_ftm_responder_stats * stats)6445 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6446 				    struct ieee80211_vif *vif,
6447 				    struct cfg80211_ftm_responder_stats *stats)
6448 {
6449 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6450 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6451 
6452 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6453 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6454 		return -EINVAL;
6455 
6456 	mutex_lock(&mvm->mutex);
6457 	*stats = mvm->ftm_resp_stats;
6458 	mutex_unlock(&mvm->mutex);
6459 
6460 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6461 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6462 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6463 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6464 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6465 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6466 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6467 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6468 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6469 
6470 	return 0;
6471 }
6472 
iwl_mvm_start_pmsr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_pmsr_request * request)6473 int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6474 		       struct cfg80211_pmsr_request *request)
6475 {
6476 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6477 
6478 	guard(mvm)(mvm);
6479 	return iwl_mvm_ftm_start(mvm, vif, request);
6480 }
6481 
iwl_mvm_abort_pmsr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_pmsr_request * request)6482 void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6483 			struct cfg80211_pmsr_request *request)
6484 {
6485 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6486 
6487 	guard(mvm)(mvm);
6488 	iwl_mvm_ftm_abort(mvm, request);
6489 }
6490 
iwl_mvm_can_hw_csum(struct sk_buff * skb)6491 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6492 {
6493 	u8 protocol = ip_hdr(skb)->protocol;
6494 
6495 	if (!IS_ENABLED(CONFIG_INET))
6496 		return false;
6497 
6498 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6499 }
6500 
iwl_mvm_mac_can_aggregate(struct ieee80211_hw * hw,struct sk_buff * head,struct sk_buff * skb)6501 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6502 				      struct sk_buff *head,
6503 				      struct sk_buff *skb)
6504 {
6505 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6506 
6507 	/* For now don't aggregate IPv6 in AMSDU */
6508 	if (skb->protocol != htons(ETH_P_IP))
6509 		return false;
6510 
6511 	if (!iwl_mvm_is_csum_supported(mvm))
6512 		return true;
6513 
6514 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6515 }
6516 
iwl_mvm_set_hw_timestamp(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_set_hw_timestamp * hwts)6517 int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6518 			     struct ieee80211_vif *vif,
6519 			     struct cfg80211_set_hw_timestamp *hwts)
6520 {
6521 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6522 	u32 protocols = 0;
6523 
6524 	/* HW timestamping is only supported for a specific station */
6525 	if (!hwts->macaddr)
6526 		return -EOPNOTSUPP;
6527 
6528 	if (hwts->enable)
6529 		protocols =
6530 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6531 
6532 	guard(mvm)(mvm);
6533 	return iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6534 }
6535 
6536 const struct ieee80211_ops iwl_mvm_hw_ops = {
6537 	.tx = iwl_mvm_mac_tx,
6538 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6539 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6540 	.get_antenna = iwl_mvm_op_get_antenna,
6541 	.set_antenna = iwl_mvm_op_set_antenna,
6542 	.start = iwl_mvm_mac_start,
6543 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6544 	.stop = iwl_mvm_mac_stop,
6545 	.add_interface = iwl_mvm_mac_add_interface,
6546 	.remove_interface = iwl_mvm_mac_remove_interface,
6547 	.config = iwl_mvm_mac_config,
6548 	.prepare_multicast = iwl_mvm_prepare_multicast,
6549 	.configure_filter = iwl_mvm_configure_filter,
6550 	.config_iface_filter = iwl_mvm_config_iface_filter,
6551 	.bss_info_changed = iwl_mvm_bss_info_changed,
6552 	.hw_scan = iwl_mvm_mac_hw_scan,
6553 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6554 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6555 	.sta_state = iwl_mvm_mac_sta_state,
6556 	.sta_notify = iwl_mvm_mac_sta_notify,
6557 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6558 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6559 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6560 	.link_sta_rc_update = iwl_mvm_sta_rc_update,
6561 	.conf_tx = iwl_mvm_mac_conf_tx,
6562 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6563 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6564 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6565 	.flush = iwl_mvm_mac_flush,
6566 	.flush_sta = iwl_mvm_mac_flush_sta,
6567 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6568 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6569 	.set_key = iwl_mvm_mac_set_key,
6570 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6571 	.remain_on_channel = iwl_mvm_roc,
6572 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6573 	.add_chanctx = iwl_mvm_add_chanctx,
6574 	.remove_chanctx = iwl_mvm_remove_chanctx,
6575 	.change_chanctx = iwl_mvm_change_chanctx,
6576 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6577 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6578 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6579 
6580 	.start_ap = iwl_mvm_start_ap,
6581 	.stop_ap = iwl_mvm_stop_ap,
6582 	.join_ibss = iwl_mvm_start_ibss,
6583 	.leave_ibss = iwl_mvm_stop_ibss,
6584 
6585 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
6586 
6587 	.set_tim = iwl_mvm_set_tim,
6588 
6589 	.channel_switch = iwl_mvm_channel_switch,
6590 	.pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6591 	.post_channel_switch = iwl_mvm_post_channel_switch,
6592 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6593 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6594 
6595 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6596 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6597 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6598 
6599 	.event_callback = iwl_mvm_mac_event_callback,
6600 
6601 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
6602 
6603 #ifdef CONFIG_PM_SLEEP
6604 	/* look at d3.c */
6605 	.suspend = iwl_mvm_suspend,
6606 	.resume = iwl_mvm_resume,
6607 	.set_wakeup = iwl_mvm_set_wakeup,
6608 	.set_rekey_data = iwl_mvm_set_rekey_data,
6609 #if IS_ENABLED(CONFIG_IPV6)
6610 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6611 #endif
6612 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6613 #endif
6614 	.get_survey = iwl_mvm_mac_get_survey,
6615 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6616 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6617 	.start_pmsr = iwl_mvm_start_pmsr,
6618 	.abort_pmsr = iwl_mvm_abort_pmsr,
6619 
6620 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6621 #ifdef CONFIG_IWLWIFI_DEBUGFS
6622 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
6623 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6624 #endif
6625 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6626 };
6627