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