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