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