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