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