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