]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/mvm/mac80211.c
iwlwifi: mvm: add option that allows a vif to disable PS
[karo-tx-linux.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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63 #include <linux/kernel.h>
64 #include <linux/slab.h>
65 #include <linux/skbuff.h>
66 #include <linux/netdevice.h>
67 #include <linux/etherdevice.h>
68 #include <linux/ip.h>
69 #include <linux/if_arp.h>
70 #include <net/mac80211.h>
71 #include <net/ieee80211_radiotap.h>
72 #include <net/tcp.h>
73
74 #include "iwl-op-mode.h"
75 #include "iwl-io.h"
76 #include "mvm.h"
77 #include "sta.h"
78 #include "time-event.h"
79 #include "iwl-eeprom-parse.h"
80 #include "fw-api-scan.h"
81 #include "iwl-phy-db.h"
82 #include "testmode.h"
83 #include "iwl-fw-error-dump.h"
84 #include "iwl-prph.h"
85
86 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
87         {
88                 .max = 1,
89                 .types = BIT(NL80211_IFTYPE_STATION),
90         },
91         {
92                 .max = 1,
93                 .types = BIT(NL80211_IFTYPE_AP) |
94                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
95                         BIT(NL80211_IFTYPE_P2P_GO),
96         },
97         {
98                 .max = 1,
99                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
100         },
101 };
102
103 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
104         {
105                 .num_different_channels = 1,
106                 .max_interfaces = 3,
107                 .limits = iwl_mvm_limits,
108                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
109         },
110 };
111
112 #ifdef CONFIG_PM_SLEEP
113 static const struct nl80211_wowlan_tcp_data_token_feature
114 iwl_mvm_wowlan_tcp_token_feature = {
115         .min_len = 0,
116         .max_len = 255,
117         .bufsize = IWL_WOWLAN_REMOTE_WAKE_MAX_TOKENS,
118 };
119
120 static const struct wiphy_wowlan_tcp_support iwl_mvm_wowlan_tcp_support = {
121         .tok = &iwl_mvm_wowlan_tcp_token_feature,
122         .data_payload_max = IWL_WOWLAN_TCP_MAX_PACKET_LEN -
123                             sizeof(struct ethhdr) -
124                             sizeof(struct iphdr) -
125                             sizeof(struct tcphdr),
126         .data_interval_max = 65535, /* __le16 in API */
127         .wake_payload_max = IWL_WOWLAN_REMOTE_WAKE_MAX_PACKET_LEN -
128                             sizeof(struct ethhdr) -
129                             sizeof(struct iphdr) -
130                             sizeof(struct tcphdr),
131         .seq = true,
132 };
133 #endif
134
135 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
136 /*
137  * Use the reserved field to indicate magic values.
138  * these values will only be used internally by the driver,
139  * and won't make it to the fw (reserved will be 0).
140  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
141  *      be the vif's ip address. in case there is not a single
142  *      ip address (0, or more than 1), this attribute will
143  *      be skipped.
144  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
145  *      the LSB bytes of the vif's mac address
146  */
147 enum {
148         BC_FILTER_MAGIC_NONE = 0,
149         BC_FILTER_MAGIC_IP,
150         BC_FILTER_MAGIC_MAC,
151 };
152
153 static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
154         {
155                 /* arp */
156                 .discard = 0,
157                 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
158                 .attrs = {
159                         {
160                                 /* frame type - arp, hw type - ethernet */
161                                 .offset_type =
162                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
163                                 .offset = sizeof(rfc1042_header),
164                                 .val = cpu_to_be32(0x08060001),
165                                 .mask = cpu_to_be32(0xffffffff),
166                         },
167                         {
168                                 /* arp dest ip */
169                                 .offset_type =
170                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
171                                 .offset = sizeof(rfc1042_header) + 2 +
172                                           sizeof(struct arphdr) +
173                                           ETH_ALEN + sizeof(__be32) +
174                                           ETH_ALEN,
175                                 .mask = cpu_to_be32(0xffffffff),
176                                 /* mark it as special field */
177                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
178                         },
179                 },
180         },
181         {
182                 /* dhcp offer bcast */
183                 .discard = 0,
184                 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
185                 .attrs = {
186                         {
187                                 /* udp dest port - 68 (bootp client)*/
188                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
189                                 .offset = offsetof(struct udphdr, dest),
190                                 .val = cpu_to_be32(0x00440000),
191                                 .mask = cpu_to_be32(0xffff0000),
192                         },
193                         {
194                                 /* dhcp - lsb bytes of client hw address */
195                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
196                                 .offset = 38,
197                                 .mask = cpu_to_be32(0xffffffff),
198                                 /* mark it as special field */
199                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
200                         },
201                 },
202         },
203         /* last filter must be empty */
204         {},
205 };
206 #endif
207
208 void iwl_mvm_ref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
209 {
210         if (!iwl_mvm_is_d0i3_supported(mvm))
211                 return;
212
213         IWL_DEBUG_RPM(mvm, "Take mvm reference - type %d\n", ref_type);
214         spin_lock_bh(&mvm->refs_lock);
215         mvm->refs[ref_type]++;
216         spin_unlock_bh(&mvm->refs_lock);
217         iwl_trans_ref(mvm->trans);
218 }
219
220 void iwl_mvm_unref(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
221 {
222         if (!iwl_mvm_is_d0i3_supported(mvm))
223                 return;
224
225         IWL_DEBUG_RPM(mvm, "Leave mvm reference - type %d\n", ref_type);
226         spin_lock_bh(&mvm->refs_lock);
227         WARN_ON(!mvm->refs[ref_type]--);
228         spin_unlock_bh(&mvm->refs_lock);
229         iwl_trans_unref(mvm->trans);
230 }
231
232 static void iwl_mvm_unref_all_except(struct iwl_mvm *mvm,
233                                      enum iwl_mvm_ref_type except_ref)
234 {
235         int i, j;
236
237         if (!iwl_mvm_is_d0i3_supported(mvm))
238                 return;
239
240         spin_lock_bh(&mvm->refs_lock);
241         for (i = 0; i < IWL_MVM_REF_COUNT; i++) {
242                 if (except_ref == i || !mvm->refs[i])
243                         continue;
244
245                 IWL_DEBUG_RPM(mvm, "Cleanup: remove mvm ref type %d (%d)\n",
246                               i, mvm->refs[i]);
247                 for (j = 0; j < mvm->refs[i]; j++)
248                         iwl_trans_unref(mvm->trans);
249                 mvm->refs[i] = 0;
250         }
251         spin_unlock_bh(&mvm->refs_lock);
252 }
253
254 int iwl_mvm_ref_sync(struct iwl_mvm *mvm, enum iwl_mvm_ref_type ref_type)
255 {
256         iwl_mvm_ref(mvm, ref_type);
257
258         if (!wait_event_timeout(mvm->d0i3_exit_waitq,
259                                 !test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status),
260                                 HZ)) {
261                 WARN_ON_ONCE(1);
262                 iwl_mvm_unref(mvm, ref_type);
263                 return -EIO;
264         }
265
266         return 0;
267 }
268
269 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
270 {
271         int i;
272
273         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
274         for (i = 0; i < NUM_PHY_CTX; i++) {
275                 mvm->phy_ctxts[i].id = i;
276                 mvm->phy_ctxts[i].ref = 0;
277         }
278 }
279
280 static int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm)
281 {
282         /* we create the 802.11 header and SSID element */
283         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID)
284                 return mvm->fw->ucode_capa.max_probe_length - 24 - 2;
285         return mvm->fw->ucode_capa.max_probe_length - 24 - 34;
286 }
287
288 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
289 {
290         struct ieee80211_hw *hw = mvm->hw;
291         int num_mac, ret, i;
292
293         /* Tell mac80211 our characteristics */
294         hw->flags = IEEE80211_HW_SIGNAL_DBM |
295                     IEEE80211_HW_SPECTRUM_MGMT |
296                     IEEE80211_HW_REPORTS_TX_ACK_STATUS |
297                     IEEE80211_HW_QUEUE_CONTROL |
298                     IEEE80211_HW_WANT_MONITOR_VIF |
299                     IEEE80211_HW_SUPPORTS_PS |
300                     IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
301                     IEEE80211_HW_AMPDU_AGGREGATION |
302                     IEEE80211_HW_TIMING_BEACON_ONLY |
303                     IEEE80211_HW_CONNECTION_MONITOR |
304                     IEEE80211_HW_CHANCTX_STA_CSA |
305                     IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
306                     IEEE80211_HW_SUPPORTS_STATIC_SMPS;
307
308         hw->queues = mvm->first_agg_queue;
309         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
310         hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
311                                     IEEE80211_RADIOTAP_MCS_HAVE_STBC;
312         hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC;
313         hw->rate_control_algorithm = "iwl-mvm-rs";
314
315         /*
316          * Enable 11w if advertised by firmware and software crypto
317          * is not enabled (as the firmware will interpret some mgmt
318          * packets, so enabling it with software crypto isn't safe)
319          */
320         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
321             !iwlwifi_mod_params.sw_crypto)
322                 hw->flags |= IEEE80211_HW_MFP_CAPABLE;
323
324         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT &&
325             IWL_UCODE_API(mvm->fw->ucode_ver) >= 9 &&
326             !iwlwifi_mod_params.uapsd_disable) {
327                 hw->flags |= IEEE80211_HW_SUPPORTS_UAPSD;
328                 hw->uapsd_queues = IWL_UAPSD_AC_INFO;
329                 hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
330         }
331
332         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
333                 hw->flags |= IEEE80211_SINGLE_HW_SCAN_ON_ALL_BANDS;
334
335         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
336         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
337         hw->chanctx_data_size = sizeof(u16);
338
339         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
340                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
341                 BIT(NL80211_IFTYPE_AP) |
342                 BIT(NL80211_IFTYPE_P2P_GO) |
343                 BIT(NL80211_IFTYPE_P2P_DEVICE) |
344                 BIT(NL80211_IFTYPE_ADHOC);
345
346         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
347         hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
348                                        REGULATORY_DISABLE_BEACON_HINTS;
349
350         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_GO_UAPSD)
351                 hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
352
353         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_CSA_FLOW)
354                 hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
355
356         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
357         hw->wiphy->n_iface_combinations =
358                 ARRAY_SIZE(iwl_mvm_iface_combinations);
359
360         hw->wiphy->max_remain_on_channel_duration = 10000;
361         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
362
363         /* Extract MAC address */
364         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
365         hw->wiphy->addresses = mvm->addresses;
366         hw->wiphy->n_addresses = 1;
367
368         /* Extract additional MAC addresses if available */
369         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
370                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
371
372         for (i = 1; i < num_mac; i++) {
373                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
374                        ETH_ALEN);
375                 mvm->addresses[i].addr[5]++;
376                 hw->wiphy->n_addresses++;
377         }
378
379         iwl_mvm_reset_phy_ctxts(mvm);
380
381         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
382
383         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
384
385         if (mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
386                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
387                         &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
388         if (mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
389                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
390                         &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
391
392         hw->wiphy->hw_version = mvm->trans->hw_id;
393
394         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
395                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
396         else
397                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
398
399         /* TODO: enable that only for firmwares that don't crash */
400         /* hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN; */
401         hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
402         hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
403         /* we create the 802.11 header and zero length SSID IE. */
404         hw->wiphy->max_sched_scan_ie_len = SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
405
406         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
407                                NL80211_FEATURE_LOW_PRIORITY_SCAN |
408                                NL80211_FEATURE_P2P_GO_OPPPS;
409
410         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
411
412         /* currently FW API supports only one optional cipher scheme */
413         if (mvm->fw->cs[0].cipher) {
414                 mvm->hw->n_cipher_schemes = 1;
415                 mvm->hw->cipher_schemes = &mvm->fw->cs[0];
416         }
417
418 #ifdef CONFIG_PM_SLEEP
419         if (iwl_mvm_is_d0i3_supported(mvm) &&
420             device_can_wakeup(mvm->trans->dev)) {
421                 mvm->wowlan.flags = WIPHY_WOWLAN_ANY;
422                 hw->wiphy->wowlan = &mvm->wowlan;
423         } else if (mvm->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
424             mvm->trans->ops->d3_suspend &&
425             mvm->trans->ops->d3_resume &&
426             device_can_wakeup(mvm->trans->dev)) {
427                 mvm->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
428                                     WIPHY_WOWLAN_DISCONNECT |
429                                     WIPHY_WOWLAN_EAP_IDENTITY_REQ |
430                                     WIPHY_WOWLAN_RFKILL_RELEASE;
431                 if (!iwlwifi_mod_params.sw_crypto)
432                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
433                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
434                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
435
436                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
437                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
438                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
439                 mvm->wowlan.tcp = &iwl_mvm_wowlan_tcp_support;
440                 hw->wiphy->wowlan = &mvm->wowlan;
441         }
442 #endif
443
444 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
445         /* assign default bcast filtering configuration */
446         mvm->bcast_filters = iwl_mvm_default_bcast_filters;
447 #endif
448
449         ret = iwl_mvm_leds_init(mvm);
450         if (ret)
451                 return ret;
452
453         ret = ieee80211_register_hw(mvm->hw);
454         if (ret)
455                 iwl_mvm_leds_exit(mvm);
456
457         return ret;
458 }
459
460 static bool iwl_mvm_defer_tx(struct iwl_mvm *mvm,
461                              struct ieee80211_sta *sta,
462                              struct sk_buff *skb)
463 {
464         struct iwl_mvm_sta *mvmsta;
465         bool defer = false;
466
467         /*
468          * double check the IN_D0I3 flag both before and after
469          * taking the spinlock, in order to prevent taking
470          * the spinlock when not needed.
471          */
472         if (likely(!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)))
473                 return false;
474
475         spin_lock(&mvm->d0i3_tx_lock);
476         /*
477          * testing the flag again ensures the skb dequeue
478          * loop (on d0i3 exit) hasn't run yet.
479          */
480         if (!test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status))
481                 goto out;
482
483         mvmsta = iwl_mvm_sta_from_mac80211(sta);
484         if (mvmsta->sta_id == IWL_MVM_STATION_COUNT ||
485             mvmsta->sta_id != mvm->d0i3_ap_sta_id)
486                 goto out;
487
488         __skb_queue_tail(&mvm->d0i3_tx, skb);
489         ieee80211_stop_queues(mvm->hw);
490
491         /* trigger wakeup */
492         iwl_mvm_ref(mvm, IWL_MVM_REF_TX);
493         iwl_mvm_unref(mvm, IWL_MVM_REF_TX);
494
495         defer = true;
496 out:
497         spin_unlock(&mvm->d0i3_tx_lock);
498         return defer;
499 }
500
501 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
502                            struct ieee80211_tx_control *control,
503                            struct sk_buff *skb)
504 {
505         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
506         struct ieee80211_sta *sta = control->sta;
507         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
508         struct ieee80211_hdr *hdr = (void *)skb->data;
509
510         if (iwl_mvm_is_radio_killed(mvm)) {
511                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
512                 goto drop;
513         }
514
515         if (IEEE80211_SKB_CB(skb)->hw_queue == IWL_MVM_OFFCHANNEL_QUEUE &&
516             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status))
517                 goto drop;
518
519         /* treat non-bufferable MMPDUs as broadcast if sta is sleeping */
520         if (unlikely(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER &&
521                      ieee80211_is_mgmt(hdr->frame_control) &&
522                      !ieee80211_is_deauth(hdr->frame_control) &&
523                      !ieee80211_is_disassoc(hdr->frame_control) &&
524                      !ieee80211_is_action(hdr->frame_control)))
525                 sta = NULL;
526
527         if (sta) {
528                 if (iwl_mvm_defer_tx(mvm, sta, skb))
529                         return;
530                 if (iwl_mvm_tx_skb(mvm, skb, sta))
531                         goto drop;
532                 return;
533         }
534
535         if (iwl_mvm_tx_skb_non_sta(mvm, skb))
536                 goto drop;
537         return;
538  drop:
539         ieee80211_free_txskb(hw, skb);
540 }
541
542 static inline bool iwl_enable_rx_ampdu(const struct iwl_cfg *cfg)
543 {
544         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
545                 return false;
546         return true;
547 }
548
549 static inline bool iwl_enable_tx_ampdu(const struct iwl_cfg *cfg)
550 {
551         if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
552                 return false;
553         if (iwlwifi_mod_params.disable_11n & IWL_ENABLE_HT_TXAGG)
554                 return true;
555
556         /* enabled by default */
557         return true;
558 }
559
560 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
561                                     struct ieee80211_vif *vif,
562                                     enum ieee80211_ampdu_mlme_action action,
563                                     struct ieee80211_sta *sta, u16 tid,
564                                     u16 *ssn, u8 buf_size)
565 {
566         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
567         int ret;
568         bool tx_agg_ref = false;
569
570         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
571                      sta->addr, tid, action);
572
573         if (!(mvm->nvm_data->sku_cap_11n_enable))
574                 return -EACCES;
575
576         /* return from D0i3 before starting a new Tx aggregation */
577         switch (action) {
578         case IEEE80211_AMPDU_TX_START:
579         case IEEE80211_AMPDU_TX_STOP_CONT:
580         case IEEE80211_AMPDU_TX_STOP_FLUSH:
581         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
582         case IEEE80211_AMPDU_TX_OPERATIONAL:
583                 /*
584                  * for tx start, wait synchronously until D0i3 exit to
585                  * get the correct sequence number for the tid.
586                  * additionally, some other ampdu actions use direct
587                  * target access, which is not handled automatically
588                  * by the trans layer (unlike commands), so wait for
589                  * d0i3 exit in these cases as well.
590                  */
591                 ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_TX_AGG);
592                 if (ret)
593                         return ret;
594
595                 tx_agg_ref = true;
596                 break;
597         default:
598                 break;
599         }
600
601         mutex_lock(&mvm->mutex);
602
603         switch (action) {
604         case IEEE80211_AMPDU_RX_START:
605                 if (!iwl_enable_rx_ampdu(mvm->cfg)) {
606                         ret = -EINVAL;
607                         break;
608                 }
609                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true);
610                 break;
611         case IEEE80211_AMPDU_RX_STOP:
612                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false);
613                 break;
614         case IEEE80211_AMPDU_TX_START:
615                 if (!iwl_enable_tx_ampdu(mvm->cfg)) {
616                         ret = -EINVAL;
617                         break;
618                 }
619                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
620                 break;
621         case IEEE80211_AMPDU_TX_STOP_CONT:
622                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
623                 break;
624         case IEEE80211_AMPDU_TX_STOP_FLUSH:
625         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
626                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
627                 break;
628         case IEEE80211_AMPDU_TX_OPERATIONAL:
629                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid, buf_size);
630                 break;
631         default:
632                 WARN_ON_ONCE(1);
633                 ret = -EINVAL;
634                 break;
635         }
636         mutex_unlock(&mvm->mutex);
637
638         /*
639          * If the tid is marked as started, we won't use it for offloaded
640          * traffic on the next D0i3 entry. It's safe to unref.
641          */
642         if (tx_agg_ref)
643                 iwl_mvm_unref(mvm, IWL_MVM_REF_TX_AGG);
644
645         return ret;
646 }
647
648 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
649                                      struct ieee80211_vif *vif)
650 {
651         struct iwl_mvm *mvm = data;
652         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
653
654         mvmvif->uploaded = false;
655         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
656
657         /* does this make sense at all? */
658         mvmvif->color++;
659
660         spin_lock_bh(&mvm->time_event_lock);
661         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
662         spin_unlock_bh(&mvm->time_event_lock);
663
664         mvmvif->phy_ctxt = NULL;
665         memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
666 }
667
668 #ifdef CONFIG_IWLWIFI_DEBUGFS
669 static void iwl_mvm_fw_error_dump(struct iwl_mvm *mvm)
670 {
671         struct iwl_fw_error_dump_file *dump_file;
672         struct iwl_fw_error_dump_data *dump_data;
673         struct iwl_fw_error_dump_info *dump_info;
674         struct iwl_mvm_dump_ptrs *fw_error_dump;
675         const struct fw_img *img;
676         u32 sram_len, sram_ofs;
677         u32 file_len, rxf_len;
678         unsigned long flags;
679         int reg_val;
680
681         lockdep_assert_held(&mvm->mutex);
682
683         if (mvm->fw_error_dump)
684                 return;
685
686         fw_error_dump = kzalloc(sizeof(*mvm->fw_error_dump), GFP_KERNEL);
687         if (!fw_error_dump)
688                 return;
689
690         img = &mvm->fw->img[mvm->cur_ucode];
691         sram_ofs = img->sec[IWL_UCODE_SECTION_DATA].offset;
692         sram_len = img->sec[IWL_UCODE_SECTION_DATA].len;
693
694         /* reading buffer size */
695         reg_val = iwl_trans_read_prph(mvm->trans, RXF_SIZE_ADDR);
696         rxf_len = (reg_val & RXF_SIZE_BYTE_CNT_MSK) >> RXF_SIZE_BYTE_CND_POS;
697
698         /* the register holds the value divided by 128 */
699         rxf_len = rxf_len << 7;
700
701         file_len = sizeof(*dump_file) +
702                    sizeof(*dump_data) * 3 +
703                    sram_len +
704                    rxf_len +
705                    sizeof(*dump_info);
706
707         dump_file = vzalloc(file_len);
708         if (!dump_file) {
709                 kfree(fw_error_dump);
710                 return;
711         }
712
713         fw_error_dump->op_mode_ptr = dump_file;
714
715         dump_file->barker = cpu_to_le32(IWL_FW_ERROR_DUMP_BARKER);
716         dump_data = (void *)dump_file->data;
717
718         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_DEV_FW_INFO);
719         dump_data->len = cpu_to_le32(sizeof(*dump_info));
720         dump_info = (void *) dump_data->data;
721         dump_info->device_family =
722                 mvm->cfg->device_family == IWL_DEVICE_FAMILY_7000 ?
723                         cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_7) :
724                         cpu_to_le32(IWL_FW_ERROR_DUMP_FAMILY_8);
725         memcpy(dump_info->fw_human_readable, mvm->fw->human_readable,
726                sizeof(dump_info->fw_human_readable));
727         strncpy(dump_info->dev_human_readable, mvm->cfg->name,
728                 sizeof(dump_info->dev_human_readable));
729         strncpy(dump_info->bus_human_readable, mvm->dev->bus->name,
730                 sizeof(dump_info->bus_human_readable));
731
732         dump_data = iwl_fw_error_next_data(dump_data);
733         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_RXF);
734         dump_data->len = cpu_to_le32(rxf_len);
735
736         if (iwl_trans_grab_nic_access(mvm->trans, false, &flags)) {
737                 u32 *rxf = (void *)dump_data->data;
738                 int i;
739
740                 for (i = 0; i < (rxf_len / sizeof(u32)); i++) {
741                         iwl_trans_write_prph(mvm->trans,
742                                              RXF_LD_FENCE_OFFSET_ADDR,
743                                              i * sizeof(u32));
744                         rxf[i] = iwl_trans_read_prph(mvm->trans,
745                                                      RXF_FIFO_RD_FENCE_ADDR);
746                 }
747                 iwl_trans_release_nic_access(mvm->trans, &flags);
748         }
749
750         dump_data = iwl_fw_error_next_data(dump_data);
751         dump_data->type = cpu_to_le32(IWL_FW_ERROR_DUMP_SRAM);
752         dump_data->len = cpu_to_le32(sram_len);
753         iwl_trans_read_mem_bytes(mvm->trans, sram_ofs, dump_data->data,
754                                  sram_len);
755
756         fw_error_dump->trans_ptr = iwl_trans_dump_data(mvm->trans);
757         fw_error_dump->op_mode_len = file_len;
758         if (fw_error_dump->trans_ptr)
759                 file_len += fw_error_dump->trans_ptr->len;
760         dump_file->file_len = cpu_to_le32(file_len);
761         mvm->fw_error_dump = fw_error_dump;
762 }
763 #endif
764
765 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
766 {
767 #ifdef CONFIG_IWLWIFI_DEBUGFS
768         static char *env[] = { "DRIVER=iwlwifi", "EVENT=error_dump", NULL };
769
770         iwl_mvm_fw_error_dump(mvm);
771
772         /* notify the userspace about the error we had */
773         kobject_uevent_env(&mvm->hw->wiphy->dev.kobj, KOBJ_CHANGE, env);
774 #endif
775
776         iwl_trans_stop_device(mvm->trans);
777
778         mvm->scan_status = IWL_MVM_SCAN_NONE;
779         mvm->ps_disabled = false;
780
781         /* just in case one was running */
782         ieee80211_remain_on_channel_expired(mvm->hw);
783
784         ieee80211_iterate_active_interfaces_atomic(
785                 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
786                 iwl_mvm_cleanup_iterator, mvm);
787
788         mvm->p2p_device_vif = NULL;
789         mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
790
791         iwl_mvm_reset_phy_ctxts(mvm);
792         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
793         memset(mvm->sta_drained, 0, sizeof(mvm->sta_drained));
794         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
795         memset(&mvm->last_bt_notif_old, 0, sizeof(mvm->last_bt_notif_old));
796         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
797         memset(&mvm->last_bt_ci_cmd_old, 0, sizeof(mvm->last_bt_ci_cmd_old));
798         memset(&mvm->bt_ack_kill_msk, 0, sizeof(mvm->bt_ack_kill_msk));
799         memset(&mvm->bt_cts_kill_msk, 0, sizeof(mvm->bt_cts_kill_msk));
800
801         ieee80211_wake_queues(mvm->hw);
802
803         /* cleanup all stale references (scan, roc), but keep the
804          * ucode_down ref until reconfig is complete */
805         iwl_mvm_unref_all_except(mvm, IWL_MVM_REF_UCODE_DOWN);
806
807         /* clear any stale d0i3 state */
808         clear_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status);
809
810         mvm->vif_count = 0;
811         mvm->rx_ba_sessions = 0;
812 }
813
814 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
815 {
816         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
817         int ret;
818
819         mutex_lock(&mvm->mutex);
820
821         /* Clean up some internal and mac80211 state on restart */
822         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
823                 iwl_mvm_restart_cleanup(mvm);
824
825         ret = iwl_mvm_up(mvm);
826
827         if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
828                 /* Something went wrong - we need to finish some cleanup
829                  * that normally iwl_mvm_mac_restart_complete() below
830                  * would do.
831                  */
832                 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
833                 iwl_mvm_d0i3_enable_tx(mvm, NULL);
834         }
835
836         mutex_unlock(&mvm->mutex);
837
838         return ret;
839 }
840
841 static void iwl_mvm_mac_restart_complete(struct ieee80211_hw *hw)
842 {
843         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
844         int ret;
845
846         mutex_lock(&mvm->mutex);
847
848         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
849         iwl_mvm_d0i3_enable_tx(mvm, NULL);
850         ret = iwl_mvm_update_quotas(mvm, NULL);
851         if (ret)
852                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
853                         ret);
854
855         /* allow transport/FW low power modes */
856         iwl_mvm_unref(mvm, IWL_MVM_REF_UCODE_DOWN);
857
858         mutex_unlock(&mvm->mutex);
859 }
860
861 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
862 {
863         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
864
865         flush_work(&mvm->d0i3_exit_work);
866         flush_work(&mvm->async_handlers_wk);
867
868         mutex_lock(&mvm->mutex);
869
870         /* disallow low power states when the FW is down */
871         iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
872
873         /* async_handlers_wk is now blocked */
874
875         /*
876          * The work item could be running or queued if the
877          * ROC time event stops just as we get here.
878          */
879         cancel_work_sync(&mvm->roc_done_wk);
880
881         iwl_trans_stop_device(mvm->trans);
882
883         iwl_mvm_async_handlers_purge(mvm);
884         /* async_handlers_list is empty and will stay empty: HW is stopped */
885
886         /* the fw is stopped, the aux sta is dead: clean up driver state */
887         iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
888
889         mutex_unlock(&mvm->mutex);
890
891         /*
892          * The worker might have been waiting for the mutex, let it run and
893          * discover that its list is now empty.
894          */
895         cancel_work_sync(&mvm->async_handlers_wk);
896 }
897
898 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
899 {
900         u16 i;
901
902         lockdep_assert_held(&mvm->mutex);
903
904         for (i = 0; i < NUM_PHY_CTX; i++)
905                 if (!mvm->phy_ctxts[i].ref)
906                         return &mvm->phy_ctxts[i];
907
908         IWL_ERR(mvm, "No available PHY context\n");
909         return NULL;
910 }
911
912 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
913                                 s8 tx_power)
914 {
915         /* FW is in charge of regulatory enforcement */
916         struct iwl_reduce_tx_power_cmd reduce_txpwr_cmd = {
917                 .mac_context_id = iwl_mvm_vif_from_mac80211(vif)->id,
918                 .pwr_restriction = cpu_to_le16(tx_power),
919         };
920
921         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0,
922                                     sizeof(reduce_txpwr_cmd),
923                                     &reduce_txpwr_cmd);
924 }
925
926 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
927                                      struct ieee80211_vif *vif)
928 {
929         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
930         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
931         int ret;
932
933         /*
934          * make sure D0i3 exit is completed, otherwise a target access
935          * during tx queue configuration could be done when still in
936          * D0i3 state.
937          */
938         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_ADD_IF);
939         if (ret)
940                 return ret;
941
942         /*
943          * Not much to do here. The stack will not allow interface
944          * types or combinations that we didn't advertise, so we
945          * don't really have to check the types.
946          */
947
948         mutex_lock(&mvm->mutex);
949
950         /* Allocate resources for the MAC context, and add it to the fw  */
951         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
952         if (ret)
953                 goto out_unlock;
954
955         /* Counting number of interfaces is needed for legacy PM */
956         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
957                 mvm->vif_count++;
958
959         /*
960          * The AP binding flow can be done only after the beacon
961          * template is configured (which happens only in the mac80211
962          * start_ap() flow), and adding the broadcast station can happen
963          * only after the binding.
964          * In addition, since modifying the MAC before adding a bcast
965          * station is not allowed by the FW, delay the adding of MAC context to
966          * the point where we can also add the bcast station.
967          * In short: there's not much we can do at this point, other than
968          * allocating resources :)
969          */
970         if (vif->type == NL80211_IFTYPE_AP ||
971             vif->type == NL80211_IFTYPE_ADHOC) {
972                 u32 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
973                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta,
974                                                qmask,
975                                                ieee80211_vif_type_p2p(vif));
976                 if (ret) {
977                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
978                         goto out_release;
979                 }
980
981                 iwl_mvm_vif_dbgfs_register(mvm, vif);
982                 goto out_unlock;
983         }
984
985         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
986         if (ret)
987                 goto out_release;
988
989         ret = iwl_mvm_power_update_mac(mvm);
990         if (ret)
991                 goto out_release;
992
993         /* beacon filtering */
994         ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
995         if (ret)
996                 goto out_remove_mac;
997
998         if (!mvm->bf_allowed_vif &&
999             vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1000                 mvm->bf_allowed_vif = mvmvif;
1001                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1002                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1003         }
1004
1005         /*
1006          * P2P_DEVICE interface does not have a channel context assigned to it,
1007          * so a dedicated PHY context is allocated to it and the corresponding
1008          * MAC context is bound to it at this stage.
1009          */
1010         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1011
1012                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1013                 if (!mvmvif->phy_ctxt) {
1014                         ret = -ENOSPC;
1015                         goto out_free_bf;
1016                 }
1017
1018                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1019                 ret = iwl_mvm_binding_add_vif(mvm, vif);
1020                 if (ret)
1021                         goto out_unref_phy;
1022
1023                 ret = iwl_mvm_add_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1024                 if (ret)
1025                         goto out_unbind;
1026
1027                 /* Save a pointer to p2p device vif, so it can later be used to
1028                  * update the p2p device MAC when a GO is started/stopped */
1029                 mvm->p2p_device_vif = vif;
1030         }
1031
1032         iwl_mvm_vif_dbgfs_register(mvm, vif);
1033         goto out_unlock;
1034
1035  out_unbind:
1036         iwl_mvm_binding_remove_vif(mvm, vif);
1037  out_unref_phy:
1038         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1039  out_free_bf:
1040         if (mvm->bf_allowed_vif == mvmvif) {
1041                 mvm->bf_allowed_vif = NULL;
1042                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1043                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1044         }
1045  out_remove_mac:
1046         mvmvif->phy_ctxt = NULL;
1047         iwl_mvm_mac_ctxt_remove(mvm, vif);
1048  out_release:
1049         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1050                 mvm->vif_count--;
1051
1052         iwl_mvm_mac_ctxt_release(mvm, vif);
1053  out_unlock:
1054         mutex_unlock(&mvm->mutex);
1055
1056         iwl_mvm_unref(mvm, IWL_MVM_REF_ADD_IF);
1057
1058         return ret;
1059 }
1060
1061 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1062                                         struct ieee80211_vif *vif)
1063 {
1064         u32 tfd_msk = 0, ac;
1065
1066         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1067                 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
1068                         tfd_msk |= BIT(vif->hw_queue[ac]);
1069
1070         if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
1071                 tfd_msk |= BIT(vif->cab_queue);
1072
1073         if (tfd_msk) {
1074                 mutex_lock(&mvm->mutex);
1075                 iwl_mvm_flush_tx_path(mvm, tfd_msk, true);
1076                 mutex_unlock(&mvm->mutex);
1077         }
1078
1079         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1080                 /*
1081                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
1082                  * We assume here that all the packets sent to the OFFCHANNEL
1083                  * queue are sent in ROC session.
1084                  */
1085                 flush_work(&mvm->roc_done_wk);
1086         } else {
1087                 /*
1088                  * By now, all the AC queues are empty. The AGG queues are
1089                  * empty too. We already got all the Tx responses for all the
1090                  * packets in the queues. The drain work can have been
1091                  * triggered. Flush it.
1092                  */
1093                 flush_work(&mvm->sta_drained_wk);
1094         }
1095 }
1096
1097 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1098                                          struct ieee80211_vif *vif)
1099 {
1100         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1101         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1102
1103         iwl_mvm_prepare_mac_removal(mvm, vif);
1104
1105         mutex_lock(&mvm->mutex);
1106
1107         if (mvm->bf_allowed_vif == mvmvif) {
1108                 mvm->bf_allowed_vif = NULL;
1109                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1110                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1111         }
1112
1113         iwl_mvm_vif_dbgfs_clean(mvm, vif);
1114
1115         /*
1116          * For AP/GO interface, the tear down of the resources allocated to the
1117          * interface is be handled as part of the stop_ap flow.
1118          */
1119         if (vif->type == NL80211_IFTYPE_AP ||
1120             vif->type == NL80211_IFTYPE_ADHOC) {
1121 #ifdef CONFIG_NL80211_TESTMODE
1122                 if (vif == mvm->noa_vif) {
1123                         mvm->noa_vif = NULL;
1124                         mvm->noa_duration = 0;
1125                 }
1126 #endif
1127                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
1128                 goto out_release;
1129         }
1130
1131         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1132                 mvm->p2p_device_vif = NULL;
1133                 iwl_mvm_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1134                 iwl_mvm_binding_remove_vif(mvm, vif);
1135                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1136                 mvmvif->phy_ctxt = NULL;
1137         }
1138
1139         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1140                 mvm->vif_count--;
1141
1142         iwl_mvm_power_update_mac(mvm);
1143         iwl_mvm_mac_ctxt_remove(mvm, vif);
1144
1145 out_release:
1146         iwl_mvm_mac_ctxt_release(mvm, vif);
1147         mutex_unlock(&mvm->mutex);
1148 }
1149
1150 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1151 {
1152         return 0;
1153 }
1154
1155 struct iwl_mvm_mc_iter_data {
1156         struct iwl_mvm *mvm;
1157         int port_id;
1158 };
1159
1160 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1161                                       struct ieee80211_vif *vif)
1162 {
1163         struct iwl_mvm_mc_iter_data *data = _data;
1164         struct iwl_mvm *mvm = data->mvm;
1165         struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1166         int ret, len;
1167
1168         /* if we don't have free ports, mcast frames will be dropped */
1169         if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1170                 return;
1171
1172         if (vif->type != NL80211_IFTYPE_STATION ||
1173             !vif->bss_conf.assoc)
1174                 return;
1175
1176         cmd->port_id = data->port_id++;
1177         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1178         len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1179
1180         ret = iwl_mvm_send_cmd_pdu(mvm, MCAST_FILTER_CMD, CMD_ASYNC, len, cmd);
1181         if (ret)
1182                 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1183 }
1184
1185 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1186 {
1187         struct iwl_mvm_mc_iter_data iter_data = {
1188                 .mvm = mvm,
1189         };
1190
1191         lockdep_assert_held(&mvm->mutex);
1192
1193         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1194                 return;
1195
1196         ieee80211_iterate_active_interfaces_atomic(
1197                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1198                 iwl_mvm_mc_iface_iterator, &iter_data);
1199 }
1200
1201 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1202                                      struct netdev_hw_addr_list *mc_list)
1203 {
1204         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1205         struct iwl_mcast_filter_cmd *cmd;
1206         struct netdev_hw_addr *addr;
1207         int addr_count = netdev_hw_addr_list_count(mc_list);
1208         bool pass_all = false;
1209         int len;
1210
1211         if (addr_count > MAX_MCAST_FILTERING_ADDRESSES) {
1212                 pass_all = true;
1213                 addr_count = 0;
1214         }
1215
1216         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1217         cmd = kzalloc(len, GFP_ATOMIC);
1218         if (!cmd)
1219                 return 0;
1220
1221         if (pass_all) {
1222                 cmd->pass_all = 1;
1223                 return (u64)(unsigned long)cmd;
1224         }
1225
1226         netdev_hw_addr_list_for_each(addr, mc_list) {
1227                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1228                                    cmd->count, addr->addr);
1229                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1230                        addr->addr, ETH_ALEN);
1231                 cmd->count++;
1232         }
1233
1234         return (u64)(unsigned long)cmd;
1235 }
1236
1237 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1238                                      unsigned int changed_flags,
1239                                      unsigned int *total_flags,
1240                                      u64 multicast)
1241 {
1242         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1243         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1244
1245         mutex_lock(&mvm->mutex);
1246
1247         /* replace previous configuration */
1248         kfree(mvm->mcast_filter_cmd);
1249         mvm->mcast_filter_cmd = cmd;
1250
1251         if (!cmd)
1252                 goto out;
1253
1254         iwl_mvm_recalc_multicast(mvm);
1255 out:
1256         mutex_unlock(&mvm->mutex);
1257         *total_flags = 0;
1258 }
1259
1260 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1261 struct iwl_bcast_iter_data {
1262         struct iwl_mvm *mvm;
1263         struct iwl_bcast_filter_cmd *cmd;
1264         u8 current_filter;
1265 };
1266
1267 static void
1268 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1269                          const struct iwl_fw_bcast_filter *in_filter,
1270                          struct iwl_fw_bcast_filter *out_filter)
1271 {
1272         struct iwl_fw_bcast_filter_attr *attr;
1273         int i;
1274
1275         memcpy(out_filter, in_filter, sizeof(*out_filter));
1276
1277         for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1278                 attr = &out_filter->attrs[i];
1279
1280                 if (!attr->mask)
1281                         break;
1282
1283                 switch (attr->reserved1) {
1284                 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1285                         if (vif->bss_conf.arp_addr_cnt != 1) {
1286                                 attr->mask = 0;
1287                                 continue;
1288                         }
1289
1290                         attr->val = vif->bss_conf.arp_addr_list[0];
1291                         break;
1292                 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1293                         attr->val = *(__be32 *)&vif->addr[2];
1294                         break;
1295                 default:
1296                         break;
1297                 }
1298                 attr->reserved1 = 0;
1299                 out_filter->num_attrs++;
1300         }
1301 }
1302
1303 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1304                                           struct ieee80211_vif *vif)
1305 {
1306         struct iwl_bcast_iter_data *data = _data;
1307         struct iwl_mvm *mvm = data->mvm;
1308         struct iwl_bcast_filter_cmd *cmd = data->cmd;
1309         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1310         struct iwl_fw_bcast_mac *bcast_mac;
1311         int i;
1312
1313         if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1314                 return;
1315
1316         bcast_mac = &cmd->macs[mvmvif->id];
1317
1318         /*
1319          * enable filtering only for associated stations, but not for P2P
1320          * Clients
1321          */
1322         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1323             !vif->bss_conf.assoc)
1324                 return;
1325
1326         bcast_mac->default_discard = 1;
1327
1328         /* copy all configured filters */
1329         for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1330                 /*
1331                  * Make sure we don't exceed our filters limit.
1332                  * if there is still a valid filter to be configured,
1333                  * be on the safe side and just allow bcast for this mac.
1334                  */
1335                 if (WARN_ON_ONCE(data->current_filter >=
1336                                  ARRAY_SIZE(cmd->filters))) {
1337                         bcast_mac->default_discard = 0;
1338                         bcast_mac->attached_filters = 0;
1339                         break;
1340                 }
1341
1342                 iwl_mvm_set_bcast_filter(vif,
1343                                          &mvm->bcast_filters[i],
1344                                          &cmd->filters[data->current_filter]);
1345
1346                 /* skip current filter if it contains no attributes */
1347                 if (!cmd->filters[data->current_filter].num_attrs)
1348                         continue;
1349
1350                 /* attach the filter to current mac */
1351                 bcast_mac->attached_filters |=
1352                                 cpu_to_le16(BIT(data->current_filter));
1353
1354                 data->current_filter++;
1355         }
1356 }
1357
1358 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1359                                     struct iwl_bcast_filter_cmd *cmd)
1360 {
1361         struct iwl_bcast_iter_data iter_data = {
1362                 .mvm = mvm,
1363                 .cmd = cmd,
1364         };
1365
1366         memset(cmd, 0, sizeof(*cmd));
1367         cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1368         cmd->max_macs = ARRAY_SIZE(cmd->macs);
1369
1370 #ifdef CONFIG_IWLWIFI_DEBUGFS
1371         /* use debugfs filters/macs if override is configured */
1372         if (mvm->dbgfs_bcast_filtering.override) {
1373                 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1374                        sizeof(cmd->filters));
1375                 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1376                        sizeof(cmd->macs));
1377                 return true;
1378         }
1379 #endif
1380
1381         /* if no filters are configured, do nothing */
1382         if (!mvm->bcast_filters)
1383                 return false;
1384
1385         /* configure and attach these filters for each associated sta vif */
1386         ieee80211_iterate_active_interfaces(
1387                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1388                 iwl_mvm_bcast_filter_iterator, &iter_data);
1389
1390         return true;
1391 }
1392 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1393                                           struct ieee80211_vif *vif)
1394 {
1395         struct iwl_bcast_filter_cmd cmd;
1396
1397         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1398                 return 0;
1399
1400         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1401                 return 0;
1402
1403         return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1404                                     sizeof(cmd), &cmd);
1405 }
1406 #else
1407 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm,
1408                                                  struct ieee80211_vif *vif)
1409 {
1410         return 0;
1411 }
1412 #endif
1413
1414 static void iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
1415 {
1416         struct ieee80211_sta *sta;
1417         struct iwl_mvm_sta *mvmsta;
1418         int i;
1419
1420         lockdep_assert_held(&mvm->mutex);
1421
1422         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1423                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1424                                                 lockdep_is_held(&mvm->mutex));
1425                 if (!sta || IS_ERR(sta) || !sta->tdls)
1426                         continue;
1427
1428                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1429                 ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
1430                                 NL80211_TDLS_TEARDOWN,
1431                                 WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
1432                                 GFP_KERNEL);
1433         }
1434 }
1435
1436 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
1437                                              struct ieee80211_vif *vif,
1438                                              struct ieee80211_bss_conf *bss_conf,
1439                                              u32 changes)
1440 {
1441         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1442         int ret;
1443
1444         /*
1445          * Re-calculate the tsf id, as the master-slave relations depend on the
1446          * beacon interval, which was not known when the station interface was
1447          * added.
1448          */
1449         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc)
1450                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1451
1452         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
1453         if (ret)
1454                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1455
1456         if (changes & BSS_CHANGED_ASSOC) {
1457                 if (bss_conf->assoc) {
1458                         /* add quota for this interface */
1459                         ret = iwl_mvm_update_quotas(mvm, NULL);
1460                         if (ret) {
1461                                 IWL_ERR(mvm, "failed to update quotas\n");
1462                                 return;
1463                         }
1464
1465                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
1466                                      &mvm->status)) {
1467                                 /*
1468                                  * If we're restarting then the firmware will
1469                                  * obviously have lost synchronisation with
1470                                  * the AP. It will attempt to synchronise by
1471                                  * itself, but we can make it more reliable by
1472                                  * scheduling a session protection time event.
1473                                  *
1474                                  * The firmware needs to receive a beacon to
1475                                  * catch up with synchronisation, use 110% of
1476                                  * the beacon interval.
1477                                  *
1478                                  * Set a large maximum delay to allow for more
1479                                  * than a single interface.
1480                                  */
1481                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
1482                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
1483                                                         5 * dur);
1484                         }
1485
1486                         iwl_mvm_sf_update(mvm, vif, false);
1487                         iwl_mvm_power_vif_assoc(mvm, vif);
1488                         if (vif->p2p) {
1489                                 iwl_mvm_ref(mvm, IWL_MVM_REF_P2P_CLIENT);
1490                                 iwl_mvm_update_smps(mvm, vif,
1491                                                     IWL_MVM_SMPS_REQ_PROT,
1492                                                     IEEE80211_SMPS_DYNAMIC);
1493                         }
1494                 } else if (mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1495                         /*
1496                          * If update fails - SF might be running in associated
1497                          * mode while disassociated - which is forbidden.
1498                          */
1499                         WARN_ONCE(iwl_mvm_sf_update(mvm, vif, false),
1500                                   "Failed to update SF upon disassociation\n");
1501
1502                         /* remove AP station now that the MAC is unassoc */
1503                         ret = iwl_mvm_rm_sta_id(mvm, vif, mvmvif->ap_sta_id);
1504                         if (ret)
1505                                 IWL_ERR(mvm, "failed to remove AP station\n");
1506
1507                         if (mvm->d0i3_ap_sta_id == mvmvif->ap_sta_id)
1508                                 mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
1509                         mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
1510                         /* remove quota for this interface */
1511                         ret = iwl_mvm_update_quotas(mvm, NULL);
1512                         if (ret)
1513                                 IWL_ERR(mvm, "failed to update quotas\n");
1514
1515                         if (vif->p2p)
1516                                 iwl_mvm_unref(mvm, IWL_MVM_REF_P2P_CLIENT);
1517                 }
1518
1519                 iwl_mvm_recalc_multicast(mvm);
1520                 iwl_mvm_configure_bcast_filter(mvm, vif);
1521
1522                 /* reset rssi values */
1523                 mvmvif->bf_data.ave_beacon_signal = 0;
1524
1525                 iwl_mvm_bt_coex_vif_change(mvm);
1526                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
1527                                     IEEE80211_SMPS_AUTOMATIC);
1528         } else if (changes & BSS_CHANGED_BEACON_INFO) {
1529                 /*
1530                  * We received a beacon _after_ association so
1531                  * remove the session protection.
1532                  */
1533                 iwl_mvm_remove_time_event(mvm, mvmvif,
1534                                           &mvmvif->time_event_data);
1535         } else if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS |
1536                               BSS_CHANGED_QOS)) {
1537                 ret = iwl_mvm_power_update_mac(mvm);
1538                 if (ret)
1539                         IWL_ERR(mvm, "failed to update power mode\n");
1540         }
1541
1542         if (changes & BSS_CHANGED_BEACON_INFO) {
1543                 iwl_mvm_sf_update(mvm, vif, false);
1544                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1545         }
1546
1547         if (changes & BSS_CHANGED_TXPOWER) {
1548                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
1549                                 bss_conf->txpower);
1550                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
1551         }
1552
1553         if (changes & BSS_CHANGED_CQM) {
1554                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
1555                 /* reset cqm events tracking */
1556                 mvmvif->bf_data.last_cqm_event = 0;
1557                 if (mvmvif->bf_data.bf_enabled) {
1558                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1559                         if (ret)
1560                                 IWL_ERR(mvm,
1561                                         "failed to update CQM thresholds\n");
1562                 }
1563         }
1564
1565         if (changes & BSS_CHANGED_ARP_FILTER) {
1566                 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
1567                 iwl_mvm_configure_bcast_filter(mvm, vif);
1568         }
1569 }
1570
1571 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
1572                                  struct ieee80211_vif *vif)
1573 {
1574         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1575         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1576         int ret;
1577
1578         /*
1579          * iwl_mvm_mac_ctxt_add() might read directly from the device
1580          * (the system time), so make sure it is available.
1581          */
1582         ret = iwl_mvm_ref_sync(mvm, IWL_MVM_REF_START_AP);
1583         if (ret)
1584                 return ret;
1585
1586         mutex_lock(&mvm->mutex);
1587
1588         /* Send the beacon template */
1589         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
1590         if (ret)
1591                 goto out_unlock;
1592
1593         /*
1594          * Re-calculate the tsf id, as the master-slave relations depend on the
1595          * beacon interval, which was not known when the AP interface was added.
1596          */
1597         if (vif->type == NL80211_IFTYPE_AP)
1598                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
1599
1600         /* Add the mac context */
1601         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1602         if (ret)
1603                 goto out_unlock;
1604
1605         /* Perform the binding */
1606         ret = iwl_mvm_binding_add_vif(mvm, vif);
1607         if (ret)
1608                 goto out_remove;
1609
1610         /* Send the bcast station. At this stage the TBTT and DTIM time events
1611          * are added and applied to the scheduler */
1612         ret = iwl_mvm_send_bcast_sta(mvm, vif, &mvmvif->bcast_sta);
1613         if (ret)
1614                 goto out_unbind;
1615
1616         /* must be set before quota calculations */
1617         mvmvif->ap_ibss_active = true;
1618
1619         /* power updated needs to be done before quotas */
1620         iwl_mvm_power_update_mac(mvm);
1621
1622         ret = iwl_mvm_update_quotas(mvm, NULL);
1623         if (ret)
1624                 goto out_quota_failed;
1625
1626         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1627         if (vif->p2p && mvm->p2p_device_vif)
1628                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
1629
1630         iwl_mvm_ref(mvm, IWL_MVM_REF_AP_IBSS);
1631
1632         iwl_mvm_bt_coex_vif_change(mvm);
1633
1634         /* we don't support TDLS during DCM */
1635         if (iwl_mvm_phy_ctx_count(mvm) > 1)
1636                 iwl_mvm_teardown_tdls_peers(mvm);
1637
1638         mutex_unlock(&mvm->mutex);
1639         return 0;
1640
1641 out_quota_failed:
1642         iwl_mvm_power_update_mac(mvm);
1643         mvmvif->ap_ibss_active = false;
1644         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1645 out_unbind:
1646         iwl_mvm_binding_remove_vif(mvm, vif);
1647 out_remove:
1648         iwl_mvm_mac_ctxt_remove(mvm, vif);
1649 out_unlock:
1650         mutex_unlock(&mvm->mutex);
1651         iwl_mvm_unref(mvm, IWL_MVM_REF_START_AP);
1652         return ret;
1653 }
1654
1655 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
1656                                  struct ieee80211_vif *vif)
1657 {
1658         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1659         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1660
1661         iwl_mvm_prepare_mac_removal(mvm, vif);
1662
1663         mutex_lock(&mvm->mutex);
1664
1665         /* Handle AP stop while in CSA */
1666         if (rcu_access_pointer(mvm->csa_vif) == vif) {
1667                 iwl_mvm_remove_time_event(mvm, mvmvif,
1668                                           &mvmvif->time_event_data);
1669                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1670         }
1671
1672         if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
1673                 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1674                 mvm->csa_tx_block_bcn_timeout = 0;
1675         }
1676
1677         mvmvif->ap_ibss_active = false;
1678         mvm->ap_last_beacon_gp2 = 0;
1679
1680         iwl_mvm_bt_coex_vif_change(mvm);
1681
1682         iwl_mvm_unref(mvm, IWL_MVM_REF_AP_IBSS);
1683
1684         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
1685         if (vif->p2p && mvm->p2p_device_vif)
1686                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false);
1687
1688         iwl_mvm_update_quotas(mvm, NULL);
1689         iwl_mvm_send_rm_bcast_sta(mvm, &mvmvif->bcast_sta);
1690         iwl_mvm_binding_remove_vif(mvm, vif);
1691
1692         iwl_mvm_power_update_mac(mvm);
1693
1694         iwl_mvm_mac_ctxt_remove(mvm, vif);
1695
1696         mutex_unlock(&mvm->mutex);
1697 }
1698
1699 static void
1700 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
1701                                  struct ieee80211_vif *vif,
1702                                  struct ieee80211_bss_conf *bss_conf,
1703                                  u32 changes)
1704 {
1705         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1706
1707         /* Changes will be applied when the AP/IBSS is started */
1708         if (!mvmvif->ap_ibss_active)
1709                 return;
1710
1711         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
1712                        BSS_CHANGED_BANDWIDTH) &&
1713             iwl_mvm_mac_ctxt_changed(mvm, vif, false))
1714                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
1715
1716         /* Need to send a new beacon template to the FW */
1717         if (changes & BSS_CHANGED_BEACON &&
1718             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
1719                 IWL_WARN(mvm, "Failed updating beacon data\n");
1720 }
1721
1722 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
1723                                      struct ieee80211_vif *vif,
1724                                      struct ieee80211_bss_conf *bss_conf,
1725                                      u32 changes)
1726 {
1727         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1728
1729         /*
1730          * iwl_mvm_bss_info_changed_station() might call
1731          * iwl_mvm_protect_session(), which reads directly from
1732          * the device (the system time), so make sure it is available.
1733          */
1734         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_BSS_CHANGED))
1735                 return;
1736
1737         mutex_lock(&mvm->mutex);
1738
1739         if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
1740                 iwl_mvm_scan_offload_stop(mvm, true);
1741
1742         switch (vif->type) {
1743         case NL80211_IFTYPE_STATION:
1744                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
1745                 break;
1746         case NL80211_IFTYPE_AP:
1747         case NL80211_IFTYPE_ADHOC:
1748                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
1749                 break;
1750         default:
1751                 /* shouldn't happen */
1752                 WARN_ON_ONCE(1);
1753         }
1754
1755         mutex_unlock(&mvm->mutex);
1756         iwl_mvm_unref(mvm, IWL_MVM_REF_BSS_CHANGED);
1757 }
1758
1759 static int iwl_mvm_cancel_scan_wait_notif(struct iwl_mvm *mvm,
1760                                           enum iwl_scan_status scan_type)
1761 {
1762         int ret;
1763         bool wait_for_handlers = false;
1764
1765         mutex_lock(&mvm->mutex);
1766
1767         if (mvm->scan_status != scan_type) {
1768                 ret = 0;
1769                 /* make sure there are no pending notifications */
1770                 wait_for_handlers = true;
1771                 goto out;
1772         }
1773
1774         switch (scan_type) {
1775         case IWL_MVM_SCAN_SCHED:
1776                 ret = iwl_mvm_scan_offload_stop(mvm, true);
1777                 break;
1778         case IWL_MVM_SCAN_OS:
1779                 ret = iwl_mvm_cancel_scan(mvm);
1780                 break;
1781         case IWL_MVM_SCAN_NONE:
1782         default:
1783                 WARN_ON_ONCE(1);
1784                 ret = -EINVAL;
1785                 break;
1786         }
1787         if (ret)
1788                 goto out;
1789
1790         wait_for_handlers = true;
1791 out:
1792         mutex_unlock(&mvm->mutex);
1793
1794         /* make sure we consume the completion notification */
1795         if (wait_for_handlers)
1796                 iwl_mvm_wait_for_async_handlers(mvm);
1797
1798         return ret;
1799 }
1800 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
1801                                struct ieee80211_vif *vif,
1802                                struct ieee80211_scan_request *hw_req)
1803 {
1804         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1805         struct cfg80211_scan_request *req = &hw_req->req;
1806         int ret;
1807
1808         if (req->n_channels == 0 ||
1809             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1810                 return -EINVAL;
1811
1812         ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_SCHED);
1813         if (ret)
1814                 return ret;
1815
1816         mutex_lock(&mvm->mutex);
1817
1818         if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
1819                 ret = -EBUSY;
1820                 goto out;
1821         }
1822
1823         iwl_mvm_ref(mvm, IWL_MVM_REF_SCAN);
1824
1825         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1826                 ret = iwl_mvm_unified_scan_lmac(mvm, vif, hw_req);
1827         else
1828                 ret = iwl_mvm_scan_request(mvm, vif, req);
1829
1830         if (ret)
1831                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1832 out:
1833         mutex_unlock(&mvm->mutex);
1834         return ret;
1835 }
1836
1837 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
1838                                        struct ieee80211_vif *vif)
1839 {
1840         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1841
1842         mutex_lock(&mvm->mutex);
1843
1844         iwl_mvm_cancel_scan(mvm);
1845
1846         mutex_unlock(&mvm->mutex);
1847 }
1848
1849 static void
1850 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
1851                                   struct ieee80211_sta *sta, u16 tids,
1852                                   int num_frames,
1853                                   enum ieee80211_frame_release_type reason,
1854                                   bool more_data)
1855 {
1856         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1857
1858         /* Called when we need to transmit (a) frame(s) from mac80211 */
1859
1860         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1861                                           tids, more_data, false);
1862 }
1863
1864 static void
1865 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
1866                                     struct ieee80211_sta *sta, u16 tids,
1867                                     int num_frames,
1868                                     enum ieee80211_frame_release_type reason,
1869                                     bool more_data)
1870 {
1871         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1872
1873         /* Called when we need to transmit (a) frame(s) from agg queue */
1874
1875         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
1876                                           tids, more_data, true);
1877 }
1878
1879 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
1880                                    struct ieee80211_vif *vif,
1881                                    enum sta_notify_cmd cmd,
1882                                    struct ieee80211_sta *sta)
1883 {
1884         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1885         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1886         int tid;
1887
1888         switch (cmd) {
1889         case STA_NOTIFY_SLEEP:
1890                 if (atomic_read(&mvm->pending_frames[mvmsta->sta_id]) > 0)
1891                         ieee80211_sta_block_awake(hw, sta, true);
1892                 spin_lock_bh(&mvmsta->lock);
1893                 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1894                         struct iwl_mvm_tid_data *tid_data;
1895
1896                         tid_data = &mvmsta->tid_data[tid];
1897                         if (tid_data->state != IWL_AGG_ON &&
1898                             tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA)
1899                                 continue;
1900                         if (iwl_mvm_tid_queued(tid_data) == 0)
1901                                 continue;
1902                         ieee80211_sta_set_buffered(sta, tid, true);
1903                 }
1904                 spin_unlock_bh(&mvmsta->lock);
1905                 /*
1906                  * The fw updates the STA to be asleep. Tx packets on the Tx
1907                  * queues to this station will not be transmitted. The fw will
1908                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
1909                  */
1910                 break;
1911         case STA_NOTIFY_AWAKE:
1912                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_STATION_COUNT))
1913                         break;
1914                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
1915                 break;
1916         default:
1917                 break;
1918         }
1919 }
1920
1921 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
1922                                        struct ieee80211_vif *vif,
1923                                        struct ieee80211_sta *sta)
1924 {
1925         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1926         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1927
1928         /*
1929          * This is called before mac80211 does RCU synchronisation,
1930          * so here we already invalidate our internal RCU-protected
1931          * station pointer. The rest of the code will thus no longer
1932          * be able to find the station this way, and we don't rely
1933          * on further RCU synchronisation after the sta_state()
1934          * callback deleted the station.
1935          */
1936         mutex_lock(&mvm->mutex);
1937         if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
1938                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
1939                                    ERR_PTR(-ENOENT));
1940         mutex_unlock(&mvm->mutex);
1941 }
1942
1943 int iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1944 {
1945         struct ieee80211_sta *sta;
1946         struct iwl_mvm_sta *mvmsta;
1947         int count = 0;
1948         int i;
1949
1950         lockdep_assert_held(&mvm->mutex);
1951
1952         for (i = 0; i < IWL_MVM_STATION_COUNT; i++) {
1953                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1954                                                 lockdep_is_held(&mvm->mutex));
1955                 if (!sta || IS_ERR(sta) || !sta->tdls)
1956                         continue;
1957
1958                 if (vif) {
1959                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
1960                         if (mvmsta->vif != vif)
1961                                 continue;
1962                 }
1963
1964                 count++;
1965         }
1966
1967         return count;
1968 }
1969
1970 static void iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm,
1971                                       struct ieee80211_vif *vif,
1972                                       bool sta_added)
1973 {
1974         int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
1975
1976         /*
1977          * Disable ps when the first TDLS sta is added and re-enable it
1978          * when the last TDLS sta is removed
1979          */
1980         if ((tdls_sta_cnt == 1 && sta_added) ||
1981             (tdls_sta_cnt == 0 && !sta_added))
1982                 iwl_mvm_power_update_mac(mvm);
1983 }
1984
1985 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
1986                                  struct ieee80211_vif *vif,
1987                                  struct ieee80211_sta *sta,
1988                                  enum ieee80211_sta_state old_state,
1989                                  enum ieee80211_sta_state new_state)
1990 {
1991         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1992         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1993         int ret;
1994
1995         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
1996                            sta->addr, old_state, new_state);
1997
1998         /* this would be a mac80211 bug ... but don't crash */
1999         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
2000                 return -EINVAL;
2001
2002         /* if a STA is being removed, reuse its ID */
2003         flush_work(&mvm->sta_drained_wk);
2004
2005         mutex_lock(&mvm->mutex);
2006         if (old_state == IEEE80211_STA_NOTEXIST &&
2007             new_state == IEEE80211_STA_NONE) {
2008                 /*
2009                  * Firmware bug - it'll crash if the beacon interval is less
2010                  * than 16. We can't avoid connecting at all, so refuse the
2011                  * station state change, this will cause mac80211 to abandon
2012                  * attempts to connect to this AP, and eventually wpa_s will
2013                  * blacklist the AP...
2014                  */
2015                 if (vif->type == NL80211_IFTYPE_STATION &&
2016                     vif->bss_conf.beacon_int < 16) {
2017                         IWL_ERR(mvm,
2018                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
2019                                 sta->addr, vif->bss_conf.beacon_int);
2020                         ret = -EINVAL;
2021                         goto out_unlock;
2022                 }
2023
2024                 if (sta->tdls &&
2025                     (vif->p2p ||
2026                      iwl_mvm_tdls_sta_count(mvm, NULL) ==
2027                                                 IWL_MVM_TDLS_STA_COUNT ||
2028                      iwl_mvm_phy_ctx_count(mvm) > 1)) {
2029                         IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
2030                         ret = -EBUSY;
2031                         goto out_unlock;
2032                 }
2033
2034                 ret = iwl_mvm_add_sta(mvm, vif, sta);
2035                 if (sta->tdls && ret == 0)
2036                         iwl_mvm_recalc_tdls_state(mvm, vif, true);
2037         } else if (old_state == IEEE80211_STA_NONE &&
2038                    new_state == IEEE80211_STA_AUTH) {
2039                 /*
2040                  * EBS may be disabled due to previous failures reported by FW.
2041                  * Reset EBS status here assuming environment has been changed.
2042                  */
2043                 mvm->last_ebs_successful = true;
2044                 ret = 0;
2045         } else if (old_state == IEEE80211_STA_AUTH &&
2046                    new_state == IEEE80211_STA_ASSOC) {
2047                 ret = iwl_mvm_update_sta(mvm, vif, sta);
2048                 if (ret == 0)
2049                         iwl_mvm_rs_rate_init(mvm, sta,
2050                                              mvmvif->phy_ctxt->channel->band,
2051                                              true);
2052         } else if (old_state == IEEE80211_STA_ASSOC &&
2053                    new_state == IEEE80211_STA_AUTHORIZED) {
2054
2055                 /* we don't support TDLS during DCM */
2056                 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2057                         iwl_mvm_teardown_tdls_peers(mvm);
2058
2059                 /* enable beacon filtering */
2060                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2061                 ret = 0;
2062         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
2063                    new_state == IEEE80211_STA_ASSOC) {
2064                 /* disable beacon filtering */
2065                 WARN_ON(iwl_mvm_disable_beacon_filter(mvm, vif, 0));
2066                 ret = 0;
2067         } else if (old_state == IEEE80211_STA_ASSOC &&
2068                    new_state == IEEE80211_STA_AUTH) {
2069                 ret = 0;
2070         } else if (old_state == IEEE80211_STA_AUTH &&
2071                    new_state == IEEE80211_STA_NONE) {
2072                 ret = 0;
2073         } else if (old_state == IEEE80211_STA_NONE &&
2074                    new_state == IEEE80211_STA_NOTEXIST) {
2075                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
2076                 if (sta->tdls)
2077                         iwl_mvm_recalc_tdls_state(mvm, vif, false);
2078         } else {
2079                 ret = -EIO;
2080         }
2081  out_unlock:
2082         mutex_unlock(&mvm->mutex);
2083
2084         return ret;
2085 }
2086
2087 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
2088 {
2089         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2090
2091         mvm->rts_threshold = value;
2092
2093         return 0;
2094 }
2095
2096 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
2097                                   struct ieee80211_vif *vif,
2098                                   struct ieee80211_sta *sta, u32 changed)
2099 {
2100         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2101
2102         if (vif->type == NL80211_IFTYPE_STATION &&
2103             changed & IEEE80211_RC_NSS_CHANGED)
2104                 iwl_mvm_sf_update(mvm, vif, false);
2105 }
2106
2107 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
2108                                struct ieee80211_vif *vif, u16 ac,
2109                                const struct ieee80211_tx_queue_params *params)
2110 {
2111         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2112         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2113
2114         mvmvif->queue_params[ac] = *params;
2115
2116         /*
2117          * No need to update right away, we'll get BSS_CHANGED_QOS
2118          * The exception is P2P_DEVICE interface which needs immediate update.
2119          */
2120         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2121                 int ret;
2122
2123                 mutex_lock(&mvm->mutex);
2124                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false);
2125                 mutex_unlock(&mvm->mutex);
2126                 return ret;
2127         }
2128         return 0;
2129 }
2130
2131 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
2132                                       struct ieee80211_vif *vif)
2133 {
2134         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2135         u32 duration = min(IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS,
2136                            200 + vif->bss_conf.beacon_int);
2137         u32 min_duration = min(IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS,
2138                                100 + vif->bss_conf.beacon_int);
2139
2140         if (WARN_ON_ONCE(vif->bss_conf.assoc))
2141                 return;
2142
2143         /*
2144          * iwl_mvm_protect_session() reads directly from the device
2145          * (the system time), so make sure it is available.
2146          */
2147         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PREPARE_TX))
2148                 return;
2149
2150         mutex_lock(&mvm->mutex);
2151         /* Try really hard to protect the session and hear a beacon */
2152         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500);
2153         mutex_unlock(&mvm->mutex);
2154
2155         iwl_mvm_unref(mvm, IWL_MVM_REF_PREPARE_TX);
2156 }
2157
2158 static void iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
2159                                                   struct ieee80211_vif *vif)
2160 {
2161         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2162         u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
2163
2164         /*
2165          * iwl_mvm_protect_session() reads directly from the device
2166          * (the system time), so make sure it is available.
2167          */
2168         if (iwl_mvm_ref_sync(mvm, IWL_MVM_REF_PROTECT_TDLS))
2169                 return;
2170
2171         mutex_lock(&mvm->mutex);
2172         /* Protect the session to hear the TDLS setup response on the channel */
2173         iwl_mvm_protect_session(mvm, vif, duration, duration, 100);
2174         mutex_unlock(&mvm->mutex);
2175
2176         iwl_mvm_unref(mvm, IWL_MVM_REF_PROTECT_TDLS);
2177 }
2178
2179 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
2180                                         struct ieee80211_vif *vif,
2181                                         struct cfg80211_sched_scan_request *req,
2182                                         struct ieee80211_scan_ies *ies)
2183 {
2184         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2185         int ret;
2186
2187         ret = iwl_mvm_cancel_scan_wait_notif(mvm, IWL_MVM_SCAN_OS);
2188         if (ret)
2189                 return ret;
2190
2191         mutex_lock(&mvm->mutex);
2192
2193         if (!iwl_mvm_is_idle(mvm)) {
2194                 ret = -EBUSY;
2195                 goto out;
2196         }
2197
2198         if (mvm->scan_status != IWL_MVM_SCAN_NONE) {
2199                 ret = -EBUSY;
2200                 goto out;
2201         }
2202
2203         mvm->scan_status = IWL_MVM_SCAN_SCHED;
2204
2205         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
2206                 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
2207                 if (ret)
2208                         goto err;
2209         }
2210
2211         ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
2212         if (ret)
2213                 goto err;
2214
2215         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
2216                 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
2217         else
2218                 ret = iwl_mvm_sched_scan_start(mvm, req);
2219
2220         if (!ret)
2221                 goto out;
2222 err:
2223         mvm->scan_status = IWL_MVM_SCAN_NONE;
2224 out:
2225         mutex_unlock(&mvm->mutex);
2226         return ret;
2227 }
2228
2229 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
2230                                        struct ieee80211_vif *vif)
2231 {
2232         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2233         int ret;
2234
2235         mutex_lock(&mvm->mutex);
2236         ret = iwl_mvm_scan_offload_stop(mvm, false);
2237         mutex_unlock(&mvm->mutex);
2238         iwl_mvm_wait_for_async_handlers(mvm);
2239
2240         return ret;
2241 }
2242
2243 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
2244                                enum set_key_cmd cmd,
2245                                struct ieee80211_vif *vif,
2246                                struct ieee80211_sta *sta,
2247                                struct ieee80211_key_conf *key)
2248 {
2249         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2250         int ret;
2251
2252         if (iwlwifi_mod_params.sw_crypto) {
2253                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
2254                 return -EOPNOTSUPP;
2255         }
2256
2257         switch (key->cipher) {
2258         case WLAN_CIPHER_SUITE_TKIP:
2259                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2260                 /* fall-through */
2261         case WLAN_CIPHER_SUITE_CCMP:
2262                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2263                 break;
2264         case WLAN_CIPHER_SUITE_AES_CMAC:
2265                 WARN_ON_ONCE(!(hw->flags & IEEE80211_HW_MFP_CAPABLE));
2266                 break;
2267         case WLAN_CIPHER_SUITE_WEP40:
2268         case WLAN_CIPHER_SUITE_WEP104:
2269                 /*
2270                  * Support for TX only, at least for now, so accept
2271                  * the key and do nothing else. Then mac80211 will
2272                  * pass it for TX but we don't have to use it for RX.
2273                  */
2274                 return 0;
2275         default:
2276                 /* currently FW supports only one optional cipher scheme */
2277                 if (hw->n_cipher_schemes &&
2278                     hw->cipher_schemes->cipher == key->cipher)
2279                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
2280                 else
2281                         return -EOPNOTSUPP;
2282         }
2283
2284         mutex_lock(&mvm->mutex);
2285
2286         switch (cmd) {
2287         case SET_KEY:
2288                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
2289                      vif->type == NL80211_IFTYPE_AP) && !sta) {
2290                         /*
2291                          * GTK on AP interface is a TX-only key, return 0;
2292                          * on IBSS they're per-station and because we're lazy
2293                          * we don't support them for RX, so do the same.
2294                          */
2295                         ret = 0;
2296                         key->hw_key_idx = STA_KEY_IDX_INVALID;
2297                         break;
2298                 }
2299
2300                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
2301                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, false);
2302                 if (ret) {
2303                         IWL_WARN(mvm, "set key failed\n");
2304                         /*
2305                          * can't add key for RX, but we don't need it
2306                          * in the device for TX so still return 0
2307                          */
2308                         key->hw_key_idx = STA_KEY_IDX_INVALID;
2309                         ret = 0;
2310                 }
2311
2312                 break;
2313         case DISABLE_KEY:
2314                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
2315                         ret = 0;
2316                         break;
2317                 }
2318
2319                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
2320                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
2321                 break;
2322         default:
2323                 ret = -EINVAL;
2324         }
2325
2326         mutex_unlock(&mvm->mutex);
2327         return ret;
2328 }
2329
2330 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
2331                                         struct ieee80211_vif *vif,
2332                                         struct ieee80211_key_conf *keyconf,
2333                                         struct ieee80211_sta *sta,
2334                                         u32 iv32, u16 *phase1key)
2335 {
2336         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2337
2338         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
2339                 return;
2340
2341         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
2342 }
2343
2344
2345 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
2346                                struct iwl_rx_packet *pkt, void *data)
2347 {
2348         struct iwl_mvm *mvm =
2349                 container_of(notif_wait, struct iwl_mvm, notif_wait);
2350         struct iwl_hs20_roc_res *resp;
2351         int resp_len = iwl_rx_packet_payload_len(pkt);
2352         struct iwl_mvm_time_event_data *te_data = data;
2353
2354         if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
2355                 return true;
2356
2357         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
2358                 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
2359                 return true;
2360         }
2361
2362         resp = (void *)pkt->data;
2363
2364         IWL_DEBUG_TE(mvm,
2365                      "Aux ROC: Recieved response from ucode: status=%d uid=%d\n",
2366                      resp->status, resp->event_unique_id);
2367
2368         te_data->uid = le32_to_cpu(resp->event_unique_id);
2369         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
2370                      te_data->uid);
2371
2372         spin_lock_bh(&mvm->time_event_lock);
2373         list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
2374         spin_unlock_bh(&mvm->time_event_lock);
2375
2376         return true;
2377 }
2378
2379 #define AUX_ROC_MAX_DELAY_ON_CHANNEL 5000
2380 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
2381                                     struct ieee80211_channel *channel,
2382                                     struct ieee80211_vif *vif,
2383                                     int duration)
2384 {
2385         int res, time_reg = DEVICE_SYSTEM_TIME_REG;
2386         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2387         struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
2388         static const u8 time_event_response[] = { HOT_SPOT_CMD };
2389         struct iwl_notification_wait wait_time_event;
2390         struct iwl_hs20_roc_req aux_roc_req = {
2391                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
2392                 .id_and_color =
2393                         cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
2394                 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
2395                 /* Set the channel info data */
2396                 .channel_info.band = (channel->band == IEEE80211_BAND_2GHZ) ?
2397                         PHY_BAND_24 : PHY_BAND_5,
2398                 .channel_info.channel = channel->hw_value,
2399                 .channel_info.width = PHY_VHT_CHANNEL_MODE20,
2400                 /* Set the time and duration */
2401                 .apply_time = cpu_to_le32(iwl_read_prph(mvm->trans, time_reg)),
2402                 .apply_time_max_delay =
2403                         cpu_to_le32(MSEC_TO_TU(AUX_ROC_MAX_DELAY_ON_CHANNEL)),
2404                 .duration = cpu_to_le32(MSEC_TO_TU(duration)),
2405          };
2406
2407         /* Set the node address */
2408         memcpy(aux_roc_req.node_addr, vif->addr, ETH_ALEN);
2409
2410         te_data->vif = vif;
2411         te_data->duration = duration;
2412         te_data->id = HOT_SPOT_CMD;
2413
2414         lockdep_assert_held(&mvm->mutex);
2415
2416         spin_lock_bh(&mvm->time_event_lock);
2417         list_add_tail(&te_data->list, &mvm->time_event_list);
2418         spin_unlock_bh(&mvm->time_event_lock);
2419
2420         /*
2421          * Use a notification wait, which really just processes the
2422          * command response and doesn't wait for anything, in order
2423          * to be able to process the response and get the UID inside
2424          * the RX path. Using CMD_WANT_SKB doesn't work because it
2425          * stores the buffer and then wakes up this thread, by which
2426          * time another notification (that the time event started)
2427          * might already be processed unsuccessfully.
2428          */
2429         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
2430                                    time_event_response,
2431                                    ARRAY_SIZE(time_event_response),
2432                                    iwl_mvm_rx_aux_roc, te_data);
2433
2434         res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, sizeof(aux_roc_req),
2435                                    &aux_roc_req);
2436
2437         if (res) {
2438                 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
2439                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
2440                 goto out_clear_te;
2441         }
2442
2443         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
2444         res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
2445         /* should never fail */
2446         WARN_ON_ONCE(res);
2447
2448         if (res) {
2449  out_clear_te:
2450                 spin_lock_bh(&mvm->time_event_lock);
2451                 iwl_mvm_te_clear_data(mvm, te_data);
2452                 spin_unlock_bh(&mvm->time_event_lock);
2453         }
2454
2455         return res;
2456 }
2457
2458 static int iwl_mvm_roc(struct ieee80211_hw *hw,
2459                        struct ieee80211_vif *vif,
2460                        struct ieee80211_channel *channel,
2461                        int duration,
2462                        enum ieee80211_roc_type type)
2463 {
2464         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2465         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2466         struct cfg80211_chan_def chandef;
2467         struct iwl_mvm_phy_ctxt *phy_ctxt;
2468         int ret, i;
2469
2470         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
2471                            duration, type);
2472
2473         switch (vif->type) {
2474         case NL80211_IFTYPE_STATION:
2475                 /* Use aux roc framework (HS20) */
2476                 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
2477                                                vif, duration);
2478                 return ret;
2479         case NL80211_IFTYPE_P2P_DEVICE:
2480                 /* handle below */
2481                 break;
2482         default:
2483                 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
2484                 return -EINVAL;
2485         }
2486
2487         mutex_lock(&mvm->mutex);
2488
2489         for (i = 0; i < NUM_PHY_CTX; i++) {
2490                 phy_ctxt = &mvm->phy_ctxts[i];
2491                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
2492                         continue;
2493
2494                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
2495                         /*
2496                          * Unbind the P2P_DEVICE from the current PHY context,
2497                          * and if the PHY context is not used remove it.
2498                          */
2499                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
2500                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2501                                 goto out_unlock;
2502
2503                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2504
2505                         /* Bind the P2P_DEVICE to the current PHY Context */
2506                         mvmvif->phy_ctxt = phy_ctxt;
2507
2508                         ret = iwl_mvm_binding_add_vif(mvm, vif);
2509                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2510                                 goto out_unlock;
2511
2512                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2513                         goto schedule_time_event;
2514                 }
2515         }
2516
2517         /* Need to update the PHY context only if the ROC channel changed */
2518         if (channel == mvmvif->phy_ctxt->channel)
2519                 goto schedule_time_event;
2520
2521         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
2522
2523         /*
2524          * Change the PHY context configuration as it is currently referenced
2525          * only by the P2P Device MAC
2526          */
2527         if (mvmvif->phy_ctxt->ref == 1) {
2528                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
2529                                                &chandef, 1, 1);
2530                 if (ret)
2531                         goto out_unlock;
2532         } else {
2533                 /*
2534                  * The PHY context is shared with other MACs. Need to remove the
2535                  * P2P Device from the binding, allocate an new PHY context and
2536                  * create a new binding
2537                  */
2538                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2539                 if (!phy_ctxt) {
2540                         ret = -ENOSPC;
2541                         goto out_unlock;
2542                 }
2543
2544                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
2545                                                1, 1);
2546                 if (ret) {
2547                         IWL_ERR(mvm, "Failed to change PHY context\n");
2548                         goto out_unlock;
2549                 }
2550
2551                 /* Unbind the P2P_DEVICE from the current PHY context */
2552                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
2553                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
2554                         goto out_unlock;
2555
2556                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
2557
2558                 /* Bind the P2P_DEVICE to the new allocated PHY context */
2559                 mvmvif->phy_ctxt = phy_ctxt;
2560
2561                 ret = iwl_mvm_binding_add_vif(mvm, vif);
2562                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
2563                         goto out_unlock;
2564
2565                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
2566         }
2567
2568 schedule_time_event:
2569         /* Schedule the time events */
2570         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
2571
2572 out_unlock:
2573         mutex_unlock(&mvm->mutex);
2574         IWL_DEBUG_MAC80211(mvm, "leave\n");
2575         return ret;
2576 }
2577
2578 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw)
2579 {
2580         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2581
2582         IWL_DEBUG_MAC80211(mvm, "enter\n");
2583
2584         mutex_lock(&mvm->mutex);
2585         iwl_mvm_stop_p2p_roc(mvm);
2586         mutex_unlock(&mvm->mutex);
2587
2588         IWL_DEBUG_MAC80211(mvm, "leave\n");
2589         return 0;
2590 }
2591
2592 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
2593                                  struct ieee80211_chanctx_conf *ctx)
2594 {
2595         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2596         struct iwl_mvm_phy_ctxt *phy_ctxt;
2597         int ret;
2598
2599         lockdep_assert_held(&mvm->mutex);
2600
2601         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
2602
2603         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
2604         if (!phy_ctxt) {
2605                 ret = -ENOSPC;
2606                 goto out;
2607         }
2608
2609         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2610                                        ctx->rx_chains_static,
2611                                        ctx->rx_chains_dynamic);
2612         if (ret) {
2613                 IWL_ERR(mvm, "Failed to add PHY context\n");
2614                 goto out;
2615         }
2616
2617         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
2618         *phy_ctxt_id = phy_ctxt->id;
2619 out:
2620         return ret;
2621 }
2622
2623 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
2624                                struct ieee80211_chanctx_conf *ctx)
2625 {
2626         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2627         int ret;
2628
2629         mutex_lock(&mvm->mutex);
2630         ret = __iwl_mvm_add_chanctx(mvm, ctx);
2631         mutex_unlock(&mvm->mutex);
2632
2633         return ret;
2634 }
2635
2636 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
2637                                      struct ieee80211_chanctx_conf *ctx)
2638 {
2639         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2640         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2641
2642         lockdep_assert_held(&mvm->mutex);
2643
2644         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
2645 }
2646
2647 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
2648                                    struct ieee80211_chanctx_conf *ctx)
2649 {
2650         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2651
2652         mutex_lock(&mvm->mutex);
2653         __iwl_mvm_remove_chanctx(mvm, ctx);
2654         mutex_unlock(&mvm->mutex);
2655 }
2656
2657 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
2658                                    struct ieee80211_chanctx_conf *ctx,
2659                                    u32 changed)
2660 {
2661         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2662         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2663         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2664
2665         if (WARN_ONCE((phy_ctxt->ref > 1) &&
2666                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
2667                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2668                                    IEEE80211_CHANCTX_CHANGE_RADAR |
2669                                    IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
2670                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
2671                       phy_ctxt->ref, changed))
2672                 return;
2673
2674         mutex_lock(&mvm->mutex);
2675         iwl_mvm_bt_coex_vif_change(mvm);
2676         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &ctx->min_def,
2677                                  ctx->rx_chains_static,
2678                                  ctx->rx_chains_dynamic);
2679         mutex_unlock(&mvm->mutex);
2680 }
2681
2682 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
2683                                         struct ieee80211_vif *vif,
2684                                         struct ieee80211_chanctx_conf *ctx,
2685                                         bool switching_chanctx)
2686 {
2687         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
2688         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
2689         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2690         int ret;
2691
2692         lockdep_assert_held(&mvm->mutex);
2693
2694         mvmvif->phy_ctxt = phy_ctxt;
2695
2696         switch (vif->type) {
2697         case NL80211_IFTYPE_AP:
2698                 /* Unless it's a CSA flow we have nothing to do here */
2699                 if (vif->csa_active) {
2700                         mvmvif->ap_ibss_active = true;
2701                         break;
2702                 }
2703         case NL80211_IFTYPE_ADHOC:
2704                 /*
2705                  * The AP binding flow is handled as part of the start_ap flow
2706                  * (in bss_info_changed), similarly for IBSS.
2707                  */
2708                 ret = 0;
2709                 goto out;
2710         case NL80211_IFTYPE_STATION:
2711                 break;
2712         case NL80211_IFTYPE_MONITOR:
2713                 /* always disable PS when a monitor interface is active */
2714                 mvmvif->ps_disabled = true;
2715                 break;
2716         default:
2717                 ret = -EINVAL;
2718                 goto out;
2719         }
2720
2721         ret = iwl_mvm_binding_add_vif(mvm, vif);
2722         if (ret)
2723                 goto out;
2724
2725         /*
2726          * Power state must be updated before quotas,
2727          * otherwise fw will complain.
2728          */
2729         iwl_mvm_power_update_mac(mvm);
2730
2731         /* Setting the quota at this stage is only required for monitor
2732          * interfaces. For the other types, the bss_info changed flow
2733          * will handle quota settings.
2734          */
2735         if (vif->type == NL80211_IFTYPE_MONITOR) {
2736                 mvmvif->monitor_active = true;
2737                 ret = iwl_mvm_update_quotas(mvm, NULL);
2738                 if (ret)
2739                         goto out_remove_binding;
2740         }
2741
2742         /* Handle binding during CSA */
2743         if ((vif->type == NL80211_IFTYPE_AP) ||
2744             (switching_chanctx && (vif->type == NL80211_IFTYPE_STATION))) {
2745                 iwl_mvm_update_quotas(mvm, NULL);
2746                 iwl_mvm_mac_ctxt_changed(mvm, vif, false);
2747         }
2748
2749         goto out;
2750
2751 out_remove_binding:
2752         iwl_mvm_binding_remove_vif(mvm, vif);
2753         iwl_mvm_power_update_mac(mvm);
2754 out:
2755         if (ret)
2756                 mvmvif->phy_ctxt = NULL;
2757         return ret;
2758 }
2759 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
2760                                       struct ieee80211_vif *vif,
2761                                       struct ieee80211_chanctx_conf *ctx)
2762 {
2763         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2764         int ret;
2765
2766         mutex_lock(&mvm->mutex);
2767         ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
2768         mutex_unlock(&mvm->mutex);
2769
2770         return ret;
2771 }
2772
2773 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
2774                                            struct ieee80211_vif *vif,
2775                                            struct ieee80211_chanctx_conf *ctx,
2776                                            bool switching_chanctx)
2777 {
2778         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2779         struct ieee80211_vif *disabled_vif = NULL;
2780
2781         lockdep_assert_held(&mvm->mutex);
2782
2783         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
2784
2785         switch (vif->type) {
2786         case NL80211_IFTYPE_ADHOC:
2787                 goto out;
2788         case NL80211_IFTYPE_MONITOR:
2789                 mvmvif->monitor_active = false;
2790                 mvmvif->ps_disabled = false;
2791                 break;
2792         case NL80211_IFTYPE_AP:
2793                 /* This part is triggered only during CSA */
2794                 if (!vif->csa_active || !mvmvif->ap_ibss_active)
2795                         goto out;
2796
2797                 /* Set CS bit on all the stations */
2798                 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
2799
2800                 /* Save blocked iface, the timeout is set on the next beacon */
2801                 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
2802
2803                 mvmvif->ap_ibss_active = false;
2804                 break;
2805         case NL80211_IFTYPE_STATION:
2806                 if (!switching_chanctx)
2807                         break;
2808
2809                 disabled_vif = vif;
2810
2811                 iwl_mvm_mac_ctxt_changed(mvm, vif, true);
2812                 break;
2813         default:
2814                 break;
2815         }
2816
2817         iwl_mvm_update_quotas(mvm, disabled_vif);
2818         iwl_mvm_binding_remove_vif(mvm, vif);
2819
2820 out:
2821         mvmvif->phy_ctxt = NULL;
2822         iwl_mvm_power_update_mac(mvm);
2823 }
2824
2825 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
2826                                          struct ieee80211_vif *vif,
2827                                          struct ieee80211_chanctx_conf *ctx)
2828 {
2829         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2830
2831         mutex_lock(&mvm->mutex);
2832         __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
2833         mutex_unlock(&mvm->mutex);
2834 }
2835
2836 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
2837                                       struct ieee80211_vif_chanctx_switch *vifs,
2838                                       int n_vifs,
2839                                       enum ieee80211_chanctx_switch_mode mode)
2840 {
2841         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2842         int ret;
2843
2844         /* we only support SWAP_CONTEXTS and with a single-vif right now */
2845         if (mode != CHANCTX_SWMODE_SWAP_CONTEXTS || n_vifs > 1)
2846                 return -EOPNOTSUPP;
2847
2848         mutex_lock(&mvm->mutex);
2849         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
2850         __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
2851
2852         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
2853         if (ret) {
2854                 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
2855                 goto out_reassign;
2856         }
2857
2858         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
2859                                            true);
2860         if (ret) {
2861                 IWL_ERR(mvm,
2862                         "failed to assign new_ctx during channel switch\n");
2863                 goto out_remove;
2864         }
2865
2866         /* we don't support TDLS during DCM - can be caused by channel switch */
2867         if (iwl_mvm_phy_ctx_count(mvm) > 1)
2868                 iwl_mvm_teardown_tdls_peers(mvm);
2869
2870         goto out;
2871
2872 out_remove:
2873         __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
2874
2875 out_reassign:
2876         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx);
2877         if (ret) {
2878                 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
2879                 goto out_restart;
2880         }
2881
2882         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
2883                                            true);
2884         if (ret) {
2885                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
2886                 goto out_restart;
2887         }
2888
2889         goto out;
2890
2891 out_restart:
2892         /* things keep failing, better restart the hw */
2893         iwl_mvm_nic_restart(mvm, false);
2894
2895 out:
2896         mutex_unlock(&mvm->mutex);
2897         return ret;
2898 }
2899
2900 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
2901                            struct ieee80211_sta *sta,
2902                            bool set)
2903 {
2904         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2905         struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
2906
2907         if (!mvm_sta || !mvm_sta->vif) {
2908                 IWL_ERR(mvm, "Station is not associated to a vif\n");
2909                 return -EINVAL;
2910         }
2911
2912         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
2913 }
2914
2915 #ifdef CONFIG_NL80211_TESTMODE
2916 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
2917         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
2918         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
2919         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
2920 };
2921
2922 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
2923                                       struct ieee80211_vif *vif,
2924                                       void *data, int len)
2925 {
2926         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
2927         int err;
2928         u32 noa_duration;
2929
2930         err = nla_parse(tb, IWL_MVM_TM_ATTR_MAX, data, len, iwl_mvm_tm_policy);
2931         if (err)
2932                 return err;
2933
2934         if (!tb[IWL_MVM_TM_ATTR_CMD])
2935                 return -EINVAL;
2936
2937         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
2938         case IWL_MVM_TM_CMD_SET_NOA:
2939                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
2940                     !vif->bss_conf.enable_beacon ||
2941                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
2942                         return -EINVAL;
2943
2944                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
2945                 if (noa_duration >= vif->bss_conf.beacon_int)
2946                         return -EINVAL;
2947
2948                 mvm->noa_duration = noa_duration;
2949                 mvm->noa_vif = vif;
2950
2951                 return iwl_mvm_update_quotas(mvm, NULL);
2952         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
2953                 /* must be associated client vif - ignore authorized */
2954                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
2955                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
2956                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
2957                         return -EINVAL;
2958
2959                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
2960                         return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2961                 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
2962         }
2963
2964         return -EOPNOTSUPP;
2965 }
2966
2967 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
2968                                     struct ieee80211_vif *vif,
2969                                     void *data, int len)
2970 {
2971         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2972         int err;
2973
2974         mutex_lock(&mvm->mutex);
2975         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
2976         mutex_unlock(&mvm->mutex);
2977
2978         return err;
2979 }
2980 #endif
2981
2982 static void iwl_mvm_channel_switch_beacon(struct ieee80211_hw *hw,
2983                                           struct ieee80211_vif *vif,
2984                                           struct cfg80211_chan_def *chandef)
2985 {
2986         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2987         struct ieee80211_vif *csa_vif;
2988
2989         mutex_lock(&mvm->mutex);
2990
2991         csa_vif = rcu_dereference_protected(mvm->csa_vif,
2992                                             lockdep_is_held(&mvm->mutex));
2993         if (WARN(csa_vif && csa_vif->csa_active,
2994                  "Another CSA is already in progress"))
2995                 goto out_unlock;
2996
2997         IWL_DEBUG_MAC80211(mvm, "CSA started to freq %d\n",
2998                            chandef->center_freq1);
2999         rcu_assign_pointer(mvm->csa_vif, vif);
3000
3001 out_unlock:
3002         mutex_unlock(&mvm->mutex);
3003 }
3004
3005 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
3006                               struct ieee80211_vif *vif, u32 queues, bool drop)
3007 {
3008         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3009         struct iwl_mvm_vif *mvmvif;
3010         struct iwl_mvm_sta *mvmsta;
3011
3012         if (!vif || vif->type != NL80211_IFTYPE_STATION)
3013                 return;
3014
3015         mutex_lock(&mvm->mutex);
3016         mvmvif = iwl_mvm_vif_from_mac80211(vif);
3017         mvmsta = iwl_mvm_sta_from_staid_protected(mvm, mvmvif->ap_sta_id);
3018
3019         if (WARN_ON_ONCE(!mvmsta))
3020                 goto done;
3021
3022         if (drop) {
3023                 if (iwl_mvm_flush_tx_path(mvm, mvmsta->tfd_queue_msk, true))
3024                         IWL_ERR(mvm, "flush request fail\n");
3025         } else {
3026                 iwl_trans_wait_tx_queue_empty(mvm->trans,
3027                                               mvmsta->tfd_queue_msk);
3028         }
3029 done:
3030         mutex_unlock(&mvm->mutex);
3031 }
3032
3033 const struct ieee80211_ops iwl_mvm_hw_ops = {
3034         .tx = iwl_mvm_mac_tx,
3035         .ampdu_action = iwl_mvm_mac_ampdu_action,
3036         .start = iwl_mvm_mac_start,
3037         .restart_complete = iwl_mvm_mac_restart_complete,
3038         .stop = iwl_mvm_mac_stop,
3039         .add_interface = iwl_mvm_mac_add_interface,
3040         .remove_interface = iwl_mvm_mac_remove_interface,
3041         .config = iwl_mvm_mac_config,
3042         .prepare_multicast = iwl_mvm_prepare_multicast,
3043         .configure_filter = iwl_mvm_configure_filter,
3044         .bss_info_changed = iwl_mvm_bss_info_changed,
3045         .hw_scan = iwl_mvm_mac_hw_scan,
3046         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
3047         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
3048         .sta_state = iwl_mvm_mac_sta_state,
3049         .sta_notify = iwl_mvm_mac_sta_notify,
3050         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3051         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
3052         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
3053         .sta_rc_update = iwl_mvm_sta_rc_update,
3054         .conf_tx = iwl_mvm_mac_conf_tx,
3055         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
3056         .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
3057         .flush = iwl_mvm_mac_flush,
3058         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
3059         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
3060         .set_key = iwl_mvm_mac_set_key,
3061         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
3062         .remain_on_channel = iwl_mvm_roc,
3063         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
3064         .add_chanctx = iwl_mvm_add_chanctx,
3065         .remove_chanctx = iwl_mvm_remove_chanctx,
3066         .change_chanctx = iwl_mvm_change_chanctx,
3067         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
3068         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
3069         .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
3070
3071         .start_ap = iwl_mvm_start_ap_ibss,
3072         .stop_ap = iwl_mvm_stop_ap_ibss,
3073         .join_ibss = iwl_mvm_start_ap_ibss,
3074         .leave_ibss = iwl_mvm_stop_ap_ibss,
3075
3076         .set_tim = iwl_mvm_set_tim,
3077
3078         .channel_switch_beacon = iwl_mvm_channel_switch_beacon,
3079
3080         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
3081
3082 #ifdef CONFIG_PM_SLEEP
3083         /* look at d3.c */
3084         .suspend = iwl_mvm_suspend,
3085         .resume = iwl_mvm_resume,
3086         .set_wakeup = iwl_mvm_set_wakeup,
3087         .set_rekey_data = iwl_mvm_set_rekey_data,
3088 #if IS_ENABLED(CONFIG_IPV6)
3089         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
3090 #endif
3091         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
3092 #endif
3093 };