iwlwifi: mvm: add tlc command name to output
[linux-block.git] / drivers / net / wireless / intel / iwlwifi / mvm / mac80211.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
2 *
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
5 *
6 * GPL LICENSE SUMMARY
7 *
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
fba8248e
SS
881 /*
882 * No need for threads to be pending here, they can leave the first
883 * taker all the work.
884 *
885 * mvmtxq->tx_request logic:
886 *
887 * If 0, no one is currently TXing, set to 1 to indicate current thread
888 * will now start TX and other threads should quit.
889 *
890 * If 1, another thread is currently TXing, set to 2 to indicate to
891 * that thread that there was another request. Since that request may
892 * have raced with the check whether the queue is empty, the TXing
893 * thread should check the queue's status one more time before leaving.
894 * This check is done in order to not leave any TX hanging in the queue
895 * until the next TX invocation (which may not even happen).
896 *
897 * If 2, another thread is currently TXing, and it will already double
898 * check the queue, so do nothing.
899 */
900 if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
901 return;
cfbc6c4c
SS
902
903 rcu_read_lock();
fba8248e
SS
904 do {
905 while (likely(!mvmtxq->stopped &&
906 (mvm->trans->system_pm_mode ==
907 IWL_PLAT_PM_MODE_DISABLED))) {
908 skb = ieee80211_tx_dequeue(hw, txq);
909
910 if (!skb)
911 break;
912
913 if (!txq->sta)
914 iwl_mvm_tx_skb_non_sta(mvm, skb);
915 else
916 iwl_mvm_tx_skb(mvm, skb, txq->sta);
917 }
918 } while (atomic_dec_return(&mvmtxq->tx_request));
cfbc6c4c 919 rcu_read_unlock();
cfbc6c4c
SS
920}
921
922static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
923 struct ieee80211_txq *txq)
924{
925 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
926 struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
927
928 /*
929 * Please note that racing is handled very carefully here:
930 * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
931 * deleted afterwards.
932 * This means that if:
933 * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
934 * queue is allocated and we can TX.
935 * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
936 * a race, should defer the frame.
937 * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
938 * need to allocate the queue and defer the frame.
939 * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
940 * queue is already scheduled for allocation, no need to allocate,
941 * should defer the frame.
942 */
943
944 /* If the queue is allocated TX and return. */
945 if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
946 /*
947 * Check that list is empty to avoid a race where txq_id is
948 * already updated, but the queue allocation work wasn't
949 * finished
950 */
951 if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
952 return;
953
954 iwl_mvm_mac_itxq_xmit(hw, txq);
955 return;
956 }
957
958 /* The list is being deleted only after the queue is fully allocated. */
959 if (!list_empty(&mvmtxq->list))
960 return;
961
962 list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
963 schedule_work(&mvm->add_stream_wk);
964}
965
7174beb6
JB
966#define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...) \
967 do { \
968 if (!(le16_to_cpu(_tid_bm) & BIT(_tid))) \
969 break; \
970 iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt); \
4203263d
EG
971 } while (0)
972
973static void
974iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
975 struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
976 enum ieee80211_ampdu_mlme_action action)
977{
978 struct iwl_fw_dbg_trigger_tlv *trig;
979 struct iwl_fw_dbg_trigger_ba *ba_trig;
980
6c042d75
SS
981 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
982 FW_DBG_TRIGGER_BA);
983 if (!trig)
4203263d
EG
984 return;
985
4203263d
EG
986 ba_trig = (void *)trig->data;
987
4203263d
EG
988 switch (action) {
989 case IEEE80211_AMPDU_TX_OPERATIONAL: {
990 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
991 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
992
993 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
994 "TX AGG START: MAC %pM tid %d ssn %d\n",
995 sta->addr, tid, tid_data->ssn);
996 break;
997 }
998 case IEEE80211_AMPDU_TX_STOP_CONT:
999 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
1000 "TX AGG STOP: MAC %pM tid %d\n",
1001 sta->addr, tid);
1002 break;
1003 case IEEE80211_AMPDU_RX_START:
1004 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
1005 "RX AGG START: MAC %pM tid %d ssn %d\n",
1006 sta->addr, tid, rx_ba_ssn);
1007 break;
1008 case IEEE80211_AMPDU_RX_STOP:
1009 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
1010 "RX AGG STOP: MAC %pM tid %d\n",
1011 sta->addr, tid);
1012 break;
1013 default:
1014 break;
1015 }
1016}
1017
8ca151b5
JB
1018static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1019 struct ieee80211_vif *vif,
50ea05ef 1020 struct ieee80211_ampdu_params *params)
8ca151b5
JB
1021{
1022 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1023 int ret;
b2492501 1024 bool tx_agg_ref = false;
50ea05ef
SS
1025 struct ieee80211_sta *sta = params->sta;
1026 enum ieee80211_ampdu_mlme_action action = params->action;
1027 u16 tid = params->tid;
1028 u16 *ssn = &params->ssn;
514c3069 1029 u16 buf_size = params->buf_size;
bb81bb68 1030 bool amsdu = params->amsdu;
10b2b201 1031 u16 timeout = params->timeout;
8ca151b5
JB
1032
1033 IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1034 sta->addr, tid, action);
1035
1036 if (!(mvm->nvm_data->sku_cap_11n_enable))
1037 return -EACCES;
1038
b2492501 1039 /* return from D0i3 before starting a new Tx aggregation */
9256c205
EP
1040 switch (action) {
1041 case IEEE80211_AMPDU_TX_START:
1042 case IEEE80211_AMPDU_TX_STOP_CONT:
1043 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1044 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1045 case IEEE80211_AMPDU_TX_OPERATIONAL:
b2492501 1046 /*
9256c205
EP
1047 * for tx start, wait synchronously until D0i3 exit to
1048 * get the correct sequence number for the tid.
1049 * additionally, some other ampdu actions use direct
1050 * target access, which is not handled automatically
1051 * by the trans layer (unlike commands), so wait for
1052 * d0i3 exit in these cases as well.
b2492501 1053 */
d40fc489
GG
1054 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
1055 if (ret)
1056 return ret;
1057
1058 tx_agg_ref = true;
9256c205
EP
1059 break;
1060 default:
1061 break;
b2492501
AN
1062 }
1063
8ca151b5
JB
1064 mutex_lock(&mvm->mutex);
1065
1066 switch (action) {
1067 case IEEE80211_AMPDU_RX_START:
b0ffe455
JB
1068 if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
1069 iwl_mvm_sta_from_mac80211(sta)->sta_id) {
1070 struct iwl_mvm_vif *mvmvif;
1071 u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1072 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1073
1074 mdata->opened_rx_ba_sessions = true;
1075 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1076 cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1077 }
e78da25e 1078 if (!iwl_enable_rx_ampdu()) {
8ca151b5
JB
1079 ret = -EINVAL;
1080 break;
1081 }
10b2b201
SS
1082 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
1083 timeout);
8ca151b5
JB
1084 break;
1085 case IEEE80211_AMPDU_RX_STOP:
10b2b201
SS
1086 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
1087 timeout);
8ca151b5
JB
1088 break;
1089 case IEEE80211_AMPDU_TX_START:
e78da25e 1090 if (!iwl_enable_tx_ampdu()) {
5d158efa
EG
1091 ret = -EINVAL;
1092 break;
1093 }
8ca151b5
JB
1094 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1095 break;
1096 case IEEE80211_AMPDU_TX_STOP_CONT:
e3d9e7ce
EG
1097 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1098 break;
8ca151b5
JB
1099 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1100 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
e3d9e7ce 1101 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
8ca151b5
JB
1102 break;
1103 case IEEE80211_AMPDU_TX_OPERATIONAL:
bb81bb68
EG
1104 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1105 buf_size, amsdu);
8ca151b5
JB
1106 break;
1107 default:
1108 WARN_ON_ONCE(1);
1109 ret = -EINVAL;
1110 break;
1111 }
4203263d
EG
1112
1113 if (!ret) {
1114 u16 rx_ba_ssn = 0;
1115
1116 if (action == IEEE80211_AMPDU_RX_START)
1117 rx_ba_ssn = *ssn;
1118
1119 iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1120 rx_ba_ssn, action);
1121 }
8ca151b5
JB
1122 mutex_unlock(&mvm->mutex);
1123
b2492501
AN
1124 /*
1125 * If the tid is marked as started, we won't use it for offloaded
1126 * traffic on the next D0i3 entry. It's safe to unref.
1127 */
1128 if (tx_agg_ref)
1129 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
1130
8ca151b5
JB
1131 return ret;
1132}
1133
1134static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1135 struct ieee80211_vif *vif)
1136{
1137 struct iwl_mvm *mvm = data;
1138 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1139
1140 mvmvif->uploaded = false;
0ae98812 1141 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
8ca151b5 1142
8ca151b5
JB
1143 spin_lock_bh(&mvm->time_event_lock);
1144 iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1145 spin_unlock_bh(&mvm->time_event_lock);
1146
fe0f2de3 1147 mvmvif->phy_ctxt = NULL;
8a275bad 1148 memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
86e177d8 1149 memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
8ca151b5
JB
1150}
1151
1152static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1153{
58629d9d
JB
1154 /* clear the D3 reconfig, we only need it to avoid dumping a
1155 * firmware coredump on reconfiguration, we shouldn't do that
1156 * on D3->D0 transition
1157 */
b6eaa45a 1158 if (!test_and_clear_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status)) {
7174beb6
JB
1159 mvm->fwrt.dump.desc = &iwl_dump_desc_assert;
1160 iwl_fw_error_dump(&mvm->fwrt);
b6eaa45a 1161 }
1bd3cbc1 1162
744cb695
EP
1163 /* cleanup all stale references (scan, roc), but keep the
1164 * ucode_down ref until reconfig is complete
1165 */
1166 iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
1167
fcb6b92a 1168 iwl_mvm_stop_device(mvm);
8ca151b5 1169
9bf13bee
JB
1170 mvm->cur_aid = 0;
1171
9af91f46 1172 mvm->scan_status = 0;
b1873300 1173 mvm->ps_disabled = false;
31b8b343 1174 mvm->calibrating = false;
8ca151b5
JB
1175
1176 /* just in case one was running */
305d236e 1177 iwl_mvm_cleanup_roc_te(mvm);
8ca151b5
JB
1178 ieee80211_remain_on_channel_expired(mvm->hw);
1179
737719fe
AN
1180 /*
1181 * cleanup all interfaces, even inactive ones, as some might have
1182 * gone down during the HW restart
1183 */
1184 ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
8ca151b5 1185
fe0f2de3 1186 mvm->p2p_device_vif = NULL;
0ae98812 1187 mvm->d0i3_ap_sta_id = IWL_MVM_INVALID_STA;
fe0f2de3
IP
1188
1189 iwl_mvm_reset_phy_ctxts(mvm);
9c3deeb5 1190 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
8a275bad 1191 memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
8a275bad 1192 memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
8ca151b5
JB
1193
1194 ieee80211_wake_queues(mvm->hw);
1195
228670b2
EP
1196 /* clear any stale d0i3 state */
1197 clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
1198
8ca151b5 1199 mvm->vif_count = 0;
113a0447 1200 mvm->rx_ba_sessions = 0;
7174beb6 1201 mvm->fwrt.dump.conf = FW_DBG_INVALID;
baf41bc3 1202 mvm->monitor_on = false;
91a8bcde
JB
1203
1204 /* keep statistics ticking */
1205 iwl_mvm_accu_radio_stats(mvm);
8ca151b5
JB
1206}
1207
a0a09243 1208int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
8ca151b5 1209{
8ca151b5
JB
1210 int ret;
1211
a0a09243 1212 lockdep_assert_held(&mvm->mutex);
8ca151b5 1213
bf8b286f
JB
1214 if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1215 /*
1216 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1217 * so later code will - from now on - see that we're doing it.
1218 */
1219 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1220 clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
a42b2af3 1221 /* Clean up some internal and mac80211 state on restart */
8ca151b5 1222 iwl_mvm_restart_cleanup(mvm);
a42b2af3
LC
1223 } else {
1224 /* Hold the reference to prevent runtime suspend while
1225 * the start procedure runs. It's a bit confusing
1226 * that the UCODE_DOWN reference is taken, but it just
1227 * means "UCODE is not UP yet". ( TODO: rename this
1228 * reference).
1229 */
1230 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1231 }
8ca151b5 1232 ret = iwl_mvm_up(mvm);
c47af22a 1233
da2eb669
SS
1234 iwl_fw_dbg_apply_point(&mvm->fwrt, IWL_FW_INI_APPLY_POST_INIT);
1235
c47af22a
JB
1236 if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1237 /* Something went wrong - we need to finish some cleanup
1238 * that normally iwl_mvm_mac_restart_complete() below
1239 * would do.
1240 */
1241 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
a75b9b33 1242#ifdef CONFIG_PM
c47af22a 1243 iwl_mvm_d0i3_enable_tx(mvm, NULL);
a75b9b33 1244#endif
c47af22a
JB
1245 }
1246
a0a09243
LC
1247 return ret;
1248}
1249
1250static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1251{
1252 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1253 int ret;
1254
37948fcf
EP
1255 /* Some hw restart cleanups must not hold the mutex */
1256 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1257 /*
1258 * Make sure we are out of d0i3. This is needed
1259 * to make sure the reference accounting is correct
1260 * (and there is no stale d0i3_exit_work).
1261 */
1262 wait_event_timeout(mvm->d0i3_exit_waitq,
1263 !test_bit(IWL_MVM_STATUS_IN_D0I3,
1264 &mvm->status),
1265 HZ);
1266 }
1267
a0a09243
LC
1268 mutex_lock(&mvm->mutex);
1269 ret = __iwl_mvm_mac_start(mvm);
8ca151b5
JB
1270 mutex_unlock(&mvm->mutex);
1271
1272 return ret;
1273}
1274
cf2c92d8 1275static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
8ca151b5 1276{
8ca151b5
JB
1277 int ret;
1278
1279 mutex_lock(&mvm->mutex);
1280
1281 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
a75b9b33 1282#ifdef CONFIG_PM
b2492501 1283 iwl_mvm_d0i3_enable_tx(mvm, NULL);
a75b9b33 1284#endif
e7afe89f 1285 ret = iwl_mvm_update_quotas(mvm, true, NULL);
8ca151b5
JB
1286 if (ret)
1287 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1288 ret);
1289
7498cf4c
EP
1290 /* allow transport/FW low power modes */
1291 iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
1292
cbd2ae2d
AN
1293 /*
1294 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1295 * of packets the FW sent out, so we must reconnect.
1296 */
1297 iwl_mvm_teardown_tdls_peers(mvm);
1298
8ca151b5
JB
1299 mutex_unlock(&mvm->mutex);
1300}
1301
088070a2
EP
1302static void iwl_mvm_resume_complete(struct iwl_mvm *mvm)
1303{
b7282643
LC
1304 if (iwl_mvm_is_d0i3_supported(mvm) &&
1305 iwl_mvm_enter_d0i3_on_suspend(mvm))
1306 WARN_ONCE(!wait_event_timeout(mvm->d0i3_exit_waitq,
1307 !test_bit(IWL_MVM_STATUS_IN_D0I3,
1308 &mvm->status),
1309 HZ),
1310 "D0i3 exit on resume timed out\n");
088070a2
EP
1311}
1312
cf2c92d8
EP
1313static void
1314iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1315 enum ieee80211_reconfig_type reconfig_type)
1316{
1317 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1318
1319 switch (reconfig_type) {
1320 case IEEE80211_RECONFIG_TYPE_RESTART:
1321 iwl_mvm_restart_complete(mvm);
1322 break;
1323 case IEEE80211_RECONFIG_TYPE_SUSPEND:
088070a2 1324 iwl_mvm_resume_complete(mvm);
cf2c92d8
EP
1325 break;
1326 }
1327}
1328
a0a09243 1329void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
8ca151b5 1330{
a0a09243 1331 lockdep_assert_held(&mvm->mutex);
7498cf4c 1332
91a8bcde
JB
1333 /* firmware counters are obviously reset now, but we shouldn't
1334 * partially track so also clear the fw_reset_accu counters.
1335 */
1336 memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1337
8ca151b5
JB
1338 /* async_handlers_wk is now blocked */
1339
1340 /*
1341 * The work item could be running or queued if the
1342 * ROC time event stops just as we get here.
1343 */
c779273b 1344 flush_work(&mvm->roc_done_wk);
8ca151b5 1345
fcb6b92a 1346 iwl_mvm_stop_device(mvm);
8ca151b5
JB
1347
1348 iwl_mvm_async_handlers_purge(mvm);
1349 /* async_handlers_list is empty and will stay empty: HW is stopped */
1350
1351 /* the fw is stopped, the aux sta is dead: clean up driver state */
712b24ad 1352 iwl_mvm_del_aux_sta(mvm);
8ca151b5 1353
0a79a0c0 1354 /*
155f7e04
EG
1355 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1356 * hw (as restart_complete() won't be called in this case) and mac80211
1357 * won't execute the restart.
8b2b9fbf
AN
1358 * But make sure to cleanup interfaces that have gone down before/during
1359 * HW restart was requested.
0a79a0c0 1360 */
155f7e04
EG
1361 if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1362 test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1363 &mvm->status))
8b2b9fbf
AN
1364 ieee80211_iterate_interfaces(mvm->hw, 0,
1365 iwl_mvm_cleanup_iterator, mvm);
0a79a0c0 1366
963221be
AB
1367 /* We shouldn't have any UIDs still set. Loop over all the UIDs to
1368 * make sure there's nothing left there and warn if any is found.
1369 */
859d914c 1370 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
963221be
AB
1371 int i;
1372
507e4cda 1373 for (i = 0; i < mvm->max_scans; i++) {
6185af2a
LC
1374 if (WARN_ONCE(mvm->scan_uid_status[i],
1375 "UMAC scan UID %d status was not cleaned\n",
1376 i))
1377 mvm->scan_uid_status[i] = 0;
963221be
AB
1378 }
1379 }
a0a09243 1380}
bc44886d 1381
a0a09243
LC
1382static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1383{
1384 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1385
1386 flush_work(&mvm->d0i3_exit_work);
1387 flush_work(&mvm->async_handlers_wk);
24afba76 1388 flush_work(&mvm->add_stream_wk);
771147b0
JB
1389
1390 /*
1391 * Lock and clear the firmware running bit here already, so that
1392 * new commands coming in elsewhere, e.g. from debugfs, will not
1393 * be able to proceed. This is important here because one of those
7174beb6 1394 * debugfs files causes the firmware dump to be triggered, and if we
771147b0
JB
1395 * don't stop debugfs accesses before canceling that it could be
1396 * retriggered after we flush it but before we've cleared the bit.
1397 */
1398 clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1399
7174beb6 1400 iwl_fw_cancel_dump(&mvm->fwrt);
d3a108a4 1401 cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
69e04642 1402 cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
7174beb6 1403 iwl_fw_free_dump_desc(&mvm->fwrt);
a0a09243
LC
1404
1405 mutex_lock(&mvm->mutex);
1406 __iwl_mvm_mac_stop(mvm);
8ca151b5
JB
1407 mutex_unlock(&mvm->mutex);
1408
1409 /*
1410 * The worker might have been waiting for the mutex, let it run and
1411 * discover that its list is now empty.
1412 */
1413 cancel_work_sync(&mvm->async_handlers_wk);
1414}
1415
fe0f2de3
IP
1416static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1417{
1418 u16 i;
1419
1420 lockdep_assert_held(&mvm->mutex);
1421
1422 for (i = 0; i < NUM_PHY_CTX; i++)
1423 if (!mvm->phy_ctxts[i].ref)
1424 return &mvm->phy_ctxts[i];
1425
1426 IWL_ERR(mvm, "No available PHY context\n");
1427 return NULL;
1428}
1429
d44c3fe6
AA
1430static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1431 s16 tx_power)
1432{
0791c2fc
HD
1433 int len;
1434 union {
1435 struct iwl_dev_tx_power_cmd v5;
1436 struct iwl_dev_tx_power_cmd_v4 v4;
1437 } cmd = {
1438 .v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1439 .v5.v3.mac_context_id =
d44c3fe6 1440 cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
0791c2fc 1441 .v5.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
d44c3fe6
AA
1442 };
1443
d44c3fe6 1444 if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
0791c2fc 1445 cmd.v5.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
d44c3fe6 1446
0791c2fc
HD
1447 if (fw_has_api(&mvm->fw->ucode_capa,
1448 IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1449 len = sizeof(cmd.v5);
1450 else if (fw_has_capa(&mvm->fw->ucode_capa,
1451 IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1452 len = sizeof(cmd.v4);
1453 else
1454 len = sizeof(cmd.v4.v3);
da03f029
JB
1455
1456 return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
d44c3fe6
AA
1457}
1458
8ca151b5
JB
1459static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1460 struct ieee80211_vif *vif)
1461{
1462 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1463 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1464 int ret;
1465
aa5e1832 1466 mvmvif->mvm = mvm;
86e177d8 1467 RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
aa5e1832 1468
d40fc489
GG
1469 /*
1470 * make sure D0i3 exit is completed, otherwise a target access
1471 * during tx queue configuration could be done when still in
1472 * D0i3 state.
1473 */
1474 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
1475 if (ret)
1476 return ret;
1477
8ca151b5
JB
1478 /*
1479 * Not much to do here. The stack will not allow interface
1480 * types or combinations that we didn't advertise, so we
1481 * don't really have to check the types.
1482 */
1483
1484 mutex_lock(&mvm->mutex);
1485
33cef925
JB
1486 /* make sure that beacon statistics don't go backwards with FW reset */
1487 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1488 mvmvif->beacon_stats.accu_num_beacons +=
1489 mvmvif->beacon_stats.num_beacons;
1490
e89044d7 1491 /* Allocate resources for the MAC context, and add it to the fw */
8ca151b5
JB
1492 ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1493 if (ret)
1494 goto out_unlock;
1495
698478c4
SS
1496 rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1497
1c2abf72 1498 /* Counting number of interfaces is needed for legacy PM */
ea183d02
IP
1499 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1500 mvm->vif_count++;
ea183d02 1501
8ca151b5
JB
1502 /*
1503 * The AP binding flow can be done only after the beacon
1504 * template is configured (which happens only in the mac80211
1505 * start_ap() flow), and adding the broadcast station can happen
1506 * only after the binding.
1507 * In addition, since modifying the MAC before adding a bcast
1508 * station is not allowed by the FW, delay the adding of MAC context to
1509 * the point where we can also add the bcast station.
1510 * In short: there's not much we can do at this point, other than
1511 * allocating resources :)
1512 */
5023d966
JB
1513 if (vif->type == NL80211_IFTYPE_AP ||
1514 vif->type == NL80211_IFTYPE_ADHOC) {
013290aa 1515 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
8ca151b5
JB
1516 if (ret) {
1517 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1518 goto out_release;
1519 }
1520
c8f54701
JB
1521 /*
1522 * Only queue for this station is the mcast queue,
1523 * which shouldn't be in TFD mask anyway
1524 */
1525 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1526 0, vif->type,
1527 IWL_STA_MULTICAST);
1528 if (ret)
1529 goto out_release;
26d6c16b 1530
77740cb4 1531 iwl_mvm_vif_dbgfs_register(mvm, vif);
8ca151b5
JB
1532 goto out_unlock;
1533 }
1534
93190fb0
AA
1535 mvmvif->features |= hw->netdev_features;
1536
8ca151b5
JB
1537 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1538 if (ret)
1539 goto out_release;
1540
999609f1 1541 ret = iwl_mvm_power_update_mac(mvm);
e5e7aa8e 1542 if (ret)
fd66fc1c 1543 goto out_remove_mac;
8ca151b5 1544
7df15b1e 1545 /* beacon filtering */
a1022927 1546 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
bd3351ba
EP
1547 if (ret)
1548 goto out_remove_mac;
1549
7df15b1e 1550 if (!mvm->bf_allowed_vif &&
73e5f2c5 1551 vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
7df15b1e 1552 mvm->bf_allowed_vif = mvmvif;
a20fd398
AO
1553 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1554 IEEE80211_VIF_SUPPORTS_CQM_RSSI;
7df15b1e
HG
1555 }
1556
8ca151b5
JB
1557 /*
1558 * P2P_DEVICE interface does not have a channel context assigned to it,
1559 * so a dedicated PHY context is allocated to it and the corresponding
1560 * MAC context is bound to it at this stage.
1561 */
1562 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
8ca151b5 1563
fe0f2de3
IP
1564 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1565 if (!mvmvif->phy_ctxt) {
1566 ret = -ENOSPC;
bd3351ba 1567 goto out_free_bf;
fe0f2de3 1568 }
8ca151b5 1569
53a9d61e 1570 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
1571 ret = iwl_mvm_binding_add_vif(mvm, vif);
1572 if (ret)
53a9d61e 1573 goto out_unref_phy;
8ca151b5 1574
d197358b 1575 ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
8ca151b5
JB
1576 if (ret)
1577 goto out_unbind;
1578
1579 /* Save a pointer to p2p device vif, so it can later be used to
1580 * update the p2p device MAC when a GO is started/stopped */
1581 mvm->p2p_device_vif = vif;
1582 }
1583
b0ffe455
JB
1584 iwl_mvm_tcm_add_vif(mvm, vif);
1585
baf41bc3
ST
1586 if (vif->type == NL80211_IFTYPE_MONITOR)
1587 mvm->monitor_on = true;
1588
63494374 1589 iwl_mvm_vif_dbgfs_register(mvm, vif);
8ca151b5
JB
1590 goto out_unlock;
1591
1592 out_unbind:
1593 iwl_mvm_binding_remove_vif(mvm, vif);
53a9d61e 1594 out_unref_phy:
fe0f2de3 1595 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
bd3351ba
EP
1596 out_free_bf:
1597 if (mvm->bf_allowed_vif == mvmvif) {
1598 mvm->bf_allowed_vif = NULL;
a20fd398
AO
1599 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1600 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
bd3351ba 1601 }
8ca151b5
JB
1602 out_remove_mac:
1603 mvmvif->phy_ctxt = NULL;
1604 iwl_mvm_mac_ctxt_remove(mvm, vif);
1605 out_release:
5ee2b215
AB
1606 if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1607 mvm->vif_count--;
8ca151b5
JB
1608 out_unlock:
1609 mutex_unlock(&mvm->mutex);
1610
d40fc489
GG
1611 iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1612
8ca151b5
JB
1613 return ret;
1614}
1615
38a12b5b
JB
1616static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1617 struct ieee80211_vif *vif)
8ca151b5 1618{
8ca151b5
JB
1619 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1620 /*
1621 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1622 * We assume here that all the packets sent to the OFFCHANNEL
1623 * queue are sent in ROC session.
1624 */
1625 flush_work(&mvm->roc_done_wk);
8ca151b5 1626 }
38a12b5b
JB
1627}
1628
1629static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1630 struct ieee80211_vif *vif)
1631{
1632 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1633 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
86e177d8 1634 struct iwl_probe_resp_data *probe_data;
38a12b5b
JB
1635
1636 iwl_mvm_prepare_mac_removal(mvm, vif);
8ca151b5 1637
b0ffe455
JB
1638 if (!(vif->type == NL80211_IFTYPE_AP ||
1639 vif->type == NL80211_IFTYPE_ADHOC))
1640 iwl_mvm_tcm_rm_vif(mvm, vif);
1641
8ca151b5
JB
1642 mutex_lock(&mvm->mutex);
1643
86e177d8
GG
1644 probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
1645 lockdep_is_held(&mvm->mutex));
1646 RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1647 if (probe_data)
1648 kfree_rcu(probe_data, rcu_head);
1649
7df15b1e
HG
1650 if (mvm->bf_allowed_vif == mvmvif) {
1651 mvm->bf_allowed_vif = NULL;
a20fd398
AO
1652 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1653 IEEE80211_VIF_SUPPORTS_CQM_RSSI);
7df15b1e
HG
1654 }
1655
63494374
JB
1656 iwl_mvm_vif_dbgfs_clean(mvm, vif);
1657
8ca151b5
JB
1658 /*
1659 * For AP/GO interface, the tear down of the resources allocated to the
38a12b5b 1660 * interface is be handled as part of the stop_ap flow.
8ca151b5 1661 */
5023d966
JB
1662 if (vif->type == NL80211_IFTYPE_AP ||
1663 vif->type == NL80211_IFTYPE_ADHOC) {
507cadf2
DS
1664#ifdef CONFIG_NL80211_TESTMODE
1665 if (vif == mvm->noa_vif) {
1666 mvm->noa_vif = NULL;
1667 mvm->noa_duration = 0;
1668 }
1669#endif
26d6c16b 1670 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
013290aa 1671 iwl_mvm_dealloc_bcast_sta(mvm, vif);
8ca151b5
JB
1672 goto out_release;
1673 }
1674
1675 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1676 mvm->p2p_device_vif = NULL;
d197358b 1677 iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
8ca151b5 1678 iwl_mvm_binding_remove_vif(mvm, vif);
fe0f2de3 1679 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
8ca151b5
JB
1680 mvmvif->phy_ctxt = NULL;
1681 }
1682
5ee2b215 1683 if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
8ca151b5 1684 mvm->vif_count--;
1c2abf72 1685
999609f1 1686 iwl_mvm_power_update_mac(mvm);
8ca151b5
JB
1687 iwl_mvm_mac_ctxt_remove(mvm, vif);
1688
698478c4
SS
1689 RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1690
baf41bc3
ST
1691 if (vif->type == NL80211_IFTYPE_MONITOR)
1692 mvm->monitor_on = false;
1693
8ca151b5 1694out_release:
8ca151b5
JB
1695 mutex_unlock(&mvm->mutex);
1696}
1697
1698static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
88f2fd73 1699{
8ca151b5
JB
1700 return 0;
1701}
1702
e59647ea
EP
1703struct iwl_mvm_mc_iter_data {
1704 struct iwl_mvm *mvm;
1705 int port_id;
1706};
1707
1708static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1709 struct ieee80211_vif *vif)
1710{
1711 struct iwl_mvm_mc_iter_data *data = _data;
1712 struct iwl_mvm *mvm = data->mvm;
1713 struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
97bce57b
LC
1714 struct iwl_host_cmd hcmd = {
1715 .id = MCAST_FILTER_CMD,
1716 .flags = CMD_ASYNC,
1717 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1718 };
e59647ea
EP
1719 int ret, len;
1720
1721 /* if we don't have free ports, mcast frames will be dropped */
1722 if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1723 return;
1724
1725 if (vif->type != NL80211_IFTYPE_STATION ||
1726 !vif->bss_conf.assoc)
1727 return;
1728
1729 cmd->port_id = data->port_id++;
1730 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1731 len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1732
97bce57b
LC
1733 hcmd.len[0] = len;
1734 hcmd.data[0] = cmd;
1735
1736 ret = iwl_mvm_send_cmd(mvm, &hcmd);
e59647ea
EP
1737 if (ret)
1738 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1739}
1740
1741static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1742{
1743 struct iwl_mvm_mc_iter_data iter_data = {
1744 .mvm = mvm,
88f2fd73
MG
1745 };
1746
e59647ea
EP
1747 lockdep_assert_held(&mvm->mutex);
1748
1749 if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1750 return;
1751
1c4abec0 1752 ieee80211_iterate_active_interfaces_atomic(
e59647ea
EP
1753 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1754 iwl_mvm_mc_iface_iterator, &iter_data);
88f2fd73
MG
1755}
1756
e59647ea
EP
1757static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1758 struct netdev_hw_addr_list *mc_list)
8ca151b5 1759{
e59647ea
EP
1760 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1761 struct iwl_mcast_filter_cmd *cmd;
1762 struct netdev_hw_addr *addr;
f3bd58f4
MS
1763 int addr_count;
1764 bool pass_all;
e59647ea
EP
1765 int len;
1766
f3bd58f4
MS
1767 addr_count = netdev_hw_addr_list_count(mc_list);
1768 pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1769 IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1770 if (pass_all)
e59647ea 1771 addr_count = 0;
e59647ea
EP
1772
1773 len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1774 cmd = kzalloc(len, GFP_ATOMIC);
1775 if (!cmd)
1776 return 0;
1777
1778 if (pass_all) {
1779 cmd->pass_all = 1;
1780 return (u64)(unsigned long)cmd;
1781 }
1782
1783 netdev_hw_addr_list_for_each(addr, mc_list) {
1784 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1785 cmd->count, addr->addr);
1786 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1787 addr->addr, ETH_ALEN);
1788 cmd->count++;
1789 }
1790
1791 return (u64)(unsigned long)cmd;
8ca151b5
JB
1792}
1793
1794static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1795 unsigned int changed_flags,
1796 unsigned int *total_flags,
1797 u64 multicast)
1798{
e59647ea
EP
1799 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1800 struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
8ca151b5 1801
e59647ea 1802 mutex_lock(&mvm->mutex);
51b6b9e0 1803
e59647ea
EP
1804 /* replace previous configuration */
1805 kfree(mvm->mcast_filter_cmd);
1806 mvm->mcast_filter_cmd = cmd;
51b6b9e0 1807
e59647ea
EP
1808 if (!cmd)
1809 goto out;
51b6b9e0 1810
61e7d91b
LC
1811 if (changed_flags & FIF_ALLMULTI)
1812 cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
1813
1814 if (cmd->pass_all)
1815 cmd->count = 0;
1816
e59647ea
EP
1817 iwl_mvm_recalc_multicast(mvm);
1818out:
1819 mutex_unlock(&mvm->mutex);
1820 *total_flags = 0;
51b6b9e0
EG
1821}
1822
effd1929
AO
1823static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1824 struct ieee80211_vif *vif,
1825 unsigned int filter_flags,
1826 unsigned int changed_flags)
1827{
1828 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1829
1830 /* We support only filter for probe requests */
1831 if (!(changed_flags & FIF_PROBE_REQ))
1832 return;
1833
1834 /* Supported only for p2p client interfaces */
1835 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1836 !vif->p2p)
1837 return;
1838
1839 mutex_lock(&mvm->mutex);
1840 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1841 mutex_unlock(&mvm->mutex);
1842}
1843
c87163b9
EP
1844#ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1845struct iwl_bcast_iter_data {
1846 struct iwl_mvm *mvm;
1847 struct iwl_bcast_filter_cmd *cmd;
1848 u8 current_filter;
1849};
1850
1851static void
1852iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1853 const struct iwl_fw_bcast_filter *in_filter,
1854 struct iwl_fw_bcast_filter *out_filter)
1855{
1856 struct iwl_fw_bcast_filter_attr *attr;
1857 int i;
1858
1859 memcpy(out_filter, in_filter, sizeof(*out_filter));
1860
1861 for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1862 attr = &out_filter->attrs[i];
1863
1864 if (!attr->mask)
1865 break;
1866
2ee8f021
EP
1867 switch (attr->reserved1) {
1868 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1869 if (vif->bss_conf.arp_addr_cnt != 1) {
1870 attr->mask = 0;
1871 continue;
1872 }
1873
1874 attr->val = vif->bss_conf.arp_addr_list[0];
1875 break;
1876 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1877 attr->val = *(__be32 *)&vif->addr[2];
1878 break;
1879 default:
1880 break;
1881 }
1882 attr->reserved1 = 0;
c87163b9
EP
1883 out_filter->num_attrs++;
1884 }
1885}
1886
1887static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1888 struct ieee80211_vif *vif)
1889{
1890 struct iwl_bcast_iter_data *data = _data;
1891 struct iwl_mvm *mvm = data->mvm;
1892 struct iwl_bcast_filter_cmd *cmd = data->cmd;
1893 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1894 struct iwl_fw_bcast_mac *bcast_mac;
1895 int i;
1896
1897 if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1898 return;
1899
1900 bcast_mac = &cmd->macs[mvmvif->id];
1901
e48393e8
IP
1902 /*
1903 * enable filtering only for associated stations, but not for P2P
1904 * Clients
1905 */
1906 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1907 !vif->bss_conf.assoc)
c87163b9
EP
1908 return;
1909
1910 bcast_mac->default_discard = 1;
1911
1912 /* copy all configured filters */
1913 for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1914 /*
1915 * Make sure we don't exceed our filters limit.
1916 * if there is still a valid filter to be configured,
1917 * be on the safe side and just allow bcast for this mac.
1918 */
1919 if (WARN_ON_ONCE(data->current_filter >=
1920 ARRAY_SIZE(cmd->filters))) {
1921 bcast_mac->default_discard = 0;
1922 bcast_mac->attached_filters = 0;
1923 break;
1924 }
1925
1926 iwl_mvm_set_bcast_filter(vif,
1927 &mvm->bcast_filters[i],
1928 &cmd->filters[data->current_filter]);
1929
1930 /* skip current filter if it contains no attributes */
1931 if (!cmd->filters[data->current_filter].num_attrs)
1932 continue;
1933
1934 /* attach the filter to current mac */
1935 bcast_mac->attached_filters |=
1936 cpu_to_le16(BIT(data->current_filter));
1937
1938 data->current_filter++;
1939 }
1940}
1941
de06a59e
EP
1942bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1943 struct iwl_bcast_filter_cmd *cmd)
c87163b9 1944{
c87163b9
EP
1945 struct iwl_bcast_iter_data iter_data = {
1946 .mvm = mvm,
de06a59e 1947 .cmd = cmd,
c87163b9
EP
1948 };
1949
3b8983b1
MS
1950 if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1951 return false;
1952
de06a59e
EP
1953 memset(cmd, 0, sizeof(*cmd));
1954 cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1955 cmd->max_macs = ARRAY_SIZE(cmd->macs);
1956
1957#ifdef CONFIG_IWLWIFI_DEBUGFS
1958 /* use debugfs filters/macs if override is configured */
1959 if (mvm->dbgfs_bcast_filtering.override) {
1960 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1961 sizeof(cmd->filters));
1962 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1963 sizeof(cmd->macs));
1964 return true;
1965 }
1966#endif
c87163b9
EP
1967
1968 /* if no filters are configured, do nothing */
1969 if (!mvm->bcast_filters)
de06a59e 1970 return false;
c87163b9
EP
1971
1972 /* configure and attach these filters for each associated sta vif */
1973 ieee80211_iterate_active_interfaces(
1974 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1975 iwl_mvm_bcast_filter_iterator, &iter_data);
1976
de06a59e
EP
1977 return true;
1978}
34672bb3
EP
1979
1980static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
de06a59e
EP
1981{
1982 struct iwl_bcast_filter_cmd cmd;
1983
1984 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1985 return 0;
1986
1987 if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1988 return 0;
1989
a1022927 1990 return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
c87163b9
EP
1991 sizeof(cmd), &cmd);
1992}
1993#else
34672bb3 1994static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
c87163b9
EP
1995{
1996 return 0;
1997}
1998#endif
1999
a07a8f37
SS
2000static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
2001 struct ieee80211_vif *vif)
2002{
2003 struct iwl_mu_group_mgmt_cmd cmd = {};
2004
2005 memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2006 WLAN_MEMBERSHIP_LEN);
2007 memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2008 WLAN_USER_POSITION_LEN);
2009
2010 return iwl_mvm_send_cmd_pdu(mvm,
2011 WIDE_ID(DATA_PATH_GROUP,
2012 UPDATE_MU_GROUPS_CMD),
2013 0, sizeof(cmd), &cmd);
2014}
2015
f92659a1
SS
2016static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2017 struct ieee80211_vif *vif)
2018{
2019 if (vif->mu_mimo_owner) {
2020 struct iwl_mu_group_mgmt_notif *notif = _data;
2021
2022 /*
2023 * MU-MIMO Group Id action frame is little endian. We treat
2024 * the data received from firmware as if it came from the
2025 * action frame, so no conversion is needed.
2026 */
2027 ieee80211_update_mu_groups(vif,
2028 (u8 *)&notif->membership_status,
2029 (u8 *)&notif->user_position);
2030 }
2031}
2032
2033void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2034 struct iwl_rx_cmd_buffer *rxb)
2035{
2036 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2037 struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2038
2039 ieee80211_iterate_active_interfaces_atomic(
2040 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2041 iwl_mvm_mu_mimo_iface_iterator, notif);
2042}
2043
514c3069
LC
2044static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2045{
2046 u8 byte_num = ppe_pos_bit / 8;
2047 u8 bit_num = ppe_pos_bit % 8;
2048 u8 residue_bits;
2049 u8 res;
2050
2051 if (bit_num <= 5)
2052 return (ppe[byte_num] >> bit_num) &
2053 (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2054
2055 /*
2056 * If bit_num > 5, we have to combine bits with next byte.
2057 * Calculate how many bits we need to take from current byte (called
2058 * here "residue_bits"), and add them to bits from next byte.
2059 */
2060
2061 residue_bits = 8 - bit_num;
2062
2063 res = (ppe[byte_num + 1] &
2064 (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2065 residue_bits;
2066 res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2067
2068 return res;
2069}
2070
2071static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2072 struct ieee80211_vif *vif, u8 sta_id)
2073{
2074 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2075 struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2076 .sta_id = sta_id,
2077 .tid_limit = IWL_MAX_TID_COUNT,
2078 .bss_color = vif->bss_conf.bss_color,
2079 .htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2080 .frame_time_rts_th =
2081 cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2082 };
2083 struct ieee80211_sta *sta;
2084 u32 flags;
2085 int i;
2086
2087 rcu_read_lock();
2088
2089 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2090 if (IS_ERR(sta)) {
2091 rcu_read_unlock();
2092 WARN(1, "Can't find STA to configure HE\n");
2093 return;
2094 }
2095
2096 if (!sta->he_cap.has_he) {
2097 rcu_read_unlock();
2098 return;
2099 }
2100
2101 flags = 0;
2102
2103 /* HTC flags */
2104 if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2105 IEEE80211_HE_MAC_CAP0_HTC_HE)
2106 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2107 if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2108 IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2109 (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2110 IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2111 u8 link_adap =
2112 ((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2113 IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2114 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2115 IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2116
2117 if (link_adap == 2)
2118 sta_ctxt_cmd.htc_flags |=
2119 cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2120 else if (link_adap == 3)
2121 sta_ctxt_cmd.htc_flags |=
2122 cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2123 }
514c3069
LC
2124 if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2125 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2126 if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2127 IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2128 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2129 if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2130 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2131
189b8d44
NG
2132 /*
2133 * Initialize the PPE thresholds to "None" (7), as described in Table
2134 * 9-262ac of 80211.ax/D3.0.
2135 */
2136 memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2137
2138 /* If PPE Thresholds exist, parse them into a FW-familiar format. */
514c3069
LC
2139 if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2140 IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2141 u8 nss = (sta->he_cap.ppe_thres[0] &
2142 IEEE80211_PPE_THRES_NSS_MASK) + 1;
2143 u8 ru_index_bitmap =
2144 (sta->he_cap.ppe_thres[0] &
2145 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2146 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2147 u8 *ppe = &sta->he_cap.ppe_thres[0];
2148 u8 ppe_pos_bit = 7; /* Starting after PPE header */
2149
2150 /*
2151 * FW currently supports only nss == MAX_HE_SUPP_NSS
2152 *
2153 * If nss > MAX: we can ignore values we don't support
2154 * If nss < MAX: we can set zeros in other streams
2155 */
2156 if (nss > MAX_HE_SUPP_NSS) {
2157 IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2158 MAX_HE_SUPP_NSS);
2159 nss = MAX_HE_SUPP_NSS;
2160 }
2161
2162 for (i = 0; i < nss; i++) {
2163 u8 ru_index_tmp = ru_index_bitmap << 1;
2164 u8 bw;
2165
2166 for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2167 ru_index_tmp >>= 1;
2168 if (!(ru_index_tmp & 1))
2169 continue;
2170
2171 sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2172 iwl_mvm_he_get_ppe_val(ppe,
2173 ppe_pos_bit);
2174 ppe_pos_bit +=
2175 IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2176 sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2177 iwl_mvm_he_get_ppe_val(ppe,
2178 ppe_pos_bit);
2179 ppe_pos_bit +=
2180 IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2181 }
2182 }
2183
2184 flags |= STA_CTXT_HE_PACKET_EXT;
2185 }
2186 rcu_read_unlock();
2187
2188 /* Mark MU EDCA as enabled, unless none detected on some AC */
2189 flags |= STA_CTXT_HE_MU_EDCA_CW;
2190 for (i = 0; i < AC_NUM; i++) {
2191 struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2192 &mvmvif->queue_params[i].mu_edca_param_rec;
2193
2194 if (!mvmvif->queue_params[i].mu_edca) {
2195 flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2196 break;
2197 }
2198
2199 sta_ctxt_cmd.trig_based_txf[i].cwmin =
2200 cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2201 sta_ctxt_cmd.trig_based_txf[i].cwmax =
2202 cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2203 sta_ctxt_cmd.trig_based_txf[i].aifsn =
2204 cpu_to_le16(mu_edca->aifsn);
2205 sta_ctxt_cmd.trig_based_txf[i].mu_time =
2206 cpu_to_le16(mu_edca->mu_edca_timer);
2207 }
2208
2209 if (vif->bss_conf.multi_sta_back_32bit)
2210 flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2211
2212 if (vif->bss_conf.ack_enabled)
2213 flags |= STA_CTXT_HE_ACK_ENABLED;
2214
2215 if (vif->bss_conf.uora_exists) {
2216 flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2217
2218 sta_ctxt_cmd.rand_alloc_ecwmin =
2219 vif->bss_conf.uora_ocw_range & 0x7;
2220 sta_ctxt_cmd.rand_alloc_ecwmax =
2221 (vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2222 }
2223
2224 /* TODO: support Multi BSSID IE */
2225
2226 sta_ctxt_cmd.flags = cpu_to_le32(flags);
2227
2228 if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2229 DATA_PATH_GROUP, 0),
2230 0, sizeof(sta_ctxt_cmd), &sta_ctxt_cmd))
2231 IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2232}
2233
8ca151b5
JB
2234static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2235 struct ieee80211_vif *vif,
2236 struct ieee80211_bss_conf *bss_conf,
2237 u32 changes)
2238{
2239 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2240 int ret;
2241
6e97b0d2
IP
2242 /*
2243 * Re-calculate the tsf id, as the master-slave relations depend on the
2244 * beacon interval, which was not known when the station interface was
2245 * added.
2246 */
514c3069 2247 if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
230ba6c5
LC
2248 if (vif->bss_conf.he_support &&
2249 !iwlwifi_mod_params.disable_11ax)
514c3069
LC
2250 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2251
6e97b0d2 2252 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
514c3069 2253 }
6e97b0d2 2254
40ecdd01
ST
2255 /* Update MU EDCA params */
2256 if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2257 bss_conf->assoc && vif->bss_conf.he_support &&
2258 !iwlwifi_mod_params.disable_11ax)
2259 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2260
3dfd3a97
JB
2261 /*
2262 * If we're not associated yet, take the (new) BSSID before associating
2263 * so the firmware knows. If we're already associated, then use the old
2264 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2265 * branch for disassociation below.
2266 */
2267 if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2268 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2269
2270 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
8ca151b5
JB
2271 if (ret)
2272 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2273
3dfd3a97
JB
2274 /* after sending it once, adopt mac80211 data */
2275 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2276 mvmvif->associated = bss_conf->assoc;
2277
8ca151b5
JB
2278 if (changes & BSS_CHANGED_ASSOC) {
2279 if (bss_conf->assoc) {
33cef925
JB
2280 /* clear statistics to get clean beacon counter */
2281 iwl_mvm_request_statistics(mvm, true);
2282 memset(&mvmvif->beacon_stats, 0,
2283 sizeof(mvmvif->beacon_stats));
2284
8ca151b5 2285 /* add quota for this interface */
7754ae79 2286 ret = iwl_mvm_update_quotas(mvm, true, NULL);
8ca151b5
JB
2287 if (ret) {
2288 IWL_ERR(mvm, "failed to update quotas\n");
2289 return;
2290 }
016d27e1
JB
2291
2292 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2293 &mvm->status)) {
2294 /*
2295 * If we're restarting then the firmware will
2296 * obviously have lost synchronisation with
2297 * the AP. It will attempt to synchronise by
2298 * itself, but we can make it more reliable by
2299 * scheduling a session protection time event.
2300 *
2301 * The firmware needs to receive a beacon to
2302 * catch up with synchronisation, use 110% of
2303 * the beacon interval.
2304 *
2305 * Set a large maximum delay to allow for more
2306 * than a single interface.
2307 */
2308 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2309 iwl_mvm_protect_session(mvm, vif, dur, dur,
d20d37bc 2310 5 * dur, false);
016d27e1 2311 }
1f3b0ff8
LE
2312
2313 iwl_mvm_sf_update(mvm, vif, false);
175a70b7 2314 iwl_mvm_power_vif_assoc(mvm, vif);
697162a1 2315 if (vif->p2p) {
29a90a49 2316 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
697162a1
EG
2317 iwl_mvm_update_smps(mvm, vif,
2318 IWL_MVM_SMPS_REQ_PROT,
2319 IEEE80211_SMPS_DYNAMIC);
2320 }
0ae98812 2321 } else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
1f3b0ff8
LE
2322 /*
2323 * If update fails - SF might be running in associated
2324 * mode while disassociated - which is forbidden.
2325 */
2326 WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
2327 "Failed to update SF upon disassociation\n");
2328
6b28f978
EG
2329 /*
2330 * If we get an assert during the connection (after the
2331 * station has been added, but before the vif is set
2332 * to associated), mac80211 will re-add the station and
2333 * then configure the vif. Since the vif is not
2334 * associated, we would remove the station here and
2335 * this would fail the recovery.
2336 */
2337 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2338 &mvm->status)) {
2339 /*
2340 * Remove AP station now that
2341 * the MAC is unassoc
2342 */
2343 ret = iwl_mvm_rm_sta_id(mvm, vif,
2344 mvmvif->ap_sta_id);
2345 if (ret)
2346 IWL_ERR(mvm,
2347 "failed to remove AP station\n");
2348
2349 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
2350 mvm->d0i3_ap_sta_id =
2351 IWL_MVM_INVALID_STA;
2352 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
2353 }
37577fe2 2354
8ca151b5 2355 /* remove quota for this interface */
7754ae79 2356 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5
JB
2357 if (ret)
2358 IWL_ERR(mvm, "failed to update quotas\n");
29a90a49
EP
2359
2360 if (vif->p2p)
2361 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
3dfd3a97
JB
2362
2363 /* this will take the cleared BSSID from bss_conf */
2364 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2365 if (ret)
2366 IWL_ERR(mvm,
2367 "failed to update MAC %pM (clear after unassoc)\n",
2368 vif->addr);
8ca151b5 2369 }
a20fd398 2370
a07a8f37
SS
2371 /*
2372 * The firmware tracks the MU-MIMO group on its own.
f92659a1 2373 * However, on HW restart we should restore this data.
a07a8f37
SS
2374 */
2375 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
f92659a1 2376 (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
a07a8f37
SS
2377 ret = iwl_mvm_update_mu_groups(mvm, vif);
2378 if (ret)
2379 IWL_ERR(mvm,
2380 "failed to update VHT MU_MIMO groups\n");
2381 }
2382
e59647ea 2383 iwl_mvm_recalc_multicast(mvm);
34672bb3 2384 iwl_mvm_configure_bcast_filter(mvm);
e59647ea 2385
a20fd398
AO
2386 /* reset rssi values */
2387 mvmvif->bf_data.ave_beacon_signal = 0;
2388
8e484f0b 2389 iwl_mvm_bt_coex_vif_change(mvm);
f94045ed
EG
2390 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2391 IEEE80211_SMPS_AUTOMATIC);
355346ba
AS
2392 if (fw_has_capa(&mvm->fw->ucode_capa,
2393 IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2394 iwl_mvm_config_scan(mvm);
b45242c9
AS
2395 }
2396
2397 if (changes & BSS_CHANGED_BEACON_INFO) {
210a544e 2398 /*
b45242c9 2399 * We received a beacon from the associated AP so
210a544e
JB
2400 * remove the session protection.
2401 */
3edfb5f4 2402 iwl_mvm_stop_session_protection(mvm, vif);
cc87d322 2403
cc87d322
EH
2404 iwl_mvm_sf_update(mvm, vif, false);
2405 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2406 }
2407
283115fb
AA
2408 if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2409 /*
2410 * Send power command on every beacon change,
2411 * because we may have not enabled beacon abort yet.
2412 */
2413 BSS_CHANGED_BEACON_INFO)) {
1bc10d3b
JB
2414 ret = iwl_mvm_power_update_mac(mvm);
2415 if (ret)
2416 IWL_ERR(mvm, "failed to update power mode\n");
2417 }
2418
88f2fd73
MG
2419 if (changes & BSS_CHANGED_TXPOWER) {
2420 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2421 bss_conf->txpower);
2422 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2423 }
a20fd398
AO
2424
2425 if (changes & BSS_CHANGED_CQM) {
3c6acb61 2426 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
a20fd398
AO
2427 /* reset cqm events tracking */
2428 mvmvif->bf_data.last_cqm_event = 0;
fa7b2e7f
AA
2429 if (mvmvif->bf_data.bf_enabled) {
2430 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2431 if (ret)
2432 IWL_ERR(mvm,
2433 "failed to update CQM thresholds\n");
2434 }
a20fd398 2435 }
2ee8f021
EP
2436
2437 if (changes & BSS_CHANGED_ARP_FILTER) {
3c6acb61 2438 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
34672bb3 2439 iwl_mvm_configure_bcast_filter(mvm);
2ee8f021 2440 }
8ca151b5
JB
2441}
2442
5023d966
JB
2443static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2444 struct ieee80211_vif *vif)
8ca151b5
JB
2445{
2446 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2447 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2448 int ret;
2449
576eeee9
EP
2450 /*
2451 * iwl_mvm_mac_ctxt_add() might read directly from the device
2452 * (the system time), so make sure it is available.
2453 */
2454 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
2455 if (ret)
2456 return ret;
2457
8ca151b5
JB
2458 mutex_lock(&mvm->mutex);
2459
2460 /* Send the beacon template */
2461 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2462 if (ret)
2463 goto out_unlock;
2464
6e97b0d2
IP
2465 /*
2466 * Re-calculate the tsf id, as the master-slave relations depend on the
2467 * beacon interval, which was not known when the AP interface was added.
2468 */
2469 if (vif->type == NL80211_IFTYPE_AP)
2470 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2471
94939080
GG
2472 mvmvif->ap_assoc_sta_count = 0;
2473
8ca151b5
JB
2474 /* Add the mac context */
2475 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2476 if (ret)
2477 goto out_unlock;
2478
2479 /* Perform the binding */
2480 ret = iwl_mvm_binding_add_vif(mvm, vif);
2481 if (ret)
2482 goto out_remove;
2483
63dd5d02
SS
2484 /*
2485 * This is not very nice, but the simplest:
2486 * For older FWs adding the mcast sta before the bcast station may
2487 * cause assert 0x2b00.
2488 * This is fixed in later FW so make the order of removal depend on
2489 * the TLV
2490 */
2491 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2492 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2493 if (ret)
2494 goto out_unbind;
2495 /*
2496 * Send the bcast station. At this stage the TBTT and DTIM time
2497 * events are added and applied to the scheduler
2498 */
2499 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2500 if (ret) {
2501 iwl_mvm_rm_mcast_sta(mvm, vif);
2502 goto out_unbind;
2503 }
2504 } else {
2505 /*
2506 * Send the bcast station. At this stage the TBTT and DTIM time
2507 * events are added and applied to the scheduler
2508 */
75fd4fec 2509 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
63dd5d02
SS
2510 if (ret)
2511 goto out_unbind;
75fd4fec 2512 ret = iwl_mvm_add_mcast_sta(mvm, vif);
63dd5d02
SS
2513 if (ret) {
2514 iwl_mvm_send_rm_bcast_sta(mvm, vif);
2515 goto out_unbind;
2516 }
2517 }
26d6c16b 2518
5691e218
IP
2519 /* must be set before quota calculations */
2520 mvmvif->ap_ibss_active = true;
2521
47242744
TM
2522 if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2523 iwl_mvm_vif_set_low_latency(mvmvif, true,
2524 LOW_LATENCY_VIF_TYPE);
2525 iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2526 }
2527
a11e144e 2528 /* power updated needs to be done before quotas */
999609f1 2529 iwl_mvm_power_update_mac(mvm);
a11e144e 2530
7754ae79 2531 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5 2532 if (ret)
a11e144e 2533 goto out_quota_failed;
8ca151b5 2534
5023d966 2535 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2536 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2537 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2538
29a90a49
EP
2539 iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
2540
8e484f0b 2541 iwl_mvm_bt_coex_vif_change(mvm);
dac94da8 2542
f697267f
AN
2543 /* we don't support TDLS during DCM */
2544 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2545 iwl_mvm_teardown_tdls_peers(mvm);
2546
939e4904 2547 goto out_unlock;
8ca151b5 2548
a11e144e 2549out_quota_failed:
999609f1 2550 iwl_mvm_power_update_mac(mvm);
5691e218 2551 mvmvif->ap_ibss_active = false;
013290aa 2552 iwl_mvm_send_rm_bcast_sta(mvm, vif);
ced19f26 2553 iwl_mvm_rm_mcast_sta(mvm, vif);
8ca151b5
JB
2554out_unbind:
2555 iwl_mvm_binding_remove_vif(mvm, vif);
2556out_remove:
2557 iwl_mvm_mac_ctxt_remove(mvm, vif);
2558out_unlock:
2559 mutex_unlock(&mvm->mutex);
576eeee9 2560 iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
8ca151b5
JB
2561 return ret;
2562}
2563
5023d966
JB
2564static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2565 struct ieee80211_vif *vif)
8ca151b5
JB
2566{
2567 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2568 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2569
38a12b5b
JB
2570 iwl_mvm_prepare_mac_removal(mvm, vif);
2571
8ca151b5
JB
2572 mutex_lock(&mvm->mutex);
2573
664322fa 2574 /* Handle AP stop while in CSA */
7f0a7c67
AO
2575 if (rcu_access_pointer(mvm->csa_vif) == vif) {
2576 iwl_mvm_remove_time_event(mvm, mvmvif,
2577 &mvmvif->time_event_data);
664322fa 2578 RCU_INIT_POINTER(mvm->csa_vif, NULL);
e9cb0327 2579 mvmvif->csa_countdown = false;
7f0a7c67 2580 }
664322fa 2581
003e5236
AO
2582 if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2583 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2584 mvm->csa_tx_block_bcn_timeout = 0;
2585 }
2586
5023d966 2587 mvmvif->ap_ibss_active = false;
1c87bbad 2588 mvm->ap_last_beacon_gp2 = 0;
8ca151b5 2589
47242744
TM
2590 if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2591 iwl_mvm_vif_set_low_latency(mvmvif, false,
2592 LOW_LATENCY_VIF_TYPE);
2593 iwl_mvm_send_low_latency_cmd(mvm, false, mvmvif->id);
2594 }
2595
8e484f0b 2596 iwl_mvm_bt_coex_vif_change(mvm);
dac94da8 2597
29a90a49
EP
2598 iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2599
5023d966 2600 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2601 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2602 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2603
7754ae79 2604 iwl_mvm_update_quotas(mvm, false, NULL);
ced19f26
SS
2605
2606 /*
2607 * This is not very nice, but the simplest:
2608 * For older FWs removing the mcast sta before the bcast station may
2609 * cause assert 0x2b00.
2610 * This is fixed in later FW (which will stop beaconing when removing
2611 * bcast station).
2612 * So make the order of removal depend on the TLV
2613 */
2614 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2615 iwl_mvm_rm_mcast_sta(mvm, vif);
013290aa 2616 iwl_mvm_send_rm_bcast_sta(mvm, vif);
ced19f26
SS
2617 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2618 iwl_mvm_rm_mcast_sta(mvm, vif);
8ca151b5 2619 iwl_mvm_binding_remove_vif(mvm, vif);
a11e144e 2620
999609f1 2621 iwl_mvm_power_update_mac(mvm);
a11e144e 2622
8ca151b5
JB
2623 iwl_mvm_mac_ctxt_remove(mvm, vif);
2624
337bfc98
AS
2625 kfree(mvmvif->ap_wep_key);
2626 mvmvif->ap_wep_key = NULL;
2627
8ca151b5
JB
2628 mutex_unlock(&mvm->mutex);
2629}
2630
5023d966
JB
2631static void
2632iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2633 struct ieee80211_vif *vif,
2634 struct ieee80211_bss_conf *bss_conf,
2635 u32 changes)
8ca151b5 2636{
be2056fc 2637 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8a5e3660 2638
be2056fc
IP
2639 /* Changes will be applied when the AP/IBSS is started */
2640 if (!mvmvif->ap_ibss_active)
2641 return;
2642
863230da 2643 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
f7d8b702 2644 BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3dfd3a97 2645 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
863230da 2646 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
8a5e3660 2647
8ca151b5 2648 /* Need to send a new beacon template to the FW */
863230da
JB
2649 if (changes & BSS_CHANGED_BEACON &&
2650 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2651 IWL_WARN(mvm, "Failed updating beacon data\n");
79b7a69d
HD
2652
2653 if (changes & BSS_CHANGED_TXPOWER) {
2654 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2655 bss_conf->txpower);
2656 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2657 }
8ca151b5
JB
2658}
2659
2660static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2661 struct ieee80211_vif *vif,
2662 struct ieee80211_bss_conf *bss_conf,
2663 u32 changes)
2664{
2665 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2666
576eeee9
EP
2667 /*
2668 * iwl_mvm_bss_info_changed_station() might call
2669 * iwl_mvm_protect_session(), which reads directly from
2670 * the device (the system time), so make sure it is available.
2671 */
2672 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2673 return;
2674
8ca151b5
JB
2675 mutex_lock(&mvm->mutex);
2676
723f02ed 2677 if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
c7d42480 2678 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
723f02ed 2679
8ca151b5
JB
2680 switch (vif->type) {
2681 case NL80211_IFTYPE_STATION:
2682 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2683 break;
2684 case NL80211_IFTYPE_AP:
5023d966
JB
2685 case NL80211_IFTYPE_ADHOC:
2686 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
8ca151b5 2687 break;
91b08c2d
AE
2688 case NL80211_IFTYPE_MONITOR:
2689 if (changes & BSS_CHANGED_MU_GROUPS)
2690 iwl_mvm_update_mu_groups(mvm, vif);
2691 break;
8ca151b5
JB
2692 default:
2693 /* shouldn't happen */
2694 WARN_ON_ONCE(1);
2695 }
2696
2697 mutex_unlock(&mvm->mutex);
576eeee9 2698 iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
8ca151b5
JB
2699}
2700
2701static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2702 struct ieee80211_vif *vif,
c56ef672 2703 struct ieee80211_scan_request *hw_req)
8ca151b5
JB
2704{
2705 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2706 int ret;
2707
6749dd80
LC
2708 if (hw_req->req.n_channels == 0 ||
2709 hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
8ca151b5
JB
2710 return -EINVAL;
2711
2712 mutex_lock(&mvm->mutex);
6749dd80 2713 ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
8ca151b5 2714 mutex_unlock(&mvm->mutex);
6749dd80 2715
8ca151b5
JB
2716 return ret;
2717}
2718
2719static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2720 struct ieee80211_vif *vif)
2721{
2722 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2723
2724 mutex_lock(&mvm->mutex);
2725
e7d3abab
LC
2726 /* Due to a race condition, it's possible that mac80211 asks
2727 * us to stop a hw_scan when it's already stopped. This can
2728 * happen, for instance, if we stopped the scan ourselves,
2729 * called ieee80211_scan_completed() and the userspace called
2730 * cancel scan scan before ieee80211_scan_work() could run.
2731 * To handle that, simply return if the scan is not running.
2732 */
262888fc 2733 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
c7d42480 2734 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
8ca151b5
JB
2735
2736 mutex_unlock(&mvm->mutex);
2737}
2738
2739static void
2740iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3e56eadf 2741 struct ieee80211_sta *sta, u16 tids,
8ca151b5
JB
2742 int num_frames,
2743 enum ieee80211_frame_release_type reason,
2744 bool more_data)
2745{
2746 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5 2747
3e56eadf 2748 /* Called when we need to transmit (a) frame(s) from mac80211 */
8ca151b5 2749
3e56eadf
JB
2750 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2751 tids, more_data, false);
2752}
2753
2754static void
2755iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2756 struct ieee80211_sta *sta, u16 tids,
2757 int num_frames,
2758 enum ieee80211_frame_release_type reason,
2759 bool more_data)
2760{
2761 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2762
9a3fcf91 2763 /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3e56eadf
JB
2764
2765 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2766 tids, more_data, true);
8ca151b5
JB
2767}
2768
65e25482
JB
2769static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2770 enum sta_notify_cmd cmd,
2771 struct ieee80211_sta *sta)
8ca151b5
JB
2772{
2773 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5b577a90 2774 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
c22b0ff5 2775 unsigned long txqs = 0, tids = 0;
3e56eadf 2776 int tid;
8ca151b5 2777
960f864b
JB
2778 /*
2779 * If we have TVQM then we get too high queue numbers - luckily
2780 * we really shouldn't get here with that because such hardware
2781 * should have firmware supporting buffer station offload.
2782 */
2783 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2784 return;
2785
c22b0ff5 2786 spin_lock_bh(&mvmsta->lock);
311590a3 2787 for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
c22b0ff5
EG
2788 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2789
6862fcee 2790 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
1c17627b
SS
2791 continue;
2792
c22b0ff5
EG
2793 __set_bit(tid_data->txq_id, &txqs);
2794
dd32162d 2795 if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
c22b0ff5
EG
2796 continue;
2797
2798 __set_bit(tid, &tids);
2799 }
2800
8ca151b5
JB
2801 switch (cmd) {
2802 case STA_NOTIFY_SLEEP:
c22b0ff5 2803 for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3e56eadf 2804 ieee80211_sta_set_buffered(sta, tid, true);
c22b0ff5
EG
2805
2806 if (txqs)
2807 iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
8ca151b5
JB
2808 /*
2809 * The fw updates the STA to be asleep. Tx packets on the Tx
2810 * queues to this station will not be transmitted. The fw will
2811 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2812 */
2813 break;
2814 case STA_NOTIFY_AWAKE:
0ae98812 2815 if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
8ca151b5 2816 break;
c22b0ff5
EG
2817
2818 if (txqs)
2819 iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
9cc40712 2820 iwl_mvm_sta_modify_ps_wake(mvm, sta);
8ca151b5
JB
2821 break;
2822 default:
2823 break;
2824 }
c22b0ff5 2825 spin_unlock_bh(&mvmsta->lock);
8ca151b5
JB
2826}
2827
65e25482
JB
2828static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2829 struct ieee80211_vif *vif,
2830 enum sta_notify_cmd cmd,
2831 struct ieee80211_sta *sta)
2832{
2833 __iwl_mvm_mac_sta_notify(hw, cmd, sta);
2834}
2835
2836void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2837{
2838 struct iwl_rx_packet *pkt = rxb_addr(rxb);
2839 struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
2840 struct ieee80211_sta *sta;
2841 struct iwl_mvm_sta *mvmsta;
2842 bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
2843
2844 if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
2845 return;
2846
2847 rcu_read_lock();
a9560029 2848 sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
65e25482
JB
2849 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
2850 rcu_read_unlock();
2851 return;
2852 }
2853
2854 mvmsta = iwl_mvm_sta_from_mac80211(sta);
2855
2856 if (!mvmsta->vif ||
2857 mvmsta->vif->type != NL80211_IFTYPE_AP) {
2858 rcu_read_unlock();
2859 return;
2860 }
2861
2862 if (mvmsta->sleeping != sleeping) {
2863 mvmsta->sleeping = sleeping;
2864 __iwl_mvm_mac_sta_notify(mvm->hw,
2865 sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
2866 sta);
2867 ieee80211_sta_ps_transition(sta, sleeping);
2868 }
2869
2870 if (sleeping) {
2871 switch (notif->type) {
2872 case IWL_MVM_PM_EVENT_AWAKE:
2873 case IWL_MVM_PM_EVENT_ASLEEP:
2874 break;
2875 case IWL_MVM_PM_EVENT_UAPSD:
2876 ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
2877 break;
2878 case IWL_MVM_PM_EVENT_PS_POLL:
2879 ieee80211_sta_pspoll(sta);
2880 break;
2881 default:
2882 break;
2883 }
2884 }
2885
2886 rcu_read_unlock();
2887}
2888
1ddbbb0c
JB
2889static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2890 struct ieee80211_vif *vif,
2891 struct ieee80211_sta *sta)
2892{
2893 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 2894 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1ddbbb0c
JB
2895
2896 /*
2897 * This is called before mac80211 does RCU synchronisation,
2898 * so here we already invalidate our internal RCU-protected
2899 * station pointer. The rest of the code will thus no longer
2900 * be able to find the station this way, and we don't rely
2901 * on further RCU synchronisation after the sta_state()
2902 * callback deleted the station.
2903 */
2904 mutex_lock(&mvm->mutex);
2905 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2906 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2907 ERR_PTR(-ENOENT));
94939080 2908
1ddbbb0c
JB
2909 mutex_unlock(&mvm->mutex);
2910}
2911
bd1ba664
JB
2912static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2913 const u8 *bssid)
2914{
b0ffe455
JB
2915 int i;
2916
2917 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2918 struct iwl_mvm_tcm_mac *mdata;
2919
2920 mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2921 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2922 mdata->opened_rx_ba_sessions = false;
2923 }
2924
bd1ba664
JB
2925 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2926 return;
2927
c5241b0c 2928 if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
cee5a882
AA
2929 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2930 return;
2931 }
2932
11dee0b4
EG
2933 if (!vif->p2p &&
2934 (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
bd1ba664
JB
2935 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2936 return;
2937 }
2938
b0ffe455
JB
2939 for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2940 if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2941 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2942 return;
2943 }
2944 }
2945
bd1ba664
JB
2946 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2947}
2948
1e8f1329
GBA
2949static void
2950iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
2951 struct ieee80211_vif *vif, u8 *peer_addr,
2952 enum nl80211_tdls_operation action)
2953{
2954 struct iwl_fw_dbg_trigger_tlv *trig;
2955 struct iwl_fw_dbg_trigger_tdls *tdls_trig;
2956
6c042d75
SS
2957 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
2958 FW_DBG_TRIGGER_TDLS);
2959 if (!trig)
1e8f1329
GBA
2960 return;
2961
1e8f1329 2962 tdls_trig = (void *)trig->data;
1e8f1329
GBA
2963
2964 if (!(tdls_trig->action_bitmap & BIT(action)))
2965 return;
2966
2967 if (tdls_trig->peer_mode &&
2968 memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
2969 return;
2970
7174beb6
JB
2971 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
2972 "TDLS event occurred, peer %pM, action %d",
2973 peer_addr, action);
1e8f1329
GBA
2974}
2975
8ca151b5
JB
2976static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2977 struct ieee80211_vif *vif,
2978 struct ieee80211_sta *sta,
2979 enum ieee80211_sta_state old_state,
2980 enum ieee80211_sta_state new_state)
2981{
2982 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2983 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6ea29ce5 2984 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
2985 int ret;
2986
2987 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2988 sta->addr, old_state, new_state);
2989
2990 /* this would be a mac80211 bug ... but don't crash */
2991 if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2992 return -EINVAL;
2993
24afba76
LK
2994 /*
2995 * If we are in a STA removal flow and in DQA mode:
2996 *
2997 * This is after the sync_rcu part, so the queues have already been
2998 * flushed. No more TXs on their way in mac80211's path, and no more in
2999 * the queues.
3000 * Also, we won't be getting any new TX frames for this station.
3001 * What we might have are deferred TX frames that need to be taken care
3002 * of.
3003 *
3004 * Drop any still-queued deferred-frame before removing the STA, and
3005 * make sure the worker is no longer handling frames for this STA.
3006 */
3007 if (old_state == IEEE80211_STA_NONE &&
c8f54701 3008 new_state == IEEE80211_STA_NOTEXIST) {
24afba76
LK
3009 flush_work(&mvm->add_stream_wk);
3010
3011 /*
3012 * No need to make sure deferred TX indication is off since the
3013 * worker will already remove it if it was on
3014 */
3015 }
3016
8ca151b5 3017 mutex_lock(&mvm->mutex);
6ea29ce5 3018 /* track whether or not the station is associated */
d94c5a82 3019 mvm_sta->sta_state = new_state;
6ea29ce5 3020
8ca151b5
JB
3021 if (old_state == IEEE80211_STA_NOTEXIST &&
3022 new_state == IEEE80211_STA_NONE) {
48bc1307
JB
3023 /*
3024 * Firmware bug - it'll crash if the beacon interval is less
3025 * than 16. We can't avoid connecting at all, so refuse the
3026 * station state change, this will cause mac80211 to abandon
3027 * attempts to connect to this AP, and eventually wpa_s will
3028 * blacklist the AP...
3029 */
3030 if (vif->type == NL80211_IFTYPE_STATION &&
3031 vif->bss_conf.beacon_int < 16) {
3032 IWL_ERR(mvm,
3033 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3034 sta->addr, vif->bss_conf.beacon_int);
3035 ret = -EINVAL;
3036 goto out_unlock;
3037 }
cf7b491d
AN
3038
3039 if (sta->tdls &&
3040 (vif->p2p ||
fa3d07e4
AN
3041 iwl_mvm_tdls_sta_count(mvm, NULL) ==
3042 IWL_MVM_TDLS_STA_COUNT ||
cf7b491d
AN
3043 iwl_mvm_phy_ctx_count(mvm) > 1)) {
3044 IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3045 ret = -EBUSY;
3046 goto out_unlock;
3047 }
3048
8ca151b5 3049 ret = iwl_mvm_add_sta(mvm, vif, sta);
1e8f1329 3050 if (sta->tdls && ret == 0) {
fa3d07e4 3051 iwl_mvm_recalc_tdls_state(mvm, vif, true);
1e8f1329
GBA
3052 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3053 NL80211_TDLS_SETUP);
3054 }
438af969
SS
3055
3056 sta->max_rc_amsdu_len = 1;
8ca151b5
JB
3057 } else if (old_state == IEEE80211_STA_NONE &&
3058 new_state == IEEE80211_STA_AUTH) {
e820c2da
HD
3059 /*
3060 * EBS may be disabled due to previous failures reported by FW.
3061 * Reset EBS status here assuming environment has been changed.
3062 */
3063 mvm->last_ebs_successful = true;
bd1ba664 3064 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
8ca151b5
JB
3065 ret = 0;
3066 } else if (old_state == IEEE80211_STA_AUTH &&
3067 new_state == IEEE80211_STA_ASSOC) {
8be30c13
AB
3068 if (vif->type == NL80211_IFTYPE_AP) {
3069 mvmvif->ap_assoc_sta_count++;
3070 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
02221a81
ST
3071 if (vif->bss_conf.he_support &&
3072 !iwlwifi_mod_params.disable_11ax)
3073 iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
8be30c13 3074 }
735a0045 3075
3baf7528
AS
3076 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3077 false);
7a453973 3078 ret = iwl_mvm_update_sta(mvm, vif, sta);
8ca151b5
JB
3079 } else if (old_state == IEEE80211_STA_ASSOC &&
3080 new_state == IEEE80211_STA_AUTHORIZED) {
28916a16
EG
3081 /* if wep is used, need to set the key for the station now */
3082 if (vif->type == NL80211_IFTYPE_AP && mvmvif->ap_wep_key) {
3083 mvm_sta->wep_key =
3084 kmemdup(mvmvif->ap_wep_key,
3085 sizeof(*mvmvif->ap_wep_key) +
3086 mvmvif->ap_wep_key->keylen,
3087 GFP_KERNEL);
3088 if (!mvm_sta->wep_key) {
3089 ret = -ENOMEM;
3090 goto out_unlock;
3091 }
3092
3093 ret = iwl_mvm_set_sta_key(mvm, vif, sta,
3094 mvm_sta->wep_key,
3095 STA_KEY_IDX_INVALID);
3096 } else {
3097 ret = 0;
3098 }
f59e0e3c
AN
3099
3100 /* we don't support TDLS during DCM */
3101 if (iwl_mvm_phy_ctx_count(mvm) > 1)
3102 iwl_mvm_teardown_tdls_peers(mvm);
3103
1e8f1329
GBA
3104 if (sta->tdls)
3105 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3106 NL80211_TDLS_ENABLE_LINK);
3107
7df15b1e 3108 /* enable beacon filtering */
fa7b2e7f 3109 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
6b7a5aea 3110
3baf7528
AS
3111 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3112 true);
8ca151b5
JB
3113 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
3114 new_state == IEEE80211_STA_ASSOC) {
7df15b1e 3115 /* disable beacon filtering */
a1022927 3116 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
8ca151b5
JB
3117 ret = 0;
3118 } else if (old_state == IEEE80211_STA_ASSOC &&
3119 new_state == IEEE80211_STA_AUTH) {
8be30c13
AB
3120 if (vif->type == NL80211_IFTYPE_AP) {
3121 mvmvif->ap_assoc_sta_count--;
3122 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3123 }
8ca151b5
JB
3124 ret = 0;
3125 } else if (old_state == IEEE80211_STA_AUTH &&
3126 new_state == IEEE80211_STA_NONE) {
3127 ret = 0;
3128 } else if (old_state == IEEE80211_STA_NONE &&
3129 new_state == IEEE80211_STA_NOTEXIST) {
3130 ret = iwl_mvm_rm_sta(mvm, vif, sta);
1e8f1329 3131 if (sta->tdls) {
fa3d07e4 3132 iwl_mvm_recalc_tdls_state(mvm, vif, false);
1e8f1329
GBA
3133 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3134 NL80211_TDLS_DISABLE_LINK);
3135 }
34a880d8
LK
3136
3137 /* Remove STA key if this is an AP using WEP */
3138 if (vif->type == NL80211_IFTYPE_AP && mvmvif->ap_wep_key) {
3139 int rm_ret = iwl_mvm_remove_sta_key(mvm, vif, sta,
28916a16 3140 mvm_sta->wep_key);
34a880d8
LK
3141
3142 if (!ret)
3143 ret = rm_ret;
28916a16
EG
3144 kfree(mvm_sta->wep_key);
3145 mvm_sta->wep_key = NULL;
34a880d8
LK
3146 }
3147
8ca151b5
JB
3148 } else {
3149 ret = -EIO;
3150 }
48bc1307 3151 out_unlock:
8ca151b5
JB
3152 mutex_unlock(&mvm->mutex);
3153
9c126cd6
LK
3154 if (sta->tdls && ret == 0) {
3155 if (old_state == IEEE80211_STA_NOTEXIST &&
3156 new_state == IEEE80211_STA_NONE)
3157 ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3158 else if (old_state == IEEE80211_STA_NONE &&
3159 new_state == IEEE80211_STA_NOTEXIST)
3160 ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3161 }
3162
8ca151b5
JB
3163 return ret;
3164}
3165
3166static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3167{
3168 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3169
3170 mvm->rts_threshold = value;
3171
3172 return 0;
3173}
3174
1f3b0ff8
LE
3175static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3176 struct ieee80211_vif *vif,
3177 struct ieee80211_sta *sta, u32 changed)
3178{
3179 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3180
3181 if (vif->type == NL80211_IFTYPE_STATION &&
3182 changed & IEEE80211_RC_NSS_CHANGED)
3183 iwl_mvm_sf_update(mvm, vif, false);
3184}
3185
8ca151b5
JB
3186static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3187 struct ieee80211_vif *vif, u16 ac,
3188 const struct ieee80211_tx_queue_params *params)
3189{
3190 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3191 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3192
3193 mvmvif->queue_params[ac] = *params;
3194
3195 /*
3196 * No need to update right away, we'll get BSS_CHANGED_QOS
3197 * The exception is P2P_DEVICE interface which needs immediate update.
3198 */
3199 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3200 int ret;
3201
3202 mutex_lock(&mvm->mutex);
3dfd3a97 3203 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
8ca151b5
JB
3204 mutex_unlock(&mvm->mutex);
3205 return ret;
3206 }
3207 return 0;
3208}
3209
3210static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
d4e36e55
IP
3211 struct ieee80211_vif *vif,
3212 u16 req_duration)
8ca151b5
JB
3213{
3214 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
7c70fee5
SS
3215 u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
3216 u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
8ca151b5 3217
576eeee9
EP
3218 /*
3219 * iwl_mvm_protect_session() reads directly from the device
3220 * (the system time), so make sure it is available.
3221 */
3222 if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
3223 return;
3224
d4e36e55
IP
3225 if (req_duration > duration)
3226 duration = req_duration;
3227
8ca151b5
JB
3228 mutex_lock(&mvm->mutex);
3229 /* Try really hard to protect the session and hear a beacon */
d20d37bc 3230 iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
8ca151b5 3231 mutex_unlock(&mvm->mutex);
576eeee9
EP
3232
3233 iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
8ca151b5
JB
3234}
3235
35a000b7
DS
3236static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3237 struct ieee80211_vif *vif,
3238 struct cfg80211_sched_scan_request *req,
633e2713 3239 struct ieee80211_scan_ies *ies)
35a000b7
DS
3240{
3241 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
35a000b7 3242
35a000b7 3243 int ret;
4660dfbb 3244
35a000b7
DS
3245 mutex_lock(&mvm->mutex);
3246
1f940386 3247 if (!vif->bss_conf.idle) {
bd5e4744
DS
3248 ret = -EBUSY;
3249 goto out;
3250 }
3251
19945dfb 3252 ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
d2496221 3253
35a000b7
DS
3254out:
3255 mutex_unlock(&mvm->mutex);
3256 return ret;
3257}
3258
37e3308c
JB
3259static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3260 struct ieee80211_vif *vif)
35a000b7
DS
3261{
3262 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33ea27f6 3263 int ret;
35a000b7
DS
3264
3265 mutex_lock(&mvm->mutex);
e7d3abab
LC
3266
3267 /* Due to a race condition, it's possible that mac80211 asks
3268 * us to stop a sched_scan when it's already stopped. This
3269 * can happen, for instance, if we stopped the scan ourselves,
3270 * called ieee80211_sched_scan_stopped() and the userspace called
3271 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3272 * could run. To handle this, simply return if the scan is
3273 * not running.
3274 */
262888fc 3275 if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
e7d3abab
LC
3276 mutex_unlock(&mvm->mutex);
3277 return 0;
3278 }
3279
c7d42480 3280 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
35a000b7 3281 mutex_unlock(&mvm->mutex);
33ea27f6 3282 iwl_mvm_wait_for_async_handlers(mvm);
37e3308c 3283
33ea27f6 3284 return ret;
35a000b7
DS
3285}
3286
8ca151b5
JB
3287static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3288 enum set_key_cmd cmd,
3289 struct ieee80211_vif *vif,
3290 struct ieee80211_sta *sta,
3291 struct ieee80211_key_conf *key)
3292{
3293 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
f5e28eac
JB
3294 struct iwl_mvm_sta *mvmsta;
3295 struct iwl_mvm_key_pn *ptk_pn;
3296 int keyidx = key->keyidx;
8ca151b5 3297 int ret;
d6ee54a9 3298 u8 key_offset;
8ca151b5 3299
3b37f4c9 3300 if (iwlwifi_mod_params.swcrypto) {
8ca151b5
JB
3301 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
3302 return -EOPNOTSUPP;
3303 }
3304
3305 switch (key->cipher) {
3306 case WLAN_CIPHER_SUITE_TKIP:
7f768ad5
DS
3307 if (!mvm->trans->cfg->gen2) {
3308 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3309 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3310 } else if (vif->type == NL80211_IFTYPE_STATION) {
3311 key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
3312 } else {
3313 IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
3314 return -EOPNOTSUPP;
3315 }
8ca151b5 3316 break;
ca8c0f4b 3317 case WLAN_CIPHER_SUITE_CCMP:
2a53d166
AB
3318 case WLAN_CIPHER_SUITE_GCMP:
3319 case WLAN_CIPHER_SUITE_GCMP_256:
85aeb58c
DS
3320 if (!iwl_mvm_has_new_tx_api(mvm))
3321 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
ca8c0f4b 3322 break;
8ca151b5 3323 case WLAN_CIPHER_SUITE_AES_CMAC:
8e160ab8
AB
3324 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3325 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
30686bf7 3326 WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
8ca151b5
JB
3327 break;
3328 case WLAN_CIPHER_SUITE_WEP40:
3329 case WLAN_CIPHER_SUITE_WEP104:
337bfc98
AS
3330 if (vif->type == NL80211_IFTYPE_AP) {
3331 struct iwl_mvm_vif *mvmvif =
3332 iwl_mvm_vif_from_mac80211(vif);
3333
3334 mvmvif->ap_wep_key = kmemdup(key,
3335 sizeof(*key) + key->keylen,
3336 GFP_KERNEL);
3337 if (!mvmvif->ap_wep_key)
3338 return -ENOMEM;
3339 }
3340
ba3943b0
JB
3341 if (vif->type != NL80211_IFTYPE_STATION)
3342 return 0;
3343 break;
8ca151b5 3344 default:
e36e5433
MS
3345 /* currently FW supports only one optional cipher scheme */
3346 if (hw->n_cipher_schemes &&
3347 hw->cipher_schemes->cipher == key->cipher)
3348 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3349 else
3350 return -EOPNOTSUPP;
8ca151b5
JB
3351 }
3352
3353 mutex_lock(&mvm->mutex);
3354
3355 switch (cmd) {
3356 case SET_KEY:
5023d966
JB
3357 if ((vif->type == NL80211_IFTYPE_ADHOC ||
3358 vif->type == NL80211_IFTYPE_AP) && !sta) {
3359 /*
3360 * GTK on AP interface is a TX-only key, return 0;
3361 * on IBSS they're per-station and because we're lazy
3362 * we don't support them for RX, so do the same.
8e160ab8 3363 * CMAC/GMAC in AP/IBSS modes must be done in software.
5023d966 3364 */
8e160ab8
AB
3365 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3366 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3367 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
81279c49
JB
3368 ret = -EOPNOTSUPP;
3369 else
3370 ret = 0;
85aeb58c
DS
3371
3372 if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
3373 key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
3374 !iwl_mvm_has_new_tx_api(mvm)) {
3375 key->hw_key_idx = STA_KEY_IDX_INVALID;
3376 break;
3377 }
6caffd4f
JB
3378 }
3379
b546dcd6
JB
3380 /* During FW restart, in order to restore the state as it was,
3381 * don't try to reprogram keys we previously failed for.
3382 */
3383 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3384 key->hw_key_idx == STA_KEY_IDX_INVALID) {
3385 IWL_DEBUG_MAC80211(mvm,
3386 "skip invalid idx key programming during restart\n");
3387 ret = 0;
3388 break;
3389 }
3390
f5e28eac
JB
3391 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3392 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 struct ieee80211_key_seq seq;
3398 int tid, q;
3399
3400 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3401 WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
acafe7e3
KC
3402 ptk_pn = kzalloc(struct_size(ptk_pn, q,
3403 mvm->trans->num_rx_queues),
f5e28eac
JB
3404 GFP_KERNEL);
3405 if (!ptk_pn) {
3406 ret = -ENOMEM;
3407 break;
3408 }
3409
3410 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3411 ieee80211_get_key_rx_seq(key, tid, &seq);
3412 for (q = 0; q < mvm->trans->num_rx_queues; q++)
3413 memcpy(ptk_pn->q[q].pn[tid],
3414 seq.ccmp.pn,
3415 IEEE80211_CCMP_PN_LEN);
3416 }
3417
3418 rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3419 }
3420
d6ee54a9
LC
3421 /* in HW restart reuse the index, otherwise request a new one */
3422 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3423 key_offset = key->hw_key_idx;
3424 else
3425 key_offset = STA_KEY_IDX_INVALID;
3426
8ca151b5 3427 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
d6ee54a9 3428 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
8ca151b5
JB
3429 if (ret) {
3430 IWL_WARN(mvm, "set key failed\n");
3431 /*
3432 * can't add key for RX, but we don't need it
3433 * in the device for TX so still return 0
3434 */
6caffd4f 3435 key->hw_key_idx = STA_KEY_IDX_INVALID;
8ca151b5
JB
3436 ret = 0;
3437 }
3438
3439 break;
3440 case DISABLE_KEY:
6caffd4f
JB
3441 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3442 ret = 0;
3443 break;
3444 }
3445
f5e28eac
JB
3446 if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3447 key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3448 (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
2a53d166
AB
3449 key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3450 key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
f5e28eac
JB
3451 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3452 ptk_pn = rcu_dereference_protected(
3453 mvmsta->ptk_pn[keyidx],
3454 lockdep_is_held(&mvm->mutex));
3455 RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3456 if (ptk_pn)
3457 kfree_rcu(ptk_pn, rcu_head);
3458 }
3459
8ca151b5
JB
3460 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3461 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3462 break;
3463 default:
3464 ret = -EINVAL;
3465 }
3466
3467 mutex_unlock(&mvm->mutex);
3468 return ret;
3469}
3470
3471static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3472 struct ieee80211_vif *vif,
3473 struct ieee80211_key_conf *keyconf,
3474 struct ieee80211_sta *sta,
3475 u32 iv32, u16 *phase1key)
3476{
3477 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3478
5023d966
JB
3479 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3480 return;
3481
8ca151b5
JB
3482 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3483}
3484
3485
b112889c
AM
3486static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3487 struct iwl_rx_packet *pkt, void *data)
3488{
3489 struct iwl_mvm *mvm =
3490 container_of(notif_wait, struct iwl_mvm, notif_wait);
3491 struct iwl_hs20_roc_res *resp;
3492 int resp_len = iwl_rx_packet_payload_len(pkt);
3493 struct iwl_mvm_time_event_data *te_data = data;
3494
3495 if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3496 return true;
3497
3498 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3499 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3500 return true;
3501 }
3502
3503 resp = (void *)pkt->data;
3504
3505 IWL_DEBUG_TE(mvm,
b71a9c35 3506 "Aux ROC: Received response from ucode: status=%d uid=%d\n",
b112889c
AM
3507 resp->status, resp->event_unique_id);
3508
3509 te_data->uid = le32_to_cpu(resp->event_unique_id);
3510 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3511 te_data->uid);
3512
3513 spin_lock_bh(&mvm->time_event_lock);
3514 list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3515 spin_unlock_bh(&mvm->time_event_lock);
3516
3517 return true;
3518}
3519
dc28e12f
MG
3520#define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3521#define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3522#define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3523#define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3524#define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
b112889c
AM
3525static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3526 struct ieee80211_channel *channel,
3527 struct ieee80211_vif *vif,
3528 int duration)
3529{
3530 int res, time_reg = DEVICE_SYSTEM_TIME_REG;
3531 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3532 struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
6eb031d2 3533 static const u16 time_event_response[] = { HOT_SPOT_CMD };
b112889c 3534 struct iwl_notification_wait wait_time_event;
dc28e12f
MG
3535 u32 dtim_interval = vif->bss_conf.dtim_period *
3536 vif->bss_conf.beacon_int;
3537 u32 req_dur, delay;
b112889c
AM
3538 struct iwl_hs20_roc_req aux_roc_req = {
3539 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3540 .id_and_color =
3541 cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3542 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
57e861d9
DS
3543 };
3544 struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
3545 &aux_roc_req.channel_info);
3546 u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
3547
3548 /* Set the channel info data */
3549 iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
3550 (channel->band == NL80211_BAND_2GHZ) ?
3551 PHY_BAND_24 : PHY_BAND_5,
3552 PHY_VHT_CHANNEL_MODE20,
3553 0);
3554
3555 /* Set the time and duration */
3556 tail->apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg));
b112889c 3557
dc28e12f
MG
3558 delay = AUX_ROC_MIN_DELAY;
3559 req_dur = MSEC_TO_TU(duration);
3560
3561 /*
3562 * If we are associated we want the delay time to be at least one
3563 * dtim interval so that the FW can wait until after the DTIM and
3564 * then start the time event, this will potentially allow us to
3565 * remain off-channel for the max duration.
3566 * Since we want to use almost a whole dtim interval we would also
3567 * like the delay to be for 2-3 dtim intervals, in case there are
3568 * other time events with higher priority.
3569 */
3570 if (vif->bss_conf.assoc) {
3571 delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3572 /* We cannot remain off-channel longer than the DTIM interval */
3573 if (dtim_interval <= req_dur) {
3574 req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3575 if (req_dur <= AUX_ROC_MIN_DURATION)
3576 req_dur = dtim_interval -
3577 AUX_ROC_MIN_SAFETY_BUFFER;
3578 }
3579 }
3580
57e861d9
DS
3581 tail->duration = cpu_to_le32(req_dur);
3582 tail->apply_time_max_delay = cpu_to_le32(delay);
dc28e12f
MG
3583
3584 IWL_DEBUG_TE(mvm,
3585 "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3586 channel->hw_value, req_dur, duration, delay,
3587 dtim_interval);
b112889c 3588 /* Set the node address */
57e861d9 3589 memcpy(tail->node_addr, vif->addr, ETH_ALEN);
b112889c 3590
a6cc5163
MG
3591 lockdep_assert_held(&mvm->mutex);
3592
3593 spin_lock_bh(&mvm->time_event_lock);
3594
3595 if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3596 spin_unlock_bh(&mvm->time_event_lock);
3597 return -EIO;
3598 }
3599
b112889c
AM
3600 te_data->vif = vif;
3601 te_data->duration = duration;
3602 te_data->id = HOT_SPOT_CMD;
3603
b112889c
AM
3604 spin_unlock_bh(&mvm->time_event_lock);
3605
3606 /*
3607 * Use a notification wait, which really just processes the
3608 * command response and doesn't wait for anything, in order
3609 * to be able to process the response and get the UID inside
3610 * the RX path. Using CMD_WANT_SKB doesn't work because it
3611 * stores the buffer and then wakes up this thread, by which
3612 * time another notification (that the time event started)
3613 * might already be processed unsuccessfully.
3614 */
3615 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3616 time_event_response,
3617 ARRAY_SIZE(time_event_response),
3618 iwl_mvm_rx_aux_roc, te_data);
3619
57e861d9 3620 res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
b112889c
AM
3621 &aux_roc_req);
3622
3623 if (res) {
3624 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3625 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3626 goto out_clear_te;
3627 }
3628
3629 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
3630 res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3631 /* should never fail */
3632 WARN_ON_ONCE(res);
3633
3634 if (res) {
3635 out_clear_te:
3636 spin_lock_bh(&mvm->time_event_lock);
3637 iwl_mvm_te_clear_data(mvm, te_data);
3638 spin_unlock_bh(&mvm->time_event_lock);
3639 }
3640
3641 return res;
3642}
3643
8ca151b5
JB
3644static int iwl_mvm_roc(struct ieee80211_hw *hw,
3645 struct ieee80211_vif *vif,
3646 struct ieee80211_channel *channel,
d339d5ca
IP
3647 int duration,
3648 enum ieee80211_roc_type type)
8ca151b5
JB
3649{
3650 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3 3651 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 3652 struct cfg80211_chan_def chandef;
31d385ae
IP
3653 struct iwl_mvm_phy_ctxt *phy_ctxt;
3654 int ret, i;
3655
3656 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3657 duration, type);
8ca151b5 3658
9834781c
JB
3659 /*
3660 * Flush the done work, just in case it's still pending, so that
3661 * the work it does can complete and we can accept new frames.
3662 */
6ed13164
MG
3663 flush_work(&mvm->roc_done_wk);
3664
a6cc5163
MG
3665 mutex_lock(&mvm->mutex);
3666
b112889c
AM
3667 switch (vif->type) {
3668 case NL80211_IFTYPE_STATION:
859d914c
JB
3669 if (fw_has_capa(&mvm->fw->ucode_capa,
3670 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
5ac6c72e
LC
3671 /* Use aux roc framework (HS20) */
3672 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3673 vif, duration);
3674 goto out_unlock;
3675 }
3676 IWL_ERR(mvm, "hotspot not supported\n");
3677 ret = -EINVAL;
a6cc5163 3678 goto out_unlock;
b112889c
AM
3679 case NL80211_IFTYPE_P2P_DEVICE:
3680 /* handle below */
3681 break;
3682 default:
3683 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
a6cc5163
MG
3684 ret = -EINVAL;
3685 goto out_unlock;
8ca151b5
JB
3686 }
3687
31d385ae
IP
3688 for (i = 0; i < NUM_PHY_CTX; i++) {
3689 phy_ctxt = &mvm->phy_ctxts[i];
3690 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3691 continue;
3692
3693 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3694 /*
3695 * Unbind the P2P_DEVICE from the current PHY context,
3696 * and if the PHY context is not used remove it.
3697 */
3698 ret = iwl_mvm_binding_remove_vif(mvm, vif);
3699 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3700 goto out_unlock;
3701
3702 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3703
3704 /* Bind the P2P_DEVICE to the current PHY Context */
3705 mvmvif->phy_ctxt = phy_ctxt;
3706
3707 ret = iwl_mvm_binding_add_vif(mvm, vif);
3708 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3709 goto out_unlock;
3710
3711 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3712 goto schedule_time_event;
3713 }
3714 }
3715
3716 /* Need to update the PHY context only if the ROC channel changed */
3717 if (channel == mvmvif->phy_ctxt->channel)
3718 goto schedule_time_event;
3719
8ca151b5 3720 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
8ca151b5 3721
31d385ae
IP
3722 /*
3723 * Change the PHY context configuration as it is currently referenced
3724 * only by the P2P Device MAC
3725 */
3726 if (mvmvif->phy_ctxt->ref == 1) {
3727 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3728 &chandef, 1, 1);
3729 if (ret)
3730 goto out_unlock;
3731 } else {
3732 /*
3733 * The PHY context is shared with other MACs. Need to remove the
3734 * P2P Device from the binding, allocate an new PHY context and
3735 * create a new binding
3736 */
3737 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3738 if (!phy_ctxt) {
3739 ret = -ENOSPC;
3740 goto out_unlock;
3741 }
3742
3743 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3744 1, 1);
3745 if (ret) {
3746 IWL_ERR(mvm, "Failed to change PHY context\n");
3747 goto out_unlock;
3748 }
3749
3750 /* Unbind the P2P_DEVICE from the current PHY context */
3751 ret = iwl_mvm_binding_remove_vif(mvm, vif);
3752 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3753 goto out_unlock;
3754
3755 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3756
3757 /* Bind the P2P_DEVICE to the new allocated PHY context */
3758 mvmvif->phy_ctxt = phy_ctxt;
3759
3760 ret = iwl_mvm_binding_add_vif(mvm, vif);
3761 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3762 goto out_unlock;
3763
3764 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3765 }
3766
3767schedule_time_event:
8ca151b5 3768 /* Schedule the time events */
e635c797 3769 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
8ca151b5 3770
31d385ae 3771out_unlock:
8ca151b5
JB
3772 mutex_unlock(&mvm->mutex);
3773 IWL_DEBUG_MAC80211(mvm, "leave\n");
8ca151b5
JB
3774 return ret;
3775}
3776
3777static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
3778{
3779 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3780
3781 IWL_DEBUG_MAC80211(mvm, "enter\n");
3782
3783 mutex_lock(&mvm->mutex);
bf5da87f 3784 iwl_mvm_stop_roc(mvm);
8ca151b5
JB
3785 mutex_unlock(&mvm->mutex);
3786
3787 IWL_DEBUG_MAC80211(mvm, "leave\n");
3788 return 0;
3789}
3790
b08c1d97
LC
3791static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3792 struct ieee80211_chanctx_conf *ctx)
8ca151b5 3793{
fe0f2de3
IP
3794 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3795 struct iwl_mvm_phy_ctxt *phy_ctxt;
8ca151b5
JB
3796 int ret;
3797
b08c1d97
LC
3798 lockdep_assert_held(&mvm->mutex);
3799
53a9d61e 3800 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
fe0f2de3 3801
fe0f2de3
IP
3802 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3803 if (!phy_ctxt) {
3804 ret = -ENOSPC;
3805 goto out;
3806 }
8ca151b5 3807
dcbc3e1a 3808 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
53a9d61e
IP
3809 ctx->rx_chains_static,
3810 ctx->rx_chains_dynamic);
fe0f2de3
IP
3811 if (ret) {
3812 IWL_ERR(mvm, "Failed to add PHY context\n");
3813 goto out;
3814 }
3815
53a9d61e 3816 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
fe0f2de3
IP
3817 *phy_ctxt_id = phy_ctxt->id;
3818out:
b08c1d97
LC
3819 return ret;
3820}
3821
3822static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3823 struct ieee80211_chanctx_conf *ctx)
3824{
3825 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3826 int ret;
3827
3828 mutex_lock(&mvm->mutex);
3829 ret = __iwl_mvm_add_chanctx(mvm, ctx);
8ca151b5 3830 mutex_unlock(&mvm->mutex);
b08c1d97 3831
8ca151b5
JB
3832 return ret;
3833}
3834
b08c1d97
LC
3835static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3836 struct ieee80211_chanctx_conf *ctx)
3837{
3838 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3839 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3840
3841 lockdep_assert_held(&mvm->mutex);
3842
3843 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3844}
3845
8ca151b5
JB
3846static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3847 struct ieee80211_chanctx_conf *ctx)
3848{
3849 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5
JB
3850
3851 mutex_lock(&mvm->mutex);
b08c1d97 3852 __iwl_mvm_remove_chanctx(mvm, ctx);
8ca151b5
JB
3853 mutex_unlock(&mvm->mutex);
3854}
3855
3856static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3857 struct ieee80211_chanctx_conf *ctx,
3858 u32 changed)
3859{
3860 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
3861 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3862 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5 3863
31d385ae
IP
3864 if (WARN_ONCE((phy_ctxt->ref > 1) &&
3865 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3866 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2dceedae
AN
3867 IEEE80211_CHANCTX_CHANGE_RADAR |
3868 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
31d385ae
IP
3869 "Cannot change PHY. Ref=%d, changed=0x%X\n",
3870 phy_ctxt->ref, changed))
3871 return;
3872
8ca151b5 3873 mutex_lock(&mvm->mutex);
7a20bcce
EG
3874
3875 /* we are only changing the min_width, may be a noop */
3876 if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
3877 if (phy_ctxt->width == ctx->min_def.width)
3878 goto out_unlock;
3879
3880 /* we are just toggling between 20_NOHT and 20 */
3881 if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
3882 ctx->min_def.width <= NL80211_CHAN_WIDTH_20)
3883 goto out_unlock;
3884 }
3885
4d66449a 3886 iwl_mvm_bt_coex_vif_change(mvm);
dcbc3e1a 3887 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
8ca151b5
JB
3888 ctx->rx_chains_static,
3889 ctx->rx_chains_dynamic);
7a20bcce
EG
3890
3891out_unlock:
8ca151b5
JB
3892 mutex_unlock(&mvm->mutex);
3893}
3894
b08c1d97
LC
3895static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
3896 struct ieee80211_vif *vif,
f0c97783
LC
3897 struct ieee80211_chanctx_conf *ctx,
3898 bool switching_chanctx)
8ca151b5 3899{
fe0f2de3
IP
3900 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3901 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5
JB
3902 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3903 int ret;
3904
b08c1d97 3905 lockdep_assert_held(&mvm->mutex);
8ca151b5 3906
fe0f2de3 3907 mvmvif->phy_ctxt = phy_ctxt;
8ca151b5
JB
3908
3909 switch (vif->type) {
3910 case NL80211_IFTYPE_AP:
4741dd04
LC
3911 /* only needed if we're switching chanctx (i.e. during CSA) */
3912 if (switching_chanctx) {
bd3398e2
AO
3913 mvmvif->ap_ibss_active = true;
3914 break;
3915 }
244726ec 3916 /* fall through */
5023d966 3917 case NL80211_IFTYPE_ADHOC:
8ca151b5
JB
3918 /*
3919 * The AP binding flow is handled as part of the start_ap flow
5023d966 3920 * (in bss_info_changed), similarly for IBSS.
8ca151b5
JB
3921 */
3922 ret = 0;
b08c1d97 3923 goto out;
8ca151b5 3924 case NL80211_IFTYPE_STATION:
19125cb0 3925 mvmvif->csa_bcn_pending = false;
2533edce 3926 break;
8ca151b5 3927 case NL80211_IFTYPE_MONITOR:
2533edce
LC
3928 /* always disable PS when a monitor interface is active */
3929 mvmvif->ps_disabled = true;
8ca151b5
JB
3930 break;
3931 default:
3932 ret = -EINVAL;
b08c1d97 3933 goto out;
8ca151b5
JB
3934 }
3935
3936 ret = iwl_mvm_binding_add_vif(mvm, vif);
3937 if (ret)
b08c1d97 3938 goto out;
8ca151b5
JB
3939
3940 /*
92d85562
AB
3941 * Power state must be updated before quotas,
3942 * otherwise fw will complain.
3943 */
999609f1 3944 iwl_mvm_power_update_mac(mvm);
92d85562
AB
3945
3946 /* Setting the quota at this stage is only required for monitor
8ca151b5
JB
3947 * interfaces. For the other types, the bss_info changed flow
3948 * will handle quota settings.
3949 */
3950 if (vif->type == NL80211_IFTYPE_MONITOR) {
1e1391ca 3951 mvmvif->monitor_active = true;
7754ae79 3952 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5
JB
3953 if (ret)
3954 goto out_remove_binding;
0e39eb03
CRI
3955
3956 ret = iwl_mvm_add_snif_sta(mvm, vif);
3957 if (ret)
3958 goto out_remove_binding;
3959
8ca151b5
JB
3960 }
3961
bd3398e2 3962 /* Handle binding during CSA */
a57c688d 3963 if (vif->type == NL80211_IFTYPE_AP) {
7754ae79 3964 iwl_mvm_update_quotas(mvm, false, NULL);
3dfd3a97 3965 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
bd3398e2
AO
3966 }
3967
4741dd04 3968 if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
74a10252 3969 mvmvif->csa_bcn_pending = true;
686e7fe1 3970
74a10252
SS
3971 if (!fw_has_capa(&mvm->fw->ucode_capa,
3972 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
3973 u32 duration = 3 * vif->bss_conf.beacon_int;
686e7fe1 3974
686e7fe1 3975
74a10252
SS
3976 /* iwl_mvm_protect_session() reads directly from the
3977 * device (the system time), so make sure it is
3978 * available.
3979 */
3980 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
3981 if (ret)
3982 goto out_remove_binding;
3983
3984 /* Protect the session to make sure we hear the first
3985 * beacon on the new channel.
3986 */
3987 iwl_mvm_protect_session(mvm, vif, duration, duration,
3988 vif->bss_conf.beacon_int / 2,
3989 true);
3990
3991 iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
3992 }
686e7fe1 3993
7754ae79 3994 iwl_mvm_update_quotas(mvm, false, NULL);
0ce04ce7
LC
3995 }
3996
b08c1d97 3997 goto out;
8ca151b5 3998
b08c1d97 3999out_remove_binding:
8ca151b5 4000 iwl_mvm_binding_remove_vif(mvm, vif);
999609f1 4001 iwl_mvm_power_update_mac(mvm);
b08c1d97 4002out:
8ca151b5
JB
4003 if (ret)
4004 mvmvif->phy_ctxt = NULL;
4005 return ret;
4006}
b08c1d97
LC
4007static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4008 struct ieee80211_vif *vif,
4009 struct ieee80211_chanctx_conf *ctx)
8ca151b5
JB
4010{
4011 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
b08c1d97 4012 int ret;
8ca151b5
JB
4013
4014 mutex_lock(&mvm->mutex);
f0c97783 4015 ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
b08c1d97
LC
4016 mutex_unlock(&mvm->mutex);
4017
4018 return ret;
4019}
4020
4021static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4022 struct ieee80211_vif *vif,
f0c97783
LC
4023 struct ieee80211_chanctx_conf *ctx,
4024 bool switching_chanctx)
b08c1d97
LC
4025{
4026 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
f0c97783 4027 struct ieee80211_vif *disabled_vif = NULL;
b08c1d97
LC
4028
4029 lockdep_assert_held(&mvm->mutex);
8ca151b5
JB
4030
4031 iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4032
8ca151b5 4033 switch (vif->type) {
5023d966 4034 case NL80211_IFTYPE_ADHOC:
b08c1d97 4035 goto out;
8ca151b5 4036 case NL80211_IFTYPE_MONITOR:
1e1391ca 4037 mvmvif->monitor_active = false;
2533edce 4038 mvmvif->ps_disabled = false;
0e39eb03 4039 iwl_mvm_rm_snif_sta(mvm, vif);
8ca151b5 4040 break;
bd3398e2
AO
4041 case NL80211_IFTYPE_AP:
4042 /* This part is triggered only during CSA */
4741dd04 4043 if (!switching_chanctx || !mvmvif->ap_ibss_active)
b08c1d97 4044 goto out;
bd3398e2 4045
7ef0aab6
AO
4046 mvmvif->csa_countdown = false;
4047
003e5236
AO
4048 /* Set CS bit on all the stations */
4049 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4050
4051 /* Save blocked iface, the timeout is set on the next beacon */
4052 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4053
bd3398e2 4054 mvmvif->ap_ibss_active = false;
f0c97783
LC
4055 break;
4056 case NL80211_IFTYPE_STATION:
4057 if (!switching_chanctx)
4058 break;
4059
4060 disabled_vif = vif;
4061
74a10252
SS
4062 if (!fw_has_capa(&mvm->fw->ucode_capa,
4063 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4064 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
f0c97783 4065 break;
8ca151b5
JB
4066 default:
4067 break;
4068 }
4069
7754ae79 4070 iwl_mvm_update_quotas(mvm, false, disabled_vif);
1e1391ca 4071 iwl_mvm_binding_remove_vif(mvm, vif);
1c2abf72 4072
b08c1d97 4073out:
a11e144e 4074 mvmvif->phy_ctxt = NULL;
999609f1 4075 iwl_mvm_power_update_mac(mvm);
b08c1d97
LC
4076}
4077
4078static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4079 struct ieee80211_vif *vif,
4080 struct ieee80211_chanctx_conf *ctx)
4081{
4082 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4083
4084 mutex_lock(&mvm->mutex);
f0c97783 4085 __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
8ca151b5
JB
4086 mutex_unlock(&mvm->mutex);
4087}
4088
50cc9574
LC
4089static int
4090iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4091 struct ieee80211_vif_chanctx_switch *vifs)
b08c1d97 4092{
b08c1d97
LC
4093 int ret;
4094
b08c1d97 4095 mutex_lock(&mvm->mutex);
f0c97783 4096 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
b08c1d97
LC
4097 __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4098
4099 ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4100 if (ret) {
4101 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4102 goto out_reassign;
4103 }
4104
f0c97783
LC
4105 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4106 true);
b08c1d97
LC
4107 if (ret) {
4108 IWL_ERR(mvm,
4109 "failed to assign new_ctx during channel switch\n");
4110 goto out_remove;
4111 }
4112
f697267f
AN
4113 /* we don't support TDLS during DCM - can be caused by channel switch */
4114 if (iwl_mvm_phy_ctx_count(mvm) > 1)
4115 iwl_mvm_teardown_tdls_peers(mvm);
4116
b08c1d97
LC
4117 goto out;
4118
4119out_remove:
4120 __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4121
4122out_reassign:
6fd1fb63 4123 if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
b08c1d97
LC
4124 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4125 goto out_restart;
4126 }
4127
6fd1fb63
LC
4128 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4129 true)) {
b08c1d97
LC
4130 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4131 goto out_restart;
4132 }
4133
4134 goto out;
4135
4136out_restart:
4137 /* things keep failing, better restart the hw */
4138 iwl_mvm_nic_restart(mvm, false);
4139
4140out:
4141 mutex_unlock(&mvm->mutex);
50cc9574
LC
4142
4143 return ret;
4144}
4145
48a256e8
LC
4146static int
4147iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4148 struct ieee80211_vif_chanctx_switch *vifs)
4149{
4150 int ret;
4151
4152 mutex_lock(&mvm->mutex);
4153 __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4154
4155 ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4156 true);
4157 if (ret) {
4158 IWL_ERR(mvm,
4159 "failed to assign new_ctx during channel switch\n");
4160 goto out_reassign;
4161 }
4162
4163 goto out;
4164
4165out_reassign:
4166 if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4167 true)) {
4168 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4169 goto out_restart;
4170 }
4171
4172 goto out;
4173
4174out_restart:
4175 /* things keep failing, better restart the hw */
4176 iwl_mvm_nic_restart(mvm, false);
4177
4178out:
4179 mutex_unlock(&mvm->mutex);
4180
4181 return ret;
4182}
4183
50cc9574
LC
4184static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4185 struct ieee80211_vif_chanctx_switch *vifs,
4186 int n_vifs,
4187 enum ieee80211_chanctx_switch_mode mode)
4188{
4189 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4190 int ret;
4191
4192 /* we only support a single-vif right now */
4193 if (n_vifs > 1)
4194 return -EOPNOTSUPP;
4195
4196 switch (mode) {
4197 case CHANCTX_SWMODE_SWAP_CONTEXTS:
4198 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4199 break;
4200 case CHANCTX_SWMODE_REASSIGN_VIF:
48a256e8 4201 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
50cc9574
LC
4202 break;
4203 default:
4204 ret = -EOPNOTSUPP;
4205 break;
4206 }
4207
b08c1d97
LC
4208 return ret;
4209}
4210
2f0282db
JB
4211static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
4212{
4213 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4214
4215 return mvm->ibss_manager;
4216}
4217
8ca151b5
JB
4218static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4219 struct ieee80211_sta *sta,
4220 bool set)
4221{
4222 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 4223 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
4224
4225 if (!mvm_sta || !mvm_sta->vif) {
4226 IWL_ERR(mvm, "Station is not associated to a vif\n");
4227 return -EINVAL;
4228 }
4229
4230 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4231}
4232
507cadf2
DS
4233#ifdef CONFIG_NL80211_TESTMODE
4234static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4235 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4236 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
f6c6ad42 4237 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
507cadf2
DS
4238};
4239
4240static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4241 struct ieee80211_vif *vif,
4242 void *data, int len)
4243{
4244 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4245 int err;
4246 u32 noa_duration;
4247
fceb6435
JB
4248 err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy,
4249 NULL);
507cadf2
DS
4250 if (err)
4251 return err;
4252
4253 if (!tb[IWL_MVM_TM_ATTR_CMD])
4254 return -EINVAL;
4255
4256 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4257 case IWL_MVM_TM_CMD_SET_NOA:
4258 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4259 !vif->bss_conf.enable_beacon ||
4260 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4261 return -EINVAL;
4262
4263 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4264 if (noa_duration >= vif->bss_conf.beacon_int)
4265 return -EINVAL;
4266
4267 mvm->noa_duration = noa_duration;
4268 mvm->noa_vif = vif;
4269
22b21041 4270 return iwl_mvm_update_quotas(mvm, true, NULL);
f6c6ad42
JB
4271 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4272 /* must be associated client vif - ignore authorized */
4273 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4274 !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4275 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4276 return -EINVAL;
4277
4278 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
a1022927
EG
4279 return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4280 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
507cadf2
DS
4281 }
4282
4283 return -EOPNOTSUPP;
4284}
4285
4286static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4287 struct ieee80211_vif *vif,
4288 void *data, int len)
4289{
4290 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4291 int err;
4292
4293 mutex_lock(&mvm->mutex);
4294 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4295 mutex_unlock(&mvm->mutex);
4296
4297 return err;
4298}
4299#endif
4300
622e3f9b
LC
4301static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4302 struct ieee80211_vif *vif,
4303 struct ieee80211_channel_switch *chsw)
4304{
4305 /* By implementing this operation, we prevent mac80211 from
4306 * starting its own channel switch timer, so that we can call
4307 * ieee80211_chswitch_done() ourselves at the right time
4308 * (which is when the absence time event starts).
4309 */
4310
4311 IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4312 "dummy channel switch op\n");
4313}
4314
74a10252
SS
4315static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
4316 struct ieee80211_vif *vif,
4317 struct ieee80211_channel_switch *chsw)
4318{
4319 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4320 struct iwl_chan_switch_te_cmd cmd = {
4321 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4322 mvmvif->color)),
4323 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4324 .tsf = cpu_to_le32(chsw->timestamp),
4325 .cs_count = chsw->count,
4326 };
4327
4328 lockdep_assert_held(&mvm->mutex);
4329
4330 return iwl_mvm_send_cmd_pdu(mvm,
4331 WIDE_ID(MAC_CONF_GROUP,
4332 CHANNEL_SWITCH_TIME_EVENT_CMD),
4333 0, sizeof(cmd), &cmd);
4334}
4335
f028905c
LC
4336static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4337 struct ieee80211_vif *vif,
4338 struct ieee80211_channel_switch *chsw)
bd3398e2
AO
4339{
4340 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
664322fa 4341 struct ieee80211_vif *csa_vif;
f6c34820 4342 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
dc88b4ba 4343 u32 apply_time;
f028905c 4344 int ret;
bd3398e2
AO
4345
4346 mutex_lock(&mvm->mutex);
664322fa 4347
81d62d5a
JB
4348 mvmvif->csa_failed = false;
4349
6b20d774 4350 IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
f028905c 4351 chsw->chandef.center_freq1);
6b20d774 4352
7174beb6
JB
4353 iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
4354 ieee80211_vif_to_wdev(vif),
4355 FW_DBG_TRIGGER_CHANNEL_SWITCH);
f35d9c55 4356
6b20d774
LC
4357 switch (vif->type) {
4358 case NL80211_IFTYPE_AP:
4359 csa_vif =
4360 rcu_dereference_protected(mvm->csa_vif,
4361 lockdep_is_held(&mvm->mutex));
4362 if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4363 "Another CSA is already in progress")) {
4364 ret = -EBUSY;
4365 goto out_unlock;
4366 }
4367
d3a108a4
AO
4368 /* we still didn't unblock tx. prevent new CS meanwhile */
4369 if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4370 lockdep_is_held(&mvm->mutex))) {
4371 ret = -EBUSY;
4372 goto out_unlock;
4373 }
4374
6b20d774 4375 rcu_assign_pointer(mvm->csa_vif, vif);
7ef0aab6 4376
7ef0aab6
AO
4377 if (WARN_ONCE(mvmvif->csa_countdown,
4378 "Previous CSA countdown didn't complete")) {
4379 ret = -EBUSY;
4380 goto out_unlock;
4381 }
4382
d3a108a4
AO
4383 mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4384
6b20d774 4385 break;
dc88b4ba 4386 case NL80211_IFTYPE_STATION:
4500e133
LC
4387 /* Schedule the time event to a bit before beacon 1,
4388 * to make sure we're in the new channel when the
f5d8f50f
IP
4389 * GO/AP arrives. In case count <= 1 immediately schedule the
4390 * TE (this might result with some packet loss or connection
4391 * loss).
4500e133 4392 */
f5d8f50f
IP
4393 if (chsw->count <= 1)
4394 apply_time = 0;
4395 else
4396 apply_time = chsw->device_timestamp +
4397 ((vif->bss_conf.beacon_int * (chsw->count - 1) -
4398 IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
dc88b4ba
LC
4399
4400 if (chsw->block_tx)
4401 iwl_mvm_csa_client_absent(mvm, vif);
4402
c6e0a3e0
LC
4403 if (mvmvif->bf_data.bf_enabled) {
4404 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4405 if (ret)
4406 goto out_unlock;
4407 }
4408
74a10252
SS
4409 if (fw_has_capa(&mvm->fw->ucode_capa,
4410 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4411 iwl_mvm_schedule_client_csa(mvm, vif, chsw);
4412 else
4413 iwl_mvm_schedule_csa_period(mvm, vif,
4414 vif->bss_conf.beacon_int,
4415 apply_time);
dc88b4ba 4416 break;
6b20d774
LC
4417 default:
4418 break;
4419 }
bd3398e2 4420
f6c34820
LC
4421 mvmvif->ps_disabled = true;
4422
4423 ret = iwl_mvm_power_update_ps(mvm);
4424 if (ret)
4425 goto out_unlock;
f028905c 4426
e198f5e7
AN
4427 /* we won't be on this channel any longer */
4428 iwl_mvm_teardown_tdls_peers(mvm);
4429
bd3398e2
AO
4430out_unlock:
4431 mutex_unlock(&mvm->mutex);
f028905c
LC
4432
4433 return ret;
bd3398e2
AO
4434}
4435
f6c34820
LC
4436static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
4437 struct ieee80211_vif *vif)
4438{
4439 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4440 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4441 int ret;
4442
4443 mutex_lock(&mvm->mutex);
4444
81d62d5a
JB
4445 if (mvmvif->csa_failed) {
4446 mvmvif->csa_failed = false;
4447 ret = -EIO;
4448 goto out_unlock;
4449 }
4450
a57c688d
LC
4451 if (vif->type == NL80211_IFTYPE_STATION) {
4452 struct iwl_mvm_sta *mvmsta;
4453
19125cb0 4454 mvmvif->csa_bcn_pending = false;
a57c688d
LC
4455 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
4456 mvmvif->ap_sta_id);
4457
4458 if (WARN_ON(!mvmsta)) {
4459 ret = -EIO;
4460 goto out_unlock;
4461 }
4462
4463 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
4464
4465 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
c6e0a3e0
LC
4466
4467 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4468 if (ret)
4469 goto out_unlock;
686e7fe1
LC
4470
4471 iwl_mvm_stop_session_protection(mvm, vif);
a57c688d
LC
4472 }
4473
f6c34820
LC
4474 mvmvif->ps_disabled = false;
4475
4476 ret = iwl_mvm_power_update_ps(mvm);
4477
a57c688d 4478out_unlock:
f6c34820
LC
4479 mutex_unlock(&mvm->mutex);
4480
4481 return ret;
4482}
4483
6110d9e5
DS
4484static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
4485{
435d0827
SS
4486 int i;
4487
06195639 4488 if (!iwl_mvm_has_new_tx_api(mvm)) {
309c4848
LC
4489 if (drop) {
4490 mutex_lock(&mvm->mutex);
6110d9e5 4491 iwl_mvm_flush_tx_path(mvm,
435d0827 4492 iwl_mvm_flushable_queues(mvm) & queues, 0);
309c4848
LC
4493 mutex_unlock(&mvm->mutex);
4494 } else {
06195639 4495 iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
309c4848 4496 }
435d0827
SS
4497 return;
4498 }
6110d9e5 4499
435d0827
SS
4500 mutex_lock(&mvm->mutex);
4501 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4502 struct ieee80211_sta *sta;
6110d9e5 4503
435d0827
SS
4504 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4505 lockdep_is_held(&mvm->mutex));
4506 if (IS_ERR_OR_NULL(sta))
4507 continue;
6110d9e5 4508
06195639
SS
4509 if (drop)
4510 iwl_mvm_flush_sta_tids(mvm, i, 0xFF, 0);
4511 else
4512 iwl_mvm_wait_sta_queues_empty(mvm,
4513 iwl_mvm_sta_from_mac80211(sta));
6110d9e5 4514 }
435d0827 4515 mutex_unlock(&mvm->mutex);
6110d9e5
DS
4516}
4517
c5b0e7c0
EG
4518static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4519 struct ieee80211_vif *vif, u32 queues, bool drop)
4520{
4521 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4522 struct iwl_mvm_vif *mvmvif;
4523 struct iwl_mvm_sta *mvmsta;
a0f6bf2a
AN
4524 struct ieee80211_sta *sta;
4525 int i;
4526 u32 msk = 0;
c5b0e7c0 4527
6110d9e5
DS
4528 if (!vif) {
4529 iwl_mvm_flush_no_vif(mvm, queues, drop);
4530 return;
4531 }
4532
4533 if (vif->type != NL80211_IFTYPE_STATION)
c5b0e7c0
EG
4534 return;
4535
24afba76 4536 /* Make sure we're done with the deferred traffic before flushing */
c8f54701 4537 flush_work(&mvm->add_stream_wk);
24afba76 4538
c5b0e7c0
EG
4539 mutex_lock(&mvm->mutex);
4540 mvmvif = iwl_mvm_vif_from_mac80211(vif);
c5b0e7c0 4541
a0f6bf2a 4542 /* flush the AP-station and all TDLS peers */
0ae98812 4543 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
a0f6bf2a
AN
4544 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4545 lockdep_is_held(&mvm->mutex));
4546 if (IS_ERR_OR_NULL(sta))
4547 continue;
4548
4549 mvmsta = iwl_mvm_sta_from_mac80211(sta);
4550 if (mvmsta->vif != vif)
4551 continue;
4552
4553 /* make sure only TDLS peers or the AP are flushed */
4554 WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4555
d49394a1
SS
4556 if (drop) {
4557 if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4558 IWL_ERR(mvm, "flush request fail\n");
4559 } else {
4560 msk |= mvmsta->tfd_queue_msk;
d6d517b7
SS
4561 if (iwl_mvm_has_new_tx_api(mvm))
4562 iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
d49394a1 4563 }
480acbce 4564 }
c5b0e7c0 4565
d49394a1 4566 mutex_unlock(&mvm->mutex);
4e6c48e0 4567
d49394a1
SS
4568 /* this can take a while, and we may need/want other operations
4569 * to succeed while doing this, so do it without the mutex held
4570 */
d6d517b7 4571 if (!drop && !iwl_mvm_has_new_tx_api(mvm))
a1a57877 4572 iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
c5b0e7c0
EG
4573}
4574
91a8bcde
JB
4575static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4576 struct survey_info *survey)
4577{
4578 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4579 int ret;
4580
4581 memset(survey, 0, sizeof(*survey));
4582
4583 /* only support global statistics right now */
4584 if (idx != 0)
4585 return -ENOENT;
4586
280a3efa
JB
4587 if (!fw_has_capa(&mvm->fw->ucode_capa,
4588 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
91a8bcde
JB
4589 return -ENOENT;
4590
4591 mutex_lock(&mvm->mutex);
4592
aab6930d 4593 if (iwl_mvm_firmware_running(mvm)) {
33cef925 4594 ret = iwl_mvm_request_statistics(mvm, false);
91a8bcde
JB
4595 if (ret)
4596 goto out;
4597 }
4598
4599 survey->filled = SURVEY_INFO_TIME |
4600 SURVEY_INFO_TIME_RX |
4601 SURVEY_INFO_TIME_TX |
4602 SURVEY_INFO_TIME_SCAN;
4603 survey->time = mvm->accu_radio_stats.on_time_rf +
4604 mvm->radio_stats.on_time_rf;
4605 do_div(survey->time, USEC_PER_MSEC);
4606
4607 survey->time_rx = mvm->accu_radio_stats.rx_time +
4608 mvm->radio_stats.rx_time;
4609 do_div(survey->time_rx, USEC_PER_MSEC);
4610
4611 survey->time_tx = mvm->accu_radio_stats.tx_time +
4612 mvm->radio_stats.tx_time;
4613 do_div(survey->time_tx, USEC_PER_MSEC);
4614
4615 survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4616 mvm->radio_stats.on_time_scan;
4617 do_div(survey->time_scan, USEC_PER_MSEC);
4618
10a7c028 4619 ret = 0;
91a8bcde
JB
4620 out:
4621 mutex_unlock(&mvm->mutex);
4622 return ret;
4623}
4624
33cef925
JB
4625static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4626 struct ieee80211_vif *vif,
4627 struct ieee80211_sta *sta,
4628 struct station_info *sinfo)
4629{
4630 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4631 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4632 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4633
988b5968
SS
4634 if (mvmsta->avg_energy) {
4635 sinfo->signal_avg = mvmsta->avg_energy;
22d0d2fa 4636 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
988b5968
SS
4637 }
4638
33cef925
JB
4639 /* if beacon filtering isn't on mac80211 does it anyway */
4640 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4641 return;
4642
4643 if (!vif->bss_conf.assoc)
4644 return;
4645
4646 mutex_lock(&mvm->mutex);
4647
4648 if (mvmvif->ap_sta_id != mvmsta->sta_id)
4649 goto unlock;
4650
4651 if (iwl_mvm_request_statistics(mvm, false))
4652 goto unlock;
4653
4654 sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4655 mvmvif->beacon_stats.accu_num_beacons;
22d0d2fa 4656 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
33cef925
JB
4657 if (mvmvif->beacon_stats.avg_signal) {
4658 /* firmware only reports a value after RXing a few beacons */
4659 sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
22d0d2fa 4660 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
33cef925
JB
4661 }
4662 unlock:
4663 mutex_unlock(&mvm->mutex);
4664}
4665
4203263d
EG
4666static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4667 struct ieee80211_vif *vif,
4668 const struct ieee80211_event *event)
d42f5350 4669{
7174beb6
JB
4670#define CHECK_MLME_TRIGGER(_cnt, _fmt...) \
4671 do { \
4672 if ((trig_mlme->_cnt) && --(trig_mlme->_cnt)) \
4673 break; \
4674 iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt); \
d42f5350
EG
4675 } while (0)
4676
d42f5350
EG
4677 struct iwl_fw_dbg_trigger_tlv *trig;
4678 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
d42f5350 4679
6c042d75
SS
4680 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
4681 FW_DBG_TRIGGER_MLME);
4682 if (!trig)
d42f5350
EG
4683 return;
4684
d42f5350 4685 trig_mlme = (void *)trig->data;
d42f5350 4686
d42f5350
EG
4687 if (event->u.mlme.data == ASSOC_EVENT) {
4688 if (event->u.mlme.status == MLME_DENIED)
4c324a51 4689 CHECK_MLME_TRIGGER(stop_assoc_denied,
d42f5350
EG
4690 "DENIED ASSOC: reason %d",
4691 event->u.mlme.reason);
4692 else if (event->u.mlme.status == MLME_TIMEOUT)
4c324a51 4693 CHECK_MLME_TRIGGER(stop_assoc_timeout,
d42f5350
EG
4694 "ASSOC TIMEOUT");
4695 } else if (event->u.mlme.data == AUTH_EVENT) {
4696 if (event->u.mlme.status == MLME_DENIED)
4c324a51 4697 CHECK_MLME_TRIGGER(stop_auth_denied,
d42f5350
EG
4698 "DENIED AUTH: reason %d",
4699 event->u.mlme.reason);
4700 else if (event->u.mlme.status == MLME_TIMEOUT)
4c324a51 4701 CHECK_MLME_TRIGGER(stop_auth_timeout,
d42f5350
EG
4702 "AUTH TIMEOUT");
4703 } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
4c324a51 4704 CHECK_MLME_TRIGGER(stop_rx_deauth,
d42f5350
EG
4705 "DEAUTH RX %d", event->u.mlme.reason);
4706 } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
4c324a51 4707 CHECK_MLME_TRIGGER(stop_tx_deauth,
d42f5350
EG
4708 "DEAUTH TX %d", event->u.mlme.reason);
4709 }
4710#undef CHECK_MLME_TRIGGER
4711}
4712
4203263d
EG
4713static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4714 struct ieee80211_vif *vif,
4715 const struct ieee80211_event *event)
4716{
4717 struct iwl_fw_dbg_trigger_tlv *trig;
4718 struct iwl_fw_dbg_trigger_ba *ba_trig;
4719
6c042d75
SS
4720 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
4721 FW_DBG_TRIGGER_BA);
4722 if (!trig)
4203263d
EG
4723 return;
4724
4203263d 4725 ba_trig = (void *)trig->data;
4203263d
EG
4726
4727 if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4728 return;
4729
7174beb6
JB
4730 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4731 "BAR received from %pM, tid %d, ssn %d",
4732 event->u.ba.sta->addr, event->u.ba.tid,
4733 event->u.ba.ssn);
4203263d
EG
4734}
4735
4203263d
EG
4736static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4737 struct ieee80211_vif *vif,
4738 const struct ieee80211_event *event)
4739{
4740 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4741
4742 switch (event->type) {
4743 case MLME_EVENT:
4744 iwl_mvm_event_mlme_callback(mvm, vif, event);
4745 break;
4746 case BAR_RX_EVENT:
4747 iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4748 break;
4749 case BA_FRAME_TIMEOUT:
528a542a
EG
4750 iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
4751 event->u.ba.tid);
4203263d
EG
4752 break;
4753 default:
4754 break;
4755 }
4756}
4757
d0ff5d22
SS
4758void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4759 struct iwl_mvm_internal_rxq_notif *notif,
4760 u32 size)
0636b938 4761{
0636b938
SS
4762 u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
4763 int ret;
4764
4765 lockdep_assert_held(&mvm->mutex);
4766
cb8550e1 4767 if (!iwl_mvm_has_new_rx_api(mvm))
0636b938
SS
4768 return;
4769
d0ff5d22
SS
4770 notif->cookie = mvm->queue_sync_cookie;
4771
4772 if (notif->sync)
4773 atomic_set(&mvm->queue_sync_counter,
4774 mvm->trans->num_rx_queues);
0636b938 4775
d0ff5d22 4776 ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif, size);
0636b938
SS
4777 if (ret) {
4778 IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
4779 goto out;
4780 }
d0ff5d22 4781
6ad04359 4782 if (notif->sync) {
3a732c65 4783 ret = wait_event_timeout(mvm->rx_sync_waitq,
6ad04359
JB
4784 atomic_read(&mvm->queue_sync_counter) == 0 ||
4785 iwl_mvm_is_radio_killed(mvm),
d0ff5d22 4786 HZ);
6ad04359
JB
4787 WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
4788 }
0636b938
SS
4789
4790out:
4791 atomic_set(&mvm->queue_sync_counter, 0);
4792 mvm->queue_sync_cookie++;
4793}
4794
4795static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
4796{
4797 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
d0ff5d22
SS
4798 struct iwl_mvm_internal_rxq_notif data = {
4799 .type = IWL_MVM_RXQ_EMPTY,
4800 .sync = 1,
4801 };
0636b938
SS
4802
4803 mutex_lock(&mvm->mutex);
d0ff5d22 4804 iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
0636b938
SS
4805 mutex_unlock(&mvm->mutex);
4806}
4807
438af969
SS
4808static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
4809{
4810 u8 protocol = ip_hdr(skb)->protocol;
4811
4812 if (!IS_ENABLED(CONFIG_INET))
4813 return false;
4814
4815 return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
4816}
4817
4818static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
4819 struct sk_buff *head,
4820 struct sk_buff *skb)
4821{
4822 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4823
4824 /* For now don't aggregate IPv6 in AMSDU */
4825 if (skb->protocol != htons(ETH_P_IP))
4826 return false;
4827
4828 if (!iwl_mvm_is_csum_supported(mvm))
4829 return true;
4830
4831 return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
4832}
4833
e5209263 4834const struct ieee80211_ops iwl_mvm_hw_ops = {
8ca151b5 4835 .tx = iwl_mvm_mac_tx,
cfbc6c4c 4836 .wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
8ca151b5
JB
4837 .ampdu_action = iwl_mvm_mac_ampdu_action,
4838 .start = iwl_mvm_mac_start,
cf2c92d8 4839 .reconfig_complete = iwl_mvm_mac_reconfig_complete,
8ca151b5
JB
4840 .stop = iwl_mvm_mac_stop,
4841 .add_interface = iwl_mvm_mac_add_interface,
4842 .remove_interface = iwl_mvm_mac_remove_interface,
4843 .config = iwl_mvm_mac_config,
e59647ea 4844 .prepare_multicast = iwl_mvm_prepare_multicast,
8ca151b5 4845 .configure_filter = iwl_mvm_configure_filter,
effd1929 4846 .config_iface_filter = iwl_mvm_config_iface_filter,
8ca151b5
JB
4847 .bss_info_changed = iwl_mvm_bss_info_changed,
4848 .hw_scan = iwl_mvm_mac_hw_scan,
4849 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1ddbbb0c 4850 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
8ca151b5
JB
4851 .sta_state = iwl_mvm_mac_sta_state,
4852 .sta_notify = iwl_mvm_mac_sta_notify,
4853 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3e56eadf 4854 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
8ca151b5 4855 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1f3b0ff8 4856 .sta_rc_update = iwl_mvm_sta_rc_update,
8ca151b5
JB
4857 .conf_tx = iwl_mvm_mac_conf_tx,
4858 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
07ecd897 4859 .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
c5b0e7c0 4860 .flush = iwl_mvm_mac_flush,
35a000b7
DS
4861 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
4862 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
8ca151b5
JB
4863 .set_key = iwl_mvm_mac_set_key,
4864 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
4865 .remain_on_channel = iwl_mvm_roc,
4866 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
8ca151b5
JB
4867 .add_chanctx = iwl_mvm_add_chanctx,
4868 .remove_chanctx = iwl_mvm_remove_chanctx,
4869 .change_chanctx = iwl_mvm_change_chanctx,
4870 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
4871 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
b08c1d97 4872 .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
8ca151b5 4873
5023d966
JB
4874 .start_ap = iwl_mvm_start_ap_ibss,
4875 .stop_ap = iwl_mvm_stop_ap_ibss,
4876 .join_ibss = iwl_mvm_start_ap_ibss,
4877 .leave_ibss = iwl_mvm_stop_ap_ibss,
8ca151b5 4878
2f0282db
JB
4879 .tx_last_beacon = iwl_mvm_tx_last_beacon,
4880
8ca151b5
JB
4881 .set_tim = iwl_mvm_set_tim,
4882
622e3f9b 4883 .channel_switch = iwl_mvm_channel_switch,
f028905c 4884 .pre_channel_switch = iwl_mvm_pre_channel_switch,
f6c34820 4885 .post_channel_switch = iwl_mvm_post_channel_switch,
bd3398e2 4886
1d3c3f63
AN
4887 .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
4888 .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
4889 .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
4890
d42f5350
EG
4891 .event_callback = iwl_mvm_mac_event_callback,
4892
0636b938
SS
4893 .sync_rx_queues = iwl_mvm_sync_rx_queues,
4894
507cadf2
DS
4895 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
4896
8ca151b5
JB
4897#ifdef CONFIG_PM_SLEEP
4898 /* look at d3.c */
4899 .suspend = iwl_mvm_suspend,
4900 .resume = iwl_mvm_resume,
4901 .set_wakeup = iwl_mvm_set_wakeup,
4902 .set_rekey_data = iwl_mvm_set_rekey_data,
4903#if IS_ENABLED(CONFIG_IPV6)
4904 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
4905#endif
4906 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
4907#endif
91a8bcde 4908 .get_survey = iwl_mvm_mac_get_survey,
33cef925 4909 .sta_statistics = iwl_mvm_mac_sta_statistics,
438af969 4910 .can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
177a11cf
GG
4911#ifdef CONFIG_IWLWIFI_DEBUGFS
4912 .sta_add_debugfs = iwl_mvm_sta_add_debugfs,
4913#endif
8ca151b5 4914};