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