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