OSDN Git Service

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