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