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