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