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