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