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