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