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