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