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