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