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