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