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