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