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