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