GNU Linux-libre 4.14.303-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         int ret;
1612
1613         lockdep_assert_held(&mvm->mutex);
1614
1615         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1616                 return;
1617
1618         ieee80211_iterate_active_interfaces_atomic(
1619                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1620                 iwl_mvm_mc_iface_iterator, &iter_data);
1621
1622         /*
1623          * Send a (synchronous) ech command so that we wait for the
1624          * multiple asynchronous MCAST_FILTER_CMD commands sent by
1625          * the interface iterator. Otherwise, we might get here over
1626          * and over again (by userspace just sending a lot of these)
1627          * and the CPU can send them faster than the firmware can
1628          * process them.
1629          * Note that the CPU is still faster - but with this we'll
1630          * actually send fewer commands overall because the CPU will
1631          * not schedule the work in mac80211 as frequently if it's
1632          * still running when rescheduled (possibly multiple times).
1633          */
1634         ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
1635         if (ret)
1636                 IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
1637 }
1638
1639 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1640                                      struct netdev_hw_addr_list *mc_list)
1641 {
1642         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1643         struct iwl_mcast_filter_cmd *cmd;
1644         struct netdev_hw_addr *addr;
1645         int addr_count;
1646         bool pass_all;
1647         int len;
1648
1649         addr_count = netdev_hw_addr_list_count(mc_list);
1650         pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1651                    IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1652         if (pass_all)
1653                 addr_count = 0;
1654
1655         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1656         cmd = kzalloc(len, GFP_ATOMIC);
1657         if (!cmd)
1658                 return 0;
1659
1660         if (pass_all) {
1661                 cmd->pass_all = 1;
1662                 return (u64)(unsigned long)cmd;
1663         }
1664
1665         netdev_hw_addr_list_for_each(addr, mc_list) {
1666                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1667                                    cmd->count, addr->addr);
1668                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1669                        addr->addr, ETH_ALEN);
1670                 cmd->count++;
1671         }
1672
1673         return (u64)(unsigned long)cmd;
1674 }
1675
1676 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1677                                      unsigned int changed_flags,
1678                                      unsigned int *total_flags,
1679                                      u64 multicast)
1680 {
1681         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1682         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1683
1684         mutex_lock(&mvm->mutex);
1685
1686         /* replace previous configuration */
1687         kfree(mvm->mcast_filter_cmd);
1688         mvm->mcast_filter_cmd = cmd;
1689
1690         if (!cmd)
1691                 goto out;
1692
1693         if (changed_flags & FIF_ALLMULTI)
1694                 cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
1695
1696         if (cmd->pass_all)
1697                 cmd->count = 0;
1698
1699         iwl_mvm_recalc_multicast(mvm);
1700 out:
1701         mutex_unlock(&mvm->mutex);
1702         *total_flags = 0;
1703 }
1704
1705 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1706                                         struct ieee80211_vif *vif,
1707                                         unsigned int filter_flags,
1708                                         unsigned int changed_flags)
1709 {
1710         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1711
1712         /* We support only filter for probe requests */
1713         if (!(changed_flags & FIF_PROBE_REQ))
1714                 return;
1715
1716         /* Supported only for p2p client interfaces */
1717         if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1718             !vif->p2p)
1719                 return;
1720
1721         mutex_lock(&mvm->mutex);
1722         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1723         mutex_unlock(&mvm->mutex);
1724 }
1725
1726 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1727 struct iwl_bcast_iter_data {
1728         struct iwl_mvm *mvm;
1729         struct iwl_bcast_filter_cmd *cmd;
1730         u8 current_filter;
1731 };
1732
1733 static void
1734 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1735                          const struct iwl_fw_bcast_filter *in_filter,
1736                          struct iwl_fw_bcast_filter *out_filter)
1737 {
1738         struct iwl_fw_bcast_filter_attr *attr;
1739         int i;
1740
1741         memcpy(out_filter, in_filter, sizeof(*out_filter));
1742
1743         for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1744                 attr = &out_filter->attrs[i];
1745
1746                 if (!attr->mask)
1747                         break;
1748
1749                 switch (attr->reserved1) {
1750                 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1751                         if (vif->bss_conf.arp_addr_cnt != 1) {
1752                                 attr->mask = 0;
1753                                 continue;
1754                         }
1755
1756                         attr->val = vif->bss_conf.arp_addr_list[0];
1757                         break;
1758                 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1759                         attr->val = *(__be32 *)&vif->addr[2];
1760                         break;
1761                 default:
1762                         break;
1763                 }
1764                 attr->reserved1 = 0;
1765                 out_filter->num_attrs++;
1766         }
1767 }
1768
1769 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1770                                           struct ieee80211_vif *vif)
1771 {
1772         struct iwl_bcast_iter_data *data = _data;
1773         struct iwl_mvm *mvm = data->mvm;
1774         struct iwl_bcast_filter_cmd *cmd = data->cmd;
1775         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1776         struct iwl_fw_bcast_mac *bcast_mac;
1777         int i;
1778
1779         if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1780                 return;
1781
1782         bcast_mac = &cmd->macs[mvmvif->id];
1783
1784         /*
1785          * enable filtering only for associated stations, but not for P2P
1786          * Clients
1787          */
1788         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1789             !vif->bss_conf.assoc)
1790                 return;
1791
1792         bcast_mac->default_discard = 1;
1793
1794         /* copy all configured filters */
1795         for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1796                 /*
1797                  * Make sure we don't exceed our filters limit.
1798                  * if there is still a valid filter to be configured,
1799                  * be on the safe side and just allow bcast for this mac.
1800                  */
1801                 if (WARN_ON_ONCE(data->current_filter >=
1802                                  ARRAY_SIZE(cmd->filters))) {
1803                         bcast_mac->default_discard = 0;
1804                         bcast_mac->attached_filters = 0;
1805                         break;
1806                 }
1807
1808                 iwl_mvm_set_bcast_filter(vif,
1809                                          &mvm->bcast_filters[i],
1810                                          &cmd->filters[data->current_filter]);
1811
1812                 /* skip current filter if it contains no attributes */
1813                 if (!cmd->filters[data->current_filter].num_attrs)
1814                         continue;
1815
1816                 /* attach the filter to current mac */
1817                 bcast_mac->attached_filters |=
1818                                 cpu_to_le16(BIT(data->current_filter));
1819
1820                 data->current_filter++;
1821         }
1822 }
1823
1824 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1825                                     struct iwl_bcast_filter_cmd *cmd)
1826 {
1827         struct iwl_bcast_iter_data iter_data = {
1828                 .mvm = mvm,
1829                 .cmd = cmd,
1830         };
1831
1832         if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1833                 return false;
1834
1835         memset(cmd, 0, sizeof(*cmd));
1836         cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1837         cmd->max_macs = ARRAY_SIZE(cmd->macs);
1838
1839 #ifdef CONFIG_IWLWIFI_DEBUGFS
1840         /* use debugfs filters/macs if override is configured */
1841         if (mvm->dbgfs_bcast_filtering.override) {
1842                 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1843                        sizeof(cmd->filters));
1844                 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1845                        sizeof(cmd->macs));
1846                 return true;
1847         }
1848 #endif
1849
1850         /* if no filters are configured, do nothing */
1851         if (!mvm->bcast_filters)
1852                 return false;
1853
1854         /* configure and attach these filters for each associated sta vif */
1855         ieee80211_iterate_active_interfaces(
1856                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1857                 iwl_mvm_bcast_filter_iterator, &iter_data);
1858
1859         return true;
1860 }
1861
1862 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1863 {
1864         struct iwl_bcast_filter_cmd cmd;
1865
1866         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1867                 return 0;
1868
1869         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1870                 return 0;
1871
1872         return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1873                                     sizeof(cmd), &cmd);
1874 }
1875 #else
1876 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1877 {
1878         return 0;
1879 }
1880 #endif
1881
1882 static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1883                                     struct ieee80211_vif *vif)
1884 {
1885         struct iwl_mu_group_mgmt_cmd cmd = {};
1886
1887         memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1888                WLAN_MEMBERSHIP_LEN);
1889         memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1890                WLAN_USER_POSITION_LEN);
1891
1892         return iwl_mvm_send_cmd_pdu(mvm,
1893                                     WIDE_ID(DATA_PATH_GROUP,
1894                                             UPDATE_MU_GROUPS_CMD),
1895                                     0, sizeof(cmd), &cmd);
1896 }
1897
1898 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1899                                            struct ieee80211_vif *vif)
1900 {
1901         if (vif->mu_mimo_owner) {
1902                 struct iwl_mu_group_mgmt_notif *notif = _data;
1903
1904                 /*
1905                  * MU-MIMO Group Id action frame is little endian. We treat
1906                  * the data received from firmware as if it came from the
1907                  * action frame, so no conversion is needed.
1908                  */
1909                 ieee80211_update_mu_groups(vif,
1910                                            (u8 *)&notif->membership_status,
1911                                            (u8 *)&notif->user_position);
1912         }
1913 }
1914
1915 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1916                                struct iwl_rx_cmd_buffer *rxb)
1917 {
1918         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1919         struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1920
1921         ieee80211_iterate_active_interfaces_atomic(
1922                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1923                         iwl_mvm_mu_mimo_iface_iterator, notif);
1924 }
1925
1926 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1927                                              struct ieee80211_vif *vif,
1928                                              struct ieee80211_bss_conf *bss_conf,
1929                                              u32 changes)
1930 {
1931         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1932         int ret;
1933
1934         /*
1935          * Re-calculate the tsf id, as the master-slave relations depend on the
1936          * beacon interval, which was not known when the station interface was
1937          * added.
1938          */
1939         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1940                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1941
1942         if (changes & BSS_CHANGED_ASSOC && !bss_conf->assoc &&
1943             mvmvif->lqm_active)
1944                 iwl_mvm_send_lqm_cmd(vif, LQM_CMD_OPERATION_STOP_MEASUREMENT,
1945                                      0, 0);
1946
1947         /*
1948          * If we're not associated yet, take the (new) BSSID before associating
1949          * so the firmware knows. If we're already associated, then use the old
1950          * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
1951          * branch for disassociation below.
1952          */
1953         if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
1954                 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1955
1956         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
1957         if (ret)
1958                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1959
1960         /* after sending it once, adopt mac80211 data */
1961         memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
1962         mvmvif->associated = bss_conf->assoc;
1963
1964         if (changes & BSS_CHANGED_ASSOC) {
1965                 if (bss_conf->assoc) {
1966                         /* clear statistics to get clean beacon counter */
1967                         iwl_mvm_request_statistics(mvm, true);
1968                         memset(&mvmvif->beacon_stats, 0,
1969                                sizeof(mvmvif->beacon_stats));
1970
1971                         /* add quota for this interface */
1972                         ret = iwl_mvm_update_quotas(mvm, true, NULL);
1973                         if (ret) {
1974                                 IWL_ERR(mvm, "failed to update quotas\n");
1975                                 return;
1976                         }
1977
1978                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1979                                      &mvm->status)) {
1980                                 /*
1981                                  * If we're restarting then the firmware will
1982                                  * obviously have lost synchronisation with
1983                                  * the AP. It will attempt to synchronise by
1984                                  * itself, but we can make it more reliable by
1985                                  * scheduling a session protection time event.
1986                                  *
1987                                  * The firmware needs to receive a beacon to
1988                                  * catch up with synchronisation, use 110% of
1989                                  * the beacon interval.
1990                                  *
1991                                  * Set a large maximum delay to allow for more
1992                                  * than a single interface.
1993                                  */
1994                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1995                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
1996                                                         5 * dur, false);
1997                         }
1998
1999                         iwl_mvm_sf_update(mvm, vif, false);
2000                         iwl_mvm_power_vif_assoc(mvm, vif);
2001                         if (vif->p2p) {
2002                                 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
2003                                 iwl_mvm_update_smps(mvm, vif,
2004                                                     IWL_MVM_SMPS_REQ_PROT,
2005                                                     IEEE80211_SMPS_DYNAMIC);
2006                         }
2007                 } else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2008                         /*
2009                          * If update fails - SF might be running in associated
2010                          * mode while disassociated - which is forbidden.
2011                          */
2012                         WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
2013                                   "Failed to update SF upon disassociation\n");
2014
2015                         /*
2016                          * If we get an assert during the connection (after the
2017                          * station has been added, but before the vif is set
2018                          * to associated), mac80211 will re-add the station and
2019                          * then configure the vif. Since the vif is not
2020                          * associated, we would remove the station here and
2021                          * this would fail the recovery.
2022                          */
2023                         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2024                                       &mvm->status)) {
2025                                 /*
2026                                  * Remove AP station now that
2027                                  * the MAC is unassoc
2028                                  */
2029                                 ret = iwl_mvm_rm_sta_id(mvm, vif,
2030                                                         mvmvif->ap_sta_id);
2031                                 if (ret)
2032                                         IWL_ERR(mvm,
2033                                                 "failed to remove AP station\n");
2034
2035                                 if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
2036                                         mvm->d0i3_ap_sta_id =
2037                                                 IWL_MVM_INVALID_STA;
2038                                 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
2039                         }
2040
2041                         /* remove quota for this interface */
2042                         ret = iwl_mvm_update_quotas(mvm, false, NULL);
2043                         if (ret)
2044                                 IWL_ERR(mvm, "failed to update quotas\n");
2045
2046                         if (vif->p2p)
2047                                 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
2048
2049                         /* this will take the cleared BSSID from bss_conf */
2050                         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2051                         if (ret)
2052                                 IWL_ERR(mvm,
2053                                         "failed to update MAC %pM (clear after unassoc)\n",
2054                                         vif->addr);
2055                 }
2056
2057                 /*
2058                  * The firmware tracks the MU-MIMO group on its own.
2059                  * However, on HW restart we should restore this data.
2060                  */
2061                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2062                     (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2063                         ret = iwl_mvm_update_mu_groups(mvm, vif);
2064                         if (ret)
2065                                 IWL_ERR(mvm,
2066                                         "failed to update VHT MU_MIMO groups\n");
2067                 }
2068
2069                 iwl_mvm_recalc_multicast(mvm);
2070                 iwl_mvm_configure_bcast_filter(mvm);
2071
2072                 /* reset rssi values */
2073                 mvmvif->bf_data.ave_beacon_signal = 0;
2074
2075                 iwl_mvm_bt_coex_vif_change(mvm);
2076                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2077                                     IEEE80211_SMPS_AUTOMATIC);
2078                 if (fw_has_capa(&mvm->fw->ucode_capa,
2079                                 IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2080                         iwl_mvm_config_scan(mvm);
2081         }
2082
2083         if (changes & BSS_CHANGED_BEACON_INFO) {
2084                 /*
2085                  * We received a beacon from the associated AP so
2086                  * remove the session protection.
2087                  */
2088                 iwl_mvm_stop_session_protection(mvm, vif);
2089
2090                 iwl_mvm_sf_update(mvm, vif, false);
2091                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2092         }
2093
2094         if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2095                        /*
2096                         * Send power command on every beacon change,
2097                         * because we may have not enabled beacon abort yet.
2098                         */
2099                        BSS_CHANGED_BEACON_INFO)) {
2100                 ret = iwl_mvm_power_update_mac(mvm);
2101                 if (ret)
2102                         IWL_ERR(mvm, "failed to update power mode\n");
2103         }
2104
2105         if (changes & BSS_CHANGED_TXPOWER) {
2106                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2107                                 bss_conf->txpower);
2108                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2109         }
2110
2111         if (changes & BSS_CHANGED_CQM) {
2112                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2113                 /* reset cqm events tracking */
2114                 mvmvif->bf_data.last_cqm_event = 0;
2115                 if (mvmvif->bf_data.bf_enabled) {
2116                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2117                         if (ret)
2118                                 IWL_ERR(mvm,
2119                                         "failed to update CQM thresholds\n");
2120                 }
2121         }
2122
2123         if (changes & BSS_CHANGED_ARP_FILTER) {
2124                 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
2125                 iwl_mvm_configure_bcast_filter(mvm);
2126         }
2127 }
2128
2129 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2130                                  struct ieee80211_vif *vif)
2131 {
2132         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2133         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2134         int ret;
2135
2136         /*
2137          * iwl_mvm_mac_ctxt_add() might read directly from the device
2138          * (the system time), so make sure it is available.
2139          */
2140         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
2141         if (ret)
2142                 return ret;
2143
2144         mutex_lock(&mvm->mutex);
2145
2146         /* Send the beacon template */
2147         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2148         if (ret)
2149                 goto out_unlock;
2150
2151         /*
2152          * Re-calculate the tsf id, as the master-slave relations depend on the
2153          * beacon interval, which was not known when the AP interface was added.
2154          */
2155         if (vif->type == NL80211_IFTYPE_AP)
2156                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2157
2158         mvmvif->ap_assoc_sta_count = 0;
2159
2160         /* Add the mac context */
2161         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2162         if (ret)
2163                 goto out_unlock;
2164
2165         /* Perform the binding */
2166         ret = iwl_mvm_binding_add_vif(mvm, vif);
2167         if (ret)
2168                 goto out_remove;
2169
2170         /*
2171          * This is not very nice, but the simplest:
2172          * For older FWs adding the mcast sta before the bcast station may
2173          * cause assert 0x2b00.
2174          * This is fixed in later FW so make the order of removal depend on
2175          * the TLV
2176          */
2177         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2178                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2179                 if (ret)
2180                         goto out_unbind;
2181                 /*
2182                  * Send the bcast station. At this stage the TBTT and DTIM time
2183                  * events are added and applied to the scheduler
2184                  */
2185                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2186                 if (ret) {
2187                         iwl_mvm_rm_mcast_sta(mvm, vif);
2188                         goto out_unbind;
2189                 }
2190         } else {
2191                 /*
2192                  * Send the bcast station. At this stage the TBTT and DTIM time
2193                  * events are added and applied to the scheduler
2194                  */
2195                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2196                 if (ret)
2197                         goto out_unbind;
2198                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2199                 if (ret) {
2200                         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2201                         goto out_unbind;
2202                 }
2203         }
2204
2205         /* must be set before quota calculations */
2206         mvmvif->ap_ibss_active = true;
2207
2208         /* power updated needs to be done before quotas */
2209         iwl_mvm_power_update_mac(mvm);
2210
2211         ret = iwl_mvm_update_quotas(mvm, false, NULL);
2212         if (ret)
2213                 goto out_quota_failed;
2214
2215         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2216         if (vif->p2p && mvm->p2p_device_vif)
2217                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2218
2219         iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
2220
2221         iwl_mvm_bt_coex_vif_change(mvm);
2222
2223         /* we don't support TDLS during DCM */
2224         if (iwl_mvm_phy_ctx_count(mvm) > 1)
2225                 iwl_mvm_teardown_tdls_peers(mvm);
2226
2227         goto out_unlock;
2228
2229 out_quota_failed:
2230         iwl_mvm_power_update_mac(mvm);
2231         mvmvif->ap_ibss_active = false;
2232         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2233         iwl_mvm_rm_mcast_sta(mvm, vif);
2234 out_unbind:
2235         iwl_mvm_binding_remove_vif(mvm, vif);
2236 out_remove:
2237         iwl_mvm_mac_ctxt_remove(mvm, vif);
2238 out_unlock:
2239         mutex_unlock(&mvm->mutex);
2240         iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
2241         return ret;
2242 }
2243
2244 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2245                                  struct ieee80211_vif *vif)
2246 {
2247         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2248         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2249
2250         iwl_mvm_prepare_mac_removal(mvm, vif);
2251
2252         mutex_lock(&mvm->mutex);
2253
2254         /* Handle AP stop while in CSA */
2255         if (rcu_access_pointer(mvm->csa_vif) == vif) {
2256                 iwl_mvm_remove_time_event(mvm, mvmvif,
2257                                           &mvmvif->time_event_data);
2258                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
2259                 mvmvif->csa_countdown = false;
2260         }
2261
2262         if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2263                 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2264                 mvm->csa_tx_block_bcn_timeout = 0;
2265         }
2266
2267         mvmvif->ap_ibss_active = false;
2268         mvm->ap_last_beacon_gp2 = 0;
2269
2270         iwl_mvm_bt_coex_vif_change(mvm);
2271
2272         iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
2273
2274         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2275         if (vif->p2p && mvm->p2p_device_vif)
2276                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2277
2278         iwl_mvm_update_quotas(mvm, false, NULL);
2279
2280         /*
2281          * This is not very nice, but the simplest:
2282          * For older FWs removing the mcast sta before the bcast station may
2283          * cause assert 0x2b00.
2284          * This is fixed in later FW (which will stop beaconing when removing
2285          * bcast station).
2286          * So make the order of removal depend on the TLV
2287          */
2288         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2289                 iwl_mvm_rm_mcast_sta(mvm, vif);
2290         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2291         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2292                 iwl_mvm_rm_mcast_sta(mvm, vif);
2293         iwl_mvm_binding_remove_vif(mvm, vif);
2294
2295         iwl_mvm_power_update_mac(mvm);
2296
2297         iwl_mvm_mac_ctxt_remove(mvm, vif);
2298
2299         mutex_unlock(&mvm->mutex);
2300 }
2301
2302 static void
2303 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2304                                  struct ieee80211_vif *vif,
2305                                  struct ieee80211_bss_conf *bss_conf,
2306                                  u32 changes)
2307 {
2308         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2309
2310         /* Changes will be applied when the AP/IBSS is started */
2311         if (!mvmvif->ap_ibss_active)
2312                 return;
2313
2314         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2315                        BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2316             iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2317                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2318
2319         /* Need to send a new beacon template to the FW */
2320         if (changes & BSS_CHANGED_BEACON &&
2321             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2322                 IWL_WARN(mvm, "Failed updating beacon data\n");
2323
2324         if (changes & BSS_CHANGED_TXPOWER) {
2325                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2326                                 bss_conf->txpower);
2327                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2328         }
2329 }
2330
2331 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2332                                      struct ieee80211_vif *vif,
2333                                      struct ieee80211_bss_conf *bss_conf,
2334                                      u32 changes)
2335 {
2336         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2337
2338         /*
2339          * iwl_mvm_bss_info_changed_station() might call
2340          * iwl_mvm_protect_session(), which reads directly from
2341          * the device (the system time), so make sure it is available.
2342          */
2343         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
2344                 return;
2345
2346         mutex_lock(&mvm->mutex);
2347
2348         if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2349                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2350
2351         switch (vif->type) {
2352         case NL80211_IFTYPE_STATION:
2353                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2354                 break;
2355         case NL80211_IFTYPE_AP:
2356         case NL80211_IFTYPE_ADHOC:
2357                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2358                 break;
2359         case NL80211_IFTYPE_MONITOR:
2360                 if (changes & BSS_CHANGED_MU_GROUPS)
2361                         iwl_mvm_update_mu_groups(mvm, vif);
2362                 break;
2363         default:
2364                 /* shouldn't happen */
2365                 WARN_ON_ONCE(1);
2366         }
2367
2368         mutex_unlock(&mvm->mutex);
2369         iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
2370 }
2371
2372 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2373                                struct ieee80211_vif *vif,
2374                                struct ieee80211_scan_request *hw_req)
2375 {
2376         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2377         int ret;
2378
2379         if (hw_req->req.n_channels == 0 ||
2380             hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2381                 return -EINVAL;
2382
2383         mutex_lock(&mvm->mutex);
2384         ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2385         mutex_unlock(&mvm->mutex);
2386
2387         return ret;
2388 }
2389
2390 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2391                                        struct ieee80211_vif *vif)
2392 {
2393         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2394
2395         mutex_lock(&mvm->mutex);
2396
2397         /* Due to a race condition, it's possible that mac80211 asks
2398          * us to stop a hw_scan when it's already stopped.  This can
2399          * happen, for instance, if we stopped the scan ourselves,
2400          * called ieee80211_scan_completed() and the userspace called
2401          * cancel scan scan before ieee80211_scan_work() could run.
2402          * To handle that, simply return if the scan is not running.
2403         */
2404         if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2405                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2406
2407         mutex_unlock(&mvm->mutex);
2408 }
2409
2410 static void
2411 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2412                                   struct ieee80211_sta *sta, u16 tids,
2413                                   int num_frames,
2414                                   enum ieee80211_frame_release_type reason,
2415                                   bool more_data)
2416 {
2417         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2418
2419         /* Called when we need to transmit (a) frame(s) from mac80211 */
2420
2421         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2422                                           tids, more_data, false);
2423 }
2424
2425 static void
2426 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2427                                     struct ieee80211_sta *sta, u16 tids,
2428                                     int num_frames,
2429                                     enum ieee80211_frame_release_type reason,
2430                                     bool more_data)
2431 {
2432         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2433
2434         /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2435
2436         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2437                                           tids, more_data, true);
2438 }
2439
2440 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2441                                      enum sta_notify_cmd cmd,
2442                                      struct ieee80211_sta *sta)
2443 {
2444         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2445         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2446         unsigned long txqs = 0, tids = 0;
2447         int tid;
2448
2449         /*
2450          * If we have TVQM then we get too high queue numbers - luckily
2451          * we really shouldn't get here with that because such hardware
2452          * should have firmware supporting buffer station offload.
2453          */
2454         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2455                 return;
2456
2457         spin_lock_bh(&mvmsta->lock);
2458         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
2459                 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2460
2461                 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
2462                         continue;
2463
2464                 __set_bit(tid_data->txq_id, &txqs);
2465
2466                 if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2467                         continue;
2468
2469                 __set_bit(tid, &tids);
2470         }
2471
2472         switch (cmd) {
2473         case STA_NOTIFY_SLEEP:
2474                 for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2475                         ieee80211_sta_set_buffered(sta, tid, true);
2476
2477                 if (txqs)
2478                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2479                 /*
2480                  * The fw updates the STA to be asleep. Tx packets on the Tx
2481                  * queues to this station will not be transmitted. The fw will
2482                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2483                  */
2484                 break;
2485         case STA_NOTIFY_AWAKE:
2486                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2487                         break;
2488
2489                 if (txqs)
2490                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2491                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
2492                 break;
2493         default:
2494                 break;
2495         }
2496         spin_unlock_bh(&mvmsta->lock);
2497 }
2498
2499 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2500                                    struct ieee80211_vif *vif,
2501                                    enum sta_notify_cmd cmd,
2502                                    struct ieee80211_sta *sta)
2503 {
2504         __iwl_mvm_mac_sta_notify(hw, cmd, sta);
2505 }
2506
2507 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2508 {
2509         struct iwl_rx_packet *pkt = rxb_addr(rxb);
2510         struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
2511         struct ieee80211_sta *sta;
2512         struct iwl_mvm_sta *mvmsta;
2513         bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
2514
2515         if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
2516                 return;
2517
2518         rcu_read_lock();
2519         sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
2520         if (WARN_ON(IS_ERR_OR_NULL(sta))) {
2521                 rcu_read_unlock();
2522                 return;
2523         }
2524
2525         mvmsta = iwl_mvm_sta_from_mac80211(sta);
2526
2527         if (!mvmsta->vif ||
2528             mvmsta->vif->type != NL80211_IFTYPE_AP) {
2529                 rcu_read_unlock();
2530                 return;
2531         }
2532
2533         if (mvmsta->sleeping != sleeping) {
2534                 mvmsta->sleeping = sleeping;
2535                 __iwl_mvm_mac_sta_notify(mvm->hw,
2536                         sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
2537                         sta);
2538                 ieee80211_sta_ps_transition(sta, sleeping);
2539         }
2540
2541         if (sleeping) {
2542                 switch (notif->type) {
2543                 case IWL_MVM_PM_EVENT_AWAKE:
2544                 case IWL_MVM_PM_EVENT_ASLEEP:
2545                         break;
2546                 case IWL_MVM_PM_EVENT_UAPSD:
2547                         ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
2548                         break;
2549                 case IWL_MVM_PM_EVENT_PS_POLL:
2550                         ieee80211_sta_pspoll(sta);
2551                         break;
2552                 default:
2553                         break;
2554                 }
2555         }
2556
2557         rcu_read_unlock();
2558 }
2559
2560 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2561                                        struct ieee80211_vif *vif,
2562                                        struct ieee80211_sta *sta)
2563 {
2564         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2565         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2566
2567         /*
2568          * This is called before mac80211 does RCU synchronisation,
2569          * so here we already invalidate our internal RCU-protected
2570          * station pointer. The rest of the code will thus no longer
2571          * be able to find the station this way, and we don't rely
2572          * on further RCU synchronisation after the sta_state()
2573          * callback deleted the station.
2574          */
2575         mutex_lock(&mvm->mutex);
2576         if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2577                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2578                                    ERR_PTR(-ENOENT));
2579
2580         mutex_unlock(&mvm->mutex);
2581 }
2582
2583 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2584                                 const u8 *bssid)
2585 {
2586         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2587                 return;
2588
2589         if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2590                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2591                 return;
2592         }
2593
2594         if (!vif->p2p &&
2595             (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2596                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2597                 return;
2598         }
2599
2600         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2601 }
2602
2603 static void
2604 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
2605                            struct ieee80211_vif *vif, u8 *peer_addr,
2606                            enum nl80211_tdls_operation action)
2607 {
2608         struct iwl_fw_dbg_trigger_tlv *trig;
2609         struct iwl_fw_dbg_trigger_tdls *tdls_trig;
2610
2611         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TDLS))
2612                 return;
2613
2614         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TDLS);
2615         tdls_trig = (void *)trig->data;
2616         if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
2617                                            ieee80211_vif_to_wdev(vif), trig))
2618                 return;
2619
2620         if (!(tdls_trig->action_bitmap & BIT(action)))
2621                 return;
2622
2623         if (tdls_trig->peer_mode &&
2624             memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
2625                 return;
2626
2627         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
2628                                 "TDLS event occurred, peer %pM, action %d",
2629                                 peer_addr, action);
2630 }
2631
2632 static void iwl_mvm_purge_deferred_tx_frames(struct iwl_mvm *mvm,
2633                                              struct iwl_mvm_sta *mvm_sta)
2634 {
2635         struct iwl_mvm_tid_data *tid_data;
2636         struct sk_buff *skb;
2637         int i;
2638
2639         spin_lock_bh(&mvm_sta->lock);
2640         for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
2641                 tid_data = &mvm_sta->tid_data[i];
2642
2643                 while ((skb = __skb_dequeue(&tid_data->deferred_tx_frames))) {
2644                         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2645
2646                         /*
2647                          * The first deferred frame should've stopped the MAC
2648                          * queues, so we should never get a second deferred
2649                          * frame for the RA/TID.
2650                          */
2651                         iwl_mvm_start_mac_queues(mvm, BIT(info->hw_queue));
2652                         ieee80211_free_txskb(mvm->hw, skb);
2653                 }
2654         }
2655         spin_unlock_bh(&mvm_sta->lock);
2656 }
2657
2658 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
2659                                  struct ieee80211_vif *vif,
2660                                  struct ieee80211_sta *sta,
2661                                  enum ieee80211_sta_state old_state,
2662                                  enum ieee80211_sta_state new_state)
2663 {
2664         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2665         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2666         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2667         int ret;
2668
2669         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
2670                            sta->addr, old_state, new_state);
2671
2672         /* this would be a mac80211 bug ... but don't crash */
2673         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2674                 return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
2675
2676         /*
2677          * If we are in a STA removal flow and in DQA mode:
2678          *
2679          * This is after the sync_rcu part, so the queues have already been
2680          * flushed. No more TXs on their way in mac80211's path, and no more in
2681          * the queues.
2682          * Also, we won't be getting any new TX frames for this station.
2683          * What we might have are deferred TX frames that need to be taken care
2684          * of.
2685          *
2686          * Drop any still-queued deferred-frame before removing the STA, and
2687          * make sure the worker is no longer handling frames for this STA.
2688          */
2689         if (old_state == IEEE80211_STA_NONE &&
2690             new_state == IEEE80211_STA_NOTEXIST) {
2691                 iwl_mvm_purge_deferred_tx_frames(mvm, mvm_sta);
2692                 flush_work(&mvm->add_stream_wk);
2693
2694                 /*
2695                  * No need to make sure deferred TX indication is off since the
2696                  * worker will already remove it if it was on
2697                  */
2698         }
2699
2700         mutex_lock(&mvm->mutex);
2701         /* track whether or not the station is associated */
2702         mvm_sta->associated = new_state >= IEEE80211_STA_ASSOC;
2703
2704         if (old_state == IEEE80211_STA_NOTEXIST &&
2705             new_state == IEEE80211_STA_NONE) {
2706                 /*
2707                  * Firmware bug - it'll crash if the beacon interval is less
2708                  * than 16. We can't avoid connecting at all, so refuse the
2709                  * station state change, this will cause mac80211 to abandon
2710                  * attempts to connect to this AP, and eventually wpa_s will
2711                  * blacklist the AP...
2712                  */
2713                 if (vif->type == NL80211_IFTYPE_STATION &&
2714                     vif->bss_conf.beacon_int < 16) {
2715                         IWL_ERR(mvm,
2716                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2717                                 sta->addr, vif->bss_conf.beacon_int);
2718                         ret = -EINVAL;
2719                         goto out_unlock;
2720                 }
2721
2722                 if (sta->tdls &&
2723                     (vif->p2p ||
2724                      iwl_mvm_tdls_sta_count(mvm, NULL) ==
2725                                                 IWL_MVM_TDLS_STA_COUNT ||
2726                      iwl_mvm_phy_ctx_count(mvm) > 1)) {
2727                         IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2728                         ret = -EBUSY;
2729                         goto out_unlock;
2730                 }
2731
2732                 ret = iwl_mvm_add_sta(mvm, vif, sta);
2733                 if (sta->tdls && ret == 0) {
2734                         iwl_mvm_recalc_tdls_state(mvm, vif, true);
2735                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
2736                                                    NL80211_TDLS_SETUP);
2737                 }
2738         } else if (old_state == IEEE80211_STA_NONE &&
2739                    new_state == IEEE80211_STA_AUTH) {
2740                 /*
2741                  * EBS may be disabled due to previous failures reported by FW.
2742                  * Reset EBS status here assuming environment has been changed.
2743                  */
2744                 mvm->last_ebs_successful = true;
2745                 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
2746                 ret = 0;
2747         } else if (old_state == IEEE80211_STA_AUTH &&
2748                    new_state == IEEE80211_STA_ASSOC) {
2749                 if (vif->type == NL80211_IFTYPE_AP) {
2750                         mvmvif->ap_assoc_sta_count++;
2751                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2752                 }
2753
2754                 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
2755                                      true);
2756                 ret = iwl_mvm_update_sta(mvm, vif, sta);
2757         } else if (old_state == IEEE80211_STA_ASSOC &&
2758                    new_state == IEEE80211_STA_AUTHORIZED) {
2759
2760                 /* we don't support TDLS during DCM */
2761                 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2762                         iwl_mvm_teardown_tdls_peers(mvm);
2763
2764                 if (sta->tdls)
2765                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
2766                                                    NL80211_TDLS_ENABLE_LINK);
2767
2768                 /* enable beacon filtering */
2769                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2770
2771                 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
2772                                      false);
2773
2774                 ret = 0;
2775         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
2776                    new_state == IEEE80211_STA_ASSOC) {
2777                 /* disable beacon filtering */
2778                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2779                 ret = 0;
2780         } else if (old_state == IEEE80211_STA_ASSOC &&
2781                    new_state == IEEE80211_STA_AUTH) {
2782                 if (vif->type == NL80211_IFTYPE_AP) {
2783                         mvmvif->ap_assoc_sta_count--;
2784                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2785                 }
2786                 ret = 0;
2787         } else if (old_state == IEEE80211_STA_AUTH &&
2788                    new_state == IEEE80211_STA_NONE) {
2789                 ret = 0;
2790         } else if (old_state == IEEE80211_STA_NONE &&
2791                    new_state == IEEE80211_STA_NOTEXIST) {
2792                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
2793                 if (sta->tdls) {
2794                         iwl_mvm_recalc_tdls_state(mvm, vif, false);
2795                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
2796                                                    NL80211_TDLS_DISABLE_LINK);
2797                 }
2798         } else {
2799                 ret = -EIO;
2800         }
2801  out_unlock:
2802         mutex_unlock(&mvm->mutex);
2803
2804         if (sta->tdls && ret == 0) {
2805                 if (old_state == IEEE80211_STA_NOTEXIST &&
2806                     new_state == IEEE80211_STA_NONE)
2807                         ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2808                 else if (old_state == IEEE80211_STA_NONE &&
2809                          new_state == IEEE80211_STA_NOTEXIST)
2810                         ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
2811         }
2812
2813         return ret;
2814 }
2815
2816 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2817 {
2818         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2819
2820         mvm->rts_threshold = value;
2821
2822         return 0;
2823 }
2824
2825 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2826                                   struct ieee80211_vif *vif,
2827                                   struct ieee80211_sta *sta, u32 changed)
2828 {
2829         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2830
2831         if (vif->type == NL80211_IFTYPE_STATION &&
2832             changed & IEEE80211_RC_NSS_CHANGED)
2833                 iwl_mvm_sf_update(mvm, vif, false);
2834 }
2835
2836 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2837                                struct ieee80211_vif *vif, u16 ac,
2838                                const struct ieee80211_tx_queue_params *params)
2839 {
2840         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2841         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2842
2843         mvmvif->queue_params[ac] = *params;
2844
2845         /*
2846          * No need to update right away, we'll get BSS_CHANGED_QOS
2847          * The exception is P2P_DEVICE interface which needs immediate update.
2848          */
2849         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2850                 int ret;
2851
2852                 mutex_lock(&mvm->mutex);
2853                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2854                 mutex_unlock(&mvm->mutex);
2855                 return ret;
2856         }
2857         return 0;
2858 }
2859
2860 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2861                                       struct ieee80211_vif *vif)
2862 {
2863         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2864         u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2865         u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2866
2867         if (WARN_ON_ONCE(vif->bss_conf.assoc))
2868                 return;
2869
2870         /*
2871          * iwl_mvm_protect_session() reads directly from the device
2872          * (the system time), so make sure it is available.
2873          */
2874         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2875                 return;
2876
2877         mutex_lock(&mvm->mutex);
2878         /* Try really hard to protect the session and hear a beacon */
2879         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
2880         mutex_unlock(&mvm->mutex);
2881
2882         iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2883 }
2884
2885 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2886                                         struct ieee80211_vif *vif,
2887                                         struct cfg80211_sched_scan_request *req,
2888                                         struct ieee80211_scan_ies *ies)
2889 {
2890         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2891
2892         int ret;
2893
2894         mutex_lock(&mvm->mutex);
2895
2896         if (!vif->bss_conf.idle) {
2897                 ret = -EBUSY;
2898                 goto out;
2899         }
2900
2901         ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
2902
2903 out:
2904         mutex_unlock(&mvm->mutex);
2905         return ret;
2906 }
2907
2908 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2909                                        struct ieee80211_vif *vif)
2910 {
2911         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2912         int ret;
2913
2914         mutex_lock(&mvm->mutex);
2915
2916         /* Due to a race condition, it's possible that mac80211 asks
2917          * us to stop a sched_scan when it's already stopped.  This
2918          * can happen, for instance, if we stopped the scan ourselves,
2919          * called ieee80211_sched_scan_stopped() and the userspace called
2920          * stop sched scan scan before ieee80211_sched_scan_stopped_work()
2921          * could run.  To handle this, simply return if the scan is
2922          * not running.
2923         */
2924         if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
2925                 mutex_unlock(&mvm->mutex);
2926                 return 0;
2927         }
2928
2929         ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
2930         mutex_unlock(&mvm->mutex);
2931         iwl_mvm_wait_for_async_handlers(mvm);
2932
2933         return ret;
2934 }
2935
2936 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2937                                enum set_key_cmd cmd,
2938                                struct ieee80211_vif *vif,
2939                                struct ieee80211_sta *sta,
2940                                struct ieee80211_key_conf *key)
2941 {
2942         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2943         struct iwl_mvm_sta *mvmsta;
2944         struct iwl_mvm_key_pn *ptk_pn;
2945         int keyidx = key->keyidx;
2946         int ret;
2947         u8 key_offset;
2948
2949         if (iwlwifi_mod_params.swcrypto) {
2950                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2951                 return -EOPNOTSUPP;
2952         }
2953
2954         switch (key->cipher) {
2955         case WLAN_CIPHER_SUITE_TKIP:
2956                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2957                 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2958                 break;
2959         case WLAN_CIPHER_SUITE_CCMP:
2960         case WLAN_CIPHER_SUITE_GCMP:
2961         case WLAN_CIPHER_SUITE_GCMP_256:
2962                 if (!iwl_mvm_has_new_tx_api(mvm))
2963                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2964                 break;
2965         case WLAN_CIPHER_SUITE_AES_CMAC:
2966         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2967         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2968                 WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
2969                 break;
2970         case WLAN_CIPHER_SUITE_WEP40:
2971         case WLAN_CIPHER_SUITE_WEP104:
2972                 /* For non-client mode, only use WEP keys for TX as we probably
2973                  * don't have a station yet anyway and would then have to keep
2974                  * track of the keys, linking them to each of the clients/peers
2975                  * as they appear. For now, don't do that, for performance WEP
2976                  * offload doesn't really matter much, but we need it for some
2977                  * other offload features in client mode.
2978                  */
2979                 if (vif->type != NL80211_IFTYPE_STATION)
2980                         return 0;
2981                 break;
2982         default:
2983                 /* currently FW supports only one optional cipher scheme */
2984                 if (hw->n_cipher_schemes &&
2985                     hw->cipher_schemes->cipher == key->cipher)
2986                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2987                 else
2988                         return -EOPNOTSUPP;
2989         }
2990
2991         mutex_lock(&mvm->mutex);
2992
2993         switch (cmd) {
2994         case SET_KEY:
2995                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
2996                      vif->type == NL80211_IFTYPE_AP) && !sta) {
2997                         /*
2998                          * GTK on AP interface is a TX-only key, return 0;
2999                          * on IBSS they're per-station and because we're lazy
3000                          * we don't support them for RX, so do the same.
3001                          * CMAC/GMAC in AP/IBSS modes must be done in software.
3002                          */
3003                         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3004                             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3005                             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3006                                 ret = -EOPNOTSUPP;
3007                         else
3008                                 ret = 0;
3009
3010                         if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
3011                             key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
3012                             !iwl_mvm_has_new_tx_api(mvm)) {
3013                                 key->hw_key_idx = STA_KEY_IDX_INVALID;
3014                                 break;
3015                         }
3016                 }
3017
3018                 /* During FW restart, in order to restore the state as it was,
3019                  * don't try to reprogram keys we previously failed for.
3020                  */
3021                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3022                     key->hw_key_idx == STA_KEY_IDX_INVALID) {
3023                         IWL_DEBUG_MAC80211(mvm,
3024                                            "skip invalid idx key programming during restart\n");
3025                         ret = 0;
3026                         break;
3027                 }
3028
3029                 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3030                     sta && iwl_mvm_has_new_rx_api(mvm) &&
3031                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3032                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3033                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3034                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3035                         struct ieee80211_key_seq seq;
3036                         int tid, q;
3037
3038                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
3039                         WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3040                         ptk_pn = kzalloc(sizeof(*ptk_pn) +
3041                                          mvm->trans->num_rx_queues *
3042                                                 sizeof(ptk_pn->q[0]),
3043                                          GFP_KERNEL);
3044                         if (!ptk_pn) {
3045                                 ret = -ENOMEM;
3046                                 break;
3047                         }
3048
3049                         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3050                                 ieee80211_get_key_rx_seq(key, tid, &seq);
3051                                 for (q = 0; q < mvm->trans->num_rx_queues; q++)
3052                                         memcpy(ptk_pn->q[q].pn[tid],
3053                                                seq.ccmp.pn,
3054                                                IEEE80211_CCMP_PN_LEN);
3055                         }
3056
3057                         rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3058                 }
3059
3060                 /* in HW restart reuse the index, otherwise request a new one */
3061                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3062                         key_offset = key->hw_key_idx;
3063                 else
3064                         key_offset = STA_KEY_IDX_INVALID;
3065
3066                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
3067                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3068                 if (ret) {
3069                         IWL_WARN(mvm, "set key failed\n");
3070                         /*
3071                          * can't add key for RX, but we don't need it
3072                          * in the device for TX so still return 0
3073                          */
3074                         key->hw_key_idx = STA_KEY_IDX_INVALID;
3075                         ret = 0;
3076                 }
3077
3078                 break;
3079         case DISABLE_KEY:
3080                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3081                         ret = 0;
3082                         break;
3083                 }
3084
3085                 if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3086                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3087                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3088                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3089                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3090                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
3091                         ptk_pn = rcu_dereference_protected(
3092                                                 mvmsta->ptk_pn[keyidx],
3093                                                 lockdep_is_held(&mvm->mutex));
3094                         RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3095                         if (ptk_pn)
3096                                 kfree_rcu(ptk_pn, rcu_head);
3097                 }
3098
3099                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3100                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3101                 break;
3102         default:
3103                 ret = -EINVAL;
3104         }
3105
3106         mutex_unlock(&mvm->mutex);
3107         return ret;
3108 }
3109
3110 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3111                                         struct ieee80211_vif *vif,
3112                                         struct ieee80211_key_conf *keyconf,
3113                                         struct ieee80211_sta *sta,
3114                                         u32 iv32, u16 *phase1key)
3115 {
3116         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3117
3118         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3119                 return;
3120
3121         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3122 }
3123
3124
3125 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3126                                struct iwl_rx_packet *pkt, void *data)
3127 {
3128         struct iwl_mvm *mvm =
3129                 container_of(notif_wait, struct iwl_mvm, notif_wait);
3130         struct iwl_hs20_roc_res *resp;
3131         int resp_len = iwl_rx_packet_payload_len(pkt);
3132         struct iwl_mvm_time_event_data *te_data = data;
3133
3134         if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3135                 return true;
3136
3137         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3138                 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3139                 return true;
3140         }
3141
3142         resp = (void *)pkt->data;
3143
3144         IWL_DEBUG_TE(mvm,
3145                      "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
3146                      resp->status, resp->event_unique_id);
3147
3148         te_data->uid = le32_to_cpu(resp->event_unique_id);
3149         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3150                      te_data->uid);
3151
3152         spin_lock_bh(&mvm->time_event_lock);
3153         list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3154         spin_unlock_bh(&mvm->time_event_lock);
3155
3156         return true;
3157 }
3158
3159 #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3160 #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3161 #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3162 #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3163 #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3164 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3165                                     struct ieee80211_channel *channel,
3166                                     struct ieee80211_vif *vif,
3167                                     int duration)
3168 {
3169         int res, time_reg = DEVICE_SYSTEM_TIME_REG;
3170         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3171         struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3172         static const u16 time_event_response[] = { HOT_SPOT_CMD };
3173         struct iwl_notification_wait wait_time_event;
3174         u32 dtim_interval = vif->bss_conf.dtim_period *
3175                 vif->bss_conf.beacon_int;
3176         u32 req_dur, delay;
3177         struct iwl_hs20_roc_req aux_roc_req = {
3178                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3179                 .id_and_color =
3180                         cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3181                 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3182                 /* Set the channel info data */
3183                 .channel_info.band = (channel->band == NL80211_BAND_2GHZ) ?
3184                         PHY_BAND_24 : PHY_BAND_5,
3185                 .channel_info.channel = channel->hw_value,
3186                 .channel_info.width = PHY_VHT_CHANNEL_MODE20,
3187                 /* Set the time and duration */
3188                 .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
3189          };
3190
3191         delay = AUX_ROC_MIN_DELAY;
3192         req_dur = MSEC_TO_TU(duration);
3193
3194         /*
3195          * If we are associated we want the delay time to be at least one
3196          * dtim interval so that the FW can wait until after the DTIM and
3197          * then start the time event, this will potentially allow us to
3198          * remain off-channel for the max duration.
3199          * Since we want to use almost a whole dtim interval we would also
3200          * like the delay to be for 2-3 dtim intervals, in case there are
3201          * other time events with higher priority.
3202          */
3203         if (vif->bss_conf.assoc) {
3204                 delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3205                 /* We cannot remain off-channel longer than the DTIM interval */
3206                 if (dtim_interval <= req_dur) {
3207                         req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3208                         if (req_dur <= AUX_ROC_MIN_DURATION)
3209                                 req_dur = dtim_interval -
3210                                         AUX_ROC_MIN_SAFETY_BUFFER;
3211                 }
3212         }
3213
3214         aux_roc_req.duration = cpu_to_le32(req_dur);
3215         aux_roc_req.apply_time_max_delay = cpu_to_le32(delay);
3216
3217         IWL_DEBUG_TE(mvm,
3218                      "ROC: Requesting to remain on channel %u for %ums\n",
3219                      channel->hw_value, req_dur);
3220         IWL_DEBUG_TE(mvm,
3221                      "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3222                      duration, delay, dtim_interval);
3223
3224         /* Set the node address */
3225         memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
3226
3227         lockdep_assert_held(&mvm->mutex);
3228
3229         spin_lock_bh(&mvm->time_event_lock);
3230
3231         if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3232                 spin_unlock_bh(&mvm->time_event_lock);
3233                 return -EIO;
3234         }
3235
3236         te_data->vif = vif;
3237         te_data->duration = duration;
3238         te_data->id = HOT_SPOT_CMD;
3239
3240         spin_unlock_bh(&mvm->time_event_lock);
3241
3242         /*
3243          * Use a notification wait, which really just processes the
3244          * command response and doesn't wait for anything, in order
3245          * to be able to process the response and get the UID inside
3246          * the RX path. Using CMD_WANT_SKB doesn't work because it
3247          * stores the buffer and then wakes up this thread, by which
3248          * time another notification (that the time event started)
3249          * might already be processed unsuccessfully.
3250          */
3251         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3252                                    time_event_response,
3253                                    ARRAY_SIZE(time_event_response),
3254                                    iwl_mvm_rx_aux_roc, te_data);
3255
3256         res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
3257                                    &aux_roc_req);
3258
3259         if (res) {
3260                 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3261                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3262                 goto out_clear_te;
3263         }
3264
3265         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
3266         res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3267         /* should never fail */
3268         WARN_ON_ONCE(res);
3269
3270         if (res) {
3271  out_clear_te:
3272                 spin_lock_bh(&mvm->time_event_lock);
3273                 iwl_mvm_te_clear_data(mvm, te_data);
3274                 spin_unlock_bh(&mvm->time_event_lock);
3275         }
3276
3277         return res;
3278 }
3279
3280 static int iwl_mvm_roc(struct ieee80211_hw *hw,
3281                        struct ieee80211_vif *vif,
3282                        struct ieee80211_channel *channel,
3283                        int duration,
3284                        enum ieee80211_roc_type type)
3285 {
3286         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3287         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3288         struct cfg80211_chan_def chandef;
3289         struct iwl_mvm_phy_ctxt *phy_ctxt;
3290         bool band_change_removal;
3291         int ret, i;
3292
3293         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3294                            duration, type);
3295
3296         flush_work(&mvm->roc_done_wk);
3297
3298         mutex_lock(&mvm->mutex);
3299
3300         switch (vif->type) {
3301         case NL80211_IFTYPE_STATION:
3302                 if (fw_has_capa(&mvm->fw->ucode_capa,
3303                                 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3304                         /* Use aux roc framework (HS20) */
3305                         ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3306                                                        vif, duration);
3307                         goto out_unlock;
3308                 }
3309                 IWL_ERR(mvm, "hotspot not supported\n");
3310                 ret = -EINVAL;
3311                 goto out_unlock;
3312         case NL80211_IFTYPE_P2P_DEVICE:
3313                 /* handle below */
3314                 break;
3315         default:
3316                 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3317                 ret = -EINVAL;
3318                 goto out_unlock;
3319         }
3320
3321         for (i = 0; i < NUM_PHY_CTX; i++) {
3322                 phy_ctxt = &mvm->phy_ctxts[i];
3323                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3324                         continue;
3325
3326                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3327                         /*
3328                          * Unbind the P2P_DEVICE from the current PHY context,
3329                          * and if the PHY context is not used remove it.
3330                          */
3331                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
3332                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3333                                 goto out_unlock;
3334
3335                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3336
3337                         /* Bind the P2P_DEVICE to the current PHY Context */
3338                         mvmvif->phy_ctxt = phy_ctxt;
3339
3340                         ret = iwl_mvm_binding_add_vif(mvm, vif);
3341                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3342                                 goto out_unlock;
3343
3344                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3345                         goto schedule_time_event;
3346                 }
3347         }
3348
3349         /* Need to update the PHY context only if the ROC channel changed */
3350         if (channel == mvmvif->phy_ctxt->channel)
3351                 goto schedule_time_event;
3352
3353         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3354
3355         /*
3356          * Check if the remain-on-channel is on a different band and that
3357          * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
3358          * so, we'll need to release and then re-configure here, since we
3359          * must not remove a PHY context that's part of a binding.
3360          */
3361         band_change_removal =
3362                 fw_has_capa(&mvm->fw->ucode_capa,
3363                             IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
3364                 mvmvif->phy_ctxt->channel->band != chandef.chan->band;
3365
3366         if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
3367                 /*
3368                  * Change the PHY context configuration as it is currently
3369                  * referenced only by the P2P Device MAC (and we can modify it)
3370                  */
3371                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3372                                                &chandef, 1, 1);
3373                 if (ret)
3374                         goto out_unlock;
3375         } else {
3376                 /*
3377                  * The PHY context is shared with other MACs (or we're trying to
3378                  * switch bands), so remove the P2P Device from the binding,
3379                  * allocate an new PHY context and create a new binding.
3380                  */
3381                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3382                 if (!phy_ctxt) {
3383                         ret = -ENOSPC;
3384                         goto out_unlock;
3385                 }
3386
3387                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3388                                                1, 1);
3389                 if (ret) {
3390                         IWL_ERR(mvm, "Failed to change PHY context\n");
3391                         goto out_unlock;
3392                 }
3393
3394                 /* Unbind the P2P_DEVICE from the current PHY context */
3395                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
3396                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3397                         goto out_unlock;
3398
3399                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3400
3401                 /* Bind the P2P_DEVICE to the new allocated PHY context */
3402                 mvmvif->phy_ctxt = phy_ctxt;
3403
3404                 ret = iwl_mvm_binding_add_vif(mvm, vif);
3405                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3406                         goto out_unlock;
3407
3408                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3409         }
3410
3411 schedule_time_event:
3412         /* Schedule the time events */
3413         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3414
3415 out_unlock:
3416         mutex_unlock(&mvm->mutex);
3417         IWL_DEBUG_MAC80211(mvm, "leave\n");
3418         return ret;
3419 }
3420
3421 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
3422 {
3423         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3424
3425         IWL_DEBUG_MAC80211(mvm, "enter\n");
3426
3427         mutex_lock(&mvm->mutex);
3428         iwl_mvm_stop_roc(mvm);
3429         mutex_unlock(&mvm->mutex);
3430
3431         IWL_DEBUG_MAC80211(mvm, "leave\n");
3432         return 0;
3433 }
3434
3435 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3436                                  struct ieee80211_chanctx_conf *ctx)
3437 {
3438         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3439         struct iwl_mvm_phy_ctxt *phy_ctxt;
3440         int ret;
3441
3442         lockdep_assert_held(&mvm->mutex);
3443
3444         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3445
3446         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3447         if (!phy_ctxt) {
3448                 ret = -ENOSPC;
3449                 goto out;
3450         }
3451
3452         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
3453                                        ctx->rx_chains_static,
3454                                        ctx->rx_chains_dynamic);
3455         if (ret) {
3456                 IWL_ERR(mvm, "Failed to add PHY context\n");
3457                 goto out;
3458         }
3459
3460         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3461         *phy_ctxt_id = phy_ctxt->id;
3462 out:
3463         return ret;
3464 }
3465
3466 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3467                                struct ieee80211_chanctx_conf *ctx)
3468 {
3469         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3470         int ret;
3471
3472         mutex_lock(&mvm->mutex);
3473         ret = __iwl_mvm_add_chanctx(mvm, ctx);
3474         mutex_unlock(&mvm->mutex);
3475
3476         return ret;
3477 }
3478
3479 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3480                                      struct ieee80211_chanctx_conf *ctx)
3481 {
3482         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3483         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3484
3485         lockdep_assert_held(&mvm->mutex);
3486
3487         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3488 }
3489
3490 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3491                                    struct ieee80211_chanctx_conf *ctx)
3492 {
3493         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3494
3495         mutex_lock(&mvm->mutex);
3496         __iwl_mvm_remove_chanctx(mvm, ctx);
3497         mutex_unlock(&mvm->mutex);
3498 }
3499
3500 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3501                                    struct ieee80211_chanctx_conf *ctx,
3502                                    u32 changed)
3503 {
3504         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3505         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3506         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3507
3508         if (WARN_ONCE((phy_ctxt->ref > 1) &&
3509                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3510                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
3511                                    IEEE80211_CHANCTX_CHANGE_RADAR |
3512                                    IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
3513                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
3514                       phy_ctxt->ref, changed))
3515                 return;
3516
3517         mutex_lock(&mvm->mutex);
3518         iwl_mvm_bt_coex_vif_change(mvm);
3519         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
3520                                  ctx->rx_chains_static,
3521                                  ctx->rx_chains_dynamic);
3522         mutex_unlock(&mvm->mutex);
3523 }
3524
3525 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
3526                                         struct ieee80211_vif *vif,
3527                                         struct ieee80211_chanctx_conf *ctx,
3528                                         bool switching_chanctx)
3529 {
3530         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3531         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3532         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3533         int ret;
3534
3535         lockdep_assert_held(&mvm->mutex);
3536
3537         mvmvif->phy_ctxt = phy_ctxt;
3538
3539         switch (vif->type) {
3540         case NL80211_IFTYPE_AP:
3541                 /* only needed if we're switching chanctx (i.e. during CSA) */
3542                 if (switching_chanctx) {
3543                         mvmvif->ap_ibss_active = true;
3544                         break;
3545                 }
3546         case NL80211_IFTYPE_ADHOC:
3547                 /*
3548                  * The AP binding flow is handled as part of the start_ap flow
3549                  * (in bss_info_changed), similarly for IBSS.
3550                  */
3551                 ret = 0;
3552                 goto out;
3553         case NL80211_IFTYPE_STATION:
3554                 mvmvif->csa_bcn_pending = false;
3555                 break;
3556         case NL80211_IFTYPE_MONITOR:
3557                 /* always disable PS when a monitor interface is active */
3558                 mvmvif->ps_disabled = true;
3559                 break;
3560         default:
3561                 ret = -EINVAL;
3562                 goto out;
3563         }
3564
3565         ret = iwl_mvm_binding_add_vif(mvm, vif);
3566         if (ret)
3567                 goto out;
3568
3569         /*
3570          * Power state must be updated before quotas,
3571          * otherwise fw will complain.
3572          */
3573         iwl_mvm_power_update_mac(mvm);
3574
3575         /* Setting the quota at this stage is only required for monitor
3576          * interfaces. For the other types, the bss_info changed flow
3577          * will handle quota settings.
3578          */
3579         if (vif->type == NL80211_IFTYPE_MONITOR) {
3580                 mvmvif->monitor_active = true;
3581                 ret = iwl_mvm_update_quotas(mvm, false, NULL);
3582                 if (ret)
3583                         goto out_remove_binding;
3584
3585                 ret = iwl_mvm_add_snif_sta(mvm, vif);
3586                 if (ret)
3587                         goto out_remove_binding;
3588
3589         }
3590
3591         /* Handle binding during CSA */
3592         if (vif->type == NL80211_IFTYPE_AP) {
3593                 iwl_mvm_update_quotas(mvm, false, NULL);
3594                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3595         }
3596
3597         if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
3598                 u32 duration = 3 * vif->bss_conf.beacon_int;
3599
3600                 /* iwl_mvm_protect_session() reads directly from the
3601                  * device (the system time), so make sure it is
3602                  * available.
3603                  */
3604                 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_CSA);
3605                 if (ret)
3606                         goto out_remove_binding;
3607
3608                 /* Protect the session to make sure we hear the first
3609                  * beacon on the new channel.
3610                  */
3611                 mvmvif->csa_bcn_pending = true;
3612                 iwl_mvm_protect_session(mvm, vif, duration, duration,
3613                                         vif->bss_conf.beacon_int / 2,
3614                                         true);
3615
3616                 iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_CSA);
3617
3618                 iwl_mvm_update_quotas(mvm, false, NULL);
3619         }
3620
3621         goto out;
3622
3623 out_remove_binding:
3624         iwl_mvm_binding_remove_vif(mvm, vif);
3625         iwl_mvm_power_update_mac(mvm);
3626 out:
3627         if (ret)
3628                 mvmvif->phy_ctxt = NULL;
3629         return ret;
3630 }
3631 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
3632                                       struct ieee80211_vif *vif,
3633                                       struct ieee80211_chanctx_conf *ctx)
3634 {
3635         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3636         int ret;
3637
3638         mutex_lock(&mvm->mutex);
3639         ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
3640         mutex_unlock(&mvm->mutex);
3641
3642         return ret;
3643 }
3644
3645 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
3646                                            struct ieee80211_vif *vif,
3647                                            struct ieee80211_chanctx_conf *ctx,
3648                                            bool switching_chanctx)
3649 {
3650         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3651         struct ieee80211_vif *disabled_vif = NULL;
3652
3653         lockdep_assert_held(&mvm->mutex);
3654
3655         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
3656
3657         switch (vif->type) {
3658         case NL80211_IFTYPE_ADHOC:
3659                 goto out;
3660         case NL80211_IFTYPE_MONITOR:
3661                 mvmvif->monitor_active = false;
3662                 mvmvif->ps_disabled = false;
3663                 iwl_mvm_rm_snif_sta(mvm, vif);
3664                 break;
3665         case NL80211_IFTYPE_AP:
3666                 /* This part is triggered only during CSA */
3667                 if (!switching_chanctx || !mvmvif->ap_ibss_active)
3668                         goto out;
3669
3670                 mvmvif->csa_countdown = false;
3671
3672                 /* Set CS bit on all the stations */
3673                 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
3674
3675                 /* Save blocked iface, the timeout is set on the next beacon */
3676                 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
3677
3678                 mvmvif->ap_ibss_active = false;
3679                 break;
3680         case NL80211_IFTYPE_STATION:
3681                 if (!switching_chanctx)
3682                         break;
3683
3684                 disabled_vif = vif;
3685
3686                 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
3687                 break;
3688         default:
3689                 break;
3690         }
3691
3692         iwl_mvm_update_quotas(mvm, false, disabled_vif);
3693         iwl_mvm_binding_remove_vif(mvm, vif);
3694
3695 out:
3696         mvmvif->phy_ctxt = NULL;
3697         iwl_mvm_power_update_mac(mvm);
3698 }
3699
3700 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
3701                                          struct ieee80211_vif *vif,
3702                                          struct ieee80211_chanctx_conf *ctx)
3703 {
3704         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3705
3706         mutex_lock(&mvm->mutex);
3707         __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
3708         mutex_unlock(&mvm->mutex);
3709 }
3710
3711 static int
3712 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
3713                                 struct ieee80211_vif_chanctx_switch *vifs)
3714 {
3715         int ret;
3716
3717         mutex_lock(&mvm->mutex);
3718         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3719         __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
3720
3721         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
3722         if (ret) {
3723                 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
3724                 goto out_reassign;
3725         }
3726
3727         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3728                                            true);
3729         if (ret) {
3730                 IWL_ERR(mvm,
3731                         "failed to assign new_ctx during channel switch\n");
3732                 goto out_remove;
3733         }
3734
3735         /* we don't support TDLS during DCM - can be caused by channel switch */
3736         if (iwl_mvm_phy_ctx_count(mvm) > 1)
3737                 iwl_mvm_teardown_tdls_peers(mvm);
3738
3739         goto out;
3740
3741 out_remove:
3742         __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
3743
3744 out_reassign:
3745         if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
3746                 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
3747                 goto out_restart;
3748         }
3749
3750         if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3751                                          true)) {
3752                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3753                 goto out_restart;
3754         }
3755
3756         goto out;
3757
3758 out_restart:
3759         /* things keep failing, better restart the hw */
3760         iwl_mvm_nic_restart(mvm, false);
3761
3762 out:
3763         mutex_unlock(&mvm->mutex);
3764
3765         return ret;
3766 }
3767
3768 static int
3769 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
3770                                     struct ieee80211_vif_chanctx_switch *vifs)
3771 {
3772         int ret;
3773
3774         mutex_lock(&mvm->mutex);
3775         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
3776
3777         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
3778                                            true);
3779         if (ret) {
3780                 IWL_ERR(mvm,
3781                         "failed to assign new_ctx during channel switch\n");
3782                 goto out_reassign;
3783         }
3784
3785         goto out;
3786
3787 out_reassign:
3788         if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
3789                                          true)) {
3790                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
3791                 goto out_restart;
3792         }
3793
3794         goto out;
3795
3796 out_restart:
3797         /* things keep failing, better restart the hw */
3798         iwl_mvm_nic_restart(mvm, false);
3799
3800 out:
3801         mutex_unlock(&mvm->mutex);
3802
3803         return ret;
3804 }
3805
3806 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
3807                                       struct ieee80211_vif_chanctx_switch *vifs,
3808                                       int n_vifs,
3809                                       enum ieee80211_chanctx_switch_mode mode)
3810 {
3811         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3812         int ret;
3813
3814         /* we only support a single-vif right now */
3815         if (n_vifs > 1)
3816                 return -EOPNOTSUPP;
3817
3818         switch (mode) {
3819         case CHANCTX_SWMODE_SWAP_CONTEXTS:
3820                 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
3821                 break;
3822         case CHANCTX_SWMODE_REASSIGN_VIF:
3823                 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
3824                 break;
3825         default:
3826                 ret = -EOPNOTSUPP;
3827                 break;
3828         }
3829
3830         return ret;
3831 }
3832
3833 static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
3834 {
3835         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3836
3837         return mvm->ibss_manager;
3838 }
3839
3840 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
3841                            struct ieee80211_sta *sta,
3842                            bool set)
3843 {
3844         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3845         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3846
3847         if (!mvm_sta || !mvm_sta->vif) {
3848                 IWL_ERR(mvm, "Station is not associated to a vif\n");
3849                 return -EINVAL;
3850         }
3851
3852         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
3853 }
3854
3855 #ifdef CONFIG_NL80211_TESTMODE
3856 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
3857         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
3858         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
3859         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
3860 };
3861
3862 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
3863                                       struct ieee80211_vif *vif,
3864                                       void *data, int len)
3865 {
3866         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
3867         int err;
3868         u32 noa_duration;
3869
3870         err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy,
3871                         NULL);
3872         if (err)
3873                 return err;
3874
3875         if (!tb[IWL_MVM_TM_ATTR_CMD])
3876                 return -EINVAL;
3877
3878         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
3879         case IWL_MVM_TM_CMD_SET_NOA:
3880                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
3881                     !vif->bss_conf.enable_beacon ||
3882                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
3883                         return -EINVAL;
3884
3885                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
3886                 if (noa_duration >= vif->bss_conf.beacon_int)
3887                         return -EINVAL;
3888
3889                 mvm->noa_duration = noa_duration;
3890                 mvm->noa_vif = vif;
3891
3892                 return iwl_mvm_update_quotas(mvm, false, NULL);
3893         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
3894                 /* must be associated client vif - ignore authorized */
3895                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
3896                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
3897                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
3898                         return -EINVAL;
3899
3900                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
3901                         return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
3902                 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3903         }
3904
3905         return -EOPNOTSUPP;
3906 }
3907
3908 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
3909                                     struct ieee80211_vif *vif,
3910                                     void *data, int len)
3911 {
3912         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3913         int err;
3914
3915         mutex_lock(&mvm->mutex);
3916         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
3917         mutex_unlock(&mvm->mutex);
3918
3919         return err;
3920 }
3921 #endif
3922
3923 static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
3924                                    struct ieee80211_vif *vif,
3925                                    struct ieee80211_channel_switch *chsw)
3926 {
3927         /* By implementing this operation, we prevent mac80211 from
3928          * starting its own channel switch timer, so that we can call
3929          * ieee80211_chswitch_done() ourselves at the right time
3930          * (which is when the absence time event starts).
3931          */
3932
3933         IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
3934                            "dummy channel switch op\n");
3935 }
3936
3937 static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
3938                                       struct ieee80211_vif *vif,
3939                                       struct ieee80211_channel_switch *chsw)
3940 {
3941         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3942         struct ieee80211_vif *csa_vif;
3943         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3944         u32 apply_time;
3945         int ret;
3946
3947         mutex_lock(&mvm->mutex);
3948
3949         mvmvif->csa_failed = false;
3950
3951         IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
3952                            chsw->chandef.center_freq1);
3953
3954         iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
3955                                        ieee80211_vif_to_wdev(vif),
3956                                        FW_DBG_TRIGGER_CHANNEL_SWITCH);
3957
3958         switch (vif->type) {
3959         case NL80211_IFTYPE_AP:
3960                 csa_vif =
3961                         rcu_dereference_protected(mvm->csa_vif,
3962                                                   lockdep_is_held(&mvm->mutex));
3963                 if (WARN_ONCE(csa_vif && csa_vif->csa_active,
3964                               "Another CSA is already in progress")) {
3965                         ret = -EBUSY;
3966                         goto out_unlock;
3967                 }
3968
3969                 /* we still didn't unblock tx. prevent new CS meanwhile */
3970                 if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
3971                                               lockdep_is_held(&mvm->mutex))) {
3972                         ret = -EBUSY;
3973                         goto out_unlock;
3974                 }
3975
3976                 rcu_assign_pointer(mvm->csa_vif, vif);
3977
3978                 if (WARN_ONCE(mvmvif->csa_countdown,
3979                               "Previous CSA countdown didn't complete")) {
3980                         ret = -EBUSY;
3981                         goto out_unlock;
3982                 }
3983
3984                 mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
3985
3986                 break;
3987         case NL80211_IFTYPE_STATION:
3988                 if (mvmvif->lqm_active)
3989                         iwl_mvm_send_lqm_cmd(vif,
3990                                              LQM_CMD_OPERATION_STOP_MEASUREMENT,
3991                                              0, 0);
3992
3993                 /* Schedule the time event to a bit before beacon 1,
3994                  * to make sure we're in the new channel when the
3995                  * GO/AP arrives. In case count <= 1 immediately schedule the
3996                  * TE (this might result with some packet loss or connection
3997                  * loss).
3998                  */
3999                 if (chsw->count <= 1)
4000                         apply_time = 0;
4001                 else
4002                         apply_time = chsw->device_timestamp +
4003                                 ((vif->bss_conf.beacon_int * (chsw->count - 1) -
4004                                   IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
4005
4006                 if (chsw->block_tx)
4007                         iwl_mvm_csa_client_absent(mvm, vif);
4008
4009                 iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
4010                                             apply_time);
4011                 if (mvmvif->bf_data.bf_enabled) {
4012                         ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4013                         if (ret)
4014                                 goto out_unlock;
4015                 }
4016
4017                 break;
4018         default:
4019                 break;
4020         }
4021
4022         mvmvif->ps_disabled = true;
4023
4024         ret = iwl_mvm_power_update_ps(mvm);
4025         if (ret)
4026                 goto out_unlock;
4027
4028         /* we won't be on this channel any longer */
4029         iwl_mvm_teardown_tdls_peers(mvm);
4030
4031 out_unlock:
4032         mutex_unlock(&mvm->mutex);
4033
4034         return ret;
4035 }
4036
4037 static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
4038                                        struct ieee80211_vif *vif)
4039 {
4040         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4041         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4042         int ret;
4043
4044         mutex_lock(&mvm->mutex);
4045
4046         if (mvmvif->csa_failed) {
4047                 mvmvif->csa_failed = false;
4048                 ret = -EIO;
4049                 goto out_unlock;
4050         }
4051
4052         if (vif->type == NL80211_IFTYPE_STATION) {
4053                 struct iwl_mvm_sta *mvmsta;
4054
4055                 mvmvif->csa_bcn_pending = false;
4056                 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
4057                                                           mvmvif->ap_sta_id);
4058
4059                 if (WARN_ON(!mvmsta)) {
4060                         ret = -EIO;
4061                         goto out_unlock;
4062                 }
4063
4064                 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
4065
4066                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4067
4068                 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4069                 if (ret)
4070                         goto out_unlock;
4071
4072                 iwl_mvm_stop_session_protection(mvm, vif);
4073         }
4074
4075         mvmvif->ps_disabled = false;
4076
4077         ret = iwl_mvm_power_update_ps(mvm);
4078
4079 out_unlock:
4080         mutex_unlock(&mvm->mutex);
4081
4082         return ret;
4083 }
4084
4085 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
4086 {
4087         if (drop) {
4088                 if (iwl_mvm_has_new_tx_api(mvm))
4089                         /* TODO new tx api */
4090                         WARN_ONCE(1,
4091                                   "Need to implement flush TX queue\n");
4092                 else
4093                         iwl_mvm_flush_tx_path(mvm,
4094                                 iwl_mvm_flushable_queues(mvm) & queues,
4095                                 0);
4096         } else {
4097                 if (iwl_mvm_has_new_tx_api(mvm)) {
4098                         struct ieee80211_sta *sta;
4099                         int i;
4100
4101                         mutex_lock(&mvm->mutex);
4102
4103                         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4104                                 sta = rcu_dereference_protected(
4105                                                 mvm->fw_id_to_mac_id[i],
4106                                                 lockdep_is_held(&mvm->mutex));
4107                                 if (IS_ERR_OR_NULL(sta))
4108                                         continue;
4109
4110                                 iwl_mvm_wait_sta_queues_empty(mvm,
4111                                                 iwl_mvm_sta_from_mac80211(sta));
4112                         }
4113
4114                         mutex_unlock(&mvm->mutex);
4115                 } else {
4116                         iwl_trans_wait_tx_queues_empty(mvm->trans,
4117                                                        queues);
4118                 }
4119         }
4120 }
4121
4122 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4123                               struct ieee80211_vif *vif, u32 queues, bool drop)
4124 {
4125         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4126         struct iwl_mvm_vif *mvmvif;
4127         struct iwl_mvm_sta *mvmsta;
4128         struct ieee80211_sta *sta;
4129         int i;
4130         u32 msk = 0;
4131
4132         if (!vif) {
4133                 iwl_mvm_flush_no_vif(mvm, queues, drop);
4134                 return;
4135         }
4136
4137         if (vif->type != NL80211_IFTYPE_STATION)
4138                 return;
4139
4140         /* Make sure we're done with the deferred traffic before flushing */
4141         flush_work(&mvm->add_stream_wk);
4142
4143         mutex_lock(&mvm->mutex);
4144         mvmvif = iwl_mvm_vif_from_mac80211(vif);
4145
4146         /* flush the AP-station and all TDLS peers */
4147         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4148                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4149                                                 lockdep_is_held(&mvm->mutex));
4150                 if (IS_ERR_OR_NULL(sta))
4151                         continue;
4152
4153                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
4154                 if (mvmsta->vif != vif)
4155                         continue;
4156
4157                 /* make sure only TDLS peers or the AP are flushed */
4158                 WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4159
4160                 if (drop) {
4161                         if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4162                                 IWL_ERR(mvm, "flush request fail\n");
4163                 } else {
4164                         msk |= mvmsta->tfd_queue_msk;
4165                         if (iwl_mvm_has_new_tx_api(mvm))
4166                                 iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4167                 }
4168         }
4169
4170         mutex_unlock(&mvm->mutex);
4171
4172         /* this can take a while, and we may need/want other operations
4173          * to succeed while doing this, so do it without the mutex held
4174          */
4175         if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4176                 iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4177 }
4178
4179 static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4180                                   struct survey_info *survey)
4181 {
4182         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4183         int ret;
4184
4185         memset(survey, 0, sizeof(*survey));
4186
4187         /* only support global statistics right now */
4188         if (idx != 0)
4189                 return -ENOENT;
4190
4191         if (!fw_has_capa(&mvm->fw->ucode_capa,
4192                          IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4193                 return -ENOENT;
4194
4195         mutex_lock(&mvm->mutex);
4196
4197         if (iwl_mvm_firmware_running(mvm)) {
4198                 ret = iwl_mvm_request_statistics(mvm, false);
4199                 if (ret)
4200                         goto out;
4201         }
4202
4203         survey->filled = SURVEY_INFO_TIME |
4204                          SURVEY_INFO_TIME_RX |
4205                          SURVEY_INFO_TIME_TX |
4206                          SURVEY_INFO_TIME_SCAN;
4207         survey->time = mvm->accu_radio_stats.on_time_rf +
4208                        mvm->radio_stats.on_time_rf;
4209         do_div(survey->time, USEC_PER_MSEC);
4210
4211         survey->time_rx = mvm->accu_radio_stats.rx_time +
4212                           mvm->radio_stats.rx_time;
4213         do_div(survey->time_rx, USEC_PER_MSEC);
4214
4215         survey->time_tx = mvm->accu_radio_stats.tx_time +
4216                           mvm->radio_stats.tx_time;
4217         do_div(survey->time_tx, USEC_PER_MSEC);
4218
4219         survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4220                             mvm->radio_stats.on_time_scan;
4221         do_div(survey->time_scan, USEC_PER_MSEC);
4222
4223         ret = 0;
4224  out:
4225         mutex_unlock(&mvm->mutex);
4226         return ret;
4227 }
4228
4229 static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4230                                        struct ieee80211_vif *vif,
4231                                        struct ieee80211_sta *sta,
4232                                        struct station_info *sinfo)
4233 {
4234         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4235         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4236         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4237
4238         if (mvmsta->avg_energy) {
4239                 sinfo->signal_avg = mvmsta->avg_energy;
4240                 sinfo->filled |= BIT(NL80211_STA_INFO_SIGNAL_AVG);
4241         }
4242
4243         /* if beacon filtering isn't on mac80211 does it anyway */
4244         if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4245                 return;
4246
4247         if (!vif->bss_conf.assoc)
4248                 return;
4249
4250         mutex_lock(&mvm->mutex);
4251
4252         if (mvmvif->ap_sta_id != mvmsta->sta_id)
4253                 goto unlock;
4254
4255         if (iwl_mvm_request_statistics(mvm, false))
4256                 goto unlock;
4257
4258         sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4259                            mvmvif->beacon_stats.accu_num_beacons;
4260         sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_RX);
4261         if (mvmvif->beacon_stats.avg_signal) {
4262                 /* firmware only reports a value after RXing a few beacons */
4263                 sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
4264                 sinfo->filled |= BIT(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4265         }
4266  unlock:
4267         mutex_unlock(&mvm->mutex);
4268 }
4269
4270 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4271                                         struct ieee80211_vif *vif,
4272                                         const struct ieee80211_event *event)
4273 {
4274 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)                               \
4275         do {                                                            \
4276                 if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))           \
4277                         break;                                          \
4278                 iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);      \
4279         } while (0)
4280
4281         struct iwl_fw_dbg_trigger_tlv *trig;
4282         struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4283
4284         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_MLME))
4285                 return;
4286
4287         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_MLME);
4288         trig_mlme = (void *)trig->data;
4289         if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
4290                                            ieee80211_vif_to_wdev(vif), trig))
4291                 return;
4292
4293         if (event->u.mlme.data == ASSOC_EVENT) {
4294                 if (event->u.mlme.status == MLME_DENIED)
4295                         CHECK_MLME_TRIGGER(stop_assoc_denied,
4296                                            "DENIED ASSOC: reason %d",
4297                                             event->u.mlme.reason);
4298                 else if (event->u.mlme.status == MLME_TIMEOUT)
4299                         CHECK_MLME_TRIGGER(stop_assoc_timeout,
4300                                            "ASSOC TIMEOUT");
4301         } else if (event->u.mlme.data == AUTH_EVENT) {
4302                 if (event->u.mlme.status == MLME_DENIED)
4303                         CHECK_MLME_TRIGGER(stop_auth_denied,
4304                                            "DENIED AUTH: reason %d",
4305                                            event->u.mlme.reason);
4306                 else if (event->u.mlme.status == MLME_TIMEOUT)
4307                         CHECK_MLME_TRIGGER(stop_auth_timeout,
4308                                            "AUTH TIMEOUT");
4309         } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
4310                 CHECK_MLME_TRIGGER(stop_rx_deauth,
4311                                    "DEAUTH RX %d", event->u.mlme.reason);
4312         } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
4313                 CHECK_MLME_TRIGGER(stop_tx_deauth,
4314                                    "DEAUTH TX %d", event->u.mlme.reason);
4315         }
4316 #undef CHECK_MLME_TRIGGER
4317 }
4318
4319 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4320                                           struct ieee80211_vif *vif,
4321                                           const struct ieee80211_event *event)
4322 {
4323         struct iwl_fw_dbg_trigger_tlv *trig;
4324         struct iwl_fw_dbg_trigger_ba *ba_trig;
4325
4326         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
4327                 return;
4328
4329         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
4330         ba_trig = (void *)trig->data;
4331         if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
4332                                            ieee80211_vif_to_wdev(vif), trig))
4333                 return;
4334
4335         if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4336                 return;
4337
4338         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4339                                 "BAR received from %pM, tid %d, ssn %d",
4340                                 event->u.ba.sta->addr, event->u.ba.tid,
4341                                 event->u.ba.ssn);
4342 }
4343
4344 static void
4345 iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
4346                                      struct ieee80211_vif *vif,
4347                                      const struct ieee80211_event *event)
4348 {
4349         struct iwl_fw_dbg_trigger_tlv *trig;
4350         struct iwl_fw_dbg_trigger_ba *ba_trig;
4351
4352         if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_BA))
4353                 return;
4354
4355         trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_BA);
4356         ba_trig = (void *)trig->data;
4357         if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
4358                                            ieee80211_vif_to_wdev(vif), trig))
4359                 return;
4360
4361         if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(event->u.ba.tid)))
4362                 return;
4363
4364         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4365                                 "Frame from %pM timed out, tid %d",
4366                                 event->u.ba.sta->addr, event->u.ba.tid);
4367 }
4368
4369 static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4370                                        struct ieee80211_vif *vif,
4371                                        const struct ieee80211_event *event)
4372 {
4373         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4374
4375         switch (event->type) {
4376         case MLME_EVENT:
4377                 iwl_mvm_event_mlme_callback(mvm, vif, event);
4378                 break;
4379         case BAR_RX_EVENT:
4380                 iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4381                 break;
4382         case BA_FRAME_TIMEOUT:
4383                 iwl_mvm_event_frame_timeout_callback(mvm, vif, event);
4384                 break;
4385         default:
4386                 break;
4387         }
4388 }
4389
4390 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4391                                      struct iwl_mvm_internal_rxq_notif *notif,
4392                                      u32 size)
4393 {
4394         u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
4395         int ret;
4396
4397         lockdep_assert_held(&mvm->mutex);
4398
4399         /* TODO - remove a000 disablement when we have RXQ config API */
4400         if (!iwl_mvm_has_new_rx_api(mvm) ||
4401             mvm->trans->cfg->device_family == IWL_DEVICE_FAMILY_A000)
4402                 return;
4403
4404         notif->cookie = mvm->queue_sync_cookie;
4405
4406         if (notif->sync)
4407                 atomic_set(&mvm->queue_sync_counter,
4408                            mvm->trans->num_rx_queues);
4409
4410         ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif, size);
4411         if (ret) {
4412                 IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
4413                 goto out;
4414         }
4415
4416         if (notif->sync) {
4417                 ret = wait_event_timeout(mvm->rx_sync_waitq,
4418                                          atomic_read(&mvm->queue_sync_counter) == 0 ||
4419                                          iwl_mvm_is_radio_killed(mvm),
4420                                          HZ);
4421                 WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
4422         }
4423
4424 out:
4425         atomic_set(&mvm->queue_sync_counter, 0);
4426         mvm->queue_sync_cookie++;
4427 }
4428
4429 static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
4430 {
4431         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4432         struct iwl_mvm_internal_rxq_notif data = {
4433                 .type = IWL_MVM_RXQ_EMPTY,
4434                 .sync = 1,
4435         };
4436
4437         mutex_lock(&mvm->mutex);
4438         iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
4439         mutex_unlock(&mvm->mutex);
4440 }
4441
4442 const struct ieee80211_ops iwl_mvm_hw_ops = {
4443         .tx = iwl_mvm_mac_tx,
4444         .ampdu_action = iwl_mvm_mac_ampdu_action,
4445         .start = iwl_mvm_mac_start,
4446         .reconfig_complete = iwl_mvm_mac_reconfig_complete,
4447         .stop = iwl_mvm_mac_stop,
4448         .add_interface = iwl_mvm_mac_add_interface,
4449         .remove_interface = iwl_mvm_mac_remove_interface,
4450         .config = iwl_mvm_mac_config,
4451         .prepare_multicast = iwl_mvm_prepare_multicast,
4452         .configure_filter = iwl_mvm_configure_filter,
4453         .config_iface_filter = iwl_mvm_config_iface_filter,
4454         .bss_info_changed = iwl_mvm_bss_info_changed,
4455         .hw_scan = iwl_mvm_mac_hw_scan,
4456         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
4457         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
4458         .sta_state = iwl_mvm_mac_sta_state,
4459         .sta_notify = iwl_mvm_mac_sta_notify,
4460         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
4461         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
4462         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
4463         .sta_rc_update = iwl_mvm_sta_rc_update,
4464         .conf_tx = iwl_mvm_mac_conf_tx,
4465         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
4466         .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
4467         .flush = iwl_mvm_mac_flush,
4468         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
4469         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
4470         .set_key = iwl_mvm_mac_set_key,
4471         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
4472         .remain_on_channel = iwl_mvm_roc,
4473         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
4474         .add_chanctx = iwl_mvm_add_chanctx,
4475         .remove_chanctx = iwl_mvm_remove_chanctx,
4476         .change_chanctx = iwl_mvm_change_chanctx,
4477         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
4478         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
4479         .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
4480
4481         .start_ap = iwl_mvm_start_ap_ibss,
4482         .stop_ap = iwl_mvm_stop_ap_ibss,
4483         .join_ibss = iwl_mvm_start_ap_ibss,
4484         .leave_ibss = iwl_mvm_stop_ap_ibss,
4485
4486         .tx_last_beacon = iwl_mvm_tx_last_beacon,
4487
4488         .set_tim = iwl_mvm_set_tim,
4489
4490         .channel_switch = iwl_mvm_channel_switch,
4491         .pre_channel_switch = iwl_mvm_pre_channel_switch,
4492         .post_channel_switch = iwl_mvm_post_channel_switch,
4493
4494         .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
4495         .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
4496         .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
4497
4498         .event_callback = iwl_mvm_mac_event_callback,
4499
4500         .sync_rx_queues = iwl_mvm_sync_rx_queues,
4501
4502         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
4503
4504 #ifdef CONFIG_PM_SLEEP
4505         /* look at d3.c */
4506         .suspend = iwl_mvm_suspend,
4507         .resume = iwl_mvm_resume,
4508         .set_wakeup = iwl_mvm_set_wakeup,
4509         .set_rekey_data = iwl_mvm_set_rekey_data,
4510 #if IS_ENABLED(CONFIG_IPV6)
4511         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
4512 #endif
4513         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
4514 #endif
4515         .get_survey = iwl_mvm_mac_get_survey,
4516         .sta_statistics = iwl_mvm_mac_sta_statistics,
4517 };