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