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