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