OSDN Git Service

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