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