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