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