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