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