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