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